Skip to content

Commit

Permalink
Handle single sharded keyspaces for analysis (#16068)
Browse files Browse the repository at this point in the history
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
  • Loading branch information
dbussink authored Jun 5, 2024
1 parent 113f4c3 commit 6def783
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
12 changes: 11 additions & 1 deletion go/vt/schemadiff/schema_diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,16 @@ func TestSchemaDiff(t *testing.T) {
entityOrder: []string{"v2"},
instantCapability: InstantDDLCapabilityIrrelevant,
},
{
name: "add view with over",
toQueries: append(
createQueries,
"create view v2 as SELECT *, ROW_NUMBER() OVER(PARTITION BY info) AS row_num1, ROW_NUMBER() OVER(PARTITION BY info ORDER BY id) AS row_num2 FROM t1;\n",
),
expectDiffs: 1,
entityOrder: []string{"v2"},
instantCapability: InstantDDLCapabilityIrrelevant,
},
{
name: "add view, alter table",
toQueries: []string{
Expand Down Expand Up @@ -1050,7 +1060,7 @@ func TestSchemaDiff(t *testing.T) {
return
}
if tc.conflictingDiffs > 0 {
assert.Error(t, err)
require.Error(t, err)
impossibleOrderErr, ok := err.(*ImpossibleApplyDiffOrderError)
assert.True(t, ok)
conflictingDiffsStatements := []string{}
Expand Down
13 changes: 8 additions & 5 deletions go/vt/vtgate/semantics/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type analyzer struct {
projErr error
unshardedErr error
warning string
canShortcut bool
singleUnshardedKeyspace bool
fullAnalysis bool
}
Expand Down Expand Up @@ -135,7 +136,7 @@ func (a *analyzer) newSemTable(
comments = commentedStmt.GetParsedComments()
}

if a.singleUnshardedKeyspace {
if a.canShortcut {
return &SemTable{
Tables: a.earlyTables.Tables,
Comments: comments,
Expand Down Expand Up @@ -386,16 +387,18 @@ func (a *analyzer) reAnalyze(statement sqlparser.SQLNode) error {
// canShortCut checks if we are dealing with a single unsharded keyspace and no tables that have managed foreign keys
// if so, we can stop the analyzer early
func (a *analyzer) canShortCut(statement sqlparser.Statement) (canShortCut bool) {
if a.fullAnalysis {
ks, _ := singleUnshardedKeyspace(a.earlyTables.Tables)
a.singleUnshardedKeyspace = ks != nil
if !a.singleUnshardedKeyspace {
return false
}
ks, _ := singleUnshardedKeyspace(a.earlyTables.Tables)
if ks == nil {

if a.fullAnalysis {
return false
}

defer func() {
a.singleUnshardedKeyspace = canShortCut
a.canShortcut = canShortCut
}()

if !sqlparser.IsDMLStatement(statement) {
Expand Down
6 changes: 4 additions & 2 deletions go/vt/vtgate/semantics/check_invalid.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ func (a *analyzer) checkForInvalidConstructs(cursor *sqlparser.Cursor) error {
return vterrors.VT12001("recursive common table expression")
}
case *sqlparser.Insert:
if node.Action == sqlparser.ReplaceAct {
if !a.singleUnshardedKeyspace && node.Action == sqlparser.ReplaceAct {
return ShardedError{Inner: &UnsupportedConstruct{errString: "REPLACE INTO with sharded keyspace"}}
}
case *sqlparser.OverClause:
return ShardedError{Inner: &UnsupportedConstruct{errString: "OVER CLAUSE with sharded keyspace"}}
if !a.singleUnshardedKeyspace {
return ShardedError{Inner: &UnsupportedConstruct{errString: "OVER CLAUSE with sharded keyspace"}}
}
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/semantics/semantic_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ func singleUnshardedKeyspace(tableInfos []TableInfo) (ks *vindexes.Keyspace, tab
return ks, tables
}

// SingleUnshardedKeyspace returns the single keyspace if all tables in the query are in the same keyspace
// SingleKeyspace returns the single keyspace if all tables in the query are in the same keyspace
func (st *SemTable) SingleKeyspace() (ks *vindexes.Keyspace) {
validKS := func(this *vindexes.Keyspace) bool {
if this == nil {
Expand Down

0 comments on commit 6def783

Please sign in to comment.