From 7416b9014879122263b47a2a8832d116d3dd9a72 Mon Sep 17 00:00:00 2001 From: Harshit Gangal Date: Wed, 23 Jun 2021 12:36:03 +0530 Subject: [PATCH] added test for AndExpressions and fixed bug Signed-off-by: Harshit Gangal --- go/vt/sqlparser/analyzer.go | 4 +- go/vt/sqlparser/analyzer_test.go | 70 ++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/go/vt/sqlparser/analyzer.go b/go/vt/sqlparser/analyzer.go index e742b4fb18e..f06dc5485cc 100644 --- a/go/vt/sqlparser/analyzer.go +++ b/go/vt/sqlparser/analyzer.go @@ -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 } diff --git a/go/vt/sqlparser/analyzer_test.go b/go/vt/sqlparser/analyzer_test.go index cf755b82418..8fed5034217 100644 --- a/go/vt/sqlparser/analyzer_test.go +++ b/go/vt/sqlparser/analyzer_test.go @@ -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