From c80e49f38be2ae9290a021e565bed793f74c1eee Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 3 Dec 2026 20:35:53 -0400 Subject: [PATCH] fix: tokenize Polars input as MySQL --- changelog.md | 1 + mycli/packages/dataframes/transform.py | 2 +- mycli_test/pytests/test_main_modes_repl.py | 27 +++++++++++++++++++++ mycli_test/pytests/test_polars_transform.py | 2 +- 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/changelog.md b/changelog.md index 5a0d87c3..03568328 100644 --- a/changelog.md +++ b/changelog.md @@ -10,6 +10,7 @@ Bug Fixes -------- * Display `DATE` and `TIME` scalars returned by `.|` transforms. * Stop truncating `/llm` table schemas at MySQL's `group_concat_max_len`. +* Allow backslash-escaped apostrophes in REPL queries with or without Polars transforms. Documentation diff --git a/mycli/packages/dataframes/transform.py b/mycli/packages/dataframes/transform.py index 0a8f74f0..ab172871 100644 --- a/mycli/packages/dataframes/transform.py +++ b/mycli/packages/dataframes/transform.py @@ -118,7 +118,7 @@ def _is_coerceable_scalarish(value: Any) -> bool: def parse_polars_transform(command: str) -> PolarsPipeline | None: """Parse a SQL statement with optional Polars transform and file output.""" try: - tokens = sqlglot.tokenize(command) + tokens = sqlglot.tokenize(command, read='mysql') except sqlglot.errors.TokenError as exc: raise PolarsTransformError(f'Unable to parse Polars transform: {exc}') from exc diff --git a/mycli_test/pytests/test_main_modes_repl.py b/mycli_test/pytests/test_main_modes_repl.py index 08327d48..faba47f0 100644 --- a/mycli_test/pytests/test_main_modes_repl.py +++ b/mycli_test/pytests/test_main_modes_repl.py @@ -2441,6 +2441,33 @@ def run( assert cli.output_calls[-1][1] == SQLResult(header=['count'], rows=[(1,)]) +@pytest.mark.parametrize( + ('sql', 'suffix'), + [ + (r"SELECT 'can\'t';", ''), + (r"SELECT 'can\'t'", ' .| df'), + ("SELECT 'literal .| df .> output.parquet $| cat $> output.csv'", ''), + ], +) +def test_one_iteration_respects_mysql_quoted_strings(monkeypatch: pytest.MonkeyPatch, sql: str, suffix: str) -> None: + patch_repl_runtime_defaults(monkeypatch) + result = SQLResult(header=['value'], rows=[('value',)]) + execute = SimpleNamespace(dbname='db', connection_id=0, run=Mock(return_value=iter([result]))) + cli = make_repl_cli(execute) + prepare = Mock(return_value=object()) + monkeypatch.setattr(repl_mode, 'prepare_polars_transform', prepare) + monkeypatch.setattr(repl_mode, 'run_polars_transform', Mock(return_value=result)) + + repl_mode._one_iteration(cli, repl_mode.ReplState(), sql + suffix) + + execute.run.assert_called_once_with(sql) + if suffix: + prepare.assert_called_once_with(sql, 'df') + else: + prepare.assert_not_called() + assert cli.query_history[-1].successful + + @pytest.mark.parametrize('command', ['SELECT 1 .| df', 'SELECT 1 .| df .> result.parquet', 'SELECT 1 .> result.parquet']) def test_polars_pipeline_shows_transforming_state(monkeypatch: pytest.MonkeyPatch, command: str) -> None: patch_repl_runtime_defaults(monkeypatch) diff --git a/mycli_test/pytests/test_polars_transform.py b/mycli_test/pytests/test_polars_transform.py index 6faa7310..155b0649 100644 --- a/mycli_test/pytests/test_polars_transform.py +++ b/mycli_test/pytests/test_polars_transform.py @@ -375,7 +375,7 @@ def test_parse_polars_transform_rejects_invalid_commands(command: str, message: def test_parse_polars_transform_reports_tokenizer_error(monkeypatch: pytest.MonkeyPatch) -> None: - def fail_tokenize(command: str) -> list[Any]: + def fail_tokenize(command: str, *, read: str | None = None) -> list[Any]: raise polars_transform.sqlglot.errors.TokenError('bad token') monkeypatch.setattr(polars_transform.sqlglot, 'tokenize', fail_tokenize)