From 651b2526de2d94f3393fb1c2b7fc4b8505c887fa Mon Sep 17 00:00:00 2001 From: dr666m1 <26474260+kitta65@users.noreply.github.com> Date: Thu, 29 Feb 2024 23:41:46 +0900 Subject: [PATCH] support range literal --- src/cst.rs | 1 + src/parser.rs | 11 +++++++++-- src/parser/tests/tests_core.rs | 20 ++++++++++++++++++++ src/types.rs | 7 +++++++ 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/cst.rs b/src/cst.rs index cfcbe9d..90925a6 100644 --- a/src/cst.rs +++ b/src/cst.rs @@ -107,6 +107,7 @@ pub enum NodeType { PivotConfig, // (SUM(c1) FOR c2 IN (v1, v2)) PivotOperator, RaiseStatement, + RangeLiteral, // RANGE '[2023-01-01, 2024-01-01)' RenameColumnClause, RepeatStatement, RevokeStatement, diff --git a/src/parser.rs b/src/parser.rs index c4ec01b..84026bc 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -377,6 +377,13 @@ impl Parser { struct_literal.push_node("type", type_); left = struct_literal; } + "RANGE" => { + let type_ = self.parse_type(false)?; + self.next_token()?; // > -> '[lower, upper)' + let mut range_literal = self.construct_node(NodeType::RangeLiteral)?; + range_literal.push_node("type", type_); + left = range_literal; + } // ARRAY "[" => { left.node_type = NodeType::ArrayLiteral; @@ -1293,10 +1300,10 @@ impl Parser { } fn parse_type(&mut self, schema: bool) -> BQ2CSTResult { let mut res = match self.get_token(0)?.literal.to_uppercase().as_str() { - "ARRAY" => { + "ARRAY" | "RANGE" => { let mut res = self.construct_node(NodeType::Type)?; if self.get_token(1)?.literal.as_str() == "<" { - self.next_token()?; // ARRAY -> < + self.next_token()?; // -> < let mut type_ = self.construct_node(NodeType::GroupedType)?; self.next_token()?; // < -> type type_.push_node("type", self.parse_type(schema)?); diff --git a/src/parser/tests/tests_core.rs b/src/parser/tests/tests_core.rs index 7ade035..f94f554 100644 --- a/src/parser/tests/tests_core.rs +++ b/src/parser/tests/tests_core.rs @@ -871,6 +871,26 @@ exprs: self: FLOAT64 (Type) rparen: self: > (Symbol) +", + 0, + )), + // ----- range ----- + Box::new(SuccessTestCase::new( + "\ +SELECT RANGE '[2023-01-01, 2024-01-01)' +", + "\ +self: SELECT (SelectStatement) +exprs: +- self: '[2023-01-01, 2024-01-01)' (RangeLiteral) + type: + self: RANGE (Type) + type_declaration: + self: < (GroupedType) + rparen: + self: > (Symbol) + type: + self: DATE (Type) ", 0, )), diff --git a/src/types.rs b/src/types.rs index 28bcb8d..4cc38e6 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1168,6 +1168,13 @@ export type RaiseStatement = XXXStatement & { }; }; +export type RangeLiteral = Expr & { + node_type: "RangeLiteral"; + children: { + type: NodeChild; + }; +}; + export type RenameColumnClause = BaseNode & { token: Token; node_type: "RenameColumnClause";