Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2998,20 +2998,30 @@ func (node *ValuesFuncExpr) replace(from, to Expr) bool {
}

// SubstrExpr represents a call to SubstrExpr(column, value_expression) or SubstrExpr(column, value_expression,value_expression)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: column->column/val

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about it, but if I reuse ColName , e.g.:

SUBSTR openb STRING ',' value_expression closeb
  {
    $$ = &SubstrExpr{Name: &ColName{Name: NewColIdent($3)}, From: $5, To: nil}
  }

How could I figure out later if it's a static string or column name? How could I stop walkSubtree in this case?
Maybe instead of having extra StrVal *SQLVal, I'll add some boolean member (static/resolved/...) to mark it's a static string expression.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. You misunderstood :). I just wanted the SubstrExpr comment to be updated to say that the input can be a column or a value, to reflect the code change.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, how about now - take a look.

// also supported syntax SubstrExpr(column from value_expression for value_expression)
// also supported syntax SubstrExpr(column from value_expression for value_expression).
// Additionally to column names, SubstrExpr is also supported for string values, e.g.:
// SubstrExpr('static string value', value_expression, value_expression)
// In this case StrVal will be set instead of Name.
type SubstrExpr struct {
Name *ColName
From Expr
To Expr
Name *ColName
StrVal *SQLVal
From Expr
To Expr
}

// Format formats the node.
func (node *SubstrExpr) Format(buf *TrackedBuffer) {
var val interface{}
if node.Name != nil {
val = node.Name
} else {
val = node.StrVal
}

if node.To == nil {
buf.Myprintf("substr(%v, %v)", node.Name, node.From)
buf.Myprintf("substr(%v, %v)", val, node.From)
} else {
buf.Myprintf("substr(%v, %v, %v)", node.Name, node.From, node.To)
buf.Myprintf("substr(%v, %v, %v)", val, node.From, node.To)
}
}

Expand All @@ -3020,7 +3030,7 @@ func (node *SubstrExpr) replace(from, to Expr) bool {
}

func (node *SubstrExpr) walkSubtree(visit Visit) error {
if node == nil {
if node == nil || node.Name == nil {
return nil
}
return Walk(
Expand Down
8 changes: 7 additions & 1 deletion go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1749,7 +1749,7 @@ func TestSubStr(t *testing.T) {
input string
output string
}{{
input: "select substr(a, 1) from t",
input: `select substr('foobar', 1) from t`,
}, {
input: "select substr(a, 1, 6) from t",
}, {
Expand All @@ -1764,6 +1764,12 @@ func TestSubStr(t *testing.T) {
}, {
input: "select substring(a from 1 for 6) from t",
output: "select substr(a, 1, 6) from t",
}, {
input: `select substr("foo" from 1 for 2) from t`,
output: `select substr('foo', 1, 2) from t`,
}, {
input: `select substring("foo", 1, 2) from t`,
output: `select substr('foo', 1, 2) from t`,
}}

for _, tcase := range validSQL {
Expand Down
Loading