Skip to content

Commit a6bc2c5

Browse files
committed
Reapply "fix the kokoro issue with unreliable row count"
This reverts commit b5c8f52.
1 parent 1a9a5a8 commit a6bc2c5

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

bigframes/core/rewrite/slices.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ def rewrite_slice(node: nodes.BigFrameNode):
7676
return node
7777

7878
slice_def = (node.start, node.stop, node.step)
79+
80+
# Handle empty slice cases explicitly before any row count dependent logic
81+
# This ensures empty slices always return empty results regardless of statistics
82+
if _is_empty_slice(node.start, node.stop, node.step):
83+
# Create a filter that will always return empty results
84+
# Use start=0, stop=0, step=1 to ensure empty result
85+
return slice_as_filter(node.child, 0, 0, 1)
86+
7987
# no-op (eg. df[::1])
8088
if slices.is_noop(slice_def, node.child.row_count):
8189
return node.child
@@ -89,6 +97,26 @@ def rewrite_slice(node: nodes.BigFrameNode):
8997
return slice_as_filter(node.child, *slice_def)
9098

9199

100+
def _is_empty_slice(start, stop, step):
101+
"""Check if a slice will always return empty results."""
102+
if start is None or stop is None:
103+
return False
104+
105+
# Normalize step
106+
if step is None:
107+
step = 1
108+
109+
# For positive step, empty if start >= stop
110+
# For negative step, empty if start <= stop
111+
if step > 0:
112+
return start >= stop
113+
elif step < 0:
114+
return start <= stop
115+
else:
116+
# step == 0 is invalid, but handle gracefully
117+
return True
118+
119+
92120
def slice_as_filter(
93121
node: nodes.BigFrameNode, start: Optional[int], stop: Optional[int], step: int
94122
) -> nodes.BigFrameNode:

0 commit comments

Comments
 (0)