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

Bugfix for Panic on Joined Queries with Non-Authoritative Tables in Vitess 19.0 #17103

Merged
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
25 changes: 25 additions & 0 deletions go/vt/vtgate/planbuilder/testdata/from_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,31 @@
]
}
},
{
"comment": "Ambiguous column is not a problem when we can merge and push down",
"query": "select foobar from user join music on user.id = music.user_id order by foobar",
"plan": {
"QueryType": "SELECT",
"Original": "select foobar from user join music on user.id = music.user_id order by foobar",
"Instructions": {
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select foobar, weight_string(foobar) from `user`, music where 1 != 1",
"OrderBy": "(0|1) ASC",
"Query": "select foobar, weight_string(foobar) from `user`, music where `user`.id = music.user_id order by foobar asc",
"ResultColumns": 1,
"Table": "`user`, music"
},
"TablesUsed": [
"user.music",
"user.user"
]
}
},
Copy link
Contributor

Choose a reason for hiding this comment

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

@systay Should we also add these tests on main for regression testing as well?

{
"comment": "index hints, make sure they are not stripped.",
"query": "select user.col from user use index(a)",
Expand Down
1 change: 1 addition & 0 deletions go/vt/vtgate/semantics/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,7 @@ func fakeSchemaInfo() *FakeSI {
si := &FakeSI{
Tables: map[string]*vindexes.Table{
"t": {Name: sqlparser.NewIdentifierCS("t"), Keyspace: unsharded},
"t3": {Name: sqlparser.NewIdentifierCS("t3"), Keyspace: unsharded},
"t1": {Name: sqlparser.NewIdentifierCS("t1"), Columns: cols1, ColumnListAuthoritative: true, Keyspace: ks2},
"t2": {Name: sqlparser.NewIdentifierCS("t2"), Columns: cols2, ColumnListAuthoritative: true, Keyspace: ks3},
},
Expand Down
10 changes: 9 additions & 1 deletion go/vt/vtgate/semantics/early_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,15 @@ func (r *earlyRewriter) fillInQualifiers(cursor *sqlparser.CopyOnWriteCursor) {
if !found {
panic("uh oh")
}
tbl := r.tables.Tables[ts.TableOffset()]
offset := ts.TableOffset()
if offset < 0 {
// this is a column that is not coming from a table - it's an alias introduced in a SELECT expression
// Example: select (1+1) as foo from bar order by foo
// we don't want to add a qualifier to foo here
cursor.Replace(sqlparser.NewColName(col.Name.String()))
return
}
tbl := r.tables.Tables[offset]
tblName, err := tbl.Name()
if err != nil {
panic(err)
Expand Down
27 changes: 27 additions & 0 deletions go/vt/vtgate/semantics/early_rewriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,33 @@ func TestOrderByColumnName(t *testing.T) {
}
}

func TestOrderByNotAllColumnsAuthoritative(t *testing.T) {
schemaInfo := fakeSchemaInfo()
cDB := "db"
tcases := []struct {
sql string
expSQL string
deps TableSet
}{{
sql: "select foo from t3 join t order by foo",
expSQL: "select foo from t3 join t order by foo asc",
deps: None,
}}
for _, tcase := range tcases {
t.Run(tcase.sql, func(t *testing.T) {
ast, err := sqlparser.NewTestParser().Parse(tcase.sql)
require.NoError(t, err)
selectStatement := ast.(sqlparser.SelectStatement)
semTable, err := Analyze(selectStatement, cDB, schemaInfo)

require.NoError(t, err)
assert.Equal(t, tcase.expSQL, sqlparser.String(selectStatement))
orderByExpr := selectStatement.GetOrderBy()[0].Expr
assert.Equal(t, tcase.deps, semTable.RecursiveDeps(orderByExpr))
})
}
}

func TestSemTableDependenciesAfterExpandStar(t *testing.T) {
schemaInfo := &FakeSI{Tables: map[string]*vindexes.Table{
"t1": {
Expand Down
Loading