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

Subquery inside CASE...WHEN... should not have alias added #2905

Closed
lephuongbg opened this issue Jun 17, 2024 · 2 comments
Closed

Subquery inside CASE...WHEN... should not have alias added #2905

lephuongbg opened this issue Jun 17, 2024 · 2 comments

Comments

@lephuongbg
Copy link

lephuongbg commented Jun 17, 2024

Given following example of using a subquery inside a CASE...WHEN... expression:

class OrderItems(p.Model):
    quantity = p.IntegerField()
    metadata = BinaryJSONField()

products_recordset = fn.jsonb_populate_recordset(
    p.SQL("NULL::universal.al_items"), OrderItems.metadata["products"].as_json()
).alias("products_recordset")

print(
    OrderItems.select(
        p.Case(
            OrderItems.metadata["price"].is_null(),
            [
                (
                    True,
                    ##### The subquery is here ######
                    p.Select(
                        columns=[
                            fn.sum(
                                products_recordset.c.price
                                * products_recordset.c.quantity
                            )
                        ],
                        from_list=[products_recordset],
                    ),
                )
            ],
            OrderItems.metadata["price"].cast("text").cast("double precision")
            * OrderItems.quantity,
        ),
    )
)

Expected:

The desired SQL output should be as follow:

SELECT CASE ("t1"."metadata" ->> 'price' IS NULL)
           WHEN 1 THEN (SELECT sum("products_recordset"."price" * "products_recordset"."quantity")
                        FROM jsonb_populate_recordset(NULL::universal.al_items,
                                                      "t1"."metadata" -> 'products') AS "products_recordset")
           ELSE (CAST(CAST("t1"."metadata" ->> 'price' AS text) AS double precision) * "t1"."quantity") END
FROM "orderitems" AS "t1";

Current output:

An extra alias was added to the subquery, causing invalid syntax.

SELECT CASE ("t1"."metadata" ->> 'price' IS NULL)
           WHEN 1 THEN (SELECT sum("products_recordset"."price" * "products_recordset"."quantity")
                        FROM jsonb_populate_recordset(NULL::universal.al_items, "t1"."metadata" ->
                                                                                'products') AS "products_recordset") AS "t2"  --- <---- Note the extra 'as "t2"' here
            ELSE (CAST(CAST("t1"."metadata"->>'price' AS text) AS double precision) * "t1"."quantity") END
FROM "orderitems" AS "t1";

Database is PostgreSQL.

@lephuongbg
Copy link
Author

Adding in_query=True to the context here would solve this particular issue, but I don't know whether it would cause other regressions.

peewee/peewee.py

Line 1845 in c5aa497

with ctx(in_function=False):

@coleifer
Copy link
Owner

coleifer commented Jun 17, 2024

Thanks, this should be fixed now.

SELECT 
  CASE ("t1"."metadata"->>'price' IS NULL) 
  WHEN 1 THEN (
    SELECT sum("products_recordset"."price" * "products_recordset"."quantity")
    FROM jsonb_populate_recordset(NULL::universal.al_items, "t1"."metadata"->'products') AS "products_recordset") 
  ELSE (CAST(CAST("t1"."metadata"->>'price' AS text) AS double precision) * "t1"."quantity") 
  END
FROM "orderitems" AS "t1"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants