Skip to content

Commit

Permalink
[Typing] Default Value (#904)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 authored Sep 13, 2024
1 parent a6f4841 commit a73f35c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
7 changes: 7 additions & 0 deletions clients/shared/default_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ func DefaultValue(column columns.Column, dialect sql.Dialect) (any, error) {
return sql.QuoteLiteral(extTime.String(column.KindDetails.ExtendedTimeDetails.Format)), nil
}
case typing.EDecimal.Kind:
if column.KindDetails.ExtendedDecimalDetails.Scale() == 0 {
switch column.DefaultValue().(type) {
case int, int8, int16, int32, int64:
return fmt.Sprint(column.DefaultValue()), nil
}
}

decimalValue, err := typing.AssertType[*decimal.Decimal](column.DefaultValue())
if err != nil {
return nil, err
Expand Down
12 changes: 9 additions & 3 deletions clients/shared/default_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,26 @@ func TestColumn_DefaultValue(t *testing.T) {
assert.Equal(t, expectedValue, actualValue, fmt.Sprintf("%s %s", testCase.name, dialect))
}
}

{
// Decimal value
{
// Type *decimal.Decimal
decimalValue := decimal.NewDecimal(numbers.MustParseDecimal("3.14159"))
col := columns.NewColumnWithDefaultValue("", typing.EDecimal, decimalValue)
col := columns.NewColumnWithDefaultValue("", typing.NewDecimalDetailsFromTemplate(typing.EDecimal, decimal.NewDetails(7, 5)), decimalValue)
value, err := DefaultValue(col, redshiftDialect.RedshiftDialect{})
assert.NoError(t, err)
assert.Equal(t, "3.14159", value)
}
{
// Type int64
col := columns.NewColumnWithDefaultValue("", typing.NewDecimalDetailsFromTemplate(typing.EDecimal, decimal.NewDetails(5, 0)), int64(123))
value, err := DefaultValue(col, redshiftDialect.RedshiftDialect{})
assert.NoError(t, err)
assert.Equal(t, "123", value)
}
{
// Wrong type (string)
col := columns.NewColumnWithDefaultValue("", typing.EDecimal, "hello")
col := columns.NewColumnWithDefaultValue("", typing.NewDecimalDetailsFromTemplate(typing.EDecimal, decimal.NewDetails(7, 5)), "hello")
_, err := DefaultValue(col, redshiftDialect.RedshiftDialect{})
assert.ErrorContains(t, err, "expected type *decimal.Decimal, got string")
}
Expand Down

0 comments on commit a73f35c

Please sign in to comment.