From 117d416300800c3bdcef32891452bca0188b7691 Mon Sep 17 00:00:00 2001 From: Christian De la Hoz Date: Tue, 31 Aug 2021 20:30:45 +0200 Subject: [PATCH] time is hard... --- corpus/select_statement/value_expression.txt | 34 ++++++++++++++++++++ grammar.js | 15 ++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/corpus/select_statement/value_expression.txt b/corpus/select_statement/value_expression.txt index 4d51346..2dfcd6b 100644 --- a/corpus/select_statement/value_expression.txt +++ b/corpus/select_statement/value_expression.txt @@ -244,3 +244,37 @@ SELECT (identifier) (comparison_null))))) +================================================================================ +time expressions +================================================================================ +SELECT + foo::interval, + foo at time zone 'utc', + interval '1 day', + interval '1' hour to second, + interval '1' day + interval '3' month; +-------------------------------------------------------------------------------- + +(source_file + (select_statement + (select_item + (op_expression + (identifier) + (cast) + (identifier))) + (select_item + (time_expression + (identifier) + (string))) + (select_item + (time_expression + (string))) + (select_item + (time_expression + (string))) + (select_item + (op_expression + (time_expression + (string)) + (time_expression + (string)))))) diff --git a/grammar.js b/grammar.js index c167078..24878df 100644 --- a/grammar.js +++ b/grammar.js @@ -741,6 +741,19 @@ module.exports = grammar({ time_expression: $ => choice( seq($.identifier, kw("at"), kw("time"), kw("zone"), $._value_expression), + // TODO(chrde): this is plain wrong - https://www.postgresql.org/docs/13/datatype-datetime.html + seq(kw("interval"), $.string, optional($._interval_fields)), + ), + + _interval_fields: $ => choice( + kw("year"), kw("month"), kw("day"), kw("hour"), kw("minute"), kw("second"), + seq(kw("year"), kw("to"), kw("month")), + seq(kw("day"), kw("to"), kw("hour")), + seq(kw("day"), kw("to"), kw("minute")), + seq(kw("day"), kw("to"), kw("second")), + seq(kw("hour"), kw("to"), kw("minute")), + seq(kw("hour"), kw("to"), kw("second")), + seq(kw("minute"), kw("to"), kw("second")), ), function_call: $ => seq( @@ -754,7 +767,7 @@ module.exports = grammar({ ), op_expression: $ => choice( - prec.left(12, seq($._value_expression, $.cast, $._value_expression)), + prec.left(12, seq($._value_expression, $.cast, $.identifier)), // array access prec.right(10, seq(choice($.minus, $.plus), $._value_expression)), // ^