Skip to content
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
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
Comment on lines +546 to +549
Copy link
Copy Markdown
Member

@frouioui frouioui Oct 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL:

mysql> select 1 as x, (select x);
+---+------------+
| x | (select x) |
+---+------------+
| 1 |          1 |
+---+------------+
1 row in set (0.00 sec)

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")
Comment on lines -40 to -42
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we are at it, we could also remove Table.PushPredicate

}

// 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"
]
}