|
17 | 17 |
|
18 | 18 | //! SQL context integration tests for paimon-datafusion. |
19 | 19 |
|
20 | | -use std::sync::atomic::{AtomicBool, Ordering}; |
| 20 | +use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; |
21 | 21 | use std::sync::{Arc, Mutex}; |
22 | 22 |
|
23 | 23 | use async_trait::async_trait; |
@@ -50,6 +50,105 @@ async fn create_sql_context(catalog: Arc<FileSystemCatalog>) -> SQLContext { |
50 | 50 | ctx |
51 | 51 | } |
52 | 52 |
|
| 53 | +struct MetadataListingCatalog { |
| 54 | + get_table_calls: AtomicUsize, |
| 55 | +} |
| 56 | + |
| 57 | +impl MetadataListingCatalog { |
| 58 | + fn new() -> Self { |
| 59 | + Self { |
| 60 | + get_table_calls: AtomicUsize::new(0), |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + fn get_table_calls(&self) -> usize { |
| 65 | + self.get_table_calls.load(Ordering::SeqCst) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +#[async_trait] |
| 70 | +impl Catalog for MetadataListingCatalog { |
| 71 | + async fn list_databases(&self) -> paimon::Result<Vec<String>> { |
| 72 | + Ok(vec!["default".to_string()]) |
| 73 | + } |
| 74 | + |
| 75 | + async fn create_database( |
| 76 | + &self, |
| 77 | + _name: &str, |
| 78 | + _ignore_if_exists: bool, |
| 79 | + _properties: std::collections::HashMap<String, String>, |
| 80 | + ) -> paimon::Result<()> { |
| 81 | + Ok(()) |
| 82 | + } |
| 83 | + |
| 84 | + async fn get_database(&self, name: &str) -> paimon::Result<paimon::catalog::Database> { |
| 85 | + Ok(paimon::catalog::Database::new( |
| 86 | + name.to_string(), |
| 87 | + std::collections::HashMap::new(), |
| 88 | + None, |
| 89 | + )) |
| 90 | + } |
| 91 | + |
| 92 | + async fn drop_database( |
| 93 | + &self, |
| 94 | + _name: &str, |
| 95 | + _ignore_if_not_exists: bool, |
| 96 | + _cascade: bool, |
| 97 | + ) -> paimon::Result<()> { |
| 98 | + Ok(()) |
| 99 | + } |
| 100 | + |
| 101 | + async fn get_table(&self, _identifier: &Identifier) -> paimon::Result<paimon::table::Table> { |
| 102 | + self.get_table_calls.fetch_add(1, Ordering::SeqCst); |
| 103 | + Err(paimon::Error::Unsupported { |
| 104 | + message: "table loading is unavailable".to_string(), |
| 105 | + }) |
| 106 | + } |
| 107 | + |
| 108 | + async fn list_tables(&self, _database_name: &str) -> paimon::Result<Vec<String>> { |
| 109 | + Ok(vec!["metadata_only".to_string()]) |
| 110 | + } |
| 111 | + |
| 112 | + async fn list_views(&self, _database_name: &str) -> paimon::Result<Vec<String>> { |
| 113 | + Ok(vec!["metadata_view".to_string()]) |
| 114 | + } |
| 115 | + |
| 116 | + async fn create_table( |
| 117 | + &self, |
| 118 | + _identifier: &Identifier, |
| 119 | + _creation: paimon::spec::Schema, |
| 120 | + _ignore_if_exists: bool, |
| 121 | + ) -> paimon::Result<()> { |
| 122 | + Ok(()) |
| 123 | + } |
| 124 | + |
| 125 | + async fn drop_table( |
| 126 | + &self, |
| 127 | + _identifier: &Identifier, |
| 128 | + _ignore_if_not_exists: bool, |
| 129 | + ) -> paimon::Result<()> { |
| 130 | + Ok(()) |
| 131 | + } |
| 132 | + |
| 133 | + async fn rename_table( |
| 134 | + &self, |
| 135 | + _from: &Identifier, |
| 136 | + _to: &Identifier, |
| 137 | + _ignore_if_not_exists: bool, |
| 138 | + ) -> paimon::Result<()> { |
| 139 | + Ok(()) |
| 140 | + } |
| 141 | + |
| 142 | + async fn alter_table( |
| 143 | + &self, |
| 144 | + _identifier: &Identifier, |
| 145 | + _changes: Vec<SchemaChange>, |
| 146 | + _ignore_if_not_exists: bool, |
| 147 | + ) -> paimon::Result<()> { |
| 148 | + Ok(()) |
| 149 | + } |
| 150 | +} |
| 151 | + |
53 | 152 | struct PartitionCatalog { |
54 | 153 | inner: Arc<FileSystemCatalog>, |
55 | 154 | fail_list_partitions: AtomicBool, |
@@ -265,6 +364,48 @@ async fn test_show_tables_is_enabled() { |
265 | 364 | .expect("SHOW TABLES should execute"); |
266 | 365 | } |
267 | 366 |
|
| 367 | +#[tokio::test] |
| 368 | +async fn test_show_tables_does_not_load_table_providers() { |
| 369 | + let catalog = Arc::new(MetadataListingCatalog::new()); |
| 370 | + let mut sql_context = SQLContext::new(); |
| 371 | + sql_context |
| 372 | + .register_catalog("paimon", catalog.clone()) |
| 373 | + .await |
| 374 | + .unwrap(); |
| 375 | + |
| 376 | + let table_names = collect_string_column(&sql_context, "SHOW TABLES", "table_name").await; |
| 377 | + |
| 378 | + assert!(table_names.contains(&"metadata_only".to_string())); |
| 379 | + assert_eq!(catalog.get_table_calls(), 0); |
| 380 | + |
| 381 | + assert!(sql_context |
| 382 | + .sql("SELECT * FROM metadata_only") |
| 383 | + .await |
| 384 | + .is_err()); |
| 385 | + assert!(catalog.get_table_calls() > 0); |
| 386 | +} |
| 387 | + |
| 388 | +#[tokio::test] |
| 389 | +async fn test_show_tables_preserves_catalog_view_type() { |
| 390 | + let catalog = Arc::new(MetadataListingCatalog::new()); |
| 391 | + let mut sql_context = SQLContext::new(); |
| 392 | + sql_context |
| 393 | + .register_catalog("paimon", catalog.clone()) |
| 394 | + .await |
| 395 | + .unwrap(); |
| 396 | + |
| 397 | + let table_types = collect_string_column( |
| 398 | + &sql_context, |
| 399 | + "SELECT table_type FROM information_schema.tables \ |
| 400 | + WHERE table_schema = 'default' AND table_name = 'metadata_view'", |
| 401 | + "table_type", |
| 402 | + ) |
| 403 | + .await; |
| 404 | + |
| 405 | + assert_eq!(table_types, vec!["VIEW"]); |
| 406 | + assert_eq!(catalog.get_table_calls(), 0); |
| 407 | +} |
| 408 | + |
268 | 409 | #[tokio::test] |
269 | 410 | async fn test_select_branch_table_reads_branch_snapshot() { |
270 | 411 | let (_tmp, catalog) = create_test_env(); |
|
0 commit comments