Skip to content

Commit

Permalink
add ExtractedSubquery to the precedence logic
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <andres@planetscale.com>
  • Loading branch information
systay authored and frouioui committed Oct 12, 2021
1 parent 2483b11 commit f31d4c3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions go/vt/sqlparser/precedence.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ func precedenceFor(in Expr) Precendence {
}
case *IntervalExpr:
return P1
case *ExtractedSubquery:
return precedenceFor(node.Original)
}

return Syntactic
Expand Down
23 changes: 23 additions & 0 deletions go/vt/sqlparser/precedence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -65,6 +66,28 @@ func TestAndOrPrecedence(t *testing.T) {
}
}

func TestNotInSubqueryPrecedence(t *testing.T) {
tree, err := Parse("select * from a where not id in (select 42)")
require.NoError(t, err)
not := tree.(*Select).Where.Expr.(*NotExpr)
cmp := not.Expr.(*ComparisonExpr)
subq := cmp.Right.(*Subquery)
fmt.Println(subq)

extracted := &ExtractedSubquery{
Original: cmp,
ArgName: "arg1",
HasValuesArg: "has_values1",
OpCode: 1,
Subquery: subq.Select,
OtherSide: cmp.Left,
}
not.Expr = extracted

output := readable(not)
assert.Equal(t, "not (:has_values1 = 1 and id in ::arg1)", output)
}

func TestPlusStarPrecedence(t *testing.T) {
validSQL := []struct {
input string
Expand Down
2 changes: 1 addition & 1 deletion go/vt/sqlparser/tracked_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (buf *TrackedBuffer) formatter(node SQLNode) {
}
}

//needParens says if we need a parenthesis
// needParens says if we need a parenthesis
// op is the operator we are printing
// val is the value we are checking if we need parens around or not
// left let's us know if the value is on the lhs or rhs of the operator
Expand Down

0 comments on commit f31d4c3

Please sign in to comment.