Skip to content

Commit fb15fbd

Browse files
authored
Strip ast::Query when it only encloses a ast::Expr (#154)
1 parent 1bea379 commit fb15fbd

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

partiql-parser/src/parse/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
//! Provides the [`parse_partiql`] function to parse a PartiQL query.
44
5+
mod parse_util;
6+
57
use crate::error::{ParseError, UnexpectedTokenData};
68
use crate::lexer;
79
use crate::preprocessor::{built_ins, FnExprSet, PreprocessingPartiqlLexer};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use partiql_ast::ast;
2+
3+
// if this is just a parenthesized expr, lift it out of the query AST, otherwise return input
4+
// e.g. `(1+2)` should be a ExprKind::Expr, not wrapped deep in a ExprKind::Query
5+
pub(crate) fn strip_query(q: Box<ast::Expr>) -> Box<ast::Expr> {
6+
if let ast::ExprKind::Query(ast::AstNode {
7+
node:
8+
ast::Query {
9+
set:
10+
ast::AstNode {
11+
node: ast::QuerySet::Expr(e),
12+
..
13+
},
14+
order_by: None,
15+
limit: None,
16+
offset: None,
17+
},
18+
..
19+
}) = q.kind
20+
{
21+
e
22+
} else {
23+
q
24+
}
25+
}

partiql-parser/src/parse/partiql.lalrpop

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use partiql_ast::ast::ToAstNode;
1111

1212
use partiql_source_map::location::{ByteOffset, BytePosition, Location, ToLocated};
1313

14+
use crate::parse::parse_util::{strip_query};
15+
1416

1517
grammar<'input, 'err>(input: &'input str,
1618
errors: &'err mut Vec<ErrorRecovery<ByteOffset, lexer::Token<'input>, ParseError<'input, BytePosition>>>);
@@ -800,7 +802,7 @@ ExprPrecedence01: ast::Expr = {
800802
};
801803

802804
ExprTerm: ast::Expr = {
803-
"(" <q:Query> ")" => *q,
805+
"(" <q:Query> ")" => *strip_query(q),
804806
<lo:@L> <lit:Literal> <hi:@R> => ast::Expr{ kind: ast::ExprKind::Lit( lit.ast(lo..hi) ) },
805807
<VarRefExpr>,
806808
<ExprTermCollection>,

0 commit comments

Comments
 (0)