kazuyukitanimura commented on code in PR #678: URL: https://github.com/apache/datafusion-comet/pull/678#discussion_r1681803041
########## dev/diffs/4.0.0-preview1.diff: ########## @@ -938,16 +938,42 @@ index 68f14f13bbd..4b8e967102f 100644 } assert(exchanges.size === 1) } -@@ -2668,7 +2675,8 @@ class SubquerySuite extends QueryTest - } - } - -- test("SPARK-43402: FileSourceScanExec supports push down data filter with scalar subquery") { -+ test("SPARK-43402: FileSourceScanExec supports push down data filter with scalar subquery", -+ IgnoreComet("TODO: https://github.com/apache/datafusion-comet/issues/551")) { +@@ -2672,18 +2679,26 @@ class SubquerySuite extends QueryTest def checkFileSourceScan(query: String, answer: Seq[Row]): Unit = { val df = sql(query) checkAnswer(df, answer) +- val fileSourceScanExec = collect(df.queryExecution.executedPlan) { +- case f: FileSourceScanExec => f +- } + sparkContext.listenerBus.waitUntilEmpty() Review Comment: hmmm it as ``` val fileSourceScanExec = sparkContext.listenerBus.waitUntilEmpty() assert(fileSourceScanExec.size === 1) ``` Does the order matter? Not sure. ########## spark/src/main/spark-3.4/org/apache/spark/sql/comet/shims/ShimCometScanExec.scala: ########## @@ -102,4 +102,11 @@ trait ShimCometScanExec { maxSplitBytes: Long, partitionValues: InternalRow): Seq[PartitionedFile] = PartitionedFileUtil.splitFiles(sparkSession, file, filePath, isSplitable, maxSplitBytes, partitionValues) + + protected def isFileSourceConstantMetadataAttribute(attr: Attribute): Boolean = { + attr.getClass.getName match { + case " org.apache.spark.sql.catalyst.expressions.FileSourceConstantMetadataAttribute" => true Review Comment: I think for Spark 3.4+ we can do a real class match instead of String? ########## spark/src/main/spark-3.3/org/apache/spark/sql/comet/shims/ShimCometScanExec.scala: ########## @@ -0,0 +1,110 @@ +/* + * 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.comet.shims + +import org.apache.comet.shims.ShimFileFormat +import org.apache.hadoop.fs.{FileStatus, Path} +import org.apache.spark.SparkException +import org.apache.spark.sql.SparkSession +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeReference} +import org.apache.spark.sql.execution.datasources.parquet.ParquetOptions +import org.apache.spark.sql.execution.datasources._ +import org.apache.spark.sql.execution.{FileSourceScanExec, PartitionedFileUtil} +import org.apache.spark.sql.types.StructType + +trait ShimCometScanExec { + def wrapped: FileSourceScanExec + + // TODO: remove after dropping Spark 3.3 support + lazy val metadataColumns: Seq[AttributeReference] = wrapped.getClass.getDeclaredMethods + .filter(_.getName == "metadataColumns") + .map { a => a.setAccessible(true); a } + .flatMap(_.invoke(wrapped).asInstanceOf[Seq[AttributeReference]]) + + // TODO: remove after dropping Spark 3.3 support and directly call + // wrapped.fileConstantMetadataColumns + lazy val fileConstantMetadataColumns: Seq[AttributeReference] = + wrapped.getClass.getDeclaredMethods + .filter(_.getName == "fileConstantMetadataColumns") + .map { a => a.setAccessible(true); a } + .flatMap(_.invoke(wrapped).asInstanceOf[Seq[AttributeReference]]) + + // TODO: remove after dropping Spark 3.3 and 3.4 support and directly call new FileScanRDD + protected def newFileScanRDD( + fsRelation: HadoopFsRelation, + readFunction: PartitionedFile => Iterator[InternalRow], + filePartitions: Seq[FilePartition], + readSchema: StructType, + options: ParquetOptions): FileScanRDD = + classOf[FileScanRDD].getDeclaredConstructors + // Prevent to pick up incorrect constructors from any custom Spark forks. + .filter(c => List(5, 6).contains(c.getParameterCount())) + .map { c => + c.getParameterCount match { + case 5 => + c.newInstance(fsRelation.sparkSession, readFunction, filePartitions, readSchema, metadataColumns) + case 6 => + c.newInstance( + fsRelation.sparkSession, + readFunction, + filePartitions, + readSchema, + fileConstantMetadataColumns, + options) + } + } + .last + .asInstanceOf[FileScanRDD] + + // TODO: remove after dropping Spark 3.3 support and directly call + // QueryExecutionErrors.SparkException + protected def invalidBucketFile(path: String, sparkVersion: String): Throwable = { + val messageParameters = if (sparkVersion >= "3.4") Map("path" -> path) else Array(path) Review Comment: (Optional) This can be optimized as well like `if (sparkVersion >= "3.4")` ########## spark/src/main/scala/org/apache/spark/sql/comet/CometScanExec.scala: ########## @@ -94,7 +95,7 @@ case class CometScanExec( val startTime = System.nanoTime() val ret = relation.location.listFiles(partitionFilters.filterNot(isDynamicPruningFilter), dataFilters) - setFilesNumAndSizeMetric(ret, true) + setFilesNumAndSizeMetric(collection.immutable.Seq(ret: _*), true) Review Comment: Hmm what would happen if we do not do this? ########## spark/src/main/spark-3.3/org/apache/spark/sql/comet/shims/ShimCometScanExec.scala: ########## @@ -0,0 +1,110 @@ +/* + * 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.comet.shims + +import org.apache.comet.shims.ShimFileFormat +import org.apache.hadoop.fs.{FileStatus, Path} +import org.apache.spark.SparkException +import org.apache.spark.sql.SparkSession +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeReference} +import org.apache.spark.sql.execution.datasources.parquet.ParquetOptions +import org.apache.spark.sql.execution.datasources._ +import org.apache.spark.sql.execution.{FileSourceScanExec, PartitionedFileUtil} +import org.apache.spark.sql.types.StructType + +trait ShimCometScanExec { + def wrapped: FileSourceScanExec + + // TODO: remove after dropping Spark 3.3 support + lazy val metadataColumns: Seq[AttributeReference] = wrapped.getClass.getDeclaredMethods + .filter(_.getName == "metadataColumns") + .map { a => a.setAccessible(true); a } + .flatMap(_.invoke(wrapped).asInstanceOf[Seq[AttributeReference]]) + + // TODO: remove after dropping Spark 3.3 support and directly call + // wrapped.fileConstantMetadataColumns + lazy val fileConstantMetadataColumns: Seq[AttributeReference] = + wrapped.getClass.getDeclaredMethods + .filter(_.getName == "fileConstantMetadataColumns") + .map { a => a.setAccessible(true); a } + .flatMap(_.invoke(wrapped).asInstanceOf[Seq[AttributeReference]]) + + // TODO: remove after dropping Spark 3.3 and 3.4 support and directly call new FileScanRDD + protected def newFileScanRDD( + fsRelation: HadoopFsRelation, + readFunction: PartitionedFile => Iterator[InternalRow], + filePartitions: Seq[FilePartition], + readSchema: StructType, + options: ParquetOptions): FileScanRDD = + classOf[FileScanRDD].getDeclaredConstructors + // Prevent to pick up incorrect constructors from any custom Spark forks. + .filter(c => List(5, 6).contains(c.getParameterCount())) + .map { c => + c.getParameterCount match { + case 5 => + c.newInstance(fsRelation.sparkSession, readFunction, filePartitions, readSchema, metadataColumns) + case 6 => + c.newInstance( + fsRelation.sparkSession, + readFunction, + filePartitions, + readSchema, + fileConstantMetadataColumns, + options) Review Comment: (Optional) I think we can remove this reflection because the argument is 5 always for Spark 3.3 ########## spark/src/main/scala/org/apache/spark/sql/comet/CometScanExec.scala: ########## @@ -134,10 +135,42 @@ case class CometScanExec( override lazy val (outputPartitioning, outputOrdering): (Partitioning, Seq[SortOrder]) = (wrapped.outputPartitioning, wrapped.outputOrdering) - @transient - private lazy val pushedDownFilters = { + def translateToV1Filters( + dataFilters: Seq[Expression], + scalarSubqueryToLiteral: ScalarSubquery => Literal): Seq[Filter] = { + val scalarSubqueryReplaced = dataFilters.map(_.transform { + // Replace scalar subquery to literal so that `DataSourceStrategy.translateFilter` can + // support translating it. + case scalarSubquery: ScalarSubquery => scalarSubqueryToLiteral(scalarSubquery) + }) + val supportNestedPredicatePushdown = DataSourceUtils.supportNestedPredicatePushdown(relation) - dataFilters.flatMap(DataSourceStrategy.translateFilter(_, supportNestedPredicatePushdown)) + // `dataFilters` should not include any constant metadata col filters + // because the metadata struct has been flatted in FileSourceStrategy + // and thus metadata col filters are invalid to be pushed down. Metadata that is generated + // during the scan can be used for filters. + scalarSubqueryReplaced + .filterNot(_.references.exists { + isFileSourceConstantMetadataAttribute + }) + .flatMap(DataSourceStrategy.translateFilter(_, supportNestedPredicatePushdown)) + } + + @transient + private lazy val pushedDownFilters = + translateToV1Filters(dataFilters, q => convertScalarSubqueryToLiteral(q)) Review Comment: Is it possible to define `pushedDownFilters` in Shims instead? We can do the old way for Spark 3.x For Spark 4.0, we can avoid the reflection like `convertScalarSubqueryToLiteral` -- 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-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org