huaxingao commented on code in PR #16082:
URL: https://github.com/apache/iceberg/pull/16082#discussion_r3384367724


##########
spark/v4.1/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/expressions/iceberg/IcebergToSparkExpression.scala:
##########
@@ -0,0 +1,149 @@
+/*
+ * 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.catalyst.expressions.iceberg
+
+import org.apache.iceberg.expressions.And
+import org.apache.iceberg.expressions.Expression
+import org.apache.iceberg.expressions.False
+import org.apache.iceberg.expressions.NamedReference
+import org.apache.iceberg.expressions.Not
+import org.apache.iceberg.expressions.Or
+import org.apache.iceberg.expressions.True
+import org.apache.iceberg.expressions.UnboundPredicate
+import org.apache.spark.sql.catalyst.expressions.{And => CAnd}
+import org.apache.spark.sql.catalyst.expressions.{Expression => 
CatalystExpression}
+import org.apache.spark.sql.catalyst.expressions.{Literal => CLiteral}
+import org.apache.spark.sql.catalyst.expressions.{Not => CNot}
+import org.apache.spark.sql.catalyst.expressions.{Or => COr}
+import org.apache.spark.sql.catalyst.expressions.Attribute
+import org.apache.spark.sql.catalyst.expressions.EqualTo
+import org.apache.spark.sql.catalyst.expressions.GreaterThan
+import org.apache.spark.sql.catalyst.expressions.GreaterThanOrEqual
+import org.apache.spark.sql.catalyst.expressions.In
+import org.apache.spark.sql.catalyst.expressions.IsNaN
+import org.apache.spark.sql.catalyst.expressions.IsNotNull
+import org.apache.spark.sql.catalyst.expressions.IsNull
+import org.apache.spark.sql.catalyst.expressions.LessThan
+import org.apache.spark.sql.catalyst.expressions.LessThanOrEqual
+import org.apache.spark.sql.catalyst.expressions.StartsWith
+import org.apache.spark.unsafe.types.UTF8String
+import scala.jdk.CollectionConverters._
+
+/**
+ * Convert an Iceberg unbound boolean [[Expression]] to a Catalyst expression 
resolved against
+ * the supplied attribute set. Each [[NamedReference]] in the Iceberg 
expression is matched by
+ * name against one of the supplied attributes, preserving that attribute's 
`ExprId` so
+ * downstream references remain resolved.
+ *
+ * Conversion covers the subset defined by the REST OpenAPI `Expression` 
schema: True/False,
+ * And/Or/Not, unary predicates (is-null/not-null/is-nan/not-nan), literal 
predicates
+ * (eq/not-eq/lt/lt-eq/gt/gt-eq/starts-with/not-starts-with), and set 
predicates (in/not-in).
+ */
+object IcebergToSparkExpression {
+
+  def convert(expr: Expression, output: Seq[Attribute]): CatalystExpression = {
+    val byName: Map[String, Attribute] = output.map(a => a.name -> a).toMap
+    toCatalyst(expr, byName)
+  }
+
+  private def toCatalyst(expr: Expression, byName: Map[String, Attribute]): 
CatalystExpression = {

Review Comment:
   Agree on direct conversion. Because resolveRef returns the original 
Attribute object from the supplied output, every field on it including its 
ExprId is unchanged, so the resulting Catalyst expression binds into the 
resolved plan in one pass. DSv2 NamedReference is string-only, so a routed path 
would have to redo the same lookup later.   
   
   One concern on literalValueToCatalyst: it misses BigDecimal → Decimal and 
date/timestamp coercion, maybe use  
CatalystTypeConverters.convertToCatalyst(value).



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to