Skip to content

Commit

Permalink
fix: do not rewrite single columns in derived tables (#11419)
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <andres@planetscale.com>

Signed-off-by: Andres Taylor <andres@planetscale.com>
  • Loading branch information
systay authored Oct 3, 2022
1 parent c7116c9 commit 4342564
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
8 changes: 8 additions & 0 deletions go/vt/sqlparser/ast_rewriting.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,14 @@ func (er *astRewriter) unnestSubQueries(cursor *Cursor, subquery *Subquery) {
if !ok {
return
}
_, isColName := expr.Expr.(*ColName)
if isColName {
// If we find a single col-name in a `dual` subquery, we can be pretty sure the user is returning a column
// already projected.
// `select 1 as x, (select x)`
// is perfectly valid - any aliased columns to the left are available inside subquery scopes
return
}
er.bindVars.NoteRewrite()
// we need to make sure that the inner expression also gets rewritten,
// so we fire off another rewriter traversal here
Expand Down
4 changes: 4 additions & 0 deletions go/vt/sqlparser/ast_rewriting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func TestRewrites(in *testing.T) {
in: "select (select database() from dual) from dual",
expected: "select :__vtdbname as `(select database() from dual)` from dual",
db: true,
}, {
// don't unnest solo columns
in: "select 1 as foobar, (select foobar)",
expected: "select 1 as foobar, (select foobar from dual) from dual",
}, {
in: "select id from user where database()",
expected: "select id from user where database()",
Expand Down
5 changes: 0 additions & 5 deletions go/vt/vtgate/planbuilder/physical/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ func (f *Filter) TableID() semantics.TableSet {
return f.Source.TableID()
}

// PushPredicate implements the PhysicalOperator interface
func (f *Filter) PushPredicate(expr sqlparser.Expr, semTable *semantics.SemTable) error {
panic("unimplemented")
}

// UnsolvedPredicates implements the PhysicalOperator interface
func (f *Filter) UnsolvedPredicates(semTable *semantics.SemTable) []sqlparser.Expr {
panic("implement me")
Expand Down
36 changes: 36 additions & 0 deletions go/vt/vtgate/planbuilder/testdata/select_cases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7694,3 +7694,39 @@ Gen4 error: unsupported: JOIN not supported between derived tables
"user.music"
]
}

# Earlier columns are in scope in subqueries https://github.com/vitessio/vitess/issues/11246
"SELECT 1 as x, (SELECT x)"
{
"QueryType": "SELECT",
"Original": "SELECT 1 as x, (SELECT x)",
"Instructions": {
"OperatorType": "Route",
"Variant": "Reference",
"Keyspace": {
"Name": "main",
"Sharded": false
},
"FieldQuery": "select 1 as x, (select x from dual where 1 != 1) from dual where 1 != 1",
"Query": "select 1 as x, (select x from dual) from dual",
"Table": "dual"
}
}
{
"QueryType": "SELECT",
"Original": "SELECT 1 as x, (SELECT x)",
"Instructions": {
"OperatorType": "Route",
"Variant": "Reference",
"Keyspace": {
"Name": "main",
"Sharded": false
},
"FieldQuery": "select 1 as x, (select x from dual where 1 != 1) from dual where 1 != 1",
"Query": "select 1 as x, (select x from dual) from dual",
"Table": "dual"
},
"TablesUsed": [
"main.dual"
]
}

0 comments on commit 4342564

Please sign in to comment.