Skip to content

Commit

Permalink
Cherry-pick 59408f9 with conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
vitess-bot[bot] committed Jul 25, 2024
1 parent 9a76c34 commit 1347a3f
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 12 deletions.
25 changes: 25 additions & 0 deletions go/test/endtoend/vtgate/queries/misc/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,31 @@ func TestTransactionModeVar(t *testing.T) {
}
}

<<<<<<< HEAD
=======
// TestAliasesInOuterJoinQueries tests that aliases work in queries that have outer join clauses.
func TestAliasesInOuterJoinQueries(t *testing.T) {
utils.SkipIfBinaryIsBelowVersion(t, 20, "vtgate")

mcmp, closer := start(t)
defer closer()

// Insert data into the 2 tables
mcmp.Exec("insert into t1(id1, id2) values (1,2), (42,5), (5, 42)")
mcmp.Exec("insert into tbl(id, unq_col, nonunq_col) values (1,2,3), (2,5,3), (3, 42, 2)")

// Check that the select query works as intended and verifying the column names as well.
mcmp.ExecWithColumnCompare("select t1.id1 as t0, t1.id1 as t1, tbl.unq_col as col from t1 left outer join tbl on t1.id2 = tbl.nonunq_col")
mcmp.ExecWithColumnCompare("select t1.id1 as t0, t1.id1 as t1, tbl.unq_col as col from t1 left outer join tbl on t1.id2 = tbl.nonunq_col order by t1.id2 limit 2")
mcmp.ExecWithColumnCompare("select t1.id1 as t0, t1.id1 as t1, tbl.unq_col as col from t1 left outer join tbl on t1.id2 = tbl.nonunq_col order by t1.id2 limit 2 offset 2")
mcmp.ExecWithColumnCompare("select t1.id1 as t0, t1.id1 as t1, count(*) as leCount from t1 left outer join tbl on t1.id2 = tbl.nonunq_col group by 1, t1")
mcmp.ExecWithColumnCompare("select t.id1, t.id2, derived.unq_col from t1 t join (select id, unq_col, nonunq_col from tbl) as derived on t.id2 = derived.nonunq_col")
if utils.BinaryIsAtLeastAtVersion(21, "vtgate") {
mcmp.ExecWithColumnCompare("select * from t1 t left join tbl on t.id1 = 666 and t.id2 = tbl.id")
}
}

>>>>>>> 59408f95e1 (bugfix: don't treat join predicates as filter predicates (#16472))
func TestAlterTableWithView(t *testing.T) {
utils.SkipIfBinaryIsBelowVersion(t, 19, "vtgate")
mcmp, closer := start(t)
Expand Down
10 changes: 10 additions & 0 deletions go/vt/vtgate/planbuilder/operators/apply_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,14 @@ type (
ApplyJoin struct {
LHS, RHS Operator

<<<<<<< HEAD

Check failure on line 37 in go/vt/vtgate/planbuilder/operators/apply_join.go

View workflow job for this annotation

GitHub Actions / Code Coverage

syntax error: unexpected <<, expected field name or embedded type

Check failure on line 37 in go/vt/vtgate/planbuilder/operators/apply_join.go

View workflow job for this annotation

GitHub Actions / Code Coverage

syntax error: unexpected <<, expected field name or embedded type
// LeftJoin will be true in the case of an outer join
LeftJoin bool
=======

Check failure on line 40 in go/vt/vtgate/planbuilder/operators/apply_join.go

View workflow job for this annotation

GitHub Actions / Code Coverage

syntax error: unexpected ==, expected field name or embedded type

Check failure on line 40 in go/vt/vtgate/planbuilder/operators/apply_join.go

View workflow job for this annotation

GitHub Actions / Code Coverage

syntax error: unexpected ==, expected field name or embedded type
// JoinType is permitted to store only 3 of the possible values
// NormalJoinType, StraightJoinType and LeftJoinType.
JoinType sqlparser.JoinType
>>>>>>> 59408f95e1 (bugfix: don't treat join predicates as filter predicates (#16472))

Check failure on line 44 in go/vt/vtgate/planbuilder/operators/apply_join.go

View workflow job for this annotation

GitHub Actions / Code Coverage

syntax error: unexpected >>, expected field name or embedded type

Check failure on line 44 in go/vt/vtgate/planbuilder/operators/apply_join.go

View workflow job for this annotation

GitHub Actions / Code Coverage

newline in rune literal

Check failure on line 44 in go/vt/vtgate/planbuilder/operators/apply_join.go

View workflow job for this annotation

GitHub Actions / Code Coverage

syntax error: unexpected >>, expected field name or embedded type

Check failure on line 44 in go/vt/vtgate/planbuilder/operators/apply_join.go

View workflow job for this annotation

GitHub Actions / Code Coverage

newline in rune literal

// JoinColumns keeps track of what AST expression is represented in the Columns array
JoinColumns *applyJoinColumns
Expand Down Expand Up @@ -277,6 +283,10 @@ func (aj *ApplyJoin) ShortDescription() string {
}

firstPart := fmt.Sprintf("on %s columns: %s", fn(aj.JoinPredicates), fn(aj.JoinColumns))
if aj.JoinType == sqlparser.LeftJoinType {
firstPart = "LEFT JOIN " + firstPart
}

if len(aj.ExtraLHSVars) == 0 {
return firstPart
}
Expand Down
28 changes: 16 additions & 12 deletions go/vt/vtgate/planbuilder/operators/route_planning.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ func mergeOrJoin(ctx *plancontext.PlanningContext, lhs, rhs Operator, joinPredic
return join, Rewrote("use a hash join because we have LIMIT on the LHS")
}

<<<<<<< HEAD

Check failure on line 360 in go/vt/vtgate/planbuilder/operators/route_planning.go

View workflow job for this annotation

GitHub Actions / Code Coverage

syntax error: unexpected <<, expected }

Check failure on line 360 in go/vt/vtgate/planbuilder/operators/route_planning.go

View workflow job for this annotation

GitHub Actions / Code Coverage

syntax error: unexpected <<, expected }
join := NewApplyJoin(ctx, Clone(rhs), Clone(lhs), nil, !inner)
newOp := pushJoinPredicates(ctx, joinPredicates, join)
return newOp, Rewrote("logical join to applyJoin, switching side because LIMIT")
Expand All @@ -365,6 +366,21 @@ func mergeOrJoin(ctx *plancontext.PlanningContext, lhs, rhs Operator, joinPredic
join := NewApplyJoin(ctx, Clone(lhs), Clone(rhs), nil, !inner)

Check failure on line 366 in go/vt/vtgate/planbuilder/operators/route_planning.go

View workflow job for this annotation

GitHub Actions / Code Coverage

syntax error: non-declaration statement outside function body

Check failure on line 366 in go/vt/vtgate/planbuilder/operators/route_planning.go

View workflow job for this annotation

GitHub Actions / Code Coverage

syntax error: non-declaration statement outside function body
newOp := pushJoinPredicates(ctx, joinPredicates, join)
return newOp, Rewrote("logical join to applyJoin ")
=======
join := NewApplyJoin(ctx, Clone(rhs), Clone(lhs), nil, joinType)
for _, pred := range joinPredicates {
join.AddJoinPredicate(ctx, pred)
}
return join, Rewrote("logical join to applyJoin, switching side because LIMIT")
}

join := NewApplyJoin(ctx, Clone(lhs), Clone(rhs), nil, joinType)
for _, pred := range joinPredicates {
join.AddJoinPredicate(ctx, pred)
}

return join, Rewrote("logical join to applyJoin ")
>>>>>>> 59408f95e1 (bugfix: don't treat join predicates as filter predicates (#16472))

Check failure on line 383 in go/vt/vtgate/planbuilder/operators/route_planning.go

View workflow job for this annotation

GitHub Actions / Code Coverage

newline in rune literal

Check failure on line 383 in go/vt/vtgate/planbuilder/operators/route_planning.go

View workflow job for this annotation

GitHub Actions / Code Coverage

newline in rune literal
}

func operatorsToRoutes(a, b Operator) (*Route, *Route) {
Expand Down Expand Up @@ -583,15 +599,3 @@ func hexEqual(a, b *sqlparser.Literal) bool {
}
return false
}

func pushJoinPredicates(ctx *plancontext.PlanningContext, exprs []sqlparser.Expr, op *ApplyJoin) Operator {
if len(exprs) == 0 {
return op
}

for _, expr := range exprs {
AddPredicate(ctx, op, expr, true, newFilterSinglePredicate)
}

return op
}
53 changes: 53 additions & 0 deletions go/vt/vtgate/planbuilder/testdata/from_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,59 @@
]
}
},
{
"comment": "Outer join with join predicates that only depend on the inner side",
"query": "select 1 from user left join user_extra on user.foo = 42 and user.bar = user_extra.bar",
"plan": {
"QueryType": "SELECT",
"Original": "select 1 from user left join user_extra on user.foo = 42 and user.bar = user_extra.bar",
"Instructions": {
"OperatorType": "Projection",
"Expressions": [
"1 as 1"
],
"Inputs": [
{
"OperatorType": "Join",
"Variant": "LeftJoin",
"JoinVars": {
"user_bar": 1,
"user_foo": 0
},
"TableName": "`user`_user_extra",
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select `user`.foo, `user`.bar from `user` where 1 != 1",
"Query": "select `user`.foo, `user`.bar from `user`",
"Table": "`user`"
},
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select 1 from user_extra where 1 != 1",
"Query": "select 1 from user_extra where user_extra.bar = :user_bar and :user_foo = 42",
"Table": "user_extra"
}
]
}
]
},
"TablesUsed": [
"user.user",
"user.user_extra"
]
}
},
{
"comment": "Parenthesized, single chunk",
"query": "select user.col from user join (unsharded as m1 join unsharded as m2)",
Expand Down

0 comments on commit 1347a3f

Please sign in to comment.