Repository: drill Updated Branches: refs/heads/master 03197d0f2 -> 9ff947288
DRILL-4279: Improve performance for skipAll query against Text/JSON/Parquet table. Project: http://git-wip-us.apache.org/repos/asf/drill/repo Commit: http://git-wip-us.apache.org/repos/asf/drill/commit/9ff94728 Tree: http://git-wip-us.apache.org/repos/asf/drill/tree/9ff94728 Diff: http://git-wip-us.apache.org/repos/asf/drill/diff/9ff94728 Branch: refs/heads/master Commit: 9ff947288f3214fe8e525e001d89a4f91b8b0728 Parents: 9b4008d Author: Jinfeng Ni <[email protected]> Authored: Tue Jan 19 16:47:08 2016 -0800 Committer: Jinfeng Ni <[email protected]> Committed: Fri Jan 29 19:30:27 2016 -0800 ---------------------------------------------------------------------- .../apache/drill/exec/store/hive/HiveScan.java | 3 +- .../drill/exec/TestHiveProjectPushDown.java | 8 + .../drill/exec/planner/logical/ColumnList.java | 200 ------------------- .../exec/planner/logical/DrillScanRel.java | 9 +- .../drill/exec/store/AbstractRecordReader.java | 38 +++- .../exec/store/easy/json/JSONRecordReader.java | 6 + .../compliant/CompliantTextRecordReader.java | 6 +- .../columnreaders/ParquetRecordReader.java | 11 + .../exec/store/text/DrillTextRecordReader.java | 5 + .../org/apache/drill/TestProjectPushDown.java | 55 +++++ .../drill/exec/store/avro/AvroFormatTest.java | 9 + 11 files changed, 132 insertions(+), 218 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/drill/blob/9ff94728/contrib/storage-hive/core/src/main/java/org/apache/drill/exec/store/hive/HiveScan.java ---------------------------------------------------------------------- diff --git a/contrib/storage-hive/core/src/main/java/org/apache/drill/exec/store/hive/HiveScan.java b/contrib/storage-hive/core/src/main/java/org/apache/drill/exec/store/hive/HiveScan.java index 504d755..1a58cbd 100644 --- a/contrib/storage-hive/core/src/main/java/org/apache/drill/exec/store/hive/HiveScan.java +++ b/contrib/storage-hive/core/src/main/java/org/apache/drill/exec/store/hive/HiveScan.java @@ -240,7 +240,8 @@ public class HiveScan extends AbstractGroupScan { Table hiveTable = hiveReadEntry.getTable(); projectedColumnCount = hiveTable.getSd().getColsSize() + hiveTable.getPartitionKeysSize(); } else { - projectedColumnCount = columns.size(); + // In cost estimation, # of project columns should be >= 1, even for skipAll query. + projectedColumnCount = Math.max(columns.size(), 1); } return projectedColumnCount * HIVE_SERDE_SCAN_OVERHEAD_FACTOR_PER_COLUMN; http://git-wip-us.apache.org/repos/asf/drill/blob/9ff94728/contrib/storage-hive/core/src/test/java/org/apache/drill/exec/TestHiveProjectPushDown.java ---------------------------------------------------------------------- diff --git a/contrib/storage-hive/core/src/test/java/org/apache/drill/exec/TestHiveProjectPushDown.java b/contrib/storage-hive/core/src/test/java/org/apache/drill/exec/TestHiveProjectPushDown.java index 5f559ea..ce99961 100644 --- a/contrib/storage-hive/core/src/test/java/org/apache/drill/exec/TestHiveProjectPushDown.java +++ b/contrib/storage-hive/core/src/test/java/org/apache/drill/exec/TestHiveProjectPushDown.java @@ -98,6 +98,14 @@ public class TestHiveProjectPushDown extends HiveTestBase { } @Test + public void testHiveCountStar() throws Exception { + String query = "SELECT count(*) as cnt FROM hive.`default`.kv"; + String expectedColNames = "\"columns\" : [ ]"; + + testHelper(query, 1, expectedColNames); + } + + @Test public void projectPushDownOnHiveParquetTable() throws Exception { try { test(String.format("alter session set `%s` = true", ExecConstants.HIVE_OPTIMIZE_SCAN_WITH_NATIVE_READERS)); http://git-wip-us.apache.org/repos/asf/drill/blob/9ff94728/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/ColumnList.java ---------------------------------------------------------------------- diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/ColumnList.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/ColumnList.java deleted file mode 100644 index e448bc5..0000000 --- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/ColumnList.java +++ /dev/null @@ -1,200 +0,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. - */ -package org.apache.drill.exec.planner.logical; - -import java.util.Collection; -import java.util.Iterator; -import java.util.List; -import java.util.ListIterator; - -import com.google.common.base.Objects; -import com.google.common.base.Preconditions; -import org.apache.drill.common.expression.SchemaPath; -import org.apache.drill.exec.physical.base.GroupScan; - -/** - * A list decorator that wraps a list of columns along with a {@link org.apache.drill.exec.planner.logical.ColumnList.Mode mode} - * that informs {@link org.apache.drill.exec.store.AbstractRecordReader readers} to scan or skip the underlying list of columns. - */ -public class ColumnList implements List<SchemaPath> { - - public static enum Mode { - SKIP_ALL, - SCAN_ALL, - SCAN_SOME - } - - private final List<SchemaPath> backend; - private final Mode mode; - - protected ColumnList(List<SchemaPath> backend, Mode mode) { - this.backend = Preconditions.checkNotNull(backend); - this.mode = Preconditions.checkNotNull(mode); - } - - public Mode getMode() { - return mode; - } - - public static ColumnList none() { - return new ColumnList(GroupScan.ALL_COLUMNS, Mode.SKIP_ALL); - } - - public static ColumnList all() { - return new ColumnList(GroupScan.ALL_COLUMNS, Mode.SCAN_ALL); - } - - public static ColumnList some(List<SchemaPath> columns) { - return new ColumnList(columns, Mode.SCAN_SOME); - } - - - @Override - public int size() { - return backend.size(); - } - - @Override - public boolean isEmpty() { - return backend.isEmpty(); - } - - @Override - public boolean contains(Object o) { - return backend.contains(o); - } - - @Override - public Iterator<SchemaPath> iterator() { - return backend.iterator(); - } - - @Override - public Object[] toArray() { - return backend.toArray(); - } - - @Override - public <T> T[] toArray(T[] a) { - return backend.toArray(a); - } - - @Override - public boolean add(SchemaPath path) { - return backend.add(path); - } - - @Override - public boolean remove(Object o) { - return backend.remove(o); - } - - @Override - public boolean containsAll(Collection<?> c) { - return backend.containsAll(c); - } - - @Override - public boolean addAll(Collection<? extends SchemaPath> c) { - return backend.addAll(c); - } - - @Override - public boolean addAll(int index, Collection<? extends SchemaPath> c) { - return backend.addAll(index, c); - } - - @Override - public boolean removeAll(Collection<?> c) { - return backend.removeAll(c); - } - - @Override - public boolean retainAll(Collection<?> c) { - return backend.retainAll(c); - } - - @Override - public void clear() { - backend.clear(); - } - - @Override - public boolean equals(Object o) { - if (o instanceof ColumnList) { - final ColumnList other = ColumnList.class.cast(o); - return backend.equals(other) && Objects.equal(mode, other.mode); - } - return false; - } - - @Override - public int hashCode() { - return Objects.hashCode(backend, mode); - } - - @Override - public SchemaPath get(int index) { - return backend.get(index); - } - - @Override - public SchemaPath set(int index, SchemaPath element) { - return backend.set(index, element); - } - - @Override - public void add(int index, SchemaPath element) { - backend.add(index, element); - } - - @Override - public SchemaPath remove(int index) { - return backend.remove(index); - } - - @Override - public int indexOf(Object o) { - return backend.indexOf(o); - } - - @Override - public int lastIndexOf(Object o) { - return backend.lastIndexOf(o); - } - - @Override - public ListIterator<SchemaPath> listIterator() { - return backend.listIterator(); - } - - @Override - public ListIterator<SchemaPath> listIterator(int index) { - return backend.listIterator(index); - } - - @Override - public List<SchemaPath> subList(int fromIndex, int toIndex) { - return backend.subList(fromIndex, toIndex); - } - - @Override - public String toString() { - return backend.toString(); - } -} http://git-wip-us.apache.org/repos/asf/drill/blob/9ff94728/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillScanRel.java ---------------------------------------------------------------------- diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillScanRel.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillScanRel.java index 0d087aa..94322d9 100644 --- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillScanRel.java +++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillScanRel.java @@ -82,13 +82,8 @@ public class DrillScanRel extends DrillScanRelBase implements DrillRel { super(DRILL_LOGICAL, cluster, traits, table); this.settings = PrelUtil.getPlannerSettings(cluster.getPlanner()); this.rowType = rowType; - if (columns == null) { // planner asks to scan all of the columns - this.columns = ColumnList.all(); - } else if (columns.size() == 0) { // planner asks to skip all of the columns - this.columns = ColumnList.none(); - } else { // planner asks to scan some columns - this.columns = ColumnList.some(columns); - } + Preconditions.checkNotNull(columns); + this.columns = columns; this.partitionFilterPushdown = partitionFilterPushdown; try { this.groupScan = drillTable.getGroupScan().clone(this.columns); http://git-wip-us.apache.org/repos/asf/drill/blob/9ff94728/exec/java-exec/src/main/java/org/apache/drill/exec/store/AbstractRecordReader.java ---------------------------------------------------------------------- diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/store/AbstractRecordReader.java b/exec/java-exec/src/main/java/org/apache/drill/exec/store/AbstractRecordReader.java index 2f1ca56..ca44d30 100644 --- a/exec/java-exec/src/main/java/org/apache/drill/exec/store/AbstractRecordReader.java +++ b/exec/java-exec/src/main/java/org/apache/drill/exec/store/AbstractRecordReader.java @@ -18,11 +18,14 @@ package org.apache.drill.exec.store; import java.util.Collection; +import java.util.List; import java.util.Map; +import com.google.common.collect.ImmutableList; +import org.apache.drill.common.expression.PathSegment; import org.apache.drill.common.expression.SchemaPath; import org.apache.drill.exec.exception.OutOfMemoryException; -import org.apache.drill.exec.planner.logical.ColumnList; +import org.apache.drill.exec.physical.base.GroupScan; import org.apache.drill.exec.record.MaterializedField.Key; import org.apache.drill.exec.vector.ValueVector; @@ -31,10 +34,14 @@ import com.google.common.base.Predicate; import com.google.common.collect.Iterables; public abstract class AbstractRecordReader implements RecordReader { + private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(AbstractRecordReader.class); + private static final String COL_NULL_ERROR = "Columns cannot be null. Use star column to select all fields."; - private static final String COL_EMPTY_ERROR = "Readers needs at least a column to read."; public static final SchemaPath STAR_COLUMN = SchemaPath.getSimplePath("*"); + // For text reader, the default columns to read is "columns[0]". + protected static final List<SchemaPath> DEFAULT_TEXT_COLS_TO_READ = ImmutableList.of(new SchemaPath(new PathSegment.NameSegment("columns", new PathSegment.ArraySegment(0)))); + private Collection<SchemaPath> columns = null; private boolean isStarQuery = false; private boolean isSkipQuery = false; @@ -47,14 +54,22 @@ public abstract class AbstractRecordReader implements RecordReader { + ", isSkipQuery = " + isSkipQuery + "]"; } - protected final void setColumns(Collection<SchemaPath> projected) { - assert Preconditions.checkNotNull(projected, COL_NULL_ERROR).size() > 0 : COL_EMPTY_ERROR; - if (projected instanceof ColumnList) { - final ColumnList columns = ColumnList.class.cast(projected); - isSkipQuery = columns.getMode() == ColumnList.Mode.SKIP_ALL; + protected final void setColumns(List<SchemaPath> projected) { + Preconditions.checkNotNull(projected, COL_NULL_ERROR); + isSkipQuery = projected.isEmpty(); + List<SchemaPath> columnsToRead = projected; + + // If no column is required (SkipQuery), by default it will use DEFAULT_COLS_TO_READ . + // Handling SkipQuery is storage-plugin specif : JSON, text reader, parquet will override, in order to + // improve query performance. + if (projected.isEmpty()) { + columnsToRead = getDefaultColumnsToRead(); } - isStarQuery = isStarQuery(projected); - columns = transformColumns(projected); + + isStarQuery = isStarQuery(columnsToRead); + columns = transformColumns(columnsToRead); + + logger.debug("columns to read : {}", columns); } protected Collection<SchemaPath> getColumns() { @@ -92,4 +107,9 @@ public abstract class AbstractRecordReader implements RecordReader { v.allocateNew(); } } + + protected List<SchemaPath> getDefaultColumnsToRead() { + return GroupScan.ALL_COLUMNS; + } + } http://git-wip-us.apache.org/repos/asf/drill/blob/9ff94728/exec/java-exec/src/main/java/org/apache/drill/exec/store/easy/json/JSONRecordReader.java ---------------------------------------------------------------------- diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/store/easy/json/JSONRecordReader.java b/exec/java-exec/src/main/java/org/apache/drill/exec/store/easy/json/JSONRecordReader.java index 1d22fed..8d82f78 100644 --- a/exec/java-exec/src/main/java/org/apache/drill/exec/store/easy/json/JSONRecordReader.java +++ b/exec/java-exec/src/main/java/org/apache/drill/exec/store/easy/json/JSONRecordReader.java @@ -21,6 +21,7 @@ import java.io.IOException; import java.io.InputStream; import java.util.List; +import com.google.common.collect.Lists; import org.apache.drill.common.exceptions.ExecutionSetupException; import org.apache.drill.common.exceptions.UserException; import org.apache.drill.common.expression.SchemaPath; @@ -28,6 +29,7 @@ import org.apache.drill.exec.ExecConstants; import org.apache.drill.exec.exception.OutOfMemoryException; import org.apache.drill.exec.ops.FragmentContext; import org.apache.drill.exec.ops.OperatorContext; +import org.apache.drill.exec.physical.base.GroupScan; import org.apache.drill.exec.physical.impl.OutputMutator; import org.apache.drill.exec.store.AbstractRecordReader; import org.apache.drill.exec.store.dfs.DrillFileSystem; @@ -140,6 +142,10 @@ public class JSONRecordReader extends AbstractRecordReader { } } + protected List<SchemaPath> getDefaultColumnsToRead() { + return ImmutableList.of(); + } + private void setupParser() throws IOException { if(hadoopPath != null){ jsonReader.setSource(stream); http://git-wip-us.apache.org/repos/asf/drill/blob/9ff94728/exec/java-exec/src/main/java/org/apache/drill/exec/store/easy/text/compliant/CompliantTextRecordReader.java ---------------------------------------------------------------------- diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/store/easy/text/compliant/CompliantTextRecordReader.java b/exec/java-exec/src/main/java/org/apache/drill/exec/store/easy/text/compliant/CompliantTextRecordReader.java index f6dab89..cf2359f 100644 --- a/exec/java-exec/src/main/java/org/apache/drill/exec/store/easy/text/compliant/CompliantTextRecordReader.java +++ b/exec/java-exec/src/main/java/org/apache/drill/exec/store/easy/text/compliant/CompliantTextRecordReader.java @@ -28,7 +28,6 @@ import java.util.Map; import javax.annotation.Nullable; -import org.apache.drill.common.exceptions.DrillRuntimeException; import org.apache.drill.common.exceptions.ExecutionSetupException; import org.apache.drill.common.exceptions.UserException; import org.apache.drill.common.expression.SchemaPath; @@ -90,6 +89,11 @@ public class CompliantTextRecordReader extends AbstractRecordReader { return super.isStarQuery(); } + @Override + protected List<SchemaPath> getDefaultColumnsToRead() { + return DEFAULT_TEXT_COLS_TO_READ; + } + /** * Performs the initial setup required for the record reader. * Initializes the input stream, handling of the output record batch http://git-wip-us.apache.org/repos/asf/drill/blob/9ff94728/exec/java-exec/src/main/java/org/apache/drill/exec/store/parquet/columnreaders/ParquetRecordReader.java ---------------------------------------------------------------------- diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/store/parquet/columnreaders/ParquetRecordReader.java b/exec/java-exec/src/main/java/org/apache/drill/exec/store/parquet/columnreaders/ParquetRecordReader.java index 7131b6c..61e05db 100644 --- a/exec/java-exec/src/main/java/org/apache/drill/exec/store/parquet/columnreaders/ParquetRecordReader.java +++ b/exec/java-exec/src/main/java/org/apache/drill/exec/store/parquet/columnreaders/ParquetRecordReader.java @@ -24,6 +24,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import com.google.common.collect.ImmutableList; import org.apache.drill.common.exceptions.DrillRuntimeException; import org.apache.drill.common.exceptions.ExecutionSetupException; import org.apache.drill.common.expression.SchemaPath; @@ -69,6 +70,10 @@ public class ParquetRecordReader extends AbstractRecordReader { private static final long DEFAULT_BATCH_LENGTH_IN_BITS = DEFAULT_BATCH_LENGTH * 8; // 256kb private static final char DEFAULT_RECORDS_TO_READ_IF_NOT_FIXED_WIDTH = 32*1024; + // When no column is required by the downstrea operator, ask SCAN to return a DEFAULT column. If such column does not exist, + // it will return as a nullable-int column. If that column happens to exist, return that column. + protected static final List<SchemaPath> DEFAULT_COLS_TO_READ = ImmutableList.of(SchemaPath.getSimplePath("_DEFAULT_COL_TO_READ_")); + // TODO - should probably find a smarter way to set this, currently 1 megabyte public static final int PARQUET_PAGE_MAX_SIZE = 1024 * 1024 * 1; @@ -508,4 +513,10 @@ public class ParquetRecordReader extends AbstractRecordReader { parquetReaderStats=null; } } + + @Override + protected List<SchemaPath> getDefaultColumnsToRead() { + return DEFAULT_COLS_TO_READ; + } + } http://git-wip-us.apache.org/repos/asf/drill/blob/9ff94728/exec/java-exec/src/main/java/org/apache/drill/exec/store/text/DrillTextRecordReader.java ---------------------------------------------------------------------- diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/store/text/DrillTextRecordReader.java b/exec/java-exec/src/main/java/org/apache/drill/exec/store/text/DrillTextRecordReader.java index bc675af..d97fc58 100644 --- a/exec/java-exec/src/main/java/org/apache/drill/exec/store/text/DrillTextRecordReader.java +++ b/exec/java-exec/src/main/java/org/apache/drill/exec/store/text/DrillTextRecordReader.java @@ -107,6 +107,11 @@ public class DrillTextRecordReader extends AbstractRecordReader { } @Override + protected List<SchemaPath> getDefaultColumnsToRead() { + return DEFAULT_TEXT_COLS_TO_READ; + } + + @Override public boolean isStarQuery() { return super.isStarQuery() || Iterables.tryFind(getColumns(), new Predicate<SchemaPath>() { private final SchemaPath COLUMNS = SchemaPath.getSimplePath("columns"); http://git-wip-us.apache.org/repos/asf/drill/blob/9ff94728/exec/java-exec/src/test/java/org/apache/drill/TestProjectPushDown.java ---------------------------------------------------------------------- diff --git a/exec/java-exec/src/test/java/org/apache/drill/TestProjectPushDown.java b/exec/java-exec/src/test/java/org/apache/drill/TestProjectPushDown.java index 60a6f49..d926422 100644 --- a/exec/java-exec/src/test/java/org/apache/drill/TestProjectPushDown.java +++ b/exec/java-exec/src/test/java/org/apache/drill/TestProjectPushDown.java @@ -209,6 +209,61 @@ public class TestProjectPushDown extends PlanTestBase { } @Test + public void testEmptyColProjectInTextScan() throws Exception { + final String sql = "SELECT count(*) cnt from cp.`store/text/data/d1/regions.csv`"; + final String expected = "\"columns\" : [ ]"; + // Verify plan + testPushDown(new PushDownTestInstance(sql, new String[] {expected})); + + // Verify execution result. + testBuilder() + .sqlQuery(sql) + .unOrdered() + .baselineColumns("cnt") + .baselineValues((long) 5) + .build() + .run(); + } + + @Test + public void testEmptyColProjectInJsonScan() throws Exception { + final String sql = "SELECT count(*) cnt from cp.`employee.json`"; + final String expected = "\"columns\" : [ ]"; + + testPushDown(new PushDownTestInstance(sql, new String[] {expected})); + + // Verify execution result. + testBuilder() + .sqlQuery(sql) + .unOrdered() + .baselineColumns("cnt") + .baselineValues((long) 1155) + .build() + .run(); + } + + @Test + public void testEmptyColProjectInParquetScan() throws Exception { + final String sql = "SELECT 1 + 1 as val from cp.`tpch/region.parquet`"; + final String expected = "\"columns\" : [ ]"; + + testPushDown(new PushDownTestInstance(sql, new String[] {expected})); + + // Verify execution result. + testBuilder() + .sqlQuery(sql) + .unOrdered() + .baselineColumns("val") + .baselineValues(2) + .baselineValues(2) + .baselineValues(2) + .baselineValues(2) + .baselineValues(2) + .build() + .run(); + } + + @Test public void testSimpleProjectPastJoinPastFilterPastJoinPushDown() throws Exception { // String sql = "select * " + // "from cp.`%s` t0, cp.`%s` t1, cp.`%s` t2 " + http://git-wip-us.apache.org/repos/asf/drill/blob/9ff94728/exec/java-exec/src/test/java/org/apache/drill/exec/store/avro/AvroFormatTest.java ---------------------------------------------------------------------- diff --git a/exec/java-exec/src/test/java/org/apache/drill/exec/store/avro/AvroFormatTest.java b/exec/java-exec/src/test/java/org/apache/drill/exec/store/avro/AvroFormatTest.java index d718342..ecf8369 100644 --- a/exec/java-exec/src/test/java/org/apache/drill/exec/store/avro/AvroFormatTest.java +++ b/exec/java-exec/src/test/java/org/apache/drill/exec/store/avro/AvroFormatTest.java @@ -262,4 +262,13 @@ public class AvroFormatTest extends BaseTestQuery { final String sql = "select * from dfs_test.`" + file + "`"; test(sql); } + + @Test + public void testCountStar() throws Exception { + + final String file = AvroTestUtil.generateStringAndUtf8Data(); + final String sql = "select count(*) from dfs_test.`" + file + "`"; + test(sql); + } + }
