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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/FormExternalLookupJoin.scala:
##########
@@ -35,80 +41,342 @@ import 
org.apache.spark.sql.execution.datasources.v2.ExternalLookupRelations
  * a condition: see [[CheckExternalLookupJoin]].
  *
  * A join is an external lookup join when its right side reads a table that 
supports external
- * lookup but not batch read. It must be a stream-static join: the left side 
is a streaming query,
- * whose micro-batches tolerate lookup results that change between requests, 
and the lookup-only
- * table is on the right side.
+ * lookup but not batch read, or when the right side carries an 
`EXTERNAL_LOOKUP` hint. A table
+ * that supports both keeps the ordinary join strategies unless the hint opts 
in. It must be a
+ * stream-static join: the left side is a streaming query, whose micro-batches 
tolerate lookup
+ * results that change between requests, and the lookup table is on the right 
side. The join reads
+ * the table directly, or through hints, aliases and views only, so a join 
that must be a lookup
+ * join either runs as one or fails here, whatever the optimizer later does to 
its lookup side.
  */
 object ExternalLookupJoinEligibility extends QueryErrorsBase {
   sealed trait Eligibility
   /**
-   * The join stays an ordinary join: the classifier recognizes no lookup-only 
relation as either
-   * of its sides. A lookup-only relation under another operator, such as a 
projection or an
-   * alias, is not recognized yet, and fails later as a batch scan.
+   * The join stays an ordinary join: it has no `EXTERNAL_LOOKUP` hint, and 
neither of its inputs is
+   * a lookup-only relation, directly or below unary operators. A lookup-only 
relation below another
+   * operator, such as a union, is not recognized, and fails later as a batch 
scan.
    */
   case object NotLookupJoin extends Eligibility
   /** The join runs as an external lookup join. */
   case object Eligible extends Eligibility
-  /** The join reads a lookup-only table, with a supported join type, but has 
no condition. */
+  /**
+   * The join must run as an external lookup join, with a supported join type, 
but has no
+   * condition.
+   */
   case object MissingCondition extends Eligibility
-  /** The join reads a lookup-only table, which no other join can read, but 
cannot run. */
+  /** The join must run as an external lookup join but cannot. */
   case class Ineligible(error: Throwable) extends Eligibility
 
   private val SUPPORTED_JOIN_TYPES = Seq(Inner, LeftOuter)
 
-  def apply(join: Join): Eligibility = {
-    // The lookup-only table must be on the right side; the sides are never 
swapped.
-    // TODO: look through projections, filters and aliases above the lookup 
relation.
-    // TODO: allow the EXTERNAL_LOOKUP hint once it is introduced, and 
classify the joins it opts
-    //   in.
-    (ExternalLookupRelations.lookupOnlyTable(join.left), 
lookupOnlyTableOnRight(join)) match {
-      case (Some(table), _) => ineligible("LOOKUP_TABLE_ON_LEFT", table)
-      case (None, Some(table)) => lookupJoinEligibility(join, table)
-      case (None, None) => NotLookupJoin
-    }
-  }
-
   /**
-   * The lookup-only table on the join's right side, also under a strategy 
hint, which is where a
-   * broadcast hint usually goes in a stream-static join. The hint is then 
reported as conflicting.
+   * An operator that the `EXTERNAL_LOOKUP` hint treats as a join: a [[Join]], 
an ASOF or NEAREST BY
+   * join, and a lateral join that the query writes. None of the last three is 
a [[Join]] when
+   * EliminateResolvedHint attaches the hints, so a join strategy hint on one 
is dropped. The
+   * planner runs a SQL ASOF join, and a DataFrame `joinAsOf` when
+   * `spark.sql.join.sortMergeAsOfJoin.enabled` is set, with its own 
sort-merge operator. Finish
+   * Analysis rewrites any other `joinAsOf` into a projection over a 
correlated scalar subquery,
+   * which operator optimization later rewrites into a join, and a NEAREST BY 
join into a plan with
+   * a join. Operator optimization rewrites a lateral join into a join. None 
of them can run as an
+   * external lookup join, so the `EXTERNAL_LOOKUP` hint on one is misplaced 
and fails, rather than
+   * being dropped like a hint that attaches to no join. A lateral join that 
analysis adds for a
+   * table argument of a table function is not written by the query, so it is 
a unary operator for
+   * the hint, as for any other analysis check.
    */
-  private def lookupOnlyTableOnRight(join: Join): Option[Table] = {
-    def underStrategyHints(plan: LogicalPlan): LogicalPlan = plan match {
-      case ResolvedHint(child, hint) if hint.strategy.isDefined => 
underStrategyHints(child)
-      case other => other
+  object HintableJoin {

Review Comment:
   This describes how other rules plan each operator, which already needed one 
correction and will drift. Could it just say that none of them is a `Join` when 
hints are attached and none can run as a lookup join, so the hint on one fails? 
The `CheckExternalLookupJoin` Scaladoc (`:400`) could then point here instead 
of repeating it.



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