From b91e3dca7fe0131037b93166572f9bf242b3e96e Mon Sep 17 00:00:00 2001 From: Nick Crews Date: Thu, 31 Oct 2024 16:40:56 -0800 Subject: [PATCH] perf(duckdb): use builtin negative array slicing in newer duckdbs Similar rationale to https://github.com/ibis-project/ibis/pull/10410 --- ibis/backends/sql/compilers/duckdb.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/ibis/backends/sql/compilers/duckdb.py b/ibis/backends/sql/compilers/duckdb.py index 5002344f4579..bdfd1376aa5a 100644 --- a/ibis/backends/sql/compilers/duckdb.py +++ b/ibis/backends/sql/compilers/duckdb.py @@ -182,16 +182,22 @@ def visit_ArraySlice(self, op, *, arg, start, stop): arg_length = self.f.len(arg) if start is None: - start = 0 + start = 1 + elif isinstance(op.start, ops.Literal): + start = op.start.value + start += start >= 0 else: - start = self.f.least(arg_length, self._neg_idx_to_pos(arg, start)) + start = self.if_(start < 0, start, start + 1) if stop is None: stop = arg_length + elif isinstance(op.stop, ops.Literal): + stop = int(op.stop.value) + stop -= stop < 0 else: - stop = self._neg_idx_to_pos(arg, stop) + stop = self.if_(stop < 0, stop - 1, stop) - return self.f.list_slice(arg, start + 1, stop) + return self.f.list_slice(arg, start, stop) def visit_ArrayMap(self, op, *, arg, body, param, index): expressions = [param]