Skip to content

Add support for first, last aggregate function parsing #882

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 18, 2023
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
10 changes: 9 additions & 1 deletion src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3362,6 +3362,8 @@ pub struct Function {
// Some functions must be called without trailing parentheses, for example Postgres
// do it for current_catalog, current_schema, etc. This flags is used for formatting.
pub special: bool,
// Required ordering for the function (if empty, there is no requirement).
pub order_by: Vec<OrderByExpr>,
}

#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
Expand All @@ -3388,12 +3390,18 @@ impl fmt::Display for Function {
if self.special {
write!(f, "{}", self.name)?;
} else {
let order_by = if !self.order_by.is_empty() {
" ORDER BY "
} else {
""
};
write!(
f,
"{}({}{})",
"{}({}{}{order_by}{})",
self.name,
if self.distinct { "DISTINCT " } else { "" },
display_comma_separated(&self.args),
display_comma_separated(&self.order_by),
)?;

if let Some(o) = &self.over {
Expand Down
2 changes: 1 addition & 1 deletion src/ast/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ where
/// *expr = Expr::Function(Function {
/// name: ObjectName(vec![Ident::new("f")]),
/// args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(old_expr))],
/// over: None, distinct: false, special: false,
/// over: None, distinct: false, special: false, order_by: vec![],
/// });
/// }
/// ControlFlow::<()>::Continue(())
Expand Down
28 changes: 24 additions & 4 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,7 @@ impl<'a> Parser<'a> {
over: None,
distinct: false,
special: true,
order_by: vec![],
}))
}
Keyword::CURRENT_TIMESTAMP
Expand Down Expand Up @@ -880,7 +881,7 @@ impl<'a> Parser<'a> {
pub fn parse_function(&mut self, name: ObjectName) -> Result<Expr, ParserError> {
self.expect_token(&Token::LParen)?;
let distinct = self.parse_all_or_distinct()?.is_some();
let args = self.parse_optional_args()?;
let (args, order_by) = self.parse_optional_args_with_orderby()?;
let over = if self.parse_keyword(Keyword::OVER) {
// TBD: support window names (`OVER mywin`) in place of inline specification
self.expect_token(&Token::LParen)?;
Expand Down Expand Up @@ -917,21 +918,23 @@ impl<'a> Parser<'a> {
over,
distinct,
special: false,
order_by,
}))
}

pub fn parse_time_functions(&mut self, name: ObjectName) -> Result<Expr, ParserError> {
let args = if self.consume_token(&Token::LParen) {
self.parse_optional_args()?
let (args, order_by) = if self.consume_token(&Token::LParen) {
self.parse_optional_args_with_orderby()?
} else {
vec![]
(vec![], vec![])
};
Ok(Expr::Function(Function {
name,
args,
over: None,
distinct: false,
special: false,
order_by,
}))
}

Expand Down Expand Up @@ -6370,6 +6373,23 @@ impl<'a> Parser<'a> {
}
}

pub fn parse_optional_args_with_orderby(
&mut self,
) -> Result<(Vec<FunctionArg>, Vec<OrderByExpr>), ParserError> {
if self.consume_token(&Token::RParen) {
Ok((vec![], vec![]))
} else {
let args = self.parse_comma_separated(Parser::parse_function_args)?;
let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
self.parse_comma_separated(Parser::parse_order_by_expr)?
} else {
vec![]
};
self.expect_token(&Token::RParen)?;
Ok((args, order_by))
}
}

/// Parse a comma-delimited list of projections after SELECT
pub fn parse_select_item(&mut self) -> Result<SelectItem, ParserError> {
match self.parse_wildcard_expr()? {
Expand Down
1 change: 1 addition & 0 deletions tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ fn parse_map_access_offset() {
over: None,
distinct: false,
special: false,
order_by: vec![],
})],
})
);
Expand Down
4 changes: 4 additions & 0 deletions tests/sqlparser_clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ fn parse_map_access_expr() {
over: None,
distinct: false,
special: false,
order_by: vec![],
})],
})],
into: None,
Expand Down Expand Up @@ -88,6 +89,7 @@ fn parse_map_access_expr() {
over: None,
distinct: false,
special: false,
order_by: vec![],
})]
}),
op: BinaryOperator::NotEq,
Expand Down Expand Up @@ -135,6 +137,7 @@ fn parse_array_fn() {
over: None,
distinct: false,
special: false,
order_by: vec![],
}),
expr_from_projection(only(&select.projection))
);
Expand Down Expand Up @@ -189,6 +192,7 @@ fn parse_delimited_identifiers() {
over: None,
distinct: false,
special: false,
order_by: vec![],
}),
expr_from_projection(&select.projection[1]),
);
Expand Down
40 changes: 40 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,7 @@ fn parse_select_count_wildcard() {
over: None,
distinct: false,
special: false,
order_by: vec![],
}),
expr_from_projection(only(&select.projection))
);
Expand All @@ -857,6 +858,7 @@ fn parse_select_count_distinct() {
over: None,
distinct: true,
special: false,
order_by: vec![],
}),
expr_from_projection(only(&select.projection))
);
Expand Down Expand Up @@ -1693,6 +1695,7 @@ fn parse_select_having() {
over: None,
distinct: false,
special: false,
order_by: vec![],
})),
op: BinaryOperator::Gt,
right: Box::new(Expr::Value(number("1"))),
Expand Down Expand Up @@ -1726,6 +1729,7 @@ fn parse_select_qualify() {
}),
distinct: false,
special: false,
order_by: vec![],
})),
op: BinaryOperator::Eq,
right: Box::new(Expr::Value(number("1"))),
Expand Down Expand Up @@ -2072,6 +2076,29 @@ fn parse_array_agg_func() {
}
}

#[test]
fn parse_agg_with_order_by() {
let supported_dialects = TestedDialects {
dialects: vec![
Box::new(GenericDialect {}),
Box::new(PostgreSqlDialect {}),
Box::new(MsSqlDialect {}),
Box::new(AnsiDialect {}),
Box::new(HiveDialect {}),
],
options: None,
};

for sql in [
"SELECT FIRST_VALUE(x ORDER BY x) AS a FROM T",
"SELECT FIRST_VALUE(x ORDER BY x) FROM tbl",
"SELECT LAST_VALUE(x ORDER BY x, y) AS a FROM T",
"SELECT LAST_VALUE(x ORDER BY x ASC, y DESC) AS a FROM T",
] {
supported_dialects.verified_stmt(sql);
}
}

#[test]
fn parse_create_table() {
let sql = "CREATE TABLE uk_cities (\
Expand Down Expand Up @@ -3116,6 +3143,7 @@ fn parse_scalar_function_in_projection() {
over: None,
distinct: false,
special: false,
order_by: vec![],
}),
expr_from_projection(only(&select.projection))
);
Expand Down Expand Up @@ -3234,6 +3262,7 @@ fn parse_named_argument_function() {
over: None,
distinct: false,
special: false,
order_by: vec![],
}),
expr_from_projection(only(&select.projection))
);
Expand Down Expand Up @@ -3272,6 +3301,7 @@ fn parse_window_functions() {
}),
distinct: false,
special: false,
order_by: vec![],
}),
expr_from_projection(&select.projection[0])
);
Expand Down Expand Up @@ -3670,6 +3700,7 @@ fn parse_at_timezone() {
over: None,
distinct: false,
special: false,
order_by: vec![],
})),
time_zone: "UTC-06:00".to_string(),
},
Expand All @@ -3696,6 +3727,7 @@ fn parse_at_timezone() {
over: None,
distinct: false,
special: false,
order_by: vec![],
},)),
time_zone: "UTC-06:00".to_string(),
},),),
Expand All @@ -3706,6 +3738,7 @@ fn parse_at_timezone() {
over: None,
distinct: false,
special: false,
order_by: vec![],
},),
alias: Ident {
value: "hour".to_string(),
Expand Down Expand Up @@ -3863,6 +3896,7 @@ fn parse_table_function() {
over: None,
distinct: false,
special: false,
order_by: vec![],
});
assert_eq!(expr, expected_expr);
assert_eq!(alias, table_alias("a"))
Expand Down Expand Up @@ -6286,6 +6320,7 @@ fn parse_time_functions() {
over: None,
distinct: false,
special: false,
order_by: vec![],
}),
expr_from_projection(&select.projection[0])
);
Expand All @@ -6302,6 +6337,7 @@ fn parse_time_functions() {
over: None,
distinct: false,
special: false,
order_by: vec![],
}),
expr_from_projection(&select.projection[0])
);
Expand All @@ -6318,6 +6354,7 @@ fn parse_time_functions() {
over: None,
distinct: false,
special: false,
order_by: vec![],
}),
expr_from_projection(&select.projection[0])
);
Expand All @@ -6334,6 +6371,7 @@ fn parse_time_functions() {
over: None,
distinct: false,
special: false,
order_by: vec![],
}),
expr_from_projection(&select.projection[0])
);
Expand All @@ -6350,6 +6388,7 @@ fn parse_time_functions() {
over: None,
distinct: false,
special: false,
order_by: vec![],
}),
expr_from_projection(&select.projection[0])
);
Expand Down Expand Up @@ -6814,6 +6853,7 @@ fn parse_pivot_table() {
over: None,
distinct: false,
special: false,
order_by: vec![],
}),
value_column: vec![Ident::new("a"), Ident::new("MONTH")],
pivot_values: vec![
Expand Down
1 change: 1 addition & 0 deletions tests/sqlparser_hive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ fn parse_delimited_identifiers() {
over: None,
distinct: false,
special: false,
order_by: vec![],
}),
expr_from_projection(&select.projection[1]),
);
Expand Down
1 change: 1 addition & 0 deletions tests/sqlparser_mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ fn parse_delimited_identifiers() {
over: None,
distinct: false,
special: false,
order_by: vec![],
}),
expr_from_projection(&select.projection[1]),
);
Expand Down
6 changes: 6 additions & 0 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,7 @@ fn parse_insert_with_on_duplicate_update() {
over: None,
distinct: false,
special: false,
order_by: vec![],
})
},
Assignment {
Expand All @@ -803,6 +804,7 @@ fn parse_insert_with_on_duplicate_update() {
over: None,
distinct: false,
special: false,
order_by: vec![],
})
},
Assignment {
Expand All @@ -815,6 +817,7 @@ fn parse_insert_with_on_duplicate_update() {
over: None,
distinct: false,
special: false,
order_by: vec![],
})
},
Assignment {
Expand All @@ -827,6 +830,7 @@ fn parse_insert_with_on_duplicate_update() {
over: None,
distinct: false,
special: false,
order_by: vec![],
})
},
Assignment {
Expand All @@ -839,6 +843,7 @@ fn parse_insert_with_on_duplicate_update() {
over: None,
distinct: false,
special: false,
order_by: vec![],
})
},
])),
Expand Down Expand Up @@ -1182,6 +1187,7 @@ fn parse_table_colum_option_on_update() {
over: None,
distinct: false,
special: false,
order_by: vec![],
})),
},],
}],
Expand Down
Loading