Repository navigation
feat(writer): add a location generator that follows table properties #3730
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
Open
smaheshwar-pltr
wants to merge
5
commits into
apache:main
Choose a base branch
from
smaheshwar-pltr:design/core-table-generator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+155
−29
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cb224a5
feat(writer): add a location generator that follows table properties
smaheshwar-pltr df3f93a
docs: clarify that DefaultLocationGenerator ignores the table layout
smaheshwar-pltr d746dce
Merge upstream main
smaheshwar-pltr a9b322c
test(writer): simplify location generator tests and docs
smaheshwar-pltr 1597f94
docs(writer): clarify the fixed default file layout
smaheshwar-pltr 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 |
|---|---|---|
|
|
@@ -54,6 +54,10 @@ const ENTROPY_DIR_DEPTH: usize = 3; | |
| #[derive(Clone, Debug)] | ||
| /// `DefaultLocationGenerator` used to generate the data dir location of data file. | ||
| /// The location is generated based on the table location and the data location in table properties. | ||
| /// | ||
| /// Paths use `{data location}/{partition path}/{file name}`, omitting the | ||
| /// partition path for unpartitioned files. This generator always uses this layout. | ||
| /// Use [`TableLocationGenerator`] to select the layout from table properties. | ||
| pub struct DefaultLocationGenerator { | ||
| data_location: String, | ||
| } | ||
|
|
@@ -203,6 +207,52 @@ impl LocationGenerator for ObjectStorageLocationGenerator { | |
| } | ||
| } | ||
|
|
||
| /// Generates data file paths using the layout configured in table properties. | ||
| /// | ||
| /// It uses [`ObjectStorageLocationGenerator`] when | ||
| /// [`write.object-storage.enabled`](crate::spec::TableProperties::PROPERTY_WRITE_OBJECT_STORAGE_ENABLED) | ||
| /// is `true`, and [`DefaultLocationGenerator`] otherwise. | ||
| #[derive(Clone, Debug)] | ||
| pub struct TableLocationGenerator { | ||
| inner: Layout, | ||
| } | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| enum Layout { | ||
| Default(DefaultLocationGenerator), | ||
| ObjectStorage(ObjectStorageLocationGenerator), | ||
| } | ||
|
|
||
| impl TableLocationGenerator { | ||
| /// Creates a generator from table metadata. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns an error if a layout-related table property has an invalid value. | ||
| pub fn new(table_metadata: &TableMetadata) -> Result<Self> { | ||
| let inner = if table_metadata | ||
| .table_properties() | ||
| .write_object_storage_enabled()? | ||
| { | ||
| Layout::ObjectStorage(ObjectStorageLocationGenerator::new(table_metadata)?) | ||
| } else { | ||
| Layout::Default(DefaultLocationGenerator::new(table_metadata)?) | ||
| }; | ||
| Ok(Self { inner }) | ||
| } | ||
| } | ||
|
|
||
| impl LocationGenerator for TableLocationGenerator { | ||
| fn generate_location(&self, partition_key: Option<&PartitionKey>, file_name: &str) -> String { | ||
| match &self.inner { | ||
| Layout::Default(generator) => generator.generate_location(partition_key, file_name), | ||
| Layout::ObjectStorage(generator) => { | ||
| generator.generate_location(partition_key, file_name) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Derive the `{parent}/{name}` context from a table location, mirroring Hadoop's | ||
| /// `Path.getParent().getName()` / `Path.getName()`. | ||
| fn path_context(table_location: &str) -> String { | ||
|
|
@@ -291,15 +341,18 @@ pub(crate) mod test { | |
| use std::collections::HashMap; | ||
| use std::sync::Arc; | ||
|
|
||
| use rstest::rstest; | ||
| use uuid::Uuid; | ||
|
|
||
| use super::LocationGenerator; | ||
| use crate::ErrorKind; | ||
| use crate::spec::{ | ||
| FormatVersion, Literal, NestedField, PartitionKey, PartitionSpec, PrimitiveType, Schema, | ||
| Struct, StructType, TableMetadata, TableProperties, Transform, Type, | ||
| }; | ||
| use crate::writer::file_writer::location_generator::{ | ||
| DefaultLocationGenerator, FileNameGenerator, ObjectStorageLocationGenerator, | ||
| TableLocationGenerator, | ||
| }; | ||
|
|
||
| #[test] | ||
|
|
@@ -609,6 +662,85 @@ pub(crate) mod test { | |
| ); | ||
| } | ||
|
|
||
| #[rstest] | ||
|
Contributor
Author
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. I'm wary that I could be over-testing here (I did somehwat like how these tests read though) |
||
| #[case::unset(&[], [ | ||
| "s3://data.db/table/data/test.parquet", | ||
| "s3://data.db/table/data/data%231=val%231/test.parquet", | ||
| ])] | ||
| #[case::disabled(&[("write.object-storage.enabled", "false")], [ | ||
| "s3://data.db/table/data/test.parquet", | ||
| "s3://data.db/table/data/data%231=val%231/test.parquet", | ||
| ])] | ||
| #[case::enabled(&[("write.object-storage.enabled", "true")], [ | ||
| "s3://data.db/table/data/0110/1010/0011/11101000/test.parquet", | ||
| "s3://data.db/table/data/0000/1011/0110/00001000/data%231=val%231/test.parquet", | ||
| ])] | ||
| #[case::uppercase(&[("write.object-storage.enabled", "TRUE")], [ | ||
| "s3://data.db/table/data/0110/1010/0011/11101000/test.parquet", | ||
| "s3://data.db/table/data/0000/1011/0110/00001000/data%231=val%231/test.parquet", | ||
| ])] | ||
| #[case::without_partition_paths(&[ | ||
| ("write.object-storage.enabled", "true"), | ||
| ("write.object-storage.partitioned-paths", "false"), | ||
| ], [ | ||
| "s3://data.db/table/data/0110/1010/0011/11101000-test.parquet", | ||
| "s3://data.db/table/data/0110/1010/0011/11101000-test.parquet", | ||
| ])] | ||
| fn table_location_follows_properties( | ||
| #[case] properties: &[(&str, &str)], | ||
| #[case] expected: [&str; 2], | ||
| ) { | ||
| let metadata = table_metadata_with( | ||
| "s3://data.db/table", | ||
| properties | ||
| .iter() | ||
| .map(|(key, value)| (key.to_string(), value.to_string())) | ||
| .collect(), | ||
| ); | ||
| let schema = Arc::new( | ||
| Schema::builder() | ||
| .with_fields(vec![ | ||
| NestedField::required(1, "data#1", Type::Primitive(PrimitiveType::String)) | ||
| .into(), | ||
| ]) | ||
| .build() | ||
| .unwrap(), | ||
| ); | ||
| let partition_spec = PartitionSpec::builder(schema.clone()) | ||
| .add_partition_field("data#1", "data#1", Transform::Identity) | ||
| .unwrap() | ||
| .build() | ||
| .unwrap(); | ||
| let partition_key = PartitionKey::new( | ||
| partition_spec, | ||
| schema, | ||
| Struct::from_iter([Some(Literal::string("val#1"))]), | ||
| ); | ||
| let generator = TableLocationGenerator::new(&metadata).unwrap(); | ||
|
|
||
| assert_eq!( | ||
| [ | ||
| generator.generate_location(None, "test.parquet"), | ||
| generator.generate_location(Some(&partition_key), "test.parquet"), | ||
| ], | ||
| expected | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn table_location_rejects_invalid_property() { | ||
| let metadata = table_metadata_with( | ||
| "s3://data.db/table", | ||
| HashMap::from([( | ||
| "write.object-storage.enabled".to_string(), | ||
| "yes".to_string(), | ||
| )]), | ||
| ); | ||
| let err = TableLocationGenerator::new(&metadata).unwrap_err(); | ||
|
|
||
| assert_eq!(err.kind(), ErrorKind::DataInvalid); | ||
| } | ||
|
|
||
| /// Build a minimal `TableMetadata` for location generator tests. | ||
| fn table_metadata_with(location: &str, properties: HashMap<String, String>) -> TableMetadata { | ||
| TableMetadata { | ||
|
|
||
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
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.
I wrote up some context in #3729 on why I've gone with this approach but open to change / discussion if folks have thoughts. A new generator here lets us keep compatibility with current client behaviour, but the downside is that
DefaultLocationGeneratorreads naturally yet may not do what's desired.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.
IAlso, Java and Python support custom location providers so that is worth considering too w.r.t. the API design we introduce here. I think the proposed design should be able to support that by e.g. adding
Custom(Arc<dyn <ErasedGenerator>)to theLayoutenum)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.
Yes, I think we should consider custom location provider here. enum seems like an acceptable workaround to me, but I don't want to block the custom location provider support for datafusion. Would appreciate more clarity here!
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.
(You're right, I'm having a think here on the best shape)