From b5d7b2af2469ee1340792a2327de3cb57ee07186 Mon Sep 17 00:00:00 2001 From: Zach Musgrave Date: Mon, 23 Sep 2024 17:40:18 -0700 Subject: [PATCH 1/2] Plumbing TableName through to more places, fixes for schema names in constraint violations tables --- go/libraries/doltcore/doltdb/root_val.go | 9 +++---- go/libraries/doltcore/env/actions/commit.go | 2 +- go/libraries/doltcore/merge/merge.go | 6 +++-- .../doltcore/sqle/dsess/transactions.go | 2 +- .../sqle/dtables/merge_status_table.go | 2 +- .../doltcore/sqle/dtables/status_table.go | 12 ++++----- .../table_of_tables_with_violations.go | 26 ++++++++++++++----- 7 files changed, 37 insertions(+), 22 deletions(-) diff --git a/go/libraries/doltcore/doltdb/root_val.go b/go/libraries/doltcore/doltdb/root_val.go index ed3b5952940..502f89d14e8 100644 --- a/go/libraries/doltcore/doltdb/root_val.go +++ b/go/libraries/doltcore/doltdb/root_val.go @@ -654,16 +654,15 @@ func TablesWithDataConflicts(ctx context.Context, root RootValue) ([]string, err } // TablesWithConstraintViolations returns all tables that have constraint violations. -func TablesWithConstraintViolations(ctx context.Context, root RootValue) ([]string, error) { - // TODO: schema name - names, err := root.GetTableNames(ctx, DefaultSchemaName) +func TablesWithConstraintViolations(ctx context.Context, root RootValue) ([]TableName, error) { + names, err := UnionTableNames(ctx, root) if err != nil { return nil, err } - violating := make([]string, 0, len(names)) + violating := make([]TableName, 0, len(names)) for _, name := range names { - tbl, _, err := root.GetTable(ctx, TableName{Name: name}) + tbl, _, err := root.GetTable(ctx, name) if err != nil { return nil, err } diff --git a/go/libraries/doltcore/env/actions/commit.go b/go/libraries/doltcore/env/actions/commit.go index 0bebcf21f59..6063be3e46a 100644 --- a/go/libraries/doltcore/env/actions/commit.go +++ b/go/libraries/doltcore/env/actions/commit.go @@ -89,7 +89,7 @@ func GetCommitStaged( return nil, err } if len(violatesConstraints) > 0 { - return nil, NewTblHasConstraintViolations(violatesConstraints) + return nil, NewTblHasConstraintViolations(doltdb.FlattenTableNames(violatesConstraints)) } if ws.MergeActive() { diff --git a/go/libraries/doltcore/merge/merge.go b/go/libraries/doltcore/merge/merge.go index 9452acbd5c1..41f14c3c05f 100644 --- a/go/libraries/doltcore/merge/merge.go +++ b/go/libraries/doltcore/merge/merge.go @@ -538,10 +538,12 @@ func GetMergeArtifactStatus(ctx context.Context, working *doltdb.WorkingSet) (as return as, err } - as.ConstraintViolationsTables, err = doltdb.TablesWithConstraintViolations(ctx, working.WorkingRoot()) + violations, err := doltdb.TablesWithConstraintViolations(ctx, working.WorkingRoot()) if err != nil { - return as, err + return ArtifactStatus{}, err } + + as.ConstraintViolationsTables = doltdb.FlattenTableNames(violations) return } diff --git a/go/libraries/doltcore/sqle/dsess/transactions.go b/go/libraries/doltcore/sqle/dsess/transactions.go index 90eab235e93..f9711a7668d 100644 --- a/go/libraries/doltcore/sqle/dsess/transactions.go +++ b/go/libraries/doltcore/sqle/dsess/transactions.go @@ -648,7 +648,7 @@ func (tx *DoltTransaction) validateWorkingSetForCommit(ctx *sql.Context, working violations := make([]string, len(badTbls)) for i, name := range badTbls { - tbl, _, err := workingRoot.GetTable(ctx, doltdb.TableName{Name: name}) + tbl, _, err := workingRoot.GetTable(ctx, name) if err != nil { return err } diff --git a/go/libraries/doltcore/sqle/dtables/merge_status_table.go b/go/libraries/doltcore/sqle/dtables/merge_status_table.go index 58833538ad9..fc6f7e2cedf 100644 --- a/go/libraries/doltcore/sqle/dtables/merge_status_table.go +++ b/go/libraries/doltcore/sqle/dtables/merge_status_table.go @@ -104,7 +104,7 @@ func newMergeStatusItr(ctx context.Context, ws *doltdb.WorkingSet) (*MergeStatus } unmergedTblNames := set.NewStrSet(inConflict) - unmergedTblNames.Add(tblsWithViolations...) + unmergedTblNames.Add(doltdb.FlattenTableNames(tblsWithViolations)...) unmergedTblNames.Add(schConflicts...) var sourceCommitSpecStr *string diff --git a/go/libraries/doltcore/sqle/dtables/status_table.go b/go/libraries/doltcore/sqle/dtables/status_table.go index 9b006dc83d4..fc885c535a9 100644 --- a/go/libraries/doltcore/sqle/dtables/status_table.go +++ b/go/libraries/doltcore/sqle/dtables/status_table.go @@ -100,9 +100,9 @@ type statusTableRow struct { status string } -func contains(str string, strs []string) bool { - for _, s := range strs { - if s == str { +func containsTableName(name string, names []doltdb.TableName) bool { + for _, s := range names { + if s.Name == name { return true } } @@ -136,7 +136,7 @@ func newStatusItr(ctx *sql.Context, st *StatusTable) (*StatusItr, error) { for _, tbl := range cvTables { rows = append(rows, statusTableRow{ - tableName: tbl, + tableName: tbl.Name, status: "constraint violation", }) } @@ -176,7 +176,7 @@ func newStatusItr(ctx *sql.Context, st *StatusTable) (*StatusItr, error) { if doltdb.IsFullTextTable(tblName) { continue } - if contains(tblName, cvTables) { + if containsTableName(tblName, cvTables) { continue } rows = append(rows, statusTableRow{ @@ -190,7 +190,7 @@ func newStatusItr(ctx *sql.Context, st *StatusTable) (*StatusItr, error) { if doltdb.IsFullTextTable(tblName) { continue } - if contains(tblName, cvTables) { + if containsTableName(tblName, cvTables) { continue } rows = append(rows, statusTableRow{ diff --git a/go/libraries/doltcore/sqle/dtables/table_of_tables_with_violations.go b/go/libraries/doltcore/sqle/dtables/table_of_tables_with_violations.go index 21ee8e2bea1..5bb1dae2461 100644 --- a/go/libraries/doltcore/sqle/dtables/table_of_tables_with_violations.go +++ b/go/libraries/doltcore/sqle/dtables/table_of_tables_with_violations.go @@ -15,6 +15,7 @@ package dtables import ( + "bytes" "fmt" "io" @@ -78,9 +79,9 @@ func (totwv *TableOfTablesWithViolations) Partitions(ctx *sql.Context) (sql.Part // PartitionRows implements the interface sql.Table. func (totwv *TableOfTablesWithViolations) PartitionRows(ctx *sql.Context, part sql.Partition) (sql.RowIter, error) { - tblName := string(part.Key()) + tblName := decodeTableName(part.Key()) var rows []sql.Row - tbl, _, ok, err := doltdb.GetTableInsensitive(ctx, totwv.root, doltdb.TableName{Name: tblName}) + tbl, ok, err := totwv.root.GetTable(ctx, tblName) if err != nil { return nil, err } @@ -91,7 +92,7 @@ func (totwv *TableOfTablesWithViolations) PartitionRows(ctx *sql.Context, part s if err != nil { return nil, err } - rows = append(rows, sql.Row{tblName, n}) + rows = append(rows, sql.Row{tblName.Name, n}) return sql.RowsToRowIter(rows...), nil } @@ -119,11 +120,24 @@ func (t *tableOfTablesPartitionIter) Close(context *sql.Context) error { } // tableOfTablesPartition is a partition returned from tableOfTablesPartitionIter, which is just a table name. -type tableOfTablesPartition string +type tableOfTablesPartition doltdb.TableName -var _ sql.Partition = tableOfTablesPartition("") +var _ sql.Partition = tableOfTablesPartition(doltdb.TableName{}) // Key implements the interface sql.Partition. func (t tableOfTablesPartition) Key() []byte { - return []byte(t) + return encodeTableName(doltdb.TableName(t)) +} + +func encodeTableName(name doltdb.TableName) []byte { + b := bytes.Buffer{} + b.WriteString(name.Schema) + b.WriteByte(0) + b.WriteString(name.Name) + return b.Bytes() +} + +func decodeTableName(b []byte) doltdb.TableName { + parts := bytes.SplitN(b, []byte{0}, 2) + return doltdb.TableName{Schema: string(parts[0]), Name: string(parts[1])} } From 7b771cfbef3250f556d2260e593a6b47edb03195 Mon Sep 17 00:00:00 2001 From: Zach Musgrave Date: Mon, 23 Sep 2024 17:44:33 -0700 Subject: [PATCH 2/2] Use TableName for constraint resolution --- .../sqle/dtables/constraint_violations_prolly.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/go/libraries/doltcore/sqle/dtables/constraint_violations_prolly.go b/go/libraries/doltcore/sqle/dtables/constraint_violations_prolly.go index ff91d86a017..4daafe8acec 100644 --- a/go/libraries/doltcore/sqle/dtables/constraint_violations_prolly.go +++ b/go/libraries/doltcore/sqle/dtables/constraint_violations_prolly.go @@ -55,7 +55,7 @@ func newProllyCVTable(ctx *sql.Context, tblName string, root doltdb.RootValue, r } m := durable.ProllyMapFromArtifactIndex(arts) return &prollyConstraintViolationsTable{ - tblName: resolvedName.Name, + tblName: resolvedName, root: root, sqlSch: sqlSch, tbl: tbl, @@ -67,7 +67,7 @@ func newProllyCVTable(ctx *sql.Context, tblName string, root doltdb.RootValue, r // prollyConstraintViolationsTable is a sql.Table implementation that provides access to the constraint violations that exist // for a user table for the v1 format. type prollyConstraintViolationsTable struct { - tblName string + tblName doltdb.TableName root doltdb.RootValue sqlSch sql.PrimaryKeySchema tbl *doltdb.Table @@ -80,12 +80,12 @@ var _ sql.DeletableTable = (*prollyConstraintViolationsTable)(nil) // Name implements the interface sql.Table. func (cvt *prollyConstraintViolationsTable) Name() string { - return doltdb.DoltConstViolTablePrefix + cvt.tblName + return doltdb.DoltConstViolTablePrefix + cvt.tblName.Name } // String implements the interface sql.Table. func (cvt *prollyConstraintViolationsTable) String() string { - return doltdb.DoltConstViolTablePrefix + cvt.tblName + return doltdb.DoltConstViolTablePrefix + cvt.tblName.Name } // Schema implements the interface sql.Table. @@ -320,7 +320,7 @@ func (d *prollyCVDeleter) Close(ctx *sql.Context) error { return err } - updatedRoot, err := d.cvt.root.PutTable(ctx, doltdb.TableName{Name: d.cvt.tblName}, updatedTbl) + updatedRoot, err := d.cvt.root.PutTable(ctx, d.cvt.tblName, updatedTbl) if err != nil { return err }