Skip to content

Commit

Permalink
Fix: overly aggressive cross join removal
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed Jun 15, 2023
1 parent d696d7f commit f957a07
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
3 changes: 3 additions & 0 deletions sqlglot/optimizer/optimize_joins.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ def optimize_joins(expression):
on = dep.args["on"]

if isinstance(on, exp.Connector):
if len(other_table_names(dep)) < 2:
continue

for predicate in on.flatten():
if name in exp.column_table_names(predicate):
predicate.replace(exp.true())
Expand Down
8 changes: 4 additions & 4 deletions tests/fixtures/optimizer/tpc-ds/tpc-ds.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12325,7 +12325,10 @@ SELECT
"call_center"."cc_manager" AS "manager",
SUM("catalog_returns"."cr_net_loss") AS "returns_loss"
FROM "call_center" AS "call_center"
CROSS JOIN "customer" AS "customer"
JOIN "household_demographics" AS "household_demographics"
ON "household_demographics"."hd_buy_potential" LIKE 'Unknown%'
JOIN "customer" AS "customer"
ON "household_demographics"."hd_demo_sk" = "customer"."c_current_hdemo_sk"
JOIN "catalog_returns" AS "catalog_returns"
ON "catalog_returns"."cr_call_center_sk" = "call_center"."cc_call_center_sk"
AND "catalog_returns"."cr_returning_customer_sk" = "customer"."c_customer_sk"
Expand All @@ -12350,9 +12353,6 @@ JOIN "customer_demographics" AS "customer_demographics"
"customer_demographics"."cd_marital_status" = 'M'
OR "customer_demographics"."cd_marital_status" = 'W'
)
JOIN "household_demographics" AS "household_demographics"
ON "household_demographics"."hd_buy_potential" LIKE 'Unknown%'
AND "household_demographics"."hd_demo_sk" = "customer"."c_current_hdemo_sk"
JOIN "date_dim" AS "date_dim"
ON "catalog_returns"."cr_returned_date_sk" = "date_dim"."d_date_sk"
AND "date_dim"."d_moy" = 12
Expand Down
26 changes: 13 additions & 13 deletions tests/fixtures/optimizer/tpc-h/tpc-h.sql
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,19 @@ order by
p_partkey
limit
100;
WITH "partsupp_2" AS (
SELECT
"partsupp"."ps_partkey" AS "ps_partkey",
"partsupp"."ps_suppkey" AS "ps_suppkey",
"partsupp"."ps_supplycost" AS "ps_supplycost"
FROM "partsupp" AS "partsupp"
), "region_2" AS (
WITH "region_2" AS (
SELECT
"region"."r_regionkey" AS "r_regionkey",
"region"."r_name" AS "r_name"
FROM "region" AS "region"
WHERE
"region"."r_name" = 'EUROPE'
), "partsupp_2" AS (
SELECT
"partsupp"."ps_partkey" AS "ps_partkey",
"partsupp"."ps_suppkey" AS "ps_suppkey",
"partsupp"."ps_supplycost" AS "ps_supplycost"
FROM "partsupp" AS "partsupp"
), "_u_0" AS (
SELECT
MIN("partsupp"."ps_supplycost") AS "_col_0",
Expand All @@ -136,13 +136,13 @@ SELECT
"supplier"."s_phone" AS "s_phone",
"supplier"."s_comment" AS "s_comment"
FROM "part" AS "part"
CROSS JOIN "nation" AS "nation"
CROSS JOIN "region_2" AS "region"
LEFT JOIN "_u_0" AS "_u_0"
ON "part"."p_partkey" = "_u_0"."_u_1"
JOIN "nation" AS "nation"
ON "nation"."n_regionkey" = "region"."r_regionkey"
JOIN "partsupp_2" AS "partsupp"
ON "part"."p_partkey" = "partsupp"."ps_partkey"
JOIN "region_2" AS "region"
ON "nation"."n_regionkey" = "region"."r_regionkey"
JOIN "supplier" AS "supplier"
ON "supplier"."s_nationkey" = "nation"."n_nationkey"
AND "supplier"."s_suppkey" = "partsupp"."ps_suppkey"
Expand Down Expand Up @@ -380,7 +380,6 @@ SELECT
1 - "lineitem"."l_discount"
)) AS "revenue"
FROM "supplier" AS "supplier"
CROSS JOIN "customer" AS "customer"
JOIN "lineitem" AS "lineitem"
ON "supplier"."s_suppkey" = "lineitem"."l_suppkey"
AND CAST("lineitem"."l_shipdate" AS DATE) <= CAST('1996-12-31' AS DATE)
Expand All @@ -391,8 +390,7 @@ JOIN "nation" AS "n1"
)
AND "supplier"."s_nationkey" = "n1"."n_nationkey"
JOIN "nation" AS "n2"
ON "customer"."c_nationkey" = "n2"."n_nationkey"
AND (
ON (
"n1"."n_name" = 'FRANCE' OR "n2"."n_name" = 'FRANCE'
)
AND (
Expand All @@ -401,6 +399,8 @@ JOIN "nation" AS "n2"
AND (
"n2"."n_name" = 'FRANCE' OR "n2"."n_name" = 'GERMANY'
)
JOIN "customer" AS "customer"
ON "customer"."c_nationkey" = "n2"."n_nationkey"
JOIN "orders" AS "orders"
ON "customer"."c_custkey" = "orders"."o_custkey"
AND "orders"."o_orderkey" = "lineitem"."l_orderkey"
Expand Down

0 comments on commit f957a07

Please sign in to comment.