JingsongLi commented on code in PR #8252:
URL: https://github.com/apache/paimon/pull/8252#discussion_r3460434065
##########
paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/catalyst/plans/logical/PaimonTableValuedFunctions.scala:
##########
@@ -189,6 +194,57 @@ object PaimonTableValuedFunctions {
}
}
+ def resolveLateralVectorSearch(
+ left: LogicalPlan,
+ right: LogicalPlan,
+ joinType: JoinType,
+ condition: Option[Expression]): Option[LogicalPlan] = {
+ extractDynamicVectorSearch(right) match {
+ case None =>
+ None
+ case Some(_) if joinType != Inner =>
+ throw new RuntimeException(
+ s"LATERAL vector_search only supports INNER join, but got:
$joinType.")
+ case Some((relation, projectList, projectOutput)) =>
+ val vectorSearchOutput = vectorSearchOutputForProject(relation,
projectList)
+ val lateralVectorSearch =
+ LateralVectorSearch(
+ left,
+ relation.innerTable,
+ relation.columnName,
+ relation.queryVectorExpr,
+ relation.limit,
+ relation.options,
+ vectorSearchOutput,
+ projectList,
+ projectOutput)
+ Some(condition.map(Filter(_,
lateralVectorSearch)).getOrElse(lateralVectorSearch))
+ }
+ }
+
+ private def vectorSearchOutputForProject(
+ relation: DynamicVectorSearchRelation,
+ projectList: Seq[NamedExpression]): Seq[Attribute] = {
+ val projectReferences =
AttributeSet.fromAttributeSets(projectList.map(_.references))
+ relation.output.filter(projectReferences.contains)
+ }
+
+ private def extractDynamicVectorSearch(plan: LogicalPlan)
Review Comment:
Filters inside the lateral subquery are still not handled here. For example,
`FROM q, LATERAL (SELECT gid FROM vector_search('t', 'embs', q.embs, 5) WHERE
dt = '20260608') r` is resolved as a right plan containing `Filter(...,
DynamicVectorSearchRelation)` (usually under `Project`). This extractor falls
through to `None`, leaving the dynamic relation with an outer reference but no
`LateralVectorSearch` physical path. Please extract `Filter` nodes here and
append their conditions to `searchFilters` (or reject them explicitly) in
addition to the outer-`WHERE` pushdown case.
##########
paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/catalyst/optimizer/PushDownLateralVectorSearchFilter.scala:
##########
@@ -0,0 +1,64 @@
+/*
+ * 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.paimon.spark.catalyst.optimizer
+
+import org.apache.paimon.spark.catalyst.plans.logical.LateralVectorSearch
+
+import org.apache.spark.sql.catalyst.expressions.{And, PredicateHelper}
+import org.apache.spark.sql.catalyst.plans.logical.{Filter, LogicalPlan}
+import org.apache.spark.sql.catalyst.rules.Rule
+
+/** Pushes filters on the query side below lateral vector search. */
+object PushDownLateralVectorSearchFilter extends Rule[LogicalPlan] with
PredicateHelper {
+
+ override def apply(plan: LogicalPlan): LogicalPlan = plan.transform {
+ case filter @ Filter(condition, lvs: LateralVectorSearch) =>
+ val predicates = splitConjunctivePredicates(condition)
+ val (pushDownToLeft, otherPredicates) = predicates.partition {
+ predicate => predicate.deterministic &&
predicate.references.subsetOf(lvs.child.outputSet)
+ }
+ val (pushDownToSearch, stayUp) = otherPredicates.partition {
Review Comment:
This removes every deterministic predicate that references only search-side
output, but `convertSearchFilters()` later throws if Spark cannot translate the
rewritten expression into a Paimon predicate. A valid query such as `WHERE
r.gid + 1 > 10`, or a filter on an expression alias from the lateral subquery,
would now fail during execution instead of being evaluated above the lateral
result. Please keep untranslatable predicates in `stayUp`, or restrict this
pushdown to simple field predicates that are known to be convertible before
dropping the upper filter.
--
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]