Skip to content

fix(e6): transpile Postgres E'...' escape strings (Samsung Tableau) - #330

Closed
tkaunlaky-e6 wants to merge 1 commit into
mainfrom
fix/samsung-tableau-escape-string
Closed

tkaunlaky-e6 wants to merge 1 commit into
mainfrom
fix/samsung-tableau-escape-string

Conversation

@tkaunlaky-e6

Copy link
Copy Markdown

Problem

87 Samsung dashboard queries (samsung-dashboard, query history 2026-10-23) fail with PARSING_FAILED / Lexical error ... Encountered: ""`.

Samsung's Tableau connects through the Postgres connector, whose dialect uses format-string-literal='Extended'. A parameter value containing a control char, quote or backslash is therefore bound into the Databricks Custom SQL as a Postgres escape string:

AND (E'VZ_1019618,...,VZ_1019613\n' = 'All' OR GNODEB_ID IN (... explode(split(E'VZ_...\n', ',')) ...))

With SAMSUNG_TABLEAU_DASHBOARD=true the "Custom SQL Query" region is parsed as Databricks, which has no E'...' syntax (ParseError: Expecting )). The transpiler errors and the planner falls back to the raw query, failing on the first backtick.

Postgres → e6 was also broken: E'x\n' printed as bare x\n (no quotes).

Fix

Change Source
BYTE_STRING_ESCAPES tokenizer setting (Python and Rust): Postgres E'...' bodies are decoded (\n, \t, \\, \', '', ...). Dialects that don't set it keep their STRING_ESCAPES, so they are unchanged. upstream tobymao/sqlglot tobymao#6818 (MRO lookup from tobymao#8484)
escape_str re-escapes only for dialects whose plain strings use backslash escapes, so Postgres plain-string output is unchanged upstream tobymao#6818
bytestring_sql: targets without byte-string syntax but with backslash escapes (e6) get a quoted string literal; others raise unsupported instead of emitting bare text upstream tobymao#7677
bytestring_sql (Postgres target): re-escape the decoded value so E'it''s' stays valid printing half of tobymao#6818
Databricks reads E'...' as a byte string only under SAMSUNG_TABLEAU_DASHBOARD (reuses the existing flag; off = Databricks unchanged) ours

Flow with the flag on: E'x\n' → tokenizer (byte string, decoded) → ByteString (existing parser) → e6 'x\n'.

Testing

  • tests/dialects, test_tokens, test_transpile, test_generator, test_parser, test_expressions, test_optimizer: 795 passed, 11,295 subtests, with both the Python and Rust tokenizers (Rust rebuilt from this branch).
  • Replay of 806 Samsung queries (704-query corpus + Oct/May query histories) through /convert-query with SAMSUNG_TABLEAU_DASHBOARD=true:
    • main: 719 OK, 87 fail
    • this branch: 806 OK. The 719 previously-OK outputs are byte-identical; the 87 now return 200 with the newline values preserved. Rust and Python tokenizer outputs are identical on all 806.
  • New tests: upstream Fix!: introduce BYTE_STRING_ESCAPES concept for postgres/duckdb e-strings tobymao/sqlglot#6818/Fix: improve transpilation of e'...' strings tobymao/sqlglot#7677 Postgres tests. test_e6.test_escape_strings covers all 7 Tableau Extended escapes (Postgres → e6) and the flagged Databricks case (run in a subprocess, since the flag is read at import).

Notes

Samsung's Tableau connects through the Postgres connector, which binds a
parameter value holding a control char, quote or backslash into the
Databricks Custom SQL as a Postgres escape string (E'VZ_...\n'). The
Databricks parse of the "Custom SQL Query" region failed, so 87 dashboard
queries fell back to the planner and failed with a backtick lexical error.

- Port upstream tobymao#6818 (Python and Rust tokenizer): BYTE_STRING_ESCAPES, so
  Postgres decodes E'...' bodies; dialects that don't set it keep their
  STRING_ESCAPES (inherited setting looked up through the MRO, as upstream).
- Port upstream tobymao#7677: bytestring_sql prints a byte string as a quoted string
  literal for targets without byte-string syntax (e6) instead of bare text.
- Re-escape decoded byte strings when writing Postgres (E'it''s' stays valid).
- Databricks reads E'...' as a byte string under SAMSUNG_TABLEAU_DASHBOARD.
@github-actions

Copy link
Copy Markdown

Benchmark for d15a39e

Click to view benchmark
Test Base PR %
long 225.7±3.84µs 223.6±4.37µs -0.93%

@tkaunlaky-e6

Copy link
Copy Markdown
Author

Closing in favour of a minimal, flag-scoped fix: in the Databricks tokenizer, E' opens a plain string when SAMSUNG_TABLEAU_DASHBOARD is on (2 files, no Rust, parser or generator changes). On all 806 Samsung queries its output is identical to this PR's.

This branch is kept for reference. It also handles apostrophes written as \' inside E'...' and E'...' in the outer Postgres query, neither of which appears in the Samsung data so far. If those cases ever show up, the upstream ports here (tobymao#6818 incl. Rust, tobymao#7677) can be reused.

@tkaunlaky-e6

Copy link
Copy Markdown
Author

Superseded by #332: two independent flags under SAMSUNG_TABLEAU_DASHBOARD, STRIP_E_STRING_PREFIX and STRIP_STRING_NEWLINES (3 files, no Rust changes).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant