Skip to content

Commit 478d767

Browse files
committed
fix(syntax-snowflake): parse a call that leaves out its schema as a qualified call
Snowflake's `<database>..<function>( ... )` now parses into the qualified call node with the name `[database, null, function]`, instead of a method call on a column that named the database. Spelling keys keep the empty part. Lineage reads only the arguments of a Snowflake schema-qualified call, so its qualifier is never traced as a column, and both spellings trace alike. The converter still refuses the call.
1 parent 53d1419 commit 478d767

11 files changed

Lines changed: 111 additions & 34 deletions

File tree

‎crates/sqlquack-semantics/src/lineage/_helpers/tracing/expressions.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,17 @@ fn expression_lineage_level(
189189
let value_type: ValueType = (parts[0].data_type.clone(), parts[0].nullability);
190190
typed(combined(parts, floor), value_type)
191191
}
192+
Expression::MethodCall(call)
193+
if matches!(call.this, Expression::Column(_))
194+
&& dialect_rules(context.profile).qualifies_calls_by_schema() =>
195+
{
196+
let parts: Vec<Lineage> = call
197+
.args
198+
.iter()
199+
.map(|argument| expression_lineage(argument, scope, context))
200+
.collect();
201+
combined(parts, TransformKind::Expression)
202+
}
192203
_ if is_aggregate(expression) => {
193204
children_lineage(expression, scope, context, TransformKind::Aggregation)
194205
}

‎crates/sqlquack-semantics/src/lineage/types.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ pub(crate) trait DialectRules: Sync {
244244
let _ = name;
245245
false
246246
}
247+
248+
/// Whether `<name>.<function>( ... )` qualifies the function by its schema rather than calling a method on a column.
249+
fn qualifies_calls_by_schema(&self) -> bool {
250+
false
251+
}
247252
}
248253

249254
/// A value's type and nullability.

‎crates/sqlquack-semantics/src/snowflake/profile/models.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ impl DialectRules for SnowflakeRules {
207207
false
208208
}
209209

210+
fn qualifies_calls_by_schema(&self) -> bool {
211+
true
212+
}
213+
210214
fn qualifiers_reach_every_name(&self) -> bool {
211215
false
212216
}

‎crates/sqlquack-semantics/src/snowflake/profile/tests/test_lineage_details.rs‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,33 @@ fn given_a_query_when_tracing_lineage_then_values_rows_and_untraced_outputs_are_
5555
expected_row_sources: &["ORDERS.AMOUNT", "ORDERS.CUSTOMER_ID"],
5656
expected_rows_resolved: true,
5757
},
58+
LineageDetailTestCase {
59+
description: "a user function's schema qualifier is no column it reads",
60+
sql: "SELECT s1.f1(amount) AS v FROM orders",
61+
dialect: Dialect::Snowflake,
62+
profile: &SNOWFLAKE,
63+
expected_outputs: &[("v", TransformKind::Expression, &["ORDERS.AMOUNT"], true)],
64+
expected_row_sources: &[],
65+
expected_rows_resolved: true,
66+
},
67+
LineageDetailTestCase {
68+
description: "a user function named with its database and schema reads only its arguments",
69+
sql: "SELECT d1.s1.f1(amount) AS v FROM orders",
70+
dialect: Dialect::Snowflake,
71+
profile: &SNOWFLAKE,
72+
expected_outputs: &[("v", TransformKind::Expression, &["ORDERS.AMOUNT"], true)],
73+
expected_row_sources: &[],
74+
expected_rows_resolved: true,
75+
},
76+
LineageDetailTestCase {
77+
description: "the same user function with its schema left out traces the same way",
78+
sql: "SELECT d1..f1(amount) AS v FROM orders",
79+
dialect: Dialect::Snowflake,
80+
profile: &SNOWFLAKE,
81+
expected_outputs: &[("v", TransformKind::Expression, &["ORDERS.AMOUNT"], true)],
82+
expected_row_sources: &[],
83+
expected_rows_resolved: true,
84+
},
5885
LineageDetailTestCase {
5986
description: "a correlated EXISTS decides rows by both levels' columns",
6087
sql: "SELECT c.name FROM customers c \

‎crates/sqlquack-syntax-duckdb/src/parse/_helpers/values/primary.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,6 @@ impl Parser<'_> {
630630
args,
631631
distinct,
632632
order_by,
633-
schema_omitted: false,
634633
}))
635634
}
636635
false => dot(this, name),

‎crates/sqlquack-syntax-snowflake/src/parser/_helpers/functions/snowflake_calls.rs‎

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
use crate::parser::_helpers::statements::analyze::snowflake_reserved_word;
44
use crate::parser::constants::VARIADIC_KEYWORD;
55
use crate::parser::models::Parser;
6-
use sqlquack_syntax::expressions::names::models::Column;
76
use sqlquack_syntax::expressions::names::models::Identifier;
8-
use sqlquack_syntax::expressions::operators::models::MethodCall;
7+
use sqlquack_syntax::expressions::operators::models::Call;
98
use sqlquack_syntax::expressions::queries::models::Subquery;
109
use sqlquack_syntax::expressions::tree::models::Expression;
1110
use sqlquack_syntax::tokens::models::SourceSpan;
@@ -195,7 +194,7 @@ impl Parser {
195194
}
196195
}
197196

198-
/// Snowflake's `<database>..<function>( ... )`: a call on the database with the schema left out.
197+
/// Snowflake's `<database>..<function>( ... )`: a qualified call whose omitted schema is a null part.
199198
pub(crate) fn parse_snowflake_default_schema_call(
200199
&mut self,
201200
database: Identifier,
@@ -213,17 +212,16 @@ impl Parser {
213212
false => self.parse_method_arguments_for(&method.name)?,
214213
};
215214
self.expect_token(TokenType::RParen)?;
216-
let database = Expression::Column(Box::new(Column {
217-
name: database,
218-
table: None,
219-
join_mark: false,
220-
trailing_comments: Vec::new(),
221-
span: None,
222-
inferred_type: None,
223-
}));
224-
let mut call = MethodCall::new(database, method, args);
225-
call.schema_omitted = true;
226-
Ok(Some(Expression::MethodCall(Box::new(call))))
215+
Ok(Some(Expression::Call(Box::new(Call {
216+
source_dialect: None,
217+
name: vec![Some(database), None, Some(method)],
218+
member: None,
219+
arguments: args,
220+
asynchronous: false,
221+
hints: Vec::new(),
222+
with: None,
223+
into: None,
224+
}))))
227225
}
228226

229227
/// Reads the OVER clause of a schema-qualified Snowflake call, `<schema>.<function>( ... ) OVER ( ... )`.

‎crates/sqlquack-syntax-snowflake/tests/parser/test_snowflake_receipted_objects.rs‎

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,22 +1397,34 @@ fn given_a_name_whose_middle_part_a_dot_dot_b_leaves_out_when_parsing_then_the_p
13971397
expected_value: json!(true),
13981398
},
13991399
StatementTestCase {
1400-
description: "a call on a database with its schema left out marks it omitted",
1400+
description: "a call on a database with its schema left out keeps the database as its first part",
14011401
sql: "SELECT SNOWFLAKE . . AGENT_RUN ( v1 ) ;",
1402-
pointer: "/0/select/expressions/0/method_call/schema_omitted",
1403-
expected_value: json!(true),
1402+
pointer: "/0/select/expressions/0/call/name/0/name",
1403+
expected_value: json!("SNOWFLAKE"),
14041404
},
14051405
StatementTestCase {
1406-
description: "a call with its schema left out is made on the database",
1406+
description: "a call on a database keeps the schema it leaves out in place as null",
14071407
sql: "SELECT SNOWFLAKE . . AGENT_RUN ( v1 ) ;",
1408-
pointer: "/0/select/expressions/0/method_call/this/column/name/name",
1409-
expected_value: json!("SNOWFLAKE"),
1408+
pointer: "/0/select/expressions/0/call/name/1",
1409+
expected_value: json!(null),
1410+
},
1411+
StatementTestCase {
1412+
description: "a call on a database keeps the function as its last part",
1413+
sql: "SELECT SNOWFLAKE . . AGENT_RUN ( v1 ) ;",
1414+
pointer: "/0/select/expressions/0/call/name/2/name",
1415+
expected_value: json!("AGENT_RUN"),
14101416
},
14111417
StatementTestCase {
1412-
description: "a schema-qualified call is not marked omitted",
1418+
description: "a call on a database keeps its arguments",
1419+
sql: "SELECT SNOWFLAKE . . AGENT_RUN ( v1 ) ;",
1420+
pointer: "/0/select/expressions/0/call/arguments/0/column/name/name",
1421+
expected_value: json!("v1"),
1422+
},
1423+
StatementTestCase {
1424+
description: "a schema-qualified call stays a method call on the schema",
14131425
sql: "SELECT CORTEX . AGENT_RUN ( v1 ) ;",
1414-
pointer: "/0/select/expressions/0/method_call/schema_omitted",
1415-
expected_value: json!(null),
1426+
pointer: "/0/select/expressions/0/method_call/this/column/name/name",
1427+
expected_value: json!("CORTEX"),
14161428
},
14171429
];
14181430
for test_case in test_cases {

‎crates/sqlquack-syntax-snowflake/tests/parser/test_snowflake_recorded.rs‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -798,11 +798,23 @@ fn given_snowflake_create_and_begin_name_when_parsing_then_the_tree_keeps_the_el
798798
expected_value: json!("c1"),
799799
},
800800
ProjectionTestCase {
801-
description: "a call without a schema keeps its database as the qualifier",
801+
description: "a call without a schema keeps its database as the first name part",
802802
sql: "SELECT d1 . . f1 ( 1 )",
803-
pointer: "/0/select/expressions/0/method_call/this/column/name/name",
803+
pointer: "/0/select/expressions/0/call/name/0/name",
804804
expected_value: json!("d1"),
805805
},
806+
ProjectionTestCase {
807+
description: "a call without a schema keeps its quoted database quoted",
808+
sql: "SELECT \"d 1\"..\"f 1\"(1)",
809+
pointer: "/0/select/expressions/0/call/name/0/quoted",
810+
expected_value: json!(true),
811+
},
812+
ProjectionTestCase {
813+
description: "a call without a schema keeps its quoted function quoted",
814+
sql: "SELECT \"d 1\"..\"f 1\"(1)",
815+
pointer: "/0/select/expressions/0/call/name/2/quoted",
816+
expected_value: json!(true),
817+
},
806818
ProjectionTestCase {
807819
description: "a data metric function keeps its query argument",
808820
sql: "SELECT SNOWFLAKE . CORE . NULL_COUNT ( SELECT c1 FROM t1 )",

‎crates/sqlquack-syntax/src/expressions/operators/models.rs‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -717,9 +717,6 @@ pub struct MethodCall {
717717
/// ORDER BY inside the call.
718718
#[serde(default, skip_serializing_if = "Vec::is_empty")]
719719
pub order_by: Vec<Ordered>,
720-
/// Snowflake `<database>..<function>( ... )`: `this` is the database; the schema is left out.
721-
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
722-
pub schema_omitted: bool,
723720
}
724721

725722
impl MethodCall {
@@ -731,7 +728,6 @@ impl MethodCall {
731728
args,
732729
distinct: false,
733730
order_by: Vec::new(),
734-
schema_omitted: false,
735731
}
736732
}
737733
}

‎crates/sqlquack/src/statements/_helpers/spellings.rs‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,24 @@ fn node_key(node: &Expression, windowed: bool) -> Option<String> {
8484
let Expression::Column(path) = &call.this else {
8585
return None;
8686
};
87-
let omitted = call.schema_omitted.then_some("");
8887
let name = path
8988
.table
9089
.iter()
91-
.chain([&path.name])
90+
.chain([&path.name, &call.method])
9291
.map(|part| part.name.as_str())
93-
.chain(omitted)
94-
.chain([call.method.name.as_str()])
9592
.collect::<Vec<_>>()
9693
.join(".");
9794
return Some(call_key(&name, &call.args, windowed, false));
9895
}
96+
Expression::Call(call) if call.name.iter().any(Option::is_none) => {
97+
let name = call
98+
.name
99+
.iter()
100+
.map(|part| part.as_ref().map_or("", |part| part.name.as_str()))
101+
.collect::<Vec<_>>()
102+
.join(".");
103+
return Some(call_key(&name, &call.arguments, windowed, false));
104+
}
99105
Expression::Case(case) if case.operand.is_some() => "case_simple",
100106
Expression::Case(_) => "case_searched",
101107
Expression::Subscript(subscript) => match &subscript.index {

0 commit comments

Comments
 (0)