Fix ClassCastException in UnifiedQueryPlanner.preserveCollation on composite-collation plans#5650
Draft
noCharger wants to merge 1 commit into
Conversation
UnifiedQueryPlanner.preserveCollation reads the collation via RelTraitSet.getCollation(), which casts to a single RelCollation. When a plan derives several collations (Calcite stores them as a RelCompositeTrait) the cast throws ClassCastException and the query fails to plan. A multi-column literal-row plan (e.g. makeresults data=, which lowers to a multi-column LogicalValues) derives such composite collations and hits this; a single-column plan derives one collation and survives. Read the collation through the composite-safe getTraits(RelCollationTraitDef.INSTANCE) and materialize a LogicalSort only when there is exactly one genuine non-empty sort collation. A composite (several incidental derived collations) is not a user sort, so the plan is left unwrapped. Single-collation behavior is unchanged. Signed-off-by: Louis Chu <lingzhichu.clz@gmail.com>
Contributor
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
Contributor
PR Code Suggestions ✨Explore these optional code suggestions:
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
UnifiedQueryPlanner.preserveCollationthrowsClassCastException: RelCompositeTrait cannot be cast to RelCollationfor any plan whose top RelNode carries more than one collation in its trait set. It surfaces as a 500Failed to plan query.Root cause. The method reads the collation via:
RelTraitSet.getCollation()is(RelCollation) getTrait(RelCollationTraitDef.INSTANCE). When a plan derives several collations, Calcite stores them as a singleRelCompositeTrait, and the cast fails. A multi-column literal-row plan (for examplemakeresults format=csv data=..., which lowers to a multi-columnLogicalValues) derives such composite collations, so it hits the cast and crashes. A single-column plan derives one collation and survives, which is why this was not caught earlier.Fix. Read the collation through the composite-safe
getTraits(RelCollationTraitDef.INSTANCE)and materialize aLogicalSortonly when there is exactly one genuine (non-empty) sort collation. A composite (several incidental derived collations) is not a user sort, so the plan is left unwrapped instead of crashing. Behavior for the single-collation case is unchanged.Related Issues
N/A
Check List
--signoff.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.