parthchandra commented on code in PR #2100:
URL: https://github.com/apache/datafusion-comet/pull/2100#discussion_r2264165976


##########
spark/src/main/scala/org/apache/comet/parquet/CometNativeFilters.scala:
##########
@@ -0,0 +1,211 @@
+/*
+ * 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.comet.parquet
+
+import java.io.ByteArrayOutputStream
+
+import scala.collection.JavaConverters._
+
+import org.apache.spark.sql.sources
+import org.apache.spark.sql.types.StructType
+
+import org.apache.comet.parquet.SourceFilterSerde.{createBinaryExpr, 
createNameExpr, createUnaryExpr, createValueExpr}
+import org.apache.comet.serde.ExprOuterClass
+import org.apache.comet.serde.QueryPlanSerde.scalarFunctionExprToProto
+
+/**
+ * Utility class for creating native filters for Comet's native reader. This 
functionality is
+ * Comet-specific and not available in Spark's native ParquetFilters.
+ *
+ * The native filters are serialized into protobuf format and passed to 
Comet's native Rust reader
+ * for efficient filter evaluation.
+ */
+object CometNativeFilters {
+
+  /**
+   * Creates native filters from Spark Filter objects for Comet's native 
reader.
+   *
+   * @param predicates
+   *   Sequence of Spark Filter objects
+   * @param dataSchema
+   *   The data schema for field resolution
+   * @param nameToParquetField
+   *   Map of field names to parquet field info (currently unused but kept for 
API compatibility)
+   * @return
+   *   Optional byte array containing serialized protobuf filters
+   */
+  def createNativeFilters(
+      predicates: Seq[sources.Filter],
+      dataSchema: StructType,
+      nameToParquetField: Map[String, Any]): Option[Array[Byte]] = {
+    predicates.reduceOption(sources.And).flatMap(createNativeFilter(_, 
dataSchema)).map { expr =>
+      val outputStream = new ByteArrayOutputStream()
+      expr.writeTo(outputStream)
+      outputStream.close()
+      outputStream.toByteArray
+    }
+  }
+
+  /**
+   * Converts a single Spark Filter to a native protobuf expression.
+   */
+  private def createNativeFilter(
+      predicate: sources.Filter,
+      dataSchema: StructType): Option[ExprOuterClass.Expr] = {
+
+    def nameUnaryExpr(name: String)(
+        f: (ExprOuterClass.Expr.Builder, ExprOuterClass.UnaryExpr) => 
ExprOuterClass.Expr.Builder)
+        : Option[ExprOuterClass.Expr] = {
+      createNameExpr(name, dataSchema).map { case (_, childExpr) =>
+        createUnaryExpr(childExpr, f)
+      }
+    }
+
+    def nameValueBinaryExpr(name: String, value: Any)(
+        f: (
+            ExprOuterClass.Expr.Builder,
+            ExprOuterClass.BinaryExpr) => ExprOuterClass.Expr.Builder)
+        : Option[ExprOuterClass.Expr] = {
+      createNameExpr(name, dataSchema).flatMap { case (dataType, childExpr) =>
+        createValueExpr(value, dataType).map(createBinaryExpr(childExpr, _, f))
+      }
+    }
+
+    predicate match {
+      case sources.IsNull(name) =>

Review Comment:
   Looks like `canMakeFilterOn` was a private method in `ParquetFilters` and 
there is an identical implementation in Spark's `ParquetFilters`



-- 
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

Reply via email to