Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/sql-parser/src/ast/defs/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1337,7 +1337,7 @@ pub struct Function<T: AstInfo> {
pub args: FunctionArgs<T>,
// aggregate functions may specify e.g. `COUNT(DISTINCT X) FILTER (WHERE ...)`
pub filter: Option<Box<Expr<T>>>,
pub over: Option<WindowSpec<T>>,
pub over: Option<Box<WindowSpec<T>>>,
// aggregate functions may specify eg `COUNT(DISTINCT x)`
pub distinct: bool,
}
Expand Down
2 changes: 1 addition & 1 deletion src/sql-parser/src/ast/defs/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub enum SetExpr<T: AstInfo> {
right: Box<SetExpr<T>>,
},
Values(Values<T>),
Show(ShowStatement<T>),
Show(Box<ShowStatement<T>>),
Table(T::ItemName),
}

Expand Down
2 changes: 1 addition & 1 deletion src/sql-parser/src/ast/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
//! name: folder.fold_item_name(node.name),
//! args: folder.fold_function_args(node.args),
//! filter: node.filter.map(|filter| Box::new(folder.fold_expr(*filter))),
//! over: node.over.map(|over| folder.fold_window_spec(over)),
//! over: node.over.map(|over| Box::new(folder.fold_window_spec(*over))),
//! distinct: node.distinct,
//! }
//! }
Expand Down
60 changes: 29 additions & 31 deletions src/sql-parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,26 +407,27 @@ impl<'a> Parser<'a> {

fn parse_statements(&mut self) -> Result<Vec<StatementParseResult<'a>>, ParserStatementError> {
let mut stmts = Vec::new();
let mut expecting_statement_delimiter = false;
loop {
// ignore empty statements (between successive statement delimiters)
while self.consume_token(&Token::Semicolon) {
expecting_statement_delimiter = false;
while self.consume_token(&Token::Semicolon) {}
if self.peek_token().is_none() {
return Ok(stmts);
}

if self.peek_token().is_none() {
break;
} else if expecting_statement_delimiter {
let stmt = self.parse_statement()?;
if let Some(next) = self.peek_token()
&& next != Token::Semicolon
{
return self
.expected(self.peek_pos(), "end of statement", self.peek_token())
.expected(self.peek_pos(), "end of statement", Some(next))
.map_no_statement_parser_err();
}

let s = self.parse_statement()?;
stmts.push(s);
expecting_statement_delimiter = true;
if stmts.is_empty() {
// Most batches contain one statement, and each spare slot is a full AST node.
stmts.reserve_exact(1);
}
stmts.push(stmt);
}
Ok(stmts)
}
/// Parse a single top-level statement (such as SELECT, INSERT, CREATE, etc.),
/// stopping before the statement separator, if any. Returns the parsed statement and the SQL
Expand Down Expand Up @@ -543,22 +544,19 @@ impl<'a> Parser<'a> {
// a top-level `Statement::Show`. Unwrap the degenerate
// wrapper so the AST is independent of redundant parens
// and the parse + display + reparse round trip is stable.
if let Query {
ctes: CteBlock::Simple(ctes),
body: SetExpr::Show(show),
order_by,
limit: None,
offset: None,
} = &query
{
if ctes.is_empty() && order_by.is_empty() {
return Ok(Statement::Show(show.clone()));
}
}
Ok(Statement::Select(SelectStatement {
query,
as_of: None, // Only the outermost SELECT may have an AS OF clause.
}))
Ok(match query {
Query {
ctes: CteBlock::Simple(ctes),
body: SetExpr::Show(show),
order_by,
limit: None,
offset: None,
} if ctes.is_empty() && order_by.is_empty() => Statement::Show(*show),
query => Statement::Select(SelectStatement {
query,
as_of: None, // Only the outermost SELECT may have an AS OF clause.
}),
})
}
unexpected => self
.expected(
Expand Down Expand Up @@ -916,13 +914,13 @@ impl<'a> Parser<'a> {
None
};

Some(WindowSpec {
Some(Box::new(WindowSpec {
partition_by,
order_by,
window_frame,
ignore_nulls,
respect_nulls,
})
}))
} else {
None
};
Expand Down Expand Up @@ -8145,7 +8143,7 @@ impl<'a> Parser<'a> {
} else if self.parse_keyword(VALUES) {
SetExpr::Values(self.parse_values()?)
} else if self.parse_keyword(SHOW) {
SetExpr::Show(self.parse_show()?)
SetExpr::Show(Box::new(self.parse_show()?))
} else if self.parse_keyword(TABLE) {
SetExpr::Table(self.parse_raw_name()?)
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/sql/src/names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2248,7 +2248,7 @@ impl<'a> Fold<Raw, Aug> for NameResolver<'a> {
),
args: self.fold_function_args(node.args),
filter: node.filter.map(|expr| Box::new(self.fold_expr(*expr))),
over: node.over.map(|over| self.fold_window_spec(over)),
over: node.over.map(|over| Box::new(self.fold_window_spec(*over))),
distinct: node.distinct,
}
})
Expand Down
10 changes: 5 additions & 5 deletions src/sql/src/plan/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2044,7 +2044,7 @@ fn plan_set_expr(
Ok((expr, scope))
}

match stmt.clone() {
match stmt.as_ref().clone() {
ShowStatement::ShowColumns(stmt) => {
show::show_columns(qcx.scx, stmt)?.plan_hir(qcx)
}
Expand Down Expand Up @@ -5717,7 +5717,7 @@ fn plan_function<'a>(
}
} else {
let (ignore_nulls, order_by_exprs, col_orders, window_frame, partition_by) =
plan_window_function_common(ecx, &f.name, &f.over)?;
plan_window_function_common(ecx, &f.name, f.over.as_deref())?;

// https://github.com/MaterializeInc/database-issues/issues/6720
match (&window_frame.start_bound, &window_frame.end_bound) {
Expand Down Expand Up @@ -6011,7 +6011,7 @@ fn plan_window_function_non_aggr<'a>(
PlanError,
> {
let (ignore_nulls, order_by_exprs, col_orders, window_frame, partition) =
plan_window_function_common(ecx, name, over)?;
plan_window_function_common(ecx, name, over.as_deref())?;

if *distinct {
sql_bail!(
Expand Down Expand Up @@ -6053,7 +6053,7 @@ fn plan_window_function_non_aggr<'a>(
fn plan_window_function_common(
ecx: &ExprContext,
name: &<Aug as AstInfo>::ItemName,
over: &Option<WindowSpec<Aug>>,
over: Option<&WindowSpec<Aug>>,
) -> Result<
(
bool,
Expand All @@ -6072,7 +6072,7 @@ fn plan_window_function_common(
);
}

let window_spec = match over.as_ref() {
let window_spec = match over {
Some(over) => over,
None => sql_bail!("window function {} requires an OVER clause", name),
};
Expand Down
14 changes: 7 additions & 7 deletions src/sql/src/plan/transform_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl<'a> FuncRewriter<'a> {
order_by: Vec<OrderByExpr<Aug>>,
filter: Option<Box<Expr<Aug>>>,
distinct: bool,
over: Option<WindowSpec<Aug>>,
over: Option<Box<WindowSpec<Aug>>>,
) -> Expr<Aug> {
if self.rewriting_table_factor && self.status.is_ok() {
self.status = Err(PlanError::Unstructured(
Expand All @@ -143,7 +143,7 @@ impl<'a> FuncRewriter<'a> {
expr: Expr<Aug>,
filter: Option<Box<Expr<Aug>>>,
distinct: bool,
over: Option<WindowSpec<Aug>>,
over: Option<Box<WindowSpec<Aug>>>,
) -> Expr<Aug> {
let sum = self
.plan_agg(
Expand Down Expand Up @@ -177,7 +177,7 @@ impl<'a> FuncRewriter<'a> {
expr: Expr<Aug>,
filter: Option<Box<Expr<Aug>>>,
distinct: bool,
over: Option<WindowSpec<Aug>>,
over: Option<Box<WindowSpec<Aug>>>,
) -> Expr<Aug> {
let sum = self
.plan_agg(
Expand Down Expand Up @@ -211,7 +211,7 @@ impl<'a> FuncRewriter<'a> {
filter: Option<Box<Expr<Aug>>>,
distinct: bool,
sample: bool,
over: Option<WindowSpec<Aug>>,
over: Option<Box<WindowSpec<Aug>>>,
) -> Expr<Aug> {
// N.B. this variance calculation uses the "textbook" algorithm, which
// is known to accumulate problematic amounts of error. The numerically
Expand Down Expand Up @@ -352,7 +352,7 @@ impl<'a> FuncRewriter<'a> {
filter: Option<Box<Expr<Aug>>>,
distinct: bool,
sample: bool,
over: Option<WindowSpec<Aug>>,
over: Option<Box<WindowSpec<Aug>>>,
) -> Expr<Aug> {
self.plan_variance(expr, filter, distinct, sample, over)
.call_unary(
Expand All @@ -366,7 +366,7 @@ impl<'a> FuncRewriter<'a> {
expr: Expr<Aug>,
filter: Option<Box<Expr<Aug>>>,
distinct: bool,
over: Option<WindowSpec<Aug>>,
over: Option<Box<WindowSpec<Aug>>>,
) -> Expr<Aug> {
// The code below converts `bool_and(x)` into:
//
Expand Down Expand Up @@ -396,7 +396,7 @@ impl<'a> FuncRewriter<'a> {
expr: Expr<Aug>,
filter: Option<Box<Expr<Aug>>>,
distinct: bool,
over: Option<WindowSpec<Aug>>,
over: Option<Box<WindowSpec<Aug>>>,
) -> Expr<Aug> {
// The code below converts `bool_or(x)`z into:
//
Expand Down