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 coverage/syntax/baseline.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"catalogue/utility.toml": "55b30dcf06bf8182232a0da6b1da75462ae171e015cc8c9b8b95b36a722b44c5",
"sources.toml": "a180c722616dfefc1568edf41938a0abf491f5a89f9609a1f666a50aa5aead94"
},
"parser_tree": "cf4724b37f8f25a48b51cafa1c2df346916212d5",
"parser_tree": "171a33a1ef814819de345e0cbad125b95879e347",
"forms": {
"sf.classes.anomaly-detection.commands.create-anomaly-detection": "unverified",
"sf.classes.anomaly-detection.commands.drop-anomaly-detection": "verified",
Expand Down
719 changes: 719 additions & 0 deletions coverage/syntax/reviews/functions.toml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,17 @@ fn expression_lineage_level(
let value_type: ValueType = (parts[0].data_type.clone(), parts[0].nullability);
typed(combined(parts, floor), value_type)
}
Expression::MethodCall(call)
if matches!(call.this, Expression::Column(_))
&& dialect_rules(context.profile).qualifies_calls_by_schema() =>
{
let parts: Vec<Lineage> = call
.args
.iter()
.map(|argument| expression_lineage(argument, scope, context))
.collect();
combined(parts, TransformKind::Expression)
}
_ if is_aggregate(expression) => {
children_lineage(expression, scope, context, TransformKind::Aggregation)
}
Expand Down
5 changes: 5 additions & 0 deletions crates/sqlquack-semantics/src/lineage/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ pub(crate) trait DialectRules: Sync {
let _ = name;
false
}

/// Whether `<name>.<function>( ... )` qualifies the function by its schema rather than calling a method on a column.
fn qualifies_calls_by_schema(&self) -> bool {
false
}
}

/// A value's type and nullability.
Expand Down
4 changes: 4 additions & 0 deletions crates/sqlquack-semantics/src/snowflake/profile/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ impl DialectRules for SnowflakeRules {
false
}

fn qualifies_calls_by_schema(&self) -> bool {
true
}

fn qualifiers_reach_every_name(&self) -> bool {
false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,33 @@ fn given_a_query_when_tracing_lineage_then_values_rows_and_untraced_outputs_are_
expected_row_sources: &["ORDERS.AMOUNT", "ORDERS.CUSTOMER_ID"],
expected_rows_resolved: true,
},
LineageDetailTestCase {
description: "a user function's schema qualifier is no column it reads",
sql: "SELECT s1.f1(amount) AS v FROM orders",
dialect: Dialect::Snowflake,
profile: &SNOWFLAKE,
expected_outputs: &[("v", TransformKind::Expression, &["ORDERS.AMOUNT"], true)],
expected_row_sources: &[],
expected_rows_resolved: true,
},
LineageDetailTestCase {
description: "a user function named with its database and schema reads only its arguments",
sql: "SELECT d1.s1.f1(amount) AS v FROM orders",
dialect: Dialect::Snowflake,
profile: &SNOWFLAKE,
expected_outputs: &[("v", TransformKind::Expression, &["ORDERS.AMOUNT"], true)],
expected_row_sources: &[],
expected_rows_resolved: true,
},
LineageDetailTestCase {
description: "the same user function with its schema left out traces the same way",
sql: "SELECT d1..f1(amount) AS v FROM orders",
dialect: Dialect::Snowflake,
profile: &SNOWFLAKE,
expected_outputs: &[("v", TransformKind::Expression, &["ORDERS.AMOUNT"], true)],
expected_row_sources: &[],
expected_rows_resolved: true,
},
LineageDetailTestCase {
description: "a correlated EXISTS decides rows by both levels' columns",
sql: "SELECT c.name FROM customers c \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,6 @@ impl Parser<'_> {
args,
distinct,
order_by,
schema_omitted: false,
}))
}
false => dot(this, name),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
use crate::parser::_helpers::statements::analyze::snowflake_reserved_word;
use crate::parser::constants::VARIADIC_KEYWORD;
use crate::parser::models::Parser;
use sqlquack_syntax::expressions::names::models::Column;
use sqlquack_syntax::expressions::names::models::Identifier;
use sqlquack_syntax::expressions::operators::models::MethodCall;
use sqlquack_syntax::expressions::operators::models::Call;
use sqlquack_syntax::expressions::queries::models::Subquery;
use sqlquack_syntax::expressions::tree::models::Expression;
use sqlquack_syntax::tokens::models::SourceSpan;
Expand Down Expand Up @@ -195,7 +194,7 @@ impl Parser {
}
}

/// Snowflake's `<database>..<function>( ... )`: a call on the database with the schema left out.
/// Snowflake's `<database>..<function>( ... )`: a qualified call whose omitted schema is a null part.
pub(crate) fn parse_snowflake_default_schema_call(
&mut self,
database: Identifier,
Expand All @@ -213,17 +212,16 @@ impl Parser {
false => self.parse_method_arguments_for(&method.name)?,
};
self.expect_token(TokenType::RParen)?;
let database = Expression::Column(Box::new(Column {
name: database,
table: None,
join_mark: false,
trailing_comments: Vec::new(),
span: None,
inferred_type: None,
}));
let mut call = MethodCall::new(database, method, args);
call.schema_omitted = true;
Ok(Some(Expression::MethodCall(Box::new(call))))
Ok(Some(Expression::Call(Box::new(Call {
source_dialect: None,
name: vec![Some(database), None, Some(method)],
member: None,
arguments: args,
asynchronous: false,
hints: Vec::new(),
with: None,
into: None,
}))))
}

/// Reads the OVER clause of a schema-qualified Snowflake call, `<schema>.<function>( ... ) OVER ( ... )`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1397,22 +1397,34 @@ fn given_a_name_whose_middle_part_a_dot_dot_b_leaves_out_when_parsing_then_the_p
expected_value: json!(true),
},
StatementTestCase {
description: "a call on a database with its schema left out marks it omitted",
description: "a call on a database with its schema left out keeps the database as its first part",
sql: "SELECT SNOWFLAKE . . AGENT_RUN ( v1 ) ;",
pointer: "/0/select/expressions/0/method_call/schema_omitted",
expected_value: json!(true),
pointer: "/0/select/expressions/0/call/name/0/name",
expected_value: json!("SNOWFLAKE"),
},
StatementTestCase {
description: "a call with its schema left out is made on the database",
description: "a call on a database keeps the schema it leaves out in place as null",
sql: "SELECT SNOWFLAKE . . AGENT_RUN ( v1 ) ;",
pointer: "/0/select/expressions/0/method_call/this/column/name/name",
expected_value: json!("SNOWFLAKE"),
pointer: "/0/select/expressions/0/call/name/1",
expected_value: json!(null),
},
StatementTestCase {
description: "a call on a database keeps the function as its last part",
sql: "SELECT SNOWFLAKE . . AGENT_RUN ( v1 ) ;",
pointer: "/0/select/expressions/0/call/name/2/name",
expected_value: json!("AGENT_RUN"),
},
StatementTestCase {
description: "a schema-qualified call is not marked omitted",
description: "a call on a database keeps its arguments",
sql: "SELECT SNOWFLAKE . . AGENT_RUN ( v1 ) ;",
pointer: "/0/select/expressions/0/call/arguments/0/column/name/name",
expected_value: json!("v1"),
},
StatementTestCase {
description: "a schema-qualified call stays a method call on the schema",
sql: "SELECT CORTEX . AGENT_RUN ( v1 ) ;",
pointer: "/0/select/expressions/0/method_call/schema_omitted",
expected_value: json!(null),
pointer: "/0/select/expressions/0/method_call/this/column/name/name",
expected_value: json!("CORTEX"),
},
];
for test_case in test_cases {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -798,11 +798,23 @@ fn given_snowflake_create_and_begin_name_when_parsing_then_the_tree_keeps_the_el
expected_value: json!("c1"),
},
ProjectionTestCase {
description: "a call without a schema keeps its database as the qualifier",
description: "a call without a schema keeps its database as the first name part",
sql: "SELECT d1 . . f1 ( 1 )",
pointer: "/0/select/expressions/0/method_call/this/column/name/name",
pointer: "/0/select/expressions/0/call/name/0/name",
expected_value: json!("d1"),
},
ProjectionTestCase {
description: "a call without a schema keeps its quoted database quoted",
sql: "SELECT \"d 1\"..\"f 1\"(1)",
pointer: "/0/select/expressions/0/call/name/0/quoted",
expected_value: json!(true),
},
ProjectionTestCase {
description: "a call without a schema keeps its quoted function quoted",
sql: "SELECT \"d 1\"..\"f 1\"(1)",
pointer: "/0/select/expressions/0/call/name/2/quoted",
expected_value: json!(true),
},
ProjectionTestCase {
description: "a data metric function keeps its query argument",
sql: "SELECT SNOWFLAKE . CORE . NULL_COUNT ( SELECT c1 FROM t1 )",
Expand Down
4 changes: 0 additions & 4 deletions crates/sqlquack-syntax/src/expressions/operators/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,9 +717,6 @@ pub struct MethodCall {
/// ORDER BY inside the call.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub order_by: Vec<Ordered>,
/// Snowflake `<database>..<function>( ... )`: `this` is the database; the schema is left out.
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub schema_omitted: bool,
}

impl MethodCall {
Expand All @@ -731,7 +728,6 @@ impl MethodCall {
args,
distinct: false,
order_by: Vec::new(),
schema_omitted: false,
}
}
}
Expand Down
14 changes: 10 additions & 4 deletions crates/sqlquack/src/statements/_helpers/spellings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,24 @@ fn node_key(node: &Expression, windowed: bool) -> Option<String> {
let Expression::Column(path) = &call.this else {
return None;
};
let omitted = call.schema_omitted.then_some("");
let name = path
.table
.iter()
.chain([&path.name])
.chain([&path.name, &call.method])
.map(|part| part.name.as_str())
.chain(omitted)
.chain([call.method.name.as_str()])
.collect::<Vec<_>>()
.join(".");
return Some(call_key(&name, &call.args, windowed, false));
}
Expression::Call(call) if call.name.iter().any(Option::is_none) => {
let name = call
.name
.iter()
.map(|part| part.as_ref().map_or("", |part| part.name.as_str()))
.collect::<Vec<_>>()
.join(".");
return Some(call_key(&name, &call.arguments, windowed, false));
}
Expression::Case(case) if case.operand.is_some() => "case_simple",
Expression::Case(_) => "case_searched",
Expression::Subscript(subscript) => match &subscript.index {
Expand Down
7 changes: 7 additions & 0 deletions crates/sqlquack/tests/cli/test_convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ fn given_snowflake_sql_when_converting_then_duckdb_sql_or_named_refusals_are_pri
mention: "SQLQuack can't convert RANDOM yet\n --> <stdin>:1:8\n |\n1 | SELECT RANDOM() AS r\n | ^^^^^^^^ no rule converts RANDOM\n",
expected_outcome: convert_outcome(1, false),
},
ConvertTestCase {
description: "a call that leaves its schema out is refused, never guessed",
sql: "SELECT d1..f1(1) AS v",
flags: &[],
mention: "1 | SELECT d1..f1(1) AS v\n | ^^^^^^ no rule converts CALL\n",
expected_outcome: convert_outcome(1, false),
},
ConvertTestCase {
description: "a subquery in a hierarchical query's projection is refused",
sql: "WITH bins AS (SELECT 1 AS id, NULL AS parent) SELECT id, (SELECT 100) AS m FROM bins START WITH parent IS NULL CONNECT BY parent = PRIOR id",
Expand Down
Loading