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

Issues with aliases and 'with' clause while finding columns #438

Open
Byunk opened this issue Oct 11, 2023 · 0 comments
Open

Issues with aliases and 'with' clause while finding columns #438

Byunk opened this issue Oct 11, 2023 · 0 comments

Comments

@Byunk
Copy link
Contributor

Byunk commented Oct 11, 2023

Hello @macbre. I'm addressing the Attribute Errors we discussed before, but I've come across a new bug. This one appears to be related to issues with aliases and 'with' queries, and it seems it might require a significant number of changes to fix.

Testing query

WITH customer_total_return AS
  (SELECT sr_customer_sk AS ctr_customer_sk,
          sr_store_sk AS ctr_store_sk,
          sum(SR_FEE) AS ctr_total_return
   FROM store_returns,
        date_dim
   WHERE sr_returned_date_sk = d_date_sk
     AND d_year =2000
   GROUP BY sr_customer_sk,
            sr_store_sk)
SELECT c_customer_id
FROM customer_total_return ctr1,
     store,
     customer
WHERE ctr1.ctr_total_return >
    (SELECT avg(ctr_total_return)*1.2
     FROM customer_total_return ctr2
     WHERE ctr1.ctr_store_sk = ctr2.ctr_store_sk)
  AND s_store_sk = ctr1.ctr_store_sk
  AND s_state = 'SD'
  AND ctr1.ctr_customer_sk = c_customer_sk
ORDER BY c_customer_id
LIMIT 100;

Running this query with #437 gives us a result that differs from what I'd expect. The parser doesn't store aliases for 'with' queries, so it can't expand 'ctr1' and 'ctr2' to 'customer_total_return' in the query.

Actual Result

{
    "group_by": [
        "sr_customer_sk",
        "sr_store_sk"
    ],
    "order_by": [
        "c_customer_id"
    ],
    "select": [
        "sr_customer_sk",
        "sr_store_sk",
        "SR_FEE",
        "c_customer_id"
    ],
    "where": [
        "sr_returned_date_sk",
        "d_date_sk",
        "d_year",
        "ctr1.ctr_total_return",
        "ctr1.ctr_store_sk",
        "ctr2.ctr_store_sk",
        "s_store_sk",
        "s_state",
        "ctr1.ctr_customer_sk",
        "c_customer_sk"
    ]
}

Expected Result

{
    "group_by": [
        "sr_customer_sk",
        "sr_store_sk"
    ],
    "order_by": [
        "c_customer_id"
    ],
    "select": [
        "sr_customer_sk",
        "sr_store_sk",
        "SR_FEE",
        "c_customer_id"
    ],
    "where": [
        "sr_returned_date_sk",
        "d_date_sk",
        "d_year",
        "s_store_sk",
        "s_state",
        "c_customer_sk"
    ]
}
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

1 participant