Fix SortCollapseRule when inner order is DESC. (#4157)

* Fix SortCollapseRule when inner order is DESC.

* Remove unused import.
This commit is contained in:
Gian Merlino 2017-04-12 19:09:45 +09:00 committed by Nishant Bangarwa
parent 15f3a94474
commit 9f4266fba1
2 changed files with 32 additions and 5 deletions

View File

@ -21,7 +21,6 @@ package io.druid.sql.calcite.rule;
import org.apache.calcite.plan.RelOptRule; import org.apache.calcite.plan.RelOptRule;
import org.apache.calcite.plan.RelOptRuleCall; import org.apache.calcite.plan.RelOptRuleCall;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.core.Sort; import org.apache.calcite.rel.core.Sort;
import org.apache.calcite.rex.RexLiteral; import org.apache.calcite.rex.RexLiteral;
@ -55,6 +54,7 @@ public class SortCollapseRule extends RelOptRule
final int firstOffset = (first.offset != null ? RexLiteral.intValue(first.offset) : 0); final int firstOffset = (first.offset != null ? RexLiteral.intValue(first.offset) : 0);
final int secondOffset = (second.offset != null ? RexLiteral.intValue(second.offset) : 0); final int secondOffset = (second.offset != null ? RexLiteral.intValue(second.offset) : 0);
final int offset = firstOffset + secondOffset;
final int fetch; final int fetch;
if (first.fetch == null && second.fetch == null) { if (first.fetch == null && second.fetch == null) {
@ -76,10 +76,13 @@ public class SortCollapseRule extends RelOptRule
); );
} }
final RelNode combined = call.builder() final Sort combined = first.copy(
.push(first.getInput()) first.getTraitSet(),
.sortLimit(firstOffset + secondOffset, fetch, first.getChildExps()) first.getInput(),
.build(); first.getCollation(),
offset == 0 ? null : call.builder().literal(offset),
call.builder().literal(fetch)
);
call.transformTo(combined); call.transformTo(combined);
} }

View File

@ -2075,6 +2075,30 @@ public class CalciteQueryTest
); );
} }
@Test
public void testSelectDistinctWithSortAsOuterQuery4() throws Exception
{
testQuery(
"SELECT * FROM (SELECT DISTINCT dim2 FROM druid.foo ORDER BY dim2 DESC LIMIT 5) LIMIT 10",
ImmutableList.<Query>of(
new TopNQueryBuilder()
.dataSource(CalciteTests.DATASOURCE1)
.intervals(QSS(Filtration.eternity()))
.granularity(Granularities.ALL)
.dimension(new DefaultDimensionSpec("dim2", "d0"))
.metric(new InvertedTopNMetricSpec(new DimensionTopNMetricSpec(null, StringComparators.LEXICOGRAPHIC)))
.threshold(5)
.context(QUERY_CONTEXT_DEFAULT)
.build()
),
ImmutableList.of(
new Object[]{""},
new Object[]{"abc"},
new Object[]{"a"}
)
);
}
@Test @Test
public void testCountDistinct() throws Exception public void testCountDistinct() throws Exception
{ {