Skip to content

Commit 03c3f7a

Browse files
committed
fix: use three-part names for MotherDuck queries
MotherDuck requires database.schema.table format for queries. Override build_select_query to include the database prefix.
1 parent a4047e5 commit 03c3f7a

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

sqlit/domains/connections/providers/motherduck/adapter.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,15 @@ def connect(self, config: ConnectionConfig) -> Any:
5050

5151
duckdb_any: Any = duckdb
5252
return duckdb_any.connect(conn_str)
53+
54+
def build_select_query(
55+
self, table: str, limit: int, database: str | None = None, schema: str | None = None
56+
) -> str:
57+
"""Build SELECT LIMIT query for MotherDuck.
58+
59+
MotherDuck requires three-part names: database.schema.table
60+
"""
61+
schema = schema or "main"
62+
if database:
63+
return f'SELECT * FROM "{database}"."{schema}"."{table}" LIMIT {limit}'
64+
return f'SELECT * FROM "{schema}"."{table}" LIMIT {limit}'

tests/unit/test_motherduck_adapter.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,36 @@ def test_motherduck_schema_uses_password_field():
4242
# Password field should be labeled as "Access Token"
4343
password_field = next(f for f in SCHEMA.fields if f.name == "password")
4444
assert password_field.label == "Access Token"
45+
46+
47+
def test_motherduck_build_select_query_with_database():
48+
"""Test MotherDuck uses three-part names (database.schema.table)."""
49+
from sqlit.domains.connections.providers.motherduck.adapter import MotherDuckAdapter
50+
51+
adapter = MotherDuckAdapter()
52+
53+
# With database - should use three-part name
54+
query = adapter.build_select_query("hacker_news", 100, database="sample_data", schema="hn")
55+
assert query == 'SELECT * FROM "sample_data"."hn"."hacker_news" LIMIT 100'
56+
57+
58+
def test_motherduck_build_select_query_without_database():
59+
"""Test MotherDuck falls back to two-part names without database."""
60+
from sqlit.domains.connections.providers.motherduck.adapter import MotherDuckAdapter
61+
62+
adapter = MotherDuckAdapter()
63+
64+
# Without database - should use two-part name
65+
query = adapter.build_select_query("my_table", 50, schema="main")
66+
assert query == 'SELECT * FROM "main"."my_table" LIMIT 50'
67+
68+
69+
def test_motherduck_build_select_query_default_schema():
70+
"""Test MotherDuck defaults to 'main' schema."""
71+
from sqlit.domains.connections.providers.motherduck.adapter import MotherDuckAdapter
72+
73+
adapter = MotherDuckAdapter()
74+
75+
# No schema specified - should default to main
76+
query = adapter.build_select_query("my_table", 25, database="my_db")
77+
assert query == 'SELECT * FROM "my_db"."main"."my_table" LIMIT 25'

0 commit comments

Comments
 (0)