From 96b4077d2db8ff817d7f151a7423b6221227fd2b Mon Sep 17 00:00:00 2001 From: tkaunlaky-e6 Date: Fri, 20 Nov 2026 17:42:35 +0530 Subject: [PATCH 1/2] fix(e6): Tableau E'...' strings in Postgres regions of the subquery tree Under SUBQUERY_TREE a Custom SQL region without backticks parses as Postgres, so a Tableau parameter E'...\n' became a Postgres byte string that e6 printed bare (engine: Lexical error on "\"). With (SAMSUNG_TABLEAU_DASHBOARD, SUBQUERY_TREE or SUBQUERY_TREE_MULTIDIALECT) + STRIP_E_STRING_PREFIX, emit it as a quoted string with its escapes decoded as Postgres does; STRIP_STRING_NEWLINES then drops the newline, as on the Databricks path. The tree's structural tokenize also treats a backslash as a string escape, so a backslash-escaped quote ('O\'Brien', E'O\'Brien') no longer ends a string early and fails the request. --- apis/utils/subquery_tree.py | 10 +++- sqlglot/dialects/e6.py | 38 ++++++++++++++ tests/dialects/test_e6.py | 53 ++++++++++++++++++++ tests/test_subquery_tree.py | 99 +++++++++++++++++++++++++++++++++++++ 4 files changed, 198 insertions(+), 2 deletions(-) diff --git a/apis/utils/subquery_tree.py b/apis/utils/subquery_tree.py index 09235f6571..c99111cfdb 100644 --- a/apis/utils/subquery_tree.py +++ b/apis/utils/subquery_tree.py @@ -21,7 +21,7 @@ from sqlglot import exp from sqlglot.errors import ParseError, TokenError from sqlglot.dialects.dialect import Dialect -from sqlglot.tokens import Token +from sqlglot.tokens import Token, Tokenizer from apis.utils.lite_parser import lite_parse from apis.utils.multidialect import MARKER, PLACEHOLDER, _splice @@ -30,6 +30,12 @@ logger = logging.getLogger(__name__) +class _BackslashTokenizer(Tokenizer): + # Discovery only needs the structure: a backslash-escaped quote ('it\'s', Tableau + # E'it\'s') must not end a string early + STRING_ESCAPES = ["'", "\\"] + + @dataclass class SQNode: """A subquery: ``label`` (``"ROOT"`` / ``"S0"`` / ...), verbatim ``raw`` source, @@ -142,7 +148,7 @@ def transpile_multidialect(query: str, pretty: bool = True, emit=None, emit_toke if emit is None and emit_tokens is None: emit_tokens = _default_emit _t_total = time.perf_counter() - tokens = Dialect().tokenize(query) + tokens = _BackslashTokenizer().tokenize(query) logger.info( "[TRANSPILE-TIMING] discovery tokenize=%.1f ms (%d tokens)", (time.perf_counter() - _t_total) * 1000, diff --git a/sqlglot/dialects/e6.py b/sqlglot/dialects/e6.py index db38e13966..17ccf005d1 100644 --- a/sqlglot/dialects/e6.py +++ b/sqlglot/dialects/e6.py @@ -1,6 +1,7 @@ from __future__ import annotations import os +import re import typing as t from sqlglot import exp, generator, parser, tokens, transforms @@ -2495,6 +2496,43 @@ def struct_sql(self, expression: exp.Struct) -> str: ] return super().struct_sql(exp.Struct(expressions=args)) + def bytestring_sql(self, expression: exp.ByteString) -> str: + # (SAMSUNG_TABLEAU_DASHBOARD, SUBQUERY_TREE or SUBQUERY_TREE_MULTIDIALECT) + + # STRIP_E_STRING_PREFIX: a Postgres E'...' escape string (a Tableau parameter in a + # region read as Postgres) becomes a quoted string with its backslash escapes + # decoded as Postgres does; the default prints it bare, e.g. E'a\n' -> a\n. + def unescape(m: t.Match) -> str: + esc = m.group(1) + if esc[0] in "xuU" and len(esc) > 1: + return chr(int(esc[1:], 16)) + if esc[0] in "01234567": + return chr(int(esc, 8)) + return {"b": "\b", "f": "\f", "n": "\n", "r": "\r", "t": "\t"}.get(esc, esc) + + if ( + self.from_dialect == "postgres" + and any( + os.getenv(flag, "false").lower() == "true" + for flag in ( + "SAMSUNG_TABLEAU_DASHBOARD", + "SUBQUERY_TREE", + "SUBQUERY_TREE_MULTIDIALECT", + ) + ) + and os.getenv("STRIP_E_STRING_PREFIX", "false").lower() == "true" + ): + return self.sql( + exp.Literal.string( + re.sub( + r"\\(x[0-9A-Fa-f]{1,2}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8}|[0-7]{1,3}|.)", + unescape, + expression.name, + flags=re.S, + ) + ) + ) + return super().bytestring_sql(expression) + def neq_sql(self, expression: exp.NEQ) -> str: return self.binary(expression, expression.meta.get("neq_op", "!=")) diff --git a/tests/dialects/test_e6.py b/tests/dialects/test_e6.py index d59e7eb0fd..12ca37a3af 100644 --- a/tests/dialects/test_e6.py +++ b/tests/dialects/test_e6.py @@ -4273,6 +4273,59 @@ def test_strip_string_newlines(self): read={"databricks": r"SELECT concat(a, '\n', b), 'x\ty', 1"}, ) + def test_postgres_escape_string(self): + """(SAMSUNG_TABLEAU_DASHBOARD, SUBQUERY_TREE or SUBQUERY_TREE_MULTIDIALECT) + + STRIP_E_STRING_PREFIX: a Postgres E'...' becomes a quoted string with its backslash + escapes decoded. Either flag alone leaves it unchanged.""" + import sqlglot + from unittest import mock + + gates = ("SAMSUNG_TABLEAU_DASHBOARD", "SUBQUERY_TREE", "SUBQUERY_TREE_MULTIDIALECT") + for flag in (*gates, "STRIP_E_STRING_PREFIX"): + with self.subTest(only=flag), mock.patch.dict(os.environ, {flag: "true"}): + self.assertEqual( + sqlglot.transpile( + r"SELECT E'a\nb'", read="postgres", write="e6", from_dialect="postgres" + )[0], + r"SELECT a\nb", + ) + + for gate in gates: + with ( + self.subTest(gate=gate), + mock.patch.dict(os.environ, {gate: "true", "STRIP_E_STRING_PREFIX": "true"}), + ): + self.validate_all(r"SELECT 'a\nb'", read={"postgres": r"SELECT E'a\nb'"}) + self.validate_all( + r"SELECT 'a\\b', 'x\ty'", + read={"postgres": r"SELECT E'a\\b', e'x\ty'"}, + ) + # octal, hex and unicode escapes; \a, \v and other letters are literal + self.validate_all( + "SELECT 'AAA', 'av', 'it\\'s'", + read={"postgres": r"SELECT E'\101\x41A', E'\a\v', E'it''s'"}, + ) + self.validate_all( + r"SELECT * FROM t WHERE ('1,2\n' = 'All' OR x IN ('1', '2'))", + read={ + "postgres": r"SELECT * FROM t WHERE (E'1,2\n' = 'All' OR x IN ('1', '2'))" + }, + ) + + # with STRIP_STRING_NEWLINES too, the Tableau parameter loses its newline + with ( + self.subTest(gate=gate, strip_newlines=True), + mock.patch.dict( + os.environ, + { + gate: "true", + "STRIP_E_STRING_PREFIX": "true", + "STRIP_STRING_NEWLINES": "true", + }, + ), + ): + self.validate_all("SELECT '1,2'", read={"postgres": r"SELECT E'1,2\n'"}) + def test_regexp_substr_native(self): """On the native executor, a Databricks REGEXP_SUBSTR (no `group`) stays REGEXP_SUBSTR; REGEXP_EXTRACT (has `group`) is untouched. The default (java) diff --git a/tests/test_subquery_tree.py b/tests/test_subquery_tree.py index e78c60a6cd..9b5389d77c 100644 --- a/tests/test_subquery_tree.py +++ b/tests/test_subquery_tree.py @@ -211,6 +211,105 @@ def test_tableau_escape_strings_without_samsung_flag(self): "FROM (SELECT EXPLODE(SPLIT('VZ_1,VZ_2', ',')) AS v) AS \"g\"))) AS \"Custom SQL Query\"", ) + def test_tableau_escape_strings_in_postgres_region(self): + # Without backticks the Custom SQL region parses as Postgres, so its E'...\n' parameter + # is a Postgres escape string; it must come out quoted and without the newline under + # SUBQUERY_TREE, or SUBQUERY_TREE_MULTIDIALECT with the MULTIDIALECT feature flag. + import os + + from fastapi.testclient import TestClient + + import converter_api + + client = TestClient(converter_api.app) + query = ( + 'SELECT "Custom SQL Query"."du_id" AS "du_id" FROM (SELECT * FROM dlt.w.t ' + "WHERE (E'VZ_1,VZ_2\\n' = 'All' OR du_id IN (SELECT trim(v) AS du_id " + "FROM (SELECT explode(split(E'VZ_1,VZ_2\\n', ',')) AS v) g))) \"Custom SQL Query\"" + ) + env = {"STRIP_E_STRING_PREFIX": "true", "STRIP_STRING_NEWLINES": "true"} + for flag, multidialect in (("SUBQUERY_TREE", False), ("SUBQUERY_TREE_MULTIDIALECT", True)): + with ( + self.subTest(flag=flag), + mock.patch.dict(os.environ, {**env, flag: "true"}), + mock.patch.object(converter_api, flag, True), + ): + response = client.post( + "/convert-query", + data={ + "query": query, + "from_sql": "postgres", + "feature_flags": json.dumps( + {"PRETTY_PRINT": False, "MULTIDIALECT": multidialect} + ), + }, + ) + self.assertEqual( + response.json().get("converted_query"), + 'SELECT "Custom SQL Query"."du_id" AS "du_id" FROM (SELECT * FROM dlt.w.t ' + "WHERE ('VZ_1,VZ_2' = 'All' OR du_id IN (SELECT TRIM(v) AS du_id " + "FROM (SELECT EXPLODE(SPLIT('VZ_1,VZ_2', ',')) AS v) AS \"g\"))) " + 'AS "Custom SQL Query"', + ) + + def test_backslash_escaped_quote(self): + # A backslash-escaped quote must not end a string early when the tree finds the + # subqueries: a Databricks 'O\'Brien' and a Tableau E'O\'Brien' both convert. The + # E'...' flags are read at import, so the Tableau case runs in a subprocess. + import os + import subprocess + import sys + from pathlib import Path + + from fastapi.testclient import TestClient + + import converter_api + + with mock.patch.object(converter_api, "SUBQUERY_TREE", True): + response = TestClient(converter_api.app).post( + "/convert-query", + data={ + "query": r"SELECT a FROM (SELECT * FROM t WHERE name = 'O\'Brien') x", + "from_sql": "databricks", + "feature_flags": json.dumps({"PRETTY_PRINT": False}), + }, + ) + self.assertEqual( + response.json().get("converted_query"), + r"""SELECT a FROM (SELECT * FROM t WHERE "name" = 'O\'Brien') AS x""", + ) + + code = ( + "import logging, sys\n" + "logging.disable(logging.CRITICAL)\n" + "from fastapi.testclient import TestClient\n" + "import converter_api\n" + "r = TestClient(converter_api.app).post('/convert-query', data={'query': sys.argv[1], " + "'from_sql': 'postgres', 'feature_flags': '{\"PRETTY_PRINT\": false}'})\n" + "print(r.json()['converted_query'])\n" + ) + env = { + **{k: v for k, v in os.environ.items() if k != "SAMSUNG_TABLEAU_DASHBOARD"}, + "SUBQUERY_TREE": "true", + "STRIP_E_STRING_PREFIX": "true", + } + result = subprocess.run( + [ + sys.executable, + "-c", + code, + r"""SELECT "q"."a" AS "a" FROM (SELECT * FROM t WHERE name = E'O\'Brien') "q\"""", + ], + capture_output=True, + text=True, + env=env, + cwd=Path(__file__).parents[1], + ) + self.assertEqual( + result.stdout.splitlines()[-1] if result.stdout else result.stderr, + r"""SELECT "q"."a" AS "a" FROM (SELECT * FROM t WHERE "name" = 'O\'Brien') AS "q\"""", + ) + def test_inner_dialect_matches_split_path(self): # A region that fails the Postgres parse is read as INNER_DIALECT, as in split_pg_outer. from fastapi.testclient import TestClient From 33c7c1fcdaab9712f9f4026a4b04629c7fc449e0 Mon Sep 17 00:00:00 2001 From: tkaunlaky-e6 Date: Fri, 20 Nov 2026 18:10:31 +0530 Subject: [PATCH 2/2] refactor(e6): read Tableau E'...' in the Postgres tokenizer instead of a regex decoder Replace the e6 bytestring_sql regex decoder with the same tokenizer setting the Databricks fix uses: under (SAMSUNG_TABLEAU_DASHBOARD, SUBQUERY_TREE or SUBQUERY_TREE_MULTIDIALECT) + STRIP_E_STRING_PREFIX the Postgres tokenizer reads E'...' as a plain quote with backslash escapes, and sqlglot decodes them (Python and Rust tokenizers). The flag is read at import, so the tests run in a subprocess. --- sqlglot/dialects/e6.py | 38 --------------- sqlglot/dialects/postgres.py | 17 +++++++ tests/dialects/test_e6.py | 90 ++++++++++++++++++------------------ tests/test_subquery_tree.py | 57 +++++++++++++---------- 4 files changed, 93 insertions(+), 109 deletions(-) diff --git a/sqlglot/dialects/e6.py b/sqlglot/dialects/e6.py index 17ccf005d1..db38e13966 100644 --- a/sqlglot/dialects/e6.py +++ b/sqlglot/dialects/e6.py @@ -1,7 +1,6 @@ from __future__ import annotations import os -import re import typing as t from sqlglot import exp, generator, parser, tokens, transforms @@ -2496,43 +2495,6 @@ def struct_sql(self, expression: exp.Struct) -> str: ] return super().struct_sql(exp.Struct(expressions=args)) - def bytestring_sql(self, expression: exp.ByteString) -> str: - # (SAMSUNG_TABLEAU_DASHBOARD, SUBQUERY_TREE or SUBQUERY_TREE_MULTIDIALECT) + - # STRIP_E_STRING_PREFIX: a Postgres E'...' escape string (a Tableau parameter in a - # region read as Postgres) becomes a quoted string with its backslash escapes - # decoded as Postgres does; the default prints it bare, e.g. E'a\n' -> a\n. - def unescape(m: t.Match) -> str: - esc = m.group(1) - if esc[0] in "xuU" and len(esc) > 1: - return chr(int(esc[1:], 16)) - if esc[0] in "01234567": - return chr(int(esc, 8)) - return {"b": "\b", "f": "\f", "n": "\n", "r": "\r", "t": "\t"}.get(esc, esc) - - if ( - self.from_dialect == "postgres" - and any( - os.getenv(flag, "false").lower() == "true" - for flag in ( - "SAMSUNG_TABLEAU_DASHBOARD", - "SUBQUERY_TREE", - "SUBQUERY_TREE_MULTIDIALECT", - ) - ) - and os.getenv("STRIP_E_STRING_PREFIX", "false").lower() == "true" - ): - return self.sql( - exp.Literal.string( - re.sub( - r"\\(x[0-9A-Fa-f]{1,2}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8}|[0-7]{1,3}|.)", - unescape, - expression.name, - flags=re.S, - ) - ) - ) - return super().bytestring_sql(expression) - def neq_sql(self, expression: exp.NEQ) -> str: return self.binary(expression, expression.meta.get("neq_op", "!=")) diff --git a/sqlglot/dialects/postgres.py b/sqlglot/dialects/postgres.py index 72ea681ae9..e2bf871396 100644 --- a/sqlglot/dialects/postgres.py +++ b/sqlglot/dialects/postgres.py @@ -324,6 +324,23 @@ class Tokenizer(tokens.Tokenizer): BIT_STRINGS = [("b'", "'"), ("B'", "'")] HEX_STRINGS = [("x'", "'"), ("X'", "'")] BYTE_STRINGS = [("e'", "'"), ("E'", "'")] + # (SAMSUNG_TABLEAU_DASHBOARD, SUBQUERY_TREE or SUBQUERY_TREE_MULTIDIALECT) + + # STRIP_E_STRING_PREFIX: read a Tableau E'...' parameter as a plain string with its + # backslash escapes decoded, as the Databricks tokenizer does. + if ( + any( + os.getenv(flag, "false").lower() == "true" + for flag in ( + "SAMSUNG_TABLEAU_DASHBOARD", + "SUBQUERY_TREE", + "SUBQUERY_TREE_MULTIDIALECT", + ) + ) + and os.getenv("STRIP_E_STRING_PREFIX", "false").lower() == "true" + ): + BYTE_STRINGS = [] + QUOTES = ["'", ("E'", "'"), ("e'", "'")] + STRING_ESCAPES = ["'", "\\"] HEREDOC_STRINGS = ["$"] HEREDOC_TAG_IS_IDENTIFIER = True diff --git a/tests/dialects/test_e6.py b/tests/dialects/test_e6.py index 12ca37a3af..1e9734c672 100644 --- a/tests/dialects/test_e6.py +++ b/tests/dialects/test_e6.py @@ -4273,58 +4273,56 @@ def test_strip_string_newlines(self): read={"databricks": r"SELECT concat(a, '\n', b), 'x\ty', 1"}, ) - def test_postgres_escape_string(self): + def test_postgres_tableau_escape_strings(self): """(SAMSUNG_TABLEAU_DASHBOARD, SUBQUERY_TREE or SUBQUERY_TREE_MULTIDIALECT) + - STRIP_E_STRING_PREFIX: a Postgres E'...' becomes a quoted string with its backslash - escapes decoded. Either flag alone leaves it unchanged.""" - import sqlglot - from unittest import mock + STRIP_E_STRING_PREFIX: Postgres reads a Tableau E'...' parameter as a plain string with + its backslash escapes decoded, as Databricks does. The flag is read at import, so the + cases run in a subprocess.""" + import json + import subprocess + import sys + + cases = { + r"SELECT E'a\nb'": r"SELECT 'a\nb'", + r"SELECT E'a\rb', E'a\tb', E'a\bb', E'a\fb'": r"SELECT 'a\rb', 'a\tb', 'a\bb', 'a\fb'", + r"SELECT E'C:\\tmp'": r"SELECT 'C:\\tmp'", + r"SELECT E'it\'s', E'it''s'": r"SELECT 'it\'s', 'it\'s'", + r"SELECT e'x\n'": r"SELECT 'x\n'", + r"SELECT * FROM t WHERE (E'1,2\n' = 'All' OR x IN ('1'))": r"SELECT * FROM t WHERE ('1,2\n' = 'All' OR x IN ('1'))", + "SELECT e.x FROM t AS e WHERE e.x = 'y'": "SELECT e.x FROM t AS e WHERE e.x = 'y'", + } + code = ( + "import json, sys, sqlglot; " + "print(json.dumps([sqlglot.transpile(q, read='postgres', write='e6', " + "from_dialect='postgres')[0] for q in json.loads(sys.argv[1])]))" + ) + + def run(**flags): + return subprocess.run( + [sys.executable, "-c", code, json.dumps(list(cases))], + capture_output=True, + text=True, + env={**os.environ, **flags}, + ) + # either flag alone: unchanged, E'...' stays a Postgres byte string gates = ("SAMSUNG_TABLEAU_DASHBOARD", "SUBQUERY_TREE", "SUBQUERY_TREE_MULTIDIALECT") - for flag in (*gates, "STRIP_E_STRING_PREFIX"): - with self.subTest(only=flag), mock.patch.dict(os.environ, {flag: "true"}): - self.assertEqual( - sqlglot.transpile( - r"SELECT E'a\nb'", read="postgres", write="e6", from_dialect="postgres" - )[0], - r"SELECT a\nb", - ) + for flag in gates: + with self.subTest(only=flag): + self.assertNotIn("'a\\nb'", run(**{flag: "true"}).stdout) for gate in gates: - with ( - self.subTest(gate=gate), - mock.patch.dict(os.environ, {gate: "true", "STRIP_E_STRING_PREFIX": "true"}), - ): - self.validate_all(r"SELECT 'a\nb'", read={"postgres": r"SELECT E'a\nb'"}) - self.validate_all( - r"SELECT 'a\\b', 'x\ty'", - read={"postgres": r"SELECT E'a\\b', e'x\ty'"}, - ) - # octal, hex and unicode escapes; \a, \v and other letters are literal - self.validate_all( - "SELECT 'AAA', 'av', 'it\\'s'", - read={"postgres": r"SELECT E'\101\x41A', E'\a\v', E'it''s'"}, - ) - self.validate_all( - r"SELECT * FROM t WHERE ('1,2\n' = 'All' OR x IN ('1', '2'))", - read={ - "postgres": r"SELECT * FROM t WHERE (E'1,2\n' = 'All' OR x IN ('1', '2'))" - }, - ) + result = run(**{gate: "true", "STRIP_E_STRING_PREFIX": "true"}) + self.assertEqual(result.stderr.count("Error"), 0, result.stderr) + for (sql, expected), actual in zip(cases.items(), json.loads(result.stdout)): + with self.subTest(gate=gate, sql=sql): + self.assertEqual(actual, expected) - # with STRIP_STRING_NEWLINES too, the Tableau parameter loses its newline - with ( - self.subTest(gate=gate, strip_newlines=True), - mock.patch.dict( - os.environ, - { - gate: "true", - "STRIP_E_STRING_PREFIX": "true", - "STRIP_STRING_NEWLINES": "true", - }, - ), - ): - self.validate_all("SELECT '1,2'", read={"postgres": r"SELECT E'1,2\n'"}) + # + STRIP_STRING_NEWLINES: the Tableau parameter loses its newline + result = run( + SUBQUERY_TREE="true", STRIP_E_STRING_PREFIX="true", STRIP_STRING_NEWLINES="true" + ) + self.assertEqual(json.loads(result.stdout)[0], "SELECT 'ab'", result.stderr) def test_regexp_substr_native(self): """On the native executor, a Databricks REGEXP_SUBSTR (no `group`) stays diff --git a/tests/test_subquery_tree.py b/tests/test_subquery_tree.py index 9b5389d77c..c1dbaf9c17 100644 --- a/tests/test_subquery_tree.py +++ b/tests/test_subquery_tree.py @@ -213,39 +213,46 @@ def test_tableau_escape_strings_without_samsung_flag(self): def test_tableau_escape_strings_in_postgres_region(self): # Without backticks the Custom SQL region parses as Postgres, so its E'...\n' parameter - # is a Postgres escape string; it must come out quoted and without the newline under - # SUBQUERY_TREE, or SUBQUERY_TREE_MULTIDIALECT with the MULTIDIALECT feature flag. + # must come out quoted and without the newline under SUBQUERY_TREE, or + # SUBQUERY_TREE_MULTIDIALECT with the MULTIDIALECT feature flag. The flags are read at + # import, so each case runs in a subprocess. import os + import subprocess + import sys + from pathlib import Path - from fastapi.testclient import TestClient - - import converter_api - - client = TestClient(converter_api.app) query = ( 'SELECT "Custom SQL Query"."du_id" AS "du_id" FROM (SELECT * FROM dlt.w.t ' "WHERE (E'VZ_1,VZ_2\\n' = 'All' OR du_id IN (SELECT trim(v) AS du_id " "FROM (SELECT explode(split(E'VZ_1,VZ_2\\n', ',')) AS v) g))) \"Custom SQL Query\"" ) - env = {"STRIP_E_STRING_PREFIX": "true", "STRIP_STRING_NEWLINES": "true"} - for flag, multidialect in (("SUBQUERY_TREE", False), ("SUBQUERY_TREE_MULTIDIALECT", True)): - with ( - self.subTest(flag=flag), - mock.patch.dict(os.environ, {**env, flag: "true"}), - mock.patch.object(converter_api, flag, True), - ): - response = client.post( - "/convert-query", - data={ - "query": query, - "from_sql": "postgres", - "feature_flags": json.dumps( - {"PRETTY_PRINT": False, "MULTIDIALECT": multidialect} - ), - }, - ) + code = ( + "import json, logging, sys\n" + "logging.disable(logging.CRITICAL)\n" + "from fastapi.testclient import TestClient\n" + "import converter_api\n" + "r = TestClient(converter_api.app).post('/convert-query', data={'query': sys.argv[1], " + "'from_sql': 'postgres', 'feature_flags': json.dumps({'PRETTY_PRINT': False, " + "'MULTIDIALECT': sys.argv[2] == '1'})})\n" + "print(r.json()['converted_query'])\n" + ) + for flag, multidialect in (("SUBQUERY_TREE", "0"), ("SUBQUERY_TREE_MULTIDIALECT", "1")): + env = { + **{k: v for k, v in os.environ.items() if k != "SAMSUNG_TABLEAU_DASHBOARD"}, + flag: "true", + "STRIP_E_STRING_PREFIX": "true", + "STRIP_STRING_NEWLINES": "true", + } + result = subprocess.run( + [sys.executable, "-c", code, query, multidialect], + capture_output=True, + text=True, + env=env, + cwd=Path(__file__).parents[1], + ) + with self.subTest(flag=flag): self.assertEqual( - response.json().get("converted_query"), + result.stdout.splitlines()[-1] if result.stdout else result.stderr, 'SELECT "Custom SQL Query"."du_id" AS "du_id" FROM (SELECT * FROM dlt.w.t ' "WHERE ('VZ_1,VZ_2' = 'All' OR du_id IN (SELECT TRIM(v) AS du_id " "FROM (SELECT EXPLODE(SPLIT('VZ_1,VZ_2', ',')) AS v) AS \"g\"))) "