|
6 | 6 | //! tables and `SHOW` statements. |
7 | 7 |
|
8 | 8 | use crate::TableType; |
| 9 | +use crate::registry::{ExtensionTypeRegistry, ExtensionTypeRegistryRef}; |
9 | 10 | use arrow::array::ArrayRef; |
10 | 11 | use arrow::array::builder::{BooleanBuilder, StringBuilder, UInt8Builder, UInt64Builder}; |
11 | 12 | use arrow::datatypes::{DataType, Field, Schema, SchemaRef}; |
12 | 13 | use arrow::record_batch::RecordBatch; |
13 | 14 | use datafusion_common::Result; |
14 | 15 | use datafusion_common::config::ConfigEntry; |
| 16 | +use std::fmt::Write; |
15 | 17 | use std::sync::Arc; |
16 | 18 |
|
17 | 19 | /// Returns the Arrow schema of `information_schema.tables` rows. |
@@ -230,6 +232,7 @@ pub struct InformationSchemaColumnsBuilder { |
230 | 232 | numeric_scales: Option<UInt64Builder>, |
231 | 233 | datetime_precisions: Option<UInt64Builder>, |
232 | 234 | interval_types: Option<StringBuilder>, |
| 235 | + extension_types: Option<ExtensionTypeRegistryRef>, |
233 | 236 | } |
234 | 237 |
|
235 | 238 | impl InformationSchemaColumnsBuilder { |
@@ -269,9 +272,21 @@ impl InformationSchemaColumnsBuilder { |
269 | 272 | datetime_precisions: full |
270 | 273 | .then(|| UInt64Builder::with_capacity(default_capacity)), |
271 | 274 | interval_types: full.then(StringBuilder::new), |
| 275 | + extension_types: None, |
272 | 276 | } |
273 | 277 | } |
274 | 278 |
|
| 279 | + /// Use the extension type registry to show the |
| 280 | + /// [`display_name`](datafusion_common::types::DFExtensionType::display_name) |
| 281 | + /// of registered extension types in `data_type`. |
| 282 | + pub fn with_extension_type_registry( |
| 283 | + mut self, |
| 284 | + extension_types: Option<ExtensionTypeRegistryRef>, |
| 285 | + ) -> Self { |
| 286 | + self.extension_types = extension_types; |
| 287 | + self |
| 288 | + } |
| 289 | + |
275 | 290 | /// Append one row describing `field`. Values not needed at this builder's |
276 | 291 | /// detail level are discarded. |
277 | 292 | pub fn add_column( |
@@ -310,8 +325,13 @@ impl InformationSchemaColumnsBuilder { |
310 | 325 | let nullable_str = if field.is_nullable() { "YES" } else { "NO" }; |
311 | 326 | self.is_nullables.append_value(nullable_str); |
312 | 327 |
|
313 | | - // "System supplied type" --> Use display format of the datatype |
314 | | - self.data_types.append_value(field.data_type().to_string()); |
| 328 | + // "System supplied type" --> Use the display name of the extension type or the |
| 329 | + // display format of the datatype |
| 330 | + append_field_type_display_name( |
| 331 | + self.extension_types.as_deref(), |
| 332 | + field, |
| 333 | + &mut self.data_types, |
| 334 | + ); |
315 | 335 |
|
316 | 336 | // "If data_type identifies a character or bit string type, the |
317 | 337 | // declared maximum length; null for all other data types or |
@@ -435,6 +455,35 @@ impl InformationSchemaColumnsBuilder { |
435 | 455 | } |
436 | 456 | } |
437 | 457 |
|
| 458 | +/// Appends the name of the type of `field` shown in `data_type` to `builder`. |
| 459 | +/// |
| 460 | +/// If the field has an extension type registered in `registry`, appends |
| 461 | +/// [`display_name`] if possible. appended. Otherwise, the storage type (raw |
| 462 | +/// Arrow physical type name) is appended. |
| 463 | +/// |
| 464 | +/// [`display_name`]: datafusion_common::types::DFExtensionType::display_name |
| 465 | +fn append_field_type_display_name( |
| 466 | + registry: Option<&dyn ExtensionTypeRegistry>, |
| 467 | + field: &Field, |
| 468 | + builder: &mut StringBuilder, |
| 469 | +) { |
| 470 | + let extension_type = registry |
| 471 | + .and_then(|registry| registry.create_extension_type_for_field(field).ok()) |
| 472 | + .flatten(); |
| 473 | + match extension_type |
| 474 | + .as_ref() |
| 475 | + .and_then(|extension_type| extension_type.display_name()) |
| 476 | + { |
| 477 | + Some(name) => builder.append_value(name), |
| 478 | + None => { |
| 479 | + // Write the storage type straight into the builder, then finish the row. |
| 480 | + // Writing to a `StringBuilder` does not fail. |
| 481 | + let _ = write!(builder, "{}", field.data_type()); |
| 482 | + builder.append_value(""); |
| 483 | + } |
| 484 | + } |
| 485 | +} |
| 486 | + |
438 | 487 | /// Returns the Arrow schema of `information_schema.schemata` rows. |
439 | 488 | /// |
440 | 489 | /// Columns and nullability match |
@@ -827,3 +876,56 @@ pub fn show_functions_schema() -> Schema { |
827 | 876 | Field::new("syntax_example", DataType::Utf8, true), |
828 | 877 | ]) |
829 | 878 | } |
| 879 | + |
| 880 | +#[cfg(test)] |
| 881 | +mod tests { |
| 882 | + use super::*; |
| 883 | + use crate::registry::MemoryExtensionTypeRegistry; |
| 884 | + use arrow::array::Array; |
| 885 | + use arrow_schema::extension::{EXTENSION_TYPE_NAME_KEY, Uuid}; |
| 886 | + use std::collections::HashMap; |
| 887 | + |
| 888 | + fn display_names( |
| 889 | + registry: Option<&dyn ExtensionTypeRegistry>, |
| 890 | + fields: &[Field], |
| 891 | + ) -> Vec<String> { |
| 892 | + let mut builder = StringBuilder::new(); |
| 893 | + for field in fields { |
| 894 | + append_field_type_display_name(registry, field, &mut builder); |
| 895 | + } |
| 896 | + let array = builder.finish(); |
| 897 | + (0..array.len()) |
| 898 | + .map(|i| array.value(i).to_owned()) |
| 899 | + .collect() |
| 900 | + } |
| 901 | + |
| 902 | + /// Fields whose extension type cannot be resolved show their storage type. The |
| 903 | + /// resolvable cases are covered by `sql_extension_types.slt`. |
| 904 | + #[test] |
| 905 | + fn test_append_field_type_display_name_fallbacks() { |
| 906 | + let registry = MemoryExtensionTypeRegistry::new_with_canonical_extension_types(); |
| 907 | + |
| 908 | + let unregistered = Field::new("x", DataType::Int32, true).with_metadata( |
| 909 | + HashMap::from([(EXTENSION_TYPE_NAME_KEY.to_owned(), "example.x".to_owned())]), |
| 910 | + ); |
| 911 | + // `arrow.uuid` with a storage type that the extension type does not support |
| 912 | + let invalid = |
| 913 | + Field::new("i", DataType::Int32, true).with_metadata(HashMap::from([( |
| 914 | + EXTENSION_TYPE_NAME_KEY.to_owned(), |
| 915 | + "arrow.uuid".to_owned(), |
| 916 | + )])); |
| 917 | + assert_eq!( |
| 918 | + display_names(Some(®istry), &[unregistered, invalid]), |
| 919 | + ["Int32", "Int32"] |
| 920 | + ); |
| 921 | + |
| 922 | + let uuid = Field::new("u", DataType::FixedSizeBinary(16), true) |
| 923 | + .with_extension_type(Uuid); |
| 924 | + let empty_registry = MemoryExtensionTypeRegistry::new_empty(); |
| 925 | + assert_eq!( |
| 926 | + display_names(Some(&empty_registry), std::slice::from_ref(&uuid)), |
| 927 | + ["FixedSizeBinary(16)"] |
| 928 | + ); |
| 929 | + assert_eq!(display_names(None, &[uuid]), ["FixedSizeBinary(16)"]); |
| 930 | + } |
| 931 | +} |
0 commit comments