Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -68,6 +70,9 @@ class FlinkExpandConversionRule(flinkConvention: Convention)
val toCollation = requiredTraits.getTrait(RelCollationTraitDef.INSTANCE)
transformedNode = satisfyCollation(flinkConvention, transformedNode, toCollation)
}
if (transformedNode == null) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This guard handles the satisfyTraitsBySelf path, but satisfyCollation has a second caller — satisfyTraitsByInput at line 87 — that now also receives the null you introduced. There finalRel flows straight into checkSatisfyRequiredTrait(finalRel, …), which dereferences node.getTraitSet and 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 so satisfyCollation never hands null to an unprepared caller? A test exercising the input-pushdown path would lock it down.

return
}
checkSatisfyRequiredTrait(transformedNode, requiredTraits)
call.transformTo(transformedNode)
}
Expand All @@ -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
Expand Down Expand Up @@ -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) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 null here turn a currently-plannable query into a RelOptPlanner.CannotPlanException if this converter was the only path offering it?

// 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stepping back from the mechanics: under a global aggregate the inner ORDER BY b is semantically dead — the collation on b only survives because nothing dropped it before the row type shrank to one field. Returning null treats the symptom at the trait-satisfaction layer. Did you consider eliminating the spurious collation upstream instead?

}
val traitSet = node.getTraitSet.replace(requiredCollation).replace(flinkConvention)
val sortCollation = RelCollationTraitDef.INSTANCE.canonize(requiredCollation)
flinkConvention match {
Expand Down
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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 AggregateTest/AggregateTestBase?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@snuyanzin Makes sense! I checked AggregateTest.scala and since batchTestUtil is created per method, setting the parallelism here won't affect the other tests.

I deleted the standalone Java file, rewrote the test in Scala, and moved it into batch/table/AggregateTest.scala. The XML plan is updated as well.

Just to be absolutely sure, I ran this new Scala test on the master branch and it successfully reproduces the IndexOutOfBoundsException crash.

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>