Repository navigation
fix(duckdb): case-insensitive .duckdb discovery, deterministic attach aliases, and single-statement SQL hardening - #2411
Conversation
…bases
_list_duckdb_files filtered candidate files with
file.path.endswith('.duckdb'), a case-sensitive check. A DuckDB database
file with an upper- or mixed-case extension (e.g. WAREHOUSE.DUCKDB, a
common shape for exported files) was silently skipped, leaving the
connector to raise DUCKDB_FILE_NOT_FOUND even though a valid database was
present in the directory.
Lower-case the path before the suffix comparison so extension matching is
case-insensitive, mirroring how filesystems treat these files.
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
Walkthrough
ChangesDuckDB connector discovery and attachment
Estimated code review effort🎯 2 (Simple) | ⏱️ ~15 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
core/wren/src/wren/connector/duckdb.py (1)
123-129: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick winHandle alias collisions introduced by case-insensitive discovery.
With this change,
warehouse.duckdbandwarehouse.DUCKDBare both returned here, but Line 102 derives the same attach alias (warehouse) for both. On a case-sensitive filesystem or object store, the second attach will now fail even though both files are valid.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@core/wren/src/wren/connector/duckdb.py` around lines 123 - 129, The DuckDB discovery logic in the file listing loop now returns case-insensitive duplicates that can map to the same attach alias in the alias derivation path, so update the attach flow in duckdb.py to detect and resolve collisions before attaching. Use the existing discovery/attach code around the URL collection and the alias creation logic to ensure each discovered .duckdb file gets a unique alias (or skip duplicates deterministically) when files like warehouse.duckdb and warehouse.DUCKDB both appear.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@core/wren/src/wren/connector/duckdb.py`:
- Around line 81-83: The LIMIT wrapping in DuckDB is currently applied directly
to the raw SQL string, so semicolon-terminated queries become invalid when
wrapped. Update the DuckDB connector logic in the execute/query path to strip
any trailing semicolons from the SQL before constructing the SELECT * FROM (...)
AS _q LIMIT wrapper, and keep the limit behavior unchanged for non-semicolon
inputs.
- Around line 85-87: The dry_run() method in DuckDBConnector currently passes
raw SQL into EXPLAIN, which still allows semicolon-separated batches to execute
beyond the first statement. Update dry_run() to detect and reject
multi-statement input before calling self.connection.execute(), keeping the
no-side-effects contract intact. Use the dry_run() method as the fix point and
ensure only a single statement is accepted before EXPLAIN is run.
---
Outside diff comments:
In `@core/wren/src/wren/connector/duckdb.py`:
- Around line 123-129: The DuckDB discovery logic in the file listing loop now
returns case-insensitive duplicates that can map to the same attach alias in the
alias derivation path, so update the attach flow in duckdb.py to detect and
resolve collisions before attaching. Use the existing discovery/attach code
around the URL collection and the alias creation logic to ensure each discovered
.duckdb file gets a unique alias (or skip duplicates deterministically) when
files like warehouse.duckdb and warehouse.DUCKDB both appear.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 0dc86538-6433-4dbf-a374-0354f3310ad4
📒 Files selected for processing (1)
core/wren/src/wren/connector/duckdb.py
Case-insensitive discovery can surface files whose basenames differ only by case (warehouse.duckdb / warehouse.DUCKDB). Both derived the same attach alias, so the second ATTACH would fail on case-sensitive filesystems/object stores. Derive a unique alias per file deterministically. Adds a regression test asserting distinct aliases (warehouse, warehouse_1).
|
Thanks @coderabbitai — good catch on the alias collision. That edge case is real: case-insensitive discovery can now surface Pushed a fix: On the two inline LIMIT/ |
|
Tip For best results, initiate chat on the files or code changes.
(•ᴗ•)🐇 ✏️ Learnings added
If you found this review helpful, would you consider giving us a shout-out on X? Thank you for using CodeRabbit! |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@core/wren/src/wren/connector/duckdb.py`:
- Around line 99-114: Alias assignment in the DuckDB connector is still
nondeterministic because db_files is processed in whatever order OpenDAL
returns. Sort db_files before the loop in the alias-deriving logic inside the
DuckDB attach path so the same file consistently gets the bare alias across
runs, while preserving the existing unique_alias/used_aliases collision
handling.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 94b10aae-f250-49e8-9843-7e2f2a430d69
📒 Files selected for processing (2)
core/wren/src/wren/connector/duckdb.pycore/wren/tests/unit/test_duckdb_file_listing.py
|
Thanks @coderabbitai — addressed the nondeterministic alias ordering. |
|
Tip For best results, initiate chat on the files or code changes.
🐇✨ ✏️ Learnings added
|
…ed order Files are sorted before alias assignment, so 'data' attaches first and the two case-colliding 'warehouse' files get warehouse/warehouse_1.
…i-statement dry_run Addresses CodeRabbit findings: - query(): a semicolon-terminated SQL string produced invalid wrapped SQL (SELECT * FROM (SELECT 1;) AS _q LIMIT n). Strip trailing ; before wrapping. - dry_run(): duckdb.execute runs semicolon-separated batches, so EXPLAIN could still execute trailing statements. Reject multi-statement input. Adds regression tests for both.
|
Addressed in cb3bcb9:
Added 3 regression tests; full |
ReviewThe core fix is correct and well-tested, but the PR bundles more than the title/description says, and the 🔴 1. Title & description don't match the diffThe body says:
But the diff actually changes four things:
The 🔴 2.
|
…ejecting multi-statement - query() and dry_run() now use a shared _strip_trailing_semicolon regex helper (matching canner/clickhouse/trino) instead of the weaker inline rstrip, which mishandled interleaved separators like 'SELECT 1 ; ; '. - dry_run() no longer pre-rejects multi-statement input (false-positived on semicolons inside string literals, e.g. SELECT ';' AS x). It now wraps in a LIMIT 0 subquery like the other connectors, so trailing statements become a natural syntax error with no side effects. - Updated/added regression tests: string-literal semicolon preserved, LIMIT 0 wrap for dry_run.
|
Thanks for the thorough review @goldmedal — all three addressed in 89bb6cb. 1. Title & description scope — Updated both to reflect the real diff: case-insensitive discovery + deterministic alias dedup + 2. 3. Shared helper (🟡) — Full suite green: |
Summary
Harden the DuckDB connector around database-file discovery and single-statement SQL handling:
.duckdbdiscovery (the original fix) —_list_duckdb_filesmatches the extension case-insensitively, so valid databases with upper/mixed-case extensions (e.g.WAREHOUSE.DUCKDB) are no longer silently skipped and no longer surface a spuriousDUCKDB_FILE_NOT_FOUND.warehouse.duckdbandwarehouse.DUCKDB, which previously derived the same attach alias._attach_databasesorts the file list and derives unique aliases deterministically (warehouse,warehouse_1, …), tracked case-insensitively.query()/dry_run()semicolon handling — both now use the shared_strip_trailing_semicolonregex helper (matchingcanner/clickhouse/trino) so a trailing;doesn't produce invalid wrapped SQL, while semicolons inside string literals are preserved.dry_run()neutralizes multi-statement input by wrapping in aLIMIT 0subquery (same as the other connectors) rather than pre-rejecting it — no side effects, no false positives.Motivation
str.endswith(".duckdb")is case-sensitive, dropping mixed-case database files on case-preserving filesystems/object stores and raisingDUCKDB_FILE_NOT_FOUNDeven when a valid database is present. The follow-on changes address edge cases that fix newly exposes (alias collisions) and align the LIMIT-wrap / dry-run semicolon handling with the rest of the codebase.Fix
_list_duckdb_files:file.path.lower().endswith(".duckdb")._attach_database:sorted()+ deterministic, case-insensitively tracked unique aliases.query()/dry_run(): reuse_strip_trailing_semicolon;dry_runwraps inLIMIT 0subquery.Verification
uv run --no-sync pytest tests/unit/test_duckdb_file_listing.py tests/unit/test_engine.py -q— 15 passed.ruff checkclean.Regression tests cover: mixed-case discovery, case-colliding alias uniquification,
query()trailing-semicolon strip,dry_run()LIMIT-0 wrap, anddry_run()preserving a semicolon inside a string literal.Scope / risk
Changes confined to
core/wren/src/wren/connector/duckdb.py(Apache-2.0 path) plus its unit tests.