Skip to content

fix: PushDownFilter for GROUP BY on uppercase col names #16049

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

aditanase
Copy link
Contributor

Which issue does this PR close?

PushDownFilter does not push a predicate when the table has columns that are not all lowercase. Tried with and without enable_ident_normalization - no change. The logic inside parse_identifiers_normalized does not seem to properly detect quotes and it will lowercase the column used in the group by expression.

Here's the query I used, just for illustration:

SELECT * FROM (
    SELECT
        fm.Timestamp,
        SUM(fm.ConsumedUnits)
    FROM fm
    WHERE
        fm.Timestamp BETWEEN to_timestamp('2025-04-10') AND to_timestamp('2025-04-20')
    GROUP BY fm.Timestamp
)
WHERE Timestamp = to_timestamp('2025-04-12')

Expected query plan:

Aggregate: groupBy=[[fm.Timestamp]], aggr=[[sum(fm.ConsumedUnits)]]
  TableScan: fm projection=[ConsumedUnits, Timestamp], full_filters=[fm.Timestamp = TimestampMicrosecond(1744416000000000, Some("UTC")), fm.Timestamp >= TimestampMicrosecond(1744243200000000, Some("UTC")), fm.Timestamp <= TimestampMicrosecond(1745107200000000, Some("UTC"))]

Actual query plan:

Filter: fm.Timestamp = TimestampMicrosecond(1744416000000000, Some("UTC"))
  Aggregate: groupBy=[[fm.Timestamp]], aggr=[[sum(fm.ConsumedUnits)]]
    TableScan: fm projection=[ConsumedUnits, Timestamp], full_filters=[fm.Timestamp >= TimestampMicrosecond(1744243200000000, Some("UTC")), fm.Timestamp <= TimestampMicrosecond(1745107200000000, Some("UTC"))]

An alterate fix could use expr_to_columns to extract the columns, as in Unnest above:

let mut group_expr_columns: HashSet<Column> = HashSet::new();
for p in &agg.group_expr {
    expr_to_columns(&p, &mut group_expr_columns)?;
}

Question: should we make the same change in the Window functions branch?

Rationale for this change

What changes are included in this PR?

Are these changes tested?

Yes, from a client application.

I did not add any unit tests, none of the existing tests in this module use upper case columns. Tried to add another table/schema, but then the test was failing, I am unsure of how to control the lowercasing of column names.

Are there any user-facing changes?

@github-actions github-actions bot added the optimizer Optimizer rules label May 14, 2025
@aditanase aditanase changed the title fix: PushDownFilter for GROUP BY on uppercase col names fix: PushDownFilter for GROUP BY on uppercase col names May 14, 2025
@aditanase aditanase force-pushed the fix-gby-pushdownfilter branch from 0495f24 to 14ceef8 Compare May 14, 2025 13:16
@aditanase
Copy link
Contributor Author

@adriangb Saw you recent commits in this area, would appreciate if you weighed in on this. Thank you! 🙌

@adriangb
Copy link
Contributor

Hmm I've been doing a lot with the physical optimizer of the same name but haven't touched the logical optimizer. The recent changes may mean that the pushdown ends up happening regardless at the physical level but I think it's worth fixing the logical level anyway.

I don't fully understand the issue: does from_qualified_name_ignore_case do something different with quotes than from_qualified_name? I also don't see any quotes in your example.

@aditanase
Copy link
Contributor Author

I don't fully understand the issue: does from_qualified_name_ignore_case do something different with quotes than from_qualified_name? I also don't see any quotes in your example.

Sorry for not being more clear. I was referring to these lines:

.map(|id| match id.quote_style {
Some(_) => id.value,

Reading it made me think that if I used quotes I might convince it to remain unchanged, but it still converts to lowercase timestamp, no matter what I tried, so I didn't include it in the code sample.

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

Successfully merging this pull request may close these issues.

2 participants