Skip to content

Commit e0c56f2

Browse files
committed
add example
1 parent fdfb4ee commit e0c56f2

2 files changed

Lines changed: 46 additions & 6 deletions

File tree

datafusion/sql/src/expr/mod.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,12 +1031,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
10311031
_ => expr,
10321032
};
10331033

1034-
// Currently drops metadata attached to the type
1035-
// https://github.com/apache/datafusion/issues/18060
1036-
Ok(Expr::Cast(Cast::new(
1037-
Box::new(expr),
1038-
dt.data_type().clone(),
1039-
)))
1034+
Ok(Expr::Cast(Cast::new_from_field(Box::new(expr), dt)))
10401035
}
10411036

10421037
/// Extracts the root expression and access chain from a compound expression.

docs/source/library-user-guide/extending-sql.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,51 @@ async fn main() -> Result<()> {
202202
}
203203
```
204204

205+
#### Example: Supporting the UUID Type
206+
207+
```rust
208+
# use std::sync::Arc;
209+
# use arrow::datatypes::{FieldRef, TimeUnit};
210+
# use datafusion::error::Result;
211+
# use datafusion::prelude::*;
212+
# use datafusion::execution::SessionStateBuilder;
213+
use datafusion_expr::planner::TypePlanner;
214+
# use sqlparser::ast;
215+
216+
#[derive(Debug)]
217+
struct MyTypePlanner;
218+
219+
impl TypePlanner for MyTypePlanner {
220+
fn plan_type_field(&self, sql_type: &ast::DataType) -> Result<Option<FieldRef>> {
221+
match sql_type {
222+
sqlparser::ast::DataType::Uuid => Ok(Some(Arc::new(
223+
Field::new("", DataType::FixedSizeBinary(16), true).with_metadata(
224+
[("ARROW:extension:name".to_string(), "arrow.uuid".to_string())]
225+
.into(),
226+
),
227+
))),
228+
_ => Ok(self
229+
.plan_type(sql_type)?
230+
.map(|data_type| data_type.into_nullable_field_ref())),
231+
}
232+
}
233+
}
234+
235+
#[tokio::main]
236+
async fn main() -> Result<()> {
237+
let state = SessionStateBuilder::new()
238+
.with_default_features()
239+
.with_type_planner(Arc::new(MyTypePlanner))
240+
.build();
241+
242+
let ctx = SessionContext::new_with_state(state);
243+
244+
// Now UUID type is recognized
245+
ctx.sql("CREATE TABLE idx (uuid UUID)").await?;
246+
Ok(())
247+
}
248+
```
249+
205250
For more details, see the [TypePlanner API documentation].
206251

207252
### RelationPlanner: Custom FROM Clause Elements

0 commit comments

Comments
 (0)