-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Unify Metadata Handing: use FieldMetadata
in Expr::Alias
and ExprSchemable
#16320
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -517,6 +517,9 @@ impl FieldMetadata { | |
|
||
/// Adds metadata from `other` into `self`, overwriting any existing keys. | ||
pub fn extend(&mut self, other: Self) { | ||
if other.is_empty() { | ||
return; | ||
} | ||
let other = Arc::unwrap_or_clone(other.into_inner()); | ||
Arc::make_mut(&mut self.inner).extend(other); | ||
} | ||
|
@@ -531,18 +534,21 @@ impl FieldMetadata { | |
self.inner.len() | ||
} | ||
|
||
/// Convert this `FieldMetadata` into a `HashMap<String, String>` | ||
pub fn to_hashmap(&self) -> std::collections::HashMap<String, String> { | ||
self.inner | ||
.iter() | ||
.map(|(k, v)| (k.to_string(), v.to_string())) | ||
.collect() | ||
} | ||
|
||
/// Updates the metadata on the Field with this metadata, if it is not empty. | ||
pub fn add_to_field(&self, field: Field) -> Field { | ||
if self.inner.is_empty() { | ||
return field; | ||
} | ||
|
||
field.with_metadata( | ||
self.inner | ||
.iter() | ||
.map(|(k, v)| (k.clone(), v.clone())) | ||
.collect(), | ||
) | ||
field.with_metadata(self.to_hashmap()) | ||
} | ||
} | ||
|
||
|
@@ -575,6 +581,24 @@ impl From<&std::collections::HashMap<String, String>> for FieldMetadata { | |
} | ||
} | ||
|
||
/// From hashbrown map | ||
impl From<HashMap<String, String>> for FieldMetadata { | ||
fn from(map: HashMap<String, String>) -> Self { | ||
let inner = map.into_iter().collect(); | ||
Self::new(inner) | ||
} | ||
} | ||
|
||
impl From<&HashMap<String, String>> for FieldMetadata { | ||
fn from(map: &HashMap<String, String>) -> Self { | ||
let inner = map | ||
.into_iter() | ||
.map(|(k, v)| (k.to_string(), v.to_string())) | ||
.collect(); | ||
Self::new(inner) | ||
} | ||
} | ||
|
||
/// UNNEST expression. | ||
#[derive(Clone, PartialEq, Eq, PartialOrd, Hash, Debug)] | ||
pub struct Unnest { | ||
|
@@ -601,7 +625,7 @@ pub struct Alias { | |
pub expr: Box<Expr>, | ||
pub relation: Option<TableReference>, | ||
pub name: String, | ||
pub metadata: Option<std::collections::HashMap<String, String>>, | ||
pub metadata: Option<FieldMetadata>, | ||
} | ||
|
||
impl Hash for Alias { | ||
|
@@ -641,10 +665,7 @@ impl Alias { | |
} | ||
} | ||
|
||
pub fn with_metadata( | ||
mut self, | ||
metadata: Option<std::collections::HashMap<String, String>>, | ||
) -> Self { | ||
pub fn with_metadata(mut self, metadata: Option<FieldMetadata>) -> Self { | ||
self.metadata = metadata; | ||
self | ||
} | ||
|
@@ -1591,15 +1612,17 @@ impl Expr { | |
/// # Example | ||
/// ``` | ||
/// # use datafusion_expr::col; | ||
/// use std::collections::HashMap; | ||
/// # use std::collections::HashMap; | ||
/// # use datafusion_expr::expr::FieldMetadata; | ||
/// let metadata = HashMap::from([("key".to_string(), "value".to_string())]); | ||
/// let metadata = FieldMetadata::from(metadata); | ||
/// let expr = col("foo").alias_with_metadata("bar", Some(metadata)); | ||
/// ``` | ||
/// | ||
pub fn alias_with_metadata( | ||
self, | ||
name: impl Into<String>, | ||
metadata: Option<std::collections::HashMap<String, String>>, | ||
metadata: Option<FieldMetadata>, | ||
) -> Expr { | ||
Expr::Alias(Alias::new(self, None::<&str>, name.into()).with_metadata(metadata)) | ||
} | ||
|
@@ -1621,16 +1644,18 @@ impl Expr { | |
/// # Example | ||
/// ``` | ||
/// # use datafusion_expr::col; | ||
/// use std::collections::HashMap; | ||
/// # use std::collections::HashMap; | ||
/// # use datafusion_expr::expr::FieldMetadata; | ||
/// let metadata = HashMap::from([("key".to_string(), "value".to_string())]); | ||
/// let metadata = FieldMetadata::from(metadata); | ||
/// let expr = col("foo").alias_qualified_with_metadata(Some("tbl"), "bar", Some(metadata)); | ||
/// ``` | ||
/// | ||
pub fn alias_qualified_with_metadata( | ||
self, | ||
relation: Option<impl Into<TableReference>>, | ||
name: impl Into<String>, | ||
metadata: Option<std::collections::HashMap<String, String>>, | ||
metadata: Option<FieldMetadata>, | ||
) -> Expr { | ||
Expr::Alias(Alias::new(self, relation, name.into()).with_metadata(metadata)) | ||
} | ||
|
@@ -3819,7 +3844,7 @@ mod test { | |
// If this test fails when you change `Expr`, please try | ||
// `Box`ing the fields to make `Expr` smaller | ||
// See https://github.com/apache/datafusion/issues/16199 for details | ||
assert_eq!(size_of::<Expr>(), 144); | ||
assert_eq!(size_of::<Expr>(), 128); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And |
||
assert_eq!(size_of::<ScalarValue>(), 64); | ||
assert_eq!(size_of::<DataType>(), 24); // 3 ptrs | ||
assert_eq!(size_of::<Vec<Expr>>(), 24); | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
This line is the point of the PR is to use the
FieldMetadata
struct introduced in this PR everywhere:FieldMetadata
structure #16317The rest of the changes in this PR are consequences of this change