Skip to content

Commit

Permalink
feat(duckdb): support registering external postgres tables with duckdb
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrist committed Nov 28, 2022
1 parent dff4cbb commit 8633e6b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
12 changes: 12 additions & 0 deletions ibis/backends/duckdb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ def _s3(full_path, table_name=None):
return table_name, dataset, []


@_generate_view_code.register(r"postgres(ql)?://.+", priority=10)
def _postgres(uri, table_name=None):
if table_name is None:
raise ValueError("`table_name` is required when registering a postgres table")
quoted_table_name = _quote(table_name)
sql = (
f"CREATE OR REPLACE VIEW {quoted_table_name} AS "
f"SELECT * FROM postgres_scan_pushdown('{uri}', 'public', '{table_name}')"
)
return sql, table_name, ["postgres_scanner"]


@_generate_view_code.register(r".+", priority=1)
def _default(path, **kwargs):
raise ValueError(
Expand Down
15 changes: 15 additions & 0 deletions ibis/backends/duckdb/tests/test_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,18 @@ def test_temp_directory(tmp_path):
def test_s3_parquet(path):
with pytest.raises(OSError):
_generate_view_code(path)


@pytest.mark.parametrize("scheme", ["postgres", "postgresql"])
@pytest.mark.parametrize(
"name, quoted_name", [("test", "test"), ("my table", '"my table"')]
)
def test_postgres(scheme, name, quoted_name):
uri = f"{scheme}://username:password@localhost:5432"
sql, table_name, exts = _generate_view_code(uri, name)
assert sql == (
f"CREATE OR REPLACE VIEW {quoted_name} AS "
f"SELECT * FROM postgres_scan_pushdown('{uri}', 'public', '{name}')"
)
assert table_name == name
assert exts == ["postgres_scanner"]

0 comments on commit 8633e6b

Please sign in to comment.