Repository: mabel-dev/opteryx

Issue:
🪲 joins aren't pushing predicates when predicates are on each relation in the join
**Sample Code/Statement** _If you can, please submit the SQL statement or Python code snippet, or a representative example using the sample datasets._

~~~sql
SELECT * 
  FROM $planets AS p 
 INNER JOIN $satellites AS s 
    ON p.id = s.planet_id 
 WHERE p.name = 'Jupiter' 
   AND s.radius = 1.0
~~~

Returns the wrong result


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__ = 329
+__build__ = 330
 
 # 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/components/cost_based_optimizer/strategies/split_conjunctive_predicates.py b/opteryx/components/cost_based_optimizer/strategies/split_conjunctive_predicates.py
--- a/opteryx/components/cost_based_optimizer/strategies/split_conjunctive_predicates.py
+++ b/opteryx/components/cost_based_optimizer/strategies/split_conjunctive_predicates.py
@@ -56,13 +56,12 @@ def visit(self, node: LogicalPlanNode, context: OptimizerContext) -> OptimizerCo
             split_predicates = _inner_split(node.condition)
             new_nodes = []
             for predicate in split_predicates:
-                new_node = LogicalPlanNode(
-                    node_type=LogicalPlanStepType.Filter, condition=predicate
-                )
+                new_node = LogicalPlanNode(node_type=LogicalPlanStepType.Filter)
+                new_node.condition = predicate
                 new_node.columns = get_all_nodes_of_type(
-                    node.condition, select_nodes=(NodeType.IDENTIFIER,)
+                    predicate, select_nodes=(NodeType.IDENTIFIER,)
                 )
-                new_node.relations = node.relations
+                new_node.relations = {c.source for c in new_node.columns}
                 new_nodes.append(new_node)
         else:
             new_nodes = [node]


Test patch:
diff --git a/tests/sql_battery/test_shapes_and_errors_battery.py b/tests/sql_battery/test_shapes_and_errors_battery.py
--- a/tests/sql_battery/test_shapes_and_errors_battery.py
+++ b/tests/sql_battery/test_shapes_and_errors_battery.py
@@ -1488,6 +1488,13 @@
         # 1474
         ("SELECT *, id FROM $planets;", None, None, SqlError),
         ("SELECT id, * FROM $planets;", None, None, SqlError),
+        # 1487
+        ("SELECT * FROM $planets AS p INNER JOIN $satellites AS s ON p.id = s.planet_id WHERE p.name = 'Jupiter' AND s.radius = 1.0", 25, 28, None),
+        ("SELECT * FROM $planets AS p INNER JOIN $satellites AS s ON p.id = s.planet_id WHERE p.name = 'Jupiter'", 67, 28, None),
+        ("SELECT * FROM $planets AS p INNER JOIN $planets AS s ON p.id = s.id WHERE p.name = 'Jupiter' AND p.id = 1.0", 0, 40, None),
+        ("SELECT * FROM sqlite.planets AS p INNER JOIN sqlite.planets AS s ON p.id = s.id WHERE p.name RLIKE 'Jupiter' AND s.id = 1.0", 0, 40, None),
+        ("SELECT * FROM sqlite.planets AS p INNER JOIN sqlite.planets AS s ON p.id = s.id WHERE p.name RLIKE 'Jupiter' AND s.name RLIKE 'Jupiter'", 1, 40, None),
+
 ]
 # fmt:on