You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fixes a ClickHouse parser gap where `ORDER BY expr WITH FILL` was mis-routed to the WITH-CTE parser.
What now parses
`ORDER BY id WITH FILL`
`ORDER BY id WITH FILL STEP 1`
`ORDER BY day WITH FILL FROM '2024-01-01' TO '2024-12-31' STEP INTERVAL 1 DAY`
`ORDER BY id DESC WITH FILL STEP 1`
Multiple columns with per-item WITH FILL
Implementation
Per-entry WITH FILL detection inside `parseOrderByClause` after the NULLS clause. Gated to ClickHouse.
New `skipClickHouseWithFillTail` helper consumes the optional `FROM expr`, `TO expr`, `STEP expr` arguments in any order.
Permissive: the FILL modifier is consumed but not yet modeled on the AST (follow-up).
The bug was that after parsing the ORDER BY items, the parser returned to the statement dispatcher which saw `WITH` and dispatched to the WITH-CTE parser. Consuming the tail inside the ORDER BY clause prevents that.
Test plan
`TestClickHouseOrderByWithFill`: 5 shapes
`go test -race ./pkg/sql/parser/ ./pkg/gosqlx/ ./pkg/sql/ast/` green
Code Review: ClickHouse ORDER BY ... WITH FILL Implementation
Review Tasks
Analyze implementation in select_clauses.go
Review test coverage in clickhouse_with_fill_test.go
Check code quality and best practices
Evaluate performance and security considerations
Provide comprehensive feedback
Summary
This PR successfully implements ClickHouse ORDER BY ... WITH FILL syntax parsing by fixing a routing issue where WITH FILL was incorrectly handled by the WITH-CTE parser. The implementation is clean, well-documented, and follows project conventions.
Proper dialect gating: Implementation correctly restricts WITH FILL to ClickHouse dialect only (select_clauses.go:554)
Follows project patterns: Consistent with existing ClickHouse-specific features like FINAL and GLOBAL JOIN
Clean separation: New functionality isolated in dedicated helper method skipClickHouseWithFillTail()
Implementation Approach
Correct fix: Addresses the core issue by consuming WITH FILL tokens within ORDER BY parsing instead of letting them fall through to CTE parser
Permissive parsing: Smart design choice to consume syntax without full AST modeling, enabling forward compatibility
Flexible argument parsing: Handles FROM/TO/STEP in any order (select_clauses.go:576-590)
Proper error handling: Uses graceful error recovery in skipClickHouseWithFillTail() to handle complex expressions
Test Coverage
Comprehensive scenarios: Tests cover all major use cases including basic FILL, STEP modifier, FROM/TO ranges, multiple columns, and DESC ordering
Proper test structure: Uses table-driven test pattern consistent with codebase standards
Dialect-specific testing: Correctly uses keywords.DialectClickHouse for testing
⚠️ Areas for Consideration
AST Modeling (Future Enhancement)
The PR correctly notes that WITH FILL modifiers are not yet modeled in the AST. While this is acceptable for the current fix, consider:
Adding a WithFill *FillClause field to OrderByExpression in a follow-up
Ensuring the current implementation can be extended without breaking changes
Edge Case Testing
Consider adding tests for:
// Mixed FILL and non-FILL columns"SELECT * FROM t ORDER BY a WITH FILL, b DESC, c WITH FILL STEP 1"// Complex expressions with FILL"SELECT * FROM t ORDER BY date_trunc('day', ts) WITH FILL STEP INTERVAL 1 DAY"// Error cases (if desired)"SELECT * FROM t ORDER BY id WITH FILL STEP"// Missing step value
Performance Considerations
✅ Minimal overhead: Implementation only activates for ClickHouse dialect
Tests interaction with other ORDER BY features (DESC, multiple columns)
📋 Recommendations
Consider adding negative tests: Test that WITH FILL is rejected in non-ClickHouse dialects
Document future AST work: Add a TODO comment referencing the follow-up issue for AST modeling
Benchmark impact: Consider adding a simple benchmark to ensure no performance regression
🎯 Overall Assessment
✅ Recommended for merge
This is a well-crafted fix that:
Solves the stated problem correctly
Follows all project conventions
Includes appropriate test coverage
Uses a forward-compatible design
Has minimal performance impact
Introduces no security concerns
The permissive parsing approach is particularly smart, allowing the parser to handle ClickHouse's WITH FILL syntax today while leaving room for full AST modeling in the future.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a ClickHouse parser gap where `ORDER BY expr WITH FILL` was mis-routed to the WITH-CTE parser.
What now parses
Implementation
The bug was that after parsing the ORDER BY items, the parser returned to the statement dispatcher which saw `WITH` and dispatched to the WITH-CTE parser. Consuming the tail inside the ORDER BY clause prevents that.
Test plan
Part of #482.
🤖 Generated with Claude Code