Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package org.apache.xtable.paimon;

import static org.apache.xtable.delta.DeltaValueConverter.convertFromDeltaPartitionValue;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -36,6 +38,7 @@
import org.apache.xtable.model.schema.InternalField;
import org.apache.xtable.model.schema.InternalPartitionField;
import org.apache.xtable.model.schema.InternalSchema;
import org.apache.xtable.model.schema.InternalType;
import org.apache.xtable.model.schema.PartitionTransformType;
import org.apache.xtable.model.stat.PartitionValue;
import org.apache.xtable.model.stat.Range;
Expand Down Expand Up @@ -69,18 +72,39 @@ public List<PartitionValue> toPartitionValues(
return Collections.emptyList();
}

String defaultPartitionName = table.coreOptions().partitionDefaultName();
List<PartitionValue> partitionValues = new ArrayList<>(partValues.size());
for (Map.Entry<String, String> entry : partValues.entrySet()) {
PartitionValue partitionValue =
InternalPartitionField partitionField = toPartitionField(entry.getKey(), internalSchema);
Object typedValue =
convertPartitionValue(
entry.getValue(),
partitionField.getSourceField().getSchema().getDataType(),
defaultPartitionName);
partitionValues.add(
PartitionValue.builder()
.partitionField(toPartitionField(entry.getKey(), internalSchema))
.range(Range.scalar(entry.getValue()))
.build();
partitionValues.add(partitionValue);
.partitionField(partitionField)
.range(Range.scalar(typedValue))
.build());
}
return partitionValues;
}

/**
* Converts a Paimon partition path value (always rendered as a String by the partition computer)
* into the typed value the Delta and Iceberg targets expect (for example an epoch-day Integer for
* DATE, an Integer for INT, a Long for BIGINT), reusing the same conversion the Delta sources
* use. A null partition value is rendered by Paimon as the configured default-partition name, so
* that sentinel is mapped back to {@code null}.
*/
static Object convertPartitionValue(
String rawValue, InternalType fieldType, String defaultPartitionName) {
if (rawValue == null || rawValue.equals(defaultPartitionName)) {
return null;
}
return convertFromDeltaPartitionValue(rawValue, fieldType, PartitionTransformType.VALUE, null);
}

public Optional<String> toPartitionPath(FileStoreTable table, BinaryRow partition) {
InternalRowPartitionComputer partitionComputer = newPartitionComputer(table);
return partitionComputer.generatePartValues(partition).entrySet().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.nio.file.Path;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -123,6 +125,36 @@ void testToPartitionValuesWithPartitionedTable() {
assertEquals(Range.scalar("INFO"), partitionValue.getRange());
}

@Test
void testConvertPartitionValueProducesTypedValues() {
String defaultName = "__DEFAULT_PARTITION__";

assertEquals(
Integer.valueOf((int) LocalDate.parse("2019-10-12").toEpochDay()),
PaimonPartitionExtractor.convertPartitionValue(
"2019-10-12", InternalType.DATE, defaultName));
assertEquals(
Integer.valueOf(42),
PaimonPartitionExtractor.convertPartitionValue("42", InternalType.INT, defaultName));
assertEquals(
Long.valueOf(10_000_000_000L),
PaimonPartitionExtractor.convertPartitionValue(
"10000000000", InternalType.LONG, defaultName));
assertEquals(
"USA",
PaimonPartitionExtractor.convertPartitionValue("USA", InternalType.STRING, defaultName));
}

@Test
void testConvertPartitionValueMapsNullAndDefaultPartitionToNull() {
String defaultName = "__DEFAULT_PARTITION__";

assertNull(
PaimonPartitionExtractor.convertPartitionValue(
defaultName, InternalType.DATE, defaultName));
assertNull(PaimonPartitionExtractor.convertPartitionValue(null, InternalType.INT, defaultName));
}

@Test
@Disabled("TODO: make it easier to create multi-partitioned table in tests")
void testToPartitionPathWithMultiplePartitionValues() {
Expand Down