-
Notifications
You must be signed in to change notification settings - Fork 28.5k
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
[SPARK-34960][SQL] Aggregate push down for ORC #34298
Changes from all commits
73aa26b
cca5a30
829cafb
c3e1a12
9b0a399
8c7c617
d85d4ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,7 +115,7 @@ case class StructType(fields: Array[StructField]) extends DataType with Seq[Stru | |
def names: Array[String] = fieldNames | ||
|
||
private lazy val fieldNamesSet: Set[String] = fieldNames.toSet | ||
private[sql] lazy val nameToField: Map[String, StructField] = fields.map(f => f.name -> f).toMap | ||
private lazy val nameToField: Map[String, StructField] = fields.map(f => f.name -> f).toMap | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This reverts the change in #33639, as we don't need to make it more public. |
||
private lazy val nameToIndex: Map[String, Int] = fieldNames.zipWithIndex.toMap | ||
|
||
override def equals(that: Any): Boolean = { | ||
|
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
* | (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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a few comments about how we store There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @viirya - sure, added some comments and an example. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thank you. |
||
|
||
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); | ||
} | ||
} |
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; | ||
} | ||
} |
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 = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this? Can't we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I see. |
||
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) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We support byte, short and double for MIN/MAX too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought to just use integer to represent all integer types (byte, short, int, long) and use float here to represent all float types (float and double), to be less verbose. We anyway will update Spark doc on website with more detailed explanation of this aggregate push down feature anyway (ideally a sheet).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. Let's have a detailed doc later on.