Skip to content

Commit

Permalink
added test for AndExpressions and fixed bug
Browse files Browse the repository at this point in the history
Signed-off-by: Harshit Gangal <harshit@planetscale.com>
  • Loading branch information
harshit-gangal committed Jun 23, 2021
1 parent 3f0a02b commit 7416b90
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
4 changes: 2 additions & 2 deletions go/vt/sqlparser/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,8 @@ func AndExpressions(exprs ...Expr) Expr {
result = expr
} else {
found := false
for j, ex := range exprs {
if i != j && EqualsExpr(expr, ex) {
for j := 0; j < i; j++ {
if EqualsExpr(expr, exprs[j]) {
found = true
break
}
Expand Down
70 changes: 70 additions & 0 deletions go/vt/sqlparser/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,76 @@ func TestSplitAndExpression(t *testing.T) {
}
}

func TestAndExpressions(t *testing.T) {
greaterThanExpr := &ComparisonExpr{
Operator: GreaterThanOp,
Left: &ColName{
Name: NewColIdent("val"),
Qualifier: TableName{
Name: NewTableIdent("a"),
},
},
Right: &ColName{
Name: NewColIdent("val"),
Qualifier: TableName{
Name: NewTableIdent("b"),
},
},
}
equalExpr := &ComparisonExpr{
Operator: EqualOp,
Left: &ColName{
Name: NewColIdent("id"),
Qualifier: TableName{
Name: NewTableIdent("a"),
},
},
Right: &ColName{
Name: NewColIdent("id"),
Qualifier: TableName{
Name: NewTableIdent("b"),
},
},
}
testcases := []struct {
name string
expressions Exprs
expectedOutput Expr
}{
{
name: "empty input",
expressions: nil,
expectedOutput: nil,
}, {
name: "two equal inputs",
expressions: Exprs{
greaterThanExpr,
equalExpr,
equalExpr,
},
expectedOutput: &AndExpr{
Left: greaterThanExpr,
Right: equalExpr,
},
},
{
name: "two equal inputs",
expressions: Exprs{
equalExpr,
equalExpr,
},
expectedOutput: equalExpr,
},
}

for _, testcase := range testcases {
t.Run(testcase.name, func(t *testing.T) {
output := AndExpressions(testcase.expressions...)
assert.Equal(t, String(testcase.expectedOutput), String(output))
})
}
}

func TestTableFromStatement(t *testing.T) {
testcases := []struct {
in, out string
Expand Down

0 comments on commit 7416b90

Please sign in to comment.