dongjoon-hyun commented on code in PR #60746:
URL: https://github.com/apache/spark/pull/60746#discussion_r4644907400
##########
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:
**[4] Nit: this is `positionsWhere` written out by hand.** The other
filtering test connectors in this PR call the helper, which this file already
imports at L54. Could this be `positionsWhere(candidates) { candidate =>
supportedPredicates.contains(candidate.name()) && ... }`?
##########
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)
+ selected.toSeq.map { index =>
+ if (index < 0 || index >= offered.length) {
throw QueryCompilationErrors.invalidDataSourceExternalLookupError(
- tableName, "UNOFFERED_SELECTED_PREDICATE",
- Map("predicate" ->
Option(predicate).map(_.describe()).getOrElse("null")))
+ tableName, "INVALID_SELECTED_PREDICATE_INDEX",
+ Map("index" -> index.toString, "numCandidates" ->
offered.length.toString))
}
- if (!seen.add(predicate)) {
+ if (seen.get(index)) {
throw QueryCompilationErrors.invalidDataSourceExternalLookupError(
- tableName, "DUPLICATE_SELECTED_PREDICATE", Map("predicate" ->
predicate.describe()))
+ tableName, "DUPLICATE_SELECTED_PREDICATE",
+ Map("index" -> index.toString, "predicate" ->
offered(index).describe()))
}
- candidate
+ seen.set(index)
+ byIndex(index)
Review Comment:
**[1] The positions are read against Spark's own order, but the connector
receives the same array object.** L190 passes `offeredPredicates` itself to
`selectLookupPredicates`, and this line maps each position through `byIndex`,
which follows `offeredCandidates`, not the array that the connector saw. Take
the candidates `[tenant = ?, event_time >= ?]`. A connector that sorts the
array in place by field name, e.g. `Arrays.sort(candidates, byFieldName)`, sees
`[event_time >= ?, tenant = ?]` and returns `[1, 0]` for its `(tenant,
event_time)` key. Spark maps `[1, 0]` to its own order and binds every
parameter row as `(event_time, tenant)`. The source then misses rows that Spark
cannot recover, and no error is raised. The Javadoc says the array is read-only
(SupportsExternalLookup.java:93), but under identity matching such a reorder
was harmless, while with positions it becomes a silent wrong result. A null
written into the array also turns `offered(index).describe()` at L402 into an N
PE.
L190 is outside this diff, so I left this here. Could L190 pass
`offeredPredicates.clone()`, so that `offered` keeps Spark's order?
##########
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:
**[6] Nit: `offered` is now read only for its length and for `describe()` in
the duplicate message, and `byIndex` copies `candidates` into a new
`IndexedSeq`.** Could the caller pass `offeredCandidates.toIndexedSeq`, and the
range check use its length, so that one collection carries the positions?
##########
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:
**[7] Nit: this helper is now shared with
PrepareExternalLookupJoinSuite.scala, which imported nothing from this file
before.** It sits between `CompactDecimalRows` and `TestExternalLookup` in a
7.5k-line file, about 700 lines after its first use at L6516. Could it go into
its own small file in this package, so that both suites import it from an
obvious place?
##########
sql/catalyst/src/main/java/org/apache/spark/sql/connector/read/SupportsExternalLookup.java:
##########
@@ -36,12 +36,13 @@
@Evolving
public interface SupportsExternalLookup extends ScanBuilder {
/**
- * Selects the offered predicates that this source serves as one lookup
access path. Spark
- * offers {@code =}, {@code <=>}, {@code <}, {@code <=}, {@code >}, {@code
>=} comparisons of a
- * lookup-side {@link NamedReference} with a {@link LookupParameter}; the
array is never empty. In
- * every offered comparison the reference is {@code children()[0]} and the
parameter is
- * {@code children()[1]}: when the query writes the lookup column on the
right, Spark flips the
- * operator, so {@code s.ts < t.event_time} is offered as {@code event_time
> ?}. Spark offers
+ * Selects the offered predicates that this source serves as one lookup
access path, and returns
+ * their positions in {@code candidates}. Spark offers {@code =}, {@code
<=>}, {@code <},
+ * {@code <=}, {@code >}, {@code >=} comparisons of a lookup-side {@link
NamedReference} with a
+ * {@link LookupParameter}; the array is never empty. In every offered
comparison the reference
+ * is {@code children()[0]} and the parameter is {@code children()[1]}: when
the query writes
+ * the lookup column on the right, Spark flips the operator, so {@code s.ts
< t.event_time} is
+ * offered as {@code event_time > ?}. Spark offers
Review Comment:
**[8] Nit: the reflow stops in the middle of the paragraph**, which leaves
the short line "offered as {@code event_time > ?}. Spark offers" in front of
full-width lines. Could the rest of the paragraph be rewrapped too?
##########
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:
**[2] Could this also go to branch-4.4?** `apache/branch-4.4`
(4.4.0-SNAPSHOT, no `v4.4.0` tag yet) still has `Predicate[]
selectLookupPredicates(Predicate[] candidates)` under `@since 4.4.0`. As the
description says, this is the cheapest time to change the API, but only if
4.4.0 ships it. If the PR lands on master alone, 4.4.0 releases the identity
contract, and master's `int[]` becomes a source and binary break for connectors
written against 4.4.0. Could it be merged to branch-4.4 too, before the 4.4.0
RC?
##########
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:
**[5] Nit: could this use `mutable.BitSet`?** `scala.collection.mutable` is
already imported at L21, and its `add` returns false for a value already
present. `val seen = mutable.BitSet.empty` with `if (!seen.add(index)) throw
...` keeps the single call of the removed `!seen.add(predicate)`, and avoids
the fully qualified `java.util.BitSet` right after this PR removed the
`java.util` import.
##########
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:
**[3] Nit: with one candidate, which every new test uses, this reads
"returned the index 1, which is not a position in the 1 candidates offered by
Spark."** It also calls the same value an index and a position. Could it state
the valid range instead, e.g. `` "`selectLookupPredicates()` returned the index
<index>, but the valid indexes are from 0 to <maxIndex>." ``?
--
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]