Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion mycli/packages/dataframes/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
27 changes: 27 additions & 0 deletions mycli_test/pytests/test_main_modes_repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion mycli_test/pytests/test_polars_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading