Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support current_timestamp() and current_date() in default #91

Merged
merged 1 commit into from
May 9, 2023
Merged
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
15 changes: 14 additions & 1 deletion server/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,20 @@ func (db *database) CreateTable(ctx context.Context, stmt *ast.CreateTable) erro
// TODO: support array literal for default expression
s += ` DEFAULT "[]"`
} else {
s += fmt.Sprintf(" DEFAULT %s", col.ast.DefaultExpr.Expr.SQL())
defSQL := col.ast.DefaultExpr.Expr.SQL()

// workaround: convert default expression for sqlite
switch exp := col.ast.DefaultExpr.Expr.(type) {
case *ast.CallExpr:
switch strings.ToUpper(exp.Func.Name) {
case "CURRENT_TIMESTAMP":
defSQL = "CURRENT_TIMESTAMP"
case "CURRENT_DATE":
defSQL = "CURRENT_DATE"
}
}

s += fmt.Sprintf(" DEFAULT %s", defSQL)
}
}
if col.valueType.Code == TCString && col.isSized && !col.isMax {
Expand Down
6 changes: 6 additions & 0 deletions server/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ CREATE INDEX FullTypesByTimestamp ON FullTypes(FTTimestamp);
N INT64 NOT NULL DEFAULT (1),
X STRING(32) NOT NULL DEFAULT ("x"),
Y ARRAY<INT64> NOT NULL DEFAULT (ARRAY<INT64>[10, 20]),
T1 TIMESTAMP NOT NULL DEFAULT (CURRENT_TIMESTAMP()),
T2 TIMESTAMP NOT NULL DEFAULT (current_timestamp()),
Date TIMESTAMP NOT NULL DEFAULT (CURRENT_DATE()),
) PRIMARY KEY(Id);
`

Expand Down Expand Up @@ -3917,6 +3920,9 @@ func TestInformationSchema(t *testing.T) {
{"", "", "DefaultValues", "N", int64(3), nil, nil, "NO", "INT64"},
{"", "", "DefaultValues", "X", int64(4), nil, nil, "NO", "STRING(32)"},
{"", "", "DefaultValues", "Y", int64(5), nil, nil, "NO", "ARRAY<INT64>"},
{"", "", "DefaultValues", "T1", int64(6), nil, nil, "NO", "TIMESTAMP"},
{"", "", "DefaultValues", "T2", int64(7), nil, nil, "NO", "TIMESTAMP"},
{"", "", "DefaultValues", "Date", int64(8), nil, nil, "NO", "TIMESTAMP"},
{"", "", "From", "ALL", int64(1), nil, nil, "NO", "INT64"},
{"", "", "From", "CAST", int64(2), nil, nil, "NO", "INT64"},
{"", "", "From", "JOIN", int64(3), nil, nil, "NO", "INT64"},
Expand Down