From 28860bc3095e945b3d39d1eaa44f4ad64918fa76 Mon Sep 17 00:00:00 2001 From: prql-bot <107324867+prql-bot@users.noreply.github.com> Date: Mon, 16 Nov 2026 08:18:40 +0000 Subject: [PATCH 1/8] fix: keep Jinja interpolations intact in prqlc watch before digits and on always-quoting targets Closes #6562 --- prqlc/prqlc/src/cli/jinja.rs | 103 ++++++++++++++++++++++++++++++----- 1 file changed, 88 insertions(+), 15 deletions(-) diff --git a/prqlc/prqlc/src/cli/jinja.rs b/prqlc/prqlc/src/cli/jinja.rs index 3766ed5dc408..7566cda6009b 100644 --- a/prqlc/prqlc/src/cli/jinja.rs +++ b/prqlc/prqlc/src/cli/jinja.rs @@ -23,6 +23,9 @@ use minijinja::{ use regex::Regex; const ANCHOR_PREFIX: &str = "_jinja_"; +/// Closes an anchor, so text that directly follows an interpolation (such as +/// the digits in `{{ ref('orders') }}2024`) can't merge into its id. +const ANCHOR_SUFFIX: &str = "_"; #[derive(Debug)] pub enum JinjaBlock<'a> { @@ -73,7 +76,7 @@ pub fn pre_process(source: &str) -> Result<(String, JinjaContext<'_>)> { if let Some(Token::Ident("config" | "set")) = tokens.get(1) { context.header.push(source_span); } else { - let id = format!("{ANCHOR_PREFIX}{next_anchor_id}"); + let id = format!("{ANCHOR_PREFIX}{next_anchor_id}{ANCHOR_SUFFIX}"); next_anchor_id += 1; anchored_source += &id; @@ -105,18 +108,33 @@ pub fn post_process(source: &str, context: JinjaContext) -> String { res += "\n"; } - let re = Regex::new(&format!(r"{ANCHOR_PREFIX}\d+")).unwrap(); + // An anchor compiles as an identifier, so a target that always quotes + // identifiers (Snowflake, Oracle) wraps it in quotes. An interpolation + // stands for SQL rather than for a name, so quotes enclosing exactly one + // anchor are dropped along with it. + let re = Regex::new(&format!( + r#""({ANCHOR_PREFIX}\d+{ANCHOR_SUFFIX})"|`({ANCHOR_PREFIX}\d+{ANCHOR_SUFFIX})`|\[({ANCHOR_PREFIX}\d+{ANCHOR_SUFFIX})\]|({ANCHOR_PREFIX}\d+{ANCHOR_SUFFIX})"# + )) + .unwrap(); let mut last_index = 0; for cap in re.captures_iter(source) { - let cap = cap.get(0).unwrap(); - let index = cap.start(); - let anchor_id = cap.as_str(); + let whole = cap.get(0).unwrap(); + let anchor_id = cap + .iter() + .skip(1) + .flatten() + .next() + .expect("one alternative matched") + .as_str(); - res += &source[last_index..index]; - res += context.anchor_map.get(anchor_id).unwrap_or(&anchor_id); + res += &source[last_index..whole.start()]; + match context.anchor_map.get(anchor_id) { + Some(value) => res += value, + None => res += whole.as_str(), + } - last_index = index + anchor_id.len(); + last_index = whole.end(); } res += &source[last_index..]; @@ -125,6 +143,8 @@ pub fn post_process(source: &str, context: JinjaContext) -> String { #[cfg(test)] mod test { + use insta::assert_snapshot; + use super::{post_process, pre_process, Span}; #[test] @@ -163,9 +183,9 @@ mod test { // everything from the start of the file. let src = "from a\n{{ foo }}\nfrom b = {{ bar }}"; let (pre_proc_text, ctx) = pre_process(src).unwrap(); - insta::assert_yaml_snapshot!(pre_proc_text, @r#""from a\n_jinja_0\nfrom b = _jinja_1""#); - insta::assert_yaml_snapshot!(ctx.anchor_map["_jinja_0"], @r#""{{ foo }}""#); - insta::assert_yaml_snapshot!(ctx.anchor_map["_jinja_1"], @r#""{{ bar }}""#); + insta::assert_yaml_snapshot!(pre_proc_text, @r#""from a\n_jinja_0_\nfrom b = _jinja_1_""#); + insta::assert_yaml_snapshot!(ctx.anchor_map["_jinja_0_"], @r#""{{ foo }}""#); + insta::assert_yaml_snapshot!(ctx.anchor_map["_jinja_1_"], @r#""{{ bar }}""#); let post_proc_text = post_process(&pre_proc_text, ctx); insta::assert_snapshot!(post_proc_text, @" from a @@ -178,8 +198,8 @@ mod test { fn test_pre_process() { let src = r###"from in_process = {{ source('salesforce', 'in_process') }}"###; let (pre_proc_text, ctx) = pre_process(src).unwrap(); - insta::assert_yaml_snapshot!(pre_proc_text, @"from in_process = _jinja_0"); - insta::assert_yaml_snapshot!(ctx.anchor_map["_jinja_0"], @r#""{{ source('salesforce', 'in_process') }}""#); + insta::assert_yaml_snapshot!(pre_proc_text, @"from in_process = _jinja_0_"); + insta::assert_yaml_snapshot!(ctx.anchor_map["_jinja_0_"], @r#""{{ source('salesforce', 'in_process') }}""#); } #[test] @@ -197,8 +217,61 @@ mod test { // slice would land mid-character. let src = r###"from café = {{ source('salesforce', 'in_process') }}"###; let (pre_proc_text, ctx) = pre_process(src).unwrap(); - insta::assert_yaml_snapshot!(pre_proc_text, @"from café = _jinja_0"); - insta::assert_yaml_snapshot!(ctx.anchor_map["_jinja_0"], @r#""{{ source('salesforce', 'in_process') }}""#); + insta::assert_yaml_snapshot!(pre_proc_text, @"from café = _jinja_0_"); + insta::assert_yaml_snapshot!(ctx.anchor_map["_jinja_0_"], @r#""{{ source('salesforce', 'in_process') }}""#); + } + + fn compile_with_jinja(src: &str) -> String { + let (prql, ctx) = pre_process(src).unwrap(); + let opts = prqlc::Options::default().no_signature(); + let sql = prqlc::compile(&prql, &opts).unwrap(); + post_process(&sql, ctx) + } + + #[test] + fn test_interpolation_followed_by_digits() { + assert_snapshot!(compile_with_jinja("from {{ ref('orders') }}2024"), @" + SELECT + * + FROM + {{ ref('orders') }}2024 + "); + } + + #[test] + fn test_interpolation_not_quoted_on_always_quoting_target() { + assert_snapshot!(compile_with_jinja( + "prql target:sql.snowflake\nfrom {{ ref('orders') }}\nfilter amount > {{ var('min') }}" + ), @r#" + SELECT + * + FROM + {{ ref('orders') }} + WHERE + "amount" > {{ var('min') }} + "#); + assert_snapshot!(compile_with_jinja( + "prql target:sql.oracle\nfrom {{ ref('orders') }}" + ), @" + SELECT + * + FROM + {{ ref('orders') }} + "); + } + + #[test] + fn test_interpolation_in_string_literal_keeps_quotes() { + assert_snapshot!(compile_with_jinja( + "prql target:sql.snowflake\nfrom orders\nfilter name == '{{ var(\"n\") }}'" + ), @r#" + SELECT + * + FROM + "orders" + WHERE + "name" = '{{ var("n") }}' + "#); } #[test] From c6a5a522021fd0cbf1115a092510db2e61995827 Mon Sep 17 00:00:00 2001 From: prql-bot <107324867+prql-bot@users.noreply.github.com> Date: Mon, 16 Nov 2026 08:19:04 +0000 Subject: [PATCH 2/8] docs: changelog for #6567 --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d1695ec8819d..85e832d2b763 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,9 @@ **Fixes**: +- `prqlc watch` keeps a Jinja interpolation intact when digits directly follow + it, and no longer wraps one in identifier quotes on Snowflake and Oracle. + (@prql-bot, #6567) - `date.to_text` on Postgres, Redshift and MSSQL escapes a `"` or `\` that sits next to letters in the format string, such as `"%Y x\"y %d"`. It previously closed the quoted section early, producing a wrong date or, on MSSQL, an From 2128b542c1ec4ae678c31521daacbec9e8c7551b Mon Sep 17 00:00:00 2001 From: prql-bot <107324867+prql-bot@users.noreply.github.com> Date: Mon, 16 Nov 2026 08:29:35 +0000 Subject: [PATCH 3/8] fix: keep double quotes inside string literals around Jinja interpolations --- prqlc/prqlc/src/cli/jinja.rs | 49 ++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/prqlc/prqlc/src/cli/jinja.rs b/prqlc/prqlc/src/cli/jinja.rs index 7566cda6009b..fd6929daa660 100644 --- a/prqlc/prqlc/src/cli/jinja.rs +++ b/prqlc/prqlc/src/cli/jinja.rs @@ -111,27 +111,34 @@ pub fn post_process(source: &str, context: JinjaContext) -> String { // An anchor compiles as an identifier, so a target that always quotes // identifiers (Snowflake, Oracle) wraps it in quotes. An interpolation // stands for SQL rather than for a name, so quotes enclosing exactly one - // anchor are dropped along with it. + // anchor are dropped along with it. Single-quoted string literals are + // matched first so that double quotes inside them are kept; anchors within + // a literal are replaced in place. + let anchor = format!(r"{ANCHOR_PREFIX}\d+{ANCHOR_SUFFIX}"); let re = Regex::new(&format!( - r#""({ANCHOR_PREFIX}\d+{ANCHOR_SUFFIX})"|`({ANCHOR_PREFIX}\d+{ANCHOR_SUFFIX})`|\[({ANCHOR_PREFIX}\d+{ANCHOR_SUFFIX})\]|({ANCHOR_PREFIX}\d+{ANCHOR_SUFFIX})"# + r#"'(?:[^']|'')*'|"({anchor})"|`({anchor})`|\[({anchor})\]|({anchor})"# )) .unwrap(); + let bare_anchor = Regex::new(&anchor).unwrap(); + let substitute = |anchor_id: &str| context.anchor_map.get(anchor_id).copied(); let mut last_index = 0; for cap in re.captures_iter(source) { let whole = cap.get(0).unwrap(); - let anchor_id = cap - .iter() - .skip(1) - .flatten() - .next() - .expect("one alternative matched") - .as_str(); - res += &source[last_index..whole.start()]; - match context.anchor_map.get(anchor_id) { - Some(value) => res += value, - None => res += whole.as_str(), + + match cap.iter().skip(1).flatten().next() { + // A quoted or bare anchor. + Some(anchor_id) => match substitute(anchor_id.as_str()) { + Some(value) => res += value, + None => res += whole.as_str(), + }, + // A string literal: replace anchors inside it, keeping its quotes. + None => { + res += &bare_anchor.replace_all(whole.as_str(), |c: ®ex::Captures| { + substitute(&c[0]).unwrap_or(&c[0]).to_string() + }) + } } last_index = whole.end(); @@ -260,6 +267,22 @@ mod test { "); } + #[test] + fn test_double_quotes_in_string_literal_kept() { + assert_snapshot!(compile_with_jinja( + r#"prql target:sql.snowflake +from orders +filter name == "\"{{ var('n') }}\" it's""# + ), @r#" + SELECT + * + FROM + "orders" + WHERE + "name" = '"{{ var('n') }}" it''s' + "#); + } + #[test] fn test_interpolation_in_string_literal_keeps_quotes() { assert_snapshot!(compile_with_jinja( From c9bdafda82d8f069133800d94d156d6fb1fd913d Mon Sep 17 00:00:00 2001 From: prql-bot <107324867+prql-bot@users.noreply.github.com> Date: Mon, 16 Nov 2026 08:42:02 +0000 Subject: [PATCH 4/8] fix: match quoted identifiers whole so an apostrophe can't swallow a Jinja anchor --- prqlc/prqlc/src/cli/jinja.rs | 41 +++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/prqlc/prqlc/src/cli/jinja.rs b/prqlc/prqlc/src/cli/jinja.rs index fd6929daa660..7a15638caa8b 100644 --- a/prqlc/prqlc/src/cli/jinja.rs +++ b/prqlc/prqlc/src/cli/jinja.rs @@ -111,12 +111,13 @@ pub fn post_process(source: &str, context: JinjaContext) -> String { // An anchor compiles as an identifier, so a target that always quotes // identifiers (Snowflake, Oracle) wraps it in quotes. An interpolation // stands for SQL rather than for a name, so quotes enclosing exactly one - // anchor are dropped along with it. Single-quoted string literals are - // matched first so that double quotes inside them are kept; anchors within - // a literal are replaced in place. + // anchor are dropped along with it. Other quoted tokens (string literals + // and identifiers) are matched whole, so a quote character inside one + // (`'"{{ x }}"'`, `"it's"`) can't pair with a quote outside it; anchors + // within such a token are replaced in place, keeping its quotes. let anchor = format!(r"{ANCHOR_PREFIX}\d+{ANCHOR_SUFFIX}"); let re = Regex::new(&format!( - r#"'(?:[^']|'')*'|"({anchor})"|`({anchor})`|\[({anchor})\]|({anchor})"# + r#""({anchor})"|`({anchor})`|\[({anchor})\]|({anchor})|'(?:[^']|'')*'|"(?:[^"]|"")*"|`(?:[^`]|``)*`"# )) .unwrap(); let bare_anchor = Regex::new(&anchor).unwrap(); @@ -133,7 +134,7 @@ pub fn post_process(source: &str, context: JinjaContext) -> String { Some(value) => res += value, None => res += whole.as_str(), }, - // A string literal: replace anchors inside it, keeping its quotes. + // Another quoted token: replace anchors inside it, keeping its quotes. None => { res += &bare_anchor.replace_all(whole.as_str(), |c: ®ex::Captures| { substitute(&c[0]).unwrap_or(&c[0]).to_string() @@ -283,6 +284,36 @@ filter name == "\"{{ var('n') }}\" it's""# "#); } + #[test] + fn test_apostrophe_in_quoted_identifier() { + assert_snapshot!(compile_with_jinja( + r#"prql target:sql.snowflake +from {{ ref('orders') }} +select {`it's`, amount} +filter amount != 'x'"# + ), @r#" + SELECT + "it's", + "amount" + FROM + {{ ref('orders') }} + WHERE + "amount" <> 'x' + "#); + } + + #[test] + fn test_interpolation_joined_to_text_in_quoted_identifier() { + assert_snapshot!(compile_with_jinja( + "prql target:sql.snowflake\nfrom {{ ref('orders') }}2024" + ), @r#" + SELECT + * + FROM + "{{ ref('orders') }}2024" + "#); + } + #[test] fn test_interpolation_in_string_literal_keeps_quotes() { assert_snapshot!(compile_with_jinja( From 1ccbb7e4b0b37fdc355aedf083a363ae70c02405 Mon Sep 17 00:00:00 2001 From: prql-bot <107324867+prql-bot@users.noreply.github.com> Date: Mon, 16 Nov 2026 08:55:06 +0000 Subject: [PATCH 5/8] fix: keep quotes on an identifier that only starts with a Jinja anchor Match each quoted token whole, including escaped quotes, before checking whether its contents are exactly one anchor, so `"_jinja_0_""suffix"` keeps its quotes and escaped quote. --- prqlc/prqlc/src/cli/jinja.rs | 56 ++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/prqlc/prqlc/src/cli/jinja.rs b/prqlc/prqlc/src/cli/jinja.rs index 7a15638caa8b..86499b6f6c48 100644 --- a/prqlc/prqlc/src/cli/jinja.rs +++ b/prqlc/prqlc/src/cli/jinja.rs @@ -110,39 +110,46 @@ pub fn post_process(source: &str, context: JinjaContext) -> String { // An anchor compiles as an identifier, so a target that always quotes // identifiers (Snowflake, Oracle) wraps it in quotes. An interpolation - // stands for SQL rather than for a name, so quotes enclosing exactly one - // anchor are dropped along with it. Other quoted tokens (string literals - // and identifiers) are matched whole, so a quote character inside one - // (`'"{{ x }}"'`, `"it's"`) can't pair with a quote outside it; anchors - // within such a token are replaced in place, keeping its quotes. + // stands for SQL rather than for a name, so a quoted identifier whose + // entire contents are one anchor is replaced along with its quotes. Each + // quoted token (string literal or identifier, including escaped quotes + // such as `''` or `""`) is matched whole before that check, so a quote + // character inside one can't pair with a quote outside it, and a token + // that merely starts with an anchor (`"_jinja_0_""x"`) keeps its quotes. + // Anchors within any other quoted token are replaced in place. let anchor = format!(r"{ANCHOR_PREFIX}\d+{ANCHOR_SUFFIX}"); let re = Regex::new(&format!( - r#""({anchor})"|`({anchor})`|\[({anchor})\]|({anchor})|'(?:[^']|'')*'|"(?:[^"]|"")*"|`(?:[^`]|``)*`"# + r#"'(?:[^']|'')*'|"(?:[^"]|"")*"|`(?:[^`]|``)*`|\[{anchor}(?:[^\]]|\]\])*\]|{anchor}"# )) .unwrap(); let bare_anchor = Regex::new(&anchor).unwrap(); + let exact_anchor = Regex::new(&format!("^{anchor}$")).unwrap(); let substitute = |anchor_id: &str| context.anchor_map.get(anchor_id).copied(); let mut last_index = 0; - for cap in re.captures_iter(source) { - let whole = cap.get(0).unwrap(); - res += &source[last_index..whole.start()]; - - match cap.iter().skip(1).flatten().next() { - // A quoted or bare anchor. - Some(anchor_id) => match substitute(anchor_id.as_str()) { - Some(value) => res += value, - None => res += whole.as_str(), - }, - // Another quoted token: replace anchors inside it, keeping its quotes. + for token in re.find_iter(source) { + res += &source[last_index..token.start()]; + let text = token.as_str(); + + // The text an identifier's quotes enclose, or the token itself when bare. + let contents = match text.as_bytes()[0] { + b'"' | b'`' | b'[' => Some(&text[1..text.len() - 1]), + b'\'' => None, + _ => Some(text), + }; + match contents + .filter(|c| exact_anchor.is_match(c)) + .and_then(substitute) + { + Some(value) => res += value, None => { - res += &bare_anchor.replace_all(whole.as_str(), |c: ®ex::Captures| { + res += &bare_anchor.replace_all(text, |c: ®ex::Captures| { substitute(&c[0]).unwrap_or(&c[0]).to_string() }) } } - last_index = whole.end(); + last_index = token.end(); } res += &source[last_index..]; @@ -328,6 +335,17 @@ filter amount != 'x'"# "#); } + #[test] + fn test_anchor_prefix_of_quoted_identifier() { + // `"_jinja_0_""suffix"` is one identifier containing an escaped quote; + // its quotes enclose more than the anchor, so they stay. + let src = r#"SELECT "_jinja_0_""suffix", `_jinja_0_``x`, "_jinja_0_" FROM t"#; + let mut ctx = super::JinjaContext::default(); + ctx.anchor_map + .insert("_jinja_0_".to_string(), r#"{{ var("name") }}"#); + assert_snapshot!(post_process(src, ctx), @r#"SELECT "{{ var("name") }}""suffix", `{{ var("name") }}``x`, {{ var("name") }} FROM t"#); + } + #[test] fn test_config_interpolation() { let src = r###"{{ config(materialized = "table") }}\nfrom in_process = {{ source('salesforce', 'in_process') }}"###; From e4e294c27921c76b3e2eabeff5609f7a219816d9 Mon Sep 17 00:00:00 2001 From: prql-bot <107324867+prql-bot@users.noreply.github.com> Date: Mon, 16 Nov 2026 08:58:22 +0000 Subject: [PATCH 6/8] fix: keep brackets around a Jinja anchor in prqlc watch output No dialect quotes identifiers with [...], so brackets around an anchor come from SQL such as an s-string index (arr[{{ var('i') }}]) and must stay. With --no-format the bracket alternative turned that into arr{{ var('i') }}. --- prqlc/prqlc/src/cli/jinja.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/prqlc/prqlc/src/cli/jinja.rs b/prqlc/prqlc/src/cli/jinja.rs index 86499b6f6c48..b1a6c7bf60ec 100644 --- a/prqlc/prqlc/src/cli/jinja.rs +++ b/prqlc/prqlc/src/cli/jinja.rs @@ -119,7 +119,7 @@ pub fn post_process(source: &str, context: JinjaContext) -> String { // Anchors within any other quoted token are replaced in place. let anchor = format!(r"{ANCHOR_PREFIX}\d+{ANCHOR_SUFFIX}"); let re = Regex::new(&format!( - r#"'(?:[^']|'')*'|"(?:[^"]|"")*"|`(?:[^`]|``)*`|\[{anchor}(?:[^\]]|\]\])*\]|{anchor}"# + r#"'(?:[^']|'')*'|"(?:[^"]|"")*"|`(?:[^`]|``)*`|{anchor}"# )) .unwrap(); let bare_anchor = Regex::new(&anchor).unwrap(); @@ -133,7 +133,7 @@ pub fn post_process(source: &str, context: JinjaContext) -> String { // The text an identifier's quotes enclose, or the token itself when bare. let contents = match text.as_bytes()[0] { - b'"' | b'`' | b'[' => Some(&text[1..text.len() - 1]), + b'"' | b'`' => Some(&text[1..text.len() - 1]), b'\'' => None, _ => Some(text), }; @@ -346,6 +346,18 @@ filter amount != 'x'"# assert_snapshot!(post_process(src, ctx), @r#"SELECT "{{ var("name") }}""suffix", `{{ var("name") }}``x`, {{ var("name") }} FROM t"#); } + #[test] + fn test_interpolation_in_brackets_keeps_brackets() { + // No target quotes identifiers with `[…]`, so brackets around an + // anchor come from SQL such as an s-string index, and stay. + let (prql, ctx) = + pre_process("prql target:sql.duckdb\nfrom t\nselect {x = s\"arr[{{ var('i') }}]\"}") + .unwrap(); + let opts = prqlc::Options::default().no_signature().no_format(); + let sql = prqlc::compile(&prql, &opts).unwrap(); + assert_snapshot!(post_process(&sql, ctx), @"SELECT arr[{{ var('i') }}] AS x FROM t"); + } + #[test] fn test_config_interpolation() { let src = r###"{{ config(materialized = "table") }}\nfrom in_process = {{ source('salesforce', 'in_process') }}"###; From ad135fd986fd089e9de76f9030d246e803f0d1a6 Mon Sep 17 00:00:00 2001 From: prql-bot <107324867+prql-bot@users.noreply.github.com> Date: Mon, 16 Nov 2026 09:07:53 +0000 Subject: [PATCH 7/8] fix: treat a dollar-quoted literal as one token in prqlc watch output MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Consume `$$…$$` and `$tag$…$tag$` whole, so a double quote inside one isn't read as an identifier quote around a Jinja anchor. --- prqlc/prqlc/src/cli/jinja.rs | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/prqlc/prqlc/src/cli/jinja.rs b/prqlc/prqlc/src/cli/jinja.rs index b1a6c7bf60ec..572f5ed13ae7 100644 --- a/prqlc/prqlc/src/cli/jinja.rs +++ b/prqlc/prqlc/src/cli/jinja.rs @@ -117,9 +117,12 @@ pub fn post_process(source: &str, context: JinjaContext) -> String { // character inside one can't pair with a quote outside it, and a token // that merely starts with an anchor (`"_jinja_0_""x"`) keeps its quotes. // Anchors within any other quoted token are replaced in place. + // A PostgreSQL dollar-quoted literal (`$$…$$`, `$tag$…$tag$`) is matched + // by its opening tag and runs to the matching closing tag; the regex + // crate has no backreferences, so that end is found below. let anchor = format!(r"{ANCHOR_PREFIX}\d+{ANCHOR_SUFFIX}"); let re = Regex::new(&format!( - r#"'(?:[^']|'')*'|"(?:[^"]|"")*"|`(?:[^`]|``)*`|{anchor}"# + r#"'(?:[^']|'')*'|"(?:[^"]|"")*"|`(?:[^`]|``)*`|\$(?:[A-Za-z_][A-Za-z0-9_]*)?\$|{anchor}"# )) .unwrap(); let bare_anchor = Regex::new(&anchor).unwrap(); @@ -127,16 +130,24 @@ pub fn post_process(source: &str, context: JinjaContext) -> String { let substitute = |anchor_id: &str| context.anchor_map.get(anchor_id).copied(); let mut last_index = 0; - for token in re.find_iter(source) { + while let Some(token) = re.find_at(source, last_index) { res += &source[last_index..token.start()]; - let text = token.as_str(); + let mut end = token.end(); // The text an identifier's quotes enclose, or the token itself when bare. - let contents = match text.as_bytes()[0] { - b'"' | b'`' => Some(&text[1..text.len() - 1]), + let contents = match token.as_str().as_bytes()[0] { + b'"' | b'`' => Some(&token.as_str()[1..token.len() - 1]), + b'$' => { + // An unclosed dollar quote runs to the end of the source. + end = source[end..] + .find(token.as_str()) + .map_or(source.len(), |i| end + i + token.len()); + None + } b'\'' => None, - _ => Some(text), + _ => Some(token.as_str()), }; + let text = &source[token.start()..end]; match contents .filter(|c| exact_anchor.is_match(c)) .and_then(substitute) @@ -149,7 +160,7 @@ pub fn post_process(source: &str, context: JinjaContext) -> String { } } - last_index = token.end(); + last_index = end; } res += &source[last_index..]; @@ -358,6 +369,17 @@ filter amount != 'x'"# assert_snapshot!(post_process(&sql, ctx), @"SELECT arr[{{ var('i') }}] AS x FROM t"); } + #[test] + fn test_double_quotes_in_dollar_quoted_literal_kept() { + let (prql, ctx) = pre_process( + "prql target:sql.postgres\nfrom t\nselect {x = s'$$\"{{ var(\"name\") }}\"$$', y = s'$q$ \"a\" $q$', z = {{ var('z') }}}", + ) + .unwrap(); + let opts = prqlc::Options::default().no_signature().no_format(); + let sql = prqlc::compile(&prql, &opts).unwrap(); + assert_snapshot!(post_process(&sql, ctx), @r#"SELECT $$"{{ var("name") }}"$$ AS x, $q$ "a" $q$ AS y, {{ var('z') }} AS z FROM t"#); + } + #[test] fn test_config_interpolation() { let src = r###"{{ config(materialized = "table") }}\nfrom in_process = {{ source('salesforce', 'in_process') }}"###; From 320eb56747335371caf959ab4fdcb82911d163e9 Mon Sep 17 00:00:00 2001 From: prql-bot <107324867+prql-bot@users.noreply.github.com> Date: Mon, 16 Nov 2026 09:18:39 +0000 Subject: [PATCH 8/8] fix: keep PostgreSQL escape strings and non-ASCII dollar tags whole in prqlc watch output --- prqlc/prqlc/src/cli/jinja.rs | 45 ++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/prqlc/prqlc/src/cli/jinja.rs b/prqlc/prqlc/src/cli/jinja.rs index 572f5ed13ae7..fb0f123b1601 100644 --- a/prqlc/prqlc/src/cli/jinja.rs +++ b/prqlc/prqlc/src/cli/jinja.rs @@ -119,10 +119,15 @@ pub fn post_process(source: &str, context: JinjaContext) -> String { // Anchors within any other quoted token are replaced in place. // A PostgreSQL dollar-quoted literal (`$$…$$`, `$tag$…$tag$`) is matched // by its opening tag and runs to the matching closing tag; the regex - // crate has no backreferences, so that end is found below. + // crate has no backreferences, so that end is found below. Its tag + // follows the rules of an unquoted identifier without `$`, where any + // non-ASCII character counts as a letter. A PostgreSQL escape string + // (`E'…'`) also takes backslash escapes such as `\'`. let anchor = format!(r"{ANCHOR_PREFIX}\d+{ANCHOR_SUFFIX}"); + let tag_start = r"[A-Za-z_\x{80}-\x{10FFFF}]"; + let tag_char = r"[A-Za-z0-9_\x{80}-\x{10FFFF}]"; let re = Regex::new(&format!( - r#"'(?:[^']|'')*'|"(?:[^"]|"")*"|`(?:[^`]|``)*`|\$(?:[A-Za-z_][A-Za-z0-9_]*)?\$|{anchor}"# + r#"\b[Ee]'(?:[^'\\]|\\[\s\S]|'')*'|'(?:[^']|'')*'|"(?:[^"]|"")*"|`(?:[^`]|``)*`|\$(?:{tag_start}{tag_char}*)?\$|{anchor}"# )) .unwrap(); let bare_anchor = Regex::new(&anchor).unwrap(); @@ -134,6 +139,21 @@ pub fn post_process(source: &str, context: JinjaContext) -> String { res += &source[last_index..token.start()]; let mut end = token.end(); + // `$` can also continue an unquoted identifier (`a$b$`), where it + // doesn't open a dollar quote. + if token.as_str().starts_with('$') + && source[..token.start()] + .chars() + .next_back() + .is_some_and(|c| { + c.is_ascii_alphanumeric() || matches!(c, '_' | '$') || !c.is_ascii() + }) + { + res += "$"; + last_index = token.start() + 1; + continue; + } + // The text an identifier's quotes enclose, or the token itself when bare. let contents = match token.as_str().as_bytes()[0] { b'"' | b'`' => Some(&token.as_str()[1..token.len() - 1]), @@ -380,6 +400,27 @@ filter amount != 'x'"# assert_snapshot!(post_process(&sql, ctx), @r#"SELECT $$"{{ var("name") }}"$$ AS x, $q$ "a" $q$ AS y, {{ var('z') }} AS z FROM t"#); } + #[test] + fn test_postgres_escape_string_and_non_ascii_dollar_tag_kept() { + // `E'…'` takes backslash escapes, so `\'` doesn't end it, and a dollar + // tag can hold any letter, including non-ASCII ones such as `é`. + let src = r#"SELECT E'it\'s "_jinja_0_"' AS x, e'\\' AS y, $é$"_jinja_0_"$é$ AS z, "_jinja_0_" FROM t"#; + let mut ctx = super::JinjaContext::default(); + ctx.anchor_map + .insert("_jinja_0_".to_string(), "{{ var('name') }}"); + assert_snapshot!(post_process(src, ctx), @r#"SELECT E'it\'s "{{ var('name') }}"' AS x, e'\\' AS y, $é$"{{ var('name') }}"$é$ AS z, {{ var('name') }} FROM t"#); + } + + #[test] + fn test_dollar_inside_identifier_is_not_a_dollar_quote() { + // PostgreSQL identifiers can contain `$`, so `a$b$` isn't a dollar quote. + let src = r#"SELECT a$b$, "_jinja_0_" FROM t"#; + let mut ctx = super::JinjaContext::default(); + ctx.anchor_map + .insert("_jinja_0_".to_string(), "{{ var('name') }}"); + assert_snapshot!(post_process(src, ctx), @"SELECT a$b$, {{ var('name') }} FROM t"); + } + #[test] fn test_config_interpolation() { let src = r###"{{ config(materialized = "table") }}\nfrom in_process = {{ source('salesforce', 'in_process') }}"###;