Skip to content

Commit

Permalink
Fix DEFAULT insert for NULL or unspecified column default values (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jycor authored Nov 14, 2024
1 parent b704225 commit 6014896
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
8 changes: 7 additions & 1 deletion server/analyzer/assign_insert_casts.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/dolthub/go-mysql-server/sql/expression"
"github.com/dolthub/go-mysql-server/sql/plan"
"github.com/dolthub/go-mysql-server/sql/transform"
"github.com/dolthub/go-mysql-server/sql/types"

pgexprs "github.com/dolthub/doltgresql/server/expression"
pgtypes "github.com/dolthub/doltgresql/server/types"
Expand Down Expand Up @@ -60,7 +61,12 @@ func AssignInsertCasts(ctx *sql.Context, a *analyzer.Analyzer, node sql.Node, sc
for rowIndex, rowExprs := range values.ExpressionTuples {
newValues[rowIndex] = make([]sql.Expression, len(rowExprs))
for columnIndex, colExpr := range rowExprs {
fromColType, ok := colExpr.Type().(pgtypes.DoltgresType)
// Null ColumnDefaultValues or empty DefaultValues are not properly typed in TypeSanitizer, so we must handle them here
colExprType := colExpr.Type()
if colExprType == nil || colExprType == types.Null {
colExprType = pgtypes.UnknownType{}
}
fromColType, ok := colExprType.(pgtypes.DoltgresType)
if !ok {
return nil, transform.NewTree, fmt.Errorf("INSERT: non-Doltgres type found in values source: %s", fromColType.String())
}
Expand Down
18 changes: 18 additions & 0 deletions testing/go/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,23 @@ func TestInsert(t *testing.T) {
},
},
},
{
Name: "null and unspecified default values",
SetUpScript: []string{
"CREATE TABLE t (i INT DEFAULT NULL, j INT)",
},
Assertions: []ScriptTestAssertion{
{
Query: "INSERT INTO t VALUES (default, default)",
SkipResultsCheck: true,
},
{
Query: "SELECT * FROM t",
Expected: []sql.Row{
{nil, nil},
},
},
},
},
})
}

0 comments on commit 6014896

Please sign in to comment.