Copilot commented on code in PR #12513:
URL: https://github.com/apache/gluten/pull/12513#discussion_r3639684147
##########
gluten-substrait/src/main/scala/org/apache/gluten/extension/columnar/offload/OffloadSingleNodeRules.scala:
##########
@@ -139,6 +141,17 @@ object OffloadJoin {
return BuildLeft
}
+ // Honor a ForceShjBuildLeft tag set by the guard rule (stats verified
safe for BuildLeft).
+ if (shj.getTagValue(RewriteJoin.ForceShjBuildLeftTag).getOrElse(false)) {
+ return BuildLeft
+ }
+
+ // When BuildLeft feature is enabled for LeftSemi but the guard didn't
approve (no tag),
+ // prevent CBO from accidentally choosing BuildLeft without safety
verification.
+ if (shj.joinType == LeftSemi &&
GlutenConfig.get.shjLeftSemiBuildLeftEnabled) {
+ return BuildRight
+ }
Review Comment:
`getShjBuildSide` forces BuildRight for `LeftSemi` whenever
`shjLeftSemiBuildLeftEnabled` is true, regardless of backend. This can
unintentionally disable ClickHouse's existing ability to build left for
LeftSemi (see CHBackend.supportHashBuildJoinTypeOnLeft) when someone enables
the new conf globally. Consider scoping this safety override to the Velox-only
path (e.g., a backend capability) so other backends keep their prior behavior.
##########
gluten-substrait/src/main/scala/org/apache/gluten/extension/columnar/util/ShuffleSkewDetector.scala:
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.gluten.extension.columnar.util
+
+import org.apache.spark.sql.execution.SparkPlan
+import org.apache.spark.sql.execution.adaptive.ShuffleQueryStageExec
+
+object ShuffleSkewDetector {
+
+ case class SkewJudgement(
+ factor: Double,
+ partitionThresholdBytes: Long,
+ minTotalBytes: Long)
+
+ def totalBytes(side: SparkPlan): Option[Long] = {
+ val stages = side.collect { case s: ShuffleQueryStageExec => s }
+ if (stages.isEmpty) return None
+ var total = 0L
+ var found = false
+ stages.foreach {
+ stage =>
+ if (stage.isMaterialized) {
+ stage.mapStats.foreach {
+ ms =>
+ total += ms.bytesByPartitionId.foldLeft(0L)(_ + _)
+ found = true
+ }
+ }
+ }
+ if (found) Some(total) else None
+ }
+
+ def analyze(side: SparkPlan, judgement: SkewJudgement): Result = {
+ val stages = side.collect { case s: ShuffleQueryStageExec => s }
+ if (stages.isEmpty) {
+ return Result(hasStage = false, materialized = false, isSkewed = false)
+ }
+ val materializedWithStats = stages.flatMap {
+ stage => if (stage.isMaterialized) stage.mapStats.map(ms =>
ms.bytesByPartitionId) else None
+ }
+ if (materializedWithStats.isEmpty) {
+ val anyMaterialized = stages.exists(_.isMaterialized)
+ return Result(hasStage = true, materialized = anyMaterialized, isSkewed
= false)
+ }
+ val bytes = materializedWithStats.maxBy(_.foldLeft(0L)(_ + _))
+ if (bytes.isEmpty) {
+ return Result(hasStage = true, materialized = true, isSkewed = false)
+ }
Review Comment:
`analyze` picks the shuffle stats to inspect via
`materializedWithStats.maxBy(totalBytes)`. In a subtree with multiple shuffle
stages, this can ignore the stage closest to the join input and miss probe-side
partition skew on that stage, undermining the safety guard. Prefer inspecting
the shuffle stage nearest the join input (consistent with the BuildLeft guard’s
intent).
##########
gluten-substrait/src/main/scala/org/apache/gluten/extension/columnar/util/ShuffleSkewDetector.scala:
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.gluten.extension.columnar.util
+
+import org.apache.spark.sql.execution.SparkPlan
+import org.apache.spark.sql.execution.adaptive.ShuffleQueryStageExec
+
+object ShuffleSkewDetector {
+
+ case class SkewJudgement(
+ factor: Double,
+ partitionThresholdBytes: Long,
+ minTotalBytes: Long)
+
+ def totalBytes(side: SparkPlan): Option[Long] = {
+ val stages = side.collect { case s: ShuffleQueryStageExec => s }
+ if (stages.isEmpty) return None
+ var total = 0L
+ var found = false
+ stages.foreach {
+ stage =>
+ if (stage.isMaterialized) {
+ stage.mapStats.foreach {
+ ms =>
+ total += ms.bytesByPartitionId.foldLeft(0L)(_ + _)
+ found = true
+ }
+ }
+ }
+ if (found) Some(total) else None
+ }
Review Comment:
`totalBytes` currently sums `bytesByPartitionId` across *all*
`ShuffleQueryStageExec` nodes under `side`. If the subtree contains multiple
shuffle stages (e.g. nested joins), this can double-count unrelated shuffles
and inflate the "rightBytes"/ratio gates, potentially enabling BuildLeft when
the immediate join input shuffle is actually small. Consider using only the
shuffle stage closest to the join input.
--
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]