-
Notifications
You must be signed in to change notification settings - Fork 14k
[FLINK-39715] [table-planner] Fix IndexOutOfBoundsException in FlinkExpandConversionRule for global aggregate after sort #28218
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
Changes from 3 commits
a02ffc8
7b1d514
6c9a92b
408646c
6f10f5f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,8 @@ import org.apache.calcite.plan.volcano.AbstractConverter | |
| import org.apache.calcite.rel.{RelCollation, RelCollations, RelCollationTraitDef, RelNode} | ||
| import org.apache.calcite.rel.RelDistribution.Type._ | ||
|
|
||
| import scala.collection.JavaConverters._ | ||
|
|
||
| /** Rule which converts an [[AbstractConverter]] to a RelNode which satisfies the target traits. */ | ||
| class FlinkExpandConversionRule(flinkConvention: Convention) | ||
| extends RelOptRule( | ||
|
|
@@ -68,6 +70,9 @@ class FlinkExpandConversionRule(flinkConvention: Convention) | |
| val toCollation = requiredTraits.getTrait(RelCollationTraitDef.INSTANCE) | ||
| transformedNode = satisfyCollation(flinkConvention, transformedNode, toCollation) | ||
| } | ||
| if (transformedNode == null) { | ||
| return | ||
| } | ||
| checkSatisfyRequiredTrait(transformedNode, requiredTraits) | ||
| call.transformTo(transformedNode) | ||
| } | ||
|
|
@@ -85,6 +90,9 @@ class FlinkExpandConversionRule(flinkConvention: Convention) | |
| // collation. So it is necessary to check whether collation satisfy requirement. | ||
| val requiredCollation = requiredTraits.getTrait(RelCollationTraitDef.INSTANCE) | ||
| val finalRel = satisfyCollation(flinkConvention, newRel, requiredCollation) | ||
| if (finalRel == null) { | ||
| return | ||
| } | ||
| checkSatisfyRequiredTrait(finalRel, requiredTraits) | ||
| call.transformTo(finalRel) | ||
| case _ => // do nothing | ||
|
|
@@ -151,6 +159,15 @@ object FlinkExpandConversionRule { | |
| requiredCollation: RelCollation): RelNode = { | ||
| val fromCollation = node.getTraitSet.getTrait(RelCollationTraitDef.INSTANCE) | ||
| if (!fromCollation.satisfies(requiredCollation)) { | ||
| val fieldCount = node.getRowType.getFieldCount | ||
| val isValidCollation = | ||
| requiredCollation.getFieldCollations.asScala.forall(_.getFieldIndex < fieldCount) | ||
| if (!isValidCollation) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On the bail itself: is the out-of-bounds case always genuinely unsatisfiable, so dropping the path is strictly safe — or could returning |
||
| // An out-of-bounds collation index can never be satisfied by sorting the current node's | ||
| // row type since the field does not exist, so skipping this path cannot cause a | ||
| // CannotPlanException for any valid query. | ||
| return null | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stepping back from the mechanics: under a global aggregate the inner |
||
| } | ||
| val traitSet = node.getTraitSet.replace(requiredCollation).replace(flinkConvention) | ||
| val sortCollation = RelCollationTraitDef.INSTANCE.canonize(requiredCollation) | ||
| flinkConvention match { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.flink.table.planner.plan.rules.physical; | ||
|
|
||
| import org.apache.flink.table.api.Table; | ||
| import org.apache.flink.table.api.TableConfig; | ||
| import org.apache.flink.table.planner.utils.BatchTableTestUtil; | ||
| import org.apache.flink.table.planner.utils.TableTestBase; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.apache.flink.table.api.Expressions.$; | ||
|
|
||
| class FlinkExpandConversionRuleTest extends TableTestBase { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any reason we need a new test class for only one test and can not reuse for instance
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @snuyanzin Makes sense! I checked I deleted the standalone Java file, rewrote the test in Scala, and moved it into Just to be absolutely sure, I ran this new Scala test on the Let me know if it looks good now. |
||
|
|
||
| private BatchTableTestUtil util; | ||
|
|
||
| @BeforeEach | ||
| void setup() { | ||
| util = batchTestUtil(TableConfig.getDefault()); | ||
| util.tableEnv().getConfig().set("parallelism.default", "1"); | ||
|
|
||
| util.tableEnv() | ||
| .executeSql( | ||
| "CREATE TABLE MyTable (" | ||
| + " a INT," | ||
| + " b STRING" | ||
| + ") WITH (" | ||
| + " 'connector' = 'values'," | ||
| + " 'bounded' = 'true'" | ||
| + ")"); | ||
| } | ||
|
|
||
| @Test | ||
| void testOrderByWithGlobalAggregate() { | ||
| Table src = util.tableEnv().from("MyTable"); | ||
| Table result = src.orderBy($("b").asc()).select($("a").max()); | ||
|
|
||
| util.verifyRelPlan(result); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| <?xml version="1.0" ?> | ||
| <!-- | ||
| Licensed to the Apache Software Foundation (ASF) under one or more | ||
| contributor license agreements. See the NOTICE file distributed with | ||
| this work for additional information regarding copyright ownership. | ||
| The ASF licenses this file to you under the Apache License, Version 2.0 | ||
| (the "License"); you may not use this file except in compliance with | ||
| the License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| --> | ||
| <Root> | ||
| <TestCase name="testOrderByWithGlobalAggregate"> | ||
| <Resource name="ast"> | ||
| <![CDATA[ | ||
| LogicalProject(EXPR$0=[$0]) | ||
| +- LogicalAggregate(group=[{}], EXPR$0=[MAX($0)]) | ||
| +- LogicalSort(sort0=[$1], dir0=[ASC]) | ||
| +- LogicalTableScan(table=[[default_catalog, default_database, MyTable]]) | ||
| ]]> | ||
| </Resource> | ||
| <Resource name="optimized rel plan"> | ||
| <![CDATA[ | ||
| HashAggregate(isMerge=[false], select=[MAX(a) AS EXPR$0]) | ||
| +- Sort(orderBy=[b ASC]) | ||
| +- Exchange(distribution=[single]) | ||
| +- TableSourceScan(table=[[default_catalog, default_database, MyTable]], fields=[a, b]) | ||
| ]]> | ||
| </Resource> | ||
| </TestCase> | ||
| </Root> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This guard handles the
satisfyTraitsBySelfpath, butsatisfyCollationhas a second caller —satisfyTraitsByInputat line 87 — that now also receives thenullyou introduced. TherefinalRelflows straight intocheckSatisfyRequiredTrait(finalRel, …), which dereferencesnode.getTraitSetand will NPE. So an input that reaches the trait-pushdown branch with the same out-of-bounds collation swaps the IndexOutOfBoundsException for a NullPointerException. Could we either apply the same guard at line 87, or hoist the bounds check sosatisfyCollationnever handsnullto an unprepared caller? A test exercising the input-pushdown path would lock it down.