Skip to content

Commit

Permalink
support DATE, TIME, and TIMESTAMP literal parsing (#346)
Browse files Browse the repository at this point in the history
  • Loading branch information
jycor authored May 20, 2024
1 parent 966f8cb commit ff7bd63
Show file tree
Hide file tree
Showing 4 changed files with 7,561 additions and 7,463 deletions.
4 changes: 4 additions & 0 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -5448,6 +5448,10 @@ func NewBitVal(in []byte) *SQLVal {
return &SQLVal{Type: BitVal, Val: in}
}

// TODO: implement a NewDateVal()
// TODO: implement a NewTimeVal()
// TODO: implement a NewTimestampVal()

// NewValArg builds a new ValArg.
func NewValArg(in []byte) *SQLVal {
return &SQLVal{Type: ValArg, Val: in}
Expand Down
59 changes: 59 additions & 0 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4022,6 +4022,55 @@ var (
"\ti int\n" +
") insert_method last",
},

// Date, Time, and Timestamp literals
{
input: "select date '2020-10-01'",
output: "select '2020-10-01'",
},
{
input: "select time '2020-10-01'",
output: "select '2020-10-01'",
},
{
input: "select timestamp '2020-10-01'",
output: "select '2020-10-01'",
},

{
input: "select date'2020-10-01'",
output: "select '2020-10-01'",
},
{
input: "select time'2020-10-01'",
output: "select '2020-10-01'",
},
{
input: "select timestamp'2020-10-01'",
output: "select '2020-10-01'",
},

{
input: "select (date '2020-10-01')",
output: "select ('2020-10-01')",
},
{
input: "select (time '2020-10-01')",
output: "select ('2020-10-01')",
},
{
input: "select (timestamp '2020-10-01')",
output: "select ('2020-10-01')",
},

{
input: "insert into t values (date '2020-10-01'), (time '2020-10-01'), (timestamp '2020-10-01')",
output: "insert into t values ('2020-10-01'), ('2020-10-01'), ('2020-10-01')",
},
{
input: "select * from (values row(date '2020-10-01', time '12:34:56', timestamp '2001-02-03 12:34:56')) t;",
output: "select * from (values row('2020-10-01', '12:34:56', '2001-02-03 12:34:56')) as t",
},
}

// Any tests that contain multiple statements within the body (such as BEGIN/END blocks) should go here.
Expand Down Expand Up @@ -5012,6 +5061,7 @@ func TestInvalid(t *testing.T) {
invalidSQL := []struct {
input string
err string
skip bool
}{
{
input: "SET @foo = `o` `ne`;",
Expand Down Expand Up @@ -5183,6 +5233,15 @@ func TestInvalid(t *testing.T) {
input: "select * from tbl into outfile 'outfile.txt' lines starting by 'd' terminated by 'e' starting by 'd' terminated by 'e'",
err: "syntax error",
},

{
input: "select date 20010203",
err: "syntax error",
},
{
input: "select date concat('2001-', '02-', '03')",
err: "syntax error",
},
}

for _, tcase := range invalidSQL {
Expand Down
Loading

0 comments on commit ff7bd63

Please sign in to comment.