Skip to content

Commit 6021ecc

Browse files
committed
Expr:Function(Box<Function>)
1 parent b3e176d commit 6021ecc

14 files changed

+402
-469
lines changed

src/ast/mod.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,7 @@ pub enum Expr {
10251025
expr: Box<Expr>,
10261026
},
10271027
/// CONVERT a value to a different data type or character encoding. e.g. `CONVERT(foo USING utf8mb4)`
1028+
// XXX too big
10281029
Convert {
10291030
/// CONVERT (false) or TRY_CONVERT (true)
10301031
/// <https://learn.microsoft.com/en-us/sql/t-sql/functions/try-convert-transact-sql?view=sql-server-ver16>
@@ -1043,6 +1044,7 @@ pub enum Expr {
10431044
styles: Vec<Expr>,
10441045
},
10451046
/// `CAST` an expression to a different data type e.g. `CAST(foo AS VARCHAR(123))`
1047+
// XXX too big
10461048
Cast {
10471049
/// The cast kind (e.g., `CAST`, `TRY_CAST`).
10481050
kind: CastKind,
@@ -1192,14 +1194,17 @@ pub enum Expr {
11921194
/// A constant of form `<data_type> 'value'`.
11931195
/// This can represent ANSI SQL `DATE`, `TIME`, and `TIMESTAMP` literals (such as `DATE '2020-01-01'`),
11941196
/// as well as constants of other types (a non-standard PostgreSQL extension).
1197+
// XXX too big
11951198
TypedString(TypedString),
11961199
/// Scalar function call e.g. `LEFT(foo, 5)`
1197-
Function(Function),
1200+
// XXX too big
1201+
Function(Box<Function>),
11981202
/// `CASE [<operand>] WHEN <condition> THEN <result> ... [ELSE <result>] END`
11991203
///
12001204
/// Note we only recognize a complete single expression as `<condition>`,
12011205
/// not `< 0` nor `1, 2, 3` as allowed in a `<simple when clause>` per
12021206
/// <https://jakewheat.github.io/sql-overview/sql-2011-foundation-grammar.html#simple-when-clause>
1207+
// XXX too big
12031208
Case {
12041209
/// The attached `CASE` token (keeps original spacing/comments).
12051210
case_token: AttachedToken,
@@ -1277,6 +1282,7 @@ pub enum Expr {
12771282
/// An array expression e.g. `ARRAY[1, 2]`
12781283
Array(Array),
12791284
/// An interval expression e.g. `INTERVAL '1' YEAR`
1285+
// XXX too big
12801286
Interval(Interval),
12811287
/// `MySQL` specific text search function [(1)].
12821288
///
@@ -1328,6 +1334,7 @@ pub enum Expr {
13281334
/// [ClickHouse](https://clickhouse.com/docs/en/sql-reference/functions#higher-order-functions---operator-and-lambdaparams-expr-function)
13291335
/// [Databricks](https://docs.databricks.com/en/sql/language-manual/sql-ref-lambda-functions.html)
13301336
/// [DuckDB](https://duckdb.org/docs/stable/sql/functions/lambda)
1337+
// XXX too big
13311338
Lambda(LambdaFunction),
13321339
/// Checks membership of a value in a JSON array
13331340
MemberOf(MemberOf),
@@ -1338,6 +1345,16 @@ impl Expr {
13381345
pub fn value(value: impl Into<ValueWithSpan>) -> Self {
13391346
Expr::Value(value.into())
13401347
}
1348+
1349+
/// Convenience method to retrieve `Expr::Function`'s value if `self` is a
1350+
/// function expression.
1351+
pub fn as_function(&self) -> Option<&Function> {
1352+
if let Expr::Function(f) = self {
1353+
Some(&**f)
1354+
} else {
1355+
None
1356+
}
1357+
}
13411358
}
13421359

13431360
/// The contents inside the `[` and `]` in a subscript expression.
@@ -10887,7 +10904,7 @@ pub enum TableObject {
1088710904
/// INSERT INTO TABLE FUNCTION remote('localhost', default.simple_table)
1088810905
/// ```
1088910906
/// [Clickhouse](https://clickhouse.com/docs/en/sql-reference/table-functions)
10890-
TableFunction(Function),
10907+
TableFunction(Box<Function>),
1089110908
}
1089210909

1089310910
impl fmt::Display for TableObject {

src/parser/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,7 +1520,7 @@ impl<'a> Parser<'a> {
15201520
filter: None,
15211521
over: None,
15221522
within_group: vec![],
1523-
})))
1523+
}.into())))
15241524
}
15251525
Keyword::CURRENT_TIMESTAMP
15261526
| Keyword::CURRENT_TIME
@@ -1582,7 +1582,7 @@ impl<'a> Parser<'a> {
15821582
null_treatment: None,
15831583
over: None,
15841584
within_group: vec![],
1585-
})))
1585+
}.into())))
15861586
}
15871587
Keyword::NOT => Ok(Some(self.parse_not()?)),
15881588
Keyword::MATCH if self.dialect.supports_match_against() => {
@@ -2408,7 +2408,7 @@ impl<'a> Parser<'a> {
24082408
self.parse_function_call(name).map(Expr::Function)
24092409
}
24102410

2411-
fn parse_function_call(&mut self, name: ObjectName) -> Result<Function, ParserError> {
2411+
fn parse_function_call(&mut self, name: ObjectName) -> Result<Box<Function>, ParserError> {
24122412
self.expect_token(&Token::LParen)?;
24132413

24142414
// Snowflake permits a subquery to be passed as an argument without
@@ -2425,7 +2425,7 @@ impl<'a> Parser<'a> {
24252425
null_treatment: None,
24262426
over: None,
24272427
within_group: vec![],
2428-
});
2428+
}.into());
24292429
}
24302430

24312431
let mut args = self.parse_function_argument_list()?;
@@ -2493,7 +2493,7 @@ impl<'a> Parser<'a> {
24932493
filter,
24942494
over,
24952495
within_group,
2496-
})
2496+
}.into())
24972497
}
24982498

24992499
/// Optionally parses a null treatment clause.
@@ -2528,7 +2528,7 @@ impl<'a> Parser<'a> {
25282528
over: None,
25292529
null_treatment: None,
25302530
within_group: vec![],
2531-
}))
2531+
}.into()))
25322532
}
25332533

25342534
/// Parse window frame `UNITS` clause: `ROWS`, `RANGE`, or `GROUPS`.
@@ -11095,7 +11095,7 @@ impl<'a> Parser<'a> {
1109511095
let object_name = self.parse_object_name(false)?;
1109611096
if self.peek_token_ref().token == Token::LParen {
1109711097
match self.parse_function(object_name)? {
11098-
Expr::Function(f) => Ok(Statement::Call(f)),
11098+
Expr::Function(f) => Ok(Statement::Call(*f)),
1109911099
other => parser_err!(
1110011100
format!("Expected a simple procedure call but found: {other}"),
1110111101
self.peek_token_ref().span.start
@@ -13839,7 +13839,7 @@ impl<'a> Parser<'a> {
1383913839
let function_expr = self.parse_function(function_name)?;
1384013840
if let Expr::Function(function) = function_expr {
1384113841
let alias = self.parse_identifier_optional_alias()?;
13842-
pipe_operators.push(PipeOperator::Call { function, alias });
13842+
pipe_operators.push(PipeOperator::Call { function: *function, alias });
1384313843
} else {
1384413844
return Err(ParserError::ParserError(
1384513845
"Expected function call after CALL".to_string(),

src/test_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ pub fn call(function: &str, args: impl IntoIterator<Item = Expr>) -> Expr {
451451
null_treatment: None,
452452
over: None,
453453
within_group: vec![],
454-
})
454+
}.into())
455455
}
456456

457457
/// Gets the first index column (mysql calls it a key part) of the first index found in a

tests/sqlparser_bigquery.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2247,7 +2247,7 @@ fn parse_map_access_expr() {
22472247
over: None,
22482248
within_group: vec![],
22492249
uses_odbc_syntax: false,
2250-
}),
2250+
}.into()),
22512251
}),
22522252
AccessExpr::Dot(Expr::Identifier(Ident::with_span(
22532253
Span::new(Location::of(1, 24), Location::of(1, 25)),

tests/sqlparser_clickhouse.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ fn parse_delimited_identifiers() {
191191
expr_from_projection(&select.projection[0]),
192192
);
193193
assert_eq!(
194-
&Expr::Function(Function {
194+
Some(&Function {
195195
name: ObjectName::from(vec![Ident::with_quote('"', "myfun")]),
196196
uses_odbc_syntax: false,
197197
parameters: FunctionArguments::None,
@@ -205,7 +205,7 @@ fn parse_delimited_identifiers() {
205205
over: None,
206206
within_group: vec![],
207207
}),
208-
expr_from_projection(&select.projection[1]),
208+
expr_from_projection(&select.projection[1]).as_function(),
209209
);
210210
match &select.projection[2] {
211211
SelectItem::ExprWithAlias { expr, alias } => {
@@ -826,7 +826,7 @@ fn parse_create_table_with_variant_default_expressions() {
826826
data_type: DataType::Datetime(None),
827827
options: vec![ColumnOptionDef {
828828
name: None,
829-
option: ColumnOption::Materialized(Expr::Function(Function {
829+
option: ColumnOption::Materialized(Expr::Function(Box::new(Function {
830830
name: ObjectName::from(vec![Ident::new("now")]),
831831
uses_odbc_syntax: false,
832832
args: FunctionArguments::List(FunctionArgumentList {
@@ -839,15 +839,15 @@ fn parse_create_table_with_variant_default_expressions() {
839839
filter: None,
840840
over: None,
841841
within_group: vec![],
842-
}))
842+
})))
843843
}],
844844
},
845845
ColumnDef {
846846
name: Ident::new("b"),
847847
data_type: DataType::Datetime(None),
848848
options: vec![ColumnOptionDef {
849849
name: None,
850-
option: ColumnOption::Ephemeral(Some(Expr::Function(Function {
850+
option: ColumnOption::Ephemeral(Some(Expr::Function(Box::new(Function {
851851
name: ObjectName::from(vec![Ident::new("now")]),
852852
uses_odbc_syntax: false,
853853
args: FunctionArguments::List(FunctionArgumentList {
@@ -860,7 +860,7 @@ fn parse_create_table_with_variant_default_expressions() {
860860
filter: None,
861861
over: None,
862862
within_group: vec![],
863-
})))
863+
}))))
864864
}],
865865
},
866866
ColumnDef {
@@ -876,7 +876,7 @@ fn parse_create_table_with_variant_default_expressions() {
876876
data_type: DataType::String(None),
877877
options: vec![ColumnOptionDef {
878878
name: None,
879-
option: ColumnOption::Alias(Expr::Function(Function {
879+
option: ColumnOption::Alias(Expr::Function(Box::new(Function {
880880
name: ObjectName::from(vec![Ident::new("toString")]),
881881
uses_odbc_syntax: false,
882882
args: FunctionArguments::List(FunctionArgumentList {
@@ -891,7 +891,7 @@ fn parse_create_table_with_variant_default_expressions() {
891891
filter: None,
892892
over: None,
893893
within_group: vec![],
894-
}))
894+
})))
895895
}],
896896
}
897897
]

0 commit comments

Comments
 (0)