Skip to content

Commit

Permalink
gen4: analyze simple update query
Browse files Browse the repository at this point in the history
Signed-off-by: Harshit Gangal <harshit@planetscale.com>
  • Loading branch information
harshit-gangal authored and systay committed Apr 5, 2022
1 parent 772499d commit c3d45ab
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 deletions.
2 changes: 1 addition & 1 deletion go/vt/vtgate/semantics/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,7 @@ func parseAndAnalyze(t *testing.T, query, dbName string) (sqlparser.Statement, *
parse, err := sqlparser.Parse(query)
require.NoError(t, err)

semTable, err := Analyze(parse.(sqlparser.SelectStatement), dbName, fakeSchemaInfo())
semTable, err := Analyze(parse, dbName, fakeSchemaInfo())
require.NoError(t, err)
return parse, semTable
}
Expand Down
89 changes: 89 additions & 0 deletions go/vt/vtgate/semantics/analyzer_update_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
Copyright 2022 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package semantics

import (
"testing"

"github.com/stretchr/testify/assert"

"vitess.io/vitess/go/vt/sqlparser"
)

func TestUpdBindingColName(t *testing.T) {
queries := []string{
"update tabl set col = 1",
"update t2 set uid = 5",
"update tabl set tabl.col = 1 ",
"update tabl set d.tabl.col = 5",
"update d.tabl set col = 1",
"update d.tabl set tabl.col = 5",
"update d.tabl set d.tabl.col = 1",
}
for _, query := range queries {
t.Run(query, func(t *testing.T) {
stmt, semTable := parseAndAnalyze(t, query, "d")
upd, _ := stmt.(*sqlparser.Update)
t1 := upd.TableExprs[0].(*sqlparser.AliasedTableExpr)
ts := semTable.TableSetFor(t1)
assert.Equal(t, SingleTableSet(0), ts)

updExpr := extractFromUpdate(upd, 0)
recursiveDeps := semTable.RecursiveDeps(updExpr.Name)
assert.Equal(t, T1, recursiveDeps, query)
assert.Equal(t, T1, semTable.DirectDeps(updExpr.Name), query)
assert.Equal(t, 1, recursiveDeps.NumberOfTables(), "number of tables is wrong")

recursiveDeps = semTable.RecursiveDeps(updExpr.Expr)
assert.Equal(t, None, recursiveDeps, query)
assert.Equal(t, None, semTable.DirectDeps(updExpr.Expr), query)
})
}
}

func TestUpdBindingExpr(t *testing.T) {
queries := []string{
"update tabl set col = col",
"update tabl set d.tabl.col = tabl.col",
"update d.tabl set col = d.tabl.col",
"update d.tabl set tabl.col = col",
}
for _, query := range queries {
t.Run(query, func(t *testing.T) {
stmt, semTable := parseAndAnalyze(t, query, "d")
upd, _ := stmt.(*sqlparser.Update)
t1 := upd.TableExprs[0].(*sqlparser.AliasedTableExpr)
ts := semTable.TableSetFor(t1)
assert.Equal(t, SingleTableSet(0), ts)

updExpr := extractFromUpdate(upd, 0)
recursiveDeps := semTable.RecursiveDeps(updExpr.Name)
assert.Equal(t, T1, recursiveDeps, query)
assert.Equal(t, T1, semTable.DirectDeps(updExpr.Name), query)
assert.Equal(t, 1, recursiveDeps.NumberOfTables(), "number of tables is wrong")

recursiveDeps = semTable.RecursiveDeps(updExpr.Expr)
assert.Equal(t, T1, recursiveDeps, query)
assert.Equal(t, T1, semTable.DirectDeps(updExpr.Expr), query)
assert.Equal(t, 1, recursiveDeps.NumberOfTables(), "number of tables is wrong")
})
}
}

func extractFromUpdate(in *sqlparser.Update, idx int) *sqlparser.UpdateExpr {
return in.Exprs[idx]
}
5 changes: 4 additions & 1 deletion go/vt/vtgate/semantics/scoper.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func newScoper() *scoper {
func (s *scoper) down(cursor *sqlparser.Cursor) error {
node := cursor.Node()
switch node := node.(type) {
case *sqlparser.Update:
currScope := newScope(s.currentScope())
s.push(currScope)
case *sqlparser.Select:
currScope := newScope(s.currentScope())
s.push(currScope)
Expand Down Expand Up @@ -149,7 +152,7 @@ func (s *scoper) up(cursor *sqlparser.Cursor) error {
if isParentSelectStatement(cursor) {
s.popScope()
}
case *sqlparser.Select, sqlparser.GroupBy:
case *sqlparser.Select, sqlparser.GroupBy, *sqlparser.Update:
s.popScope()
case *sqlparser.Where:
if node.Type != sqlparser.HavingClause {
Expand Down

0 comments on commit c3d45ab

Please sign in to comment.