You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "suibianwanwank (via GitHub)" <gi...@apache.org> on 2025/03/17 17:10:43 UTC

[PR] Fix the well known `count-bug` similar cases [datafusion]

suibianwanwank opened a new pull request, #15281:
URL: https://github.com/apache/datafusion/pull/15281

   ## Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   - Closes #15032.
   
   ## Rationale for this change
   
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   ## What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   ## Are these changes tested?
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   
   ## Are there any user-facing changes?
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please add the `api change` label.
   -->
   


-- 
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: github-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: Queries similar to `count-bug` produce incorrect results [datafusion]

Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on PR #15281:
URL: https://github.com/apache/datafusion/pull/15281#issuecomment-2764477011

   It seems `agg_exprs_evaluation_result_on_empty_batch` assumes the result is empty and convert it to null or 0. But I don't understand how does it ensure the result is empty


-- 
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: github-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: Queries similar to `count-bug` produce incorrect results [datafusion]

Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on PR #15281:
URL: https://github.com/apache/datafusion/pull/15281#issuecomment-2769722147

   The projection required to be in the group expression. I think the query of these 2 are equivalent but the subquery one group by `e2.b` and the join query group by `e1.b`.
   
   Not sure if this rewrite could be general enough 🤔 
   
   ```
   query IT
   select e1.b, (select case when max(e2.a) > 10 then 'a' else 'b' end from t2 e2 where e2.b = e1.b + 1) from t1 e1;
   ----
   0 a
   2 a
   
   query TT
   explain
   select e1.b, (select case when max(e2.a) > 10 then 'a' else 'b' end from t2 e2 where e2.b = e1.b + 1) from t1 e1;
   ----
   logical_plan
   01)Projection: e1.b, CASE WHEN __scalar_sq_1.__always_true IS NULL THEN Utf8("b") ELSE __scalar_sq_1.CASE WHEN max(e2.a) > Int64(10) THEN Utf8("a") ELSE Utf8("b") END END AS CASE WHEN max(e2.a) > Int64(10) THEN Utf8("a") ELSE Utf8("b") END
   02)--Left Join: CAST(e1.b AS Int64) + Int64(1) = CAST(__scalar_sq_1.b AS Int64)
   03)----SubqueryAlias: e1
   04)------TableScan: t1 projection=[b]
   05)----SubqueryAlias: __scalar_sq_1
   06)------Projection: CASE WHEN max(e2.a) > Int32(10) THEN Utf8("a") ELSE Utf8("b") END AS CASE WHEN max(e2.a) > Int64(10) THEN Utf8("a") ELSE Utf8("b") END, e2.b, Boolean(true) AS __always_true
   07)--------Aggregate: groupBy=[[e2.b]], aggr=[[max(e2.a)]]
   08)----------SubqueryAlias: e2
   09)------------TableScan: t2 projection=[a, b]
   physical_plan
   01)ProjectionExec: expr=[b@0 as b, CASE WHEN __always_true@2 IS NULL THEN b ELSE CASE WHEN max(e2.a) > Int64(10) THEN Utf8("a") ELSE Utf8("b") END@1 END as CASE WHEN max(e2.a) > Int64(10) THEN Utf8("a") ELSE Utf8("b") END]
   02)--CoalesceBatchesExec: target_batch_size=8192
   03)----HashJoinExec: mode=Partitioned, join_type=Left, on=[(e1.b + Int64(1)@1, CAST(__scalar_sq_1.b AS Int64)@3)], projection=[b@0, CASE WHEN max(e2.a) > Int64(10) THEN Utf8("a") ELSE Utf8("b") END@2, __always_true@4]
   04)------CoalesceBatchesExec: target_batch_size=8192
   05)--------RepartitionExec: partitioning=Hash([e1.b + Int64(1)@1], 4), input_partitions=1
   06)----------ProjectionExec: expr=[b@0 as b, CAST(b@0 AS Int64) + 1 as e1.b + Int64(1)]
   07)------------DataSourceExec: partitions=1, partition_sizes=[1]
   08)------CoalesceBatchesExec: target_batch_size=8192
   09)--------RepartitionExec: partitioning=Hash([CAST(__scalar_sq_1.b AS Int64)@3], 4), input_partitions=4
   10)----------ProjectionExec: expr=[CASE WHEN max(e2.a)@1 > 10 THEN a ELSE b END as CASE WHEN max(e2.a) > Int64(10) THEN Utf8("a") ELSE Utf8("b") END, b@0 as b, true as __always_true, CAST(b@0 AS Int64) as CAST(__scalar_sq_1.b AS Int64)]
   11)------------AggregateExec: mode=FinalPartitioned, gby=[b@0 as b], aggr=[max(e2.a)]
   12)--------------CoalesceBatchesExec: target_batch_size=8192
   13)----------------RepartitionExec: partitioning=Hash([b@0], 4), input_partitions=4
   14)------------------RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1
   15)--------------------AggregateExec: mode=Partial, gby=[b@1 as b], aggr=[max(e2.a)]
   16)----------------------DataSourceExec: partitions=1, partition_sizes=[1]
   
   query IT
   SELECT
       e1.b,
       CASE 
           WHEN MAX(e2.a) > 10 THEN 'a' 
           ELSE 'b' 
       END AS result
   FROM t2 e2
   LEFT JOIN t1 e1 ON e2.b = e1.b + 1
   GROUP BY e1.b;
   ----
   2 a
   0 a
   
   query TT
   explain
   SELECT
       e1.b,
       CASE 
           WHEN MAX(e2.a) > 10 THEN 'a' 
           ELSE 'b' 
       END AS result
   FROM t2 e2
   LEFT JOIN t1 e1 ON e2.b = e1.b + 1
   GROUP BY e1.b;
   ----
   logical_plan
   01)Projection: e1.b, CASE WHEN max(e2.a) > Int32(10) THEN Utf8("a") ELSE Utf8("b") END AS result
   02)--Aggregate: groupBy=[[e1.b]], aggr=[[max(e2.a)]]
   03)----Projection: e2.a, e1.b
   04)------Left Join: CAST(e2.b AS Int64) = CAST(e1.b AS Int64) + Int64(1)
   05)--------SubqueryAlias: e2
   06)----------TableScan: t2 projection=[a, b]
   07)--------SubqueryAlias: e1
   08)----------TableScan: t1 projection=[b]
   physical_plan
   01)ProjectionExec: expr=[b@0 as b, CASE WHEN max(e2.a)@1 > 10 THEN a ELSE b END as result]
   02)--AggregateExec: mode=FinalPartitioned, gby=[b@0 as b], aggr=[max(e2.a)]
   03)----CoalesceBatchesExec: target_batch_size=8192
   04)------RepartitionExec: partitioning=Hash([b@0], 4), input_partitions=4
   05)--------AggregateExec: mode=Partial, gby=[b@1 as b], aggr=[max(e2.a)]
   06)----------RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1
   07)------------ProjectionExec: expr=[a@1 as a, b@0 as b]
   08)--------------CoalesceBatchesExec: target_batch_size=8192
   09)----------------HashJoinExec: mode=Partitioned, join_type=Right, on=[(e1.b + Int64(1)@1, CAST(e2.b AS Int64)@2)], projection=[b@0, a@2]
   10)------------------ProjectionExec: expr=[b@0 as b, CAST(b@0 AS Int64) + 1 as e1.b + Int64(1)]
   11)--------------------DataSourceExec: partitions=1, partition_sizes=[1]
   12)------------------ProjectionExec: expr=[a@0 as a, b@1 as b, CAST(b@1 AS Int64) as CAST(e2.b AS Int64)]
   13)--------------------DataSourceExec: partitions=1, partition_sizes=[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: github-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: Queries similar to `count-bug` produce incorrect results [datafusion]

Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on PR #15281:
URL: https://github.com/apache/datafusion/pull/15281#issuecomment-2764483579

   ```
   statement count 0
   create table t(a int, b int) as values (11, 2), (3, 0);
   
   statement count 0
   create table t2(a int, b int) as values (11, 3), (13, 1);
   
   query IT
   select e.b ,(select case when max(e2.a) > 10 then 'a' else 'b' end from t2 e2 where e2.b = e.b+1 ) from t e;
   ----
   0 a
   2 a
   
   query TT
   explain select e.b ,(select case when max(e2.a) > 10 then 'a' else 'b' end from t2 e2 where e2.b = e.b+1 ) from t e;
   ----
   logical_plan
   01)Projection: e.b, CASE WHEN __scalar_sq_1.__always_true IS NULL THEN Utf8("b") ELSE __scalar_sq_1.CASE WHEN max(e2.a) > Int64(10) THEN Utf8("a") ELSE Utf8("b") END END AS CASE WHEN max(e2.a) > Int64(10) THEN Utf8("a") ELSE Utf8("b") END
   02)--Left Join: CAST(e.b AS Int64) + Int64(1) = CAST(__scalar_sq_1.b AS Int64)
   03)----SubqueryAlias: e
   04)------TableScan: t projection=[b]
   05)----SubqueryAlias: __scalar_sq_1
   06)------Projection: CASE WHEN max(e2.a) > Int32(10) THEN Utf8("a") ELSE Utf8("b") END AS CASE WHEN max(e2.a) > Int64(10) THEN Utf8("a") ELSE Utf8("b") END, e2.b, Boolean(true) AS __always_true
   07)--------Aggregate: groupBy=[[e2.b]], aggr=[[max(e2.a)]]
   08)----------SubqueryAlias: e2
   09)------------TableScan: t2 projection=[a, b]
   physical_plan
   01)ProjectionExec: expr=[b@0 as b, CASE WHEN __always_true@2 IS NULL THEN b ELSE CASE WHEN max(e2.a) > Int64(10) THEN Utf8("a") ELSE Utf8("b") END@1 END as CASE WHEN max(e2.a) > Int64(10) THEN Utf8("a") ELSE Utf8("b") END]
   02)--CoalesceBatchesExec: target_batch_size=8192
   03)----HashJoinExec: mode=Partitioned, join_type=Left, on=[(e.b + Int64(1)@1, CAST(__scalar_sq_1.b AS Int64)@3)], projection=[b@0, CASE WHEN max(e2.a) > Int64(10) THEN Utf8("a") ELSE Utf8("b") END@2, __always_true@4]
   04)------CoalesceBatchesExec: target_batch_size=8192
   05)--------RepartitionExec: partitioning=Hash([e.b + Int64(1)@1], 4), input_partitions=1
   06)----------ProjectionExec: expr=[b@0 as b, CAST(b@0 AS Int64) + 1 as e.b + Int64(1)]
   07)------------DataSourceExec: partitions=1, partition_sizes=[1]
   08)------CoalesceBatchesExec: target_batch_size=8192
   09)--------RepartitionExec: partitioning=Hash([CAST(__scalar_sq_1.b AS Int64)@3], 4), input_partitions=4
   10)----------ProjectionExec: expr=[CASE WHEN max(e2.a)@1 > 10 THEN a ELSE b END as CASE WHEN max(e2.a) > Int64(10) THEN Utf8("a") ELSE Utf8("b") END, b@0 as b, true as __always_true, CAST(b@0 AS Int64) as CAST(__scalar_sq_1.b AS Int64)]
   11)------------AggregateExec: mode=FinalPartitioned, gby=[b@0 as b], aggr=[max(e2.a)]
   12)--------------CoalesceBatchesExec: target_batch_size=8192
   13)----------------RepartitionExec: partitioning=Hash([b@0], 4), input_partitions=4
   14)------------------RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1
   15)--------------------AggregateExec: mode=Partial, gby=[b@1 as b], aggr=[max(e2.a)]
   16)----------------------DataSourceExec: partitions=1, partition_sizes=[1]
   
   ```
   
   I guess we incorrectly consider them as "unmatched columns", even if the filter matched, we still generate `CASE WHEN __scalar_sq_1.__always_true IS NULL` condition. Ideally, we should avoid this.


-- 
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: github-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: Queries similar to `count-bug` produce incorrect results [datafusion]

Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on PR #15281:
URL: https://github.com/apache/datafusion/pull/15281#issuecomment-2764473375

   ```
   D create table t1(a int, b int);
   D insert values (11, 2), (3, 0);
   Parser Error: syntax error at or near "values"
   LINE 1: insert values (11, 2), (3, 0);
                  ^
   D insert into t1 values (11, 2), (3, 0);
   D create table t2(a int, b int);
   D insert into t2 values (11, 3), (3, 0);
   D select e.b ,(select case when max(e2.a) > 10 then 'a' else 'b' end from t2 e2 where e2.b = e.b+1 ) from t e;
   Catalog Error: Table with name t does not exist!
   Did you mean "t1"?
   LINE 1: ...d from t2 e2 where e2.b = e.b+1 ) from t e;
                                                     ^
   D select e.b ,(select case when max(e2.a) > 10 then 'a' else 'b' end from t2 e2 where e2.b = e.b+1 ) from t1 e;
   ┌───────┬───────────────────────────────────────────────────────────────────────────────────────────────────────┐
   │   b   │ (SELECT CASE  WHEN ((max(e2.a) > 10)) THEN ('a') ELSE 'b' END FROM t2 AS e2 WHERE (e2.b = (e.b + 1))) │
   │ int32 │                                                varchar                                                │
   ├───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────┤
   │     2 │ a                                                                                                     │
   │     0 │                                                                                                       │
   └───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────┘
   D select e.b ,(select case when max(e2.a) > 10 then 'a' else 'b' end from t2 e2 where e2.b = e.b+2 ) from t1 e;
   ┌───────┬───────────────────────────────────────────────────────────────────────────────────────────────────────┐
   │   b   │ (SELECT CASE  WHEN ((max(e2.a) > 10)) THEN ('a') ELSE 'b' END FROM t2 AS e2 WHERE (e2.b = (e.b + 2))) │
   │ int32 │                                                varchar                                                │
   ├───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────┤
   │     0 │                                                                                                       │
   │     2 │                                                                                                       │
   └───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────┘
   ```
   
   ```
   postgres=# select e.b ,(select case when max(e2.a) > 10 then 'a' else 'b' end from t2 e2 where e2.b = e.b+1 ) from t1 e;
    b | case 
   ---+------
    2 | a
    0 | b
   (2 rows)
   
   postgres=# select e.b ,(select case when max(e2.a) > 10 then 'a' else 'b' end from t2 e2 where e2.b = e.b+2 ) from t1 e;
    b | case 
   ---+------
    2 | b
    0 | b
   (2 rows)
   ```
   
   it seems the result is consistent with postgres


-- 
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: github-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: Queries similar to `count-bug` produce incorrect results [datafusion]

Posted by "suibianwanwank (via GitHub)" <gi...@apache.org>.
suibianwanwank commented on PR #15281:
URL: https://github.com/apache/datafusion/pull/15281#issuecomment-2769836164

   Assume the data of e1 is:
   ```
   b
   1     
   1     
   2     
   2     
   ```
   
   The data for e2 is:
   ```
   b  a
   2   1  
   2   1 
   ```
   For query:
   ```sql
   select e1.b, (select case when max(e2.a) > 10 then 'a' else 'b' end from t2 e2 where e2.b = e1.b + 1) from t1 e1;
   ```
   The returned result is:
   ```
   e1.b  expr
   2       b
   2       b
   ```
   
   But for the query:
   ```sql
   SELECT
       e1.b,
       CASE 
           WHEN MAX(e2.a) > 10 THEN 'a' 
           ELSE 'b' 
       END AS result
   FROM t2 e2
   LEFT JOIN t1 e1 ON e2.b = e1.b + 1
   GROUP BY e1.b;
   ```
   The returned result is:
   ```
   e1.b  expr
   2       b
   ```


-- 
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: github-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: Queries similar to `count-bug` produce incorrect results [datafusion]

Posted by "suibianwanwank (via GitHub)" <gi...@apache.org>.
suibianwanwank commented on PR #15281:
URL: https://github.com/apache/datafusion/pull/15281#issuecomment-2769563811

   @jayzhan211 From this field, it seems there's no issue, but how to get the e.b field if we perform aggregation on the Join. Since this query requires all rows from the left table to be included in the results. This might also involve the equivalence of Aggregate operations under outer joins.  
   ```SQL
   select e.b ,(select case when max(e2.a) > 10 then 'a' else 'b' end from t2 e2 where e2.b = e.b+1 ) from t1 e;
   ```


-- 
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: github-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: Queries similar to `count-bug` produce incorrect results [datafusion]

Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on PR #15281:
URL: https://github.com/apache/datafusion/pull/15281#issuecomment-2764487737

   ```
   For query,
   select e.b ,(select case when max(e2.a) > 10 then 'a' else 'b' end from t e2 where e2.b = e.b+1 ) from t e;
   
   After `SimplifyExpressions`, we know e2.b = 2.b + 1 is false, since they are the same column. And, then we can infer taht max(e2.a) is NULL.
   select e.b ,(select case when max(e2.a) > 10 then 'a' else 'b' end from t e2 where e2.b = e.b+1 ) from t e;
   
   we can optimize this query into
   select e.b ,(select case when NULL > 10 then 'a' else 'b' end from t e2 where e2.b = e.b+1 ) from t e;
   ```
   
   ```
   For query,
   select e.b ,(select case when max(e2.a) > 10 then 'a' else 'b' end from t2 e2 where e2.b = e.b+1 ) from t1 e;
   
   We can't do anything
   ```


-- 
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: github-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: Queries similar to `count-bug` produce incorrect results [datafusion]

Posted by "xudong963 (via GitHub)" <gi...@apache.org>.
xudong963 commented on PR #15281:
URL: https://github.com/apache/datafusion/pull/15281#issuecomment-2764488022

   > Hi @xudong963, would you be interested in reviewing this PR? Any feedback would be greatly appreciated!
   
   I'm sorry, it's weird I didn't receive the notification, I'll take a look tomorrow.


-- 
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: github-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: Queries similar to `count-bug` produce incorrect results [datafusion]

Posted by "suibianwanwank (via GitHub)" <gi...@apache.org>.
suibianwanwank commented on PR #15281:
URL: https://github.com/apache/datafusion/pull/15281#issuecomment-2764502074

   > > Hi @xudong963, would you be interested in reviewing this PR? Any feedback would be greatly appreciated!
   > 
   > I'm sorry, it's weird I didn't receive the notification, I'll take a look tomorrow.
   
   Thanks!
   


-- 
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: github-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: Queries similar to `count-bug` produce incorrect results [datafusion]

Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on PR #15281:
URL: https://github.com/apache/datafusion/pull/15281#issuecomment-2764486169

   In your original query, we have eq check on the same column, so I think we can evaluate to false in `SimplifyExpressions` before `DecorrelatePredicateSubquery`.
   
   In my query, we have eq check on different columns, in this case we have no idea on whether the column is matched or not. I guess we have nothing to do in logical layer.


-- 
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: github-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: Queries similar to `count-bug` produce incorrect results [datafusion]

Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on PR #15281:
URL: https://github.com/apache/datafusion/pull/15281#issuecomment-2772260110

   I think the result is consistent with Postgres


-- 
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: github-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: Queries similar to `count-bug` produce incorrect results [datafusion]

Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on PR #15281:
URL: https://github.com/apache/datafusion/pull/15281#issuecomment-2777259273

   Thanks @suibianwanwank 


-- 
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: github-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: Queries similar to `count-bug` produce incorrect results [datafusion]

Posted by "suibianwanwank (via GitHub)" <gi...@apache.org>.
suibianwanwank commented on PR #15281:
URL: https://github.com/apache/datafusion/pull/15281#issuecomment-2731893665

   @alamb Hi, I have updated the PR title to start with "fix," but it seems that the "bug" label has not been added. And the PR is now ready for review, Please take a look at your convenience~ Thank you!


-- 
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: github-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: Queries similar to `count-bug` produce incorrect results [datafusion]

Posted by "suibianwanwank (via GitHub)" <gi...@apache.org>.
suibianwanwank commented on PR #15281:
URL: https://github.com/apache/datafusion/pull/15281#issuecomment-2764559643

   I'm not sure if I misunderstood something. The fields on both sides of the `=` here come from two tables. I think this is similar to SELECT * FROM A t1, A t2 where t1.id = t2.id + 1
   
   Assuming the data in table T is:
   ```
   A.id
   1
   2
   ```
   
   Assuming this isn't optimized as a HashJoin, during the Join, the Cartesian product result should be:
   ```
   t1.id t2.id
   1     1
   1     2
   2     1
   2     2
   ```
   
   The final output result would be:
   ```
   t1.id t2.id
   1     2
   ```
   In this scenario, t1 and t2 originate from the same column, but "t1.id = t2.id + 1" cannot be equivalent to false. Is this situation similar to what occurs in correlated queries? 
   So I'm not sure how to implement this optimization during LogicalPlan


-- 
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: github-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: Queries similar to `count-bug` produce incorrect results [datafusion]

Posted by "suibianwanwank (via GitHub)" <gi...@apache.org>.
suibianwanwank commented on PR #15281:
URL: https://github.com/apache/datafusion/pull/15281#issuecomment-2764503913

   > ```
   >After `SimplifyExpressions`, we know e2.b = 2.b + 1 is false, since they are the same column. And, then we can infer taht >max(e2.a) is NULL.
   >select e.b ,(select case when max(e2.a) > 10 then 'a' else 'b' end from t e2 where e2.b = e.b+1 ) from t e;
   > ```
   @jayzhan211 This doesn't seem entirely correct; even with the same table, we can't guarantee that b and b+1 can't match. This is illustrated by the example you gave earlier, where b+1 might match to a different line than itself.
   ```sql
   statement count 0
   create table t(a int, b int) as values (11, 2), (3, 0);
   
   statement count 0
   create table t2(a int, b int) as values (11, 3), (12, 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: github-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Fix the well known `count-bug` similar cases [datafusion]

Posted by "suibianwanwank (via GitHub)" <gi...@apache.org>.
suibianwanwank commented on code in PR #15281:
URL: https://github.com/apache/datafusion/pull/15281#discussion_r1999272559


##########
datafusion/optimizer/src/scalar_subquery_to_join.rs:
##########
@@ -476,19 +485,19 @@ mod tests {
             .build()?;
 
         let expected = "Projection: customer.c_custkey [c_custkey:Int64]\
-        \n  Filter: customer.c_acctbal < __scalar_sq_1.sum(orders.o_totalprice) [c_custkey:Int64, c_name:Utf8, sum(orders.o_totalprice):Float64;N, o_custkey:Int64;N]\
-        \n    Left Join:  Filter: __scalar_sq_1.o_custkey = customer.c_custkey [c_custkey:Int64, c_name:Utf8, sum(orders.o_totalprice):Float64;N, o_custkey:Int64;N]\
-        \n      TableScan: customer [c_custkey:Int64, c_name:Utf8]\
-        \n      SubqueryAlias: __scalar_sq_1 [sum(orders.o_totalprice):Float64;N, o_custkey:Int64]\
-        \n        Projection: sum(orders.o_totalprice), orders.o_custkey [sum(orders.o_totalprice):Float64;N, o_custkey:Int64]\
-        \n          Aggregate: groupBy=[[orders.o_custkey]], aggr=[[sum(orders.o_totalprice)]] [o_custkey:Int64, sum(orders.o_totalprice):Float64;N]\
-        \n            Filter: orders.o_totalprice < __scalar_sq_2.sum(lineitem.l_extendedprice) [o_orderkey:Int64, o_custkey:Int64, o_orderstatus:Utf8, o_totalprice:Float64;N, sum(lineitem.l_extendedprice):Float64;N, l_orderkey:Int64;N]\
-        \n              Left Join:  Filter: __scalar_sq_2.l_orderkey = orders.o_orderkey [o_orderkey:Int64, o_custkey:Int64, o_orderstatus:Utf8, o_totalprice:Float64;N, sum(lineitem.l_extendedprice):Float64;N, l_orderkey:Int64;N]\
-        \n                TableScan: orders [o_orderkey:Int64, o_custkey:Int64, o_orderstatus:Utf8, o_totalprice:Float64;N]\
-        \n                SubqueryAlias: __scalar_sq_2 [sum(lineitem.l_extendedprice):Float64;N, l_orderkey:Int64]\
-        \n                  Projection: sum(lineitem.l_extendedprice), lineitem.l_orderkey [sum(lineitem.l_extendedprice):Float64;N, l_orderkey:Int64]\
-        \n                    Aggregate: groupBy=[[lineitem.l_orderkey]], aggr=[[sum(lineitem.l_extendedprice)]] [l_orderkey:Int64, sum(lineitem.l_extendedprice):Float64;N]\
-        \n                      TableScan: lineitem [l_orderkey:Int64, l_partkey:Int64, l_suppkey:Int64, l_linenumber:Int32, l_quantity:Float64, l_extendedprice:Float64]";
+            \n  Filter: customer.c_acctbal < __scalar_sq_1.sum(orders.o_totalprice) [c_custkey:Int64, c_name:Utf8, sum(orders.o_totalprice):Float64;N, o_custkey:Int64;N, __always_true:Boolean;N]\

Review Comment:
   These plan changes are due to redundant columns. It will be trimmed later in the optimization.



-- 
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: github-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: Queries similar to `count-bug` produce incorrect results [datafusion]

Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on PR #15281:
URL: https://github.com/apache/datafusion/pull/15281#issuecomment-2764506178

   when we compare table1.a = table2.b, we compare them row by row.
   
   ```
   t1.a t2.b
   1 1
   2 3
   3 2
   4 4
   ```
   
   ```
   t1.a=t2.b
   true
   false
   false
   true
   ```
   
   


-- 
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: github-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: Queries similar to `count-bug` produce incorrect results [datafusion]

Posted by "suibianwanwank (via GitHub)" <gi...@apache.org>.
suibianwanwank commented on PR #15281:
URL: https://github.com/apache/datafusion/pull/15281#issuecomment-2758959408

   Hi @xudong963, would you be interested in reviewing this PR? Any feedback would be greatly appreciated!


-- 
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: github-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: Queries similar to `count-bug` produce incorrect results [datafusion]

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


-- 
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: github-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: Queries similar to `count-bug` produce incorrect results [datafusion]

Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on PR #15281:
URL: https://github.com/apache/datafusion/pull/15281#issuecomment-2769195062

   ```
   [2025-04-01T12:19:41Z DEBUG datafusion_optimizer::utils] scalar_subquery_to_join:
       Projection: e.b, __scalar_sq_1.CASE WHEN max(e2.a) > Int64(10) THEN Utf8("a") ELSE Utf8("b") END AS CASE WHEN max(e2.a) > Int64(10) THEN Utf8("a") ELSE Utf8("b") END
         Left Join:  Filter: CAST(__scalar_sq_1.b AS Int64) = CAST(e.b AS Int64) + Int64(1)
           SubqueryAlias: e
             TableScan: t
           SubqueryAlias: __scalar_sq_1
             Projection: CASE WHEN max(e2.a) > Int32(10) THEN Utf8("a") ELSE Utf8("b") END AS CASE WHEN max(e2.a) > Int64(10) THEN Utf8("a") ELSE Utf8("b") END, e2.b
               Aggregate: groupBy=[[e2.b]], aggr=[[max(e2.a)]]
                 SubqueryAlias: e2
                   TableScan: t2
   ```
   
   In `scalar_subquery_to_join` optimization, we have left join on `__scalar_sq_1` and `e`.
   
   I wonder could we join the plan first and then call aggregation on top of joined plan.
   
   The plan I expected is something like this
   ```
   [2025-04-01T12:19:41Z DEBUG datafusion_optimizer::utils] scalar_subquery_to_join:
   
   Projection: CASE WHEN max(e2.a) > Int32(10) THEN Utf8("a") ELSE Utf8("b") END AS CASE WHEN 
       max(e2.a) > Int64(10) THEN Utf8("a") ELSE Utf8("b") END, e2.b
      Aggregate: groupBy=[[e2.b]], aggr=[[max(e2.a)]]
         Left Join:  Filter: CAST(e2.b AS Int64) = CAST(e.b AS Int64) + Int64(1)
           SubqueryAlias: e
             TableScan: t
           SubqueryAlias: e2
             TableScan: t2
   ```


-- 
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: github-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: Queries similar to `count-bug` produce incorrect results [datafusion]

Posted by "suibianwanwank (via GitHub)" <gi...@apache.org>.
suibianwanwank commented on PR #15281:
URL: https://github.com/apache/datafusion/pull/15281#issuecomment-2764487354

   > In your original query, we have eq check on the same column, so I think we can evaluate to false in `SimplifyExpressions` before `DecorrelatePredicateSubquery`.
   > 
   > In my query, we have eq check on different columns, in this case we have no idea on whether the column is matched or not. I guess we have nothing to do in logical layer.
   
   Now I understand what you meant before. For convenience, my example caused confusion.


-- 
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: github-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: Queries similar to `count-bug` produce incorrect results [datafusion]

Posted by "suibianwanwank (via GitHub)" <gi...@apache.org>.
suibianwanwank commented on PR #15281:
URL: https://github.com/apache/datafusion/pull/15281#issuecomment-2764481850

   I guess you might want to see this code, which outputs a True Literal at the Join to determine if there is a matching line or not
   https://github.com/apache/datafusion/blob/14635dab4ea0049bda36c5a9068138ab6fb2ecd2/datafusion/optimizer/src/decorrelate.rs#L309-L315


-- 
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: github-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: Queries similar to `count-bug` produce incorrect results [datafusion]

Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on PR #15281:
URL: https://github.com/apache/datafusion/pull/15281#issuecomment-2765965510

   > I'm not sure if I misunderstood something. The fields on both sides of the `=` here come from two tables. I think this is similar to SELECT * FROM A t1, A t2 where t1.id = t2.id + 1
   > 
   > Assuming the data in table T is:
   > 
   > ```
   > A.id
   > 1
   > 2
   > ```
   > 
   > Assuming this isn't optimized as a HashJoin, during the Join, the Cartesian product result should be:
   > 
   > ```
   > t1.id t2.id
   > 1     1
   > 1     2
   > 2     1
   > 2     2
   > ```
   > 
   > The final output result would be:
   > 
   > ```
   > t1.id t2.id
   > 1     2
   > ```
   > 
   > In this scenario, t1 and t2 originate from the same column, but "t1.id = t2.id + 1" cannot be equivalent to false. Is this situation similar to what occurs in correlated queries? So I'm not sure how to implement this optimization during LogicalPlan
   
   Alright, it is join like equality not row by row


-- 
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: github-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: Queries similar to `count-bug` produce incorrect results [datafusion]

Posted by "suibianwanwank (via GitHub)" <gi...@apache.org>.
suibianwanwank commented on PR #15281:
URL: https://github.com/apache/datafusion/pull/15281#issuecomment-2769782451

   > The projection required to be in the group expression. I think the query of these 2 are equivalent but the subquery one group by `e2.b` and the join query group by `e1.b`.
   > 
   > The only problem left is that whether this rewrite is general enough to work well on most of the subquery
   > 
   > ```
   > query IT
   > select e1.b, (select case when max(e2.a) > 10 then 'a' else 'b' end from t2 e2 where e2.b = e1.b + 1) from t1 e1;
   > ----
   > 0 a
   > 2 a
   > 
   > query TT
   > explain
   > select e1.b, (select case when max(e2.a) > 10 then 'a' else 'b' end from t2 e2 where e2.b = e1.b + 1) from t1 e1;
   > ----
   > logical_plan
   > 01)Projection: e1.b, CASE WHEN __scalar_sq_1.__always_true IS NULL THEN Utf8("b") ELSE __scalar_sq_1.CASE WHEN max(e2.a) > Int64(10) THEN Utf8("a") ELSE Utf8("b") END END AS CASE WHEN max(e2.a) > Int64(10) THEN Utf8("a") ELSE Utf8("b") END
   > 02)--Left Join: CAST(e1.b AS Int64) + Int64(1) = CAST(__scalar_sq_1.b AS Int64)
   > 03)----SubqueryAlias: e1
   > 04)------TableScan: t1 projection=[b]
   > 05)----SubqueryAlias: __scalar_sq_1
   > 06)------Projection: CASE WHEN max(e2.a) > Int32(10) THEN Utf8("a") ELSE Utf8("b") END AS CASE WHEN max(e2.a) > Int64(10) THEN Utf8("a") ELSE Utf8("b") END, e2.b, Boolean(true) AS __always_true
   > 07)--------Aggregate: groupBy=[[e2.b]], aggr=[[max(e2.a)]]
   > 08)----------SubqueryAlias: e2
   > 09)------------TableScan: t2 projection=[a, b]
   > physical_plan
   > 01)ProjectionExec: expr=[b@0 as b, CASE WHEN __always_true@2 IS NULL THEN b ELSE CASE WHEN max(e2.a) > Int64(10) THEN Utf8("a") ELSE Utf8("b") END@1 END as CASE WHEN max(e2.a) > Int64(10) THEN Utf8("a") ELSE Utf8("b") END]
   > 02)--CoalesceBatchesExec: target_batch_size=8192
   > 03)----HashJoinExec: mode=Partitioned, join_type=Left, on=[(e1.b + Int64(1)@1, CAST(__scalar_sq_1.b AS Int64)@3)], projection=[b@0, CASE WHEN max(e2.a) > Int64(10) THEN Utf8("a") ELSE Utf8("b") END@2, __always_true@4]
   > 04)------CoalesceBatchesExec: target_batch_size=8192
   > 05)--------RepartitionExec: partitioning=Hash([e1.b + Int64(1)@1], 4), input_partitions=1
   > 06)----------ProjectionExec: expr=[b@0 as b, CAST(b@0 AS Int64) + 1 as e1.b + Int64(1)]
   > 07)------------DataSourceExec: partitions=1, partition_sizes=[1]
   > 08)------CoalesceBatchesExec: target_batch_size=8192
   > 09)--------RepartitionExec: partitioning=Hash([CAST(__scalar_sq_1.b AS Int64)@3], 4), input_partitions=4
   > 10)----------ProjectionExec: expr=[CASE WHEN max(e2.a)@1 > 10 THEN a ELSE b END as CASE WHEN max(e2.a) > Int64(10) THEN Utf8("a") ELSE Utf8("b") END, b@0 as b, true as __always_true, CAST(b@0 AS Int64) as CAST(__scalar_sq_1.b AS Int64)]
   > 11)------------AggregateExec: mode=FinalPartitioned, gby=[b@0 as b], aggr=[max(e2.a)]
   > 12)--------------CoalesceBatchesExec: target_batch_size=8192
   > 13)----------------RepartitionExec: partitioning=Hash([b@0], 4), input_partitions=4
   > 14)------------------RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1
   > 15)--------------------AggregateExec: mode=Partial, gby=[b@1 as b], aggr=[max(e2.a)]
   > 16)----------------------DataSourceExec: partitions=1, partition_sizes=[1]
   > 
   > query IT
   > SELECT
   >     e1.b,
   >     CASE 
   >         WHEN MAX(e2.a) > 10 THEN 'a' 
   >         ELSE 'b' 
   >     END AS result
   > FROM t2 e2
   > LEFT JOIN t1 e1 ON e2.b = e1.b + 1
   > GROUP BY e1.b;
   > ----
   > 2 a
   > 0 a
   > 
   > query TT
   > explain
   > SELECT
   >     e1.b,
   >     CASE 
   >         WHEN MAX(e2.a) > 10 THEN 'a' 
   >         ELSE 'b' 
   >     END AS result
   > FROM t2 e2
   > LEFT JOIN t1 e1 ON e2.b = e1.b + 1
   > GROUP BY e1.b;
   > ----
   > logical_plan
   > 01)Projection: e1.b, CASE WHEN max(e2.a) > Int32(10) THEN Utf8("a") ELSE Utf8("b") END AS result
   > 02)--Aggregate: groupBy=[[e1.b]], aggr=[[max(e2.a)]]
   > 03)----Projection: e2.a, e1.b
   > 04)------Left Join: CAST(e2.b AS Int64) = CAST(e1.b AS Int64) + Int64(1)
   > 05)--------SubqueryAlias: e2
   > 06)----------TableScan: t2 projection=[a, b]
   > 07)--------SubqueryAlias: e1
   > 08)----------TableScan: t1 projection=[b]
   > physical_plan
   > 01)ProjectionExec: expr=[b@0 as b, CASE WHEN max(e2.a)@1 > 10 THEN a ELSE b END as result]
   > 02)--AggregateExec: mode=FinalPartitioned, gby=[b@0 as b], aggr=[max(e2.a)]
   > 03)----CoalesceBatchesExec: target_batch_size=8192
   > 04)------RepartitionExec: partitioning=Hash([b@0], 4), input_partitions=4
   > 05)--------AggregateExec: mode=Partial, gby=[b@1 as b], aggr=[max(e2.a)]
   > 06)----------RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1
   > 07)------------ProjectionExec: expr=[a@1 as a, b@0 as b]
   > 08)--------------CoalesceBatchesExec: target_batch_size=8192
   > 09)----------------HashJoinExec: mode=Partitioned, join_type=Right, on=[(e1.b + Int64(1)@1, CAST(e2.b AS Int64)@2)], projection=[b@0, a@2]
   > 10)------------------ProjectionExec: expr=[b@0 as b, CAST(b@0 AS Int64) + 1 as e1.b + Int64(1)]
   > 11)--------------------DataSourceExec: partitions=1, partition_sizes=[1]
   > 12)------------------ProjectionExec: expr=[a@0 as a, b@1 as b, CAST(b@1 AS Int64) as CAST(e2.b AS Int64)]
   > 13)--------------------DataSourceExec: partitions=1, partition_sizes=[1]
   > ```
   I believe there may be a difference between the two plans. In the first plan, e1.b comes from all rows in table t2. In the second plan, e1.b is deduplicated across all rows.


-- 
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: github-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org