Skip to content

Commit e808787

Browse files
committed
Merge wip/udf-repeated-argument-t8c: refuse a Python function argument named twice
2 parents 395ced6 + 94ae397 commit e808787

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

‎crates/sqlquack/src/project/_helpers/function_files.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ pub(crate) fn python_function(text: &str) -> Result<Value, String> {
8585
let mut named: Vec<(String, String)> = Vec::new();
8686
for argument in &arguments {
8787
match argument.as_array().map(Vec::as_slice) {
88+
Some([Value::String(name), _])
89+
if named.iter().any(|(earlier, _)| earlier == name) =>
90+
{
91+
return Err(format!(
92+
"line {line}: the argument {name} is given twice; Python keeps only the last"
93+
));
94+
}
8895
Some([Value::String(name), Value::String(type_name)]) => {
8996
named.push((name.clone(), type_name.clone()));
9097
}

‎crates/sqlquack/tests/cli/test_project.rs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,23 @@ fn given_a_project_when_a_declaration_clashes_or_is_unconfirmed_then_the_error_s
347347
expected_exit: Some(2),
348348
expected_output: "plain.py: has no @udf(...) decorator",
349349
},
350+
ProjectTestCase {
351+
description: "a Python function naming one argument twice is an error, as Python keeps only the last",
352+
files: &[
353+
POLICY_FILE,
354+
SOURCES,
355+
ProjectFile {
356+
path: "functions/python/label.py",
357+
repository: &[],
358+
text: "from sqlbuild.functions import udf\n\n@udf(arguments={'a': 'INTEGER', 'a': 'VARCHAR'}, returns='VARCHAR')\ndef main(a):\n return a\n",
359+
},
360+
],
361+
folder: "",
362+
flags: &[],
363+
sql: "SELECT 1 AS n",
364+
expected_exit: Some(2),
365+
expected_output: "line 3: the argument a is given twice; Python keeps only the last",
366+
},
350367
];
351368
for test_case in test_cases {
352369
let outcome = project_convert(&test_case);

0 commit comments

Comments
 (0)