You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@gluten.apache.org by "exmy (via GitHub)" <gi...@apache.org> on 2026/08/19 02:59:28 UTC

[PR] [CH] Fix nullable filtering after Expand with constant columns [gluten]

exmy opened a new pull request, #12818:
URL: https://github.com/apache/gluten/pull/12818

   
   ## What changes are proposed in this pull request?
   
   This PR fixes incorrect nullable filtering after Expand in the ClickHouse
   backend.
   
   The changes:
   
   - Materialize constant selection columns in regular Expand.
   - Apply the same behavior to AdvancedExpand.
   - Preserve nullable types for NULL literals.
   - Add regression tests for both regular and lazy aggregate Expand paths.
   
   Expand emits each grouping-set projection as a separate chunk.
   
   When a selected input column originates from a SQL literal, `castColumn`
   may preserve it as a `ColumnConst`. In the reproducing query, the first
   chunk contains a constant non-null `msg_type`, so `isNotNull(msg_type)`
   produces a constant true filter.
   
   The ClickHouse `FilterTransform` version used by the backend preserves
   `ConstantFilterDescription` between chunks. It checks the previous
   `always_true` state before recalculating the filter for the current chunk.
   
   As a result, the following chunk, whose `msg_type` is a real nullable NULL,
   can bypass filtering and produce extra grouping-set rows.
   
   For example:
   
   ```sql
   select msg_type, os, count(*) as cnt
   from (
     select 'file' as msg_type, 'Android' as os, id from range(10)
     union all
     select 'file' as msg_type, 'iOS' as os, id from range(10)
   ) t
   group by grouping sets ((msg_type), (os))
   having msg_type is not null
   order by msg_type, os, cnt
   ```
   
   the incorrect result was:
   
   
   ```text
   NULL  Android  10
   NULL  iOS      10
   file  NULL     20
   ```
   
   The expected result is:
   ```text
   file  NULL  20
   ```
   
   Materialize constant selection columns in regular and advanced Expand to prevent stale constant filter state from leaking null rows. Preserve nullable types for null literals and add regression coverage for both Expand paths.
   
   ## How was this patch tested?
   
   add ut
   
   ## Was this patch authored or co-authored using generative AI tooling?
   
   Codex
   


-- 
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: commits-unsubscribe@gluten.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@gluten.apache.org
For additional commands, e-mail: commits-help@gluten.apache.org


Re: [PR] [GLUTEN-12834][CH] Fix nullable filtering after Expand with constant columns [gluten]

Posted by "exmy (via GitHub)" <gi...@apache.org>.
exmy merged PR #12818:
URL: https://github.com/apache/gluten/pull/12818


-- 
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: commits-unsubscribe@gluten.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@gluten.apache.org
For additional commands, e-mail: commits-help@gluten.apache.org


Re: [PR] [CH] Fix nullable filtering after Expand with constant columns [gluten]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #12818:
URL: https://github.com/apache/gluten/pull/12818#issuecomment-5337050595

   Run Gluten Clickhouse CI on x86


-- 
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: commits-unsubscribe@gluten.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@gluten.apache.org
For additional commands, e-mail: commits-help@gluten.apache.org


Re: [PR] [CH] Fix nullable filtering after Expand with constant columns [gluten]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #12818:
URL: https://github.com/apache/gluten/pull/12818#issuecomment-5336966603

   Run Gluten Clickhouse CI on x86


-- 
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: commits-unsubscribe@gluten.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@gluten.apache.org
For additional commands, e-mail: commits-help@gluten.apache.org


Re: [PR] [CH] Fix nullable filtering after Expand with constant columns [gluten]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #12818:
URL: https://github.com/apache/gluten/pull/12818#discussion_r3809737608


##########
backends-clickhouse/src/test/scala/org/apache/gluten/execution/tpch/GlutenClickHouseTPCHSaltNullParquetSuite.scala:
##########
@@ -878,6 +878,80 @@ class GlutenClickHouseTPCHSaltNullParquetSuite
     compareResultsAgainstVanillaSpark(sql, true, { _ => })
   }
 
+  test("grouping sets preserves nullable columns across union") {
+    val sql =
+      """
+        |select msg_type, os, count(*) as cnt
+        |from (
+        |  select 'file' as msg_type, 'Android' as os, id from range(10)
+        |  union all
+        |  select 'file' as msg_type, 'iOS' as os, id from range(10)
+        |) t
+        |group by grouping sets ((msg_type), (os))
+        |having msg_type is not null
+        |order by msg_type, os, cnt
+        |""".stripMargin
+    withSparkEnvAndSQLConf(CHConfig.runtimeConfig("enable_lazy_aggregate_expand"), "false") {
+      compareResultsAgainstVanillaSpark(
+        sql,
+        true,
+        {
+          df =>
+            val expands = collectWithSubqueries(df.queryExecution.executedPlan) {
+              case e: ExpandExecTransformer
+                  if !e.child.isInstanceOf[HashAggregateExecBaseTransformer] =>
+                e
+            }
+            assert(expands.size == 1)
+        }
+      )
+    }
+  }
+
+  test("lazy aggregate expand preserves nullable columns across union") {
+    val sql =
+      """
+        |select msg_type, os, count(*) as cnt
+        |from (
+        |  select 'file' as msg_type, 'Android' as os, id from range(10)
+        |  union all
+        |  select 'file' as msg_type, 'iOS' as os, id from range(10)
+        |) t
+        |group by grouping sets ((msg_type), (os))
+        |having msg_type is not null
+        |order by msg_type, os, cnt
+        |""".stripMargin
+    withSparkEnvAndSQLConf(CHConfig.runtimeConfig("enable_lazy_aggregate_expand"), "true") {
+      compareResultsAgainstVanillaSpark(
+        sql,
+        true,
+        {
+          df =>
+            val expands = collectWithSubqueries(df.queryExecution.executedPlan) {
+              case e: ExpandExecTransformer
+                  if e.child.isInstanceOf[HashAggregateExecBaseTransformer] =>
+                e
+            }
+            assert(expands.size == 1)
+        }
+      )
+    }
+  }
+
+  private def withSparkEnvAndSQLConf(key: String, value: String)(f: => Unit): Unit = {
+    val sparkConf = SparkEnv.get.conf
+    val previousValue = sparkConf.getOption(key)
+    sparkConf.set(key, value)
+    try {
+      withSQLConf(key -> value)(f)
+    } finally {

Review Comment:
   `CHBackend.enableLazyAggregateExpand()` is read from `SparkEnv.get.conf` (not `SQLConf`), so the `withSQLConf(key -> value)` wrapper here is redundant for the only current call sites and can be removed to reduce confusion about which config source actually controls the behavior under test.



##########
cpp-ch/local-engine/Parser/RelParsers/ExpandRelParser.cpp:
##########
@@ -113,6 +114,12 @@ ExpandField ExpandRelParser::buildExpandField(const DB::Block & header, const su
             else if (project_expr.has_literal())
             {
                 auto [type, field] = parseLiteral(project_expr.literal());
+                // A NULL literal is nullable even when the type carried by the
+                // Substrait literal does not explicitly encode nullability.
+                // Keep that information in the Expand output type so the
+                // generated column contains a real null map.
+                if (field.isNull() && type && !type->isNullable())
+                    type = std::make_shared<DB::DataTypeNullable>(type);

Review Comment:
   Instead of constructing `DataTypeNullable` directly, prefer `DB::makeNullable(type)` for consistency with the rest of the codebase and to let ClickHouse handle edge cases (e.g., nested wrappers) in one place.



-- 
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: commits-unsubscribe@gluten.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@gluten.apache.org
For additional commands, e-mail: commits-help@gluten.apache.org