From 88c1d6e6e2bc8e5ee5936b423c00fb70a2a77e49 Mon Sep 17 00:00:00 2001 From: calin p Date: Sun, 18 Oct 2026 14:30:30 +0300 Subject: [PATCH 1/5] fix: compile dates, timestamps and intervals for sql.oracle - date.trunc compiles to TRUNC(col, 'MM') with Oracle's format models; units below a minute are reported as unsupported - date.to_text compiles to TO_CHAR with Oracle format models; Oracle's FM is a toggle, so it's switched off after each element - date.diff reports that Oracle has no DATEDIFF, instead of emitting it - TIMESTAMP literals separate date and time with a space (ORA-01861 with T) - intervals are INTERVAL '3' DAY, with a leading precision above two digits; weeks become days, milli- and microseconds fractional seconds - time literals are reported as unsupported: Oracle has no TIME type Co-Authored-By: Claude Opus 5.5 --- prqlc/prqlc/src/sql/dialect.rs | 48 ++++++++++++ prqlc/prqlc/src/sql/gen_expr.rs | 109 +++++++++++++++++++++++++- prqlc/prqlc/src/sql/std.sql.prql | 8 ++ prqlc/prqlc/tests/integration/sql.rs | 113 +++++++++++++++++++++++++++ 4 files changed, 277 insertions(+), 1 deletion(-) diff --git a/prqlc/prqlc/src/sql/dialect.rs b/prqlc/prqlc/src/sql/dialect.rs index c0f05be1532c..caa140610f2c 100644 --- a/prqlc/prqlc/src/sql/dialect.rs +++ b/prqlc/prqlc/src/sql/dialect.rs @@ -781,6 +781,54 @@ impl DialectHandler for OracleDialect { fn table_alias_uses_as(&self) -> bool { false } + + // `INTERVAL '3' DAY`; an unquoted number is a syntax error. + // https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Literals.html#GUID-DC8D1DAD-7D04-45EA-9546-82810CD09A9D + fn interval_quoting_style(&self, _dtf: &DateTimeField) -> IntervalQuotingStyle { + IntervalQuotingStyle::ValueQuoted + } + + // https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Format-Models.html + // Unlike Postgres's, Oracle's `FM` is a toggle for the rest of the format, + // so it's switched off again after the element it applies to. + fn translate_chrono_item(&self, item: Item) -> Result { + Ok(match item { + Item::Numeric(Numeric::Year, Pad::Zero) => "YYYY".to_string(), + Item::Numeric(Numeric::YearMod100, Pad::Zero) => "YY".to_string(), + Item::Numeric(Numeric::Month, Pad::None) => "FMMMFM".to_string(), + Item::Numeric(Numeric::Month, Pad::Zero) => "MM".to_string(), + Item::Numeric(Numeric::Day, Pad::None) => "FMDDFM".to_string(), + Item::Numeric(Numeric::Day, Pad::Zero) => "DD".to_string(), + Item::Numeric(Numeric::Hour, Pad::None) => "FMHH24FM".to_string(), + Item::Numeric(Numeric::Hour, Pad::Zero) => "HH24".to_string(), + Item::Numeric(Numeric::Hour12, Pad::Zero) => "HH12".to_string(), + Item::Numeric(Numeric::Minute, Pad::Zero) => "MI".to_string(), + Item::Numeric(Numeric::Second, Pad::Zero) => "SS".to_string(), + Item::Numeric(Numeric::Nanosecond, Pad::Zero) => "FF6".to_string(), // Microseconds + Item::Fixed(Fixed::ShortMonthName) => "Mon".to_string(), + // Long names are blank-padded to the longest name without `FM` + Item::Fixed(Fixed::LongMonthName) => "FMMonthFM".to_string(), + Item::Fixed(Fixed::ShortWeekdayName) => "Dy".to_string(), + Item::Fixed(Fixed::LongWeekdayName) => "FMDayFM".to_string(), + Item::Fixed(Fixed::UpperAmPm) => "AM".to_string(), + Item::Fixed(Fixed::RFC3339) => "YYYY-MM-DD\"T\"HH24:MI:SS.FF6\"Z\"".to_string(), + Item::Literal(literal) if literal.contains('"') => { + return Err(Error::new_simple( + "a double quote can't be part of a date format on Oracle", + )) + } + Item::Literal(literal) => { + // Text is quoted, so that it isn't read as format elements + if literal.chars().any(|c| c.is_ascii_alphanumeric()) { + format!("\"{literal}\"") + } else { + literal.replace('\'', "''") + } + } + Item::Space(spaces) => spaces.to_string(), + item => return Err(unsupported_format_specifier(&item, "Oracle")), + }) + } } #[cfg(test)] diff --git a/prqlc/prqlc/src/sql/gen_expr.rs b/prqlc/prqlc/src/sql/gen_expr.rs index 74e80307d5f4..eb294ec1fe42 100644 --- a/prqlc/prqlc/src/sql/gen_expr.rs +++ b/prqlc/prqlc/src/sql/gen_expr.rs @@ -224,7 +224,7 @@ fn process_date_trunc( .. }, col_expr] = args { - use crate::sql::dialect::{BigQueryDialect, MsSqlDialect}; + use crate::sql::dialect::{BigQueryDialect, MsSqlDialect, OracleDialect}; if ctx.dialect.is::() { // BigQuery: DATE_TRUNC(col, DAY) — unit is an unquoted uppercase keyword @@ -276,6 +276,46 @@ fn process_date_trunc( parameters: sql_ast::FunctionArguments::None, uses_odbc_syntax: false, })); + } else if ctx.dialect.is::() { + // Oracle: TRUNC(col, 'MM') — the unit is a datetime format model + let format = match unit.to_lowercase().as_str() { + "year" => "YYYY", + "quarter" => "Q", + "month" => "MM", + "week" => "IW", + "day" => "DD", + "hour" => "HH24", + "minute" => "MI", + _ => { + return Err(Error::new_simple(format!( + "`date.trunc` to `{unit}` is not supported on Oracle" + )) + .push_hint("supported units: year, quarter, month, week, day, hour, minute") + .with_span(unit_expr.span)) + } + }; + let col = translate_expr(col_expr.clone(), ctx)?.into_ast(); + return Ok(sql_ast::Expr::Function(Function { + name: ObjectName(vec![sqlparser::ast::ObjectNamePart::Identifier( + sql_ast::Ident::new("TRUNC"), + )]), + args: sql_ast::FunctionArguments::List(FunctionArgumentList { + args: vec![ + FunctionArg::Unnamed(FunctionArgExpr::Expr(col)), + FunctionArg::Unnamed(FunctionArgExpr::Expr(sql_ast::Expr::Value( + Value::SingleQuotedString(format.to_string()).into(), + ))), + ], + clauses: vec![], + duplicate_treatment: None, + }), + over: None, + filter: None, + null_treatment: None, + within_group: vec![], + parameters: sql_ast::FunctionArguments::None, + uses_odbc_syntax: false, + })); } // All other dialects: DATE_TRUNC('unit', col) via the s-string in std.sql.prql @@ -531,6 +571,19 @@ pub(super) fn translate_literal(l: Literal, ctx: &Context) -> Result sql_ast::Expr::Value(Value::Number(format!("{f:?}"), false).into()), Literal::Integer(i) => sql_ast::Expr::Value(Value::Number(format!("{i}"), false).into()), Literal::Date(value) => translate_datetime_literal(sql_ast::DataType::Date, value, ctx), + Literal::Time(_) if ctx.dialect.is::() => { + return Err(Error::new_simple( + "time literals are not supported on Oracle, which has no TIME type", + )) + } + Literal::Timestamp(value) if ctx.dialect.is::() => { + // Oracle's TIMESTAMP literal separates the date and the time with a space + translate_datetime_literal( + sql_ast::DataType::Timestamp(None, sql_ast::TimezoneInfo::None), + value.replacen('T', " ", 1), + ctx, + ) + } Literal::Time(value) => translate_datetime_literal( sql_ast::DataType::Time(None, sql_ast::TimezoneInfo::None), value, @@ -541,6 +594,9 @@ pub(super) fn translate_literal(l: Literal, ctx: &Context) -> Result() => { + translate_oracle_interval(vau.n, &vau.unit)? + } Literal::ValueAndUnit(vau) => { let sql_parser_datetime = match vau.unit.as_str() { "years" => DateTimeField::Year, @@ -604,6 +660,57 @@ pub(super) fn translate_literal(l: Literal, ctx: &Context) -> Result +fn translate_oracle_interval(n: i64, unit: &str) -> Result { + let fraction = |n: i64, digits: u32| { + let scale = 10_i64.pow(digits); + let sign = if n < 0 { "-" } else { "" }; + let (whole, part) = (n.abs() / scale, n.abs() % scale); + let part = format!("{part:0width$}", width = digits as usize); + let part = part.trim_end_matches('0'); + if part.is_empty() { + format!("{sign}{whole}") + } else { + format!("{sign}{whole}.{part}") + } + }; + let (value, field) = match unit { + "years" => (n.to_string(), DateTimeField::Year), + "months" => (n.to_string(), DateTimeField::Month), + "weeks" => ((n * 7).to_string(), DateTimeField::Day), + "days" => (n.to_string(), DateTimeField::Day), + "hours" => (n.to_string(), DateTimeField::Hour), + "minutes" => (n.to_string(), DateTimeField::Minute), + "seconds" => (n.to_string(), DateTimeField::Second), + "milliseconds" => (fraction(n, 3), DateTimeField::Second), + "microseconds" => (fraction(n, 6), DateTimeField::Second), + _ => { + return Err(Error::new_simple(format!( + "Unsupported interval unit: {unit}" + ))) + } + }; + let digits = value + .trim_start_matches('-') + .split('.') + .next() + .map_or(1, |whole| whole.len() as u64); + Ok(sql_ast::Expr::Interval(sqlparser::ast::Interval { + value: Box::new(sql_ast::Expr::Value( + Value::SingleQuotedString(value).into(), + )), + leading_field: Some(field), + // Oracle's default leading precision is 2 digits + leading_precision: (digits > 2).then_some(digits), + last_field: None, + fractional_seconds_precision: None, + })) +} + fn translate_datetime_literal( data_type: sql_ast::DataType, value: String, diff --git a/prqlc/prqlc/src/sql/std.sql.prql b/prqlc/prqlc/src/sql/std.sql.prql index ab4e0fead528..748ca0511880 100644 --- a/prqlc/prqlc/src/sql/std.sql.prql +++ b/prqlc/prqlc/src/sql/std.sql.prql @@ -355,6 +355,14 @@ module mysql { } module oracle { + # Date functions + module date { + # https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/TO_CHAR-datetime.html + let to_text = format column -> s"TO_CHAR({column:0}, {format:0})" + # Oracle has no DATEDIFF equivalent + let diff = unit start end -> null + } + # Text functions module text { # Oracle's `CONCAT` takes exactly two arguments before 23ai, so the generic diff --git a/prqlc/prqlc/tests/integration/sql.rs b/prqlc/prqlc/tests/integration/sql.rs index af86336933da..c47db028be6f 100644 --- a/prqlc/prqlc/tests/integration/sql.rs +++ b/prqlc/prqlc/tests/integration/sql.rs @@ -7669,6 +7669,119 @@ fn test_oracle_table_alias_omits_as() { "#); } +#[test] +fn test_oracle_dates() { + // Oracle has no `DATE_TRUNC`: `TRUNC` takes a format model. Its + // `TIMESTAMP` literal separates date and time with a space. + assert_snapshot!(compile_with_sql_dialect(r###" + from t + filter d > @2024-01-31 && ts < @2025-06-01T10:30:00.123456 + select { + y = (d | date.trunc "year"), + q = (d | date.trunc "quarter"), + m = (d | date.trunc "month"), + w = (d | date.trunc "week"), + dd = (ts | date.trunc "day"), + h = (ts | date.trunc "hour"), + mi = (ts | date.trunc "minute"), + } + "###, sql::Dialect::Oracle + ).unwrap(), @r#" + SELECT + TRUNC("d", 'YYYY') AS "y", + TRUNC("d", 'Q') AS "q", + TRUNC("d", 'MM') AS "m", + TRUNC("d", 'IW') AS "w", + TRUNC("ts", 'DD') AS "dd", + TRUNC("ts", 'HH24') AS "h", + TRUNC("ts", 'MI') AS "mi" + FROM + "t" + WHERE + "d" > DATE '2024-01-31' + AND "ts" < TIMESTAMP '2025-06-01 10:30:00.123456' + "#); + + assert_snapshot!(compile_with_sql_dialect(r###" + from t + select {s = (d | date.trunc "second")} + "###, sql::Dialect::Oracle + ).unwrap_err(), @r#" + Error: + ╭─[ :3:33 ] + │ + 3 │ select {s = (d | date.trunc "second")} + │ ────┬─── + │ ╰───── `date.trunc` to `second` is not supported on Oracle + │ + │ Help: supported units: year, quarter, month, week, day, hour, minute + ───╯ + "#); + + assert_snapshot!(compile_with_sql_dialect(r###" + from t + select {x = @10:30:00} + "###, sql::Dialect::Oracle + ).unwrap_err(), @"Error: time literals are not supported on Oracle, which has no TIME type"); +} + +#[test] +fn test_oracle_intervals() { + // `INTERVAL '3' DAY`, with a leading precision above two digits. There's no + // week, millisecond or microsecond field. + assert_snapshot!(compile_with_sql_dialect(r###" + from t + select { + a = d + 1days, + b = d - 2months, + c = d + 100days, + e = d + 2weeks, + f = ts + 1500milliseconds, + g = ts - 250microseconds, + h = d + 1000years, + } + "###, sql::Dialect::Oracle + ).unwrap(), @r#" + SELECT + "d" + INTERVAL '1' DAY AS "a", + "d" - INTERVAL '2' MONTH AS "b", + "d" + INTERVAL '100' DAY (3) AS "c", + "d" + INTERVAL '14' DAY AS "e", + "ts" + INTERVAL '1.5' SECOND AS "f", + "ts" - INTERVAL '0.00025' SECOND AS "g", + "d" + INTERVAL '1000' YEAR (4) AS "h" + FROM + "t" + "#); +} + +#[test] +fn test_oracle_date_to_text() { + // Oracle's `FM` is a toggle for the rest of the format model, so it is + // switched off again after each element. + assert_snapshot!(compile_with_sql_dialect(r###" + from t + select { + a = (d | date.to_text "%-d/%-m/%y"), + b = (d | date.to_text "%A %B %-d %Y at %I:%M:%S %p"), + c = (d | date.to_text "%+"), + e = (d | date.to_text "%H:%M:%S.%f, it's %a %b"), + } + "###, sql::Dialect::Oracle + ).unwrap(), @r#" + SELECT + TO_CHAR("d", 'FMDDFM/FMMMFM/YY') AS "a", + TO_CHAR( + "d", + 'FMDayFM FMMonthFM FMDDFM YYYY "at" HH12:MI:SS AM' + ) AS "b", + TO_CHAR("d", 'YYYY-MM-DD"T"HH24:MI:SS.FF6"Z"') AS "c", + TO_CHAR("d", 'HH24:MI:SS.FF6, "it''s" Dy Mon') AS "e" + FROM + "t" + "#); +} + #[test] fn test_sqlite_text_pattern_parenthesizes_compound_argument() { // SQLite ranks `||` above both `*`/`/`/`%` and `+`/`-`, so an From 240b33864bd9de27a649b4df93edf584a9970300 Mon Sep 17 00:00:00 2001 From: calin p Date: Sun, 18 Oct 2026 15:08:57 +0300 Subject: [PATCH 2/5] docs: changelog entry for #6455 Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 07d2a7e39e0d..a3515a6f589d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,10 @@ **Fixes**: +- On the `sql.oracle` target, `date.trunc` compiles to `TRUNC`, `date.to_text` + to `TO_CHAR`, timestamp literals use a space, and intervals are quoted with a + leading precision where needed; `date.diff` and time literals are reported as + unsupported. (@calin2k, #6455) - `first` and `last` respect the window frame: an explicit `window rows:...` was previously dropped, and `last` inside `group (sort ...)` returned the current row's value rather than the group's last. (@prql-bot, #6435) From 7799518472b015b69962bbbcf5f7ef1eb486a409 Mon Sep 17 00:00:00 2001 From: calin p Date: Sun, 18 Oct 2026 15:49:25 +0300 Subject: [PATCH 3/5] fix: complete Oracle TIMESTAMP literals (seconds, offset with a colon) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Oracle's TIMESTAMP literal requires the seconds (ORA-01861 for @2030-06-01T10:30) and an offset written as ±HH:MM (ORA-01874 for -0800, ORA-01882 for Z). Co-Authored-By: Claude Opus 5.5 --- prqlc/prqlc/src/sql/gen_expr.rs | 26 ++++++++++++++++++++++++-- prqlc/prqlc/tests/integration/sql.rs | 6 +++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/prqlc/prqlc/src/sql/gen_expr.rs b/prqlc/prqlc/src/sql/gen_expr.rs index eb294ec1fe42..0a8a00ba628b 100644 --- a/prqlc/prqlc/src/sql/gen_expr.rs +++ b/prqlc/prqlc/src/sql/gen_expr.rs @@ -577,10 +577,9 @@ pub(super) fn translate_literal(l: Literal, ctx: &Context) -> Result() => { - // Oracle's TIMESTAMP literal separates the date and the time with a space translate_datetime_literal( sql_ast::DataType::Timestamp(None, sql_ast::TimezoneInfo::None), - value.replacen('T', " ", 1), + oracle_timestamp(&value), ctx, ) } @@ -660,6 +659,29 @@ pub(super) fn translate_literal(l: Literal, ctx: &Context) -> Result String { + let re = Regex::new( + r"^(\d{4}-\d{2}-\d{2})[T ](\d{2}:\d{2})(:\d{2}(?:\.\d+)?)?(Z|[+-]\d{2}(?::?\d{2})?)?$", + ) + .unwrap(); + let Some(c) = re.captures(value) else { + return value.replacen('T', " ", 1); + }; + let seconds = c.get(3).map_or(":00", |m| m.as_str()); + let offset = match c.get(4).map(|m| m.as_str()) { + None => String::new(), + Some("Z") => " +00:00".to_string(), + Some(o) => { + let digits: String = o[1..].chars().filter(|ch| *ch != ':').collect(); + let minutes = digits.get(2..4).unwrap_or("00"); + format!(" {}{}:{}", &o[..1], &digits[..2], minutes) + } + }; + format!("{} {}{}{}", &c[1], &c[2], seconds, offset) +} + /// Oracle's interval literals: `INTERVAL '3' DAY`. There's no week, millisecond /// or microsecond field, so those become days and fractional seconds, and a /// value of more than two digits needs a leading precision, `INTERVAL '100' diff --git a/prqlc/prqlc/tests/integration/sql.rs b/prqlc/prqlc/tests/integration/sql.rs index c47db028be6f..c0e38b6c04e6 100644 --- a/prqlc/prqlc/tests/integration/sql.rs +++ b/prqlc/prqlc/tests/integration/sql.rs @@ -7675,7 +7675,8 @@ fn test_oracle_dates() { // `TIMESTAMP` literal separates date and time with a space. assert_snapshot!(compile_with_sql_dialect(r###" from t - filter d > @2024-01-31 && ts < @2025-06-01T10:30:00.123456 + filter d > @2024-01-31 && ts < @2025-06-01T10:30:00.123456 && ts > @2025-01-01T08:15 + filter ts != @2025-03-01T12:00:00Z && ts != @2020-01-01T13:19:55-0800 select { y = (d | date.trunc "year"), q = (d | date.trunc "quarter"), @@ -7700,6 +7701,9 @@ fn test_oracle_dates() { WHERE "d" > DATE '2024-01-31' AND "ts" < TIMESTAMP '2025-06-01 10:30:00.123456' + AND "ts" > TIMESTAMP '2025-01-01 08:15:00' + AND "ts" <> TIMESTAMP '2025-03-01 12:00:00 +00:00' + AND "ts" <> TIMESTAMP '2020-01-01 13:19:55 -08:00' "#); assert_snapshot!(compile_with_sql_dialect(r###" From f416fcef9909507123e82eda4a3280eeb785d45b Mon Sep 17 00:00:00 2001 From: calin p Date: Sun, 18 Oct 2026 21:34:18 +0300 Subject: [PATCH 4/5] fix: address review on Oracle dates and intervals - Oracle TIMESTAMP literals: an hour-only time and a fraction right after the minutes are completed too (`08` -> `08:00:00`, `08:15.5` -> `08:15:00.5`). - A `weeks` interval too large for days is an error instead of an overflow. - Drop OracleDialect::interval_quoting_style, which was never called: every Oracle interval goes through translate_oracle_interval. Co-Authored-By: Claude Opus 5.5 --- prqlc/prqlc/src/sql/dialect.rs | 6 ------ prqlc/prqlc/src/sql/gen_expr.rs | 22 ++++++++++++++-------- prqlc/prqlc/tests/integration/sql.rs | 9 +++++++++ 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/prqlc/prqlc/src/sql/dialect.rs b/prqlc/prqlc/src/sql/dialect.rs index caa140610f2c..39113657d886 100644 --- a/prqlc/prqlc/src/sql/dialect.rs +++ b/prqlc/prqlc/src/sql/dialect.rs @@ -782,12 +782,6 @@ impl DialectHandler for OracleDialect { false } - // `INTERVAL '3' DAY`; an unquoted number is a syntax error. - // https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Literals.html#GUID-DC8D1DAD-7D04-45EA-9546-82810CD09A9D - fn interval_quoting_style(&self, _dtf: &DateTimeField) -> IntervalQuotingStyle { - IntervalQuotingStyle::ValueQuoted - } - // https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Format-Models.html // Unlike Postgres's, Oracle's `FM` is a toggle for the rest of the format, // so it's switched off again after the element it applies to. diff --git a/prqlc/prqlc/src/sql/gen_expr.rs b/prqlc/prqlc/src/sql/gen_expr.rs index 0a8a00ba628b..720db9ed199d 100644 --- a/prqlc/prqlc/src/sql/gen_expr.rs +++ b/prqlc/prqlc/src/sql/gen_expr.rs @@ -663,23 +663,24 @@ pub(super) fn translate_literal(l: Literal, ctx: &Context) -> Result String { let re = Regex::new( - r"^(\d{4}-\d{2}-\d{2})[T ](\d{2}:\d{2})(:\d{2}(?:\.\d+)?)?(Z|[+-]\d{2}(?::?\d{2})?)?$", + r"^(\d{4}-\d{2}-\d{2})[T ](\d{2})(:\d{2})?(:\d{2})?(\.\d+)?(Z|[+-]\d{2}:?\d{2})?$", ) .unwrap(); let Some(c) = re.captures(value) else { return value.replacen('T', " ", 1); }; - let seconds = c.get(3).map_or(":00", |m| m.as_str()); - let offset = match c.get(4).map(|m| m.as_str()) { + let minutes = c.get(3).map_or(":00", |m| m.as_str()); + let seconds = c.get(4).map_or(":00", |m| m.as_str()); + let fraction = c.get(5).map_or("", |m| m.as_str()); + let offset = match c.get(6).map(|m| m.as_str()) { None => String::new(), Some("Z") => " +00:00".to_string(), Some(o) => { - let digits: String = o[1..].chars().filter(|ch| *ch != ':').collect(); - let minutes = digits.get(2..4).unwrap_or("00"); - format!(" {}{}:{}", &o[..1], &digits[..2], minutes) + let o = o.replace(':', ""); + format!(" {}:{}", &o[..3], &o[3..]) } }; - format!("{} {}{}{}", &c[1], &c[2], seconds, offset) + format!("{} {}{minutes}{seconds}{fraction}{offset}", &c[1], &c[2]) } /// Oracle's interval literals: `INTERVAL '3' DAY`. There's no week, millisecond @@ -703,7 +704,12 @@ fn translate_oracle_interval(n: i64, unit: &str) -> Result { let (value, field) = match unit { "years" => (n.to_string(), DateTimeField::Year), "months" => (n.to_string(), DateTimeField::Month), - "weeks" => ((n * 7).to_string(), DateTimeField::Day), + "weeks" => { + let days = n + .checked_mul(7) + .ok_or_else(|| Error::new_simple(format!("`{n}weeks` is too large an interval")))?; + (days.to_string(), DateTimeField::Day) + } "days" => (n.to_string(), DateTimeField::Day), "hours" => (n.to_string(), DateTimeField::Hour), "minutes" => (n.to_string(), DateTimeField::Minute), diff --git a/prqlc/prqlc/tests/integration/sql.rs b/prqlc/prqlc/tests/integration/sql.rs index c0e38b6c04e6..b9ea6418c324 100644 --- a/prqlc/prqlc/tests/integration/sql.rs +++ b/prqlc/prqlc/tests/integration/sql.rs @@ -7677,6 +7677,7 @@ fn test_oracle_dates() { from t filter d > @2024-01-31 && ts < @2025-06-01T10:30:00.123456 && ts > @2025-01-01T08:15 filter ts != @2025-03-01T12:00:00Z && ts != @2020-01-01T13:19:55-0800 + filter ts != @2025-01-01T08 && ts != @2025-01-01T08:15.5 select { y = (d | date.trunc "year"), q = (d | date.trunc "quarter"), @@ -7704,6 +7705,8 @@ fn test_oracle_dates() { AND "ts" > TIMESTAMP '2025-01-01 08:15:00' AND "ts" <> TIMESTAMP '2025-03-01 12:00:00 +00:00' AND "ts" <> TIMESTAMP '2020-01-01 13:19:55 -08:00' + AND "ts" <> TIMESTAMP '2025-01-01 08:00:00' + AND "ts" <> TIMESTAMP '2025-01-01 08:15:00.5' "#); assert_snapshot!(compile_with_sql_dialect(r###" @@ -7757,6 +7760,12 @@ fn test_oracle_intervals() { FROM "t" "#); + + assert_snapshot!(compile_with_sql_dialect(r###" + from t + select {c = ts + 2000000000000000000weeks} + "###, sql::Dialect::Oracle + ).unwrap_err(), @"Error: `2000000000000000000weeks` is too large an interval"); } #[test] From a5da1410280682c4af167952ccfb496f06cadf9c Mon Sep 17 00:00:00 2001 From: calin p Date: Sun, 18 Oct 2026 22:18:27 +0300 Subject: [PATCH 5/5] test: cover the remaining Oracle date format and interval paths `%m`, `%d` and `%-H`; the errors for a double quote in a format and for a specifier Oracle can't produce; a whole number of seconds from milliseconds; and the two fallbacks the lexer doesn't reach (unit tests). Co-Authored-By: Claude Opus 5.5 --- prqlc/prqlc/src/sql/gen_expr.rs | 11 +++++++++ prqlc/prqlc/tests/integration/sql.rs | 37 ++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/prqlc/prqlc/src/sql/gen_expr.rs b/prqlc/prqlc/src/sql/gen_expr.rs index 720db9ed199d..e15e136bf610 100644 --- a/prqlc/prqlc/src/sql/gen_expr.rs +++ b/prqlc/prqlc/src/sql/gen_expr.rs @@ -1464,6 +1464,17 @@ mod test { use super::*; + #[test] + fn test_oracle_timestamp_unknown_form_only_replaces_the_t() { + // the lexer doesn't produce this, but nothing is dropped if it does + assert_eq!(oracle_timestamp("2025-01-01T08x"), "2025-01-01 08x"); + } + + #[test] + fn test_oracle_interval_unknown_unit() { + assert!(translate_oracle_interval(1, "fortnights").is_err()); + } + #[test] fn test_range_of_ranges() -> Result<()> { fn from_ints(start: Option, end: Option) -> Range { diff --git a/prqlc/prqlc/tests/integration/sql.rs b/prqlc/prqlc/tests/integration/sql.rs index b9ea6418c324..226f0154d02d 100644 --- a/prqlc/prqlc/tests/integration/sql.rs +++ b/prqlc/prqlc/tests/integration/sql.rs @@ -7746,6 +7746,7 @@ fn test_oracle_intervals() { f = ts + 1500milliseconds, g = ts - 250microseconds, h = d + 1000years, + i = ts + 2000milliseconds, } "###, sql::Dialect::Oracle ).unwrap(), @r#" @@ -7756,7 +7757,8 @@ fn test_oracle_intervals() { "d" + INTERVAL '14' DAY AS "e", "ts" + INTERVAL '1.5' SECOND AS "f", "ts" - INTERVAL '0.00025' SECOND AS "g", - "d" + INTERVAL '1000' YEAR (4) AS "h" + "d" + INTERVAL '1000' YEAR (4) AS "h", + "ts" + INTERVAL '2' SECOND AS "i" FROM "t" "#); @@ -7779,6 +7781,7 @@ fn test_oracle_date_to_text() { b = (d | date.to_text "%A %B %-d %Y at %I:%M:%S %p"), c = (d | date.to_text "%+"), e = (d | date.to_text "%H:%M:%S.%f, it's %a %b"), + f = (d | date.to_text "%d.%m %-H"), } "###, sql::Dialect::Oracle ).unwrap(), @r#" @@ -7789,10 +7792,40 @@ fn test_oracle_date_to_text() { 'FMDayFM FMMonthFM FMDDFM YYYY "at" HH12:MI:SS AM' ) AS "b", TO_CHAR("d", 'YYYY-MM-DD"T"HH24:MI:SS.FF6"Z"') AS "c", - TO_CHAR("d", 'HH24:MI:SS.FF6, "it''s" Dy Mon') AS "e" + TO_CHAR("d", 'HH24:MI:SS.FF6, "it''s" Dy Mon') AS "e", + TO_CHAR("d", 'DD.MM FMHH24FM') AS "f" FROM "t" "#); + + // Oracle's format model has no way to escape a double quote inside quoted text. + assert_snapshot!(compile_with_sql_dialect(r###" + from t + select {a = (d | date.to_text "%d \"%m\"")} + "###, sql::Dialect::Oracle + ).unwrap_err(), @r#" + Error: + ╭─[ :3:35 ] + │ + 3 │ select {a = (d | date.to_text "%d \"%m\"")} + │ ─────┬───── + │ ╰─────── a double quote can't be part of a date format on Oracle + ───╯ + "#); + + assert_snapshot!(compile_with_sql_dialect(r###" + from t + select {a = (d | date.to_text "%e")} + "###, sql::Dialect::Oracle + ).unwrap_err(), @r#" + Error: + ╭─[ :3:35 ] + │ + 3 │ select {a = (d | date.to_text "%e")} + │ ──┬─ + │ ╰─── format specifier `%_d` is not supported for Oracle + ───╯ + "#); } #[test]