Skip to content
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

[FLINK-21592][table-planner-blink] RemoveSingleAggregateRule fails du… #15082

Merged
merged 3 commits into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -118,11 +118,7 @@
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
* Copied to fix CALCITE-4333, should be removed for the next Calcite upgrade.
*
* <p>Changes: Line 671 ~ Line 681, Line 430 ~ Line 441.
*/
/** Copied to fix calcite issues. */
public class RelDecorrelator implements ReflectiveVisitor {
// ~ Static fields/initializers ---------------------------------------------

Expand Down Expand Up @@ -439,6 +435,9 @@ public Frame decorrelateRel(Sort rel) {
return null;
}

// BEGIN FLINK MODIFICATION
// Reason: to de-correlate sort rel when its parent is not a correlate
// Should be removed after CALCITE-4333 is fixed
final RelNode newInput = frame.r;

Mappings.TargetMapping mapping =
Expand All @@ -452,6 +451,7 @@ public Frame decorrelateRel(Sort rel) {

final int offset = rel.offset == null ? -1 : RexLiteral.intValue(rel.offset);
final int fetch = rel.fetch == null ? -1 : RexLiteral.intValue(rel.fetch);
// END FLINK MODIFICATION

final RelNode newSort =
relBuilder
Expand Down Expand Up @@ -685,6 +685,9 @@ private static void shiftMapping(Map<Integer, Integer> mapping, int startIndex,

public Frame getInvoke(RelNode r, RelNode parent) {
final Frame frame = dispatcher.invoke(r);
// BEGIN FLINK MODIFICATION
// Reason: to de-correlate sort rel when its parent is not a correlate
// Should be removed after CALCITE-4333 is fixed
if (frame != null && parent instanceof Correlate && r instanceof Sort) {
Sort sort = (Sort) r;
// Can not decorrelate if the sort has per-correlate-key attributes like
Expand All @@ -696,6 +699,7 @@ public Frame getInvoke(RelNode r, RelNode parent) {
return null;
}
}
// END FLINK MODIFICATION
if (frame != null) {
map.put(r, frame);
}
Expand Down Expand Up @@ -1869,13 +1873,15 @@ public void onMatch(RelOptRuleCall call) {
return;
}

// singleAggRel produces a nullable type, so create the new
// projection that casts proj expr to a nullable type.
// BEGIN FLINK MODIFICATION
// Reason: fix the nullability mismatch issue
final RelBuilder relBuilder = call.builder();
final boolean nullable = singleAggregate.getAggCallList().get(0).getType().isNullable();
final RelDataType type =
relBuilder
.getTypeFactory()
.createTypeWithNullability(projExprs.get(0).getType(), true);
.createTypeWithNullability(projExprs.get(0).getType(), nullable);
// END FLINK MODIFICATION
final RexNode cast = relBuilder.getRexBuilder().makeCast(type, projExprs.get(0));
relBuilder.push(aggregate).project(cast);
call.transformTo(relBuilder.build());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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="testRemoveSingleAggregateRule">
<Resource name="sql">
<![CDATA[select (select count(x)-1 from foo where foo.y=bar.i) from bar]]>
</Resource>
<Resource name="ast">
<![CDATA[
LogicalProject(EXPR$0=[$SCALAR_QUERY({
LogicalProject(EXPR$0=[-($0, 1)])
LogicalAggregate(group=[{}], agg#0=[COUNT($0)])
LogicalProject(x=[$0])
LogicalFilter(condition=[=($1, $cor0.i)])
LogicalTableScan(table=[[default_catalog, default_database, foo, source: [TestTableSource(x, y)]]])
})])
+- LogicalTableScan(table=[[default_catalog, default_database, bar, source: [TestTableSource(i, s)]]])
]]>
</Resource>
<Resource name="optimized rel plan">
<![CDATA[
Calc(select=[-(CASE(IS NULL($f1), 0:BIGINT, $f1), 1) AS EXPR$0])
+- HashJoin(joinType=[LeftOuterJoin], where=[=(i, y)], select=[i, y, $f1], build=[right])
:- Exchange(distribution=[hash[i]])
: +- Calc(select=[i])
: +- LegacyTableSourceScan(table=[[default_catalog, default_database, bar, source: [TestTableSource(i, s)]]], fields=[i, s])
+- HashAggregate(isMerge=[true], groupBy=[y], select=[y, Final_COUNT(count$0) AS $f1])
+- Exchange(distribution=[hash[y]])
+- LocalHashAggregate(groupBy=[y], select=[y, Partial_COUNT(x) AS count$0])
+- Calc(select=[x, y], where=[IS NOT NULL(y)])
+- LegacyTableSourceScan(table=[[default_catalog, default_database, foo, source: [TestTableSource(x, y)]]], fields=[x, y])
]]>
</Resource>
</TestCase>
</Root>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.logical

import org.apache.flink.api.scala._
import org.apache.flink.table.api._
import org.apache.flink.table.planner.utils.TableTestBase

import org.junit.{Before, Test}

/**
* Test for RemoveSingleAggregateRule.
*/
class RemoveSingleAggregateRuleTest extends TableTestBase {

private val util = batchTestUtil()

@Before
def setup(): Unit = {
util.addTableSource[(Int, Int)]("foo", 'x, 'y)
util.addTableSource[(Int, String)]("bar", 'i, 's)
}

@Test
def testRemoveSingleAggregateRule(): Unit = {
util.verifyRelPlan("select (select count(x)-1 from foo where foo.y=bar.i) from bar")
}

}