Skip to content

Commit f8e9e18

Browse files
committed
Expr:Function(Box<Function>)
1 parent 64f4b1f commit f8e9e18

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
@@ -1014,6 +1014,7 @@ pub enum Expr {
10141014
expr: Box<Expr>,
10151015
},
10161016
/// CONVERT a value to a different data type or character encoding. e.g. `CONVERT(foo USING utf8mb4)`
1017+
// XXX too big
10171018
Convert {
10181019
/// CONVERT (false) or TRY_CONVERT (true)
10191020
/// <https://learn.microsoft.com/en-us/sql/t-sql/functions/try-convert-transact-sql?view=sql-server-ver16>
@@ -1032,6 +1033,7 @@ pub enum Expr {
10321033
styles: Vec<Expr>,
10331034
},
10341035
/// `CAST` an expression to a different data type e.g. `CAST(foo AS VARCHAR(123))`
1036+
// XXX too big
10351037
Cast {
10361038
/// The cast kind (e.g., `CAST`, `TRY_CAST`).
10371039
kind: CastKind,
@@ -1181,14 +1183,17 @@ pub enum Expr {
11811183
/// A constant of form `<data_type> 'value'`.
11821184
/// This can represent ANSI SQL `DATE`, `TIME`, and `TIMESTAMP` literals (such as `DATE '2020-01-01'`),
11831185
/// as well as constants of other types (a non-standard PostgreSQL extension).
1186+
// XXX too big
11841187
TypedString(TypedString),
11851188
/// Scalar function call e.g. `LEFT(foo, 5)`
1186-
Function(Function),
1189+
// XXX too big
1190+
Function(Box<Function>),
11871191
/// `CASE [<operand>] WHEN <condition> THEN <result> ... [ELSE <result>] END`
11881192
///
11891193
/// Note we only recognize a complete single expression as `<condition>`,
11901194
/// not `< 0` nor `1, 2, 3` as allowed in a `<simple when clause>` per
11911195
/// <https://jakewheat.github.io/sql-overview/sql-2011-foundation-grammar.html#simple-when-clause>
1196+
// XXX too big
11921197
Case {
11931198
/// The attached `CASE` token (keeps original spacing/comments).
11941199
case_token: AttachedToken,
@@ -1266,6 +1271,7 @@ pub enum Expr {
12661271
/// An array expression e.g. `ARRAY[1, 2]`
12671272
Array(Array),
12681273
/// An interval expression e.g. `INTERVAL '1' YEAR`
1274+
// XXX too big
12691275
Interval(Interval),
12701276
/// `MySQL` specific text search function [(1)].
12711277
///
@@ -1317,6 +1323,7 @@ pub enum Expr {
13171323
/// [ClickHouse](https://clickhouse.com/docs/en/sql-reference/functions#higher-order-functions---operator-and-lambdaparams-expr-function)
13181324
/// [Databricks](https://docs.databricks.com/en/sql/language-manual/sql-ref-lambda-functions.html)
13191325
/// [DuckDB](https://duckdb.org/docs/stable/sql/functions/lambda)
1326+
// XXX too big
13201327
Lambda(LambdaFunction),
13211328
/// Checks membership of a value in a JSON array
13221329
MemberOf(MemberOf),
@@ -1327,6 +1334,16 @@ impl Expr {
13271334
pub fn value(value: impl Into<ValueWithSpan>) -> Self {
13281335
Expr::Value(value.into())
13291336
}
1337+
1338+
/// Convenience method to retrieve `Expr::Function`'s value if `self` is a
1339+
/// function expression.
1340+
pub fn as_function(&self) -> Option<&Function> {
1341+
if let Expr::Function(f) = self {
1342+
Some(&**f)
1343+
} else {
1344+
None
1345+
}
1346+
}
13301347
}
13311348

13321349
/// The contents inside the `[` and `]` in a subscript expression.
@@ -10741,7 +10758,7 @@ pub enum TableObject {
1074110758
/// INSERT INTO TABLE FUNCTION remote('localhost', default.simple_table)
1074210759
/// ```
1074310760
/// [Clickhouse](https://clickhouse.com/docs/en/sql-reference/table-functions)
10744-
TableFunction(Function),
10761+
TableFunction(Box<Function>),
1074510762
}
1074610763

1074710764
impl fmt::Display for TableObject {

src/parser/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,7 +1516,7 @@ impl<'a> Parser<'a> {
15161516
filter: None,
15171517
over: None,
15181518
within_group: vec![],
1519-
})))
1519+
}.into())))
15201520
}
15211521
Keyword::CURRENT_TIMESTAMP
15221522
| Keyword::CURRENT_TIME
@@ -1578,7 +1578,7 @@ impl<'a> Parser<'a> {
15781578
null_treatment: None,
15791579
over: None,
15801580
within_group: vec![],
1581-
})))
1581+
}.into())))
15821582
}
15831583
Keyword::NOT => Ok(Some(self.parse_not()?)),
15841584
Keyword::MATCH if self.dialect.supports_match_against() => {
@@ -2404,7 +2404,7 @@ impl<'a> Parser<'a> {
24042404
self.parse_function_call(name).map(Expr::Function)
24052405
}
24062406

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

24102410
// Snowflake permits a subquery to be passed as an argument without
@@ -2421,7 +2421,7 @@ impl<'a> Parser<'a> {
24212421
null_treatment: None,
24222422
over: None,
24232423
within_group: vec![],
2424-
});
2424+
}.into());
24252425
}
24262426

24272427
let mut args = self.parse_function_argument_list()?;
@@ -2489,7 +2489,7 @@ impl<'a> Parser<'a> {
24892489
filter,
24902490
over,
24912491
within_group,
2492-
})
2492+
}.into())
24932493
}
24942494

24952495
/// Optionally parses a null treatment clause.
@@ -2524,7 +2524,7 @@ impl<'a> Parser<'a> {
25242524
over: None,
25252525
null_treatment: None,
25262526
within_group: vec![],
2527-
}))
2527+
}.into()))
25282528
}
25292529

25302530
/// Parse window frame `UNITS` clause: `ROWS`, `RANGE`, or `GROUPS`.
@@ -11036,7 +11036,7 @@ impl<'a> Parser<'a> {
1103611036
let object_name = self.parse_object_name(false)?;
1103711037
if self.peek_token_ref().token == Token::LParen {
1103811038
match self.parse_function(object_name)? {
11039-
Expr::Function(f) => Ok(Statement::Call(f)),
11039+
Expr::Function(f) => Ok(Statement::Call(*f)),
1104011040
other => parser_err!(
1104111041
format!("Expected a simple procedure call but found: {other}"),
1104211042
self.peek_token_ref().span.start
@@ -13780,7 +13780,7 @@ impl<'a> Parser<'a> {
1378013780
let function_expr = self.parse_function(function_name)?;
1378113781
if let Expr::Function(function) = function_expr {
1378213782
let alias = self.parse_identifier_optional_alias()?;
13783-
pipe_operators.push(PipeOperator::Call { function, alias });
13783+
pipe_operators.push(PipeOperator::Call { function: *function, alias });
1378413784
} else {
1378513785
return Err(ParserError::ParserError(
1378613786
"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
@@ -2237,7 +2237,7 @@ fn parse_map_access_expr() {
22372237
over: None,
22382238
within_group: vec![],
22392239
uses_odbc_syntax: false,
2240-
}),
2240+
}.into()),
22412241
}),
22422242
AccessExpr::Dot(Expr::Identifier(Ident::with_span(
22432243
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)