@@ -36,7 +36,6 @@ use common_query::Output;
3636use common_recordbatch:: RecordBatches ;
3737use common_telemetry:: logging:: { debug, info} ;
3838use datafusion:: sql:: sqlparser:: ast:: ObjectName ;
39- use datafusion_common:: TableReference ;
4039use datanode:: instance:: sql:: table_idents_to_full_name;
4140use datanode:: instance:: InstanceRef as DnInstanceRef ;
4241use datatypes:: schema:: Schema ;
@@ -45,7 +44,7 @@ use meta_client::client::{MetaClient, MetaClientBuilder};
4544use meta_client:: MetaClientOpts ;
4645use partition:: manager:: PartitionRuleManager ;
4746use partition:: route:: TableRoutes ;
48- use query:: query_engine:: options:: QueryOptions ;
47+ use query:: query_engine:: options:: { validate_catalog_and_schema , QueryOptions } ;
4948use servers:: error as server_error;
5049use servers:: interceptor:: { SqlQueryInterceptor , SqlQueryInterceptorRef } ;
5150use servers:: promql:: { PromqlHandler , PromqlHandlerRef } ;
@@ -558,64 +557,34 @@ pub fn check_permission(
558557 Statement :: ShowCreateTable ( _) | Statement :: Alter ( _) => { }
559558
560559 Statement :: Insert ( insert) => {
561- let ( catalog, schema, _) =
562- table_idents_to_full_name ( insert. table_name ( ) , query_ctx. clone ( ) )
563- . map_err ( BoxedError :: new)
564- . context ( ExternalSnafu ) ?;
565-
566- validate_param ( & catalog, & schema, query_ctx) ?;
560+ validate_param ( insert. table_name ( ) , query_ctx) ?;
567561 }
568562 Statement :: CreateTable ( stmt) => {
569- let tab_ref = obj_name_to_tab_ref ( & stmt. name ) ?;
570- validate_tab_ref ( tab_ref, query_ctx) ?;
563+ validate_param ( & stmt. name , query_ctx) ?;
571564 }
572565 Statement :: DropTable ( drop_stmt) => {
573- let tab_ref = obj_name_to_tab_ref ( drop_stmt. table_name ( ) ) ?;
574- validate_tab_ref ( tab_ref, query_ctx) ?;
566+ validate_param ( drop_stmt. table_name ( ) , query_ctx) ?;
575567 }
576568 Statement :: ShowTables ( stmt) => {
577569 if let Some ( database) = & stmt. database {
578- validate_param ( & query_ctx. current_catalog ( ) , database, query_ctx) ?;
570+ validate_catalog_and_schema ( & query_ctx. current_catalog ( ) , database, query_ctx)
571+ . map_err ( BoxedError :: new)
572+ . context ( SqlExecInterceptedSnafu ) ?;
579573 }
580574 }
581575 Statement :: DescribeTable ( stmt) => {
582- let tab_ref = obj_name_to_tab_ref ( stmt. name ( ) ) ?;
583- validate_tab_ref ( tab_ref, query_ctx) ?;
576+ validate_param ( stmt. name ( ) , query_ctx) ?;
584577 }
585578 }
586579 Ok ( ( ) )
587580}
588581
589- fn obj_name_to_tab_ref ( obj : & ObjectName ) -> Result < TableReference > {
590- match & obj. 0 [ ..] {
591- [ table] => Ok ( TableReference :: Bare {
592- table : & table. value ,
593- } ) ,
594- [ schema, table] => Ok ( TableReference :: Partial {
595- schema : & schema. value ,
596- table : & table. value ,
597- } ) ,
598- [ catalog, schema, table] => Ok ( TableReference :: Full {
599- catalog : & catalog. value ,
600- schema : & schema. value ,
601- table : & table. value ,
602- } ) ,
603- _ => error:: InvalidSqlSnafu {
604- err_msg : format ! (
605- "expect table name to be <catalog>.<schema>.<table>, <schema>.<table> or <table>, actual: {obj}" ,
606- ) ,
607- } . fail ( ) ,
608- }
609- }
610-
611- fn validate_tab_ref ( tab_ref : TableReference , query_ctx : & QueryContextRef ) -> Result < ( ) > {
612- query:: query_engine:: options:: validate_table_references ( tab_ref, query_ctx)
582+ fn validate_param ( name : & ObjectName , query_ctx : & QueryContextRef ) -> Result < ( ) > {
583+ let ( catalog, schema, _) = table_idents_to_full_name ( name, query_ctx. clone ( ) )
613584 . map_err ( BoxedError :: new)
614- . context ( SqlExecInterceptedSnafu )
615- }
585+ . context ( ExternalSnafu ) ?;
616586
617- fn validate_param ( catalog : & str , schema : & str , query_ctx : & QueryContextRef ) -> Result < ( ) > {
618- query:: query_engine:: options:: validate_catalog_and_schema ( catalog, schema, query_ctx)
587+ validate_catalog_and_schema ( & catalog, & schema, query_ctx)
619588 . map_err ( BoxedError :: new)
620589 . context ( SqlExecInterceptedSnafu )
621590}
0 commit comments