viirya commented on code in PR #60746:
URL: https://github.com/apache/spark/pull/60746#discussion_r4645144184


##########
sql/catalyst/src/main/java/org/apache/spark/sql/connector/read/SupportsExternalLookup.java:
##########
@@ -100,9 +102,9 @@ public interface SupportsExternalLookup extends ScanBuilder 
{
    * example, with a streaming {@code s}:
    * <pre>{@code
    *   SELECT s.*, t.name FROM s JOIN t ON t.id = s.id
-   *   => selected [id = ?], then pruneColumns(<id, name>);
+   *   => selected [0], that is [id = ?], then pruneColumns(<id, name>);
    *      Spark keeps id because it evaluates id = ? on every returned row
    * }</pre>
    */
-  Predicate[] selectLookupPredicates(Predicate[] candidates);
+  int[] selectLookupPredicates(Predicate[] candidates);

Review Comment:
   Yes. This needs to reach master, branch-4.x and branch-4.4 before the 4.4.0 
RC, so 4.4.0 ships the `int[]` method. I'll make sure of that when merging, 
with a separate backport PR if the cherry-pick doesn't apply.



##########
common/utils/src/main/resources/error/error-conditions.json:
##########
@@ -2410,14 +2410,19 @@
       },
       "DUPLICATE_SELECTED_PREDICATE" : {
         "message" : [
-          "`selectLookupPredicates()` returned the predicate <predicate> more 
than once."
+          "`selectLookupPredicates()` returned the index <index>, of the 
predicate <predicate>, more than once."
         ]
       },
       "INVALID_INPUT_ORDINAL" : {
         "message" : [
           "The lookup result cursor returned the input ordinal <ordinal>, but 
the request had <numParameterRows> parameter rows."
         ]
       },
+      "INVALID_SELECTED_PREDICATE_INDEX" : {
+        "message" : [
+          "`selectLookupPredicates()` returned the index <index>, which is not 
a position in the <numCandidates> candidates offered by Spark."

Review Comment:
   Done, with your wording: "returned the index <index>, but the valid indexes 
are from 0 to <maxIndex>."



##########
sql/core/src/test/scala/org/apache/spark/sql/connector/ExternalLookupJoinSuite.scala:
##########
@@ -6230,21 +6234,23 @@ object SimpleExternalLookupTable extends Table with 
SupportsRead {
         SimpleExternalLookupDataSource.lookupCandidateDescriptions.set(
           candidates.map(_.toString).toSeq)
         val supportedPredicates = Set("=", "<=>", "<", "<=", ">", ">=")
-        val supported = candidates.filter { candidate =>
+        val supported = candidates.indices.filter { index =>

Review Comment:
   Done, it calls `positionsWhere` now.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/PrepareExternalLookupJoin.scala:
##########
@@ -390,24 +388,21 @@ object PrepareExternalLookupJoin extends 
Rule[LogicalPlan] with PredicateHelper
       throw QueryCompilationErrors.unsupportedExternalLookupJoinError(
         "NO_LOOKUP_PREDICATE", Map("table" -> toSQLFreeFormId(tableName)))
     }
-    val candidateByPredicate = new IdentityHashMap[V2Predicate, 
OfferedLookupPredicate]()
-    offered.zip(candidates).foreach { case (predicate, candidate) =>
-      candidateByPredicate.put(predicate, candidate)
-    }
-    val seen = Collections.newSetFromMap(new IdentityHashMap[V2Predicate, 
java.lang.Boolean]())
-    selected.toSeq.map { predicate =>
-      // A null element is not an offered instance either.
-      val candidate = Option(predicate).map(candidateByPredicate.get).orNull
-      if (candidate == null) {
+    val byIndex = candidates.toIndexedSeq
+    val seen = new java.util.BitSet(offered.length)

Review Comment:
   Done, `mutable.BitSet` with `if (!seen.add(index))`.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/PrepareExternalLookupJoin.scala:
##########
@@ -390,24 +388,21 @@ object PrepareExternalLookupJoin extends 
Rule[LogicalPlan] with PredicateHelper
       throw QueryCompilationErrors.unsupportedExternalLookupJoinError(
         "NO_LOOKUP_PREDICATE", Map("table" -> toSQLFreeFormId(tableName)))
     }
-    val candidateByPredicate = new IdentityHashMap[V2Predicate, 
OfferedLookupPredicate]()
-    offered.zip(candidates).foreach { case (predicate, candidate) =>
-      candidateByPredicate.put(predicate, candidate)
-    }
-    val seen = Collections.newSetFromMap(new IdentityHashMap[V2Predicate, 
java.lang.Boolean]())
-    selected.toSeq.map { predicate =>
-      // A null element is not an offered instance either.
-      val candidate = Option(predicate).map(candidateByPredicate.get).orNull
-      if (candidate == null) {
+    val byIndex = candidates.toIndexedSeq

Review Comment:
   Done in part. The caller now passes `offeredCandidates.toIndexedSeq`, the 
range check uses its length, and `byIndex` is gone. `offered` stays, because 
the check for [1] compares the connector's copy against it, and the duplicate 
message describes the predicate from it.



##########
sql/core/src/test/scala/org/apache/spark/sql/connector/ExternalLookupJoinSuite.scala:
##########
@@ -7192,6 +7199,13 @@ object CompactDecimalRows {
   }
 }
 
+/** Helpers for the test implementations of 
`SupportsExternalLookup.selectLookupPredicates`. */
+object ExternalLookupSelection {

Review Comment:
   Done, moved to `ExternalLookupSelection.scala` in this package.



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