Skip to content

[improvement](parser) Skip hidden tokens in internal SQL parsing - #67434

Open
morrySnow wants to merge 1 commit into
apache:masterfrom
morrySnow:codex/antlr4-lean-token-mode
Open

[improvement](parser) Skip hidden tokens in internal SQL parsing#67434
morrySnow wants to merge 1 commit into
apache:masterfrom
morrySnow:codex/antlr4-lean-token-mode

Conversation

@morrySnow

@morrySnow morrySnow commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: None

Related PR: None

Problem Summary: Internal Nereids AST parsing allocates whitespace and ordinary line-comment tokens even though ANTLR does not consume hidden-channel tokens. This independent PR adds an opt-in lean lexer mode for direct AST construction and skips only WS and SIMPLE_COMMENT. Public lexer, scan, and comment-normalization paths remain in full-token mode; BRACKETED_COMMENT remains on channel 2 for hints; character offsets are unchanged. Syntax failures are retried with full tokens so the existing exception type, position, and diagnostic text are preserved.

Benchmark

Lower allocation is better. Normalized allocation (gc.alloc.rate.norm) is the primary metric because wall-clock results were affected by host scheduling noise.

  • Host: MacBookPro17,1, Apple M1 (8 cores, 16 GB), macOS 15.0.1
  • Runtime: OpenJDK 17.0.20.1, ANTLR 4.13.1, JMH 1.37, 1 thread, 1 GB heap
  • JMH: 3 forks, 4 x 300 ms warmup, 7 x 400 ms measurement, -prof gc
  • Baseline: 049410596f4 (upstream/master); parser JAR SHA-256 923ed2a22142ee9b5dcfefbba5766b9696a653a4218e8208e42a61270e9d986f; benchmark JAR SHA-256 f88c09c16f5872ff1e8cef2368e83622cf9cc6e4217350cc845a862dbaa92f56
  • Candidate: 5ef28695247; parser JAR SHA-256 20104fd49c902833898e1132f6fb6b2bf200a7608356546d92829543e581593e; benchmark JAR SHA-256 6ecd1a5566f386729b0a7c8b1c4f3db7e7719975cb06512ed04a5f1fc8e33b64
  • Workloads: typical SELECT, 32-column comment-heavy SELECT, 128-column wide SELECT, and hinted join SELECT

Full and lean values below are measured in the same candidate artifact, so token mode is the only changed variable. Values are bytes/op.

Path Workload Full Lean Change
Tokenize Typical 3,448.0 2,541.4 -26.3%
Tokenize Comment-heavy 12,874.8 6,544.2 -49.2%
Tokenize Wide SELECT 63,832.1 40,754.9 -36.2%
Tokenize Hinted 3,328.0 2,565.4 -22.9%
Parser CST Typical 15,319.7 14,605.6 -4.7%
Parser CST Comment-heavy 52,838.8 46,501.7 -12.0%
Parser CST Wide SELECT 263,463.9 240,372.4 -8.8%
Parser CST Hinted 15,098.4 14,503.0 -3.9%
FE LogicalPlan Typical 29,080.4 28,229.5 -2.9%
FE LogicalPlan Comment-heavy 89,697.9 83,261.4 -7.2%
FE LogicalPlan Wide SELECT 485,763.7 462,621.3 -4.8%
FE LogicalPlan Hinted 34,862.4 34,062.6 -2.3%

The reduction comes from avoiding CommonToken allocation for whitespace and ordinary line comments. Hint and default-channel tokens are still allocated. A frozen-artifact B/C/C/B control with lean mode disabled showed no normalized-allocation regression for SELECT 1 (-0.8%) or a typical SELECT (-0.6%). Control latency was scheduling-sensitive, including one severely disturbed baseline fork, so it is not used as acceptance evidence.

Commands:

cd fe
mvn -Pbenchmark -pl fe-sql-parser,fe-sql-parser-benchmark \
  -Dmaven.build.cache.enabled=false package

java -jar fe-sql-parser-benchmark/target/doris-fe-sql-parser-benchmarks.jar \
  LeanTokenModeBenchmark -prof gc

Semantic differential

  • Corpus: all 4,610 tracked *.sql files; SHA-256 of git ls-files -s -- '*.sql' is 567e209d57e5eaf6546ff03bf887437b8d647ed5f7ecb85bc657b987dd04be10
  • Current master and candidate both accepted 4,063 files and rejected 547 files in Legacy and ANSI modes
  • Baseline/candidate CST and exact-error-message signatures matched byte-for-byte: Legacy 8036532e957b06a1bbc106338a1caa08cbc9acbd2d08f6eb4fcd7665f6881b2a; ANSI e27f127f60bcc1ce47bbc4ec4cc83351acc0adceb735488fdcb9853727e2c394
  • Candidate full versus lean: 18,440 combinations across the corpus, noBackslashEscapes, and ANSI modes had identical filtered token tuples and CST signatures
  • Focused FE tests cover hints, full-token public consumers, CREATE VIEW and sync-MV source intervals, encryption offsets, and exact invalid-SQL diagnostics
  • An 8-thread test covers deterministic lexing with ANTLR's shared static DFA

Release note

None

Check List (For Author)

  • Test

    • Regression test
    • Unit Test
    • Manual test
    • 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
    • fe-sql-parser: 88 passed, 0 failures
    • LeanTokenModeTest and EncryptSQLTest: 7 passed, 0 failures via run-fe-ut.sh; all 60 reactor modules built successfully
    • Parser, benchmark, and FE Checkstyle: 0 violations
    • Tracked SQL semantic differential and 18,440 full/lean cases passed
    • JMH lexer, parser, FE LogicalPlan, and full-mode control runs completed
  • 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
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?

### What problem does this PR solve?

Issue Number: None

Related PR: None

Problem Summary: Internal Nereids AST parsing allocates whitespace and ordinary line-comment tokens even though the parser does not consume them. Add an opt-in lean lexer mode for direct AST paths, preserve full-token public consumers, hints, source offsets, and diagnostics, and fall back to full tokens on syntax errors. JMH GC profiling shows 22.9%-49.2% lower tokenization allocation, 3.9%-12.0% lower parser allocation, and 2.3%-7.2% lower FE LogicalPlan allocation across representative SQL workloads.

### Release note

None

### Check List (For Author)

- Test: Unit Test and benchmark
    - Maven fe-sql-parser suite: 88 tests passed
    - LeanTokenModeTest and EncryptSQLTest: 7 tests passed
    - Tracked SQL corpus: 18,440 full/lean mode cases matched
    - JMH benchmark with GC profiler
- Behavior changed: No
- Does this need documentation: No
@morrySnow
morrySnow force-pushed the codex/antlr4-lean-token-mode branch from 1317d48 to 5ef2869 Compare September 2, 2026 08:06
@morrySnow morrySnow changed the title [improvement](parser) Skip hidden tokens in internal SQL parsing [improvement](fe) Skip hidden tokens in internal SQL parsing Sep 2, 2026
@morrySnow morrySnow changed the title [improvement](fe) Skip hidden tokens in internal SQL parsing [improvement](parser) Skip hidden tokens in internal SQL parsing Sep 2, 2026
@morrySnow

Copy link
Copy Markdown
Contributor Author

run buildall

@morrySnow
morrySnow marked this pull request as ready for review September 2, 2026 08:08
@hello-stephen

Copy link
Copy Markdown
Contributor

FE UT Coverage Report

Increment line coverage 92.86% (13/14) 🎉
Increment coverage report
Complete coverage report

@hello-stephen

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

------ Round 1 ----------------------------------
============================================
q1	17624	3087	3061	3061
q2	2174	257	227	227
q3	10176	868	504	504
q4	4671	252	205	205
q5	7671	566	383	383
q6	133	113	93	93
q7	518	491	388	388
q8	9245	851	890	851
q9	3429	2399	2340	2340
q10	6487	854	689	689
q11	393	194	182	182
q12	603	264	209	209
q13	18112	1515	1163	1163
q14	157	150	141	141
q15	q16	426	385	356	356
q17	1346	928	835	835
q18	3117	2226	2256	2226
q19	1263	933	798	798
q20	378	290	203	203
q21	5621	1687	1873	1687
q22	328	272	229	229
Total cold run time: 93872 ms
Total hot run time: 16770 ms

----- Round 2, with runtime_filter_mode=off -----
============================================
q1	3422	3370	3339	3339
q2	510	403	367	367
q3	2235	2231	2127	2127
q4	1179	1167	895	895
q5	2164	2081	2117	2081
q6	170	118	89	89
q7	1020	960	876	876
q8	1599	1425	1411	1411
q9	3101	3079	3124	3079
q10	1829	1784	1588	1588
q11	357	271	258	258
q12	455	425	350	350
q13	1481	1539	1145	1145
q14	167	181	163	163
q15	q16	393	396	359	359
q17	3618	3224	3165	3165
q18	4772	4388	4690	4388
q19	868	788	919	788
q20	1005	961	808	808
q21	3881	3123	3306	3123
q22	397	345	308	308
Total cold run time: 34623 ms
Total hot run time: 30707 ms

@hello-stephen

Copy link
Copy Markdown
Contributor
TPC-DS: Total hot run time: 81388 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 5ef28695247744992e354071e683656aea30a5fe, data reload: false

query5	4262	404	335	335
query6	394	132	121	121
query7	4963	429	235	235
query8	295	128	120	120
query9	8705	2889	2857	2857
query10	395	241	191	191
query11	5395	1049	916	916
query12	126	71	74	71
query13	1197	476	321	321
query14	6072	2184	2070	2070
query14_1	1961	1937	1936	1936
query15	171	120	119	119
query16	920	394	352	352
query17	823	454	379	379
query18	2331	338	260	260
query19	188	128	100	100
query20	67	65	66	65
query21	197	97	82	82
query22	5529	5372	5384	5372
query23	6717	6133	5896	5896
query23_1	5986	5862	5875	5862
query24	7286	1052	766	766
query24_1	761	765	773	765
query25	402	271	233	233
query26	1246	225	132	132
query27	2796	393	256	256
query28	4748	1505	1475	1475
query29	917	415	329	329
query30	249	153	126	126
query31	802	386	320	320
query32	148	74	69	69
query33	451	218	169	169
query34	1002	836	498	498
query35	397	386	341	341
query36	571	569	539	539
query37	128	75	67	67
query38	1049	831	788	788
query39	493	460	459	459
query39_1	454	469	482	469
query40	210	88	74	74
query41	54	56	53	53
query42	73	72	72	72
query43	235	242	215	215
query44	1028	537	541	537
query45	103	99	94	94
query46	809	826	525	525
query47	748	764	718	718
query48	325	310	227	227
query49	531	237	174	174
query50	770	269	196	196
query51	8188	8012	7978	7978
query52	75	69	63	63
query53	199	199	146	146
query54	228	187	182	182
query55	79	62	65	62
query56	214	184	189	184
query57	674	649	674	649
query58	227	173	172	172
query59	1226	1252	1098	1098
query60	273	187	172	172
query61	114	115	114	114
query62	365	211	187	187
query63	177	141	143	141
query64	2825	657	595	595
query65	1564	1614	1616	1614
query66	1921	250	200	200
query67	9721	9673	9822	9673
query68	2992	1266	686	686
query69	358	215	196	196
query70	664	608	618	608
query71	254	172	168	168
query72	2379	1765	1547	1547
query73	693	575	366	366
query74	2000	1196	1120	1120
query75	1162	1089	964	964
query76	2378	735	534	534
query77	267	244	216	216
query78	4158	3631	3159	3159
query79	2781	810	582	582
query80	1585	328	289	289
query81	523	151	129	129
query82	743	130	94	94
query83	279	210	185	185
query84	304	111	87	87
query85	826	397	300	300
query86	477	174	163	163
query87	1005	984	891	891
query88	2963	2118	2111	2111
query89	297	200	182	182
query90	2111	119	130	119
query91	136	124	100	100
query92	100	70	71	70
query93	1941	1090	680	680
query94	726	275	214	214
query95	535	248	234	234
query96	796	565	282	282
query97	1024	1027	968	968
query98	186	139	131	131
query99	424	355	309	309
Total cold run time: 180099 ms
Total hot run time: 81388 ms

@hello-stephen

Copy link
Copy Markdown
Contributor
ClickBench: Total hot run time: 14.62 s
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/clickbench-tools
ClickBench test result on commit 5ef28695247744992e354071e683656aea30a5fe, data reload: false

query1	0.00	0.00	0.00
query2	0.08	0.04	0.04
query3	0.26	0.10	0.08
query4	1.60	0.10	0.10
query5	0.19	0.16	0.16
query6	1.22	0.68	0.68
query7	0.04	0.01	0.00
query8	0.06	0.03	0.03
query9	0.28	0.21	0.21
query10	0.37	0.34	0.34
query11	0.18	0.12	0.12
query12	0.14	0.12	0.12
query13	0.30	0.30	0.29
query14	0.45	0.44	0.44
query15	0.36	0.35	0.34
query16	0.24	0.22	0.22
query17	0.66	0.70	0.68
query18	0.19	0.17	0.17
query19	1.15	1.16	1.17
query20	0.01	0.02	0.01
query21	15.44	0.16	0.11
query22	5.01	0.04	0.04
query23	16.20	0.25	0.10
query24	3.12	0.32	0.25
query25	0.10	0.04	0.04
query26	0.74	0.16	0.13
query27	0.04	0.03	0.02
query28	3.50	0.59	0.28
query29	12.41	3.14	2.56
query30	0.25	0.11	0.12
query31	2.76	0.36	0.16
query32	3.51	0.32	0.24
query33	1.44	1.38	1.47
query34	15.36	2.22	1.79
query35	1.70	1.73	1.72
query36	0.46	0.30	0.28
query37	0.06	0.04	0.03
query38	0.05	0.03	0.03
query39	0.04	0.02	0.02
query40	0.12	0.08	0.08
query41	0.08	0.03	0.02
query42	0.03	0.02	0.02
query43	0.03	0.02	0.02
Total cold run time: 90.23 s
Total hot run time: 14.62 s

@hello-stephen

Copy link
Copy Markdown
Contributor

FE Regression Coverage Report

Increment line coverage 50.00% (13/26) 🎉
Increment coverage report
Complete coverage report

@hello-stephen

Copy link
Copy Markdown
Contributor

FE Regression Coverage Report

Increment line coverage 17.33% (13/75) 🎉
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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants