uros-b commented on code in PR #57395:
URL: https://github.com/apache/spark/pull/57395#discussion_r3621562629


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/columnar/InMemoryRelation.scala:
##########
@@ -197,8 +197,11 @@ class DefaultCachedBatchSerializer extends 
SimpleMetricsCachedBatchSerializer {
       conf: SQLConf): RDD[ColumnarBatch] = {
     val offHeapColumnVectorEnabled = conf.offHeapColumnVectorEnabled
     val outputSchema = DataTypeUtils.fromAttributes(selectedAttributes)
+    // Build a single exprId -> ordinal map so each selected attribute is a 
constant-time lookup,
+    // instead of rebuilding the exprId list and linear-scanning it per 
selected attribute.
+    val cacheAttributeOrdinals = 
cacheAttributes.iterator.map(_.exprId).zipWithIndex.toMap

Review Comment:
   Catalyst already provides the exact utility this PR hand-rolls: 
`AttributeSeq.indexOf(exprId: ExprId)` 
(catalyst/expressions/package.scala:110-133) is a @transient lazy exprId -> 
ordinal HashMap that returns -1 on miss, and it is already in scope here via 
import org.apache.spark.sql.catalyst.expressions._.
   
   Please consider reusing it, e.g.
   ```
   val cacheAttrSeq = AttributeSeq(cacheAttributes); selectedAttributes.map(a 
=> cacheAttrSeq.indexOf(a.exprId)))
   ```



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/columnar/InMemoryRelation.scala:
##########
@@ -230,10 +233,13 @@ class DefaultCachedBatchSerializer extends 
SimpleMetricsCachedBatchSerializer {
       cacheAttributes: Seq[Attribute],
       selectedAttributes: Seq[Attribute],
       conf: SQLConf): RDD[InternalRow] = {
-    // Find the ordinals and data types of the requested columns.
+    // Find the ordinals and data types of the requested columns. Build a 
single
+    // exprId -> ordinal map so each requested column is a constant-time 
lookup, instead of
+    // rebuilding the exprId list and linear-scanning it per requested column.
+    val cacheAttributeOrdinals = 
cacheAttributes.iterator.map(_.exprId).zipWithIndex.toMap

Review Comment:
   Same as https://github.com/apache/spark/pull/57395/changes#r3621562629.



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