-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-23727][SQL] Support for pushing down filters for DateType in parquet #20851
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
Changes from 6 commits
079af71
59d8483
1f2b450
4df29df
8f36d1e
82c5a73
784b625
7946bea
423a938
a58eec8
b43e36c
b70def0
759f468
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 |
|---|---|---|
|
|
@@ -353,6 +353,13 @@ object SQLConf { | |
| .booleanConf | ||
| .createWithDefault(true) | ||
|
|
||
| val PARQUET_FILTER_PUSHDOWN_DATE_ENABLED = buildConf("spark.sql.parquet.filterPushdown.date") | ||
| .doc("If true, enables Parquet filter push-down optimization for Date. " + | ||
| "This configuration only has an effect when 'spark.sql.parquet.filterPushdown' is enabled.") | ||
| .internal() | ||
| .booleanConf | ||
| .createWithDefault(true) | ||
|
Member
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. For this kind of thing, we had better start with
Member
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. +1 |
||
|
|
||
| val PARQUET_WRITE_LEGACY_FORMAT = buildConf("spark.sql.parquet.writeLegacyFormat") | ||
| .doc("Whether to be compatible with the legacy Parquet format adopted by Spark 1.4 and prior " + | ||
| "versions, when converting Parquet schema to Spark SQL schema and vice versa.") | ||
|
|
@@ -1319,6 +1326,8 @@ class SQLConf extends Serializable with Logging { | |
|
|
||
| def parquetFilterPushDown: Boolean = getConf(PARQUET_FILTER_PUSHDOWN_ENABLED) | ||
|
|
||
| def parquetFilterPushDownDate: Boolean = getConf(PARQUET_FILTER_PUSHDOWN_DATE_ENABLED) | ||
|
|
||
| def orcFilterPushDown: Boolean = getConf(ORC_FILTER_PUSHDOWN_ENABLED) | ||
|
|
||
| def verifyPartitionPath: Boolean = getConf(HIVE_VERIFY_PARTITION_PATH) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,8 @@ import org.apache.parquet.filter2.predicate._ | |
| import org.apache.parquet.filter2.predicate.FilterApi._ | ||
| import org.apache.parquet.io.api.Binary | ||
|
|
||
| import org.apache.spark.sql.catalyst.util.DateTimeUtils | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.sources | ||
| import org.apache.spark.sql.types._ | ||
|
|
||
|
|
@@ -50,6 +52,12 @@ private[parquet] object ParquetFilters { | |
| (n: String, v: Any) => FilterApi.eq( | ||
| binaryColumn(n), | ||
| Option(v).map(b => Binary.fromReusedByteArray(v.asInstanceOf[Array[Byte]])).orNull) | ||
| case DateType if SQLConf.get.parquetFilterPushDownDate => | ||
| (n: String, v: Any) => FilterApi.eq( | ||
| intColumn(n), | ||
| Option(v).map { d => | ||
| DateTimeUtils.fromJavaDate(d.asInstanceOf[java.sql.Date]).asInstanceOf[Integer] | ||
|
Contributor
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. shall we respect session local timezone here?
Contributor
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. I think we should do |
||
| }.orNull) | ||
| } | ||
|
|
||
| private val makeNotEq: PartialFunction[DataType, (String, Any) => FilterPredicate] = { | ||
|
|
@@ -72,6 +80,12 @@ private[parquet] object ParquetFilters { | |
| (n: String, v: Any) => FilterApi.notEq( | ||
| binaryColumn(n), | ||
| Option(v).map(b => Binary.fromReusedByteArray(v.asInstanceOf[Array[Byte]])).orNull) | ||
| case DateType if SQLConf.get.parquetFilterPushDownDate => | ||
| (n: String, v: Any) => FilterApi.notEq( | ||
| intColumn(n), | ||
| Option(v).map { d => | ||
| DateTimeUtils.fromJavaDate(d.asInstanceOf[java.sql.Date]).asInstanceOf[Integer] | ||
| }.orNull) | ||
| } | ||
|
|
||
| private val makeLt: PartialFunction[DataType, (String, Any) => FilterPredicate] = { | ||
|
|
@@ -91,6 +105,12 @@ private[parquet] object ParquetFilters { | |
| case BinaryType => | ||
| (n: String, v: Any) => | ||
| FilterApi.lt(binaryColumn(n), Binary.fromReusedByteArray(v.asInstanceOf[Array[Byte]])) | ||
| case DateType if SQLConf.get.parquetFilterPushDownDate => | ||
| (n: String, v: Any) => FilterApi.lt( | ||
| intColumn(n), | ||
| Option(v).map { d => | ||
| DateTimeUtils.fromJavaDate(d.asInstanceOf[java.sql.Date]).asInstanceOf[Integer] | ||
| }.orNull) | ||
| } | ||
|
|
||
| private val makeLtEq: PartialFunction[DataType, (String, Any) => FilterPredicate] = { | ||
|
|
@@ -110,6 +130,12 @@ private[parquet] object ParquetFilters { | |
| case BinaryType => | ||
| (n: String, v: Any) => | ||
| FilterApi.ltEq(binaryColumn(n), Binary.fromReusedByteArray(v.asInstanceOf[Array[Byte]])) | ||
| case DateType if SQLConf.get.parquetFilterPushDownDate => | ||
| (n: String, v: Any) => FilterApi.ltEq( | ||
| intColumn(n), | ||
| Option(v).map { d => | ||
| DateTimeUtils.fromJavaDate(d.asInstanceOf[java.sql.Date]).asInstanceOf[Integer] | ||
| }.orNull) | ||
| } | ||
|
|
||
| private val makeGt: PartialFunction[DataType, (String, Any) => FilterPredicate] = { | ||
|
|
@@ -129,6 +155,12 @@ private[parquet] object ParquetFilters { | |
| case BinaryType => | ||
| (n: String, v: Any) => | ||
| FilterApi.gt(binaryColumn(n), Binary.fromReusedByteArray(v.asInstanceOf[Array[Byte]])) | ||
| case DateType if SQLConf.get.parquetFilterPushDownDate => | ||
|
Contributor
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. we should refactor it, so that adding a new data type doesn't need to touch so many places. This can be done later. |
||
| (n: String, v: Any) => FilterApi.gt( | ||
| intColumn(n), | ||
| Option(v).map { d => | ||
| DateTimeUtils.fromJavaDate(d.asInstanceOf[java.sql.Date]).asInstanceOf[Integer] | ||
| }.orNull) | ||
| } | ||
|
|
||
| private val makeGtEq: PartialFunction[DataType, (String, Any) => FilterPredicate] = { | ||
|
|
@@ -148,6 +180,12 @@ private[parquet] object ParquetFilters { | |
| case BinaryType => | ||
| (n: String, v: Any) => | ||
| FilterApi.gtEq(binaryColumn(n), Binary.fromReusedByteArray(v.asInstanceOf[Array[Byte]])) | ||
| case DateType if SQLConf.get.parquetFilterPushDownDate => | ||
| (n: String, v: Any) => FilterApi.gtEq( | ||
| intColumn(n), | ||
| Option(v).map { d => | ||
| DateTimeUtils.fromJavaDate(d.asInstanceOf[java.sql.Date]).asInstanceOf[Integer] | ||
| }.orNull) | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| package org.apache.spark.sql.execution.datasources.parquet | ||
|
|
||
| import java.nio.charset.StandardCharsets | ||
| import java.sql.Date | ||
|
|
||
| import org.apache.parquet.filter2.predicate.{FilterPredicate, Operators} | ||
| import org.apache.parquet.filter2.predicate.FilterApi._ | ||
|
|
@@ -76,8 +77,10 @@ class ParquetFilterSuite extends QueryTest with ParquetTest with SharedSQLContex | |
| expected: Seq[Row]): Unit = { | ||
| val output = predicate.collect { case a: Attribute => a }.distinct | ||
|
|
||
| withSQLConf(SQLConf.PARQUET_FILTER_PUSHDOWN_ENABLED.key -> "true") { | ||
| withSQLConf(SQLConf.PARQUET_VECTORIZED_READER_ENABLED.key -> "false") { | ||
| withSQLConf( | ||
| SQLConf.PARQUET_FILTER_PUSHDOWN_ENABLED.key -> "true", | ||
| SQLConf.PARQUET_FILTER_PUSHDOWN_DATE_ENABLED.key -> "true", | ||
| SQLConf.PARQUET_VECTORIZED_READER_ENABLED.key -> "false") { | ||
| val query = df | ||
| .select(output.map(e => Column(e)): _*) | ||
| .where(Column(predicate)) | ||
|
|
@@ -102,7 +105,6 @@ class ParquetFilterSuite extends QueryTest with ParquetTest with SharedSQLContex | |
| maybeFilter.exists(_.getClass === filterClass) | ||
| } | ||
| checker(stripSparkFilter(query), expected) | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -313,6 +315,36 @@ class ParquetFilterSuite extends QueryTest with ParquetTest with SharedSQLContex | |
| } | ||
| } | ||
|
|
||
| test("filter pushdown - date") { | ||
| implicit class IntToDate(int: Int) { | ||
|
Member
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. Shall we pass a string here and convert it into a date?
Author
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. How about this way? It is from ORC's date push down.
Member
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. Either way looks fine but I usually stick to what other codes do around the fix though ..
Author
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. Ok, do you mean this way? Looks like we need more words :).
Member
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. I think
Member
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. Yup, I think this one is better than the current one.
Author
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. As per @cloud-fan 's suggestion, I have changed 1.d to 1.date, which one is perferred?
Contributor
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. the string one is more readable to me |
||
| def d: Date = new Date(Date.valueOf("2018-03-01").getTime + 24 * 60 * 60 * 1000 * (int - 1)) | ||
| } | ||
|
|
||
| withParquetDataFrame((1 to 4).map(i => Tuple1(i.d))) { implicit df => | ||
|
Member
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. These test cases only cover the limited cases. We need to check the boundary values
Author
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. Could you kindly give me some examples about what kind of boundary tests? I checked parquet integer push down and ORC date type push down, seems like have covered all their tests. |
||
| checkFilterPredicate('_1.isNull, classOf[Eq[_]], Seq.empty[Row]) | ||
| checkFilterPredicate('_1.isNotNull, classOf[NotEq[_]], (1 to 4).map(i => Row.apply(i.d))) | ||
|
|
||
| checkFilterPredicate('_1 === 1.d, classOf[Eq[_]], 1.d) | ||
|
Contributor
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.
Author
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. I agree 1.date is better, but binary is using "1.b", shall we keep the same pattern with it?
Contributor
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.
Author
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. Got it, thanks very much for explanation. |
||
| checkFilterPredicate('_1 <=> 1.d, classOf[Eq[_]], 1.d) | ||
| checkFilterPredicate('_1 =!= 1.d, classOf[NotEq[_]], (2 to 4).map(i => Row.apply(i.d))) | ||
|
|
||
| checkFilterPredicate('_1 < 2.d, classOf[Lt[_]], 1.d) | ||
| checkFilterPredicate('_1 > 3.d, classOf[Gt[_]], 4.d) | ||
| checkFilterPredicate('_1 <= 1.d, classOf[LtEq[_]], 1.d) | ||
| checkFilterPredicate('_1 >= 4.d, classOf[GtEq[_]], 4.d) | ||
|
|
||
| checkFilterPredicate(Literal(1.d) === '_1, classOf[Eq[_]], 1.d) | ||
| checkFilterPredicate(Literal(1.d) <=> '_1, classOf[Eq[_]], 1.d) | ||
| checkFilterPredicate(Literal(2.d) > '_1, classOf[Lt[_]], 1.d) | ||
| checkFilterPredicate(Literal(3.d) < '_1, classOf[Gt[_]], 4.d) | ||
| checkFilterPredicate(Literal(1.d) >= '_1, classOf[LtEq[_]], 1.d) | ||
| checkFilterPredicate(Literal(4.d) <= '_1, classOf[GtEq[_]], 4.d) | ||
|
|
||
| checkFilterPredicate(!('_1 < 4.d), classOf[GtEq[_]], 4.d) | ||
| checkFilterPredicate('_1 < 2.d || '_1 > 3.d, classOf[Operators.Or], Seq(Row(1.d), Row(4.d))) | ||
| } | ||
| } | ||
|
|
||
| test("SPARK-6554: don't push down predicates which reference partition columns") { | ||
| import testImplicits._ | ||
|
|
||
|
|
||
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.
Should be an internal conf, i.e.,
.internal()?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.
@gatorsmile any idea?
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.
Yes, it should be an internal conf. In Spark 3.0 release, we will revisit all the internal confs and remove all the unnecessary confs.