Skip to content

Commit

Permalink
Cherry-pick 951f273 with conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
vitess-bot[bot] committed May 15, 2024
1 parent 61ece6f commit 54d1aec
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 76 deletions.
52 changes: 52 additions & 0 deletions go/test/endtoend/vtgate/queries/misc/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,55 @@ func TestAlterTableWithView(t *testing.T) {

mcmp.AssertMatches("select * from v1", `[[INT64(1) INT64(1)]]`)
}
<<<<<<< HEAD
=======

// TestStraightJoin tests that Vitess respects the ordering of join in a STRAIGHT JOIN query.
func TestStraightJoin(t *testing.T) {
utils.SkipIfBinaryIsBelowVersion(t, 20, "vtgate")
mcmp, closer := start(t)
defer closer()

mcmp.Exec("insert into tbl(id, unq_col, nonunq_col) values (1,0,10), (2,10,10), (3,4,20), (4,30,20), (5,40,10)")
mcmp.Exec(`insert into t1(id1, id2) values (10, 11), (20, 13)`)

mcmp.AssertMatchesNoOrder("select tbl.unq_col, tbl.nonunq_col, t1.id2 from t1 join tbl where t1.id1 = tbl.nonunq_col",
`[[INT64(0) INT64(10) INT64(11)] [INT64(10) INT64(10) INT64(11)] [INT64(4) INT64(20) INT64(13)] [INT64(40) INT64(10) INT64(11)] [INT64(30) INT64(20) INT64(13)]]`,
)
// Verify that in a normal join query, vitess joins tbl with t1.
res, err := mcmp.VtConn.ExecuteFetch("vexplain plan select tbl.unq_col, tbl.nonunq_col, t1.id2 from t1 join tbl where t1.id1 = tbl.nonunq_col", 100, false)
require.NoError(t, err)
require.Contains(t, fmt.Sprintf("%v", res.Rows), "tbl_t1")

// Test the same query with a straight join
mcmp.AssertMatchesNoOrder("select tbl.unq_col, tbl.nonunq_col, t1.id2 from t1 straight_join tbl where t1.id1 = tbl.nonunq_col",
`[[INT64(0) INT64(10) INT64(11)] [INT64(10) INT64(10) INT64(11)] [INT64(4) INT64(20) INT64(13)] [INT64(40) INT64(10) INT64(11)] [INT64(30) INT64(20) INT64(13)]]`,
)
// Verify that in a straight join query, vitess joins t1 with tbl.
res, err = mcmp.VtConn.ExecuteFetch("vexplain plan select tbl.unq_col, tbl.nonunq_col, t1.id2 from t1 straight_join tbl where t1.id1 = tbl.nonunq_col", 100, false)
require.NoError(t, err)
require.Contains(t, fmt.Sprintf("%v", res.Rows), "t1_tbl")
}

func TestColumnAliases(t *testing.T) {
utils.SkipIfBinaryIsBelowVersion(t, 20, "vtgate")
mcmp, closer := start(t)
defer closer()

mcmp.Exec("insert into t1(id1, id2) values (0,0), (1,1)")
mcmp.ExecWithColumnCompare(`select a as k from (select count(*) as a from t1) t`)
}

func TestEnumSetVals(t *testing.T) {
utils.SkipIfBinaryIsBelowVersion(t, 20, "vtgate")

mcmp, closer := start(t)
defer closer()
require.NoError(t, utils.WaitForAuthoritative(t, keyspaceName, "tbl_enum_set", clusterInstance.VtgateProcess.ReadVSchema))

mcmp.Exec("insert into tbl_enum_set(id, enum_col, set_col) values (1, 'medium', 'a,b,e'), (2, 'small', 'e,f,g'), (3, 'large', 'c'), (4, 'xsmall', 'a,b'), (5, 'medium', 'a,d')")

mcmp.AssertMatches("select id, enum_col, cast(enum_col as signed) from tbl_enum_set order by enum_col, id", `[[INT64(4) ENUM("xsmall") INT64(1)] [INT64(2) ENUM("small") INT64(2)] [INT64(1) ENUM("medium") INT64(3)] [INT64(5) ENUM("medium") INT64(3)] [INT64(3) ENUM("large") INT64(4)]]`)
mcmp.AssertMatches("select id, set_col, cast(set_col as unsigned) from tbl_enum_set order by set_col, id", `[[INT64(4) SET("a,b") UINT64(3)] [INT64(3) SET("c") UINT64(4)] [INT64(5) SET("a,d") UINT64(9)] [INT64(1) SET("a,b,e") UINT64(19)] [INT64(2) SET("e,f,g") UINT64(112)]]`)
}
>>>>>>> 951f2732f3 (Fix aliasing in queries by keeping required projections (#15943))
8 changes: 8 additions & 0 deletions go/vt/vtgate/planbuilder/operator_transformers.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,11 @@ func transformProjection(ctx *plancontext.PlanningContext, op *operators.Project
if cols := op.AllOffsets(); cols != nil {
// if all this op is doing is passing through columns from the input, we
// can use the faster SimpleProjection
<<<<<<< HEAD

Check failure on line 357 in go/vt/vtgate/planbuilder/operator_transformers.go

View workflow job for this annotation

GitHub Actions / Code Coverage

syntax error: unexpected <<, expected }

Check failure on line 357 in go/vt/vtgate/planbuilder/operator_transformers.go

View workflow job for this annotation

GitHub Actions / Code Coverage

syntax error: unexpected <<, expected }
return useSimpleProjection(ctx, op, cols, src)
=======

Check failure on line 359 in go/vt/vtgate/planbuilder/operator_transformers.go

View workflow job for this annotation

GitHub Actions / Code Coverage

syntax error: unexpected ==, expected }

Check failure on line 359 in go/vt/vtgate/planbuilder/operator_transformers.go

View workflow job for this annotation

GitHub Actions / Code Coverage

syntax error: unexpected ==, expected }
return useSimpleProjection(cols, colNames, src)
>>>>>>> 951f2732f3 (Fix aliasing in queries by keeping required projections (#15943))

Check failure on line 361 in go/vt/vtgate/planbuilder/operator_transformers.go

View workflow job for this annotation

GitHub Actions / Code Coverage

invalid character U+0023 '#'

Check failure on line 361 in go/vt/vtgate/planbuilder/operator_transformers.go

View workflow job for this annotation

GitHub Actions / Code Coverage

invalid character U+0023 '#'
}

ap, err := op.GetAliasedProjections()
Expand Down Expand Up @@ -399,12 +403,16 @@ func getEvalEngingeExpr(ctx *plancontext.PlanningContext, pe *operators.ProjExpr

// useSimpleProjection uses nothing at all if the output is already correct,
// or SimpleProjection when we have to reorder or truncate the columns
<<<<<<< HEAD

Check failure on line 406 in go/vt/vtgate/planbuilder/operator_transformers.go

View workflow job for this annotation

GitHub Actions / Code Coverage

syntax error: non-declaration statement outside function body

Check failure on line 406 in go/vt/vtgate/planbuilder/operator_transformers.go

View workflow job for this annotation

GitHub Actions / Code Coverage

syntax error: non-declaration statement outside function body
func useSimpleProjection(ctx *plancontext.PlanningContext, op *operators.Projection, cols []int, src logicalPlan) (logicalPlan, error) {
columns := op.Source.GetColumns(ctx)
if len(columns) == len(cols) && elementsMatchIndices(cols) {
// the columns are already in the right order. we don't need anything at all here
return src, nil
}
=======

Check failure on line 413 in go/vt/vtgate/planbuilder/operator_transformers.go

View workflow job for this annotation

GitHub Actions / Code Coverage

syntax error: unexpected ==, expected }

Check failure on line 413 in go/vt/vtgate/planbuilder/operator_transformers.go

View workflow job for this annotation

GitHub Actions / Code Coverage

syntax error: unexpected ==, expected }
func useSimpleProjection(cols []int, colNames []string, src logicalPlan) (logicalPlan, error) {
>>>>>>> 951f2732f3 (Fix aliasing in queries by keeping required projections (#15943))

Check failure on line 415 in go/vt/vtgate/planbuilder/operator_transformers.go

View workflow job for this annotation

GitHub Actions / Code Coverage

syntax error: unexpected >>, expected }

Check failure on line 415 in go/vt/vtgate/planbuilder/operator_transformers.go

View workflow job for this annotation

GitHub Actions / Code Coverage

invalid character U+0023 '#'

Check failure on line 415 in go/vt/vtgate/planbuilder/operator_transformers.go

View workflow job for this annotation

GitHub Actions / Code Coverage

syntax error: unexpected >>, expected }

Check failure on line 415 in go/vt/vtgate/planbuilder/operator_transformers.go

View workflow job for this annotation

GitHub Actions / Code Coverage

invalid character U+0023 '#'
return &simpleProjection{
logicalPlanCommon: newBuilderCommon(src),
eSimpleProj: &engine.SimpleProjection{
Expand Down
63 changes: 37 additions & 26 deletions go/vt/vtgate/planbuilder/testdata/aggr_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -3459,35 +3459,46 @@
"Count": "10",
"Inputs": [
{
"OperatorType": "Join",
"Variant": "LeftJoin",
"JoinColumnIndexes": "R:0",
"JoinVars": {
"user_id": 0
},
"TableName": "`user`_user_extra",
"OperatorType": "SimpleProjection",
"ColumnNames": [
"col"
],
"Columns": [
0
],
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select `user`.id from `user` where 1 != 1",
"Query": "select `user`.id from `user`",
"Table": "`user`"
},
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
"OperatorType": "Join",
"Variant": "LeftJoin",
"JoinColumnIndexes": "R:0",
"JoinVars": {
"user_id": 0
},
"FieldQuery": "select user_extra.col from user_extra where 1 != 1",
"Query": "select user_extra.col from user_extra where user_extra.id = :user_id",
"Table": "user_extra"
"TableName": "`user`_user_extra",
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select `user`.id from `user` where 1 != 1",
"Query": "select `user`.id from `user`",
"Table": "`user`"
},
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select user_extra.col from user_extra where 1 != 1",
"Query": "select user_extra.col from user_extra where user_extra.id = :user_id",
"Table": "user_extra"
}
]
}
]
}
Expand Down
98 changes: 60 additions & 38 deletions go/vt/vtgate/planbuilder/testdata/cte_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -272,35 +272,46 @@
"Count": "10",
"Inputs": [
{
"OperatorType": "Join",
"Variant": "LeftJoin",
"JoinColumnIndexes": "R:0",
"JoinVars": {
"user_id": 0
},
"TableName": "`user`_user_extra",
"OperatorType": "SimpleProjection",
"ColumnNames": [
"col"
],
"Columns": [
0
],
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select `user`.id from `user` where 1 != 1",
"Query": "select `user`.id from `user`",
"Table": "`user`"
},
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
"OperatorType": "Join",
"Variant": "LeftJoin",
"JoinColumnIndexes": "R:0",
"JoinVars": {
"user_id": 0
},
"FieldQuery": "select user_extra.col from user_extra where 1 != 1",
"Query": "select user_extra.col from user_extra where user_extra.id = :user_id",
"Table": "user_extra"
"TableName": "`user`_user_extra",
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select `user`.id from `user` where 1 != 1",
"Query": "select `user`.id from `user`",
"Table": "`user`"
},
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select user_extra.col from user_extra where 1 != 1",
"Query": "select user_extra.col from user_extra where user_extra.id = :user_id",
"Table": "user_extra"
}
]
}
]
}
Expand Down Expand Up @@ -1334,20 +1345,31 @@
"QueryType": "SELECT",
"Original": "with t as (select count(*) as a from user) select a as k from t",
"Instructions": {
"OperatorType": "Aggregate",
"Variant": "Scalar",
"Aggregates": "sum_count_star(0) AS a",
"OperatorType": "SimpleProjection",
"ColumnNames": [
"k"
],
"Columns": [
0
],
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select count(*) as a from `user` where 1 != 1",
"Query": "select count(*) as a from `user`",
"Table": "`user`"
"OperatorType": "Aggregate",
"Variant": "Scalar",
"Aggregates": "sum_count_star(0) AS a",
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select count(*) as a from `user` where 1 != 1",
"Query": "select count(*) as a from `user`",
"Table": "`user`"
}
]
}
]
},
Expand Down
35 changes: 23 additions & 12 deletions go/vt/vtgate/planbuilder/testdata/from_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -2857,20 +2857,31 @@
"QueryType": "SELECT",
"Original": "select a as k from (select count(*) as a from user) t",
"Instructions": {
"OperatorType": "Aggregate",
"Variant": "Scalar",
"Aggregates": "sum_count_star(0) AS a",
"OperatorType": "SimpleProjection",
"ColumnNames": [
"k"
],
"Columns": [
0
],
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select count(*) as a from `user` where 1 != 1",
"Query": "select count(*) as a from `user`",
"Table": "`user`"
"OperatorType": "Aggregate",
"Variant": "Scalar",
"Aggregates": "sum_count_star(0) AS a",
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select count(*) as a from `user` where 1 != 1",
"Query": "select count(*) as a from `user`",
"Table": "`user`"
}
]
}
]
},
Expand Down

0 comments on commit 54d1aec

Please sign in to comment.