Skip to content

Commit 3db5082

Browse files
committed
Add simple registration.
Pretty-printing record batches is working.
1 parent 5c60c1f commit 3db5082

4 files changed

Lines changed: 70 additions & 6 deletions

File tree

datafusion/core/src/execution/session_state.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,11 @@ impl SessionStateBuilder {
11251125
.get_or_insert_with(Vec::new)
11261126
.extend(SessionStateDefaults::default_window_functions());
11271127

1128+
self.extension_types
1129+
.get_or_insert_with(|| Arc::new(MemoryExtensionTypeRegistry::new()))
1130+
.extend(&SessionStateDefaults::default_extension_types())
1131+
.expect("MemoryExtensionTypeRegistry is not read-only.");
1132+
11281133
self.table_functions
11291134
.get_or_insert_with(HashMap::new)
11301135
.extend(

datafusion/core/src/execution/session_state_defaults.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,16 @@ use crate::execution::context::SessionState;
3030
#[cfg(feature = "nested_expressions")]
3131
use crate::functions_nested;
3232
use crate::{functions, functions_aggregate, functions_table, functions_window};
33+
use arrow_schema::extension::{ExtensionType, Uuid};
3334
use datafusion_catalog::TableFunction;
3435
use datafusion_catalog::{MemoryCatalogProvider, MemorySchemaProvider};
3536
use datafusion_execution::config::SessionConfig;
3637
use datafusion_execution::object_store::ObjectStoreUrl;
3738
use datafusion_execution::runtime_env::RuntimeEnv;
3839
use datafusion_expr::planner::ExprPlanner;
40+
use datafusion_expr::registry::{
41+
ExtensionTypeRegistrationRef, SimpleExtensionTypeRegistration,
42+
};
3943
use datafusion_expr::{AggregateUDF, ScalarUDF, WindowUDF};
4044
use std::collections::HashMap;
4145
use std::sync::Arc;
@@ -122,6 +126,14 @@ impl SessionStateDefaults {
122126
functions_window::all_default_window_functions()
123127
}
124128

129+
/// returns the list of default [`WindowUDF`]s
130+
pub fn default_extension_types() -> Vec<ExtensionTypeRegistrationRef> {
131+
vec![SimpleExtensionTypeRegistration::new_arc(
132+
Uuid::NAME,
133+
Arc::new(Uuid),
134+
)]
135+
}
136+
125137
/// returns the list of default [`TableFunction`]s
126138
pub fn default_table_functions() -> Vec<Arc<TableFunction>> {
127139
functions_table::all_default_table_functions()

datafusion/core/tests/extension_types/pretty_print_data_frames.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ async fn test_pretty_print_extension_types() -> Result<()> {
4242
assert_snapshot!(
4343
result,
4444
@r"
45-
+---------------+
46-
| ascii(test.a) |
47-
+---------------+
48-
| 97 |
49-
+---------------+
45+
+--------------------------------------------------+
46+
| my_uuids |
47+
+--------------------------------------------------+
48+
| arrow.uuid(00000000-0000-0000-0000-000000000000) |
49+
| arrow.uuid(00010203-0405-0607-0809-000102030506) |
50+
+--------------------------------------------------+
5051
"
5152
);
5253

datafusion/expr/src/registry.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::planner::ExprPlanner;
2222
use crate::{AggregateUDF, ScalarUDF, UserDefinedLogicalNode, WindowUDF};
2323
use arrow_schema::Field;
2424
use datafusion_common::types::{LogicalTypeRef, NativeType};
25-
use datafusion_common::{HashMap, Result, not_impl_err, plan_datafusion_err};
25+
use datafusion_common::{not_impl_err, plan_datafusion_err, HashMap, Result};
2626
use std::collections::HashSet;
2727
use std::fmt::Debug;
2828
use std::sync::{Arc, RwLock};
@@ -272,6 +272,17 @@ pub trait ExtensionTypeRegistry: Debug + Send + Sync {
272272
extension_type: ExtensionTypeRegistrationRef,
273273
) -> Result<Option<ExtensionTypeRegistrationRef>>;
274274

275+
/// Extends the registry with the provided extension types.
276+
///
277+
/// Returns an error if the type cannot be registered, for example, if the registry is
278+
/// read-only.
279+
fn extend(&self, extension_types: &[ExtensionTypeRegistrationRef]) -> Result<()> {
280+
for extension_type in extension_types {
281+
self.register_extension_type(extension_type.clone())?;
282+
}
283+
Ok(())
284+
}
285+
275286
/// Deregisters an extension type registration with the name `name`, returning the
276287
/// implementation that was deregistered.
277288
///
@@ -283,6 +294,41 @@ pub trait ExtensionTypeRegistry: Debug + Send + Sync {
283294
) -> Result<Option<ExtensionTypeRegistrationRef>>;
284295
}
285296

297+
/// A simple implementation of [ExtensionTypeRegistration] where the logical type instance can be
298+
/// obtained by cloning the inner [`LogicalTypeRef`].
299+
///
300+
/// As a result, the logical type instance cannot depend on any parameters. For example, if the
301+
/// logical type is `Opaque(Uuid)`, and the `create_logical_type` method should return an instance
302+
/// of `Opaque` where `Uuid` is a parameter, then this implementation cannot be used, as the inner
303+
/// type (`Uuid`) needs to be extracted from the field and passed into the `Opaque` constructor.
304+
#[derive(Debug)]
305+
pub struct SimpleExtensionTypeRegistration {
306+
/// The name of the extension type.
307+
name: String,
308+
/// The logical type instance.
309+
logical_type: LogicalTypeRef,
310+
}
311+
312+
impl SimpleExtensionTypeRegistration {
313+
/// Creates a new registration for the given `name` and `logical_type`.
314+
pub fn new_arc(name: &str, logical_type: LogicalTypeRef) -> Arc<Self> {
315+
Arc::new(Self {
316+
name: name.to_string(),
317+
logical_type,
318+
})
319+
}
320+
}
321+
322+
impl ExtensionTypeRegistration for SimpleExtensionTypeRegistration {
323+
fn type_name(&self) -> &str {
324+
&self.name
325+
}
326+
327+
fn create_logical_type(&self, _field: &Field) -> Result<LogicalTypeRef> {
328+
Ok(Arc::clone(&self.logical_type))
329+
}
330+
}
331+
286332
/// An [`ExtensionTypeRegistry`] that uses in memory [`HashMap`]s.
287333
#[derive(Clone, Debug)]
288334
pub struct MemoryExtensionTypeRegistry {

0 commit comments

Comments
 (0)