-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
<!-- Thanks for sending a pull request! Here are some tips for you: 1. If this is your first time, please read our contributor guidelines: https://github.com/delta-io/delta/blob/master/CONTRIBUTING.md 2. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP] Your PR title ...'. 3. Be sure to keep the PR description updated to reflect all changes. 4. Please write your PR title to summarize what this PR proposes. 5. If possible, provide a concise example to reproduce the issue for a faster review. 6. If applicable, include the corresponding issue number in the PR title and link it in the body. --> #### Which Delta project/connector is this regarding? <!-- Please add the component selected below to the beginning of the pull request title For example: [Spark] Title of my pull request --> - [ ] Spark - [ ] Standalone - [x] Flink - [ ] Kernel - [ ] Other (fill in here) ## Description <!-- - Describe what this PR changes. - Describe why we need the change. If this PR resolves an issue be sure to include "Resolves #XXX" to correctly link and close the issue upon merge. --> Adds support for table partition of DATE type for RowData elements. Resolves #2331 ## How was this patch tested? <!-- If tests were added, say they were added here. Please make sure to test the changes thoroughly including negative and positive cases if possible. If the changes were tested in any way other than unit tests, please clarify how you tested step by step (ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future). If the changes were not tested, please explain why. --> Tests were updated to include the new type. ## Does this PR introduce _any_ user-facing changes? <!-- If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible. If possible, please also clarify if this is a user-facing change compared to the released Delta Lake versions or within the unreleased branches such as master. If no, write 'No'. --> No --------- Signed-off-by: Tulio Cavalcanti <[email protected]>
- Loading branch information
1 parent
81afbe3
commit 6c22aca
Showing
3 changed files
with
116 additions
and
4 deletions.
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
103 changes: 103 additions & 0 deletions
103
connectors/flink/src/test/java/io/delta/flink/sink/DeltaSinkITCase.java
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,103 @@ | ||
package io.delta.flink.sink; | ||
|
||
import java.nio.file.Path; | ||
import java.time.LocalDate; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
import java.util.stream.Stream; | ||
import java.util.stream.StreamSupport; | ||
|
||
import io.delta.flink.utils.DeltaTestUtils; | ||
import org.apache.flink.streaming.api.datastream.DataStream; | ||
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; | ||
import org.apache.flink.table.data.RowData; | ||
import org.apache.flink.table.data.util.DataFormatConverters; | ||
import org.apache.flink.table.types.logical.DateType; | ||
import org.apache.flink.table.types.logical.IntType; | ||
import org.apache.flink.table.types.logical.RowType; | ||
import org.apache.flink.table.types.utils.TypeConversions; | ||
import org.apache.flink.types.Row; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
import io.delta.standalone.DeltaLog; | ||
import io.delta.standalone.data.CloseableIterator; | ||
import io.delta.standalone.data.RowRecord; | ||
|
||
public class DeltaSinkITCase { | ||
|
||
@Test | ||
public void testWritePartitionedByDate(@TempDir Path tempDir) throws Exception { | ||
final String deltaTablePath = tempDir.toString(); | ||
|
||
final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); | ||
|
||
final RowType rowType = new RowType(Arrays.asList( | ||
new RowType.RowField("part1", new DateType()), | ||
new RowType.RowField("data1", new IntType()) | ||
)); | ||
|
||
final DataFormatConverters.DataFormatConverter<RowData, Row> typeConverter = | ||
DataFormatConverters.getConverterForDataType( | ||
TypeConversions.fromLogicalToDataType(rowType) | ||
); | ||
|
||
final DeltaSink<RowData> deltaSink = DeltaSink | ||
.forRowData( | ||
new org.apache.flink.core.fs.Path(deltaTablePath), | ||
DeltaTestUtils.getHadoopConf(), | ||
rowType | ||
) | ||
.withPartitionColumns("part1") | ||
.build(); | ||
|
||
LocalDate startDate = LocalDate.of(2024, 8, 1); | ||
int numOfDays = 30; | ||
List<LocalDate> inputDates = IntStream.range(1, numOfDays) | ||
.mapToObj(startDate::plusDays) | ||
.collect(Collectors.toList()); | ||
|
||
RowData[] elements = inputDates.stream() | ||
.map(date -> Row.of(date, 0)) | ||
.map(typeConverter::toInternal) | ||
.toArray(RowData[]::new); | ||
|
||
final DataStream<RowData> inputStream = env.fromElements(elements); | ||
|
||
inputStream.sinkTo(deltaSink); | ||
|
||
env.execute("Delta Sink Example"); | ||
|
||
DeltaLog deltaLog = | ||
DeltaLog.forTable(DeltaTestUtils.getHadoopConf(), deltaTablePath); | ||
|
||
try (CloseableIterator<RowRecord> rowRecordIterator = deltaLog.snapshot().open()) { | ||
|
||
Iterable<RowRecord> iterable = () -> rowRecordIterator; | ||
Stream<RowRecord> rowRecordStream = StreamSupport.stream(iterable.spliterator(), false); | ||
|
||
List<LocalDate> resultDates = rowRecordStream | ||
.map(rowRecord -> rowRecord.getDate("part1").toLocalDate()) | ||
.collect(Collectors.toList()); | ||
|
||
org.assertj.core.api.Assertions.assertThat(resultDates) | ||
.withFailMessage("Delta table failed to store date partitions.") | ||
.containsAll(inputDates); | ||
|
||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
List<String> deltaPartitionColumns = deltaLog | ||
.snapshot() | ||
.getMetadata() | ||
.getPartitionColumns(); | ||
|
||
org.assertj.core.api.Assertions.assertThat(deltaPartitionColumns) | ||
.withFailMessage("Delta table failed to store date partitions to the delta log.") | ||
.containsAll(Collections.singletonList("part1")); | ||
} | ||
} |
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