Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix result when summarize is passed to duckdb #841

Merged
merged 15 commits into from
Sep 1, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* [Feature] Improved messages when loading configurations from `pyproject.toml` file.
* [Feature] Add `--schema/-s` for `%sqlcmd` commands that support `--table/-t` and ensure `--table schema.table` works (#519)
* [Fix] Fix result not displayed when `SUMMARIZE` argument is used in duckdb with a sqlalchemy connection (#836)

## 0.10.1 (2023-08-30)

Expand Down
2 changes: 1 addition & 1 deletion src/sql/connection/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ def _connection_execute(self, query, parameters=None):
# TODO: we can parse the query to ensure that it's a SELECT statement
# for example, it might start with WITH but the final statement might
# not be a SELECT
is_select = first_word_statement in {"select", "with", "from"}
is_select = first_word_statement in {"select", "with", "from", "summarize"}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a note explaining why we're adding "summarize" here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


operation = partial(self._execute_with_parameters, query, parameters)
out = self._execute_with_error_handling(operation)
Expand Down
32 changes: 32 additions & 0 deletions src/tests/test_magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1932,3 +1932,35 @@ def test_whitespaces_linebreaks_near_first_token(ip, query):
{{query}}"""
).result
assert str(out) == expected_result


def test_summarize_in_duckdb(ip_empty):
expected_result = {
"column_name": ("id", "x"),
"column_type": ("INTEGER", "INTEGER"),
"min": ("1", "-1"),
"max": ("3", "2"),
"approx_unique": ("3", "3"),
"avg": ("2.0", "0.6666666666666666"),
"std": ("1.0", "1.5275252316519468"),
"q25": ("1", "0"),
"q50": ("2", "1"),
"q75": ("3", "2"),
"count": (3, 3),
"null_percentage": ("0.0%", "0.0%"),
}

ip_empty.run_cell("%sql duckdb://")
ip_empty.run_cell("%sql CREATE TABLE table1 (id INTEGER, x INTEGER)")
ip_empty.run_cell(
"""%%sql
INSERT INTO table1 VALUES (1, -1), (2, 1), (3, 2)"""
)
out = ip_empty.run_cell("%sql SUMMARIZE table1").result
assert out.dict() == expected_result

out = ip_empty.run_cell(
"""%%sql
SUMMARIZE table1"""
).result
assert out.dict() == expected_result