Skip to content

Fix SELECT * REPLACE behavior in WHERE clause with new analyzer#87630

Merged
antaljanosbenjamin merged 5 commits intoClickHouse:masterfrom
xiaohuanlin:fix-select-replace-where-85313
Oct 6, 2025
Merged

Fix SELECT * REPLACE behavior in WHERE clause with new analyzer#87630
antaljanosbenjamin merged 5 commits intoClickHouse:masterfrom
xiaohuanlin:fix-select-replace-where-85313

Conversation

@xiaohuanlin
Copy link
Copy Markdown
Contributor

@xiaohuanlin xiaohuanlin commented Sep 25, 2025

Fix SELECT * REPLACE behavior in WHERE clause with new analyzer

This commit fixes issue #85313 where SELECT * REPLACE with WHERE clause on the same column returned incorrect results with the new analyzer. For example:

SELECT * REPLACE(a + 10 AS b) FROM test WHERE b = 11 was returning empty results instead of matching rows where the replaced expression equals 11.

The root cause was that the new analyzer processes SELECT * REPLACE and WHERE clause sequentially, causing WHERE conditions to see original column values instead of replaced values. The old analyzer processes both together correctly.

The fix captures REPLACE transformer mappings during resolveMatcher processing and immediately applies inline identifier replacement to WHERE and HAVING clauses using lambda functions.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

  • Fixed incorrect SELECT * REPLACE behavior in WHERE clause with new analyzer when filtering on replaced columns.

@antaljanosbenjamin antaljanosbenjamin self-assigned this Sep 25, 2025
@antaljanosbenjamin antaljanosbenjamin added the can be tested Allows running workflows for external contributors label Sep 25, 2025
@clickhouse-gh
Copy link
Copy Markdown
Contributor

clickhouse-gh bot commented Sep 25, 2025

Workflow [PR], commit [e703049]

Summary:

job_name test_name status info comment
Integration tests (arm_binary, distributed plan, 3/4) failure
test_scheduler_cpu_preemptive/test.py::test_independent_pools FAIL

@clickhouse-gh clickhouse-gh bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 25, 2025
@xiaohuanlin xiaohuanlin force-pushed the fix-select-replace-where-85313 branch from f38494e to a59ff0c Compare September 27, 2025 03:49
- Fix column reference issues in WHERE, PREWHERE, ORDER BY, GROUP BY, HAVING, LIMIT BY, WINDOW clauses
- Replace original test with comprehensive 17-scenario test suite
- Ensure consistent behavior between new and old analyzer
@xiaohuanlin xiaohuanlin force-pushed the fix-select-replace-where-85313 branch from a59ff0c to e62ac0a Compare September 27, 2025 15:59
Address review comments from @antaljanosbenjamin:

1. LIMIT BY test enhancement:
   - Add test data with duplicate values to properly test LIMIT BY grouping
   - Use expression 'b < 200' to test replaced column in LIMIT BY condition

2. GROUP BY test expansion:
   - Preserve subquery test (Test 16) - validates replaced values in subquery work with outer GROUP BY
   - Add direct usage test (Test 17) - validates replaced column can be used directly in GROUP BY clause

3. HAVING test expansion:
   - Preserve subquery test (Test 18) - validates replaced values in subquery work with outer HAVING
   - Add direct usage test (Test 19) - validates replaced column can be used directly in HAVING clause

Test coverage now includes both subquery patterns (important for nested query validation)
and direct usage patterns (requested by reviewer) for GROUP BY and HAVING clauses.
Copy link
Copy Markdown
Member

@antaljanosbenjamin antaljanosbenjamin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One question only to be sure we are not making a mistake here. Apart from that, it looks good.

@antaljanosbenjamin
Copy link
Copy Markdown
Member

Failed test is caused by #87655. Merging PR.

@antaljanosbenjamin antaljanosbenjamin added this pull request to the merge queue Oct 6, 2025
Merged via the queue into ClickHouse:master with commit bfc360e Oct 6, 2025
121 of 123 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants