vranes commented on code in PR #59834: URL: https://github.com/apache/spark/pull/59834#discussion_r4402706340
########## sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/AsOfJoinMatchConditionResolution.scala: ########## @@ -0,0 +1,210 @@ +/* + * 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.spark.sql.catalyst.analysis + +import org.apache.spark.sql.catalyst.expressions._ +import org.apache.spark.sql.catalyst.plans._ +import org.apache.spark.sql.catalyst.plans.logical.AsOfJoin +import org.apache.spark.sql.internal.SQLConf +import org.apache.spark.sql.types.{DecimalType, DoubleType, IntegralType, NullType} + +/** The executable fields produced by resolving ASOF keys. */ +private[sql] case class MaterializedMatchCondition( + asOfCondition: Expression, + direction: AsOfJoinDirection, + leftSortExpressions: Seq[Expression], + rightSortExpressions: Seq[Expression], + orderExpression: Option[Expression] = None) { + + /** Replaces the pending operands with the resolved comparison and sort keys. */ + def toJoin(join: AsOfJoin): AsOfJoin = { + join.copy( + asOfCondition = asOfCondition, + direction = direction, + leftSortExprs = leftSortExpressions, + rightSortExprs = rightSortExpressions, + matchLeftOperand = None, + matchOperator = None, + matchRightOperand = None, + requiresSortMergeAsOfJoin = orderExpression.isEmpty, + orderExpression = orderExpression + ) + } +} + +/** Shared ASOF key normalization for the fixed-point and single-pass analyzers. */ +private[sql] object AsOfJoinMatchConditionResolution { + + /** Materializes a SQL MATCH_CONDITION once both operands are resolved. */ + def materialize( + join: AsOfJoin, + leftSet: AttributeSet, + rightSet: AttributeSet, + leftOperand: Expression, + operator: MatchComparisonOperator, + rightOperand: Expression): Option[MaterializedMatchCondition] = { + AsOfJoinValidation.validateMatchConditionTableReferences( + join = join, + leftSet = leftSet, + rightSet = rightSet, + leftExpr = leftOperand, + rightExpr = rightOperand + ) + + if (leftOperand.resolved && rightOperand.resolved) { + AsOfJoinValidation.validateMatchConditionOperands( + join = join, + leftExpr = leftOperand, + rightExpr = rightOperand + ) + val (normalizedLeft, normalizedRight, normalizedOperator) = + AsOfJoin.normalizeMatchOperands( + leftSet = leftSet, + rightSet = rightSet, + expr1 = leftOperand, + operator = operator, + expr2 = rightOperand + ) + val (asOfCondition, leftSort, rightSort) = AsOfJoin.materializeMatchComparison( + leftOperand = normalizedLeft, + rightOperand = normalizedRight, + normalizedOp = normalizedOperator + ) + Some(MaterializedMatchCondition( + asOfCondition = asOfCondition, + direction = normalizedOperator.direction, + leftSortExpressions = leftSort, + rightSortExpressions = rightSort + )) + } else { + None + } + } + + /** Resolves DataFrame keys and selects the execution strategy during analysis. */ + def materializeDataFrame( + join: AsOfJoin, + leftSet: AttributeSet, + rightSet: AttributeSet, + leftOperand: Expression, + rightOperand: Expression): Option[MaterializedMatchCondition] = { + if (!leftOperand.resolved || !rightOperand.resolved || !join.tolerance.forall(_.resolved)) { + return None + } + + AsOfJoinValidation.validateTolerance(join) + + val useSortMerge = SQLConf.get.sortMergeAsOfJoinEnabled + if (useSortMerge) { + AsOfJoinValidation.validateMatchConditionTableReferences( + join = join, + leftSet = leftSet, + rightSet = rightSet, + leftExpr = leftOperand, + rightExpr = rightOperand + ) + AsOfJoinValidation.validateMatchConditionOperands( + join = join, + leftExpr = leftOperand, + rightExpr = rightOperand + ) + } + AsOfJoinValidation.validateNearestStringKeys( Review Comment: This is pre-existing on the old correlated-subquery path, and the plan is to deprecate it and remove it completely. If we need to fix it before removal we can do that in a separate PR to limit the scope of this PR. -- 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]
