-
Notifications
You must be signed in to change notification settings - Fork 3k
Support timestamp in partition path #3933
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.sql.Timestamp; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
|
|
@@ -33,8 +34,11 @@ | |
| import org.apache.avro.io.DatumWriter; | ||
| import org.apache.iceberg.AssertHelpers; | ||
| import org.apache.iceberg.DataFile; | ||
| import org.apache.iceberg.expressions.Literal; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Lists; | ||
| import org.apache.iceberg.types.Types; | ||
| import org.apache.iceberg.util.DateTimeUtil; | ||
| import org.apache.spark.sql.Dataset; | ||
| import org.apache.spark.sql.Row; | ||
| import org.apache.spark.sql.RowFactory; | ||
|
|
@@ -381,6 +385,25 @@ public void addDataPartitionedByDateToPartitioned() { | |
| sql("SELECT id, name, dept, date FROM %s ORDER BY id", tableName)); | ||
| } | ||
|
|
||
| @Test | ||
| public void addDataPartitionedByTimestampToPartitioned() { | ||
| createTsPartitionedFileTable("parquet"); | ||
|
|
||
| String createIceberg = | ||
| "CREATE TABLE %s (id Integer, name String, dept String, ts Timestamp) USING iceberg PARTITIONED BY (ts)"; | ||
|
|
||
| sql(createIceberg, tableName); | ||
|
|
||
| Object result = scalarSql("CALL %s.system.add_files('%s', '`parquet`.`%s`')", | ||
| catalogName, tableName, fileTableDir.getAbsolutePath()); | ||
|
|
||
| Assert.assertEquals(4L, result); | ||
|
|
||
| assertEquals("Iceberg table contains correct data", | ||
| sql("SELECT id, name, dept, ts FROM %s ORDER BY id", sourceTableName), | ||
| sql("SELECT id, name, dept, ts FROM %s ORDER BY id", tableName)); | ||
| } | ||
|
|
||
| @Test | ||
| public void addFilteredPartitionsToPartitioned() { | ||
| createCompositePartitionedTable("parquet"); | ||
|
|
@@ -819,6 +842,27 @@ private static java.sql.Date toDate(String value) { | |
| RowFactory.create(4, "Will Doe", "facilities", toDate("2021-01-02"))), | ||
| new StructType(dateStruct)).repartition(2); | ||
|
|
||
| private static final StructField[] timeStampstruct = { | ||
| new StructField("id", DataTypes.IntegerType, true, Metadata.empty()), | ||
| new StructField("name", DataTypes.StringType, true, Metadata.empty()), | ||
| new StructField("dept", DataTypes.StringType, true, Metadata.empty()), | ||
| new StructField("ts", DataTypes.TimestampType, true, Metadata.empty()) | ||
| }; | ||
|
|
||
| private static Timestamp toTimestamp(String value) { | ||
|
Contributor
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. Okay, I see. I was wrong before. This is actually producing a Timestamp because that's what Spark's public row API uses. Instead, could you use SparkSQL and literal values to avoid doing this conversion manually? I think that will make for a much more reliable test. |
||
| Literal<Long> timestamp = Literal.of(value).to(Types.TimestampType.withoutZone()); | ||
| return Timestamp.valueOf(DateTimeUtil.timestampFromMicros(timestamp.value())); | ||
| } | ||
|
|
||
| private static final Dataset<Row> timestampDF = | ||
| spark.createDataFrame( | ||
| ImmutableList.of( | ||
| RowFactory.create(1, "John Doe", "hr", toTimestamp("2021-01-01T00:00:00.999999999")), | ||
| RowFactory.create(2, "Jane Doe", "hr", toTimestamp("2021-01-01T00:00:00.999999999")), | ||
| RowFactory.create(3, "Matt Doe", "hr", toTimestamp("2021-01-02T00:00:00.999999999")), | ||
| RowFactory.create(4, "Will Doe", "facilities", toTimestamp("2021-01-02T00:00:00.999999999"))), | ||
| new StructType(timeStampstruct)).repartition(2); | ||
|
|
||
| private void createUnpartitionedFileTable(String format) { | ||
| String createParquet = | ||
| "CREATE TABLE %s (id Integer, name String, dept String, subdept String) USING %s LOCATION '%s'"; | ||
|
|
@@ -900,4 +944,13 @@ private void createDatePartitionedFileTable(String format) { | |
|
|
||
| dateDF.write().insertInto(sourceTableName); | ||
| } | ||
|
|
||
| private void createTsPartitionedFileTable(String format) { | ||
| String createParquet = | ||
| "CREATE TABLE %s (id Integer, name String, dept String, ts timestamp) USING %s PARTITIONED BY (ts) " + | ||
| "LOCATION '%s'"; | ||
|
|
||
| sql(createParquet, sourceTableName, format, fileTableDir.getAbsolutePath()); | ||
| timestampDF.write().insertInto(sourceTableName); | ||
| } | ||
| } | ||
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 it possible to do this conversion without using
java.sql.Timestamp?