@@ -38,8 +38,8 @@ use datatypes::schema::{RawSchema, Schema};
3838use meta_client:: client:: MetaClient ;
3939use meta_client:: rpc:: router:: DeleteRequest as MetaDeleteRequest ;
4040use meta_client:: rpc:: {
41- CreateRequest as MetaCreateRequest , Partition as MetaPartition , PutRequest , RouteResponse ,
42- TableName ,
41+ CompareAndPutRequest , CreateRequest as MetaCreateRequest , Partition as MetaPartition ,
42+ RouteResponse , TableName ,
4343} ;
4444use partition:: partition:: { PartitionBound , PartitionDef } ;
4545use query:: parser:: QueryStatement ;
@@ -60,8 +60,9 @@ use crate::datanode::DatanodeClients;
6060use crate :: error:: {
6161 self , AlterExprToRequestSnafu , CatalogEntrySerdeSnafu , CatalogNotFoundSnafu , CatalogSnafu ,
6262 ColumnDataTypeSnafu , DeserializePartitionSnafu , ParseSqlSnafu , PrimaryKeyNotFoundSnafu ,
63- RequestDatanodeSnafu , RequestMetaSnafu , Result , SchemaNotFoundSnafu , StartMetaClientSnafu ,
64- TableNotFoundSnafu , TableSnafu , ToTableInsertRequestSnafu ,
63+ RequestDatanodeSnafu , RequestMetaSnafu , Result , SchemaExistsSnafu , SchemaNotFoundSnafu ,
64+ StartMetaClientSnafu , TableAlreadyExistSnafu , TableNotFoundSnafu , TableSnafu ,
65+ ToTableInsertRequestSnafu ,
6566} ;
6667use crate :: expr_factory;
6768use crate :: instance:: parse_stmt;
@@ -105,6 +106,26 @@ impl DistInstance {
105106 & create_table. table_name ,
106107 ) ;
107108
109+ if self
110+ . catalog_manager
111+ . table (
112+ & table_name. catalog_name ,
113+ & table_name. schema_name ,
114+ & table_name. table_name ,
115+ )
116+ . context ( CatalogSnafu ) ?
117+ . is_some ( )
118+ {
119+ return if create_table. create_if_not_exists {
120+ Ok ( Output :: AffectedRows ( 0 ) )
121+ } else {
122+ TableAlreadyExistSnafu {
123+ table : table_name. to_string ( ) ,
124+ }
125+ . fail ( )
126+ } ;
127+ }
128+
108129 let mut table_info = create_table_info ( create_table) ?;
109130
110131 let response = self
@@ -157,7 +178,7 @@ impl DistInstance {
157178 . register_table( request)
158179 . await
159180 . context( CatalogSnafu ) ?,
160- error :: TableAlreadyExistSnafu {
181+ TableAlreadyExistSnafu {
161182 table: table_name. to_string( )
162183 }
163184 ) ;
@@ -266,6 +287,10 @@ impl DistInstance {
266287 let create_expr = & mut expr_factory:: create_to_expr ( & stmt, query_ctx) ?;
267288 Ok ( self . create_table ( create_expr, stmt. partitions ) . await ?)
268289 }
290+ Statement :: Alter ( alter_table) => {
291+ let expr = grpc:: to_alter_expr ( alter_table, query_ctx) ?;
292+ return self . handle_alter_table ( expr) . await ;
293+ }
269294 Statement :: DropTable ( stmt) => {
270295 let ( catalog, schema, table) =
271296 table_idents_to_full_name ( stmt. table_name ( ) , query_ctx)
@@ -358,10 +383,19 @@ impl DistInstance {
358383 . store_client ( )
359384 . context ( StartMetaClientSnafu ) ?;
360385
361- let request = PutRequest :: default ( )
386+ let request = CompareAndPutRequest :: new ( )
362387 . with_key ( key. to_string ( ) )
363388 . with_value ( value. as_bytes ( ) . context ( CatalogEntrySerdeSnafu ) ?) ;
364- client. put ( request. into ( ) ) . await . context ( RequestMetaSnafu ) ?;
389+ let response = client
390+ . compare_and_put ( request. into ( ) )
391+ . await
392+ . context ( RequestMetaSnafu ) ?;
393+ ensure ! (
394+ response. success,
395+ SchemaExistsSnafu {
396+ name: key. schema_name
397+ }
398+ ) ;
365399
366400 Ok ( Output :: AffectedRows ( 1 ) )
367401 }
0 commit comments