Skip to content

Commit 2ee5296

Browse files
authored
enh: support diagonal concatenation for duckdb (#3108)
* enh: support diagonal concatenation for duckdb * link to gh
1 parent 538973e commit 2ee5296

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

narwhals/_duckdb/namespace.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from itertools import chain
66
from typing import TYPE_CHECKING, Any
77

8+
import duckdb
89
from duckdb import CoalesceOperator, Expression
910
from duckdb.typing import BIGINT, VARCHAR
1011

@@ -86,6 +87,14 @@ def concat(
8687
if how == "vertical" and not all(x.schema == schema for x in items[1:]):
8788
msg = "inputs should all have the same schema"
8889
raise TypeError(msg)
90+
if how == "diagonal":
91+
res = first.native
92+
for _item in native_items[1:]:
93+
# TODO(unassigned): use relational API when available https://github.com/duckdb/duckdb/discussions/16996
94+
res = duckdb.sql("""
95+
from res select * union all by name from _item select *
96+
""")
97+
return first._with_native(res)
8998
res = reduce(lambda x, y: x.union(y), native_items)
9099
return first._with_native(res)
91100

tests/frame/concat_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def test_concat_vertical(constructor: Constructor) -> None:
6464
def test_concat_diagonal(
6565
constructor: Constructor, request: pytest.FixtureRequest
6666
) -> None:
67-
if "duckdb" in str(constructor) or "ibis" in str(constructor):
67+
if "ibis" in str(constructor):
6868
request.applymarker(pytest.mark.xfail)
6969
data_1 = {"a": [1, 3], "b": [4, 6]}
7070
data_2 = {"a": [100, 200], "z": ["x", "y"]}

0 commit comments

Comments
 (0)