forked from Kyligence/spark
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-34960][SQL] Aggregate push down for ORC
### What changes were proposed in this pull request? This PR is to add aggregate push down feature for ORC data source v2 reader. At a high level, the PR does: * The supported aggregate expression is MIN/MAX/COUNT same as [Parquet aggregate push down](apache#33639). * BooleanType, ByteType, ShortType, IntegerType, LongType, FloatType, DoubleType, DateType are allowed in MIN/MAXX aggregate push down. All other columns types are not allowed in MIN/MAX aggregate push down. * All columns types are supported in COUNT aggregate push down. * Nested column's sub-fields are disallowed in aggregate push down. * If the file does not have valid statistics, Spark will throw exception and fail query. * If aggregate has filter or group-by column, aggregate will not be pushed down. At code level, the PR does: * `OrcScanBuilder`: `pushAggregation()` checks whether the aggregation can be pushed down. The most checking logic is shared between Parquet and ORC, extracted into `AggregatePushDownUtils.getSchemaForPushedAggregation()`. `OrcScanBuilder` will create a `OrcScan` with aggregation and aggregation data schema. * `OrcScan`: `createReaderFactory` creates a ORC reader factory with aggregation and schema. Similar change with `ParquetScan`. * `OrcPartitionReaderFactory`: `buildReaderWithAggregates` creates a ORC reader with aggregate push down (i.e. read ORC file footer to process columns statistics, instead of reading actual data in the file). `buildColumnarReaderWithAggregates` creates a columnar ORC reader similarly. Both delegate the real work to read footer in `OrcUtils.createAggInternalRowFromFooter`. * `OrcUtils.createAggInternalRowFromFooter`: reads ORC file footer to process columns statistics (real heavy lift happens here). Similar to `ParquetUtils.createAggInternalRowFromFooter`. Leverage utility method such as `OrcFooterReader.readStatistics`. * `OrcFooterReader`: `readStatistics` reads the ORC `ColumnStatistics[]` into Spark `OrcColumnStatistics`. The transformation is needed here, because ORC `ColumnStatistics[]` stores all columns statistics in a flatten array style, and hard to process. Spark `OrcColumnStatistics` stores the statistics in nested tree structure (e.g. like `StructType`). This is used by `OrcUtils.createAggInternalRowFromFooter` * `OrcColumnStatistics`: the easy-to-manipulate structure for ORC `ColumnStatistics`. This is used by `OrcFooterReader.readStatistics`. ### Why are the changes needed? To improve the performance of query with aggregate. ### Does this PR introduce _any_ user-facing change? Yes. A user-facing config `spark.sql.orc.aggregatePushdown` is added to control enabling/disabling the aggregate push down for ORC. By default the feature is disabled. ### How was this patch tested? Added unit test in `FileSourceAggregatePushDownSuite.scala`. Refactored all unit tests in apache#33639, and it now works for both Parquet and ORC. Closes apache#34298 from c21/orc-agg. Authored-by: Cheng Su <[email protected]> Signed-off-by: Liang-Chi Hsieh <[email protected]>
- Loading branch information
Showing
16 changed files
with
870 additions
and
322 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...ore/src/main/java/org/apache/spark/sql/execution/datasources/orc/OrcColumnStatistics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* 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.spark.sql.execution.datasources.orc; | ||
|
||
import org.apache.orc.ColumnStatistics; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Columns statistics interface wrapping ORC {@link ColumnStatistics}s. | ||
* | ||
* Because ORC {@link ColumnStatistics}s are stored as an flatten array in ORC file footer, | ||
* this class is used to covert ORC {@link ColumnStatistics}s from array to nested tree structure, | ||
* according to data types. The flatten array stores all data types (including nested types) in | ||
* tree pre-ordering. This is used for aggregate push down in ORC. | ||
* | ||
* For nested data types (array, map and struct), the sub-field statistics are stored recursively | ||
* inside parent column's children field. Here is an example of {@link OrcColumnStatistics}: | ||
* | ||
* Data schema: | ||
* c1: int | ||
* c2: struct<f1: int, f2: float> | ||
* c3: map<key: int, value: string> | ||
* c4: array<int> | ||
* | ||
* OrcColumnStatistics | ||
* | (children) | ||
* --------------------------------------------- | ||
* / | \ \ | ||
* c1 c2 c3 c4 | ||
* (integer) (struct) (map) (array) | ||
* (min:1, | (children) | (children) | (children) | ||
* max:10) ----- ----- element | ||
* / \ / \ (integer) | ||
* c2.f1 c2.f2 key value | ||
* (integer) (float) (integer) (string) | ||
* (min:0.1, (min:"a", | ||
* max:100.5) max:"zzz") | ||
*/ | ||
public class OrcColumnStatistics { | ||
private final ColumnStatistics statistics; | ||
private final List<OrcColumnStatistics> children; | ||
|
||
public OrcColumnStatistics(ColumnStatistics statistics) { | ||
this.statistics = statistics; | ||
this.children = new ArrayList<>(); | ||
} | ||
|
||
public ColumnStatistics getStatistics() { | ||
return statistics; | ||
} | ||
|
||
public OrcColumnStatistics get(int ordinal) { | ||
if (ordinal < 0 || ordinal >= children.size()) { | ||
throw new IndexOutOfBoundsException( | ||
String.format("Ordinal %d out of bounds of statistics size %d", ordinal, children.size())); | ||
} | ||
return children.get(ordinal); | ||
} | ||
|
||
public void add(OrcColumnStatistics newChild) { | ||
children.add(newChild); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
sql/core/src/main/java/org/apache/spark/sql/execution/datasources/orc/OrcFooterReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* 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.spark.sql.execution.datasources.orc; | ||
|
||
import org.apache.orc.ColumnStatistics; | ||
import org.apache.orc.Reader; | ||
import org.apache.orc.TypeDescription; | ||
import org.apache.spark.sql.types.*; | ||
|
||
import java.util.Arrays; | ||
import java.util.LinkedList; | ||
import java.util.Queue; | ||
|
||
/** | ||
* {@link OrcFooterReader} is a util class which encapsulates the helper | ||
* methods of reading ORC file footer. | ||
*/ | ||
public class OrcFooterReader { | ||
|
||
/** | ||
* Read the columns statistics from ORC file footer. | ||
* | ||
* @param orcReader the reader to read ORC file footer. | ||
* @return Statistics for all columns in the file. | ||
*/ | ||
public static OrcColumnStatistics readStatistics(Reader orcReader) { | ||
TypeDescription orcSchema = orcReader.getSchema(); | ||
ColumnStatistics[] orcStatistics = orcReader.getStatistics(); | ||
StructType sparkSchema = OrcUtils.toCatalystSchema(orcSchema); | ||
return convertStatistics(sparkSchema, new LinkedList<>(Arrays.asList(orcStatistics))); | ||
} | ||
|
||
/** | ||
* Convert a queue of ORC {@link ColumnStatistics}s into Spark {@link OrcColumnStatistics}. | ||
* The queue of ORC {@link ColumnStatistics}s are assumed to be ordered as tree pre-order. | ||
*/ | ||
private static OrcColumnStatistics convertStatistics( | ||
DataType sparkSchema, Queue<ColumnStatistics> orcStatistics) { | ||
OrcColumnStatistics statistics = new OrcColumnStatistics(orcStatistics.remove()); | ||
if (sparkSchema instanceof StructType) { | ||
for (StructField field : ((StructType) sparkSchema).fields()) { | ||
statistics.add(convertStatistics(field.dataType(), orcStatistics)); | ||
} | ||
} else if (sparkSchema instanceof MapType) { | ||
statistics.add(convertStatistics(((MapType) sparkSchema).keyType(), orcStatistics)); | ||
statistics.add(convertStatistics(((MapType) sparkSchema).valueType(), orcStatistics)); | ||
} else if (sparkSchema instanceof ArrayType) { | ||
statistics.add(convertStatistics(((ArrayType) sparkSchema).elementType(), orcStatistics)); | ||
} | ||
return statistics; | ||
} | ||
} |
141 changes: 141 additions & 0 deletions
141
...re/src/main/scala/org/apache/spark/sql/execution/datasources/AggregatePushDownUtils.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* | ||
* 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.spark.sql.execution.datasources | ||
|
||
import org.apache.spark.sql.catalyst.InternalRow | ||
import org.apache.spark.sql.catalyst.expressions.Expression | ||
import org.apache.spark.sql.connector.expressions.NamedReference | ||
import org.apache.spark.sql.connector.expressions.aggregate.{AggregateFunc, Aggregation, Count, CountStar, Max, Min} | ||
import org.apache.spark.sql.execution.RowToColumnConverter | ||
import org.apache.spark.sql.execution.vectorized.{OffHeapColumnVector, OnHeapColumnVector} | ||
import org.apache.spark.sql.types.{BooleanType, ByteType, DateType, DoubleType, FloatType, IntegerType, LongType, ShortType, StructField, StructType} | ||
import org.apache.spark.sql.vectorized.{ColumnarBatch, ColumnVector} | ||
|
||
/** | ||
* Utility class for aggregate push down to Parquet and ORC. | ||
*/ | ||
object AggregatePushDownUtils { | ||
|
||
/** | ||
* Get the data schema for aggregate to be pushed down. | ||
*/ | ||
def getSchemaForPushedAggregation( | ||
aggregation: Aggregation, | ||
schema: StructType, | ||
partitionNames: Set[String], | ||
dataFilters: Seq[Expression]): Option[StructType] = { | ||
|
||
var finalSchema = new StructType() | ||
|
||
def getStructFieldForCol(col: NamedReference): StructField = { | ||
schema.apply(col.fieldNames.head) | ||
} | ||
|
||
def isPartitionCol(col: NamedReference) = { | ||
partitionNames.contains(col.fieldNames.head) | ||
} | ||
|
||
def processMinOrMax(agg: AggregateFunc): Boolean = { | ||
val (column, aggType) = agg match { | ||
case max: Max => (max.column, "max") | ||
case min: Min => (min.column, "min") | ||
case _ => | ||
throw new IllegalArgumentException(s"Unexpected type of AggregateFunc ${agg.describe}") | ||
} | ||
|
||
if (isPartitionCol(column)) { | ||
// don't push down partition column, footer doesn't have max/min for partition column | ||
return false | ||
} | ||
val structField = getStructFieldForCol(column) | ||
|
||
structField.dataType match { | ||
// not push down complex type | ||
// not push down Timestamp because INT96 sort order is undefined, | ||
// Parquet doesn't return statistics for INT96 | ||
// not push down Parquet Binary because min/max could be truncated | ||
// (https://issues.apache.org/jira/browse/PARQUET-1685), Parquet Binary | ||
// could be Spark StringType, BinaryType or DecimalType. | ||
// not push down for ORC with same reason. | ||
case BooleanType | ByteType | ShortType | IntegerType | ||
| LongType | FloatType | DoubleType | DateType => | ||
finalSchema = finalSchema.add(structField.copy(s"$aggType(" + structField.name + ")")) | ||
true | ||
case _ => | ||
false | ||
} | ||
} | ||
|
||
if (aggregation.groupByColumns.nonEmpty || dataFilters.nonEmpty) { | ||
// Parquet/ORC footer has max/min/count for columns | ||
// e.g. SELECT COUNT(col1) FROM t | ||
// but footer doesn't have max/min/count for a column if max/min/count | ||
// are combined with filter or group by | ||
// e.g. SELECT COUNT(col1) FROM t WHERE col2 = 8 | ||
// SELECT COUNT(col1) FROM t GROUP BY col2 | ||
// However, if the filter is on partition column, max/min/count can still be pushed down | ||
// Todo: add support if groupby column is partition col | ||
// (https://issues.apache.org/jira/browse/SPARK-36646) | ||
return None | ||
} | ||
|
||
aggregation.aggregateExpressions.foreach { | ||
case max: Max => | ||
if (!processMinOrMax(max)) return None | ||
case min: Min => | ||
if (!processMinOrMax(min)) return None | ||
case count: Count => | ||
if (count.column.fieldNames.length != 1 || count.isDistinct) return None | ||
finalSchema = | ||
finalSchema.add(StructField(s"count(" + count.column.fieldNames.head + ")", LongType)) | ||
case _: CountStar => | ||
finalSchema = finalSchema.add(StructField("count(*)", LongType)) | ||
case _ => | ||
return None | ||
} | ||
|
||
Some(finalSchema) | ||
} | ||
|
||
/** | ||
* Check if two Aggregation `a` and `b` is equal or not. | ||
*/ | ||
def equivalentAggregations(a: Aggregation, b: Aggregation): Boolean = { | ||
a.aggregateExpressions.sortBy(_.hashCode()) | ||
.sameElements(b.aggregateExpressions.sortBy(_.hashCode())) && | ||
a.groupByColumns.sortBy(_.hashCode()).sameElements(b.groupByColumns.sortBy(_.hashCode())) | ||
} | ||
|
||
/** | ||
* Convert the aggregates result from `InternalRow` to `ColumnarBatch`. | ||
* This is used for columnar reader. | ||
*/ | ||
def convertAggregatesRowToBatch( | ||
aggregatesAsRow: InternalRow, | ||
aggregatesSchema: StructType, | ||
offHeap: Boolean): ColumnarBatch = { | ||
val converter = new RowToColumnConverter(aggregatesSchema) | ||
val columnVectors = if (offHeap) { | ||
OffHeapColumnVector.allocateColumns(1, aggregatesSchema) | ||
} else { | ||
OnHeapColumnVector.allocateColumns(1, aggregatesSchema) | ||
} | ||
converter.convert(aggregatesAsRow, columnVectors.toArray) | ||
new ColumnarBatch(columnVectors.asInstanceOf[Array[ColumnVector]], 1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.