Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(optimizer)!: Preserve DPipe in simplify_concat #3317

Merged
merged 4 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions sqlglot/optimizer/simplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import typing as t
from collections import deque
from decimal import Decimal
from functools import reduce

import sqlglot
from sqlglot import Dialect, exp
Expand Down Expand Up @@ -783,6 +784,8 @@ def simplify_concat(expression):

if concat_type is exp.ConcatWs:
new_args = [sep_expr] + new_args
elif concat_type is exp.Concat and isinstance(expression, exp.DPipe):
georgesittas marked this conversation as resolved.
Show resolved Hide resolved
return reduce(lambda x, y: exp.DPipe(this=x, expression=y), new_args)

return concat_type(expressions=new_args, **args)

Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/optimizer/simplify.sql
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ CONCAT_WS(sep, 'a', 'b');
CONCAT_WS(sep, 'a', 'b');

'a' || 'b' || x;
CONCAT('ab', x);
'ab' || x;

CONCAT(a, b) IN (SELECT * FROM foo WHERE cond);
CONCAT(a, b) IN (SELECT * FROM foo WHERE cond);
Expand Down
15 changes: 7 additions & 8 deletions tests/fixtures/optimizer/tpc-ds/tpc-ds.sql
Original file line number Diff line number Diff line change
Expand Up @@ -822,23 +822,23 @@ WITH "salesreturns" AS (
), "x" AS (
SELECT
'store channel' AS "channel",
CONCAT('store', "ssr"."s_store_id") AS "id",
'store' || "ssr"."s_store_id" AS "id",
"ssr"."sales" AS "sales",
"ssr"."returns1" AS "returns1",
"ssr"."profit" - "ssr"."profit_loss" AS "profit"
FROM "ssr" AS "ssr"
UNION ALL
SELECT
'catalog channel' AS "channel",
CONCAT('catalog_page', "csr"."cp_catalog_page_id") AS "id",
'catalog_page' || "csr"."cp_catalog_page_id" AS "id",
"csr"."sales" AS "sales",
"csr"."returns1" AS "returns1",
"csr"."profit" - "csr"."profit_loss" AS "profit"
FROM "csr" AS "csr"
UNION ALL
SELECT
'web channel' AS "channel",
CONCAT('web_site', "wsr"."web_site_id") AS "id",
'web_site' || "wsr"."web_site_id" AS "id",
"wsr"."sales" AS "sales",
"wsr"."returns1" AS "returns1",
"wsr"."profit" - "wsr"."profit_loss" AS "profit"
Expand Down Expand Up @@ -10927,23 +10927,23 @@ WITH "date_dim_2" AS (
), "x" AS (
SELECT
'store channel' AS "channel",
CONCAT('store', "ssr"."store_id") AS "id",
'store' || "ssr"."store_id" AS "id",
"ssr"."sales" AS "sales",
"ssr"."returns1" AS "returns1",
"ssr"."profit" AS "profit"
FROM "ssr" AS "ssr"
UNION ALL
SELECT
'catalog channel' AS "channel",
CONCAT('catalog_page', "csr"."catalog_page_id") AS "id",
'catalog_page' || "csr"."catalog_page_id" AS "id",
"csr"."sales" AS "sales",
"csr"."returns1" AS "returns1",
"csr"."profit" AS "profit"
FROM "csr" AS "csr"
UNION ALL
SELECT
'web channel' AS "channel",
CONCAT('web_site', "wsr"."web_site_id") AS "id",
'web_site' || "wsr"."web_site_id" AS "id",
"wsr"."sales" AS "sales",
"wsr"."returns1" AS "returns1",
"wsr"."profit" AS "profit"
Expand Down Expand Up @@ -11368,7 +11368,7 @@ ORDER BY c_customer_id
LIMIT 100;
SELECT
"customer"."c_customer_id" AS "customer_id",
CONCAT("customer"."c_last_name", ', ', "customer"."c_first_name") AS "customername"
"customer"."c_last_name" || ', ' || "customer"."c_first_name" AS "customername"
FROM "customer" AS "customer"
JOIN "customer_address" AS "customer_address"
ON "customer"."c_current_addr_sk" = "customer_address"."ca_address_sk"
Expand Down Expand Up @@ -12743,4 +12743,3 @@ ORDER BY
"sm_type",
"cc_name"
LIMIT 100;