Skip to content

Commit

Permalink
support range literal
Browse files Browse the repository at this point in the history
  • Loading branch information
kitta65 committed Feb 29, 2024
1 parent 281f381 commit 651b252
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/cst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ pub enum NodeType {
PivotConfig, // (SUM(c1) FOR c2 IN (v1, v2))
PivotOperator,
RaiseStatement,
RangeLiteral, // RANGE<DATE> '[2023-01-01, 2024-01-01)'
RenameColumnClause,
RepeatStatement,
RevokeStatement,
Expand Down
11 changes: 9 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1293,10 +1300,10 @@ impl Parser {
}
fn parse_type(&mut self, schema: bool) -> BQ2CSTResult<Node> {
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)?);
Expand Down
20 changes: 20 additions & 0 deletions src/parser/tests/tests_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,26 @@ exprs:
self: FLOAT64 (Type)
rparen:
self: > (Symbol)
",
0,
)),
// ----- range -----
Box::new(SuccessTestCase::new(
"\
SELECT RANGE<DATE> '[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,
)),
Expand Down
7 changes: 7 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 651b252

Please sign in to comment.