@@ -3,13 +3,15 @@ use crate::models::{StorageProfile, StorageProfileCreateRequest};
33use crate :: models:: { Warehouse , WarehouseCreateRequest } ;
44use crate :: repository:: { StorageProfileRepository , WarehouseRepository } ;
55use async_trait:: async_trait;
6- use datafusion:: catalog_common:: CatalogProvider ;
7- use datafusion:: prelude:: * ;
8- use iceberg_catalog_rest:: { RestCatalog , RestCatalogConfig } ;
9- use iceberg_datafusion:: IcebergCatalogProvider ;
10- use std:: collections:: HashMap ;
116use std:: sync:: Arc ;
127use uuid:: Uuid ;
8+ use datafusion:: prelude:: * ;
9+ use iceberg_rest_catalog:: apis:: configuration:: Configuration ;
10+ use iceberg_rust:: catalog:: bucket:: ObjectStoreBuilder ;
11+ use datafusion_iceberg:: catalog:: catalog:: IcebergCatalog ;
12+ use iceberg_rest_catalog:: catalog:: RestCatalog ;
13+ use arrow:: record_batch:: RecordBatch ;
14+ use object_store:: local:: LocalFileSystem ;
1315
1416#[ async_trait]
1517pub trait ControlService : Send + Sync {
@@ -33,7 +35,8 @@ pub trait ControlService: Send + Sync {
3335 // async fn delete_table(&self, id: Uuid) -> Result<()>;
3436 // async fn list_tables(&self) -> Result<Vec<Table>>;
3537
36- async fn query_table ( & self , warehouse_id : & Uuid , query : & String ) -> Result < ( & str ) > ;
38+ async fn query_table ( & self , warehouse_id : & Uuid , database_name : & String , table_name : & String , query : & String ) -> Result <
39+ ( String ) > ;
3740}
3841
3942pub struct ControlServiceImpl {
@@ -102,25 +105,58 @@ impl ControlService for ControlServiceImpl {
102105 self . warehouse_repo . list ( ) . await
103106 }
104107
105- async fn query_table ( & self , warehouse_id : & Uuid , query : & String ) -> Result < ( & str ) > {
106- let config = RestCatalogConfig :: builder ( )
107- . uri ( "http://0.0.0.0:3000/catalog" . to_string ( ) )
108- . warehouse ( warehouse_id. to_string ( ) )
109- . props ( HashMap :: default ( ) )
110- . build ( ) ;
108+ async fn query_table ( & self , warehouse_id : & Uuid , database_name : & String , table_name : & String , query : & String ) -> Result <
109+ ( String ) > {
110+ let config = {
111+ let mut config = Configuration :: new ( ) ;
112+ config. base_path = "http://0.0.0.0:3000/catalog" . to_string ( ) ;
113+ config
114+ } ;
115+ let builder = {
116+ Arc :: new ( LocalFileSystem :: new ( ) )
117+ } ;
118+ let rest_client = RestCatalog :: new (
119+ Some ( warehouse_id. to_string ( ) . as_str ( ) ) ,
120+ config,
121+ ObjectStoreBuilder :: Filesystem ( builder) ,
122+ ) ;
123+ let catalog = IcebergCatalog :: new ( Arc :: new ( rest_client) , None )
124+ . await
125+ . unwrap ( ) ;
126+
127+ let ctx = SessionContext :: new ( ) ;
128+ ctx. register_catalog ( "catalog" , Arc :: new ( catalog) ) ;
129+
130+ let provider = ctx. catalog ( "catalog" ) . unwrap ( ) ;
131+ let schemas = provider. schema_names ( ) ;
132+ println ! ( "{schemas:?}" ) ;
111133
112- let catalog = RestCatalog :: new ( config) ;
134+ let tables = provider. schema ( database_name) . unwrap ( ) . table_names ( ) ;
135+ println ! ( "{tables:?}" ) ;
113136
114- let catalog = IcebergCatalogProvider :: try_new ( Arc :: new ( catalog) )
137+ println ! ( "{}" , query) ;
138+ let records = ctx
139+ . sql ( query)
140+ . await
141+ . unwrap ( )
142+ . collect ( )
115143 . await
116144 . unwrap ( ) ;
145+ println ! ( "{records:?}" ) ;
146+
147+ let df = ctx. sql ( query, ) . await . unwrap ( ) ;
148+ df. show ( ) . await . unwrap ( ) ;
117149
118- // Test that catalog loaded successfully
119- println ! ( "SCHEMAS: {:?}" , catalog. schema_names( ) ) ;
150+ let buf = Vec :: new ( ) ;
151+ let mut writer = arrow_json:: ArrayWriter :: new ( buf) ;
152+ let record_refs: Vec < & RecordBatch > = records. iter ( ) . collect ( ) ;
153+ writer. write_batches ( & record_refs) . unwrap ( ) ;
154+ writer. finish ( ) . unwrap ( ) ;
120155
121- // TODO rest of the query code
156+ // Get the underlying buffer back,
157+ let buf = writer. into_inner ( ) ;
122158
123- Ok ( ( "OK" ) )
159+ Ok ( ( String :: from_utf8 ( buf ) . unwrap ( ) ) )
124160 }
125161}
126162
0 commit comments