@@ -265,18 +265,21 @@ def join(
265265 if self ._backend_version < (1 , 1 , 4 ):
266266 msg = f"DuckDB>=1.1.4 is required for cross-join, found version: { self ._backend_version } "
267267 raise NotImplementedError (msg )
268- rel = self .native .set_alias ("lhs" ).cross (
269- other .native .set_alias ("rhs" )
270- ) # pragma: no cover
268+ rel = self .native .set_alias ("lhs" ).cross (other .native .set_alias ("rhs" ))
271269 else :
272270 # help mypy
273271 assert left_on is not None # noqa: S101
274272 assert right_on is not None # noqa: S101
275- condition = " and " .join (
276- f'lhs."{ left } " = rhs."{ right } "' for left , right in zip (left_on , right_on )
273+ it = (
274+ col (f'lhs."{ left } "' ) == col (f'rhs."{ right } "' )
275+ for left , right in zip (left_on , right_on )
277276 )
277+ condition : duckdb .Expression = reduce (and_ , it )
278278 rel = self .native .set_alias ("lhs" ).join (
279- other .native .set_alias ("rhs" ), condition = condition , how = native_how
279+ other .native .set_alias ("rhs" ),
280+ # NOTE: Fixed in `--pre` https://github.com/duckdb/duckdb/pull/16933
281+ condition = condition , # type: ignore[arg-type, unused-ignore]
282+ how = native_how ,
280283 )
281284
282285 if native_how in {"inner" , "left" , "cross" , "outer" }:
@@ -310,21 +313,22 @@ def join_asof(
310313 ) -> Self :
311314 lhs = self .native
312315 rhs = other .native
313- conditions = []
316+ conditions : list [ duckdb . Expression ] = []
314317 if by_left is not None and by_right is not None :
315- conditions += [
316- f'lhs."{ left } " = rhs."{ right } "' for left , right in zip (by_left , by_right )
317- ]
318+ conditions .extend (
319+ col (f'lhs."{ left } "' ) == col (f'rhs."{ right } "' )
320+ for left , right in zip (by_left , by_right )
321+ )
318322 else :
319323 by_left = by_right = []
320324 if strategy == "backward" :
321- conditions += [ f'lhs."{ left_on } " >= rhs."{ right_on } "' ]
325+ conditions . append ( col ( f'lhs."{ left_on } "' ) >= col ( f' rhs."{ right_on } "'))
322326 elif strategy == "forward" :
323- conditions += [ f'lhs."{ left_on } " <= rhs."{ right_on } "' ]
327+ conditions . append ( col ( f'lhs."{ left_on } "' ) <= col ( f' rhs."{ right_on } "'))
324328 else :
325329 msg = "Only 'backward' and 'forward' strategies are currently supported for DuckDB"
326330 raise NotImplementedError (msg )
327- condition = " and " . join ( conditions )
331+ condition : duckdb . Expression = reduce ( and_ , conditions )
328332 select = ["lhs.*" ]
329333 for name in rhs .columns :
330334 if name in lhs .columns and (
@@ -333,6 +337,8 @@ def join_asof(
333337 select .append (f'rhs."{ name } " as "{ name } { suffix } "' )
334338 elif right_on is None or name not in {right_on , * by_right }:
335339 select .append (f'"{ name } "' )
340+ # Replace with Python API call once
341+ # https://github.com/duckdb/duckdb/discussions/16947 is addressed.
336342 query = f"""
337343 SELECT { "," .join (select )}
338344 FROM lhs
@@ -350,8 +356,7 @@ def collect_schema(self: Self) -> dict[str, DType]:
350356 def unique (
351357 self : Self , subset : Sequence [str ] | None , * , keep : Literal ["any" , "none" ]
352358 ) -> Self :
353- subset_ = subset if keep == "any" else (subset or self .columns )
354- if subset_ :
359+ if subset_ := subset if keep == "any" else (subset or self .columns ):
355360 # Sanitise input
356361 if any (x not in self .columns for x in subset_ ):
357362 msg = f"Columns { set (subset_ ).difference (self .columns )} not found in { self .columns } ."
@@ -366,13 +371,10 @@ def unique(
366371 count(*) over ({ partition_by_sql } ) as "{ count_name } "
367372 from rel
368373 """ # noqa: S608
369- if keep == "none" :
370- keep_condition = col (count_name ) == lit (1 )
371- else :
372- keep_condition = col (idx_name ) == lit (1 )
374+ name = count_name if keep == "none" else idx_name
373375 return self ._with_native (
374376 duckdb .sql (query )
375- .filter (keep_condition )
377+ .filter (col ( name ) == lit ( 1 ) )
376378 .select (StarExpression (exclude = [count_name , idx_name ]))
377379 )
378380 return self ._with_native (self .native .unique (", " .join (self .columns )))
@@ -463,6 +465,8 @@ def unpivot(
463465
464466 unpivot_on = ", " .join (f'"{ name } "' for name in on_ )
465467 rel = self .native # noqa: F841
468+ # Replace with Python API once
469+ # https://github.com/duckdb/duckdb/discussions/16980 is addressed.
466470 query = f"""
467471 unpivot rel
468472 on { unpivot_on }
0 commit comments