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

Fix aliasing in queries by keeping required projections #15943

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions go/test/endtoend/vtgate/queries/misc/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,15 @@ func TestStraightJoin(t *testing.T) {
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")

Expand Down
9 changes: 2 additions & 7 deletions go/vt/vtgate/planbuilder/operator_transformers.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ func transformProjection(ctx *plancontext.PlanningContext, op *operators.Project
if cols, colNames := op.AllOffsets(); cols != nil {
// if all this op is doing is passing through columns from the input, we
// can use the faster SimpleProjection
return useSimpleProjection(ctx, op, cols, colNames, src)
return useSimpleProjection(cols, colNames, src)
}

ap, err := op.GetAliasedProjections()
Expand Down Expand Up @@ -407,12 +407,7 @@ 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
func useSimpleProjection(ctx *plancontext.PlanningContext, op *operators.Projection, cols []int, colNames []string, 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
}
func useSimpleProjection(cols []int, colNames []string, src logicalPlan) (logicalPlan, error) {
return &simpleProjection{
GuptaManan100 marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -3503,35 +3503,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
"OperatorType": "Join",
"Variant": "LeftJoin",
"JoinColumnIndexes": "R:0",
"JoinVars": {
"user_id": 0
},
"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"
"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 @@ -1325,20 +1336,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 @@ -2848,20 +2848,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
Loading