Skip to content

Commit

Permalink
Merge pull request #8381 from dolthub/taylor/dolt-diff
Browse files Browse the repository at this point in the history
Fix diff related table functions for doltgres
  • Loading branch information
tbantle22 authored Sep 24, 2024
2 parents bd95816 + 27a3056 commit a63bf91
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
17 changes: 10 additions & 7 deletions go/libraries/doltcore/sqle/dolt_diff_stat_table_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,11 @@ func (ds *DiffStatTableFunction) RowIter(ctx *sql.Context, row sql.Row) (sql.Row
// If tableNameExpr defined, return a single table diff stat result
if ds.tableNameExpr != nil {
delta := findMatchingDelta(deltas, tableName)
diffStat, hasDiff, err := getDiffStatNodeFromDelta(ctx, delta, fromRefDetails.root, toRefDetails.root, tableName)
schemaName := delta.FromName.Schema
if schemaName == "" {
schemaName = delta.ToName.Schema
}
diffStat, hasDiff, err := getDiffStatNodeFromDelta(ctx, delta, fromRefDetails.root, toRefDetails.root, doltdb.TableName{Name: tableName, Schema: schemaName})
if err != nil {
return nil, err
}
Expand All @@ -309,8 +313,7 @@ func (ds *DiffStatTableFunction) RowIter(ctx *sql.Context, row sql.Row) (sql.Row
if tblName.Name == "" {
tblName = delta.FromName
}
// TODO: schema name
diffStat, hasDiff, err := getDiffStatNodeFromDelta(ctx, delta, fromRefDetails.root, toRefDetails.root, tblName.Name)
diffStat, hasDiff, err := getDiffStatNodeFromDelta(ctx, delta, fromRefDetails.root, toRefDetails.root, tblName)
if err != nil {
if errors.Is(err, diff.ErrPrimaryKeySetChanged) {
ctx.Warn(dtables.PrimaryKeyChangeWarningCode, fmt.Sprintf("stat for table %s cannot be determined. Primary key set changed.", tblName))
Expand Down Expand Up @@ -369,10 +372,10 @@ func (ds *DiffStatTableFunction) evaluateArguments() (interface{}, interface{},

// getDiffStatNodeFromDelta returns diffStatNode object and whether there is data diff or not. It gets tables
// from roots and diff stat if there is a valid table exists in both fromRoot and toRoot.
func getDiffStatNodeFromDelta(ctx *sql.Context, delta diff.TableDelta, fromRoot, toRoot doltdb.RootValue, tableName string) (diffStatNode, bool, error) {
func getDiffStatNodeFromDelta(ctx *sql.Context, delta diff.TableDelta, fromRoot, toRoot doltdb.RootValue, tableName doltdb.TableName) (diffStatNode, bool, error) {
var oldColLen int
var newColLen int
fromTable, _, fromTableExists, err := doltdb.GetTableInsensitive(ctx, fromRoot, doltdb.TableName{Name: tableName})
fromTable, _, fromTableExists, err := doltdb.GetTableInsensitive(ctx, fromRoot, tableName)
if err != nil {
return diffStatNode{}, false, err
}
Expand All @@ -385,7 +388,7 @@ func getDiffStatNodeFromDelta(ctx *sql.Context, delta diff.TableDelta, fromRoot,
oldColLen = len(fromSch.GetAllCols().GetColumns())
}

toTable, _, toTableExists, err := doltdb.GetTableInsensitive(ctx, toRoot, doltdb.TableName{Name: tableName})
toTable, _, toTableExists, err := doltdb.GetTableInsensitive(ctx, toRoot, tableName)
if err != nil {
return diffStatNode{}, false, err
}
Expand All @@ -412,7 +415,7 @@ func getDiffStatNodeFromDelta(ctx *sql.Context, delta diff.TableDelta, fromRoot,
return diffStatNode{}, false, err
}

return diffStatNode{tableName, diffStat, oldColLen, newColLen, keyless}, hasDiff, nil
return diffStatNode{tableName.Name, diffStat, oldColLen, newColLen, keyless}, hasDiff, nil
}

// getDiffStat returns diff.DiffStatProgress object and whether there is a data diff or not.
Expand Down
6 changes: 3 additions & 3 deletions go/libraries/doltcore/sqle/dolt_patch_table_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ import (
"golang.org/x/exp/slices"

"github.com/dolthub/dolt/go/libraries/doltcore/diff"
"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
"github.com/dolthub/dolt/go/libraries/doltcore/env"
"github.com/dolthub/dolt/go/libraries/doltcore/schema"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/dsess"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/dtables"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/index"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/resolve"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/sqlfmt"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/sqlutil"
"github.com/dolthub/dolt/go/store/types"
Expand Down Expand Up @@ -145,11 +145,11 @@ func (p *PatchTableFunction) PartitionRows(ctx *sql.Context, partition sql.Parti

// If tableNameExpr defined, return a single table patch result
if p.tableNameExpr != nil {
fromTblExists, err := fromRefDetails.root.HasTable(ctx, doltdb.TableName{Name: tableName})
_, _, fromTblExists, err := resolve.Table(ctx, fromRefDetails.root, tableName)
if err != nil {
return nil, err
}
toTblExists, err := toRefDetails.root.HasTable(ctx, doltdb.TableName{Name: tableName})
_, _, toTblExists, err := resolve.Table(ctx, toRefDetails.root, tableName)
if err != nil {
return nil, err
}
Expand Down
10 changes: 3 additions & 7 deletions go/libraries/doltcore/sqle/dolt_schema_diff_table_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/dolthub/dolt/go/libraries/doltcore/diff"
"github.com/dolthub/dolt/go/libraries/doltcore/schema"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/dsess"
"github.com/dolthub/dolt/go/libraries/doltcore/table/editor"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/sqlfmt"
)

const schemaDiffDefaultRowCount = 100
Expand Down Expand Up @@ -314,18 +314,14 @@ func (ds *SchemaDiffTableFunction) RowIter(ctx *sql.Context, row sql.Row) (sql.R
var fromCreate, toCreate string

if delta.FromTable != nil {
fromSqlDb := NewUserSpaceDatabase(fromRoot, editor.Options{})
fromSqlCtx, fromEngine, _ := PrepareCreateTableStmt(ctx, fromSqlDb)
fromCreate, err = GetCreateTableStmt(fromSqlCtx, fromEngine, delta.FromName.Name)
fromCreate, err = sqlfmt.GenerateCreateTableStatement(delta.FromName.Name, delta.FromSch, delta.FromFks, delta.FromFksParentSch)
if err != nil {
return nil, err
}
}

if delta.ToTable != nil {
toSqlDb := NewUserSpaceDatabase(toRoot, editor.Options{})
toSqlCtx, toEngine, _ := PrepareCreateTableStmt(ctx, toSqlDb)
toCreate, err = GetCreateTableStmt(toSqlCtx, toEngine, delta.ToName.Name)
toCreate, err = sqlfmt.GenerateCreateTableStatement(delta.ToName.Name, delta.ToSch, delta.ToFks, delta.ToFksParentSch)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions go/libraries/doltcore/sqle/dtables/commit_diff_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/dolthub/dolt/go/libraries/doltcore/rowconv"
"github.com/dolthub/dolt/go/libraries/doltcore/schema"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/index"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/resolve"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/sqlutil"
"github.com/dolthub/dolt/go/store/types"
)
Expand Down Expand Up @@ -60,12 +61,11 @@ var _ sql.StatisticsTable = (*CommitDiffTable)(nil)
func NewCommitDiffTable(ctx *sql.Context, dbName, tblName string, ddb *doltdb.DoltDB, wRoot, sRoot doltdb.RootValue) (sql.Table, error) {
diffTblName := doltdb.DoltCommitDiffTablePrefix + tblName

// TODO: schema
table, _, ok, err := doltdb.GetTableInsensitive(ctx, wRoot, doltdb.TableName{Name: tblName})
_, table, tableExists, err := resolve.Table(ctx, wRoot, tblName)
if err != nil {
return nil, err
}
if !ok {
if !tableExists {
return nil, sql.ErrTableNotFound.New(diffTblName)
}

Expand Down

0 comments on commit a63bf91

Please sign in to comment.