Skip to content

Commit

Permalink
[FLINK] [#2331] Add support to partition table by date type (#3533)
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
tuliocavalcanti authored Sep 10, 2024
1 parent 81afbe3 commit 6c22aca
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import org.apache.flink.streaming.api.functions.sink.filesystem.BucketAssigner;
import org.apache.flink.table.data.RowData;
import org.apache.flink.table.data.conversion.DateDateConverter;
import org.apache.flink.table.types.logical.LogicalType;
import org.apache.flink.table.types.logical.LogicalTypeRoot;
import org.apache.flink.table.types.logical.RowType;
Expand Down Expand Up @@ -99,6 +100,10 @@ public LinkedHashMap<String, String> generatePartitionValues(
partitionValues.put(partitionKey, String.valueOf(element.getShort(keyIndex)));
} else if (keyType.getTypeRoot() == LogicalTypeRoot.TINYINT) {
partitionValues.put(partitionKey, String.valueOf(element.getByte(keyIndex)));
} else if (keyType.getTypeRoot() == LogicalTypeRoot.DATE) {
DateDateConverter converter = new DateDateConverter();
String value = String.valueOf(converter.toExternal(element.getInt(keyIndex)));
partitionValues.put(partitionKey, value);
} else {
throw new RuntimeException("Type not supported " + keyType.getTypeRoot());
}
Expand Down
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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package io.delta.flink.sink.internal;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.LinkedHashMap;
import javax.annotation.Nullable;
Expand All @@ -27,6 +28,7 @@
import org.apache.flink.table.data.RowData;
import org.apache.flink.table.data.util.DataFormatConverters;
import org.apache.flink.table.types.logical.BigIntType;
import org.apache.flink.table.types.logical.DateType;
import org.apache.flink.table.types.logical.DoubleType;
import org.apache.flink.table.types.logical.IntType;
import org.apache.flink.table.types.logical.RowType;
Expand Down Expand Up @@ -97,22 +99,23 @@ public void testRowDataPartitionComputer() {
new RowType.RowField("partition_col3", new BigIntType()),
new RowType.RowField("partition_col4", new SmallIntType()),
new RowType.RowField("partition_col5", new TinyIntType()),
new RowType.RowField("col5", new VarCharType()),
new RowType.RowField("col6", new IntType())
new RowType.RowField("partition_col6", new DateType()),
new RowType.RowField("col7", new VarCharType()),
new RowType.RowField("col8", new IntType())
));
DataFormatConverters.DataFormatConverter<RowData, Row> converter =
DataFormatConverters.getConverterForDataType(
TypeConversions.fromLogicalToDataType(testRowType)
);
String[] partitionCols = {"partition_col1", "partition_col2", "partition_col3",
"partition_col4", "partition_col5"};
"partition_col4", "partition_col5", "partition_col6"};

DeltaPartitionComputer<RowData> partitionComputer =
new DeltaPartitionComputer.DeltaRowDataPartitionComputer(testRowType, partitionCols);

RowData record = converter.toInternal(
Row.of("1", Integer.MAX_VALUE, Long.MAX_VALUE, Short.MAX_VALUE, Byte.MAX_VALUE,
"some_val", 2));
LocalDate.of(9999,12,31), "some_val", 2));

// WHEN
LinkedHashMap<String, String> partitionValues =
Expand All @@ -125,6 +128,7 @@ public void testRowDataPartitionComputer() {
put("partition_col3", String.valueOf(Long.MAX_VALUE));
put("partition_col4", String.valueOf(Short.MAX_VALUE));
put("partition_col5", String.valueOf(Byte.MAX_VALUE));
put("partition_col6", "9999-12-31");
}};

assertEquals(expected, partitionValues);
Expand Down

0 comments on commit 6c22aca

Please sign in to comment.