-
Notifications
You must be signed in to change notification settings - Fork 29k
[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 1 commit
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 |
|---|---|---|
|
|
@@ -50,6 +50,15 @@ 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 => | ||
| (n: String, v: Any) => { | ||
| FilterApi.eq( | ||
| intColumn(n), | ||
| Option(v).map{ date => | ||
| val days = date.asInstanceOf[java.sql.Date].getTime / (24 * 60 * 60 * 1000) | ||
| days.toInt.asInstanceOf[Integer] | ||
|
||
| }.orNull) | ||
| } | ||
| } | ||
|
|
||
| private val makeNotEq: PartialFunction[DataType, (String, Any) => FilterPredicate] = { | ||
|
|
@@ -72,6 +81,15 @@ 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 => | ||
| (n: String, v: Any) => { | ||
|
||
| FilterApi.notEq( | ||
| intColumn(n), | ||
| Option(v).map{ date => | ||
| val days = date.asInstanceOf[java.sql.Date].getTime / (24 * 60 * 60 * 1000) | ||
| days.toInt.asInstanceOf[Integer] | ||
| }.orNull) | ||
| } | ||
| } | ||
|
|
||
| private val makeLt: PartialFunction[DataType, (String, Any) => FilterPredicate] = { | ||
|
|
@@ -91,6 +109,15 @@ private[parquet] object ParquetFilters { | |
| case BinaryType => | ||
| (n: String, v: Any) => | ||
| FilterApi.lt(binaryColumn(n), Binary.fromReusedByteArray(v.asInstanceOf[Array[Byte]])) | ||
| case DateType => | ||
| (n: String, v: Any) => { | ||
| FilterApi.lt( | ||
| intColumn(n), | ||
| Option(v).map{ date => | ||
| val days = date.asInstanceOf[java.sql.Date].getTime / (24 * 60 * 60 * 1000) | ||
| days.toInt.asInstanceOf[Integer] | ||
| }.orNull) | ||
| } | ||
| } | ||
|
|
||
| private val makeLtEq: PartialFunction[DataType, (String, Any) => FilterPredicate] = { | ||
|
|
@@ -110,6 +137,15 @@ private[parquet] object ParquetFilters { | |
| case BinaryType => | ||
| (n: String, v: Any) => | ||
| FilterApi.ltEq(binaryColumn(n), Binary.fromReusedByteArray(v.asInstanceOf[Array[Byte]])) | ||
| case DateType => | ||
| (n: String, v: Any) => { | ||
| FilterApi.ltEq( | ||
| intColumn(n), | ||
| Option(v).map{ date => | ||
| val days = date.asInstanceOf[java.sql.Date].getTime / (24 * 60 * 60 * 1000) | ||
| days.toInt.asInstanceOf[Integer] | ||
| }.orNull) | ||
| } | ||
| } | ||
|
|
||
| private val makeGt: PartialFunction[DataType, (String, Any) => FilterPredicate] = { | ||
|
|
@@ -129,6 +165,15 @@ private[parquet] object ParquetFilters { | |
| case BinaryType => | ||
| (n: String, v: Any) => | ||
| FilterApi.gt(binaryColumn(n), Binary.fromReusedByteArray(v.asInstanceOf[Array[Byte]])) | ||
| case DateType => | ||
| (n: String, v: Any) => { | ||
| FilterApi.gt( | ||
| intColumn(n), | ||
| Option(v).map{ date => | ||
| val days = date.asInstanceOf[java.sql.Date].getTime / (24 * 60 * 60 * 1000) | ||
| days.toInt.asInstanceOf[Integer] | ||
| }.orNull) | ||
| } | ||
| } | ||
|
|
||
| private val makeGtEq: PartialFunction[DataType, (String, Any) => FilterPredicate] = { | ||
|
|
@@ -148,6 +193,15 @@ private[parquet] object ParquetFilters { | |
| case BinaryType => | ||
| (n: String, v: Any) => | ||
| FilterApi.gtEq(binaryColumn(n), Binary.fromReusedByteArray(v.asInstanceOf[Array[Byte]])) | ||
| case DateType => | ||
|
||
| (n: String, v: Any) => { | ||
| FilterApi.gtEq( | ||
| intColumn(n), | ||
| Option(v).map{ date => | ||
| val days = date.asInstanceOf[java.sql.Date].getTime / (24 * 60 * 60 * 1000) | ||
| days.toInt.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._ | ||
|
|
@@ -313,6 +314,36 @@ class ParquetFilterSuite extends QueryTest with ParquetTest with SharedSQLContex | |
| } | ||
| } | ||
|
|
||
| test("filter pushdown - date") { | ||
| implicit class IntToDate(int: Int) { | ||
|
||
| 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 => | ||
|
||
| 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) | ||
|
||
| 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.
nit:
p{->p {