Skip to content

[fix](fe) Fix datediff folding for zero date#64084

Open
feiniaofeiafei wants to merge 2 commits into
apache:masterfrom
feiniaofeiafei:codex-fix-datediff-zero-date-fold
Open

[fix](fe) Fix datediff folding for zero date#64084
feiniaofeiafei wants to merge 2 commits into
apache:masterfrom
feiniaofeiafei:codex-fix-datediff-zero-date-fold

Conversation

@feiniaofeiafei
Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: None

Related PR: None

Problem Summary: FE constant folding for DATEDIFF used Java proleptic date arithmetic, while BE runtime evaluates DATEDIFF by subtracting Doris day numbers. For zero-date inputs such as 0000-01-01, Java date arithmetic disagrees with Doris day-number semantics, so folded DATEDIFF could return a different result from runtime execution and from TO_DAYS(a) - TO_DAYS(b). This change reuses the FE day-number calculation used by TO_DAYS for DATEDIFF folding, keeping folded and runtime semantics consistent for zero-date boundary cases.

Release note

None

Check List (For Author)

  • Test: Unit Test
    • Attempted: ./run-fe-ut.sh --run org.apache.doris.nereids.rules.expression.FoldConstantTest
    • Result: failed before running tests because current FE compile fails in LogicalPlanBuilder.java with missing ctx.HOT(); the DATEDIFF-specific compile errors found in the first run were fixed and did not recur.
  • Behavior changed: No
  • Does this need documentation: No

What problem does this PR solve?

Issue Number: close #xxx

Related PR: #xxx

Problem Summary:

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

### What problem does this PR solve?

Issue Number: None

Related PR: None

Problem Summary: FE constant folding for DATEDIFF used Java proleptic date arithmetic, while BE runtime evaluates DATEDIFF by subtracting Doris day numbers. For zero-date inputs such as 0000-01-01, Java date arithmetic disagrees with Doris day-number semantics, so folded DATEDIFF could return a different result from runtime execution and from TO_DAYS(a) - TO_DAYS(b). This change reuses the FE day-number calculation used by TO_DAYS for DATEDIFF folding, keeping folded and runtime semantics consistent for zero-date boundary cases.

### Release note

None

### Check List (For Author)

- Test: Unit Test
    - Attempted: ./run-fe-ut.sh --run org.apache.doris.nereids.rules.expression.FoldConstantTest
    - Result: failed before running tests because current FE compile fails in LogicalPlanBuilder.java with missing ctx.HOT(); the DATEDIFF-specific compile errors found in the first run were fixed and did not recur.
- Behavior changed: No
- Does this need documentation: No
@hello-stephen
Copy link
Copy Markdown
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?

@feiniaofeiafei
Copy link
Copy Markdown
Contributor Author

run buildall

@feiniaofeiafei
Copy link
Copy Markdown
Contributor Author

run buildall

@hello-stephen
Copy link
Copy Markdown
Contributor

TPC-H: Total hot run time: 29093 ms
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/tpch-tools
Tpch sf100 test result on commit 4bc5df76e185ebdcbd3334082ec844b93be64600, data reload: false

------ Round 1 ----------------------------------
orders	Doris	NULL	NULL	0	0	0	NULL	0	NULL	NULL	2023-12-26 18:27:23	2023-12-26 18:42:55	NULL	utf-8	NULL	NULL	
============================================
q1	17717	4068	4032	4032
q2	q3	10762	1411	832	832
q4	4693	478	344	344
q5	7553	881	583	583
q6	183	180	138	138
q7	777	853	632	632
q8	9362	1615	1612	1612
q9	5707	4506	4462	4462
q10	6744	1818	1530	1530
q11	436	270	253	253
q12	632	437	294	294
q13	18136	3412	2747	2747
q14	266	255	245	245
q15	q16	816	779	708	708
q17	1003	1015	945	945
q18	6778	5846	5506	5506
q19	2339	1232	951	951
q20	526	403	268	268
q21	6499	2883	2697	2697
q22	459	377	314	314
Total cold run time: 101388 ms
Total hot run time: 29093 ms

----- Round 2, with runtime_filter_mode=off -----
orders	Doris	NULL	NULL	150000000	42	6422171781	NULL	22778155	NULL	NULL	2023-12-26 18:27:23	2023-12-26 18:42:55	NULL	utf-8	NULL	NULL	
============================================
q1	5090	4701	4746	4701
q2	q3	4842	5237	4672	4672
q4	2111	2207	1421	1421
q5	4809	4842	4677	4677
q6	234	179	130	130
q7	1890	1816	1559	1559
q8	2448	2127	2086	2086
q9	7829	7625	7375	7375
q10	4718	4695	4196	4196
q11	524	385	353	353
q12	728	744	530	530
q13	3003	3418	2805	2805
q14	271	277	250	250
q15	q16	677	689	612	612
q17	1279	1247	1251	1247
q18	7136	6748	6663	6663
q19	1109	1099	1126	1099
q20	2220	2207	1957	1957
q21	5296	4617	4461	4461
q22	528	449	430	430
Total cold run time: 56742 ms
Total hot run time: 51224 ms

@hello-stephen
Copy link
Copy Markdown
Contributor

TPC-DS: Total hot run time: 168752 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 4bc5df76e185ebdcbd3334082ec844b93be64600, data reload: false

query5	4322	621	477	477
query6	441	201	190	190
query7	4915	582	287	287
query8	368	213	212	212
query9	8792	4016	3981	3981
query10	446	316	250	250
query11	5953	2353	2169	2169
query12	161	101	102	101
query13	1269	572	421	421
query14	6444	5415	5067	5067
query14_1	4439	4361	4339	4339
query15	206	199	171	171
query16	1020	456	428	428
query17	1099	702	583	583
query18	2491	459	336	336
query19	194	183	138	138
query20	112	109	105	105
query21	215	136	128	128
query22	13609	13548	13309	13309
query23	17410	16516	16212	16212
query23_1	16246	16328	16377	16328
query24	7434	1763	1299	1299
query24_1	1314	1315	1331	1315
query25	547	449	382	382
query26	1293	305	174	174
query27	2744	523	352	352
query28	4508	2026	2024	2024
query29	1063	612	475	475
query30	319	231	200	200
query31	1119	1099	952	952
query32	105	61	57	57
query33	509	316	254	254
query34	1183	1131	635	635
query35	758	790	672	672
query36	1420	1392	1229	1229
query37	152	108	91	91
query38	3212	3133	3024	3024
query39	933	917	897	897
query39_1	891	883	872	872
query40	224	123	100	100
query41	66	63	62	62
query42	93	96	93	93
query43	319	333	280	280
query44	
query45	195	187	180	180
query46	1097	1174	720	720
query47	2423	2425	2308	2308
query48	404	434	306	306
query49	622	478	363	363
query50	976	354	257	257
query51	4318	4280	4256	4256
query52	89	89	76	76
query53	231	266	194	194
query54	272	215	216	215
query55	78	78	70	70
query56	247	220	230	220
query57	1445	1411	1347	1347
query58	243	210	221	210
query59	1566	1651	1423	1423
query60	304	261	248	248
query61	186	184	184	184
query62	699	664	565	565
query63	234	186	185	185
query64	2632	840	672	672
query65	
query66	1803	486	365	365
query67	29791	29738	29525	29525
query68	
query69	442	307	282	282
query70	916	918	949	918
query71	298	229	215	215
query72	3119	2878	2364	2364
query73	873	784	392	392
query74	5135	4987	4792	4792
query75	2651	2556	2231	2231
query76	2302	1150	735	735
query77	350	378	286	286
query78	12301	12482	11789	11789
query79	1291	1104	781	781
query80	574	479	397	397
query81	449	282	241	241
query82	363	161	124	124
query83	358	273	264	264
query84	256	149	120	120
query85	860	570	438	438
query86	375	292	301	292
query87	3346	3284	3164	3164
query88	3615	2720	2727	2720
query89	438	381	324	324
query90	1906	182	195	182
query91	177	159	134	134
query92	65	63	55	55
query93	1385	1524	833	833
query94	535	358	295	295
query95	679	372	347	347
query96	1055	782	345	345
query97	2702	2731	2595	2595
query98	215	207	203	203
query99	1162	1170	1034	1034
Total cold run time: 250887 ms
Total hot run time: 168752 ms

@hello-stephen
Copy link
Copy Markdown
Contributor

TPC-H: Total hot run time: 29560 ms
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/tpch-tools
Tpch sf100 test result on commit c32466d9622b69cbcb85add2d7868f736b47650f, data reload: false

------ Round 1 ----------------------------------
orders	Doris	NULL	NULL	0	0	0	NULL	0	NULL	NULL	2023-12-26 18:27:23	2023-12-26 18:42:55	NULL	utf-8	NULL	NULL	
============================================
q1	17592	4177	4150	4150
q2	q3	10846	1408	792	792
q4	4689	478	355	355
q5	7612	885	599	599
q6	191	187	150	150
q7	789	870	636	636
q8	9422	1601	1581	1581
q9	6000	4522	4492	4492
q10	6747	1814	1515	1515
q11	434	277	257	257
q12	629	435	313	313
q13	18172	3354	2781	2781
q14	268	263	252	252
q15	q16	803	769	709	709
q17	994	960	956	956
q18	6921	5810	5598	5598
q19	1302	1249	1075	1075
q20	519	402	265	265
q21	6273	2970	2757	2757
q22	491	398	327	327
Total cold run time: 100694 ms
Total hot run time: 29560 ms

----- Round 2, with runtime_filter_mode=off -----
orders	Doris	NULL	NULL	150000000	42	6422171781	NULL	22778155	NULL	NULL	2023-12-26 18:27:23	2023-12-26 18:42:55	NULL	utf-8	NULL	NULL	
============================================
q1	5335	4982	4783	4783
q2	q3	4972	5324	4672	4672
q4	2107	2225	1367	1367
q5	4862	4939	4688	4688
q6	229	180	134	134
q7	1905	1898	1578	1578
q8	2452	2189	2098	2098
q9	7964	7598	7516	7516
q10	4693	4689	4244	4244
q11	553	407	362	362
q12	743	740	533	533
q13	3022	3471	2784	2784
q14	266	278	248	248
q15	q16	688	694	615	615
q17	1303	1255	1276	1255
q18	7546	6803	6961	6803
q19	1124	1105	1150	1105
q20	2205	2214	1951	1951
q21	5345	4596	4627	4596
q22	528	486	416	416
Total cold run time: 57842 ms
Total hot run time: 51748 ms

@hello-stephen
Copy link
Copy Markdown
Contributor

TPC-DS: Total hot run time: 172066 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 c32466d9622b69cbcb85add2d7868f736b47650f, data reload: false

query5	4390	673	503	503
query6	474	223	227	223
query7	4837	573	316	316
query8	413	265	248	248
query9	8790	4132	4135	4132
query10	472	335	293	293
query11	5908	2364	2160	2160
query12	163	109	106	106
query13	1332	627	474	474
query14	6498	5491	5203	5203
query14_1	4499	4480	4460	4460
query15	217	207	183	183
query16	1070	488	479	479
query17	1180	774	637	637
query18	2761	510	388	388
query19	231	202	163	163
query20	121	118	113	113
query21	224	150	125	125
query22	13666	13543	13411	13411
query23	17210	16598	16185	16185
query23_1	16253	16439	16365	16365
query24	7667	1802	1353	1353
query24_1	1352	1359	1355	1355
query25	629	545	466	466
query26	1305	335	185	185
query27	2594	511	352	352
query28	4433	2041	2028	2028
query29	1166	677	567	567
query30	319	248	210	210
query31	1134	1104	974	974
query32	117	70	67	67
query33	563	366	316	316
query34	1187	1172	653	653
query35	777	781	694	694
query36	1401	1416	1219	1219
query37	159	113	99	99
query38	3217	3150	3044	3044
query39	964	924	927	924
query39_1	881	877	910	877
query40	234	136	111	111
query41	80	83	75	75
query42	101	98	99	98
query43	324	338	301	301
query44	
query45	210	191	184	184
query46	1096	1246	756	756
query47	2381	2375	2238	2238
query48	413	436	308	308
query49	669	515	395	395
query50	1070	374	263	263
query51	4392	4274	4225	4225
query52	91	95	83	83
query53	252	284	207	207
query54	303	250	233	233
query55	84	81	73	73
query56	268	257	259	257
query57	1428	1468	1349	1349
query58	263	236	225	225
query59	1662	1706	1457	1457
query60	298	274	260	260
query61	203	196	194	194
query62	706	661	597	597
query63	236	193	193	193
query64	2638	888	750	750
query65	
query66	1851	508	428	428
query67	29797	29703	29424	29424
query68	
query69	458	327	279	279
query70	1006	981	938	938
query71	307	235	222	222
query72	3303	2979	2687	2687
query73	820	797	456	456
query74	5160	4989	4808	4808
query75	2712	2616	2291	2291
query76	2348	1160	802	802
query77	401	395	311	311
query78	12546	12441	11876	11876
query79	1477	1048	781	781
query80	1369	525	461	461
query81	513	295	255	255
query82	602	163	125	125
query83	376	302	265	265
query84	282	155	125	125
query85	1084	674	563	563
query86	445	297	281	281
query87	3398	3349	3200	3200
query88	3767	2819	2777	2777
query89	445	382	338	338
query90	1946	196	190	190
query91	201	192	162	162
query92	77	66	60	60
query93	1572	1449	924	924
query94	766	382	341	341
query95	711	410	466	410
query96	1017	859	345	345
query97	2683	2740	2571	2571
query98	215	213	205	205
query99	1185	1192	1040	1040
Total cold run time: 255364 ms
Total hot run time: 172066 ms

@hello-stephen
Copy link
Copy Markdown
Contributor

FE Regression Coverage Report

Increment line coverage 66.67% (4/6) 🎉
Increment coverage report
Complete coverage report

@feiniaofeiafei
Copy link
Copy Markdown
Contributor Author

run p0

@feiniaofeiafei
Copy link
Copy Markdown
Contributor Author

run cloud_p0

@feiniaofeiafei
Copy link
Copy Markdown
Contributor Author

run external

@feiniaofeiafei
Copy link
Copy Markdown
Contributor Author

run nonConcurrent

@hello-stephen
Copy link
Copy Markdown
Contributor

FE Regression Coverage Report

Increment line coverage 1.93% (4/207) 🎉
Increment coverage report
Complete coverage report

3 similar comments
@hello-stephen
Copy link
Copy Markdown
Contributor

FE Regression Coverage Report

Increment line coverage 1.93% (4/207) 🎉
Increment coverage report
Complete coverage report

@hello-stephen
Copy link
Copy Markdown
Contributor

FE Regression Coverage Report

Increment line coverage 1.93% (4/207) 🎉
Increment coverage report
Complete coverage report

@hello-stephen
Copy link
Copy Markdown
Contributor

FE Regression Coverage Report

Increment line coverage 1.93% (4/207) 🎉
Increment coverage report
Complete coverage report

@morrySnow
Copy link
Copy Markdown
Contributor

/review

@github-actions github-actions Bot added the approved Indicates a PR has been approved by one committer. label Jun 5, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 5, 2026

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

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 5, 2026

PR approved by anyone and no changes requested.

Copy link
Copy Markdown
Contributor

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

Code review completed. I did not find any blocking correctness issue in this PR.

Critical checkpoint conclusions:

  • Goal and test: The PR aims to make Nereids constant folding for datediff use Doris day-number semantics instead of Java date arithmetic. The change accomplishes this for the modified DateV2/DateTimeV2 folding paths, and the added FE unit cases cover the reported 0000-01-01 boundary for DateV2 and DateTimeV2 operands.
  • Scope and clarity: The change is small and focused. It reuses the existing FE to_days day-number calculation rather than adding a separate divergent implementation.
  • Concurrency and lifecycle: No concurrency, locking, static initialization, or non-trivial lifecycle behavior is introduced.
  • Configuration and compatibility: No new configuration, storage format, FE-BE protocol, or incompatible behavior is introduced.
  • Parallel paths: BE runtime datediff subtracts daynr() values, and the exposed FE helper mirrors BE calc_daynr special handling. I did not find another changed path that needs the same modification in this PR.
  • Conditional logic: The zero-date special handling already exists in the shared day-number helper and matches BE semantics.
  • Test coverage: Coverage is limited to FE unit tests and specifically includes the zero-date folding case. I did not run tests locally in this review.
  • Observability: No new observability is needed for this constant-folding change.
  • Transaction, persistence, and data writes: Not applicable.
  • Performance and memory: The new logic is constant-time and does not introduce material allocation or memory-tracking concerns.

User focus response: No additional user-provided review focus was supplied.

@hello-stephen
Copy link
Copy Markdown
Contributor

FE Regression Coverage Report

Increment line coverage 1.32% (4/304) 🎉
Increment coverage report
Complete coverage report

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. dev/3.1.x dev/4.0.x dev/4.1.x reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants