Skip to content

Commit

Permalink
remove unneeded projections
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <andres@planetscale.com>
  • Loading branch information
systay committed Aug 7, 2023
1 parent 9b19a24 commit 6b4a544
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 101 deletions.
30 changes: 18 additions & 12 deletions go/vt/vtgate/planbuilder/operators/projection.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,21 +303,24 @@ func (p *Projection) ShortDescription() string {
}

func (p *Projection) Compact(ctx *plancontext.PlanningContext) (ops.Operator, *rewrite.ApplyResult, error) {
if !p.isDerived() {
// for projections that are not derived tables, we can check if it is safe to remove or not
needed := false
for i, projection := range p.Projections {
e, ok := projection.(Offset)
if !ok || e.Offset != i {
needed = true
break
}
}
if !needed {
return p.Source, rewrite.NewTree("removed projection only passing through the input", p), nil
if p.isDerived() {
return p, rewrite.SameTree, nil
}

// for projections that are not derived tables, we can check if it is safe to remove or not
needed := false
for i, projection := range p.Projections {
e, ok := projection.(Offset)
if !ok || e.Offset != i {
needed = true
break
}
}

if !needed {
return p.Source, rewrite.NewTree("removed projection only passing through the input", p), nil
}

switch src := p.Source.(type) {
case *Route:
return p.compactWithRoute(ctx, src)
Expand Down Expand Up @@ -428,6 +431,9 @@ func (p *Projection) planOffsets(ctx *plancontext.PlanningContext) error {
}
}

p.TableID = nil
p.Alias = ""

return nil
}

Expand Down
157 changes: 68 additions & 89 deletions go/vt/vtgate/planbuilder/testdata/union_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -785,29 +785,22 @@
"QueryType": "SELECT",
"Original": "select * from ((select id from user union select id+1 from user) union select user_id from user_extra) as t",
"Instructions": {
"OperatorType": "SimpleProjection",
"Columns": [
0
"OperatorType": "Distinct",
"Collations": [
"(0:1)"
],
"ResultColumns": 1,
"Inputs": [
{
"OperatorType": "Distinct",
"Collations": [
"(0:1)"
],
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select id, weight_string(id) from (select id from `user` where 1 != 1 union select id + 1 from `user` where 1 != 1 union select user_id from user_extra where 1 != 1) as dt where 1 != 1",
"Query": "select id, weight_string(id) from (select id from `user` union select id + 1 from `user` union select user_id from user_extra) as dt",
"Table": "`user`, user_extra"
}
]
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select id, weight_string(id) from (select id from `user` where 1 != 1 union select id + 1 from `user` where 1 != 1 union select user_id from user_extra where 1 != 1) as dt where 1 != 1",
"Query": "select id, weight_string(id) from (select id from `user` union select id + 1 from `user` union select user_id from user_extra) as dt",
"Table": "`user`, user_extra"
}
]
},
Expand Down Expand Up @@ -1013,49 +1006,41 @@
"TableName": "`user`_`user`",
"Inputs": [
{
"OperatorType": "SimpleProjection",
"Columns": [
0
],
"OperatorType": "Concatenate",
"Inputs": [
{
"OperatorType": "Concatenate",
"OperatorType": "Limit",
"Count": "INT64(5)",
"Inputs": [
{
"OperatorType": "Limit",
"Count": "INT64(5)",
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select id, weight_string(id) from `user` where 1 != 1",
"OrderBy": "(0|1) ASC",
"Query": "select id, weight_string(id) from `user` order by id asc limit :__upper_limit",
"Table": "`user`"
}
]
},
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select id, weight_string(id) from `user` where 1 != 1",
"OrderBy": "(0|1) ASC",
"Query": "select id, weight_string(id) from `user` order by id asc limit :__upper_limit",
"Table": "`user`"
}
]
},
{
"OperatorType": "Limit",
"Count": "INT64(5)",
"Inputs": [
{
"OperatorType": "Limit",
"Count": "INT64(5)",
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select id, weight_string(id) from `user` where 1 != 1",
"OrderBy": "(0|1) DESC",
"Query": "select id, weight_string(id) from `user` order by id desc limit :__upper_limit",
"Table": "`user`"
}
]
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select id, weight_string(id) from `user` where 1 != 1",
"OrderBy": "(0|1) DESC",
"Query": "select id, weight_string(id) from `user` order by id desc limit :__upper_limit",
"Table": "`user`"
}
]
}
Expand Down Expand Up @@ -1156,41 +1141,35 @@
],
"Inputs": [
{
"OperatorType": "Projection",
"Expressions": null,
"OperatorType": "Distinct",
"Collations": [
"(0:1)"
],
"Inputs": [
{
"OperatorType": "Distinct",
"Collations": [
"(0:1)"
],
"OperatorType": "Concatenate",
"Inputs": [
{
"OperatorType": "Concatenate",
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select id + 42 as foo, weight_string(id + 42) from `user` where 1 != 1",
"Query": "select distinct id + 42 as foo, weight_string(id + 42) from `user`",
"Table": "`user`"
},
{
"OperatorType": "Route",
"Variant": "Unsharded",
"Keyspace": {
"Name": "main",
"Sharded": false
},
"FieldQuery": "select 1 + id as foo, weight_string(1 + id) from unsharded where 1 != 1",
"Query": "select distinct 1 + id as foo, weight_string(1 + id) from unsharded",
"Table": "unsharded"
}
]
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select id + 42 as foo, weight_string(id + 42) from `user` where 1 != 1",
"Query": "select distinct id + 42 as foo, weight_string(id + 42) from `user`",
"Table": "`user`"
},
{
"OperatorType": "Route",
"Variant": "Unsharded",
"Keyspace": {
"Name": "main",
"Sharded": false
},
"FieldQuery": "select 1 + id as foo, weight_string(1 + id) from unsharded where 1 != 1",
"Query": "select distinct 1 + id as foo, weight_string(1 + id) from unsharded",
"Table": "unsharded"
}
]
}
Expand Down

0 comments on commit 6b4a544

Please sign in to comment.