From 7ac0e8e6b7504770d262edf1586f442730d5765c Mon Sep 17 00:00:00 2001 From: Taylor Bantle Date: Tue, 24 Sep 2024 12:20:36 -0700 Subject: [PATCH 1/4] Fix dolt_diff_stat for doltgres --- .../sqle/dolt_diff_stat_table_function.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/go/libraries/doltcore/sqle/dolt_diff_stat_table_function.go b/go/libraries/doltcore/sqle/dolt_diff_stat_table_function.go index ef31574986..8897837aa3 100644 --- a/go/libraries/doltcore/sqle/dolt_diff_stat_table_function.go +++ b/go/libraries/doltcore/sqle/dolt_diff_stat_table_function.go @@ -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 } @@ -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)) @@ -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 } @@ -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 } @@ -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. From d02b67941540435123d5bd4b69fb3508fe180849 Mon Sep 17 00:00:00 2001 From: Taylor Bantle Date: Tue, 24 Sep 2024 13:53:33 -0700 Subject: [PATCH 2/4] Fix dolt_patch and dolt_schema_diff for doltgres --- go/cmd/dolt/commands/schcmds/export.go | 2 +- go/cmd/dolt/commands/schcmds/import.go | 2 +- go/cmd/dolt/commands/schcmds/show.go | 2 +- .../doltcore/sqle/dolt_patch_table_function.go | 6 +++--- .../doltcore/sqle/dolt_schema_diff_table_function.go | 10 +++------- go/libraries/doltcore/sqle/show_create_table.go | 5 +++-- .../table/untyped/sqlexport/batch_sqlwriter.go | 2 +- .../doltcore/table/untyped/sqlexport/sqlwriter.go | 2 +- 8 files changed, 14 insertions(+), 17 deletions(-) diff --git a/go/cmd/dolt/commands/schcmds/export.go b/go/cmd/dolt/commands/schcmds/export.go index 300f1deb4e..f5af2f6c8c 100644 --- a/go/cmd/dolt/commands/schcmds/export.go +++ b/go/cmd/dolt/commands/schcmds/export.go @@ -151,7 +151,7 @@ func exportSchemas(ctx context.Context, apr *argparser.ArgParseResults, root dol func exportTblSchema(ctx context.Context, tblName string, root doltdb.RootValue, wr io.Writer, opts editor.Options) errhand.VerboseError { sqlCtx, engine, _ := dsqle.PrepareCreateTableStmt(ctx, dsqle.NewUserSpaceDatabase(root, opts)) - stmt, err := dsqle.GetCreateTableStmt(sqlCtx, engine, tblName) + stmt, err := dsqle.GetCreateTableStmt(sqlCtx, engine, doltdb.TableName{Name: tblName}) if err != nil { return errhand.VerboseErrorFromError(err) } diff --git a/go/cmd/dolt/commands/schcmds/import.go b/go/cmd/dolt/commands/schcmds/import.go index 3937bc41d7..43be2127ca 100644 --- a/go/cmd/dolt/commands/schcmds/import.go +++ b/go/cmd/dolt/commands/schcmds/import.go @@ -306,7 +306,7 @@ func importSchema(ctx context.Context, dEnv *env.DoltEnv, apr *argparser.ArgPars sqlDb := sqle.NewUserSpaceDatabase(root, editor.Options{}) sqlCtx, engine, _ := sqle.PrepareCreateTableStmt(ctx, sqlDb) - stmt, err := sqle.GetCreateTableStmt(sqlCtx, engine, tblName) + stmt, err := sqle.GetCreateTableStmt(sqlCtx, engine, doltdb.TableName{Name: tblName}) if err != nil { return errhand.VerboseErrorFromError(err) } diff --git a/go/cmd/dolt/commands/schcmds/show.go b/go/cmd/dolt/commands/schcmds/show.go index 4e8824123f..b56d1da024 100644 --- a/go/cmd/dolt/commands/schcmds/show.go +++ b/go/cmd/dolt/commands/schcmds/show.go @@ -154,7 +154,7 @@ func printSchemas(ctx context.Context, apr *argparser.ArgParseResults, dEnv *env notFound = append(notFound, tblName) } else { cli.Println(bold.Sprint(tblName), "@", cmStr) - stmt, err := dsqle.GetCreateTableStmt(sqlCtx, engine, tblName) + stmt, err := dsqle.GetCreateTableStmt(sqlCtx, engine, doltdb.TableName{Name: tblName}) if err != nil { return errhand.VerboseErrorFromError(err) } diff --git a/go/libraries/doltcore/sqle/dolt_patch_table_function.go b/go/libraries/doltcore/sqle/dolt_patch_table_function.go index 1a6732e60d..cd6b712b76 100644 --- a/go/libraries/doltcore/sqle/dolt_patch_table_function.go +++ b/go/libraries/doltcore/sqle/dolt_patch_table_function.go @@ -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" @@ -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 } diff --git a/go/libraries/doltcore/sqle/dolt_schema_diff_table_function.go b/go/libraries/doltcore/sqle/dolt_schema_diff_table_function.go index 2cb72393b0..71d85b3043 100644 --- a/go/libraries/doltcore/sqle/dolt_schema_diff_table_function.go +++ b/go/libraries/doltcore/sqle/dolt_schema_diff_table_function.go @@ -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 @@ -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 } diff --git a/go/libraries/doltcore/sqle/show_create_table.go b/go/libraries/doltcore/sqle/show_create_table.go index deab24085c..0d6d0103be 100644 --- a/go/libraries/doltcore/sqle/show_create_table.go +++ b/go/libraries/doltcore/sqle/show_create_table.go @@ -21,6 +21,7 @@ import ( sqle "github.com/dolthub/go-mysql-server" "github.com/dolthub/go-mysql-server/sql" + "github.com/dolthub/dolt/go/libraries/doltcore/doltdb" "github.com/dolthub/dolt/go/libraries/doltcore/env" "github.com/dolthub/dolt/go/libraries/doltcore/sqle/dsess" "github.com/dolthub/dolt/go/libraries/doltcore/sqle/writer" @@ -41,8 +42,8 @@ func PrepareCreateTableStmt(ctx context.Context, sqlDb dsess.SqlDatabase) (*sql. return sqlCtx, engine, sess } -func GetCreateTableStmt(ctx *sql.Context, engine *sqle.Engine, tableName string) (string, error) { - _, rowIter, _, err := engine.Query(ctx, fmt.Sprintf("SHOW CREATE TABLE `%s`;", tableName)) +func GetCreateTableStmt(ctx *sql.Context, engine *sqle.Engine, tableName doltdb.TableName) (string, error) { + _, rowIter, _, err := engine.Query(ctx, fmt.Sprintf("SHOW CREATE TABLE `%s`;", tableName.String())) if err != nil { return "", err } diff --git a/go/libraries/doltcore/table/untyped/sqlexport/batch_sqlwriter.go b/go/libraries/doltcore/table/untyped/sqlexport/batch_sqlwriter.go index 32aca3c252..f4c4617c4f 100644 --- a/go/libraries/doltcore/table/untyped/sqlexport/batch_sqlwriter.go +++ b/go/libraries/doltcore/table/untyped/sqlexport/batch_sqlwriter.go @@ -154,7 +154,7 @@ func (w *BatchSqlExportWriter) maybeWriteDropCreate(ctx context.Context) error { b.WriteString(sqlfmt.DropTableIfExistsStmt(w.tableName)) b.WriteRune('\n') sqlCtx, engine, _ := dsqle.PrepareCreateTableStmt(ctx, dsqle.NewUserSpaceDatabase(w.root, w.editOpts)) - createTableStmt, err := dsqle.GetCreateTableStmt(sqlCtx, engine, w.tableName) + createTableStmt, err := dsqle.GetCreateTableStmt(sqlCtx, engine, doltdb.TableName{Name: w.tableName}) if err != nil { return err } diff --git a/go/libraries/doltcore/table/untyped/sqlexport/sqlwriter.go b/go/libraries/doltcore/table/untyped/sqlexport/sqlwriter.go index b4c621bf3f..fd79bef236 100644 --- a/go/libraries/doltcore/table/untyped/sqlexport/sqlwriter.go +++ b/go/libraries/doltcore/table/untyped/sqlexport/sqlwriter.go @@ -129,7 +129,7 @@ func (w *SqlExportWriter) maybeWriteDropCreate(ctx context.Context) error { b.WriteString(sqlfmt.DropTableIfExistsStmt(w.tableName)) b.WriteRune('\n') sqlCtx, engine, _ := dsqle.PrepareCreateTableStmt(ctx, dsqle.NewUserSpaceDatabase(w.root, w.editOpts)) - createTableStmt, err := dsqle.GetCreateTableStmt(sqlCtx, engine, w.tableName) + createTableStmt, err := dsqle.GetCreateTableStmt(sqlCtx, engine, doltdb.TableName{Name: w.tableName}) if err != nil { return err } From 74c1208920ac08aa2cfd577ee9ec6957ef2e8444 Mon Sep 17 00:00:00 2001 From: Taylor Bantle Date: Tue, 24 Sep 2024 14:12:51 -0700 Subject: [PATCH 3/4] Fix dolt_commit_diff_$ --- go/libraries/doltcore/sqle/dtables/commit_diff_table.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/go/libraries/doltcore/sqle/dtables/commit_diff_table.go b/go/libraries/doltcore/sqle/dtables/commit_diff_table.go index 7406ad64fa..886394a814 100644 --- a/go/libraries/doltcore/sqle/dtables/commit_diff_table.go +++ b/go/libraries/doltcore/sqle/dtables/commit_diff_table.go @@ -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" ) @@ -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) } From 27a3056cae2ce98c4c9318b9270117a0f8122eb8 Mon Sep 17 00:00:00 2001 From: Taylor Bantle Date: Tue, 24 Sep 2024 14:49:37 -0700 Subject: [PATCH 4/4] Revert GetCreateTableStmt changes --- go/cmd/dolt/commands/schcmds/export.go | 2 +- go/cmd/dolt/commands/schcmds/import.go | 2 +- go/cmd/dolt/commands/schcmds/show.go | 2 +- go/libraries/doltcore/sqle/show_create_table.go | 5 ++--- .../doltcore/table/untyped/sqlexport/batch_sqlwriter.go | 2 +- go/libraries/doltcore/table/untyped/sqlexport/sqlwriter.go | 2 +- 6 files changed, 7 insertions(+), 8 deletions(-) diff --git a/go/cmd/dolt/commands/schcmds/export.go b/go/cmd/dolt/commands/schcmds/export.go index f5af2f6c8c..300f1deb4e 100644 --- a/go/cmd/dolt/commands/schcmds/export.go +++ b/go/cmd/dolt/commands/schcmds/export.go @@ -151,7 +151,7 @@ func exportSchemas(ctx context.Context, apr *argparser.ArgParseResults, root dol func exportTblSchema(ctx context.Context, tblName string, root doltdb.RootValue, wr io.Writer, opts editor.Options) errhand.VerboseError { sqlCtx, engine, _ := dsqle.PrepareCreateTableStmt(ctx, dsqle.NewUserSpaceDatabase(root, opts)) - stmt, err := dsqle.GetCreateTableStmt(sqlCtx, engine, doltdb.TableName{Name: tblName}) + stmt, err := dsqle.GetCreateTableStmt(sqlCtx, engine, tblName) if err != nil { return errhand.VerboseErrorFromError(err) } diff --git a/go/cmd/dolt/commands/schcmds/import.go b/go/cmd/dolt/commands/schcmds/import.go index 43be2127ca..3937bc41d7 100644 --- a/go/cmd/dolt/commands/schcmds/import.go +++ b/go/cmd/dolt/commands/schcmds/import.go @@ -306,7 +306,7 @@ func importSchema(ctx context.Context, dEnv *env.DoltEnv, apr *argparser.ArgPars sqlDb := sqle.NewUserSpaceDatabase(root, editor.Options{}) sqlCtx, engine, _ := sqle.PrepareCreateTableStmt(ctx, sqlDb) - stmt, err := sqle.GetCreateTableStmt(sqlCtx, engine, doltdb.TableName{Name: tblName}) + stmt, err := sqle.GetCreateTableStmt(sqlCtx, engine, tblName) if err != nil { return errhand.VerboseErrorFromError(err) } diff --git a/go/cmd/dolt/commands/schcmds/show.go b/go/cmd/dolt/commands/schcmds/show.go index b56d1da024..4e8824123f 100644 --- a/go/cmd/dolt/commands/schcmds/show.go +++ b/go/cmd/dolt/commands/schcmds/show.go @@ -154,7 +154,7 @@ func printSchemas(ctx context.Context, apr *argparser.ArgParseResults, dEnv *env notFound = append(notFound, tblName) } else { cli.Println(bold.Sprint(tblName), "@", cmStr) - stmt, err := dsqle.GetCreateTableStmt(sqlCtx, engine, doltdb.TableName{Name: tblName}) + stmt, err := dsqle.GetCreateTableStmt(sqlCtx, engine, tblName) if err != nil { return errhand.VerboseErrorFromError(err) } diff --git a/go/libraries/doltcore/sqle/show_create_table.go b/go/libraries/doltcore/sqle/show_create_table.go index 0d6d0103be..deab24085c 100644 --- a/go/libraries/doltcore/sqle/show_create_table.go +++ b/go/libraries/doltcore/sqle/show_create_table.go @@ -21,7 +21,6 @@ import ( sqle "github.com/dolthub/go-mysql-server" "github.com/dolthub/go-mysql-server/sql" - "github.com/dolthub/dolt/go/libraries/doltcore/doltdb" "github.com/dolthub/dolt/go/libraries/doltcore/env" "github.com/dolthub/dolt/go/libraries/doltcore/sqle/dsess" "github.com/dolthub/dolt/go/libraries/doltcore/sqle/writer" @@ -42,8 +41,8 @@ func PrepareCreateTableStmt(ctx context.Context, sqlDb dsess.SqlDatabase) (*sql. return sqlCtx, engine, sess } -func GetCreateTableStmt(ctx *sql.Context, engine *sqle.Engine, tableName doltdb.TableName) (string, error) { - _, rowIter, _, err := engine.Query(ctx, fmt.Sprintf("SHOW CREATE TABLE `%s`;", tableName.String())) +func GetCreateTableStmt(ctx *sql.Context, engine *sqle.Engine, tableName string) (string, error) { + _, rowIter, _, err := engine.Query(ctx, fmt.Sprintf("SHOW CREATE TABLE `%s`;", tableName)) if err != nil { return "", err } diff --git a/go/libraries/doltcore/table/untyped/sqlexport/batch_sqlwriter.go b/go/libraries/doltcore/table/untyped/sqlexport/batch_sqlwriter.go index f4c4617c4f..32aca3c252 100644 --- a/go/libraries/doltcore/table/untyped/sqlexport/batch_sqlwriter.go +++ b/go/libraries/doltcore/table/untyped/sqlexport/batch_sqlwriter.go @@ -154,7 +154,7 @@ func (w *BatchSqlExportWriter) maybeWriteDropCreate(ctx context.Context) error { b.WriteString(sqlfmt.DropTableIfExistsStmt(w.tableName)) b.WriteRune('\n') sqlCtx, engine, _ := dsqle.PrepareCreateTableStmt(ctx, dsqle.NewUserSpaceDatabase(w.root, w.editOpts)) - createTableStmt, err := dsqle.GetCreateTableStmt(sqlCtx, engine, doltdb.TableName{Name: w.tableName}) + createTableStmt, err := dsqle.GetCreateTableStmt(sqlCtx, engine, w.tableName) if err != nil { return err } diff --git a/go/libraries/doltcore/table/untyped/sqlexport/sqlwriter.go b/go/libraries/doltcore/table/untyped/sqlexport/sqlwriter.go index fd79bef236..b4c621bf3f 100644 --- a/go/libraries/doltcore/table/untyped/sqlexport/sqlwriter.go +++ b/go/libraries/doltcore/table/untyped/sqlexport/sqlwriter.go @@ -129,7 +129,7 @@ func (w *SqlExportWriter) maybeWriteDropCreate(ctx context.Context) error { b.WriteString(sqlfmt.DropTableIfExistsStmt(w.tableName)) b.WriteRune('\n') sqlCtx, engine, _ := dsqle.PrepareCreateTableStmt(ctx, dsqle.NewUserSpaceDatabase(w.root, w.editOpts)) - createTableStmt, err := dsqle.GetCreateTableStmt(sqlCtx, engine, doltdb.TableName{Name: w.tableName}) + createTableStmt, err := dsqle.GetCreateTableStmt(sqlCtx, engine, w.tableName) if err != nil { return err }