-
Notifications
You must be signed in to change notification settings - Fork 115
Fix DuckDB identifier and struct value quoting for special characters #297
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
ca2eebb
Fix DuckDB reserved keyword quoting for column names
sfc-gh-dachristensen b2608f9
Fix backslash escaping in QuoteDuckDBFieldName for struct field names
sfc-gh-dachristensen ba5304e
Fix DuckDB struct value serialization for field names with single quotes
sfc-gh-dachristensen 750def8
Add backslash and single-quote field name coverage to struct tests
sfc-gh-dachristensen 861c091
Address PR #297 review feedback
sfc-gh-dachristensen 95c7140
Fix pgindent comment wrapping and disambiguate postgres-logs artifact
sfc-gh-dachristensen 51b5aaa
generate_duckdb_kwlist: fall back to fetching kwlist.hpp from github
sfc-gh-dachristensen 65fb677
Include from_pg_version in postgres-logs artifact name
sfc-gh-dachristensen 521f521
Use SPI parameter binding for composite-type catalog lookup
sfc-gh-dachristensen 5d0db5d
Route struct key emit in read_data.c through QuoteDuckDBStructKey
sfc-gh-dachristensen e9e27ce
Match pgindent on argValues continuation indent
sfc-gh-dachristensen 0813228
Add geometry coverage for reserved-keyword column/field names
sfc-gh-dachristensen 1773d78
Use SQL-standard quote doubling for struct keys emitted into SQL text
sfc-gh-dachristensen 1e6ef21
Add review-4270327414 coverage
sfc-gh-dachristensen 99a47f0
Cover remaining MAP-of-INTERVAL and strptime paths
sfc-gh-dachristensen 7a9aa17
Fix default location requirement and rollback guard in new tests
sfc-gh-dachristensen 4d7f43a
s3log strptime test: drop the EXPLAIN-strptime assertion
sfc-gh-dachristensen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
339 changes: 339 additions & 0 deletions
339
pg_lake_copy/tests/pytests/test_duckdb_reserved_keyword_copy.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,339 @@ | ||
| """ | ||
| Tests for correct identifier quoting during COPY TO / COPY FROM when table | ||
| columns are named after DuckDB reserved keywords. | ||
|
|
||
| DuckDB reserves several keywords (PIVOT, QUALIFY, LAMBDA, etc.) that | ||
| PostgreSQL does not. These are legal unquoted column names in PostgreSQL but | ||
| require quoting in the SQL that pg_lake generates for pgduck_server. | ||
|
|
||
| See: https://github.com/Snowflake-Labs/pg_lake/issues/277 | ||
| """ | ||
|
|
||
| import pytest | ||
| from utils_pytest import * | ||
|
|
||
| # DuckDB-only reserved keywords that are safe as unquoted PostgreSQL | ||
| # identifiers. | ||
| DUCKDB_ONLY_RESERVED = [ | ||
| "describe", | ||
| "lambda", | ||
| "pivot", | ||
| "pivot_longer", | ||
| "pivot_wider", | ||
| "qualify", | ||
| "show", | ||
| "summarize", | ||
| "unpivot", | ||
| ] | ||
|
|
||
| # Smaller set used in multi-column tests for conciseness. | ||
| TEST_KEYWORDS = ["pivot", "qualify", "lambda", "show"] | ||
|
|
||
|
|
||
| def test_copy_to_parquet_with_reserved_keyword_columns(pg_conn, s3): | ||
| """ | ||
| COPY ... TO parquet must succeed when the table has DuckDB-reserved | ||
| keyword column names, and COPY ... FROM must read the file back correctly. | ||
| """ | ||
| url = f"s3://{TEST_BUCKET}/test_kw_copy_parquet/kw_cols.parquet" | ||
|
|
||
| try: | ||
| run_command( | ||
| """ | ||
| CREATE TABLE test_kw_copy_src ( | ||
| pivot int, | ||
| qualify int, | ||
| lambda int, | ||
| show int | ||
| ); | ||
| INSERT INTO test_kw_copy_src VALUES (1, 2, 3, 4), (10, 20, 30, 40); | ||
| """, | ||
| pg_conn, | ||
| ) | ||
|
|
||
| run_command( | ||
| f"COPY test_kw_copy_src TO '{url}' WITH (format 'parquet')", | ||
| pg_conn, | ||
| ) | ||
|
|
||
| run_command( | ||
| f""" | ||
| CREATE TABLE test_kw_copy_dst ( | ||
| pivot int, | ||
| qualify int, | ||
| lambda int, | ||
| show int | ||
| ); | ||
| COPY test_kw_copy_dst FROM '{url}' WITH (format 'parquet'); | ||
| """, | ||
| pg_conn, | ||
| ) | ||
|
|
||
| result = run_query( | ||
| "SELECT pivot, qualify, lambda, show FROM test_kw_copy_dst ORDER BY pivot", | ||
| pg_conn, | ||
| ) | ||
| assert result == [[1, 2, 3, 4], [10, 20, 30, 40]] | ||
| finally: | ||
| pg_conn.rollback() | ||
|
|
||
|
|
||
| def test_copy_to_csv_with_reserved_keyword_columns(pg_conn, s3): | ||
| """ | ||
| COPY ... TO CSV and back must work with DuckDB-reserved keyword columns. | ||
| """ | ||
| url = f"s3://{TEST_BUCKET}/test_kw_copy_csv/kw_cols.csv" | ||
|
|
||
| try: | ||
| run_command( | ||
| f""" | ||
| CREATE TABLE test_kw_csv_src (pivot int, qualify int, lambda int, show int); | ||
| INSERT INTO test_kw_csv_src VALUES (3, 6, 9, 12), (30, 60, 90, 120); | ||
| COPY test_kw_csv_src TO '{url}' WITH (format 'csv', header on); | ||
|
|
||
| CREATE TABLE test_kw_csv_dst (pivot int, qualify int, lambda int, show int); | ||
| COPY test_kw_csv_dst FROM '{url}' WITH (format 'csv', header on); | ||
| """, | ||
| pg_conn, | ||
| ) | ||
|
|
||
| result = run_query( | ||
| "SELECT pivot, qualify, lambda, show FROM test_kw_csv_dst ORDER BY pivot", | ||
| pg_conn, | ||
| ) | ||
| assert result == [[3, 6, 9, 12], [30, 60, 90, 120]] | ||
| finally: | ||
| pg_conn.rollback() | ||
|
|
||
|
|
||
| def test_copy_to_json_with_reserved_keyword_columns(pg_conn, s3): | ||
| """ | ||
| COPY ... TO JSON and back must work with DuckDB-reserved keyword columns. | ||
| """ | ||
| url = f"s3://{TEST_BUCKET}/test_kw_copy_json/kw_cols.json" | ||
|
|
||
| try: | ||
| run_command( | ||
| f""" | ||
| CREATE TABLE test_kw_json_src (pivot int, qualify int, lambda int, show int); | ||
| INSERT INTO test_kw_json_src VALUES (7, 14, 21, 28), (70, 140, 210, 280); | ||
| COPY test_kw_json_src TO '{url}' WITH (format 'json'); | ||
|
|
||
| CREATE TABLE test_kw_json_dst (pivot int, qualify int, lambda int, show int); | ||
| COPY test_kw_json_dst FROM '{url}' WITH (format 'json'); | ||
| """, | ||
| pg_conn, | ||
| ) | ||
|
|
||
| result = run_query( | ||
| "SELECT pivot, qualify, lambda, show FROM test_kw_json_dst ORDER BY pivot", | ||
| pg_conn, | ||
| ) | ||
| assert result == [[7, 14, 21, 28], [70, 140, 210, 280]] | ||
| finally: | ||
| pg_conn.rollback() | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Composite type field names with embedded double-quotes | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def test_copy_roundtrip_composite_with_embedded_quote(pg_conn, s3): | ||
| """ | ||
| COPY TO/FROM must handle composite types whose field names contain | ||
| double-quote characters. The STRUCT type definition sent to DuckDB must | ||
| properly escape the embedded quotes. | ||
|
|
||
| Reproduces: https://github.com/snowflake-eng/sfpg-extension-pg_lake_replication/issues/361 | ||
| """ | ||
| url = f"s3://{TEST_BUCKET}/test_kw_copy_composite_quote/data.parquet" | ||
|
|
||
| try: | ||
| run_command( | ||
| """ | ||
| CREATE TYPE test_kw_composite_quote AS ( | ||
| U&"has\\0022quote" text, | ||
| normal int, | ||
| "has'single" int, | ||
| U&"has\\005Cback" int | ||
| ) | ||
| """, | ||
| pg_conn, | ||
| ) | ||
|
|
||
| run_command( | ||
| """ | ||
| CREATE TABLE test_kw_cq_src (id int, s test_kw_composite_quote); | ||
| INSERT INTO test_kw_cq_src VALUES (1, ROW('hello', 42, 7, 8)); | ||
| """, | ||
| pg_conn, | ||
| ) | ||
|
|
||
| run_command( | ||
| f"COPY test_kw_cq_src TO '{url}' WITH (format 'parquet')", | ||
| pg_conn, | ||
| ) | ||
|
|
||
| run_command( | ||
| f""" | ||
| CREATE TABLE test_kw_cq_dst (id int, s test_kw_composite_quote); | ||
| COPY test_kw_cq_dst FROM '{url}' WITH (format 'parquet'); | ||
| """, | ||
| pg_conn, | ||
| ) | ||
|
|
||
| result = run_query( | ||
| "SELECT id, (s).normal FROM test_kw_cq_dst", | ||
| pg_conn, | ||
| ) | ||
| assert result == [[1, 42]] | ||
|
|
||
| result = run_query( | ||
| 'SELECT (s).U&"has\\0022quote" FROM test_kw_cq_dst', | ||
| pg_conn, | ||
| ) | ||
| assert result == [["hello"]] | ||
|
|
||
| result = run_query( | ||
| """SELECT (s)."has'single", (s).U&"has\\005Cback" FROM test_kw_cq_dst""", | ||
| pg_conn, | ||
| ) | ||
| assert result == [[7, 8]] | ||
| finally: | ||
| pg_conn.rollback() | ||
|
|
||
|
|
||
| def test_copy_roundtrip_csv_composite_with_embedded_quote(pg_conn, s3): | ||
| """ | ||
| COPY TO/FROM CSV with composite types containing double-quote field names. | ||
| This exercises the read_csv columns= type string path. | ||
|
|
||
| Reproduces: https://github.com/snowflake-eng/sfpg-extension-pg_lake_replication/issues/361 | ||
| """ | ||
| url = f"s3://{TEST_BUCKET}/test_kw_copy_csv_composite_quote/data.csv" | ||
|
|
||
| try: | ||
| run_command( | ||
| """ | ||
| CREATE TYPE test_kw_csv_cq AS ( | ||
| U&"has\\0022quote" text, | ||
| normal int, | ||
| "has'single" int, | ||
| U&"has\\005Cback" int | ||
| ) | ||
| """, | ||
| pg_conn, | ||
| ) | ||
|
|
||
| run_command( | ||
| """ | ||
| CREATE TABLE test_kw_csv_cq_src (id int, s test_kw_csv_cq); | ||
| INSERT INTO test_kw_csv_cq_src VALUES (1, ROW('world', 99, 11, 12)); | ||
| """, | ||
| pg_conn, | ||
| ) | ||
|
|
||
| run_command( | ||
| f"COPY test_kw_csv_cq_src TO '{url}' WITH (format 'csv', header on)", | ||
| pg_conn, | ||
| ) | ||
|
|
||
| run_command( | ||
| f""" | ||
| CREATE TABLE test_kw_csv_cq_dst (id int, s test_kw_csv_cq); | ||
| COPY test_kw_csv_cq_dst FROM '{url}' WITH (format 'csv', header on); | ||
| """, | ||
| pg_conn, | ||
| ) | ||
|
|
||
| result = run_query( | ||
| "SELECT id, (s).normal FROM test_kw_csv_cq_dst", | ||
| pg_conn, | ||
| ) | ||
| assert result == [[1, 99]] | ||
|
|
||
| result = run_query( | ||
| """SELECT (s)."has'single", (s).U&"has\\005Cback" FROM test_kw_csv_cq_dst""", | ||
| pg_conn, | ||
| ) | ||
| assert result == [[11, 12]] | ||
| finally: | ||
| pg_conn.rollback() | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Parameterised round-trip tests — each keyword, each format | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("keyword", DUCKDB_ONLY_RESERVED) | ||
| def test_copy_roundtrip_parquet(pg_conn, s3, keyword): | ||
| """Each DuckDB-only reserved keyword survives a parquet round-trip.""" | ||
| url = f"s3://{TEST_BUCKET}/test_kw_roundtrip_parquet/{keyword}/data.parquet" | ||
|
|
||
| try: | ||
| run_command( | ||
| f""" | ||
| CREATE TABLE test_kw_pq_src_{keyword} ({keyword} int); | ||
| INSERT INTO test_kw_pq_src_{keyword} VALUES (42); | ||
| COPY test_kw_pq_src_{keyword} TO '{url}' WITH (format 'parquet'); | ||
|
|
||
| CREATE TABLE test_kw_pq_dst_{keyword} ({keyword} int); | ||
| COPY test_kw_pq_dst_{keyword} FROM '{url}' WITH (format 'parquet'); | ||
| """, | ||
| pg_conn, | ||
| ) | ||
|
|
||
| result = run_query(f"SELECT {keyword} FROM test_kw_pq_dst_{keyword}", pg_conn) | ||
| assert result == [[42]] | ||
| finally: | ||
| pg_conn.rollback() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("keyword", DUCKDB_ONLY_RESERVED) | ||
| def test_copy_roundtrip_csv(pg_conn, s3, keyword): | ||
| """Each DuckDB-only reserved keyword survives a CSV round-trip.""" | ||
| url = f"s3://{TEST_BUCKET}/test_kw_roundtrip_csv/{keyword}/data.csv" | ||
|
|
||
| try: | ||
| run_command( | ||
| f""" | ||
| CREATE TABLE test_kw_csv_src_{keyword} ({keyword} int); | ||
| INSERT INTO test_kw_csv_src_{keyword} VALUES (42); | ||
| COPY test_kw_csv_src_{keyword} TO '{url}' WITH (format 'csv', header on); | ||
|
|
||
| CREATE TABLE test_kw_csv_dst_{keyword} ({keyword} int); | ||
| COPY test_kw_csv_dst_{keyword} FROM '{url}' WITH (format 'csv', header on); | ||
| """, | ||
| pg_conn, | ||
| ) | ||
|
|
||
| result = run_query(f"SELECT {keyword} FROM test_kw_csv_dst_{keyword}", pg_conn) | ||
| assert result == [[42]] | ||
| finally: | ||
| pg_conn.rollback() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("keyword", DUCKDB_ONLY_RESERVED) | ||
| def test_copy_roundtrip_json(pg_conn, s3, keyword): | ||
| """Each DuckDB-only reserved keyword survives a JSON round-trip.""" | ||
| url = f"s3://{TEST_BUCKET}/test_kw_roundtrip_json/{keyword}/data.json" | ||
|
|
||
| try: | ||
| run_command( | ||
| f""" | ||
| CREATE TABLE test_kw_json_src_{keyword} ({keyword} int); | ||
| INSERT INTO test_kw_json_src_{keyword} VALUES (42); | ||
| COPY test_kw_json_src_{keyword} TO '{url}' WITH (format 'json'); | ||
|
|
||
| CREATE TABLE test_kw_json_dst_{keyword} ({keyword} int); | ||
| COPY test_kw_json_dst_{keyword} FROM '{url}' WITH (format 'json'); | ||
| """, | ||
| pg_conn, | ||
| ) | ||
|
|
||
| result = run_query(f"SELECT {keyword} FROM test_kw_json_dst_{keyword}", pg_conn) | ||
| assert result == [[42]] | ||
| finally: | ||
| pg_conn.rollback() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.