Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions go/cmd/dolt/commands/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -1146,9 +1146,9 @@ func schemaFromCreateTableStmt(createTableStmt string) (schema.Schema, error) {
IsPartOfPK: primaryCols[col.Name.Lowered()],
TypeInfo: typeInfo,
Default: defBuf.String(),
Generated: "", // TODO
OnUpdate: "", // TODO
Virtual: false, // TODO
Generated: genBuf.String(),
OnUpdate: onUpBuf.String(),
Virtual: col.Type.GeneratedExpr != nil && !bool(col.Type.Stored),
AutoIncrement: col.Type.Autoincrement == true,
Comment: comment,
}
Expand Down
6 changes: 3 additions & 3 deletions go/libraries/doltcore/sqle/sqlfmt/row_fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ func SqlRowAsDeleteStmt(ctx *sql.Context, r sql.Row, tableName string, tableSch
// The row to change is keyed by the primary key columns of
// |tableSch|, using their values from |r|.
//
// TODO(elianddb): Schema isn't recording column's Generated marker
// correctly, so Column.IsGenerated doesn't filter.
// Generated columns are skipped, matching InsertStatementPrefix and
// SqlRowAsTupleString.
func SqlRowAsUpdateStmt(ctx *sql.Context, r sql.Row, tableName string, tableSch schema.Schema, colsToUpdate *set.StrSet) (string, error) {
var b strings.Builder
b.WriteString("UPDATE ")
Expand All @@ -300,7 +300,7 @@ func SqlRowAsUpdateStmt(ctx *sql.Context, r sql.Row, tableName string, tableSch
i := 0
seenOne := false
err := tableSch.GetAllCols().Iter(func(_ uint64, col schema.Column) (stop bool, err error) {
if colsToUpdate.Contains(col.Name) {
if colsToUpdate.Contains(col.Name) && !col.IsGenerated() {
if seenOne {
b.WriteRune(',')
}
Expand Down
12 changes: 12 additions & 0 deletions go/libraries/doltcore/sqle/sqlfmt/row_fmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/dolthub/dolt/go/libraries/doltcore/schema/typeinfo"
_ "github.com/dolthub/dolt/go/libraries/doltcore/sqle"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/sqlfmt"
"github.com/dolthub/dolt/go/libraries/utils/set"
"github.com/dolthub/dolt/go/store/types"
)

Expand Down Expand Up @@ -83,6 +84,17 @@ func TestInsertStatementPrefixSkipsGeneratedCols(t *testing.T) {
assert.Equal(t, "INSERT INTO `table_name` (`id`,`a`) VALUES ", prefix)
}

func TestSqlRowAsUpdateStmtSkipsGeneratedCols(t *testing.T) {
// See https://github.com/dolthub/dolt/issues/11445
sch := newGeneratedColSchema()
colsToUpdate := set.NewStrSet([]string{"a", "c"})

stmt, err := sqlfmt.SqlRowAsUpdateStmt(sql.NewEmptyContext(), sql.Row{int64(1), "x", "x!"}, "table_name", sch, colsToUpdate)

require.NoError(t, err)
assert.Equal(t, "UPDATE `table_name` SET `a`='x' WHERE `id`=1;", stmt)
}

func TestSqlRowAsTupleString(t *testing.T) {
// See https://github.com/dolthub/dolt/issues/11439
sch := newGeneratedColSchema()
Expand Down
6 changes: 5 additions & 1 deletion go/libraries/doltcore/sqle/sqlfmt/schema_fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ func GenerateDataDiffStatement(ctx *sql.Context, tableName string, sch schema.Sc
updatedCols := set.NewEmptyStrSet()
for i, diffType := range colDiffTypes {
if diffType != diff.None {
updatedCols.Add(sch.GetAllCols().GetByIndex(i).Name)
col := sch.GetAllCols().GetByIndex(i)
if col.IsGenerated() {
continue
}
updatedCols.Add(col.Name)
}
}
if updatedCols.Size() == 0 {
Expand Down
35 changes: 34 additions & 1 deletion integration-tests/bats/sql-diff.bats
Original file line number Diff line number Diff line change
Expand Up @@ -970,4 +970,37 @@ SQL
[[ "$output" =~ "dolt_ignore | create ignored table" ]] || false
! [[ "$output" =~ "ignore_table" ]] || false

}
}

@test "sql-diff: stored generated columns omitted from INSERT and UPDATE" {
# See https://github.com/dolthub/dolt/issues/11445
dolt sql <<SQL
CREATE TABLE t (
id INT PRIMARY KEY,
a INT,
g INT GENERATED ALWAYS AS (a + 1) STORED
);
SQL
dolt add -A
dolt commit -m "create table with stored generated column"
dolt sql -q "INSERT INTO t (id, a) VALUES (1, 10)"
dolt add -A
dolt commit -m "seed row"
dolt sql -q "UPDATE t SET a=99 WHERE id=1"
dolt sql -q "INSERT INTO t (id, a) VALUES (2, 20)"

run dolt diff -r sql
[ "$status" -eq 0 ]
[[ "$output" =~ "UPDATE \`t\` SET \`a\`=99 WHERE \`id\`=1;" ]] || false
[[ "$output" =~ "INSERT INTO \`t\` (\`id\`,\`a\`) VALUES (2,20);" ]] || false
! [[ "$output" =~ "\`g\`" ]] || false

dolt diff -r sql > query
dolt reset --hard
run dolt sql < query
[ "$status" -eq 0 ]
run dolt sql -r csv -q "SELECT id, a, g FROM t ORDER BY id"
[ "$status" -eq 0 ]
[[ "${lines[1]}" = "1,99,100" ]] || false
[[ "${lines[2]}" = "2,20,21" ]] || false
}
Loading