Skip to content

[fix](fe) Reject sync MV creation when MV qualifier differs from base table qualifier#63352

Open
morrySnow wants to merge 3 commits into
apache:masterfrom
morrySnow:fix-19133
Open

[fix](fe) Reject sync MV creation when MV qualifier differs from base table qualifier#63352
morrySnow wants to merge 3 commits into
apache:masterfrom
morrySnow:fix-19133

Conversation

@morrySnow
Copy link
Copy Markdown
Contributor

@morrySnow morrySnow commented May 18, 2026

What problem does this PR solve?

Problem Summary: When creating a sync materialized view, if the MV name specified a different database than the base table (e.g. CREATE MATERIALIZED VIEW db1.mv_t AS SELECT d FROM db.t), Doris silently succeeded instead of raising an error. A sync materialized view is stored as an index on its base table and must therefore reside in the same database. The same error now also fires when the current session database differs from the base table's database.

Root cause

In CreateMaterializedViewCommand.validate(), the dbName field is extracted from the base table (via the plan validator), but the MV name's database (name.getDb()) was never compared against it. As a result, specifying a different database in the MV name was silently ignored and the MV was always created under the base table's database without any error.

Fix

After extracting dbName from the validator, determine the MV's effective database:

  • Use name.getDb() if explicitly specified.
  • Fall back to the session's current database otherwise.

If the effective MV database differs from the base table's database, throw an AnalysisException.

Release note

Sync materialized view creation now fails with an explicit error when the materialized view's effective database (explicitly specified or current session database) does not match the base table's database.

Check List (For Author)

  • Test: Regression test added under regression-test/suites/mv_p0/test_cross_db_mv_error/
  • Behavior changed: Yes — previously the statement succeeded silently; now it raises an AnalysisException
  • Does this need documentation: No

…table database

### What problem does this PR solve?

Issue Number: close #DORIS-19133

Problem Summary: When creating a sync materialized view, if the MV name
specified a different database than the base table (e.g.
`CREATE MATERIALIZED VIEW db1.mv_t AS SELECT d FROM db.t`), Doris
silently succeeded instead of raising an error. A sync materialized view
is stored as an index on its base table and must therefore reside in the
same database. The same error now also fires when the current session
database differs from the base table's database.

### Release note

Sync materialized view creation now fails with an explicit error when
the materialized view's effective database (explicitly specified or
current session database) does not match the base table's database.

### Check List (For Author)

- Test: Regression test added under regression-test/suites/mv_p0/test_cross_db_mv_error/
- Behavior changed: Yes — previously the statement succeeded silently; now it raises an AnalysisException
- Does this need documentation: No

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@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?

@morrySnow
Copy link
Copy Markdown
Contributor Author

run buildall

@morrySnow
Copy link
Copy Markdown
Contributor Author

/review

Refactor the database-consistency check into a package-private
helper method checkDatabaseConsistency() so it can be tested
directly without running the full Nereids planner.

Add CreateMaterializedViewCommandTest with four unit tests covering:
- same database: no exception
- different database: AnalysisException with descriptive message
- null mvDb: check skipped (no exception)
- empty mvDb: check skipped (no exception)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@morrySnow
Copy link
Copy Markdown
Contributor Author

run buildall

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.

I found one blocking correctness gap in the new sync MV database validation. The PR fixes db-name mismatches but still ignores the MV catalog qualifier/current catalog, so catalog-mismatched MV names can be silently accepted and routed to the internal base table.

Critical checkpoint conclusions:

  • Goal/test: The PR targets rejecting sync MV creation when the MV effective database differs from the base table database, and adds regression coverage for explicit/implicit database mismatches. Catalog mismatch coverage is missing.
  • Scope: The implementation is small and focused, but incomplete for fully-qualified MV names because TableNameInfo carries catalog as well as database.
  • Concurrency/lifecycle: No new concurrency or lifecycle-sensitive state is introduced.
  • Configuration/compatibility: No new config, protocol, storage, or persistence compatibility changes.
  • Parallel paths: Sync MV creation routes through this command and downstream Alter handling; downstream still uses the base table db and only the MV table name, so the catalog/database validation must be complete before that point.
  • Tests: Regression tests cover db mismatch and success in the same db, but not catalog-qualified MV names or current-catalog mismatch.
  • Observability/performance: No additional observability needed; no performance concern in this validation path.

User focus: No additional user-provided review focus was present.

@hello-stephen
Copy link
Copy Markdown
Contributor

FE UT Coverage Report

Increment line coverage 85.71% (6/7) 🎉
Increment coverage report
Complete coverage report

Extend the database/catalog validation to also reject sync MV creation
when the MV's effective catalog is not the internal catalog.

A sync materialized view is stored as an index on an OlapTable which
always lives in the internal catalog. If the MV name carries an
external catalog qualifier (e.g. hive_catalog.db.mv_t), or the
current session catalog is not the internal catalog, creation must
fail with an explicit AnalysisException.

Changes:
- Add checkCatalogConsistency() alongside checkDatabaseConsistency()
  and call it from validate()
- Extend CreateMaterializedViewCommandTest with catalog check tests
- Extend regression test with catalog mismatch case

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@morrySnow
Copy link
Copy Markdown
Contributor Author

Fixed in the latest commit. Added checkCatalogConsistency() that rejects sync MV creation when the MV's effective catalog (explicit or session) is not the internal catalog. Also extended the unit test and regression test to cover catalog mismatch cases.

@morrySnow morrySnow changed the title [fix](fe) Reject sync MV creation when MV database differs from base table database [fix](fe) Reject sync MV creation when MV qualifier differs from base table qualifier May 18, 2026
@morrySnow
Copy link
Copy Markdown
Contributor Author

/review

@morrySnow
Copy link
Copy Markdown
Contributor Author

run buildall

@hello-stephen
Copy link
Copy Markdown
Contributor

TPC-H: Total hot run time: 31213 ms
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/tpch-tools
Tpch sf100 test result on commit a24cf0ee849ddaafa0d3c47d58eef4f193856d71, 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	17639	3893	3887	3887
q2	q3	10768	1477	793	793
q4	4687	478	343	343
q5	7554	2254	2115	2115
q6	237	175	149	149
q7	955	765	665	665
q8	9386	1769	1575	1575
q9	5129	4908	4891	4891
q10	6374	2058	1779	1779
q11	431	266	251	251
q12	631	422	297	297
q13	18111	3366	2827	2827
q14	265	260	233	233
q15	q16	814	765	707	707
q17	955	818	894	818
q18	6912	5757	5542	5542
q19	1321	1238	1135	1135
q20	508	391	256	256
q21	6192	2716	2609	2609
q22	468	382	341	341
Total cold run time: 99337 ms
Total hot run time: 31213 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	4828	4613	4530	4530
q2	q3	4861	5325	4609	4609
q4	2165	2245	1398	1398
q5	4740	4862	4633	4633
q6	228	173	129	129
q7	1812	1777	1570	1570
q8	2428	2070	2010	2010
q9	7825	7417	7237	7237
q10	4467	4420	4037	4037
q11	528	383	346	346
q12	710	725	504	504
q13	3068	3392	2828	2828
q14	292	280	250	250
q15	q16	684	694	602	602
q17	1254	1235	1237	1235
q18	7101	6785	6621	6621
q19	1122	1053	1060	1053
q20	2226	2206	1918	1918
q21	5309	4649	4485	4485
q22	528	475	421	421
Total cold run time: 56176 ms
Total hot run time: 50416 ms

@hello-stephen
Copy link
Copy Markdown
Contributor

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

query5	4345	651	512	512
query6	330	239	201	201
query7	4290	536	314	314
query8	329	235	224	224
query9	8907	4075	4050	4050
query10	478	363	294	294
query11	5818	2352	2184	2184
query12	193	130	153	130
query13	1276	611	428	428
query14	5933	5386	5100	5100
query14_1	4368	4381	4324	4324
query15	204	203	186	186
query16	1025	436	427	427
query17	985	724	578	578
query18	2520	476	353	353
query19	221	198	170	170
query20	134	133	127	127
query21	240	142	117	117
query22	13665	13631	13407	13407
query23	17286	16452	16032	16032
query23_1	16256	16308	16251	16251
query24	7478	1763	1302	1302
query24_1	1344	1304	1303	1303
query25	572	508	455	455
query26	1308	327	177	177
query27	2686	564	326	326
query28	4498	1953	1891	1891
query29	1025	638	529	529
query30	321	239	203	203
query31	1114	1086	965	965
query32	96	82	73	73
query33	552	367	308	308
query34	1216	1126	653	653
query35	766	794	690	690
query36	1321	1326	1169	1169
query37	160	108	93	93
query38	3217	3166	3089	3089
query39	940	924	907	907
query39_1	886	884	884	884
query40	242	152	131	131
query41	71	69	69	69
query42	114	111	111	111
query43	327	323	289	289
query44	
query45	221	208	207	207
query46	1078	1164	739	739
query47	2342	2380	2244	2244
query48	404	445	309	309
query49	653	514	419	419
query50	1010	366	257	257
query51	4279	4237	4244	4237
query52	110	108	95	95
query53	255	284	215	215
query54	331	286	268	268
query55	102	96	91	91
query56	319	327	325	325
query57	1416	1410	1335	1335
query58	308	277	283	277
query59	1571	1606	1427	1427
query60	324	324	310	310
query61	157	157	147	147
query62	664	626	590	590
query63	237	201	204	201
query64	2404	816	623	623
query65	
query66	1718	488	360	360
query67	30178	30186	29972	29972
query68	
query69	455	351	300	300
query70	1008	1002	897	897
query71	294	269	262	262
query72	2981	2719	2370	2370
query73	845	797	401	401
query74	5104	4983	4778	4778
query75	2671	2611	2257	2257
query76	2286	1154	778	778
query77	395	409	331	331
query78	12137	12388	11540	11540
query79	1440	975	753	753
query80	662	548	439	439
query81	451	283	240	240
query82	1383	162	123	123
query83	358	321	243	243
query84	301	138	109	109
query85	875	523	450	450
query86	385	320	325	320
query87	3403	3382	3252	3252
query88	3534	2667	2642	2642
query89	436	393	342	342
query90	1964	183	176	176
query91	175	163	140	140
query92	78	75	75	75
query93	1536	1431	849	849
query94	556	330	313	313
query95	663	375	450	375
query96	1024	804	328	328
query97	2698	2704	2552	2552
query98	237	234	241	234
query99	1119	1106	987	987
Total cold run time: 253767 ms
Total hot run time: 170793 ms

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.

No new blocking issues found in this pass.

Critical checkpoint conclusions:

  • Goal and coverage: the PR rejects sync MV creation when the effective MV catalog/database does not match the internal base-table namespace; the regression test covers explicit cross-db, implicit session-db, explicit external-catalog rejection, and a same-db success case, with focused unit coverage for the helper checks.
  • Scope: the implementation is small and localized to CreateMaterializedViewCommand plus targeted tests.
  • Concurrency/lifecycle: no new concurrency, locking, persistence, or lifecycle-sensitive state is introduced.
  • Compatibility: this is an intentional validation behavior change for invalid sync MV statements; no storage or wire-format compatibility concerns found.
  • Parallel paths: sync MV creation flows through this command and downstream Alter.processCreateMaterializedView() uses the base table database, so validating before creation is the right path.
  • Error handling: uses Nereids AnalysisException, consistent with surrounding validation failures in this class.
  • Test results: I reviewed the added test logic but did not run the regression or FE unit tests in this runner.
  • Observability/performance: no new hot path or observability requirement identified.

Existing review context: I did not repeat the already-known catalog-validation thread; the current head includes catalog consistency validation and an explicit catalog mismatch regression case.

User focus: no additional user-provided review focus was specified.

@hello-stephen
Copy link
Copy Markdown
Contributor

TPC-H: Total hot run time: 31491 ms
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/tpch-tools
Tpch sf100 test result on commit f48f85978509921fdfc68a46aa859320f63a07be, 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	17656	3974	3877	3877
q2	q3	10770	1361	813	813
q4	4676	472	343	343
q5	7613	2288	2131	2131
q6	232	176	136	136
q7	915	777	645	645
q8	9347	1741	1577	1577
q9	5279	4936	4912	4912
q10	6378	2112	1804	1804
q11	441	286	262	262
q12	636	436	294	294
q13	18079	3539	2791	2791
q14	257	255	235	235
q15	q16	824	757	708	708
q17	1007	1017	980	980
q18	6954	5668	5484	5484
q19	1321	1264	1087	1087
q20	521	564	299	299
q21	6442	2872	2745	2745
q22	464	434	368	368
Total cold run time: 99812 ms
Total hot run time: 31491 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	4614	4581	4464	4464
q2	q3	4944	5244	4606	4606
q4	2117	2218	1446	1446
q5	4948	4607	4548	4548
q6	231	178	141	141
q7	1911	1691	1548	1548
q8	2401	2091	2056	2056
q9	7583	7120	7155	7120
q10	4470	4394	3987	3987
q11	532	379	349	349
q12	709	722	509	509
q13	3046	3415	2821	2821
q14	273	274	260	260
q15	q16	683	712	606	606
q17	1273	1237	1229	1229
q18	7198	6774	6664	6664
q19	1121	1090	1104	1090
q20	2201	2192	1931	1931
q21	5331	4628	4486	4486
q22	506	459	410	410
Total cold run time: 56092 ms
Total hot run time: 50271 ms

@hello-stephen
Copy link
Copy Markdown
Contributor

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

query5	4316	663	549	549
query6	341	217	200	200
query7	4230	556	322	322
query8	344	235	235	235
query9	8827	4005	3958	3958
query10	452	361	302	302
query11	5780	2370	2230	2230
query12	182	130	125	125
query13	1276	613	433	433
query14	6005	5357	5076	5076
query14_1	4387	4376	4379	4376
query15	218	205	182	182
query16	1012	463	449	449
query17	1159	749	616	616
query18	2515	504	373	373
query19	229	211	166	166
query20	140	134	130	130
query21	239	141	138	138
query22	13694	13509	13379	13379
query23	17314	16342	15989	15989
query23_1	16122	16096	16161	16096
query24	7440	1758	1284	1284
query24_1	1282	1313	1327	1313
query25	610	512	461	461
query26	1316	334	177	177
query27	2738	575	353	353
query28	4413	1964	1955	1955
query29	1033	640	526	526
query30	297	243	207	207
query31	1126	1082	929	929
query32	99	76	76	76
query33	541	356	310	310
query34	1218	1101	629	629
query35	770	791	674	674
query36	1311	1312	1212	1212
query37	153	111	93	93
query38	3210	3149	3050	3050
query39	946	935	918	918
query39_1	877	879	865	865
query40	226	141	124	124
query41	63	62	62	62
query42	114	112	114	112
query43	327	325	279	279
query44	
query45	209	205	200	200
query46	1081	1183	720	720
query47	2264	2341	2179	2179
query48	397	444	296	296
query49	632	488	391	391
query50	1021	345	239	239
query51	4339	4340	4233	4233
query52	105	106	93	93
query53	260	278	200	200
query54	322	286	260	260
query55	99	91	87	87
query56	293	303	333	303
query57	1404	1405	1300	1300
query58	301	271	274	271
query59	1562	1593	1395	1395
query60	337	326	315	315
query61	161	157	154	154
query62	673	622	580	580
query63	249	200	209	200
query64	2405	823	610	610
query65	
query66	1718	470	395	395
query67	29956	29864	29195	29195
query68	
query69	471	349	303	303
query70	1066	942	968	942
query71	305	273	265	265
query72	3082	2748	2402	2402
query73	853	758	392	392
query74	5098	4888	4718	4718
query75	2655	2612	2245	2245
query76	2282	1155	787	787
query77	396	413	325	325
query78	11949	12036	11606	11606
query79	1428	1018	752	752
query80	626	537	454	454
query81	459	282	247	247
query82	1364	157	120	120
query83	363	298	247	247
query84	258	140	107	107
query85	891	547	456	456
query86	411	334	347	334
query87	3397	3410	3181	3181
query88	3527	2628	2637	2628
query89	439	383	330	330
query90	1997	183	169	169
query91	178	170	137	137
query92	75	78	76	76
query93	1548	1392	863	863
query94	542	340	300	300
query95	706	403	357	357
query96	1094	777	360	360
query97	2692	2726	2569	2569
query98	243	230	246	230
query99	1087	1090	985	985
Total cold run time: 253111 ms
Total hot run time: 168802 ms

@hello-stephen
Copy link
Copy Markdown
Contributor

FE Regression Coverage Report

Increment line coverage 44.00% (11/25) 🎉
Increment coverage report
Complete coverage report

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants