@@ -29,15 +29,106 @@ use iceberg::io::FileIO;
2929use iceberg:: spec:: { TableMetadata , TableMetadataBuilder } ;
3030use iceberg:: table:: Table ;
3131use iceberg:: {
32- Catalog , Error , ErrorKind , MetadataLocation , Namespace , NamespaceIdent , Result , TableCommit ,
33- TableCreation , TableIdent ,
32+ Catalog , CatalogBuilder , Error , ErrorKind , MetadataLocation , Namespace , NamespaceIdent , Result ,
33+ TableCommit , TableCreation , TableIdent ,
3434} ;
35- use typed_builder:: TypedBuilder ;
3635use volo_thrift:: MaybeException ;
3736
3837use super :: utils:: * ;
3938use crate :: error:: { from_io_error, from_thrift_error, from_thrift_exception} ;
4039
40+ /// HMS catalog address
41+ pub const HMS_CATALOG_PROP_URI : & str = "uri" ;
42+
43+ /// HMS Catalog thrift transport
44+ pub const HMS_CATALOG_PROP_THRIFT_TRANSPORT : & str = "thrift_transport" ;
45+ /// HMS Catalog framed thrift transport
46+ pub const THRIFT_TRANSPORT_FRAMED : & str = "framed" ;
47+ /// HMS Catalog buffered thrift transport
48+ pub const THRIFT_TRANSPORT_BUFFERED : & str = "buffered" ;
49+
50+ /// HMS Catalog warehouse location
51+ pub const HMS_CATALOG_PROP_WAREHOUSE : & str = "warehouse" ;
52+
53+ /// Builder for [`RestCatalog`].
54+ #[ derive( Debug ) ]
55+ pub struct HmsCatalogBuilder ( HmsCatalogConfig ) ;
56+
57+ impl Default for HmsCatalogBuilder {
58+ fn default ( ) -> Self {
59+ Self ( HmsCatalogConfig {
60+ name : None ,
61+ address : "" . to_string ( ) ,
62+ thrift_transport : HmsThriftTransport :: default ( ) ,
63+ warehouse : "" . to_string ( ) ,
64+ props : HashMap :: new ( ) ,
65+ } )
66+ }
67+ }
68+
69+ impl CatalogBuilder for HmsCatalogBuilder {
70+ type C = HmsCatalog ;
71+
72+ fn load (
73+ mut self ,
74+ name : impl Into < String > ,
75+ props : HashMap < String , String > ,
76+ ) -> impl Future < Output = Result < Self :: C > > + Send {
77+ self . 0 . name = Some ( name. into ( ) ) ;
78+
79+ if props. contains_key ( HMS_CATALOG_PROP_URI ) {
80+ self . 0 . address = props. get ( HMS_CATALOG_PROP_URI ) . cloned ( ) . unwrap_or_default ( ) ;
81+ }
82+
83+ if let Some ( tt) = props. get ( HMS_CATALOG_PROP_THRIFT_TRANSPORT ) {
84+ self . 0 . thrift_transport = match tt. to_lowercase ( ) . as_str ( ) {
85+ THRIFT_TRANSPORT_FRAMED => HmsThriftTransport :: Framed ,
86+ THRIFT_TRANSPORT_BUFFERED => HmsThriftTransport :: Buffered ,
87+ _ => HmsThriftTransport :: default ( ) ,
88+ } ;
89+ }
90+
91+ if props. contains_key ( HMS_CATALOG_PROP_WAREHOUSE ) {
92+ self . 0 . warehouse = props
93+ . get ( HMS_CATALOG_PROP_WAREHOUSE )
94+ . cloned ( )
95+ . unwrap_or_default ( ) ;
96+ }
97+
98+ self . 0 . props = props
99+ . into_iter ( )
100+ . filter ( |( k, _) | {
101+ k != HMS_CATALOG_PROP_URI
102+ && k != HMS_CATALOG_PROP_THRIFT_TRANSPORT
103+ && k != HMS_CATALOG_PROP_WAREHOUSE
104+ } )
105+ . collect ( ) ;
106+
107+ let result = {
108+ if self . 0 . name . is_none ( ) {
109+ Err ( Error :: new (
110+ ErrorKind :: DataInvalid ,
111+ "Catalog name is required" ,
112+ ) )
113+ } else if self . 0 . address . is_empty ( ) {
114+ Err ( Error :: new (
115+ ErrorKind :: DataInvalid ,
116+ "Catalog address is required" ,
117+ ) )
118+ } else if self . 0 . warehouse . is_empty ( ) {
119+ Err ( Error :: new (
120+ ErrorKind :: DataInvalid ,
121+ "Catalog warehouse is required" ,
122+ ) )
123+ } else {
124+ HmsCatalog :: new ( self . 0 )
125+ }
126+ } ;
127+
128+ std:: future:: ready ( result)
129+ }
130+ }
131+
41132/// Which variant of the thrift transport to communicate with HMS
42133/// See: <https://github.com/apache/thrift/blob/master/doc/specs/thrift-rpc.md#framed-vs-unframed-transport>
43134#[ derive( Debug , Default ) ]
@@ -50,12 +141,12 @@ pub enum HmsThriftTransport {
50141}
51142
52143/// Hive metastore Catalog configuration.
53- #[ derive( Debug , TypedBuilder ) ]
54- pub struct HmsCatalogConfig {
144+ #[ derive( Debug ) ]
145+ pub ( crate ) struct HmsCatalogConfig {
146+ name : Option < String > ,
55147 address : String ,
56148 thrift_transport : HmsThriftTransport ,
57149 warehouse : String ,
58- #[ builder( default ) ]
59150 props : HashMap < String , String > ,
60151}
61152
@@ -78,7 +169,7 @@ impl Debug for HmsCatalog {
78169
79170impl HmsCatalog {
80171 /// Create a new hms catalog.
81- pub fn new ( config : HmsCatalogConfig ) -> Result < Self > {
172+ fn new ( config : HmsCatalogConfig ) -> Result < Self > {
82173 let address = config
83174 . address
84175 . as_str ( )
0 commit comments