Support DuckDB Dialect#539
Conversation
|
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
| return expr | ||
|
|
||
| return expr.this if isinstance(expr.this, (exp.Count, exp.CountIf)) else expr | ||
| return expr.this if isinstance(expr.this, exp.Count) else expr |
There was a problem hiding this comment.
Including CountIf here created an issue for DuckDB when a LEFT JOIN produces all-NULL rows for a group, DuckDB’s COUNT_IF(NULL_condition) returns NULL (not 0). The COALESCE is needed here.
Tested with all dialects and they worked fine.
| SELECT | ||
| COUNT(*) AS n_rows | ||
| FROM main.publication | ||
| FROM defog.academic.publication |
There was a problem hiding this comment.
Most defog Databricks updates are related to using correct databricks defog graph metadata. In earlier version, it was using the Sqlite metadata.
| - name: Run DEFOG refresh if stale (locked by date) | ||
| run: uv run python -m tests.gen_data.databricks_task | ||
| - name: Run DEFOG refresh | ||
| run: uv run python -m tests.gen_data.databricks_task --force |
There was a problem hiding this comment.
Force regenerating with each run. This was mainly because I had to stop running the testing mid running and that made the data incomplete.
| try: | ||
| self.cursor.execute(sql) | ||
|
|
||
| # This is only for MyPy to pass and know about fetch_pandas_all() |
There was a problem hiding this comment.
used type: ignore ... as I needed to used a different method for DuckDB so that made that TYPE_CHECKING not really needed now.
| raise pydough.active_session.error_builder.sql_runtime_failure( | ||
| sql, e, True | ||
| ) from e | ||
| finally: |
There was a problem hiding this comment.
Don't close cursor until session is done.
The old pattern created and closed a fresh cursor per method call (execute_query_df, execute_ddl, get_table_columns). DuckDB's cursor() returns an independent sub-connection, so closing between calls made TEMPORARY TABLEs and TEMPORARY VIEWs invisible to subsequent queries. The cursor is now created lazily on first access and reused for the lifetime of the connection.
| ) | ||
| return expr | ||
|
|
||
| def _remap_zero_to_one(self, idx: SQLGlotExpression) -> SQLGlotExpression: |
There was a problem hiding this comment.
Put in base and re-use in the needed dialects (Databricks, Postgres, DuckDB)
john-sanchez31
left a comment
There was a problem hiding this comment.
I left a some of comments below but overall LGTM!
| - [Databricks_TPCH.ipynb](notebooks/Databricks_TPCH.ipynb) demonstrates how to connect a Databricks database with PyDough. | ||
| - [Trino_TPCH.ipynb](notebooks/Trino_TPCH.ipynb) demonstrates how to connect a Trino database with PyDough. | ||
|
|
||
| - [DuckDB_TPCH.ipynb](notebooks/DuckDB_TPCH.ipynb) demonstrates how to connect a DuckDB database with PyDough. |
There was a problem hiding this comment.
We didn't add databricks and duckdb to our main README file on the root folder on how to run the tests section. Let's add them
|
|
||
| - `databricks`: uses a Databricks database. [See here](https://docs.databricks.com/dev-tools/python-sql-connector.html) for details on the connection API and what keyword arguments can be passed in. | ||
|
|
||
| - `duckdb`: uses a DuckDB database. [See here](https://duckdb.org/docs/api/python) for details on the connection API and what keyword arguments can be passed in. |
There was a problem hiding this comment.
Lets also add Databricks and DuckDB on documentation/functions.md to the list list of reference links for the format strings
| } | ||
| ] | ||
| } | ||
| ] |
| columns arrive as VARCHAR, then explicitly CAST every column to its | ||
| correct DuckDB type. See ``_DUCKDB_TPCH_TABLE_SELECTS`` for the casts. | ||
| """ | ||
| import duckdb |
There was a problem hiding this comment.
Why the import is here?
There was a problem hiding this comment.
Needed in the duckdb functions only to be able to export sqlite data into the duckdb memory. If outside, will give an error when trying to import with other tests.
| sqlite_defog_connection: DatabaseContext, | ||
| ) -> None: | ||
| """ | ||
| Test executing the defog analytical questions on the sqlite database, |
There was a problem hiding this comment.
Shouldn't this be duckdb?
Linked ticket
Closes #541
Type of change
What changed and why?
Adds full DuckDB SQL support to PyDough
How I tested this?
Notes for reviewers
dbgengenerated different random-text fields (address, comment) than the standard TPC-H used everywhere else which resulted in different output in a lot of our tests.