-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[draft] Add LogicalType
, try to support user-defined types
#11160
Closed
Closed
Changes from 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5427f4b
draft: add logical types and support extension types
notfilippo 996bbdc
Add type relation
notfilippo f8facb1
Merge remote-tracking branch 'origin/main' into fr/logical-types
notfilippo d65e857
Add example with TODOs
notfilippo ce68d1b
Format
notfilippo f5f79bb
Merge branch 'main' into fr/logical-types
notfilippo 481f6ca
Merge remote-tracking branch 'origin/main' into fr/logical-types
notfilippo 9fd793a
Format
notfilippo 8b9715d
Union mode
notfilippo 981486a
Prepare 40 release
andygrove 43fb645
regenerate changelog
andygrove 4cae813
manually remove a reverted PR from the breaking change section
andygrove e1eca31
Merge remote-tracking branch 'origin/branch-40' into fr/logical-types
notfilippo 5be9029
Rename types to reflect proposal
notfilippo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
use datafusion::datasource::TableProvider; | ||
use datafusion::execution::context::SessionState; | ||
use datafusion::physical_plan::ExecutionPlan; | ||
use datafusion_common::logical_type::schema::{LogicalSchema, LogicalSchemaRef}; | ||
use datafusion::error::Result; | ||
use datafusion_expr::{Expr, TableType}; | ||
use std::any::Any; | ||
use std::sync::Arc; | ||
use arrow::util::pretty::pretty_format_batches; | ||
use arrow_schema::{DataType, Field, TimeUnit}; | ||
use datafusion::prelude::SessionContext; | ||
use datafusion_common::logical_type::{ExtensionType}; | ||
use datafusion_common::logical_type::field::LogicalField; | ||
use datafusion_common::logical_type::signature::LogicalType; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
let ctx = SessionContext::new(); | ||
ctx.register_table("example", Arc::new(ExampleTableSource::default()))?; | ||
|
||
let df = ctx.sql("SELECT * FROM example").await?; | ||
let records = df.collect().await?; | ||
|
||
println!("{}", pretty_format_batches(&records)?); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[derive(Debug)] | ||
struct CustomMagicalType { | ||
logical: LogicalType, | ||
physical: DataType | ||
} | ||
|
||
impl Default for CustomMagicalType { | ||
fn default() -> Self { | ||
Self { | ||
logical: LogicalType::Utf8, | ||
physical: DataType::new_list(DataType::UInt8, false), | ||
} | ||
} | ||
} | ||
|
||
impl ExtensionType for CustomMagicalType { | ||
fn logical(&self) -> &LogicalType { | ||
&self.logical | ||
} | ||
|
||
fn physical(&self) -> &DataType { | ||
&self.physical | ||
} | ||
|
||
// TODO: materialisation methods? | ||
} | ||
|
||
#[derive(Default)] | ||
struct ExampleTableSource {} | ||
|
||
#[async_trait::async_trait] | ||
impl TableProvider for ExampleTableSource { | ||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
|
||
fn schema(&self) -> LogicalSchemaRef { | ||
// TODO: ugly? | ||
let custom_magical_type: Arc<dyn ExtensionType + Send + Sync> = Arc::new(CustomMagicalType::default()); | ||
|
||
// This schema will be equivalent to: | ||
// a -> Timestamp(Microsecond, None) | ||
// b -> Utf8 | ||
// c -> Int64 | ||
Arc::new(LogicalSchema::new(vec![ | ||
LogicalField::new( | ||
"a", | ||
DataType::RunEndEncoded( | ||
Arc::new(Field::new("run_ends", DataType::Int64, false)), | ||
Arc::new(Field::new("values", DataType::Timestamp(TimeUnit::Microsecond, None), false)) | ||
), | ||
false | ||
), | ||
LogicalField::new( | ||
"b", | ||
custom_magical_type, | ||
false | ||
), | ||
LogicalField::new( | ||
"c", | ||
DataType::Int64, | ||
true, | ||
) | ||
])) | ||
} | ||
|
||
fn table_type(&self) -> TableType { | ||
TableType::Base | ||
} | ||
|
||
async fn scan( | ||
&self, | ||
_state: &SessionState, | ||
_projection: Option<&Vec<usize>>, | ||
_filters: &[Expr], | ||
_limit: Option<usize>, | ||
) -> Result<Arc<dyn ExecutionPlan>> { | ||
todo!() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the primary usecase of the LogicalType so that we could write functions that take the logical type, or define custom behavior?
Like I wonder is the idea that we could now create a
ScalarFunctionImpl
whose signature refers to LogicalType rather than PhysicalType 🤔 Or somehow plan a binary operation on logical types?BTW I think the work we are doing with @samuelcolvin @dharanad and @jayzhan211 in #11207 would make it straightforward to implement a custom comparsion (via a function) for this magical type
That might be a good thing to show too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potentially both. The motivation behind this change is to simplify the interaction between standard types represented by different encodings (like RunArray, the various Views and DictionaryArray but potentially also user defined ones via Arrow Extension Types).