Skip to content

Commit

Permalink
Merge pull request #8390 from dolthub/zachmu/reset-2
Browse files Browse the repository at this point in the history
doltgres fixes for constraint violation tables
  • Loading branch information
zachmu authored Sep 25, 2024
2 parents 8f8f08b + bb6cf2a commit f7384c0
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 27 deletions.
9 changes: 4 additions & 5 deletions go/libraries/doltcore/doltdb/root_val.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/env/actions/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
6 changes: 4 additions & 2 deletions go/libraries/doltcore/merge/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/sqle/dsess/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/sqle/dtables/merge_status_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions go/libraries/doltcore/sqle/dtables/status_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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",
})
}
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package dtables

import (
"bytes"
"fmt"
"io"

Expand Down Expand Up @@ -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
}
Expand All @@ -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
}

Expand Down Expand Up @@ -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])}
}

0 comments on commit f7384c0

Please sign in to comment.