Skip to content
Permalink
Browse files
DRILL-6553: Fix TopN for unnest operator
closes #1353
  • Loading branch information
vvysotskyi authored and arina-ielchiieva committed Jul 4, 2018
1 parent e047a2b commit 5c9303d7e3797377191ff9a57103ef033e023db1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
@@ -50,15 +50,15 @@ public DrillLateralJoinRelBase(RelOptCluster cluster, RelTraitSet traits, RelNod
this.excludeCorrelateColumn = excludeCorrelateCol;
}

@Override public RelOptCost computeSelfCost(RelOptPlanner planner,
RelMetadataQuery mq) {
@Override
public RelOptCost computeSelfCost(RelOptPlanner planner, RelMetadataQuery mq) {
DrillCostBase.DrillCostFactory costFactory = (DrillCostBase.DrillCostFactory) planner.getCostFactory();

double rowCount = mq.getRowCount(this.getLeft());
double rowCount = estimateRowCount(mq);
long fieldWidth = PrelUtil.getPlannerSettings(planner).getOptions()
.getOption(ExecConstants.AVERAGE_FIELD_WIDTH_KEY).num_val;
.getLong(ExecConstants.AVERAGE_FIELD_WIDTH_KEY);

double rowSize = (this.getLeft().getRowType().getFieldList().size()) * fieldWidth;
double rowSize = left.getRowType().getFieldList().size() * fieldWidth;

double cpuCost = rowCount * rowSize * DrillCostBase.BASE_CPU_COST;
double memCost = !excludeCorrelateColumn ? CORRELATE_MEM_COPY_COST : 0.0;
@@ -117,4 +117,9 @@ public RelDataType constructRowType(RelDataType inputRowType) {
}
return inputRowType;
}

@Override
public double estimateRowCount(RelMetadataQuery mq) {
return mq.getRowCount(left);
}
}
@@ -18,8 +18,10 @@
package org.apache.drill.exec.physical.impl.lateraljoin;

import org.apache.drill.categories.OperatorTest;
import org.apache.drill.exec.ExecConstants;
import org.apache.drill.exec.planner.physical.PlannerSettings;
import org.apache.drill.test.ClusterFixture;
import org.apache.drill.test.ClusterFixtureBuilder;
import org.apache.drill.test.ClusterTest;
import org.apache.drill.test.TestBuilder;
import org.junit.BeforeClass;
@@ -43,8 +45,10 @@ public class TestE2EUnnestAndLateral extends ClusterTest {
public static void setupTestFiles() throws Exception {
dirTestWatcher.copyResourceToRoot(Paths.get("lateraljoin", "multipleFiles", regularTestFile_1));
dirTestWatcher.copyResourceToRoot(Paths.get("lateraljoin", "multipleFiles", regularTestFile_2));
startCluster(ClusterFixture.builder(dirTestWatcher).maxParallelization(1));
test("alter session set `planner.enable_unnest_lateral`=%s", true);
ClusterFixtureBuilder builder = ClusterFixture.builder(dirTestWatcher)
.sessionOption(ExecConstants.ENABLE_UNNEST_LATERAL_KEY, true)
.maxParallelization(1);
startCluster(builder);
}

/***********************************************************************************************
@@ -97,7 +101,6 @@ public void testLateral_WithTopNInSubQuery() throws Exception {
/**
* Test which disables the TopN operator from planner settings before running query using SORT and LIMIT in
* subquery. The same query as in above test is executed and same result is expected.
* @throws Exception
*/
@Test
public void testLateral_WithSortAndLimitInSubQuery() throws Exception {
@@ -17,8 +17,11 @@
*/
package org.apache.drill.exec.physical.impl.lateraljoin;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import org.apache.drill.PlanTestBase;
@@ -499,4 +502,34 @@ public void testLateralAndUnnestExplainPlan() throws Exception {
assertTrue(matcher.find());
}
}

@Test
public void testUnnestTopN() throws Exception {
String query =
"select customer.c_custkey," +
"customer.c_name," +
"t.o.o_orderkey," +
"t.o.o_totalprice\n" +
"from dfs.`lateraljoin/multipleFiles` customer," +
"unnest(customer.c_orders) t(o)\n" +
"order by customer.c_custkey," +
"t.o.o_orderkey," +
"t.o.o_totalprice\n" +
"limit 50";

ClusterFixtureBuilder builder = ClusterFixture.builder(dirTestWatcher)
.setOptionDefault(ExecConstants.ENABLE_UNNEST_LATERAL_KEY, true);

try (ClusterFixture cluster = builder.build();
ClientFixture client = cluster.clientFixture()) {
String plan = client.queryBuilder()
.sql(query)
.explainText();

assertThat("Query plan doesn't contain TopN operator",
plan, containsString("TopN(limit=[50])"));
assertThat("Query plan shouldn't contain Sort operator",
plan, not(containsString("Sort")));
}
}
}

0 comments on commit 5c9303d

Please sign in to comment.