Skip to content

Conversation

@yujun777
Copy link
Contributor

@yujun777 yujun777 commented Sep 25, 2025

for nested case when, replace the inner case duplicate condition to true/false when this condition also exists in outer case condition:

  1. if it exists in outer case's current branch condition, replace it with TRUE:
case when A then (case when A and B then 1 else 2 end)
...
end

then inner case condition A will replace with TRUE:

case when A then  (case when TRUE and B then 1 else 2 end)
...
end
  1. if it exists in outer case's previous branch condition, replace it with FALSE
case when A then C 
     when B then (case when A and D then 1 else 2 end)                 
 ...
end

then inner case condition A will replace with FALSE:

case when A then C 
     when B then (case when FALSE and D then 1 else 2 end)                    
...
end

this PR also opt fold case when and fold if statement. for case when / if expression, if all their branches values equals, then rewrite them to the same value.

Release note

None

Check List (For Author)

  • Test

    • Regression test
    • Unit Test
    • Manual test (add detailed scripts or steps below)
    • No need to test or manual test. Explain why:
      • This is a refactor/code format and no logic has been changed.
      • Previous test can cover this change.
      • No code files have been changed.
      • Other reason
  • Behavior changed:

    • No.
    • Yes.
  • Does this need documentation?

    • No.
    • Yes.

Check List (For Reviewer who merge this PR)

  • Confirm the release note
  • Confirm test cases
  • Confirm document
  • Add branch pick label

@hello-stephen
Copy link
Contributor

Thank you for your contribution to Apache Doris.
Don't know what should be done next? See How to process your PR.

Please clearly describe your PR:

  1. What problem was fixed (it's best to include specific error reporting information). How it was fixed.
  2. Which behaviors were modified. What was the previous behavior, what is it now, why was it modified, and what possible impacts might there be.
  3. What features were added. Why was this function added?
  4. Which code was refactored and why was this part of the code refactored?
  5. Which functions were optimized and what is the difference before and after the optimization?

@yujun777
Copy link
Contributor Author

run buildall

@yujun777 yujun777 force-pushed the replace-inner-case-when-cond branch from 3fa3969 to 77aefc8 Compare September 25, 2025 13:49
@yujun777
Copy link
Contributor Author

run buildall

@yujun777 yujun777 force-pushed the replace-inner-case-when-cond branch from 77aefc8 to e3f49ae Compare September 25, 2025 14:01
@yujun777
Copy link
Contributor Author

run buildall

@doris-robot
Copy link

ClickBench: Total hot run time: 30.04 s
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/clickbench-tools
ClickBench test result on commit e3f49aed7dd8f816111dcec8b641637ebf6ff9d0, data reload: false

query1	0.06	0.04	0.05
query2	0.09	0.05	0.05
query3	0.25	0.08	0.09
query4	1.61	0.12	0.12
query5	0.28	0.26	0.25
query6	1.18	0.66	0.64
query7	0.03	0.03	0.02
query8	0.06	0.05	0.05
query9	0.59	0.52	0.50
query10	0.58	0.61	0.58
query11	0.16	0.11	0.12
query12	0.15	0.12	0.12
query13	0.62	0.63	0.62
query14	1.02	1.03	1.03
query15	0.90	0.87	0.90
query16	0.41	0.40	0.40
query17	1.08	1.07	1.03
query18	0.21	0.20	0.20
query19	1.92	1.82	1.83
query20	0.02	0.01	0.01
query21	15.42	0.92	0.57
query22	0.77	1.08	0.67
query23	15.03	1.38	0.62
query24	7.29	1.76	0.36
query25	0.30	0.11	0.12
query26	0.65	0.16	0.13
query27	0.07	0.07	0.05
query28	8.78	1.39	0.92
query29	12.53	3.91	3.34
query30	0.28	0.14	0.11
query31	2.83	0.61	0.39
query32	3.26	0.57	0.49
query33	3.10	3.08	3.21
query34	16.30	5.50	4.87
query35	4.92	4.90	4.89
query36	0.71	0.50	0.50
query37	0.10	0.07	0.08
query38	0.07	0.05	0.05
query39	0.03	0.04	0.03
query40	0.19	0.16	0.14
query41	0.10	0.04	0.04
query42	0.04	0.03	0.03
query43	0.04	0.04	0.04
Total cold run time: 104.03 s
Total hot run time: 30.04 s

@hello-stephen
Copy link
Contributor

FE UT Coverage Report

Increment line coverage 93.98% (125/133) 🎉
Increment coverage report
Complete coverage report

@hello-stephen
Copy link
Contributor

FE Regression Coverage Report

Increment line coverage 78.95% (105/133) 🎉
Increment coverage report
Complete coverage report

@yujun777
Copy link
Contributor Author

run buildall

@yujun777 yujun777 changed the title [feat](case when) replace nested inner case when condition to true/false [feat](case when) replace nested inner case when duplicate condition to true/false Sep 26, 2025
@doris-robot
Copy link

ClickBench: Total hot run time: 30.04 s
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/clickbench-tools
ClickBench test result on commit d95ae1dde89db4046691a1cb5bfd5240dfd22f2d, data reload: false

query1	0.06	0.06	0.05
query2	0.10	0.06	0.06
query3	0.27	0.08	0.08
query4	1.61	0.12	0.12
query5	0.28	0.26	0.25
query6	1.21	0.66	0.65
query7	0.03	0.02	0.02
query8	0.05	0.05	0.05
query9	0.61	0.54	0.52
query10	0.58	0.61	0.58
query11	0.16	0.11	0.12
query12	0.15	0.12	0.12
query13	0.63	0.63	0.62
query14	1.02	1.03	1.01
query15	0.88	0.84	0.87
query16	0.41	0.40	0.41
query17	1.05	1.05	1.03
query18	0.22	0.20	0.20
query19	1.99	1.87	1.79
query20	0.01	0.01	0.02
query21	15.43	0.91	0.58
query22	0.76	1.12	0.59
query23	15.09	1.40	0.61
query24	6.78	2.28	0.59
query25	0.48	0.20	0.15
query26	0.69	0.16	0.14
query27	0.07	0.06	0.05
query28	9.58	1.38	0.92
query29	12.57	3.91	3.24
query30	0.29	0.14	0.12
query31	2.83	0.58	0.38
query32	3.26	0.58	0.49
query33	3.07	3.06	3.08
query34	16.20	5.45	4.84
query35	4.94	4.86	4.92
query36	0.69	0.53	0.52
query37	0.10	0.08	0.07
query38	0.06	0.05	0.04
query39	0.03	0.04	0.03
query40	0.18	0.16	0.15
query41	0.08	0.03	0.04
query42	0.04	0.04	0.04
query43	0.04	0.04	0.03
Total cold run time: 104.58 s
Total hot run time: 30.04 s

@hello-stephen
Copy link
Contributor

FE Regression Coverage Report

Increment line coverage 78.95% (105/133) 🎉
Increment coverage report
Complete coverage report

@yujun777
Copy link
Contributor Author

run p0

@yujun777
Copy link
Contributor Author

run feut

@hello-stephen
Copy link
Contributor

FE Regression Coverage Report

Increment line coverage 78.95% (105/133) 🎉
Increment coverage report
Complete coverage report

Comment on lines 589 to 601
Expression typeCoercionThen = TypeCoercionUtils.ensureSameResultType(
originCaseWhen, whenClause.getResult(), context);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why need to call ensureSameResultType? after analysis, all when clause's result and defualt value should be same type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update

// a) when enter a case/if branch, set this condition to TRUE literal
// b) when leave a case/if branch, set this condition to FALSE literal
// c) when leave the whole case/if statement, remove this condition literal
private final Map<Expression, BooleanLiteral> conditionLiterals = Maps.newHashMap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think u'd better add a ut to check this map work as u expected under all conditions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add ut

@yujun777 yujun777 force-pushed the replace-inner-case-when-cond branch from d95ae1d to f15a4b8 Compare September 30, 2025 03:45
@yujun777
Copy link
Contributor Author

run buildall

@yujun777 yujun777 force-pushed the replace-inner-case-when-cond branch from 8696a2a to 9298b5e Compare September 30, 2025 04:02
@yujun777
Copy link
Contributor Author

run buildall

@yujun777 yujun777 force-pushed the replace-inner-case-when-cond branch from 097dfe5 to 0f8ad80 Compare September 30, 2025 08:00
@yujun777
Copy link
Contributor Author

run buildall

@yujun777
Copy link
Contributor Author

run buildall

@yujun777
Copy link
Contributor Author

run buildall

@doris-robot
Copy link

ClickBench: Total hot run time: 30.74 s
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/clickbench-tools
ClickBench test result on commit 9d054ee5c9a2e1f85a3569e0a2019d08a8a885b2, data reload: false

query1	0.06	0.06	0.05
query2	0.09	0.05	0.06
query3	0.25	0.08	0.08
query4	1.61	0.12	0.12
query5	0.29	0.26	0.27
query6	1.19	0.64	0.66
query7	0.03	0.02	0.02
query8	0.06	0.05	0.05
query9	0.65	0.52	0.52
query10	0.58	0.60	0.60
query11	0.17	0.12	0.12
query12	0.15	0.12	0.12
query13	0.64	0.63	0.61
query14	1.03	1.04	1.04
query15	0.90	0.89	0.86
query16	0.41	0.41	0.40
query17	1.07	1.05	1.06
query18	0.22	0.20	0.20
query19	1.92	1.85	1.84
query20	0.01	0.02	0.02
query21	15.43	0.90	0.57
query22	0.77	1.14	0.73
query23	14.91	1.39	0.62
query24	6.49	1.95	0.91
query25	0.47	0.19	0.09
query26	0.68	0.16	0.14
query27	0.06	0.06	0.05
query28	9.38	1.37	0.93
query29	12.55	3.94	3.27
query30	0.28	0.14	0.12
query31	2.84	0.60	0.39
query32	3.25	0.56	0.47
query33	3.07	3.07	3.21
query34	16.26	5.52	4.89
query35	4.98	4.98	4.96
query36	0.70	0.53	0.51
query37	0.10	0.07	0.08
query38	0.06	0.06	0.04
query39	0.04	0.03	0.03
query40	0.18	0.16	0.14
query41	0.08	0.04	0.03
query42	0.04	0.03	0.04
query43	0.04	0.04	0.03
Total cold run time: 103.99 s
Total hot run time: 30.74 s

@yujun777
Copy link
Contributor Author

run buildall

@doris-robot
Copy link

TPC-DS: Total hot run time: 190424 ms
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/tpcds-tools
TPC-DS sf100 test result on commit cca571a55047b1bfb3545786840712c53ecd67fa, data reload: false

query1	1065	435	421	421
query2	6564	1686	1677	1677
query3	6753	221	224	221
query4	26206	23836	23189	23189
query5	5220	650	488	488
query6	346	262	246	246
query7	4651	508	306	306
query8	304	273	264	264
query9	8692	2558	2575	2558
query10	541	347	288	288
query11	16196	15540	14976	14976
query12	190	133	117	117
query13	1684	564	461	461
query14	12603	9296	9217	9217
query15	256	189	182	182
query16	7749	679	511	511
query17	1572	817	691	691
query18	2092	448	354	354
query19	263	240	192	192
query20	140	138	138	138
query21	264	141	116	116
query22	4902	4859	4961	4859
query23	34563	33497	33666	33497
query24	8272	2517	2545	2517
query25	587	567	469	469
query26	1236	283	179	179
query27	3254	542	398	398
query28	4365	2202	2167	2167
query29	823	644	566	566
query30	379	245	205	205
query31	957	839	803	803
query32	85	82	69	69
query33	1424	376	331	331
query34	828	869	537	537
query35	845	842	796	796
query36	995	1076	952	952
query37	128	112	82	82
query38	3606	3633	3497	3497
query39	1475	1408	1406	1406
query40	219	126	112	112
query41	59	59	57	57
query42	161	112	122	112
query43	487	497	470	470
query44	1335	858	821	821
query45	183	176	173	173
query46	838	1008	649	649
query47	1754	1800	1774	1774
query48	405	443	321	321
query49	764	510	413	413
query50	639	703	406	406
query51	3840	3936	3925	3925
query52	107	108	98	98
query53	236	277	204	204
query54	599	602	521	521
query55	87	81	84	81
query56	337	314	323	314
query57	1181	1216	1132	1132
query58	286	272	283	272
query59	2558	2624	2584	2584
query60	375	348	328	328
query61	158	155	162	155
query62	822	747	691	691
query63	242	214	206	206
query64	4776	1272	968	968
query65	4109	4001	4009	4001
query66	1102	460	330	330
query67	16167	15389	15021	15021
query68	9286	975	597	597
query69	488	326	284	284
query70	1322	1318	1351	1318
query71	506	346	316	316
query72	5883	4808	4834	4808
query73	698	562	362	362
query74	8917	9307	8709	8709
query75	4500	3385	2818	2818
query76	3813	1166	755	755
query77	945	413	324	324
query78	9667	9882	8975	8975
query79	4036	834	582	582
query80	693	565	528	528
query81	483	267	224	224
query82	476	163	133	133
query83	294	268	249	249
query84	301	120	95	95
query85	880	477	431	431
query86	341	302	297	297
query87	3819	3807	3621	3621
query88	3281	2241	2246	2241
query89	440	324	302	302
query90	2057	224	220	220
query91	165	162	136	136
query92	92	70	69	69
query93	2591	995	645	645
query94	694	423	337	337
query95	397	323	318	318
query96	499	599	289	289
query97	2953	2970	2867	2867
query98	239	225	209	209
query99	1468	1400	1283	1283
Total cold run time: 288125 ms
Total hot run time: 190424 ms

@doris-robot
Copy link

ClickBench: Total hot run time: 30.74 s
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/clickbench-tools
ClickBench test result on commit cca571a55047b1bfb3545786840712c53ecd67fa, data reload: false

query1	0.06	0.06	0.06
query2	0.09	0.06	0.06
query3	0.25	0.08	0.08
query4	1.61	0.11	0.12
query5	0.28	0.26	0.25
query6	1.18	0.66	0.64
query7	0.04	0.03	0.03
query8	0.05	0.04	0.05
query9	0.64	0.54	0.52
query10	0.61	0.58	0.58
query11	0.16	0.10	0.11
query12	0.15	0.12	0.12
query13	0.63	0.63	0.61
query14	1.01	1.06	1.04
query15	0.89	0.86	0.86
query16	0.39	0.39	0.42
query17	1.05	1.02	1.04
query18	0.22	0.19	0.20
query19	1.90	1.82	1.86
query20	0.02	0.02	0.01
query21	15.45	0.94	0.58
query22	0.76	1.16	0.67
query23	14.99	1.38	0.66
query24	7.40	1.25	1.12
query25	0.51	0.29	0.08
query26	0.63	0.15	0.13
query27	0.07	0.05	0.05
query28	9.31	1.34	0.93
query29	12.56	3.89	3.27
query30	0.28	0.15	0.11
query31	2.82	0.60	0.38
query32	3.23	0.56	0.48
query33	3.07	3.14	3.16
query34	16.00	5.47	4.79
query35	4.90	4.93	4.94
query36	0.70	0.52	0.51
query37	0.10	0.07	0.07
query38	0.06	0.05	0.05
query39	0.04	0.03	0.03
query40	0.17	0.16	0.15
query41	0.09	0.03	0.03
query42	0.04	0.03	0.02
query43	0.05	0.03	0.03
Total cold run time: 104.46 s
Total hot run time: 30.74 s

@hello-stephen
Copy link
Contributor

FE UT Coverage Report

Increment line coverage 94.96% (132/139) 🎉
Increment coverage report
Complete coverage report

@hello-stephen
Copy link
Contributor

FE Regression Coverage Report

Increment line coverage 79.14% (110/139) 🎉
Increment coverage report
Complete coverage report

@yujun777
Copy link
Contributor Author

run p0

@hello-stephen
Copy link
Contributor

FE Regression Coverage Report

Increment line coverage 79.14% (110/139) 🎉
Increment coverage report
Complete coverage report

@github-actions github-actions bot added the approved Indicates a PR has been approved by one committer. label Oct 9, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Oct 9, 2025

PR approved by at least one committer and no changes requested.

@github-actions
Copy link
Contributor

github-actions bot commented Oct 9, 2025

PR approved by anyone and no changes requested.

@morrySnow morrySnow merged commit 00ffa86 into apache:master Oct 10, 2025
27 of 28 checks passed
yujun777 added a commit to yujun777/doris that referenced this pull request Nov 20, 2025
…to true/false (apache#56469)

for nested case when, replace the inner case duplicate condition to
true/false when this condition also exists in outer case condition:

1. if it exists in outer case's current branch condition, replace it with TRUE:
case when A then (case when A and B then 1 else 2 end)
 ...
end

then inner case condition A will replace with TRUE:
case when A then  (case when TRUE and B then 1 else 2 end)
...
end

2. if it exists in outer case's previous branch condition, replace it with FALSE:
case when A then C 
     when B then (case when A and D then 1 else 2 end)                 
 ...
end

then inner case condition A will replace with FALSE:
case when A then C 
     when B then (case when FALSE and D then 1 else 2 end)                    
...
end

this PR also opt fold case when and fold if statement. for case when /
if expression, if all their branches values equals, then rewrite them to
the same value.
yujun777 added a commit to yujun777/doris that referenced this pull request Dec 1, 2025
…to true/false (apache#56469)

for nested case when, replace the inner case duplicate condition to
true/false when this condition also exists in outer case condition:

1. if it exists in outer case's current branch condition, replace it with TRUE:
case when A then (case when A and B then 1 else 2 end)
 ...
end

then inner case condition A will replace with TRUE:
case when A then  (case when TRUE and B then 1 else 2 end)
...
end

2. if it exists in outer case's previous branch condition, replace it with FALSE:
case when A then C 
     when B then (case when A and D then 1 else 2 end)                 
 ...
end

then inner case condition A will replace with FALSE:
case when A then C 
     when B then (case when FALSE and D then 1 else 2 end)                    
...
end

this PR also opt fold case when and fold if statement. for case when /
if expression, if all their branches values equals, then rewrite them to
the same value.
yujun777 added a commit to yujun777/doris that referenced this pull request Dec 2, 2025
…to true/false (apache#56469)

for nested case when, replace the inner case duplicate condition to
true/false when this condition also exists in outer case condition:

1. if it exists in outer case's current branch condition, replace it with TRUE:
case when A then (case when A and B then 1 else 2 end)
 ...
end

then inner case condition A will replace with TRUE:
case when A then  (case when TRUE and B then 1 else 2 end)
...
end

2. if it exists in outer case's previous branch condition, replace it with FALSE:
case when A then C
     when B then (case when A and D then 1 else 2 end)
 ...
end

then inner case condition A will replace with FALSE:
case when A then C
     when B then (case when FALSE and D then 1 else 2 end)
...
end

this PR also opt fold case when and fold if statement. for case when /
if expression, if all their branches values equals, then rewrite them to
the same value.
yujun777 added a commit to yujun777/doris that referenced this pull request Dec 8, 2025
…to true/false (apache#56469)

for nested case when, replace the inner case duplicate condition to
true/false when this condition also exists in outer case condition:

1. if it exists in outer case's current branch condition, replace it with TRUE:
case when A then (case when A and B then 1 else 2 end)
 ...
end

then inner case condition A will replace with TRUE:
case when A then  (case when TRUE and B then 1 else 2 end)
...
end

2. if it exists in outer case's previous branch condition, replace it with FALSE:
case when A then C
     when B then (case when A and D then 1 else 2 end)
 ...
end

then inner case condition A will replace with FALSE:
case when A then C
     when B then (case when FALSE and D then 1 else 2 end)
...
end

this PR also opt fold case when and fold if statement. for case when /
if expression, if all their branches values equals, then rewrite them to
the same value.
yujun777 added a commit to yujun777/doris that referenced this pull request Jan 9, 2026
…to true/false (apache#56469)

for nested case when, replace the inner case duplicate condition to
true/false when this condition also exists in outer case condition:

1. if it exists in outer case's current branch condition, replace it with TRUE:
case when A then (case when A and B then 1 else 2 end)
 ...
end

then inner case condition A will replace with TRUE:
case when A then  (case when TRUE and B then 1 else 2 end)
...
end

2. if it exists in outer case's previous branch condition, replace it with FALSE:
case when A then C
     when B then (case when A and D then 1 else 2 end)
 ...
end

then inner case condition A will replace with FALSE:
case when A then C
     when B then (case when FALSE and D then 1 else 2 end)
...
end

this PR also opt fold case when and fold if statement. for case when /
if expression, if all their branches values equals, then rewrite them to
the same value.
yujun777 added a commit to yujun777/doris that referenced this pull request Jan 12, 2026
…to true/false (apache#56469)

for nested case when, replace the inner case duplicate condition to
true/false when this condition also exists in outer case condition:

1. if it exists in outer case's current branch condition, replace it with TRUE:
case when A then (case when A and B then 1 else 2 end)
 ...
end

then inner case condition A will replace with TRUE:
case when A then  (case when TRUE and B then 1 else 2 end)
...
end

2. if it exists in outer case's previous branch condition, replace it with FALSE:
case when A then C
     when B then (case when A and D then 1 else 2 end)
 ...
end

then inner case condition A will replace with FALSE:
case when A then C
     when B then (case when FALSE and D then 1 else 2 end)
...
end

this PR also opt fold case when and fold if statement. for case when /
if expression, if all their branches values equals, then rewrite them to
the same value.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by one committer. reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants