Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use EvaluateCondition for conditions in join statements #1926

Merged
merged 2 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions enginetest/queries/join_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,36 @@ var JoinScriptTests = []ScriptTest{
},
},
},
{
Name: "Join with truthy condition",
SetUpScript: []string{
"CREATE TABLE `a` (aa int);",
"INSERT INTO `a` VALUES (1), (2);",

"CREATE TABLE `b` (bb int);",
"INSERT INTO `b` VALUES (1), (2);",
},
Assertions: []ScriptTestAssertion{
{
Query: "SELECT * FROM a LEFT JOIN b ON 1;",
Expected: []sql.Row{
{1, 2},
{1, 1},
{2, 2},
{2, 1},
},
},
{
Query: "SELECT * FROM a RIGHT JOIN b ON 8+9;",
Expected: []sql.Row{
{1, 2},
{1, 1},
{2, 2},
{2, 1},
},
},
},
},
}

var SkippedJoinQueryTests = []QueryTest{
Expand Down
29 changes: 8 additions & 21 deletions sql/rowexec/join_iters.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@ func (i *joinIter) Next(ctx *sql.Context) (sql.Row, error) {
}

row := i.buildRow(primary, secondary)
res, err := i.cond.Eval(ctx, row)
matches := res == true
res, err := sql.EvaluateCondition(ctx, i.cond, row)
if err != nil {
return nil, err
}
Expand All @@ -169,7 +168,7 @@ func (i *joinIter) Next(ctx *sql.Context) (sql.Row, error) {
continue
}

if !matches {
if !sql.IsTrue(res) {
continue
}

Expand All @@ -184,16 +183,6 @@ func (i *joinIter) removeParentRow(r sql.Row) sql.Row {
return r
}

func conditionIsTrue(ctx *sql.Context, row sql.Row, cond sql.Expression) (bool, error) {
v, err := cond.Eval(ctx, row)
if err != nil {
return false, err
}

// Expressions containing nil evaluate to nil, not false
return v == true, nil
}

// buildRow builds the result set row using the rows from the primary and secondary tables
func (i *joinIter) buildRow(primary, secondary sql.Row) sql.Row {
row := make(sql.Row, i.rowSize)
Expand Down Expand Up @@ -269,7 +258,6 @@ const (

func (i *existsIter) Next(ctx *sql.Context) (sql.Row, error) {
var row sql.Row
var matches bool
var right sql.Row
var left sql.Row
var rIter sql.RowIter
Expand Down Expand Up @@ -328,8 +316,7 @@ func (i *existsIter) Next(ctx *sql.Context) (sql.Row, error) {
}
case esCompare:
row = i.buildRow(left, right)
res, err := i.cond.Eval(ctx, row)
matches = res == true
res, err := sql.EvaluateCondition(ctx, i.cond, row)
if err != nil {
return nil, err
}
Expand All @@ -339,7 +326,7 @@ func (i *existsIter) Next(ctx *sql.Context) (sql.Row, error) {
continue
}

if !matches {
if !sql.IsTrue(res) {
nextState = esIncRight
} else {
err = rIter.Close(ctx)
Expand Down Expand Up @@ -476,11 +463,11 @@ func (i *fullJoinIter) Next(ctx *sql.Context) (sql.Row, error) {
}

row := i.buildRow(i.leftRow, rightRow)
matches, err := conditionIsTrue(ctx, row, i.cond)
matches, err := sql.EvaluateCondition(ctx, i.cond, row)
if err != nil {
return nil, err
}
if !matches {
if !sql.IsTrue(matches) {
continue
}
rkey, err := sql.HashOf(rightRow)
Expand Down Expand Up @@ -841,9 +828,9 @@ func (i *lateralJoinIterator) Next(ctx *sql.Context) (sql.Row, error) {
row := i.buildRow(i.lRow, i.rRow)
i.rRow = nil
if i.cond != nil {
if res, err := i.cond.Eval(ctx, row); err != nil {
if res, err := sql.EvaluateCondition(ctx, i.cond, row); err != nil {
return nil, err
} else if res == false {
} else if sql.IsFalse(res) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure whether this should be !sql.IsTrue like the other cases, but I've left it as IsFalse to keep to current behaviour

Copy link
Contributor

Choose a reason for hiding this comment

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

This should definitely be !sql.IsTrue so that it handles null cases correctly.

If you fix that then this PR looks perfect.

continue
}
}
Expand Down