wren-engine icon indicating copy to clipboard operation
wren-engine copied to clipboard

Duplicate CTE Name

Open grieve54706 opened this issue 1 year ago • 0 comments

Input SQL

SELECT COUNT(*) AS "count" FROM "tpch"."Orders" WHERE ("tpch"."Orders"."orderkey" IS NOT NULL) AND (("tpch"."Orders"."orderkey" <> ?) OR ("tpch"."Orders"."orderkey" IS NULL))

Rewritten SQL

WITH
  "Orders" AS (
   SELECT
     "Orders"."orderkey" "orderkey"
   , "Orders"."custkey" "custkey"
   , "Orders"."orderstatus" "orderstatus"
   , "Orders"."totalprice" "totalprice"
   , "Orders"."orderdate" "orderdate"
   FROM
     (
      SELECT
        o_orderkey "orderkey"
      , o_custkey "custkey"
      , o_orderstatus "orderstatus"
      , o_totalprice "totalprice"
      , o_orderdate "orderdate"
      FROM
        (
         SELECT *
         FROM
           tpch.orders
      )  "Orders"
   )  "Orders"
) 
, "Orders" AS (
   SELECT
     "Orders"."orderkey" "orderkey"
   , "Orders"."custkey" "custkey"
   , "Orders"."orderstatus" "orderstatus"
   , "Orders"."totalprice" "totalprice"
   , "Orders"."orderdate" "orderdate"
   FROM
     (
      SELECT
        o_orderkey "orderkey"
      , o_custkey "custkey"
      , o_orderstatus "orderstatus"
      , o_totalprice "totalprice"
      , o_orderdate "orderdate"
      FROM
        (
         SELECT *
         FROM
           tpch.orders
      )  "Orders"
   )  "Orders"
) 
SELECT COUNT(*) "count"
FROM
  "Orders"
WHERE (("Orders"."orderkey" IS NOT NULL) AND (("Orders"."orderkey" <> ?) OR ("Orders"."orderkey" IS NULL)))

Both code blocks add duplicate descriptors. May the descriptor for count(*) need to modify. https://github.com/Canner/wren-engine/blob/88d527b241abdc5bd56e3bdf396c1d2b379d7aaf/wren-base/src/main/java/io/wren/base/sqlrewrite/WrenSqlRewrite.java#L95-L106

grieve54706 avatar Apr 15 '24 10:04 grieve54706