Repository: mabel-dev/opteryx

Issue:
🪲 `CROSS JOIN` statistics incorrect
### Thank you for taking the time to report a problem with Opteryx.
_To help us to respond to your request we ask that you try to provide the below detail about the bug._

**Describe the bug** _A clear and specific description of what the bug is. What the error, incorrect or unexpected behaviour was._


**Expected behaviour** _A clear and concise description of what you expected to happen._


**Sample Code/Statement** _If you can, please submit the SQL statement or Python code snippet, or a representative example using the sample datasets._

~~~sql

~~~

**Additional context** _Add any other context about the problem here, for example what you have done to try to diagnose or workaround the problem._



Discussion:

Resolving patch:
diff --git a/opteryx/__version__.py b/opteryx/__version__.py
--- a/opteryx/__version__.py
+++ b/opteryx/__version__.py
@@ -1,4 +1,4 @@
-__build__ = 376
+__build__ = 380
 
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
diff --git a/opteryx/cursor.py b/opteryx/cursor.py
--- a/opteryx/cursor.py
+++ b/opteryx/cursor.py
@@ -261,7 +261,7 @@ def execute(self, operation: str, params: Optional[Iterable] = None):
                 import orso
 
                 meta_dataframe = orso.DataFrame(
-                    rows=[(result_data.record_count,)],
+                    rows=[(result_data.record_count,)],  # type: ignore
                     schema=RelationSchema(
                         name="table",
                         columns=[FlatColumn(name="rows_affected", type=OrsoTypes.INTEGER)],
diff --git a/opteryx/operators/cross_join_node.py b/opteryx/operators/cross_join_node.py
--- a/opteryx/operators/cross_join_node.py
+++ b/opteryx/operators/cross_join_node.py
@@ -36,7 +36,7 @@
 
 
 def _cross_join_unnest_column(
-    morsels: BasePlanNode, source: Node, target_column: FlatColumn
+    morsels: BasePlanNode, source: Node, target_column: FlatColumn, statistics
 ) -> Generator[pyarrow.Table, None, None]:
     """
     Perform a cross join on an unnested column of pyarrow tables.
@@ -60,6 +60,7 @@ def _cross_join_unnest_column(
 
     # Loop through each morsel from the morsels execution
     for left_morsel in morsels.execute():
+        start = time.monotonic_ns()
         # Break the morsel into batches to avoid memory issues
         for left_block in left_morsel.to_batches(max_chunksize=batch_size):
             # Fetch the data of the column to be unnested
@@ -85,7 +86,10 @@ def _cross_join_unnest_column(
             new_block = left_block.take(indices)
             new_block = pyarrow.Table.from_batches([new_block], schema=left_morsel.schema)
             new_block = new_block.append_column(target_column.identity, [new_column_data])
+
+            statistics.time_cross_join_unnest += time.monotonic_ns() - start
             yield new_block
+            start = time.monotonic_ns()
 
 
 def _cross_join_unnest_literal(
@@ -224,8 +228,9 @@ def execute(self) -> Generator:
         else:
             start = time.monotonic_ns()
             for morsel in _cross_join_unnest_column(
-                morsels=left_node, source=self._unnest_column, target_column=self._unnest_target
+                morsels=left_node,
+                source=self._unnest_column,
+                target_column=self._unnest_target,
+                statistics=self.statistics,
             ):
-                self.statistics.time_cross_join_unnest += time.monotonic_ns() - start
                 yield morsel
-                start = time.monotonic_ns()


Test patch: