1#![doc(
19 html_logo_url = "https://raw.githubusercontent.com/apache/datafusion/19fe44cf2f30cbdd63d4a4f52c74055163c6cc38/docs/logos/standalone_logo/logo_original.svg",
20 html_favicon_url = "https://raw.githubusercontent.com/apache/datafusion/19fe44cf2f30cbdd63d4a4f52c74055163c6cc38/docs/logos/standalone_logo/logo_original.svg"
21)]
22#![cfg_attr(docsrs, feature(doc_auto_cfg))]
23#![deny(clippy::clone_on_ref_ptr)]
26
27mod column;
28mod dfschema;
29mod functional_dependencies;
30mod join_type;
31mod param_value;
32#[cfg(feature = "pyarrow")]
33mod pyarrow;
34mod schema_reference;
35mod table_reference;
36mod unnest;
37
38pub mod alias;
39pub mod cast;
40pub mod config;
41pub mod cse;
42pub mod diagnostic;
43pub mod display;
44pub mod encryption;
45pub mod error;
46pub mod file_options;
47pub mod format;
48pub mod hash_utils;
49pub mod instant;
50pub mod nested_struct;
51mod null_equality;
52pub mod parsers;
53pub mod pruning;
54pub mod rounding;
55pub mod scalar;
56pub mod spans;
57pub mod stats;
58pub mod test_util;
59pub mod tree_node;
60pub mod types;
61pub mod utils;
62
63pub use arrow;
65pub use column::Column;
66pub use dfschema::{
67 qualified_name, DFSchema, DFSchemaRef, ExprSchema, SchemaExt, ToDFSchema,
68};
69pub use diagnostic::Diagnostic;
70pub use error::{
71 field_not_found, unqualified_field_not_found, DataFusionError, Result, SchemaError,
72 SharedResult,
73};
74pub use file_options::file_type::{
75 GetExt, DEFAULT_ARROW_EXTENSION, DEFAULT_AVRO_EXTENSION, DEFAULT_CSV_EXTENSION,
76 DEFAULT_JSON_EXTENSION, DEFAULT_PARQUET_EXTENSION,
77};
78pub use functional_dependencies::{
79 aggregate_functional_dependencies, get_required_group_by_exprs_indices,
80 get_target_functional_dependencies, Constraint, Constraints, Dependency,
81 FunctionalDependence, FunctionalDependencies,
82};
83use hashbrown::hash_map::DefaultHashBuilder;
84pub use join_type::{JoinConstraint, JoinSide, JoinType};
85pub use null_equality::NullEquality;
86pub use param_value::ParamValues;
87pub use scalar::{ScalarType, ScalarValue};
88pub use schema_reference::SchemaReference;
89pub use spans::{Location, Span, Spans};
90pub use stats::{ColumnStatistics, Statistics};
91pub use table_reference::{ResolvedTableReference, TableReference};
92pub use unnest::{RecursionUnnestOption, UnnestOptions};
93pub use utils::project_schema;
94
95#[doc(hidden)]
101pub use error::{
102 _config_datafusion_err, _exec_datafusion_err, _internal_datafusion_err,
103 _not_impl_datafusion_err, _plan_datafusion_err, _resources_datafusion_err,
104 _substrait_datafusion_err,
105};
106
107pub type HashMap<K, V, S = DefaultHashBuilder> = hashbrown::HashMap<K, V, S>;
109pub type HashSet<T, S = DefaultHashBuilder> = hashbrown::HashSet<T, S>;
110
111#[macro_export]
116macro_rules! downcast_value {
117 ($Value: expr, $Type: ident) => {{
118 use $crate::__private::DowncastArrayHelper;
119 $Value.downcast_array_helper::<$Type>()?
120 }};
121 ($Value: expr, $Type: ident, $T: tt) => {{
122 use $crate::__private::DowncastArrayHelper;
123 $Value.downcast_array_helper::<$Type<$T>>()?
124 }};
125}
126
127#[doc(hidden)]
129pub mod __private {
130 use crate::error::_internal_datafusion_err;
131 use crate::Result;
132 use arrow::array::Array;
133 use std::any::{type_name, Any};
134
135 #[doc(hidden)]
136 pub trait DowncastArrayHelper {
137 fn downcast_array_helper<U: Any>(&self) -> Result<&U>;
138 }
139
140 impl<T: Array + ?Sized> DowncastArrayHelper for T {
141 fn downcast_array_helper<U: Any>(&self) -> Result<&U> {
142 self.as_any().downcast_ref().ok_or_else(|| {
143 let actual_type = self.data_type();
144 let desired_type_name = type_name::<U>();
145 _internal_datafusion_err!(
146 "could not cast array of type {} to {}",
147 actual_type,
148 desired_type_name
149 )
150 })
151 }
152 }
153}
154
155#[cfg(test)]
156mod tests {
157 use arrow::array::{ArrayRef, Int32Array, UInt64Array};
158 use std::any::{type_name, type_name_of_val};
159 use std::sync::Arc;
160
161 #[test]
162 fn test_downcast_value() -> crate::Result<()> {
163 let boxed: ArrayRef = Arc::new(Int32Array::from(vec![1, 2, 3]));
164 let array = downcast_value!(&boxed, Int32Array);
165 assert_eq!(type_name_of_val(&array), type_name::<&Int32Array>());
166
167 let expected: Int32Array = vec![1, 2, 3].into_iter().map(Some).collect();
168 assert_eq!(array, &expected);
169 Ok(())
170 }
171
172 #[test]
173 fn test_downcast_value_err_message() {
174 let boxed: ArrayRef = Arc::new(Int32Array::from(vec![1, 2, 3]));
175 let error: crate::DataFusionError = (|| {
176 downcast_value!(&boxed, UInt64Array);
177 Ok(())
178 })()
179 .err()
180 .unwrap();
181
182 assert_starts_with(
183 error.to_string(),
184 "Internal error: could not cast array of type Int32 to arrow_array::array::primitive_array::PrimitiveArray<arrow_array::types::UInt64Type>"
185 );
186 }
187
188 fn assert_starts_with(actual: impl AsRef<str>, expected_prefix: impl AsRef<str>) {
191 let actual = actual.as_ref();
192 let expected_prefix = expected_prefix.as_ref();
193 assert!(
194 actual.starts_with(expected_prefix),
195 "Expected '{actual}' to start with '{expected_prefix}'"
196 );
197 }
198}