Skip to content

[fix](fe) Fix SHOW CATALOGS WHERE filtering - #68299

Open
Gabriel39 wants to merge 2 commits into
apache:masterfrom
Gabriel39:fix/show-catalogs-where-filter
Open

Gabriel39 wants to merge 2 commits into
apache:masterfrom
Gabriel39:fix/show-catalogs-where-filter

Conversation

@Gabriel39

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: None

Related PR: None

Problem Summary: SHOW CATALOGS passed WHERE expressions to the LIKE pattern matcher, so valid predicates failed as invalid patterns. Keep LIKE patterns and WHERE expressions separate, then evaluate WHERE against the privilege-filtered SHOW rows.

Release note

Fix SHOW CATALOGS WHERE filtering.

Check List (For Author)

  • Test: Unit Test; regression test added
    • Added an FE unit test covering parsing, matching, and non-matching WHERE predicates.
    • Added regression coverage to the existing SHOW CATALOG suite.
    • Ran the targeted FE unit test and the full FE build with Checkstyle.
  • Behavior changed: Yes. SHOW CATALOGS WHERE now filters the displayed rows.
  • Does this need documentation: No

### What problem does this PR solve?

Issue Number: None

Related PR: None

Problem Summary: SHOW CATALOGS passed WHERE expressions to the LIKE pattern matcher, so valid predicates failed as invalid patterns. Keep LIKE patterns and WHERE expressions separate, then evaluate WHERE against the privilege-filtered SHOW rows.

### Release note

Fix SHOW CATALOGS WHERE filtering.

### Check List (For Author)

- Test: Unit Test; regression test added
- Behavior changed: Yes. SHOW CATALOGS WHERE now filters the displayed rows.
- 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?

@Gabriel39

Copy link
Copy Markdown
Contributor Author

run buildall

@Gabriel39

Copy link
Copy Markdown
Contributor Author

/review

@hello-stephen

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

------ Round 1 ----------------------------------
============================================
q1	17702	3846	3756	3756
q2	2189	357	298	298
q3	10075	1410	801	801
q4	4681	490	345	345
q5	7506	807	541	541
q6	179	172	137	137
q7	734	789	591	591
q8	9303	1384	1469	1384
q9	5412	4171	4175	4171
q10	6840	1304	1028	1028
q11	431	267	240	240
q12	629	422	288	288
q13	18039	2622	1990	1990
q14	263	260	234	234
q15	q16	730	712	659	659
q17	2057	1115	1071	1071
q18	6506	5599	5517	5517
q19	1169	1181	1046	1046
q20	452	381	273	273
q21	5403	3007	2731	2731
q22	411	350	292	292
Total cold run time: 100711 ms
Total hot run time: 27393 ms

----- Round 2, with runtime_filter_mode=off -----
============================================
q1	4142	4047	4052	4047
q2	719	569	521	521
q3	4433	4853	4268	4268
q4	2202	2295	1436	1436
q5	4213	4117	4068	4068
q6	224	169	127	127
q7	1694	1607	1443	1443
q8	2174	2162	2083	2083
q9	7400	7239	7364	7239
q10	3658	3618	3174	3174
q11	539	381	352	352
q12	749	732	508	508
q13	2508	2783	2120	2120
q14	304	284	281	281
q15	q16	684	739	628	628
q17	7941	7200	7072	7072
q18	11938	11120	11838	11120
q19	1179	1078	1090	1078
q20	2212	2206	1931	1931
q21	5338	4599	4430	4430
q22	500	437	416	416
Total cold run time: 64751 ms
Total hot run time: 58342 ms

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Requesting changes for four correctness and lifecycle regressions in the new SHOW CATALOGS WHERE execution path.

Checkpoint conclusions:

  • Goal and scope: LIKE and WHERE parsing are now separated correctly, and the constructor/call-site scope is focused, but the new runtime reconstruction path is not behavior-preserving.
  • Lifecycle, observability, and policy: the nested query mutates the user's executor and shared query state, misclassifies audits/metrics, and leaves later statements exempt from SQL block/scan-limit checks.
  • Authorization and condition handling: privilege filtering still precedes WHERE, but an empty authorized result skips predicate analysis entirely.
  • Compatibility and result semantics: SHOW NULL sentinels are reconstructed as non-null strings; old-image null comments have the same mismatch. The new BE sort can also change the established order of legal Unicode catalog names.
  • Concurrency and snapshots: catalog rows are still collected under the existing catalog-manager read lock; no new lock-order or shared-mutation issue was found beyond the executor/context lifecycle issue above.
  • Configuration, persistence, protocol, transactions, and data writes: no new configuration, write, transaction, storage-format, or FE/BE field change is introduced. Old persisted metadata was checked because it feeds the new literal conversion.
  • Parallel paths and performance: singular SHOW and LIKE remain unchanged. The backend dependency and linear inline-plan growth match established SHOW-WHERE paths and were not raised separately.
  • Tests: the added tests cover the reported LIKE-versus-WHERE happy path but not connection-state leakage, zero-visible-catalog analysis, SQL NULL predicates, or Unicode ordering. Per the review contract, this was a static review only; I did not run builds or tests, so author/CI validation was not independently reproduced.
  • User focus: no additional review focus was supplied.

Review status: complete after three convergence rounds; all candidates were adjudicated and no unresolved point remains.

@hello-stephen

Copy link
Copy Markdown
Contributor
TPC-DS: Total hot run time: 152218 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 158bb81caddb7f0e335c09a298e1423b02508ae5, data reload: false

query5	4301	609	433	433
query6	425	203	187	187
query7	4909	547	283	283
query8	321	180	162	162
query9	8797	3961	4000	3961
query10	485	307	262	262
query11	5768	3542	3234	3234
query12	153	91	84	84
query13	1246	597	436	436
query14	6528	4515	4218	4218
query14_1	3975	3987	3964	3964
query15	201	192	179	179
query16	993	467	421	421
query17	938	683	535	535
query18	2459	461	368	368
query19	205	182	149	149
query20	84	80	81	80
query21	221	138	117	117
query22	13057	13003	12728	12728
query23	13937	12986	12463	12463
query23_1	12490	12478	12412	12412
query24	7284	1161	708	708
query24_1	697	679	711	679
query25	555	431	371	371
query26	1287	290	178	178
query27	2722	552	328	328
query28	4578	1944	1967	1944
query29	1639	729	535	535
query30	307	229	185	185
query31	905	775	636	636
query32	152	93	95	93
query33	532	311	252	252
query34	1222	1076	636	636
query35	749	748	659	659
query36	813	800	726	726
query37	149	109	96	96
query38	1827	1774	1676	1676
query39	701	692	657	657
query39_1	655	654	655	654
query40	222	134	105	105
query41	72	71	69	69
query42	103	95	93	93
query43	338	344	304	304
query44	1371	741	719	719
query45	187	179	168	168
query46	1114	1165	744	744
query47	1490	1490	1404	1404
query48	408	394	344	344
query49	588	394	300	300
query50	927	341	248	248
query51	10471	10742	10467	10467
query52	88	89	76	76
query53	233	244	183	183
query54	249	211	180	180
query55	77	74	69	69
query56	226	221	220	220
query57	1571	1475	1354	1354
query58	275	258	246	246
query59	2014	2169	1877	1877
query60	279	239	217	217
query61	142	144	146	144
query62	401	322	263	263
query63	214	179	171	171
query64	2803	988	780	780
query65	3445	3396	3432	3396
query66	1822	428	329	329
query67	19894	20200	19829	19829
query68	3136	1496	859	859
query69	403	289	257	257
query70	930	819	863	819
query71	351	237	223	223
query72	2607	2512	2200	2200
query73	787	769	403	403
query74	4646	4500	4318	4318
query75	2310	2282	1956	1956
query76	2311	1142	716	716
query77	374	395	293	293
query78	9061	9097	8404	8404
query79	1338	1205	760	760
query80	594	471	369	369
query81	539	322	285	285
query82	644	166	130	130
query83	302	228	201	201
query84	325	148	113	113
query85	839	476	380	380
query86	336	244	223	223
query87	2015	1971	1817	1817
query88	3614	2750	2702	2702
query89	368	287	250	250
query90	2007	180	177	177
query91	175	160	127	127
query92	103	89	89	89
query93	1515	1541	863	863
query94	533	344	298	298
query95	668	481	337	337
query96	1052	771	333	333
query97	2407	2431	2295	2295
query98	165	153	152	152
query99	718	748	623	623
Total cold run time: 236159 ms
Total hot run time: 152218 ms

@hello-stephen

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

query1	0.01	0.01	0.01
query2	0.10	0.05	0.05
query3	0.26	0.14	0.14
query4	1.62	0.14	0.14
query5	0.25	0.22	0.22
query6	1.16	0.92	0.94
query7	0.04	0.01	0.00
query8	0.06	0.04	0.04
query9	0.40	0.34	0.34
query10	0.57	0.58	0.56
query11	0.20	0.14	0.15
query12	0.18	0.15	0.14
query13	0.47	0.48	0.48
query14	0.95	0.95	0.96
query15	0.63	0.58	0.61
query16	0.32	0.31	0.31
query17	1.12	1.10	1.06
query18	0.22	0.21	0.22
query19	2.02	1.96	1.94
query20	0.02	0.02	0.01
query21	15.43	0.22	0.14
query22	4.85	0.06	0.05
query23	16.12	0.31	0.12
query24	2.97	0.43	0.33
query25	0.13	0.05	0.05
query26	0.74	0.20	0.15
query27	0.04	0.03	0.03
query28	3.52	0.76	0.35
query29	12.47	4.14	3.26
query30	0.28	0.16	0.16
query31	2.77	0.60	0.30
query32	3.22	0.61	0.49
query33	3.12	3.25	3.29
query34	15.65	3.93	3.31
query35	3.25	3.20	3.20
query36	0.57	0.42	0.43
query37	0.09	0.07	0.07
query38	0.05	0.04	0.03
query39	0.04	0.02	0.02
query40	0.18	0.15	0.14
query41	0.08	0.03	0.03
query42	0.03	0.03	0.03
query43	0.04	0.03	0.04
Total cold run time: 96.24 s
Total hot run time: 24.2 s

@Gabriel39

Copy link
Copy Markdown
Contributor Author

run buildall

@Gabriel39

Copy link
Copy Markdown
Contributor Author

/review

@hello-stephen

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

------ Round 1 ----------------------------------
============================================
q1	17778	3920	3825	3825
q2	2191	368	332	332
q3	10047	1372	814	814
q4	4683	482	350	350
q5	7469	859	554	554
q6	188	177	143	143
q7	740	815	618	618
q8	9305	1555	1491	1491
q9	5402	4189	4134	4134
q10	6840	1347	1019	1019
q11	433	277	247	247
q12	640	417	300	300
q13	18060	2625	2046	2046
q14	273	260	239	239
q15	q16	728	714	663	663
q17	1830	1158	1100	1100
q18	6466	5578	5526	5526
q19	1165	1248	1044	1044
q20	464	399	264	264
q21	5455	3283	3119	3119
q22	457	380	320	320
Total cold run time: 100614 ms
Total hot run time: 28148 ms

----- Round 2, with runtime_filter_mode=off -----
============================================
q1	4677	4753	4456	4456
q2	766	582	542	542
q3	4759	5219	4568	4568
q4	2230	2334	1445	1445
q5	4482	4412	4673	4412
q6	227	179	134	134
q7	1784	1701	1496	1496
q8	2465	2144	2043	2043
q9	7455	7274	7180	7180
q10	3701	3593	3180	3180
q11	547	381	352	352
q12	702	700	505	505
q13	2318	2595	1977	1977
q14	279	275	251	251
q15	q16	677	686	590	590
q17	7363	6710	6679	6679
q18	11850	11031	11848	11031
q19	1114	1009	985	985
q20	2204	2194	1922	1922
q21	5136	4223	4418	4223
q22	535	464	411	411
Total cold run time: 65271 ms
Total hot run time: 58382 ms

@hello-stephen

Copy link
Copy Markdown
Contributor
TPC-DS: Total hot run time: 153478 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 1af2a0af77882f29e1e8bab9e02f9dc9f6e80f7b, data reload: false

query5	4322	620	485	485
query6	448	222	202	202
query7	5034	520	298	298
query8	331	183	169	169
query9	8804	4027	4002	4002
query10	456	318	274	274
query11	5904	3545	3219	3219
query12	149	92	86	86
query13	1259	614	423	423
query14	6544	4620	4341	4341
query14_1	4086	4051	4037	4037
query15	204	198	180	180
query16	1002	461	433	433
query17	931	678	543	543
query18	2423	453	337	337
query19	206	182	142	142
query20	83	81	86	81
query21	223	141	121	121
query22	13067	13006	12978	12978
query23	14111	12974	12528	12528
query23_1	12731	12697	12695	12695
query24	7652	1125	667	667
query24_1	706	710	679	679
query25	558	441	378	378
query26	1260	317	161	161
query27	2695	532	348	348
query28	4569	1969	1972	1969
query29	1612	713	521	521
query30	290	220	185	185
query31	886	752	644	644
query32	146	93	94	93
query33	499	313	243	243
query34	1236	1092	640	640
query35	729	749	642	642
query36	788	801	703	703
query37	143	106	97	97
query38	1832	1769	1713	1713
query39	694	680	669	669
query39_1	659	667	656	656
query40	223	118	101	101
query41	66	68	65	65
query42	94	93	89	89
query43	361	357	323	323
query44	1344	717	718	717
query45	184	173	168	168
query46	1092	1196	733	733
query47	1498	1503	1387	1387
query48	424	395	299	299
query49	580	408	287	287
query50	957	349	260	260
query51	10800	10664	10490	10490
query52	105	93	77	77
query53	240	259	179	179
query54	264	209	196	196
query55	81	75	69	69
query56	227	230	238	230
query57	1462	1423	1254	1254
query58	287	252	253	252
query59	2061	2080	1873	1873
query60	293	246	226	226
query61	148	144	154	144
query62	404	315	270	270
query63	220	178	177	177
query64	2787	997	824	824
query65	3469	3432	3457	3432
query66	1795	422	315	315
query67	19822	20292	19946	19946
query68	3378	1598	950	950
query69	407	302	271	271
query70	904	838	845	838
query71	294	234	212	212
query72	2715	2484	2286	2286
query73	835	734	436	436
query74	4687	4465	4306	4306
query75	2290	2287	1933	1933
query76	2336	1106	749	749
query77	371	412	309	309
query78	9111	8987	8378	8378
query79	1204	1135	735	735
query80	574	482	379	379
query81	532	321	279	279
query82	374	158	138	138
query83	310	229	188	188
query84	322	138	114	114
query85	888	502	374	374
query86	330	238	229	229
query87	1968	2013	1847	1847
query88	3636	2728	2719	2719
query89	371	289	247	247
query90	1853	190	183	183
query91	172	157	131	131
query92	108	95	93	93
query93	1489	1395	802	802
query94	529	349	292	292
query95	681	456	345	345
query96	1038	801	360	360
query97	2427	2470	2339	2339
query98	162	149	145	145
query99	723	720	599	599
Total cold run time: 237251 ms
Total hot run time: 153478 ms

@hello-stephen

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

query1	0.01	0.01	0.00
query2	0.09	0.05	0.05
query3	0.25	0.13	0.13
query4	1.61	0.14	0.14
query5	0.25	0.23	0.22
query6	1.16	0.94	0.96
query7	0.05	0.01	0.01
query8	0.05	0.04	0.04
query9	0.40	0.34	0.34
query10	0.54	0.52	0.55
query11	0.20	0.15	0.14
query12	0.19	0.14	0.14
query13	0.47	0.47	0.48
query14	0.96	0.96	0.93
query15	0.63	0.58	0.60
query16	0.30	0.31	0.32
query17	1.07	1.08	1.12
query18	0.23	0.20	0.21
query19	2.08	1.86	1.95
query20	0.02	0.01	0.01
query21	15.46	0.19	0.14
query22	5.07	0.05	0.05
query23	16.16	0.30	0.13
query24	2.95	0.41	0.33
query25	0.11	0.05	0.04
query26	0.74	0.22	0.15
query27	0.06	0.03	0.03
query28	3.61	0.75	0.37
query29	12.48	3.96	3.17
query30	0.27	0.16	0.15
query31	2.78	0.56	0.31
query32	3.24	0.59	0.48
query33	3.07	3.28	3.21
query34	15.47	3.93	3.26
query35	3.19	3.24	3.22
query36	0.56	0.43	0.44
query37	0.08	0.07	0.06
query38	0.04	0.04	0.03
query39	0.04	0.03	0.03
query40	0.18	0.15	0.14
query41	0.09	0.03	0.03
query42	0.04	0.03	0.03
query43	0.04	0.04	0.03
Total cold run time: 96.29 s
Total hot run time: 23.93 s

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Request changes: the new SHOW CATALOGS WHERE execution path has seven distinct issues (five P1 correctness failures and two P2 concurrency/performance defects).

Checkpoint conclusions:

  • Goal and proof: LIKE/WHERE parsing and the direct uncached equality cases are improved, but general WHERE semantics are not achieved: valid scalar/NULL predicates, session-dependent expressions, server-prepared parameters, repeated cached execution, and the supported dropped-current-catalog state fail or return wrong rows.
  • Scope and focus: the parser change is focused, but the command adds a bespoke cloned-session and per-row full-planner pipeline instead of reusing the query-backed strategy of parallel SHOW commands. That larger mechanism is the source of the findings.
  • Concurrency: CatalogMgr still snapshots privilege-filtered rows under its existing read lock, with no new lock-order problem. However, aliases are constructed before a per-row StatementContext exists, exposing the shared unsynchronized fallback ExprId generator and allowing silent wrong-field substitution under concurrent requests.
  • Lifecycle: AutoCloseConnectContext fixes the previously reported outer-state mutation and planner resources are released. The clone nevertheless omits SQL-observable session state, cannot reproduce a dropped selected catalog, loses prepared bindings, and lets synthetic results escape through the global SQL cache.
  • Configuration: no setting is added. Default enable_sql_cache=true directly triggers the partial-result replay; the default FE constant-fold path provides no fallback for supported scalars that survive folding.
  • Compatibility: singular SHOW, LIKE, and the no-WHERE fast path remain intact. No persisted metadata, storage format, thrift field, function symbol, or rolling FE/BE compatibility surface changes. Server-prepared SHOW is an existing supported protocol path and regresses here.
  • Parallel paths and stage placement: query-backed SHOW-WHERE implementations can execute the accepted scalar language; this one requires total FE folding and plans separately for every catalog row.
  • Conditions and errors: privilege-before-filter ordering, typed zero-row binding, SQL-NULL reconstruction, direct row order, and exception restoration are sound at this head. Surviving filters throw an internal invariant, stale namespaces NPE, and missing prepared bindings fail analysis.
  • Tests and claimed results: the unit tests cover parser separation, direct equality, empty-input name analysis, outer restoration, Unicode order, and NULL conversion. The regression is a one-shot smoke call without row assertions. Missing coverage includes non-FE scalars/UNKNOWN, user/session state, repeated COM_QUERY cache replay, prepared placeholders, dropped-current-catalog WHERE, concurrent ExprId uniqueness, and high catalog counts.
  • Observability: no new distributed lifecycle requires metrics, but existing planner/cache telemetry would not expose the silent wrong rows from cache replay or ExprId collision.
  • Transactions, persistence, and data writes: not applicable; these paths do not modify EditLog/replay, transactions, metadata, or user data.
  • FE/BE variables, protocol, and memory safety: no new transmitted variable or BE memory/nullability code exists. Temporary planner resources are released; the material resource issue is the uncapped repeated planning cost.
  • Performance: one SHOW performs a full Nereids analysis/optimization/translation cycle for every visible catalog, with no catalog-count bound.
  • Security and other correctness: authorization still precedes filtering, but the cached synthetic row records no catalog-list or SHOW-grant dependency, so metadata/RBAC changes can replay stale visibility. Restricted-user metadata visibility is within the repository threat model.
  • Existing review threads: the four earlier lifecycle, empty-input, Unicode-order, and NULL-sentinel findings are statically fixed at the reviewed head and are not duplicated below.
  • User focus: no additional user-provided focus was supplied.
  • Validation and status: this was static review only as required; I did not run builds or tests, so author/CI claims were not independently reproduced. The review is capped/incomplete after the third allowed round because that final round found the prepared-placeholder defect; all candidates found so far were independently adjudicated and the seven accepted issues are included inline.

NereidsPlanner planner = new NereidsPlanner(statementContext);
planner.plan(adapter, filterContext.getSessionVariable().toThrift());
Optional<ResultSet> resultSet = planner.handleQueryInFe(adapter);
if (!resultSet.isPresent()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[P1] Do not require every valid WHERE predicate to fold entirely in FE. For example, SHOW CATALOGS WHERE hex(CatalogName) = '696E7465726E616C' is accepted, but hex has no FE executable registration, so the reduced tree still contains PhysicalFilter. That node is not ComputeResultSet; handleQueryInFe returns empty and this path throws the internal invariant instead of evaluating the valid predicate. Please execute the full accepted expression language (or deliberately validate a narrower language with a user-facing analysis error) and cover a non-FE-foldable scalar.


private ConnectContext buildFilterContext(ConnectContext outerContext) {
ConnectContext filterContext = new ConnectContext();
filterContext.setSessionVariable(VariableMgr.cloneSessionVariable(outerContext.getSessionVariable()));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[P1] Preserve the caller's SQL-observable session state in the isolated evaluation context. This fresh context does not copy user variables, connection id, last query id, or the original statement clock: after SET @wanted='internal', SHOW CATALOGS WHERE CatalogName=@wanted fails as an unsupported variable, and connection_id()/last_query_id() fold against default clone values. This is separate from the fixed outer-state mutation—the wrapper restores correctly, but the predicate reads a different session. Please use a complete purpose-built snapshot (while isolating mutable execution state) and add session-dependent predicate coverage.

LogicalPlanAdapter adapter = new LogicalPlanAdapter(plan, statementContext);
NereidsPlanner planner = new NereidsPlanner(statementContext);
planner.plan(adapter, filterContext.getSessionVariable().toThrift());
Optional<ResultSet> resultSet = planner.handleQueryInFe(adapter);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[P1] Keep these synthetic per-row plans out of the global SQL cache. With default enable_sql_cache=true, every iteration uses this same full SHOW text, and handleQueryInFe caches the first one-row (or empty) result. For two matching catalogs, the first execution assembles both rows, but the next identical canonical command can be replaced before parsing by that first partial LogicalSqlCache; it also has no catalog-list or SHOW-grant dependency to invalidate after metadata/privilege changes. Please disable cache admission for this internal evaluation and add a connection-level test that executes the same two-match SHOW twice.

filterContext.setSessionVariable(VariableMgr.cloneSessionVariable(outerContext.getSessionVariable()));
filterContext.setEnv(Env.getCurrentEnv());
filterContext.changeDefaultCatalog(outerContext.getDefaultCatalog());
filterContext.setDatabase(outerContext.getDatabase());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[P1] Preserve the existing dropped-current-catalog behavior when building this context. query_p0/show/test_show_catalogs.groovy explicitly switches to a catalog, drops that current catalog, and requires plain SHOW CATALOGS to remain valid. In that state line 183 copies the stale name, then setDatabase unconditionally calls getCurrentCatalog().getDb(db), so adding any WHERE clause now NPEs before filtering. Please copy the nullable namespace without resolving it against a missing catalog and extend that regression with a WHERE form.

// Multi-row VALUES plans require BE execution. Filtering one row at a time keeps this path in FE
// and preserves the CatalogMgr order instead of applying a different SQL string collation.
List<List<String>> filteredRows = new ArrayList<>(rows.size());
for (List<String> row : rows) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[P2] Avoid running a complete Nereids planning/translation cycle once per visible catalog. Each iteration creates a fresh planner and calls plan, which reaches analysis, rewrites, memo optimization, post-processing, fragment splitting, and physical translation; CatalogMgr.showCatalogs places no bound on the number of external catalogs. This makes one metadata filter consume planner CPU and allocations proportional to catalog count. Please bind/plan once for the row set (carrying an ordinal if needed to retain manager order), or compile the predicate once for repeated row evaluation.

// and preserves the CatalogMgr order instead of applying a different SQL string collation.
List<List<String>> filteredRows = new ArrayList<>(rows.size());
for (List<String> row : rows) {
filteredRows.addAll(executeFilterPlan(filterContext, filterPlan(toExpressions(row), false)));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[P2] Install the per-row StatementContext before constructing these aliases. At this call site Java evaluates toExpressions(row) first, while the installed fresh ConnectContext still has no statement context, so every Alias draws from StatementScopeIdGenerator's shared test-only fallback. That generator uses an unsynchronized nextId++; concurrent SHOWs can therefore give two columns in one row the same ExprId. Because SlotReference equality is ExprId-only and generateReplaceMap uses putIfAbsent, a predicate on Type can then be substituted with the earlier CatalogName literal and silently return the wrong row set. Please allocate these IDs from the installed per-row context and add concurrent uniqueness coverage.

}

private List<List<String>> executeFilterPlan(ConnectContext filterContext, LogicalPlan plan) throws Exception {
StatementContext statementContext = new StatementContext(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[P1] Preserve bound server-prepared parameters in this inner statement context. COM_STMT_EXECUTE has already placed the literal for SHOW CATALOGS WHERE CatalogName = ? in the caller's StatementContext, but this replacement context starts with an empty idToPlaceholderRealExpr. Since it is no longer in prepare stage, ExpressionAnalyzer.visitPlaceholder receives no real expression and the parameterized SHOW fails during analysis instead of filtering. Please carry the execution's placeholder bindings into this isolated context (or bind the retained predicate before replacing it) and add a server-prepared regression with a bound catalog name.

@hello-stephen

Copy link
Copy Markdown
Contributor

FE UT Coverage Report

Increment line coverage 84.85% (56/66) 🎉
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