wangyum commented on code in PR #40805: URL: https://github.com/apache/spark/pull/40805#discussion_r1168093290
########## sql/core/src/main/scala/org/apache/spark/sql/execution/bucketing/UnwrapCastInJoinCondition.scala: ########## @@ -0,0 +1,141 @@ +/* + * 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.execution.bucketing + +import org.apache.spark.sql.catalyst.analysis.AnsiTypeCoercion.findWiderTypeForTwo +import org.apache.spark.sql.catalyst.expressions.{Attribute, Cast, EvalMode, Expression} +import org.apache.spark.sql.catalyst.rules.Rule +import org.apache.spark.sql.execution.SparkPlan +import org.apache.spark.sql.execution.joins.{ShuffledHashJoinExec, ShuffledJoin, SortMergeJoinExec} +import org.apache.spark.sql.types.{DataType, DecimalType, IntegralType} + +/** + * This rule unwrap the cast in one side of the `SortMergeJoin` and `ShuffledHashJoin` join keys. + */ +object UnwrapCastInJoinCondition extends Rule[SparkPlan] { + def apply(plan: SparkPlan): SparkPlan = { + if (!conf.unwrapCastInJoinConditionEnabled) { + return plan + } + + plan transform { + case ExtractJoinWithUnwrapCastInJoinCondition(join, joinKeys) => + val (leftKeys, rightKeys) = joinKeys.unzip + join match { + case j: SortMergeJoinExec => + j.copy(leftKeys = leftKeys, rightKeys = rightKeys) + case j: ShuffledHashJoinExec => + j.copy(leftKeys = leftKeys, rightKeys = rightKeys) + case other => other + } + case other => other + } + } +} + +/** + * An extractor that extracts `SortMergeJoinExec` and `ShuffledHashJoin`, + * where one sides can do bucketed read after unwrap cast in join keys. + */ +object ExtractJoinWithUnwrapCastInJoinCondition extends BucketJoinHelper { + private def isIntegralType(dt: DataType): Boolean = dt match { + case _: IntegralType => true + case DecimalType.Fixed(_, 0) => true + case _ => false + } + + private def unwrapCastInJoinKeys(joinKeys: Seq[Expression]): Seq[Expression] = { + joinKeys.map { + case Cast(a: Attribute, _, _, _) if isIntegralType(a.dataType) => a + case e => e + } + } + + private def followLeftJoinKeyType( + unwrapLeftKeys: Seq[Expression], + unwrapRightKeys: Seq[Expression]): Seq[(Expression, Expression)] = { + unwrapLeftKeys.zip(unwrapRightKeys).map { + case (l, r) if l.dataType != r.dataType => + // Use TRY mode to avoid runtime exception in ANSI mode or data issue in non-ANSI mode. + l -> Cast(r, l.dataType, evalMode = EvalMode.TRY) Review Comment: Use `TRY` mode: ```sql CREATE TABLE t2 USING parquet CLUSTERED BY (i) INTO 8 buckets AS SELECT CAST(v AS bigint) AS i FROM values(1), (9223372036854775807) AS data(v); ``` Default: ```sql scala> sql("select cast(i as int) from t2").show +---+ | i| +---+ | -1| | 1| +---+ scala> sql("set spark.sql.ansi.enabled=true") res7: org.apache.spark.sql.DataFrame = [key: string, value: string] scala> sql("select cast(i as int) from t2").show 23/04/17 10:02:37 ERROR Executor: Exception in task 0.0 in stage 4.0 (TID 6) org.apache.spark.SparkArithmeticException: [CAST_OVERFLOW] The value 9223372036854775807L of the type "BIGINT" cannot be cast to "INT" due to an overflow. Use `try_cast` to tolerate overflow and return NULL instead. If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error. ``` Use `TRY` mode: ``` scala> sql("select try_cast(i as int) from t2").show +----+ | i| +----+ |null| | 1| +----+ scala> sql("set spark.sql.ansi.enabled=true") res12: org.apache.spark.sql.DataFrame = [key: string, value: string] scala> sql("select try_cast(i as int) from t2").show +----+ | i| +----+ |null| | 1| +----+ ``` -- 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]
