forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 51
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
timestamps #72
Merged
Merged
timestamps #72
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ 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.sources | ||
import org.apache.spark.sql.types._ | ||
|
||
|
@@ -49,6 +50,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 TimestampType => | ||
(n: String, v: Any) => FilterApi.eq( | ||
longColumn(n), convertTimestamp(v.asInstanceOf[java.sql.Timestamp])) | ||
case DateType => | ||
(n: String, v: Any) => FilterApi.eq( | ||
intColumn(n), convertDate(v.asInstanceOf[java.sql.Date])) | ||
} | ||
|
||
private val makeNotEq: PartialFunction[DataType, (String, Any) => FilterPredicate] = { | ||
|
@@ -70,6 +77,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 TimestampType => | ||
(n: String, v: Any) => FilterApi.notEq( | ||
longColumn(n), convertTimestamp(v.asInstanceOf[java.sql.Timestamp])) | ||
case DateType => | ||
(n: String, v: Any) => FilterApi.notEq( | ||
intColumn(n), convertDate(v.asInstanceOf[java.sql.Date])) | ||
} | ||
|
||
private val makeLt: PartialFunction[DataType, (String, Any) => FilterPredicate] = { | ||
|
@@ -88,6 +101,12 @@ private[parquet] object ParquetFilters { | |
case BinaryType => | ||
(n: String, v: Any) => | ||
FilterApi.lt(binaryColumn(n), Binary.fromReusedByteArray(v.asInstanceOf[Array[Byte]])) | ||
case TimestampType => | ||
(n: String, v: Any) => FilterApi.lt( | ||
longColumn(n), convertTimestamp(v.asInstanceOf[java.sql.Timestamp])) | ||
case DateType => | ||
(n: String, v: Any) => FilterApi.lt( | ||
intColumn(n), convertDate(v.asInstanceOf[java.sql.Date])) | ||
} | ||
|
||
private val makeLtEq: PartialFunction[DataType, (String, Any) => FilterPredicate] = { | ||
|
@@ -106,6 +125,12 @@ private[parquet] object ParquetFilters { | |
case BinaryType => | ||
(n: String, v: Any) => | ||
FilterApi.ltEq(binaryColumn(n), Binary.fromReusedByteArray(v.asInstanceOf[Array[Byte]])) | ||
case TimestampType => | ||
(n: String, v: Any) => FilterApi.ltEq( | ||
longColumn(n), convertTimestamp(v.asInstanceOf[java.sql.Timestamp])) | ||
case DateType => | ||
(n: String, v: Any) => FilterApi.ltEq( | ||
intColumn(n), convertDate(v.asInstanceOf[java.sql.Date])) | ||
} | ||
|
||
private val makeGt: PartialFunction[DataType, (String, Any) => FilterPredicate] = { | ||
|
@@ -124,6 +149,12 @@ private[parquet] object ParquetFilters { | |
case BinaryType => | ||
(n: String, v: Any) => | ||
FilterApi.gt(binaryColumn(n), Binary.fromReusedByteArray(v.asInstanceOf[Array[Byte]])) | ||
case TimestampType => | ||
(n: String, v: Any) => FilterApi.gt( | ||
longColumn(n), convertTimestamp(v.asInstanceOf[java.sql.Timestamp])) | ||
case DateType => | ||
(n: String, v: Any) => FilterApi.gt( | ||
intColumn(n), convertDate(v.asInstanceOf[java.sql.Date])) | ||
} | ||
|
||
private val makeGtEq: PartialFunction[DataType, (String, Any) => FilterPredicate] = { | ||
|
@@ -142,6 +173,28 @@ private[parquet] object ParquetFilters { | |
case BinaryType => | ||
(n: String, v: Any) => | ||
FilterApi.gtEq(binaryColumn(n), Binary.fromReusedByteArray(v.asInstanceOf[Array[Byte]])) | ||
case TimestampType => | ||
(n: String, v: Any) => FilterApi.gtEq( | ||
longColumn(n), convertTimestamp(v.asInstanceOf[java.sql.Timestamp])) | ||
case DateType => | ||
(n: String, v: Any) => FilterApi.gtEq( | ||
intColumn(n), convertDate(v.asInstanceOf[java.sql.Date])) | ||
} | ||
|
||
private def convertDate(d: java.sql.Date): java.lang.Integer = { | ||
if (d != null) { | ||
DateTimeUtils.fromJavaDate(d).asInstanceOf[java.lang.Integer] | ||
} else { | ||
null | ||
} | ||
} | ||
|
||
private def convertTimestamp(t: java.sql.Timestamp): java.lang.Long = { | ||
if (t != null) { | ||
DateTimeUtils.fromJavaTimestamp(t).asInstanceOf[java.lang.Long] | ||
} else { | ||
null | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -153,23 +206,32 @@ private[parquet] object ParquetFilters { | |
* using such fields, otherwise Parquet library will throw exception (PARQUET-389). | ||
* Here we filter out such fields. | ||
*/ | ||
private def getFieldMap(dataType: DataType): Map[String, DataType] = dataType match { | ||
case StructType(fields) => | ||
// Here we don't flatten the fields in the nested schema but just look up through | ||
// root fields. Currently, accessing to nested fields does not push down filters | ||
// and it does not support to create filters for them. | ||
fields.filter { f => | ||
!f.metadata.contains(StructType.metadataKeyForOptionalField) || | ||
!f.metadata.getBoolean(StructType.metadataKeyForOptionalField) | ||
}.map(f => f.name -> f.dataType).toMap | ||
case _ => Map.empty[String, DataType] | ||
} | ||
private def getFieldMap(dataType: DataType, int96AsTimestamp: Boolean): Map[String, DataType] = | ||
dataType match { | ||
case StructType(fields) => | ||
// Here we don't flatten the fields in the nested schema but just look up through | ||
// root fields. Currently, accessing to nested fields does not push down filters | ||
// and it does not support to create filters for them. | ||
// scalastyle:off println | ||
fields.filterNot { f => | ||
val isTs = DataTypes.TimestampType.acceptsType(f.dataType) | ||
|
||
val isOptionalField = f.metadata.contains(StructType.metadataKeyForOptionalField) && | ||
f.metadata.getBoolean(StructType.metadataKeyForOptionalField) | ||
|
||
(isTs && int96AsTimestamp) || isOptionalField | ||
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. nit: could be lazy with the boolean evaluations:
|
||
}.map(f => f.name -> f.dataType).toMap | ||
case _ => Map.empty[String, DataType] | ||
} | ||
|
||
/** | ||
* Converts data sources filters to Parquet filter predicates. | ||
*/ | ||
def createFilter(schema: StructType, predicate: sources.Filter): Option[FilterPredicate] = { | ||
val dataTypeOf = getFieldMap(schema) | ||
def createFilter( | ||
schema: StructType, | ||
predicate: sources.Filter, | ||
int96AsTimestamp: Boolean): Option[FilterPredicate] = { | ||
val dataTypeOf = getFieldMap(schema, int96AsTimestamp) | ||
|
||
// NOTE: | ||
// | ||
|
@@ -221,18 +283,20 @@ private[parquet] object ParquetFilters { | |
// Pushing one side of AND down is only safe to do at the top level. | ||
// You can see ParquetRelation's initializeLocalJobFunc method as an example. | ||
for { | ||
lhsFilter <- createFilter(schema, lhs) | ||
rhsFilter <- createFilter(schema, rhs) | ||
lhsFilter <- createFilter(schema, lhs, int96AsTimestamp) | ||
rhsFilter <- createFilter(schema, rhs, int96AsTimestamp) | ||
} yield FilterApi.and(lhsFilter, rhsFilter) | ||
|
||
case sources.Or(lhs, rhs) => | ||
for { | ||
lhsFilter <- createFilter(schema, lhs) | ||
rhsFilter <- createFilter(schema, rhs) | ||
lhsFilter <- createFilter(schema, lhs, int96AsTimestamp) | ||
rhsFilter <- createFilter(schema, rhs, int96AsTimestamp) | ||
} yield FilterApi.or(lhsFilter, rhsFilter) | ||
|
||
case sources.Not(pred) => | ||
createFilter(schema, pred).map(FilterApi.not).map(LogicalInverseRewriter.rewrite) | ||
createFilter(schema, pred, int96AsTimestamp) | ||
.map(FilterApi.not) | ||
.map(LogicalInverseRewriter.rewrite) | ||
|
||
case sources.In(name, values) if dataTypeOf.contains(name) => | ||
val eq = makeEq.lift(dataTypeOf(name)) | ||
|
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
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
maybe info? FilterCompat.get logs at that level