cloud-fan commented on code in PR #55925:
URL: https://github.com/apache/spark/pull/55925#discussion_r3449087014
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -1366,6 +1366,18 @@ object SQLConf {
.booleanConf
.createWithDefault(true)
+ val REWRITE_COUNT_DISTINCT_CONDITIONAL_ENABLED =
+ buildConf("spark.sql.optimizer.rewriteCountDistinctConditional.enabled")
+ .doc("When true, rewrites COUNT(DISTINCT IF(cond, base, NULL)) and " +
+ "COUNT(DISTINCT CASE WHEN cond THEN base END) into " +
+ "COUNT(DISTINCT base) FILTER (WHERE cond). This reduces the Expand
factor " +
+ "in RewriteDistinctAggregates from Nx to 1x when multiple conditional
distinct " +
+ "counts share the same base column.")
+ .version("4.3.0")
+ .withBindingPolicy(ConfigBindingPolicy.SESSION)
+ .booleanConf
+ .createWithDefault(false)
Review Comment:
In the prior round, the standalone-rule form could push a lone
`COUNT(DISTINCT IF(...))` onto the Expand path, which justified default-off.
Folding the normalization into `rewrite()` behind the `mayNeedtoRewrite` gate
removed that regression: the integrated rewrite is now neutral-or-better in
every case (same-base counts collapse Nx->1x; mixed/different-base counts are
cost-neutral). So the original default-off justification no longer holds.
Could this default on? If it stays off for rollout conservatism, that's
reasonable — but worth stating the reason in the PR description / doc, since
the regression it used to guard against is gone.
##########
sql/core/src/test/scala/org/apache/spark/sql/RewriteDistinctAggregatesConditionalQuerySuite.scala:
##########
@@ -0,0 +1,226 @@
+/*
+ * 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
+
+import org.apache.spark.sql.catalyst.expressions.aggregate.AggregateExpression
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, LogicalPlan}
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.test.SharedSparkSession
+
+class RewriteDistinctAggregatesConditionalQuerySuite extends QueryTest with
SharedSparkSession {
+
+ private def checkRewriteAndResult(
+ conditionalSql: String,
+ filterSql: String): Unit = {
+ // Verify the rewrite produces the same result as the explicit FILTER form.
+ val withRewrite = withSQLConf(
+ SQLConf.REWRITE_COUNT_DISTINCT_CONDITIONAL_ENABLED.key -> "true") {
+ spark.sql(conditionalSql)
+ }
+ val withoutRewrite = withSQLConf(
+ SQLConf.REWRITE_COUNT_DISTINCT_CONDITIONAL_ENABLED.key -> "false") {
+ spark.sql(conditionalSql)
+ }
+ val explicitFilter = spark.sql(filterSql)
+
+ // Rewritten query should match explicit FILTER query.
+ checkAnswer(withRewrite, explicitFilter)
+ // Non-rewritten query should also match explicit FILTER query.
+ checkAnswer(withoutRewrite, explicitFilter)
+ }
+
+ private def hasFilterClause(plan: LogicalPlan): Boolean = {
+ val aggregateExpressions = plan.collect {
+ case a: Aggregate => a.aggregateExpressions
+ }.flatten
+ val aggregateExprs = aggregateExpressions.flatMap {
+ _.collect { case ae: AggregateExpression => ae }
+ }
+ aggregateExprs.exists(_.filter.isDefined)
+ }
+
+ test("rewrite COUNT(DISTINCT IF(cond, col, NULL)) correctness") {
Review Comment:
This test (and "CASE WHEN ... correctness", "CASE WHEN ... ELSE NULL",
"rewrite with no GROUP BY", "rewrite with all NULLs", "rewrite with
duplicates") uses a single conditional distinct count. A lone `COUNT(DISTINCT
IF(...))` has no FILTER and forms one distinct group, so `mayNeedtoRewrite`
returns false, `rewrite()` is never called, and
`normalizeCountDistinctConditional` never runs — the conf is a no-op here. The
test passes whether the canonicalization works, is broken, or is absent; it
actually exercises the gated-out single-count path, not the rewrite its name
claims.
Only `multiple conditional distinct counts collapse` (IF, two *different*
bases) and `rewrite is present in optimized plan` (asserts FILTER, but no
`checkAnswer`) fire the canonicalization, so the CASE patterns and the
NULL/duplicate edge cases are never validated through a firing rewrite at
execution.
Give each test >=2 conditional distinct counts (ideally on the same base, so
the collapse is exercised too) and `checkAnswer` against the explicit
double-FILTER form. This is the same class of weakness flagged for
`RewriteDistinctAggregatesSuite` (lines 184/239), which was fixed there with
multi-count inputs but not carried over to this suite.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -1366,6 +1366,18 @@ object SQLConf {
.booleanConf
.createWithDefault(true)
+ val REWRITE_COUNT_DISTINCT_CONDITIONAL_ENABLED =
+ buildConf("spark.sql.optimizer.rewriteCountDistinctConditional.enabled")
Review Comment:
Optional / author's call: the closest functional sibling,
`spark.sql.optimizer.propagateDistinctKeys.enabled` (also a pure optimizer-rule
on/off toggle), is `.internal()`. Most non-internal `spark.sql.optimizer.*`
confs are user-facing tuning knobs (DPP, runtime bloom filters); a pure rule
switch with no tuning dimension reads more like the internal group. Consider
`.internal()` for consistency — precedent exists both ways, so not blocking.
--
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]