Skip to content

Commit

Permalink
precendence and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
James Cor committed Sep 18, 2023
1 parent 3317b7c commit c21ca8a
Show file tree
Hide file tree
Showing 3 changed files with 11,709 additions and 11,631 deletions.
68 changes: 68 additions & 0 deletions go/vt/sqlparser/precedence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,71 @@ func TestIsPrecedence(t *testing.T) {
}
}
}

func fmtSetOp(s SelectStatement) string {
switch s := s.(type) {
case *Union:
return fmt.Sprintf("(%s %s %s)", fmtSetOp(s.Left), s.Type, fmtSetOp(s.Right))
case *Select:
return String(s)
}
return ""
}

func TestSetOperatorPrecedence(t *testing.T) {
validSQL := []struct {
input string
output string
}{
{
input: "select 1 union select 2 union select 3 union select 4",
output: "(((select 1 union select 2) union select 3) union select 4)",
},
{
input: "select 1 intersect select 2 intersect select 3 intersect select 4",
output: "(((select 1 intersect select 2) intersect select 3) intersect select 4)",
},
{
input: "select 1 except select 2 except select 3 except select 4",
output: "(((select 1 except select 2) except select 3) except select 4)",
},

{
input: "select 1 union select 2 intersect select 3 except select 4",
output: "((select 1 union (select 2 intersect select 3)) except select 4)",
},
{
input: "select 1 union select 2 except select 3 intersect select 4",
output: "((select 1 union select 2) except (select 3 intersect select 4))",
},

{
input: "select 1 intersect select 2 union select 3 except select 4",
output: "(((select 1 intersect select 2) union select 3) except select 4)",
},
{
input: "select 1 intersect select 2 except select 3 union select 4",
output: "(((select 1 intersect select 2) except select 3) union select 4)",
},

{
input: "select 1 except select 2 intersect select 3 union select 4",
output: "((select 1 except (select 2 intersect select 3)) union select 4)",
},
{
input: "select 1 except select 2 union select 3 intersect select 4",
output: "((select 1 except select 2) union (select 3 intersect select 4))",
},
}
for _, tcase := range validSQL {
tree, err := Parse(tcase.input)
if err != nil {
t.Error(err)
continue
}
expr := fmtSetOp(tree.(SelectStatement))
if expr != tcase.output {
t.Errorf("Parse: \n%s, want: \n%s", expr, tcase.output)
}
}
}
Loading

0 comments on commit c21ca8a

Please sign in to comment.