Skip to content

Commit febd122

Browse files
committed
Merge wip/ignore-nulls-port-2: convert GREATEST_IGNORE_NULLS and LEAST_IGNORE_NULLS
2 parents 9b91fff + 5594c47 commit febd122

5 files changed

Lines changed: 51 additions & 29 deletions

File tree

‎coverage/sql-size/baseline.json‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8496,6 +8496,10 @@
84968496
"translation/functions/get.toml#t003_field_case": 939,
84978497
"translation/functions/get.toml#t004_kind_mismatch": 616,
84988498
"translation/functions/get.toml#t006_null_object": 963,
8499+
"translation/functions/greatest-ignore-nulls.toml#t001_number": 492,
8500+
"translation/functions/greatest-ignore-nulls.toml#t003_varchar": 326,
8501+
"translation/functions/greatest-ignore-nulls.toml#t004_date": 274,
8502+
"translation/functions/greatest-ignore-nulls.toml#t005_null_expr": 548,
84998503
"translation/functions/greatest.toml#t001_number": 637,
85008504
"translation/functions/greatest.toml#t003_varchar": 434,
85018505
"translation/functions/greatest.toml#t004_date": 376,
@@ -8593,6 +8597,10 @@
85938597
"translation/functions/lead.toml#t002_ignore_nulls": 1180,
85948598
"translation/functions/lead.toml#t003_negative_offset": 1166,
85958599
"translation/functions/lead.toml#t004_null_expr": 1167,
8600+
"translation/functions/least-ignore-nulls.toml#t001_number": 489,
8601+
"translation/functions/least-ignore-nulls.toml#t003_varchar": 323,
8602+
"translation/functions/least-ignore-nulls.toml#t004_date": 271,
8603+
"translation/functions/least-ignore-nulls.toml#t005_null_expr": 545,
85968604
"translation/functions/least.toml#t001_number": 634,
85978605
"translation/functions/least.toml#t003_varchar": 431,
85988606
"translation/functions/least.toml#t004_date": 373,

‎coverage/translation/results-baseline.json‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9784,11 +9784,11 @@
97849784
"translation/functions/getdate.toml#t003_null_none": "refused",
97859785
"translation/functions/getvariable.toml#t001_unset": "refused",
97869786
"translation/functions/getvariable.toml#t002_null_name": "refused",
9787-
"translation/functions/greatest-ignore-nulls.toml#t001_number": "refused",
9787+
"translation/functions/greatest-ignore-nulls.toml#t001_number": "matching",
97889788
"translation/functions/greatest-ignore-nulls.toml#t002_first_type": "refused",
9789-
"translation/functions/greatest-ignore-nulls.toml#t003_varchar": "refused",
9790-
"translation/functions/greatest-ignore-nulls.toml#t004_date": "refused",
9791-
"translation/functions/greatest-ignore-nulls.toml#t005_null_expr": "refused",
9789+
"translation/functions/greatest-ignore-nulls.toml#t003_varchar": "matching",
9790+
"translation/functions/greatest-ignore-nulls.toml#t004_date": "matching",
9791+
"translation/functions/greatest-ignore-nulls.toml#t005_null_expr": "matching",
97929792
"translation/functions/greatest.toml#t001_number": "matching",
97939793
"translation/functions/greatest.toml#t002_first_type": "refused",
97949794
"translation/functions/greatest.toml#t003_varchar": "matching",
@@ -10172,11 +10172,11 @@
1017210172
"translation/functions/lead.toml#t002_ignore_nulls": "matching",
1017310173
"translation/functions/lead.toml#t003_negative_offset": "matching",
1017410174
"translation/functions/lead.toml#t004_null_expr": "matching",
10175-
"translation/functions/least-ignore-nulls.toml#t001_number": "refused",
10175+
"translation/functions/least-ignore-nulls.toml#t001_number": "matching",
1017610176
"translation/functions/least-ignore-nulls.toml#t002_first_type": "refused",
10177-
"translation/functions/least-ignore-nulls.toml#t003_varchar": "refused",
10178-
"translation/functions/least-ignore-nulls.toml#t004_date": "refused",
10179-
"translation/functions/least-ignore-nulls.toml#t005_null_expr": "refused",
10177+
"translation/functions/least-ignore-nulls.toml#t003_varchar": "matching",
10178+
"translation/functions/least-ignore-nulls.toml#t004_date": "matching",
10179+
"translation/functions/least-ignore-nulls.toml#t005_null_expr": "matching",
1018010180
"translation/functions/least.toml#t001_number": "matching",
1018110181
"translation/functions/least.toml#t002_first_type": "refused",
1018210182
"translation/functions/least.toml#t003_varchar": "matching",
@@ -16796,11 +16796,11 @@
1679616796
"sf.functions.getdate.t003": "unmet",
1679716797
"sf.functions.getvariable.t001": "unmet",
1679816798
"sf.functions.getvariable.t002": "unmet",
16799-
"sf.functions.greatest-ignore-nulls.t001": "unmet",
16799+
"sf.functions.greatest-ignore-nulls.t001": "met",
1680016800
"sf.functions.greatest-ignore-nulls.t002": "unmet",
16801-
"sf.functions.greatest-ignore-nulls.t003": "unmet",
16802-
"sf.functions.greatest-ignore-nulls.t004": "unmet",
16803-
"sf.functions.greatest-ignore-nulls.t005": "unmet",
16801+
"sf.functions.greatest-ignore-nulls.t003": "met",
16802+
"sf.functions.greatest-ignore-nulls.t004": "met",
16803+
"sf.functions.greatest-ignore-nulls.t005": "met",
1680416804
"sf.functions.greatest.t001": "met",
1680516805
"sf.functions.greatest.t002": "unmet",
1680616806
"sf.functions.greatest.t003": "met",
@@ -17186,11 +17186,11 @@
1718617186
"sf.functions.lead.t002": "met",
1718717187
"sf.functions.lead.t003": "met",
1718817188
"sf.functions.lead.t004": "met",
17189-
"sf.functions.least-ignore-nulls.t001": "unmet",
17189+
"sf.functions.least-ignore-nulls.t001": "met",
1719017190
"sf.functions.least-ignore-nulls.t002": "unmet",
17191-
"sf.functions.least-ignore-nulls.t003": "unmet",
17192-
"sf.functions.least-ignore-nulls.t004": "unmet",
17193-
"sf.functions.least-ignore-nulls.t005": "unmet",
17191+
"sf.functions.least-ignore-nulls.t003": "met",
17192+
"sf.functions.least-ignore-nulls.t004": "met",
17193+
"sf.functions.least-ignore-nulls.t005": "met",
1719417194
"sf.functions.least.t001": "met",
1719517195
"sf.functions.least.t002": "unmet",
1719617196
"sf.functions.least.t003": "met",

‎crates/sqlquack-rules/src/catalogue/constants.rs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,8 +1219,13 @@ pub const RULES: &[RuleSpec] = &[
12191219
},
12201220
RuleSpec {
12211221
id: LEAST_GREATEST,
1222-
constructs: &["fn:LEAST", "fn:GREATEST"],
1223-
conditions: "Arguments that compare directly with each other, NUMBER beside FLOAT included as recorded; the result has their unified type and is NULL when any argument is NULL (DuckDB's own functions skip NULLs).",
1222+
constructs: &[
1223+
"fn:LEAST",
1224+
"fn:GREATEST",
1225+
"fn:LEAST_IGNORE_NULLS",
1226+
"fn:GREATEST_IGNORE_NULLS",
1227+
],
1228+
conditions: "Arguments that compare directly with each other, NUMBER beside FLOAT included as recorded; the result has their unified type. LEAST and GREATEST are NULL when any argument is NULL; LEAST_IGNORE_NULLS and GREATEST_IGNORE_NULLS skip NULLs, as DuckDB's own functions do, and are NULL only when every argument is.",
12241229
failures: &[],
12251230
},
12261231
RuleSpec {

‎crates/sqlquack-rules/src/conversion/_helpers/expressions/calls.rs‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ use crate::conversion::constants::{
7878
};
7979
use sqlquack_policy::policy::constants::NUMERIC_RANGE;
8080

81+
/// The suffix of LEAST and GREATEST forms that skip NULL arguments.
82+
const IGNORE_NULLS_SUFFIX: &str = "_IGNORE_NULLS";
83+
8184
/// SEQ4() or SEQ8() in a query level reading only a generator: the row's number from 0.
8285
fn generator_sequence(
8386
name: &str,
@@ -334,14 +337,16 @@ pub(crate) fn convert_function(payload: &Value, site: Site<'_>) -> Result<Emitte
334337
("NVL2", [tested, when_set, otherwise]) => {
335338
nvl2((tested, when_set, otherwise), &construct, site)
336339
}
337-
("LEAST" | "GREATEST", parts) if !parts.is_empty() => {
340+
("LEAST" | "GREATEST" | "LEAST_IGNORE_NULLS" | "GREATEST_IGNORE_NULLS", parts)
341+
if !parts.is_empty() =>
342+
{
338343
least_greatest(&name, parts, &construct, site)
339344
}
340345
_ => Err(missing(construct, format!("no rule converts fn:{name}"))),
341346
}
342347
}
343348

344-
/// LEAST or GREATEST, NULL when any argument is NULL, as in Snowflake.
349+
/// LEAST or GREATEST, NULL when any argument is NULL; the _IGNORE_NULLS forms skip NULLs.
345350
fn least_greatest(
346351
name: &str,
347352
arguments: &[Value],
@@ -357,19 +362,23 @@ fn least_greatest(
357362
comparable(converted[0].clone(), other.clone(), construct, site.context)?;
358363
}
359364
let (branches, target) = unified(&converted, construct, site.context)?;
365+
let (function, ignoring) = match name.strip_suffix(IGNORE_NULLS_SUFFIX) {
366+
Some(function) => (function.to_ascii_lowercase(), true),
367+
None => (name.to_ascii_lowercase(), false),
368+
};
369+
let call = format!("{function}({})", branches.join(", "));
360370
let any_null: Vec<String> = branches
361371
.iter()
362372
.map(|branch| format!("{branch} IS NULL"))
363373
.collect();
364-
Ok(Emitted::new(
365-
format!(
366-
"(CASE WHEN {} THEN NULL ELSE {}({}) END)",
367-
any_null.join(" OR "),
368-
name.to_ascii_lowercase(),
369-
branches.join(", ")
374+
let sql = match ignoring {
375+
true => call,
376+
false => format!(
377+
"(CASE WHEN {} THEN NULL ELSE {call} END)",
378+
any_null.join(" OR ")
370379
),
371-
target,
372-
))
380+
};
381+
Ok(Emitted::new(sql, target))
373382
}
374383

375384
fn nullif(left: &Value, right: &Value, site: Site<'_>) -> Result<Emitted, Refusal> {

‎sqlquack.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ minimum_bytes = 4194304
302302
# can return another value.
303303

304304
[divergences.numeric_range]
305-
constructs = ["fn:SUM", "fn:AVG", "node:add", "node:sub", "node:mul", "node:div", "node:eq", "node:neq", "node:lt", "node:lte", "node:gt", "node:gte", "node:null_safe_eq", "node:null_safe_neq", "node:in", "fn:EQUAL_NULL", "fn:NULLIF", "node:union", "node:except", "node:intersect", "node:case", "fn:COALESCE", "node:coalesce", "fn:DECODE", "fn:GREATEST", "fn:LEAST", "node:nvl", "fn:NVL2", "fn:IFF", "fn:IFNULL", "fn:ZEROIFNULL", "fn:EXP", "fn:VARIANCE", "fn:VAR_SAMP", "fn:VAR_POP", "fn:VARIANCE_POP", "fn:STDDEV", "fn:STDDEV_SAMP", "fn:STDDEV_POP"]
305+
constructs = ["fn:SUM", "fn:AVG", "node:add", "node:sub", "node:mul", "node:div", "node:eq", "node:neq", "node:lt", "node:lte", "node:gt", "node:gte", "node:null_safe_eq", "node:null_safe_neq", "node:in", "fn:EQUAL_NULL", "fn:NULLIF", "node:union", "node:except", "node:intersect", "node:case", "fn:COALESCE", "node:coalesce", "fn:DECODE", "fn:GREATEST", "fn:LEAST", "fn:GREATEST_IGNORE_NULLS", "fn:LEAST_IGNORE_NULLS", "node:nvl", "fn:NVL2", "fn:IFF", "fn:IFNULL", "fn:ZEROIFNULL", "fn:EXP", "fn:VARIANCE", "fn:VAR_SAMP", "fn:VAR_POP", "fn:VARIANCE_POP", "fn:STDDEV", "fn:STDDEV_SAMP", "fn:STDDEV_POP"]
306306
default = "warn"
307307
strict = "refuse"
308308
failure = "loud"

0 commit comments

Comments
 (0)