You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "mbutrovich (via GitHub)" <gi...@apache.org> on 2024/11/19 21:08:29 UTC

[PR] Use Parquet schema for scan instead of Spark schema [datafusion-comet]

mbutrovich opened a new pull request, #1103:
URL: https://github.com/apache/datafusion-comet/pull/1103

   Currently we get the scan schema from the plan nodes scan schema, and then serialize that back to a Parquet schema, then parse that on the native side. This is lossy, particularly with timestamps. For example:
   
   ```
   schema: message root {
     optional int64 _0 (TIMESTAMP(MILLIS,true));
     optional int64 _1 (TIMESTAMP(MICROS,true));
     optional int64 _2 (TIMESTAMP(MILLIS,true));
     optional int64 _3 (TIMESTAMP(MILLIS,false));
     optional int64 _4 (TIMESTAMP(MICROS,true));
     optional int64 _5 (TIMESTAMP(MICROS,false));
     optional int64 _6 (INTEGER(64,true));
   }
   
   dataSchema: message spark_schema {
     optional int96 _0;
     optional int96 _1;
     optional int96 _2;
     optional int64 _3 (TIMESTAMP(MICROS,false));
     optional int96 _4;
     optional int64 _5 (TIMESTAMP(MICROS,false));
     optional int64 _6;
   }
   ```
   The former is the original Parquet footer, the latter is what we get after going through Spark. We need the original to handle int96 correctly in ParquetExec.
   
   This PR extracts some code from elsewhere (CometParquetFileFormat, CometNativeScanExec) to read the footer from the Parquet file, and serialize the original metadata. We also now generate the projection vector on the Spark side because the required columns is in Spark schema format, so will not match the Parquet schema 1:1. On the native side, we now have to regenerate the required schema from the Parquet schema using the projection vector (converted to a DF ProjectionMask).


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] [comet-parquet-exec] Use Parquet schema for scan instead of Spark schema [datafusion-comet]

Posted by "parthchandra (via GitHub)" <gi...@apache.org>.
parthchandra commented on code in PR #1103:
URL: https://github.com/apache/datafusion-comet/pull/1103#discussion_r1852568916


##########
spark/src/main/scala/org/apache/comet/serde/QueryPlanSerde.scala:
##########
@@ -3191,9 +3191,25 @@ object QueryPlanSerde extends Logging with ShimQueryPlanSerde with CometExprShim
 
   private def partition2Proto(
       partition: FilePartition,
-      nativeScanBuilder: OperatorOuterClass.NativeScan.Builder): Unit = {
+      nativeScanBuilder: OperatorOuterClass.NativeScan.Builder,
+      scan: CometScanExec): Unit = {
     val partitionBuilder = OperatorOuterClass.SparkFilePartition.newBuilder()
+    val sparkContext = scan.session.sparkContext
+    var schema_saved: Boolean = false;
     partition.files.foreach(file => {
+      if (!schema_saved) {
+        // TODO: This code shouldn't be here, but for POC it's fine.
+        // Extract the schema and stash it.
+        val hadoopConf =
+          scan.relation.sparkSession.sessionState.newHadoopConfWithOptions(scan.relation.options)
+        val broadcastedHadoopConf =
+          sparkContext.broadcast(new SerializableConfiguration(hadoopConf))
+        val sharedConf = broadcastedHadoopConf.value.value
+        val footer = FooterReader.readFooter(sharedConf, file)

Review Comment:
   Ah, thanks. I don't think we ever travel this path. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] [comet-parquet-exec] Use Parquet schema for scan instead of Spark schema [datafusion-comet]

Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #1103:
URL: https://github.com/apache/datafusion-comet/pull/1103#discussion_r1850931678


##########
spark/src/main/scala/org/apache/comet/serde/QueryPlanSerde.scala:
##########
@@ -2507,23 +2508,22 @@ object QueryPlanSerde extends Logging with ShimQueryPlanSerde with CometExprShim
               partitions.foreach(p => {
                 val inputPartitions = p.asInstanceOf[DataSourceRDDPartition].inputPartitions
                 inputPartitions.foreach(partition => {
-                  partition2Proto(partition.asInstanceOf[FilePartition], nativeScanBuilder)
+                  partition2Proto(partition.asInstanceOf[FilePartition], nativeScanBuilder, scan)
                 })
               })
             case rdd: FileScanRDD =>
               rdd.filePartitions.foreach(partition => {
-                partition2Proto(partition, nativeScanBuilder)
+                partition2Proto(partition, nativeScanBuilder, scan)
               })
             case _ =>
+              assert(false)
           }
 
-          val requiredSchemaParquet =
-            new SparkToParquetSchemaConverter(conf).convert(scan.requiredSchema)
-          val dataSchemaParquet =
-            new SparkToParquetSchemaConverter(conf).convert(scan.relation.dataSchema)
+          val projection_vector: Array[java.lang.Long] = scan.requiredSchema.fields.map(field => {

Review Comment:
   If you just need Arrow types, can you just convert Spark types to Arrow types? For example, if the column in Spark is treated as timestamp type, its Arrow type is timestamp too.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] [comet-parquet-exec] Use Parquet schema for scan instead of Spark schema [datafusion-comet]

Posted by "mbutrovich (via GitHub)" <gi...@apache.org>.
mbutrovich closed pull request #1103: [comet-parquet-exec] Use Parquet schema for scan instead of Spark schema
URL: https://github.com/apache/datafusion-comet/pull/1103


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] [comet-parquet-exec] Use Parquet schema for scan instead of Spark schema [datafusion-comet]

Posted by "mbutrovich (via GitHub)" <gi...@apache.org>.
mbutrovich commented on code in PR #1103:
URL: https://github.com/apache/datafusion-comet/pull/1103#discussion_r1850288853


##########
spark/src/main/scala/org/apache/comet/serde/QueryPlanSerde.scala:
##########
@@ -3191,9 +3191,25 @@ object QueryPlanSerde extends Logging with ShimQueryPlanSerde with CometExprShim
 
   private def partition2Proto(
       partition: FilePartition,
-      nativeScanBuilder: OperatorOuterClass.NativeScan.Builder): Unit = {
+      nativeScanBuilder: OperatorOuterClass.NativeScan.Builder,
+      scan: CometScanExec): Unit = {
     val partitionBuilder = OperatorOuterClass.SparkFilePartition.newBuilder()
+    val sparkContext = scan.session.sparkContext
+    var schema_saved: Boolean = false;
     partition.files.foreach(file => {
+      if (!schema_saved) {
+        // TODO: This code shouldn't be here, but for POC it's fine.
+        // Extract the schema and stash it.
+        val hadoopConf =
+          scan.relation.sparkSession.sessionState.newHadoopConfWithOptions(scan.relation.options)
+        val broadcastedHadoopConf =
+          sparkContext.broadcast(new SerializableConfiguration(hadoopConf))
+        val sharedConf = broadcastedHadoopConf.value.value
+        val footer = FooterReader.readFooter(sharedConf, file)

Review Comment:
   In theory it's just replacing the call that currently takes place during the `fileReader` instantiation, but yeah I'm still curious if it's already cached somewhere. I see references within Spark to a `footersCache` so I'm curious to look for that as well.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] [comet-parquet-exec] Use Parquet schema for scan instead of Spark schema [datafusion-comet]

Posted by "mbutrovich (via GitHub)" <gi...@apache.org>.
mbutrovich commented on code in PR #1103:
URL: https://github.com/apache/datafusion-comet/pull/1103#discussion_r1850288853


##########
spark/src/main/scala/org/apache/comet/serde/QueryPlanSerde.scala:
##########
@@ -3191,9 +3191,25 @@ object QueryPlanSerde extends Logging with ShimQueryPlanSerde with CometExprShim
 
   private def partition2Proto(
       partition: FilePartition,
-      nativeScanBuilder: OperatorOuterClass.NativeScan.Builder): Unit = {
+      nativeScanBuilder: OperatorOuterClass.NativeScan.Builder,
+      scan: CometScanExec): Unit = {
     val partitionBuilder = OperatorOuterClass.SparkFilePartition.newBuilder()
+    val sparkContext = scan.session.sparkContext
+    var schema_saved: Boolean = false;
     partition.files.foreach(file => {
+      if (!schema_saved) {
+        // TODO: This code shouldn't be here, but for POC it's fine.
+        // Extract the schema and stash it.
+        val hadoopConf =
+          scan.relation.sparkSession.sessionState.newHadoopConfWithOptions(scan.relation.options)
+        val broadcastedHadoopConf =
+          sparkContext.broadcast(new SerializableConfiguration(hadoopConf))
+        val sharedConf = broadcastedHadoopConf.value.value
+        val footer = FooterReader.readFooter(sharedConf, file)

Review Comment:
   In theory it's just replacing the call that currently takes place during the file_reader instantiation, but yeah I'm still curious if it's already cached somewhere. I see references within Spark to a `footersCache` so I'm curious to look for that as well.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] [comet-parquet-exec] Use Parquet schema for scan instead of Spark schema [datafusion-comet]

Posted by "mbutrovich (via GitHub)" <gi...@apache.org>.
mbutrovich commented on code in PR #1103:
URL: https://github.com/apache/datafusion-comet/pull/1103#discussion_r1850297863


##########
spark/src/main/scala/org/apache/comet/serde/QueryPlanSerde.scala:
##########
@@ -2507,23 +2508,22 @@ object QueryPlanSerde extends Logging with ShimQueryPlanSerde with CometExprShim
               partitions.foreach(p => {
                 val inputPartitions = p.asInstanceOf[DataSourceRDDPartition].inputPartitions
                 inputPartitions.foreach(partition => {
-                  partition2Proto(partition.asInstanceOf[FilePartition], nativeScanBuilder)
+                  partition2Proto(partition.asInstanceOf[FilePartition], nativeScanBuilder, scan)
                 })
               })
             case rdd: FileScanRDD =>
               rdd.filePartitions.foreach(partition => {
-                partition2Proto(partition, nativeScanBuilder)
+                partition2Proto(partition, nativeScanBuilder, scan)
               })
             case _ =>
+              assert(false)
           }
 
-          val requiredSchemaParquet =
-            new SparkToParquetSchemaConverter(conf).convert(scan.requiredSchema)
-          val dataSchemaParquet =
-            new SparkToParquetSchemaConverter(conf).convert(scan.relation.dataSchema)
+          val projection_vector: Array[java.lang.Long] = scan.requiredSchema.fields.map(field => {

Review Comment:
   The `SchemaConverter` seems like it could be handled in DF's `SchemaAdapter`. I'll look at `clipParquetSchema` as well, thanks!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] [comet-parquet-exec] Use Parquet schema for scan instead of Spark schema [datafusion-comet]

Posted by "mbutrovich (via GitHub)" <gi...@apache.org>.
mbutrovich commented on code in PR #1103:
URL: https://github.com/apache/datafusion-comet/pull/1103#discussion_r1852256631


##########
spark/src/main/scala/org/apache/comet/serde/QueryPlanSerde.scala:
##########
@@ -3191,9 +3191,25 @@ object QueryPlanSerde extends Logging with ShimQueryPlanSerde with CometExprShim
 
   private def partition2Proto(
       partition: FilePartition,
-      nativeScanBuilder: OperatorOuterClass.NativeScan.Builder): Unit = {
+      nativeScanBuilder: OperatorOuterClass.NativeScan.Builder,
+      scan: CometScanExec): Unit = {
     val partitionBuilder = OperatorOuterClass.SparkFilePartition.newBuilder()
+    val sparkContext = scan.session.sparkContext
+    var schema_saved: Boolean = false;
     partition.files.foreach(file => {
+      if (!schema_saved) {
+        // TODO: This code shouldn't be here, but for POC it's fine.
+        // Extract the schema and stash it.
+        val hadoopConf =
+          scan.relation.sparkSession.sessionState.newHadoopConfWithOptions(scan.relation.options)
+        val broadcastedHadoopConf =
+          sparkContext.broadcast(new SerializableConfiguration(hadoopConf))
+        val sharedConf = broadcastedHadoopConf.value.value
+        val footer = FooterReader.readFooter(sharedConf, file)

Review Comment:
   It's actually part of parquet-hadoop: https://github.com/apache/parquet-java/blob/1e04ec74060a04cb029b6e61cf1098c1aba1446e/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ParquetInputFormat.java#L525



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] [comet-parquet-exec] Use Parquet schema for scan instead of Spark schema [datafusion-comet]

Posted by "parthchandra (via GitHub)" <gi...@apache.org>.
parthchandra commented on code in PR #1103:
URL: https://github.com/apache/datafusion-comet/pull/1103#discussion_r1851245300


##########
spark/src/main/scala/org/apache/comet/serde/QueryPlanSerde.scala:
##########
@@ -3191,9 +3191,25 @@ object QueryPlanSerde extends Logging with ShimQueryPlanSerde with CometExprShim
 
   private def partition2Proto(
       partition: FilePartition,
-      nativeScanBuilder: OperatorOuterClass.NativeScan.Builder): Unit = {
+      nativeScanBuilder: OperatorOuterClass.NativeScan.Builder,
+      scan: CometScanExec): Unit = {
     val partitionBuilder = OperatorOuterClass.SparkFilePartition.newBuilder()
+    val sparkContext = scan.session.sparkContext
+    var schema_saved: Boolean = false;
     partition.files.foreach(file => {
+      if (!schema_saved) {
+        // TODO: This code shouldn't be here, but for POC it's fine.
+        // Extract the schema and stash it.
+        val hadoopConf =
+          scan.relation.sparkSession.sessionState.newHadoopConfWithOptions(scan.relation.options)
+        val broadcastedHadoopConf =
+          sparkContext.broadcast(new SerializableConfiguration(hadoopConf))
+        val sharedConf = broadcastedHadoopConf.value.value
+        val footer = FooterReader.readFooter(sharedConf, file)

Review Comment:
   I didn't know about a footersCache in Spark. Could you share a link maybe?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] [comet-parquet-exec] Use Parquet schema for scan instead of Spark schema [datafusion-comet]

Posted by "parthchandra (via GitHub)" <gi...@apache.org>.
parthchandra commented on code in PR #1103:
URL: https://github.com/apache/datafusion-comet/pull/1103#discussion_r1849161016


##########
spark/src/main/scala/org/apache/comet/serde/QueryPlanSerde.scala:
##########
@@ -2507,23 +2508,22 @@ object QueryPlanSerde extends Logging with ShimQueryPlanSerde with CometExprShim
               partitions.foreach(p => {
                 val inputPartitions = p.asInstanceOf[DataSourceRDDPartition].inputPartitions
                 inputPartitions.foreach(partition => {
-                  partition2Proto(partition.asInstanceOf[FilePartition], nativeScanBuilder)
+                  partition2Proto(partition.asInstanceOf[FilePartition], nativeScanBuilder, scan)
                 })
               })
             case rdd: FileScanRDD =>
               rdd.filePartitions.foreach(partition => {
-                partition2Proto(partition, nativeScanBuilder)
+                partition2Proto(partition, nativeScanBuilder, scan)
               })
             case _ =>
+              assert(false)
           }
 
-          val requiredSchemaParquet =
-            new SparkToParquetSchemaConverter(conf).convert(scan.requiredSchema)
-          val dataSchemaParquet =
-            new SparkToParquetSchemaConverter(conf).convert(scan.relation.dataSchema)
+          val projection_vector: Array[java.lang.Long] = scan.requiredSchema.fields.map(field => {

Review Comment:
   This change essentially means that any schema 'adaptation' made in `SparkToParquetSchemaConverter.convert` to support legacy timestamps and decimals will not be supported. But we will probably fail tests with incorrect results.
   Also, Comet's Parquet file reader uses `CometParquetReadSupport.clipParquetSchema` to do similar conversion and it includes support for Parquet [field_id](https://github.com/apache/parquet-format/blob/c70281359087dfaee8bd43bed9748675f4aabe11/src/main/thrift/parquet.thrift#L473) which is desirable for delta sources like Iceberg.  
   Basically a field_id, if present, identifies a field more precisely (in the event of field name changes) in a schema.



##########
spark/src/main/scala/org/apache/comet/serde/QueryPlanSerde.scala:
##########
@@ -3191,9 +3191,25 @@ object QueryPlanSerde extends Logging with ShimQueryPlanSerde with CometExprShim
 
   private def partition2Proto(
       partition: FilePartition,
-      nativeScanBuilder: OperatorOuterClass.NativeScan.Builder): Unit = {
+      nativeScanBuilder: OperatorOuterClass.NativeScan.Builder,
+      scan: CometScanExec): Unit = {
     val partitionBuilder = OperatorOuterClass.SparkFilePartition.newBuilder()
+    val sparkContext = scan.session.sparkContext
+    var schema_saved: Boolean = false;
     partition.files.foreach(file => {
+      if (!schema_saved) {
+        // TODO: This code shouldn't be here, but for POC it's fine.
+        // Extract the schema and stash it.
+        val hadoopConf =
+          scan.relation.sparkSession.sessionState.newHadoopConfWithOptions(scan.relation.options)
+        val broadcastedHadoopConf =
+          sparkContext.broadcast(new SerializableConfiguration(hadoopConf))
+        val sharedConf = broadcastedHadoopConf.value.value
+        val footer = FooterReader.readFooter(sharedConf, file)

Review Comment:
   You're right. This can never be in production code. For one, this is expensive. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] [comet-parquet-exec] Use Parquet schema for scan instead of Spark schema [datafusion-comet]

Posted by "mbutrovich (via GitHub)" <gi...@apache.org>.
mbutrovich commented on code in PR #1103:
URL: https://github.com/apache/datafusion-comet/pull/1103#discussion_r1850995050


##########
spark/src/main/scala/org/apache/comet/serde/QueryPlanSerde.scala:
##########
@@ -2507,23 +2508,22 @@ object QueryPlanSerde extends Logging with ShimQueryPlanSerde with CometExprShim
               partitions.foreach(p => {
                 val inputPartitions = p.asInstanceOf[DataSourceRDDPartition].inputPartitions
                 inputPartitions.foreach(partition => {
-                  partition2Proto(partition.asInstanceOf[FilePartition], nativeScanBuilder)
+                  partition2Proto(partition.asInstanceOf[FilePartition], nativeScanBuilder, scan)
                 })
               })
             case rdd: FileScanRDD =>
               rdd.filePartitions.foreach(partition => {
-                partition2Proto(partition, nativeScanBuilder)
+                partition2Proto(partition, nativeScanBuilder, scan)
               })
             case _ =>
+              assert(false)
           }
 
-          val requiredSchemaParquet =
-            new SparkToParquetSchemaConverter(conf).convert(scan.requiredSchema)
-          val dataSchemaParquet =
-            new SparkToParquetSchemaConverter(conf).convert(scan.relation.dataSchema)
+          val projection_vector: Array[java.lang.Long] = scan.requiredSchema.fields.map(field => {

Review Comment:
   My concern is that...
   
   1. Java side parses Parquet metadata, generates a Spark schema
   2. Java side converts Spark schema to Arrow schema (following Comet conversion rules)
   3. Serialize Arrow types, native side feeds this into ParquetExec as the data schema
   
   ...may yield different results than:
   
   1. Java side serializes original Parquet metadata
   2. Serialize schema message
   3. Native side parses message, generates Arrow schema and feeds this into ParquetExec as the data schema
   
   I guess I could exhaustively test this hypothesis with all types.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org