Skip to content

Commit

Permalink
differentiate view from base table in vschema
Browse files Browse the repository at this point in the history
Signed-off-by: Harshit Gangal <harshit@planetscale.com>
  • Loading branch information
harshit-gangal committed Jan 28, 2025
1 parent 33981fa commit d5c2934
Show file tree
Hide file tree
Showing 51 changed files with 424 additions and 379 deletions.
10 changes: 10 additions & 0 deletions go/test/endtoend/vtgate/schematracker/multi_ks/views_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ func TestViewQueries(t *testing.T) {
require.NoError(t, err)
defer conn.Close()

viewExists := func(t *testing.T, ksMap map[string]any) bool {
// wait for the view definition to change
views := ksMap["views"]
viewsMap := views.(map[string]any)
_, ok := viewsMap["cv3"]
return ok
}

utils.WaitForVschemaCondition(t, clusterInstance.VtgateProcess, shardedKs, viewExists, "view cv3 not found")

// Insert some data
insertData(t, conn)

Expand Down
4 changes: 2 additions & 2 deletions go/test/vschemawrapper/vschema_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func (vw *VSchemaWrapper) Destination() key.Destination {
return vw.Dest
}

func (vw *VSchemaWrapper) FindTable(tab sqlparser.TableName) (*vindexes.Table, string, topodatapb.TabletType, key.Destination, error) {
func (vw *VSchemaWrapper) FindTable(tab sqlparser.TableName) (*vindexes.BaseTable, string, topodatapb.TabletType, key.Destination, error) {
destKeyspace, destTabletType, destTarget, err := topoproto.ParseDestination(tab.Qualifier.String(), topodatapb.TabletType_PRIMARY)
if err != nil {
return nil, destKeyspace, destTabletType, destTarget, err
Expand Down Expand Up @@ -284,7 +284,7 @@ func (vw *VSchemaWrapper) FindViewTarget(name sqlparser.TableName) (*vindexes.Ke
return nil, nil
}

func (vw *VSchemaWrapper) FindTableOrVindex(tab sqlparser.TableName) (*vindexes.Table, vindexes.Vindex, string, topodatapb.TabletType, key.Destination, error) {
func (vw *VSchemaWrapper) FindTableOrVindex(tab sqlparser.TableName) (*vindexes.BaseTable, vindexes.Vindex, string, topodatapb.TabletType, key.Destination, error) {
return vw.Vcursor.FindTableOrVindex(tab)
}

Expand Down
8 changes: 4 additions & 4 deletions go/vt/schemadiff/semantics.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ var _ semantics.SchemaInformation = (*declarativeSchemaInformation)(nil)
// declarativeSchemaInformation is a utility wrapper around FakeSI, and adds a few utility functions
// to make it more simple and accessible to schemadiff's logic.
type declarativeSchemaInformation struct {
Tables map[string]*vindexes.Table
Tables map[string]*vindexes.BaseTable
env *Environment
}

func newDeclarativeSchemaInformation(env *Environment) *declarativeSchemaInformation {
return &declarativeSchemaInformation{
Tables: make(map[string]*vindexes.Table),
Tables: make(map[string]*vindexes.BaseTable),
env: env,
}
}

// FindTableOrVindex implements the SchemaInformation interface
func (si *declarativeSchemaInformation) FindTableOrVindex(tablename sqlparser.TableName) (*vindexes.Table, vindexes.Vindex, string, topodatapb.TabletType, key.Destination, error) {
func (si *declarativeSchemaInformation) FindTableOrVindex(tablename sqlparser.TableName) (*vindexes.BaseTable, vindexes.Vindex, string, topodatapb.TabletType, key.Destination, error) {
table := si.Tables[tablename.Name.String()]
return table, nil, "", 0, nil, nil
}
Expand Down Expand Up @@ -86,7 +86,7 @@ func (si *declarativeSchemaInformation) FindMirrorRule(tablename sqlparser.Table

// addTable adds a fake table with an empty column list
func (si *declarativeSchemaInformation) addTable(tableName string) {
tbl := &vindexes.Table{
tbl := &vindexes.BaseTable{
Name: sqlparser.NewIdentifierCS(tableName),
Columns: []vindexes.Column{},
ColumnListAuthoritative: true,
Expand Down
6 changes: 3 additions & 3 deletions go/vt/vtgate/engine/fake_vcursor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func (t *noopVCursor) InTransactionAndIsDML() bool {
panic("implement me")
}

func (t *noopVCursor) FindRoutedTable(sqlparser.TableName) (*vindexes.Table, error) {
func (t *noopVCursor) FindRoutedTable(sqlparser.TableName) (*vindexes.BaseTable, error) {
panic("implement me")
}

Expand Down Expand Up @@ -480,7 +480,7 @@ func (f *loggingVCursor) GetUDV(key string) *querypb.BindVariable {
}

type tableRoutes struct {
tbl *vindexes.Table
tbl *vindexes.BaseTable
}

func (f *loggingVCursor) ExecutePrimitive(ctx context.Context, primitive Primitive, bindVars map[string]*querypb.BindVariable, wantfields bool) (*sqltypes.Result, error) {
Expand Down Expand Up @@ -845,7 +845,7 @@ func (f *loggingVCursor) SetPriority(string) {
panic("implement me")
}

func (f *loggingVCursor) FindRoutedTable(tbl sqlparser.TableName) (*vindexes.Table, error) {
func (f *loggingVCursor) FindRoutedTable(tbl sqlparser.TableName) (*vindexes.BaseTable, error) {
f.log = append(f.log, fmt.Sprintf("FindTable(%s)", sqlparser.String(tbl)))
return f.tableRoutes.tbl, nil
}
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/engine/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func newInsert(
ignore bool,
keyspace *vindexes.Keyspace,
vindexValues [][][]evalengine.Expr,
table *vindexes.Table,
table *vindexes.BaseTable,
prefix string,
mid sqlparser.Values,
suffix sqlparser.OnDup,
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/engine/insert_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type (
func newInsertSelect(
ignore bool,
keyspace *vindexes.Keyspace,
table *vindexes.Table,
table *vindexes.BaseTable,
prefix string,
suffix sqlparser.OnDup,
vv [][]int,
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/engine/primitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ type (

LookupRowLockShardSession() vtgatepb.CommitOrder

FindRoutedTable(tablename sqlparser.TableName) (*vindexes.Table, error)
FindRoutedTable(tablename sqlparser.TableName) (*vindexes.BaseTable, error)

// GetDBDDLPlugin gets the configured plugin for DROP/CREATE DATABASE
GetDBDDLPluginName() string
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/engine/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func TestInformationSchemaWithTableAndSchemaWithRoutedTables(t *testing.T) {
}
if tc.routed {
vc.tableRoutes = tableRoutes{
tbl: &vindexes.Table{
tbl: &vindexes.BaseTable{
Name: sqlparser.NewIdentifierCS("routedTable"),
Keyspace: &vindexes.Keyspace{Name: "routedKeyspace"},
}}
Expand Down
10 changes: 5 additions & 5 deletions go/vt/vtgate/executorcontext/vcursor_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ func (vc *VCursorImpl) StartPrimitiveTrace() func() engine.Stats {

// FindTable finds the specified table. If the keyspace what specified in the input, it gets used as qualifier.
// Otherwise, the keyspace from the request is used, if one was provided.
func (vc *VCursorImpl) FindTable(name sqlparser.TableName) (*vindexes.Table, string, topodatapb.TabletType, key.Destination, error) {
func (vc *VCursorImpl) FindTable(name sqlparser.TableName) (*vindexes.BaseTable, string, topodatapb.TabletType, key.Destination, error) {
destKeyspace, destTabletType, dest, err := vc.parseDestinationTarget(name.Qualifier.String())
if err != nil {
return nil, "", destTabletType, nil, err
Expand All @@ -417,7 +417,7 @@ func (vc *VCursorImpl) FindView(name sqlparser.TableName) sqlparser.TableStateme
return vc.vschema.FindView(ks, name.Name.String())
}

func (vc *VCursorImpl) FindRoutedTable(name sqlparser.TableName) (*vindexes.Table, error) {
func (vc *VCursorImpl) FindRoutedTable(name sqlparser.TableName) (*vindexes.BaseTable, error) {
destKeyspace, destTabletType, _, err := vc.parseDestinationTarget(name.Qualifier.String())
if err != nil {
return nil, err
Expand All @@ -435,7 +435,7 @@ func (vc *VCursorImpl) FindRoutedTable(name sqlparser.TableName) (*vindexes.Tabl
}

// FindTableOrVindex finds the specified table or vindex.
func (vc *VCursorImpl) FindTableOrVindex(name sqlparser.TableName) (*vindexes.Table, vindexes.Vindex, string, topodatapb.TabletType, key.Destination, error) {
func (vc *VCursorImpl) FindTableOrVindex(name sqlparser.TableName) (*vindexes.BaseTable, vindexes.Vindex, string, topodatapb.TabletType, key.Destination, error) {
if name.Qualifier.IsEmpty() && name.Name.String() == "dual" {
// The magical MySQL dual table should only be resolved
// when it is not qualified by a database name.
Expand Down Expand Up @@ -489,7 +489,7 @@ func ParseDestinationTarget(targetString string, tablet topodatapb.TabletType, v
return destKeyspace, destTabletType, dest, err
}

func (vc *VCursorImpl) getDualTable() (*vindexes.Table, vindexes.Vindex, string, topodatapb.TabletType, key.Destination, error) {
func (vc *VCursorImpl) getDualTable() (*vindexes.BaseTable, vindexes.Vindex, string, topodatapb.TabletType, key.Destination, error) {
ksName := vc.getActualKeyspace()
var ks *vindexes.Keyspace
if ksName == "" {
Expand All @@ -498,7 +498,7 @@ func (vc *VCursorImpl) getDualTable() (*vindexes.Table, vindexes.Vindex, string,
} else {
ks = vc.vschema.Keyspaces[ksName].Keyspace
}
tbl := &vindexes.Table{
tbl := &vindexes.BaseTable{
Name: sqlparser.NewIdentifierCS("dual"),
Keyspace: ks,
Type: vindexes.TypeReference,
Expand Down
6 changes: 3 additions & 3 deletions go/vt/vtgate/planbuilder/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func checkFKError(vschema plancontext.VSchema, ddlStatement sqlparser.DDLStateme
}

func findTableDestinationAndKeyspace(vschema plancontext.VSchema, ddlStatement sqlparser.DDLStatement) (key.Destination, *vindexes.Keyspace, error) {
var table *vindexes.Table
var table *vindexes.BaseTable
var destination key.Destination
var keyspace *vindexes.Keyspace
var err error
Expand Down Expand Up @@ -325,7 +325,7 @@ func buildDropTable(vschema plancontext.VSchema, ddlStatement sqlparser.DDLState
for i, tab := range ddlStatement.GetFromTables() {
var destinationTab key.Destination
var keyspaceTab *vindexes.Keyspace
var table *vindexes.Table
var table *vindexes.BaseTable
var err error
table, _, _, _, destinationTab, err = vschema.FindTableOrVindex(tab)

Expand Down Expand Up @@ -369,7 +369,7 @@ func buildRenameTable(vschema plancontext.VSchema, renameTable *sqlparser.Rename
for _, tabPair := range renameTable.TablePairs {
var destinationFrom key.Destination
var keyspaceFrom *vindexes.Keyspace
var table *vindexes.Table
var table *vindexes.BaseTable
var err error
table, _, _, _, destinationFrom, err = vschema.FindTableOrVindex(tabPair.FromTable)

Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func rewriteSingleTbl(del *sqlparser.Delete) (*sqlparser.Delete, error) {
return del, nil
}

func deleteUnshardedShortcut(stmt *sqlparser.Delete, ks *vindexes.Keyspace, tables []*vindexes.Table) engine.Primitive {
func deleteUnshardedShortcut(stmt *sqlparser.Delete, ks *vindexes.Keyspace, tables []*vindexes.BaseTable) engine.Primitive {
edml := engine.NewDML()
edml.Keyspace = ks
edml.Opcode = engine.Unsharded
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/planbuilder/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ func gen4InsertStmtPlanner(version querypb.ExecuteOptions_PlannerVersion, insStm
return newPlanResult(plan, operators.TablesUsed(op)...), nil
}

func errOutIfPlanCannotBeConstructed(ctx *plancontext.PlanningContext, vTbl *vindexes.Table) error {
func errOutIfPlanCannotBeConstructed(ctx *plancontext.PlanningContext, vTbl *vindexes.BaseTable) error {
if !vTbl.Keyspace.Sharded {
return nil
}
return ctx.SemTable.NotUnshardedErr
}

func insertUnshardedShortcut(ctx *plancontext.PlanningContext, stmt *sqlparser.Insert, ks *vindexes.Keyspace, tables []*vindexes.Table) engine.Primitive {
func insertUnshardedShortcut(ctx *plancontext.PlanningContext, stmt *sqlparser.Insert, ks *vindexes.Keyspace, tables []*vindexes.BaseTable) engine.Primitive {
eIns := &engine.Insert{
InsertCommon: engine.InsertCommon{
Opcode: engine.InsertUnsharded,
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/operator_transformers.go
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ func buildDeletePrimitive(ctx *plancontext.PlanningContext, rb *operators.Route,
return &engine.Delete{DML: edml}, nil
}

func createDMLPrimitive(ctx *plancontext.PlanningContext, rb *operators.Route, hints *queryHints, vTbl *vindexes.Table, query string, colVindexes []*vindexes.ColumnVindex, vindexQuery string) *engine.DML {
func createDMLPrimitive(ctx *plancontext.PlanningContext, rb *operators.Route, hints *queryHints, vTbl *vindexes.BaseTable, query string, colVindexes []*vindexes.ColumnVindex, vindexQuery string) *engine.DML {
rp := newRoutingParams(ctx, rb.Routing.OpCode())
rb.Routing.UpdateRoutingParams(ctx, rp)
edml := &engine.DML{
Expand Down
10 changes: 5 additions & 5 deletions go/vt/vtgate/planbuilder/operators/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func createOperatorFromDelete(ctx *plancontext.PlanningContext, deleteStmt *sqlp
}

delClone := sqlparser.Clone(deleteStmt)
var vTbl *vindexes.Table
var vTbl *vindexes.BaseTable
op, vTbl = createDeleteOperator(ctx, deleteStmt)

if deleteStmt.Comments != nil {
Expand Down Expand Up @@ -154,7 +154,7 @@ func createDeleteWithInputOp(ctx *plancontext.PlanningContext, del *sqlparser.De
}

// getFirstVindex returns the first Vindex, if available
func getFirstVindex(vTbl *vindexes.Table) vindexes.Vindex {
func getFirstVindex(vTbl *vindexes.BaseTable) vindexes.Vindex {
if len(vTbl.ColumnVindexes) > 0 {
return vTbl.ColumnVindexes[0].Vindex
}
Expand Down Expand Up @@ -204,7 +204,7 @@ func createDeleteOpWithTarget(ctx *plancontext.PlanningContext, target semantics
}
}

func createDeleteOperator(ctx *plancontext.PlanningContext, del *sqlparser.Delete) (Operator, *vindexes.Table) {
func createDeleteOperator(ctx *plancontext.PlanningContext, del *sqlparser.Delete) (Operator, *vindexes.BaseTable) {
op := crossJoin(ctx, del.TableExprs)

sqc := &SubQueryBuilder{}
Expand Down Expand Up @@ -310,7 +310,7 @@ func addOrdering(ctx *plancontext.PlanningContext, op Operator, orderBy sqlparse
return newOrdering(op, order)
}

func updateQueryGraphWithSource(ctx *plancontext.PlanningContext, input Operator, tblID semantics.TableSet, vTbl *vindexes.Table) *vindexes.Table {
func updateQueryGraphWithSource(ctx *plancontext.PlanningContext, input Operator, tblID semantics.TableSet, vTbl *vindexes.BaseTable) *vindexes.BaseTable {
sourceTable, _, _, _, _, err := ctx.VSchema.FindTableOrVindex(vTbl.Source.TableName)
if err != nil {
panic(err)
Expand Down Expand Up @@ -339,7 +339,7 @@ func updateQueryGraphWithSource(ctx *plancontext.PlanningContext, input Operator
return vTbl
}

func createFkCascadeOpForDelete(ctx *plancontext.PlanningContext, parentOp Operator, delStmt *sqlparser.Delete, childFks []vindexes.ChildFKInfo, deletedTbl *vindexes.Table) Operator {
func createFkCascadeOpForDelete(ctx *plancontext.PlanningContext, parentOp Operator, delStmt *sqlparser.Delete, childFks []vindexes.ChildFKInfo, deletedTbl *vindexes.BaseTable) Operator {
var fkChildren []*FkChild
var selectExprs []sqlparser.SelectExpr
tblName := delStmt.Targets[0]
Expand Down
6 changes: 3 additions & 3 deletions go/vt/vtgate/planbuilder/operators/dml_planning.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ type DMLCommon struct {

type TargetTable struct {
ID semantics.TableSet
VTable *vindexes.Table
VTable *vindexes.BaseTable
Name sqlparser.TableName
}

// dmlOp stores intermediary value for Update/Delete Operator with the vindexes. Table for ordering.
type dmlOp struct {
op Operator
vTbl *vindexes.Table
vTbl *vindexes.BaseTable
cols []*sqlparser.ColName
updList updList
}
Expand Down Expand Up @@ -87,7 +87,7 @@ func shortDesc(target TargetTable, ovq *sqlparser.Select) string {

// getVindexInformation returns the vindex and VindexPlusPredicates for the DML,
// If it cannot find a unique vindex match, it returns an error.
func getVindexInformation(id semantics.TableSet, table *vindexes.Table) *vindexes.ColumnVindex {
func getVindexInformation(id semantics.TableSet, table *vindexes.BaseTable) *vindexes.ColumnVindex {
// Check that we have a primary vindex which is valid
if len(table.ColumnVindexes) == 0 || !table.ColumnVindexes[0].IsUnique() {
panic(vterrors.VT09001(table.Name))
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/operators/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func QualifiedTableNames(ks *vindexes.Keyspace, ts []sqlparser.TableName) []stri
return collect()
}

func QualifiedTables(ks *vindexes.Keyspace, vts []*vindexes.Table) []string {
func QualifiedTables(ks *vindexes.Keyspace, vts []*vindexes.BaseTable) []string {
add, collect := collectSortedUniqueStrings()
for _, vt := range vts {
add(QualifiedIdentifier(ks, vt.Name))
Expand Down
Loading

0 comments on commit d5c2934

Please sign in to comment.