You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@paimon.apache.org by lz...@apache.org on 2024/03/14 03:49:01 UTC
(incubator-paimon) branch master updated: [core] Fix merge index out of range for sequence field (#3007)
This is an automated email from the ASF dual-hosted git repository.
lzljs3620320 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-paimon.git
The following commit(s) were added to refs/heads/master by this push:
new 3793f2eca [core] Fix merge index out of range for sequence field (#3007)
3793f2eca is described below
commit 3793f2ecaef69f380680f5a06ea6084227d4aaf8
Author: Jingsong Lee <ji...@gmail.com>
AuthorDate: Thu Mar 14 11:48:58 2024 +0800
[core] Fix merge index out of range for sequence field (#3007)
---
.../main/java/org/apache/paimon/CoreOptions.java | 6 ++-
.../java/org/apache/paimon/KeyValueFileStore.java | 12 +----
.../paimon/operation/KeyValueFileStoreRead.java | 59 ++++++++++++++++++----
.../org/apache/paimon/schema/SchemaValidation.java | 34 ++++++-------
.../apache/paimon/table/query/LocalTableQuery.java | 4 +-
.../paimon/utils/UserDefinedSeqComparator.java | 13 +----
.../paimon/table/PrimaryKeyFileStoreTableTest.java | 23 +++++++--
.../paimon/flink/lookup/FullCacheLookupTable.java | 2 +-
.../flink/source/TestChangelogDataReadWrite.java | 5 +-
9 files changed, 99 insertions(+), 59 deletions(-)
diff --git a/paimon-common/src/main/java/org/apache/paimon/CoreOptions.java b/paimon-common/src/main/java/org/apache/paimon/CoreOptions.java
index faea53997..b5b7ea432 100644
--- a/paimon-common/src/main/java/org/apache/paimon/CoreOptions.java
+++ b/paimon-common/src/main/java/org/apache/paimon/CoreOptions.java
@@ -1492,8 +1492,10 @@ public class CoreOptions implements Serializable {
return options.get(DYNAMIC_BUCKET_ASSIGNER_PARALLELISM);
}
- public Optional<List<String>> sequenceField() {
- return options.getOptional(SEQUENCE_FIELD).map(s -> Arrays.asList(s.split(",")));
+ public List<String> sequenceField() {
+ return options.getOptional(SEQUENCE_FIELD)
+ .map(s -> Arrays.asList(s.split(",")))
+ .orElse(Collections.emptyList());
}
public Optional<String> rowkindField() {
diff --git a/paimon-core/src/main/java/org/apache/paimon/KeyValueFileStore.java b/paimon-core/src/main/java/org/apache/paimon/KeyValueFileStore.java
index 3f4023b9d..e9b4cc5fd 100644
--- a/paimon-core/src/main/java/org/apache/paimon/KeyValueFileStore.java
+++ b/paimon-core/src/main/java/org/apache/paimon/KeyValueFileStore.java
@@ -38,14 +38,11 @@ import org.apache.paimon.schema.SchemaManager;
import org.apache.paimon.table.BucketMode;
import org.apache.paimon.table.CatalogEnvironment;
import org.apache.paimon.types.RowType;
-import org.apache.paimon.utils.FieldsComparator;
import org.apache.paimon.utils.FileStorePathFactory;
import org.apache.paimon.utils.KeyComparatorSupplier;
import org.apache.paimon.utils.UserDefinedSeqComparator;
import org.apache.paimon.utils.ValueEqualiserSupplier;
-import javax.annotation.Nullable;
-
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
@@ -123,12 +120,12 @@ public class KeyValueFileStore extends AbstractFileStore<KeyValue> {
@Override
public KeyValueFileStoreRead newRead() {
return new KeyValueFileStoreRead(
+ options,
schemaManager,
schemaId,
keyType,
valueType,
newKeyComparator(),
- userDefinedSeqComparator(),
mfFactory,
newReaderFactoryBuilder());
}
@@ -146,11 +143,6 @@ public class KeyValueFileStore extends AbstractFileStore<KeyValue> {
options);
}
- @Nullable
- private FieldsComparator userDefinedSeqComparator() {
- return UserDefinedSeqComparator.create(valueType, options);
- }
-
@Override
public KeyValueFileStoreWrite newWrite(String commitUser) {
return newWrite(commitUser, null);
@@ -175,7 +167,7 @@ public class KeyValueFileStore extends AbstractFileStore<KeyValue> {
keyType,
valueType,
keyComparatorSupplier,
- this::userDefinedSeqComparator,
+ () -> UserDefinedSeqComparator.create(valueType, options),
valueEqualiserSupplier,
mfFactory,
pathFactory(),
diff --git a/paimon-core/src/main/java/org/apache/paimon/operation/KeyValueFileStoreRead.java b/paimon-core/src/main/java/org/apache/paimon/operation/KeyValueFileStoreRead.java
index 179522610..db4c0ac92 100644
--- a/paimon-core/src/main/java/org/apache/paimon/operation/KeyValueFileStoreRead.java
+++ b/paimon-core/src/main/java/org/apache/paimon/operation/KeyValueFileStoreRead.java
@@ -46,13 +46,15 @@ import org.apache.paimon.schema.TableSchema;
import org.apache.paimon.table.source.DataSplit;
import org.apache.paimon.table.source.DeletionFile;
import org.apache.paimon.types.RowType;
-import org.apache.paimon.utils.FieldsComparator;
import org.apache.paimon.utils.ProjectedRow;
+import org.apache.paimon.utils.Projection;
+import org.apache.paimon.utils.UserDefinedSeqComparator;
import javax.annotation.Nullable;
import java.io.IOException;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
@@ -72,7 +74,7 @@ public class KeyValueFileStoreRead implements FileStoreRead<KeyValue> {
private final Comparator<InternalRow> keyComparator;
private final MergeFunctionFactory<KeyValue> mfFactory;
private final MergeSorter mergeSorter;
- @Nullable private final FieldsComparator userDefinedSeqComparator;
+ private final List<String> sequenceFields;
@Nullable private int[][] keyProjectedFields;
@@ -86,12 +88,12 @@ public class KeyValueFileStoreRead implements FileStoreRead<KeyValue> {
private boolean forceKeepDelete = false;
public KeyValueFileStoreRead(
+ CoreOptions options,
SchemaManager schemaManager,
long schemaId,
RowType keyType,
RowType valueType,
Comparator<InternalRow> keyComparator,
- @Nullable FieldsComparator userDefinedSeqComparator,
MergeFunctionFactory<KeyValue> mfFactory,
KeyValueFileReaderFactory.Builder readerFactoryBuilder) {
this.tableSchema = schemaManager.schema(schemaId);
@@ -99,26 +101,59 @@ public class KeyValueFileStoreRead implements FileStoreRead<KeyValue> {
this.fileIO = readerFactoryBuilder.fileIO();
this.keyComparator = keyComparator;
this.mfFactory = mfFactory;
- this.userDefinedSeqComparator = userDefinedSeqComparator;
this.mergeSorter =
new MergeSorter(
CoreOptions.fromMap(tableSchema.options()), keyType, valueType, null);
+ this.sequenceFields = options.sequenceField();
}
- public KeyValueFileStoreRead withKeyProjection(int[][] projectedFields) {
+ public KeyValueFileStoreRead withKeyProjection(@Nullable int[][] projectedFields) {
readerFactoryBuilder.withKeyProjection(projectedFields);
this.keyProjectedFields = projectedFields;
return this;
}
- public KeyValueFileStoreRead withValueProjection(int[][] projectedFields) {
- AdjustedProjection projection = mfFactory.adjustProjection(projectedFields);
+ public KeyValueFileStoreRead withValueProjection(@Nullable int[][] projectedFields) {
+ if (projectedFields == null) {
+ return this;
+ }
+
+ int[][] newProjectedFields = projectedFields;
+ if (sequenceFields.size() > 0) {
+ // make sure projection contains sequence fields
+ List<String> fieldNames = tableSchema.fieldNames();
+ List<String> projectedNames = Projection.of(projectedFields).project(fieldNames);
+ int[] lackFields =
+ sequenceFields.stream()
+ .filter(f -> !projectedNames.contains(f))
+ .mapToInt(fieldNames::indexOf)
+ .toArray();
+ if (lackFields.length > 0) {
+ newProjectedFields =
+ Arrays.copyOf(projectedFields, projectedFields.length + lackFields.length);
+ for (int i = 0; i < lackFields.length; i++) {
+ newProjectedFields[projectedFields.length + i] = new int[] {lackFields[i]};
+ }
+ }
+ }
+
+ AdjustedProjection projection = mfFactory.adjustProjection(newProjectedFields);
this.pushdownProjection = projection.pushdownProjection;
this.outerProjection = projection.outerProjection;
if (pushdownProjection != null) {
readerFactoryBuilder.withValueProjection(pushdownProjection);
mergeSorter.setProjectedValueType(readerFactoryBuilder.projectedValueType());
}
+
+ if (newProjectedFields != projectedFields) {
+ // Discard the completed sequence fields
+ if (outerProjection == null) {
+ outerProjection = Projection.range(0, projectedFields.length).toNestedIndexes();
+ } else {
+ outerProjection = Arrays.copyOf(outerProjection, projectedFields.length);
+ }
+ }
+
return this;
}
@@ -231,7 +266,7 @@ public class KeyValueFileStoreRead implements FileStoreRead<KeyValue> {
split.deletionFiles().orElse(null),
false),
keyComparator,
- userDefinedSeqComparator,
+ createUdsComparator(),
mergeSorter,
forceKeepDelete));
}
@@ -264,7 +299,7 @@ public class KeyValueFileStoreRead implements FileStoreRead<KeyValue> {
? overlappedSectionFactory
: nonOverlappedSectionFactory,
keyComparator,
- userDefinedSeqComparator,
+ createUdsComparator(),
mergeFuncWrapper,
mergeSorter));
}
@@ -322,4 +357,10 @@ public class KeyValueFileStoreRead implements FileStoreRead<KeyValue> {
ProjectedRow projectedRow = ProjectedRow.from(keyProjectedFields);
return reader.transform(kv -> kv.replaceKey(projectedRow.replaceRow(kv.key())));
}
+
+ @Nullable
+ private UserDefinedSeqComparator createUdsComparator() {
+ return UserDefinedSeqComparator.create(
+ readerFactoryBuilder.projectedValueType(), sequenceFields);
+ }
}
diff --git a/paimon-core/src/main/java/org/apache/paimon/schema/SchemaValidation.java b/paimon-core/src/main/java/org/apache/paimon/schema/SchemaValidation.java
index 68f009c41..5cd00d8c6 100644
--- a/paimon-core/src/main/java/org/apache/paimon/schema/SchemaValidation.java
+++ b/paimon-core/src/main/java/org/apache/paimon/schema/SchemaValidation.java
@@ -167,13 +167,13 @@ public class SchemaValidation {
}
}
- Optional<List<String>> sequenceField = options.sequenceField();
- sequenceField.ifPresent(
- fields ->
- checkArgument(
- schema.fieldNames().containsAll(fields),
- "Nonexistent sequence fields: '%s'",
- fields));
+ List<String> sequenceField = options.sequenceField();
+ if (sequenceField.size() > 0) {
+ checkArgument(
+ schema.fieldNames().containsAll(sequenceField),
+ "Nonexistent sequence fields: '%s'",
+ sequenceField);
+ }
Optional<String> rowkindField = options.rowkindField();
rowkindField.ifPresent(
@@ -183,18 +183,18 @@ public class SchemaValidation {
"Nonexistent rowkind field: '%s'",
field));
- sequenceField.ifPresent(
- fields ->
- fields.forEach(
- field ->
- checkArgument(
- options.fieldAggFunc(field) == null,
- "Should not define aggregation on sequence field: '%s'",
- field)));
+ if (sequenceField.size() > 0) {
+ sequenceField.forEach(
+ field ->
+ checkArgument(
+ options.fieldAggFunc(field) == null,
+ "Should not define aggregation on sequence field: '%s'",
+ field));
+ }
CoreOptions.MergeEngine mergeEngine = options.mergeEngine();
if (mergeEngine == CoreOptions.MergeEngine.FIRST_ROW) {
- if (sequenceField.isPresent()) {
+ if (sequenceField.size() > 0) {
throw new IllegalArgumentException(
"Do not support use sequence field on FIRST_MERGE merge engine");
}
@@ -214,7 +214,7 @@ public class SchemaValidation {
schema.primaryKeys(), schema.partitionKeys()));
}
- if (sequenceField.isPresent()) {
+ if (sequenceField.size() > 0) {
throw new IllegalArgumentException(
String.format(
"You can not use sequence.field in cross partition update case "
diff --git a/paimon-core/src/main/java/org/apache/paimon/table/query/LocalTableQuery.java b/paimon-core/src/main/java/org/apache/paimon/table/query/LocalTableQuery.java
index 99ad909ff..8489ac509 100644
--- a/paimon-core/src/main/java/org/apache/paimon/table/query/LocalTableQuery.java
+++ b/paimon-core/src/main/java/org/apache/paimon/table/query/LocalTableQuery.java
@@ -90,10 +90,10 @@ public class LocalTableQuery implements TableQuery {
if (options.needLookup()) {
startLevel = 1;
} else {
- if (options.sequenceField().isPresent()) {
+ if (options.sequenceField().size() > 0) {
throw new UnsupportedOperationException(
"Not support sequence field definition, but is: "
- + options.sequenceField().get());
+ + options.sequenceField());
}
if (options.mergeEngine() != DEDUPLICATE) {
diff --git a/paimon-core/src/main/java/org/apache/paimon/utils/UserDefinedSeqComparator.java b/paimon-core/src/main/java/org/apache/paimon/utils/UserDefinedSeqComparator.java
index b1ea75ac1..bab0ba377 100644
--- a/paimon-core/src/main/java/org/apache/paimon/utils/UserDefinedSeqComparator.java
+++ b/paimon-core/src/main/java/org/apache/paimon/utils/UserDefinedSeqComparator.java
@@ -27,7 +27,6 @@ import org.apache.paimon.types.RowType;
import javax.annotation.Nullable;
import java.util.List;
-import java.util.Optional;
/** A {@link FieldsComparator} for user defined sequence fields. */
public class UserDefinedSeqComparator implements FieldsComparator {
@@ -52,17 +51,7 @@ public class UserDefinedSeqComparator implements FieldsComparator {
@Nullable
public static UserDefinedSeqComparator create(RowType rowType, CoreOptions options) {
- Optional<List<String>> sequenceField = options.sequenceField();
- if (!sequenceField.isPresent()) {
- return null;
- }
-
- List<String> fieldNames = rowType.getFieldNames();
- int[] fields = sequenceField.get().stream().mapToInt(fieldNames::indexOf).toArray();
- RecordComparator comparator =
- CodeGenUtils.newRecordComparator(
- rowType.getFieldTypes(), fields, "UserDefinedSeqComparator");
- return new UserDefinedSeqComparator(fields, comparator);
+ return create(rowType, options.sequenceField());
}
@Nullable
diff --git a/paimon-core/src/test/java/org/apache/paimon/table/PrimaryKeyFileStoreTableTest.java b/paimon-core/src/test/java/org/apache/paimon/table/PrimaryKeyFileStoreTableTest.java
index ff280c27e..68adf63fb 100644
--- a/paimon-core/src/test/java/org/apache/paimon/table/PrimaryKeyFileStoreTableTest.java
+++ b/paimon-core/src/test/java/org/apache/paimon/table/PrimaryKeyFileStoreTableTest.java
@@ -186,7 +186,7 @@ public class PrimaryKeyFileStoreTableTest extends FileStoreTableTestBase {
}
@Test
- public void testSequenceNumber() throws Exception {
+ public void testSequenceField() throws Exception {
FileStoreTable table =
createFileStoreTable(conf -> conf.set(CoreOptions.SEQUENCE_FIELD, "b"));
StreamTableWrite write = table.newWrite(commitUser);
@@ -200,13 +200,28 @@ public class PrimaryKeyFileStoreTableTest extends FileStoreTableTestBase {
write.close();
commit.close();
- List<Split> splits = toSplits(table.newSnapshotReader().read().dataSplits());
- TableRead read = table.newRead();
- assertThat(getResult(read, splits, binaryRow(1), 0, BATCH_ROW_TO_STRING))
+ List<Split> splits = table.newSnapshotReader().read().splits();
+ InnerTableRead read = table.newRead();
+ Function<InternalRow, String> toString = BATCH_ROW_TO_STRING;
+ assertThat(getResult(read, splits, binaryRow(1), 0, toString))
.isEqualTo(
Arrays.asList(
"1|10|200|binary|varbinary|mapKey:mapVal|multiset",
"1|11|101|binary|varbinary|mapKey:mapVal|multiset"));
+
+ // verify projection with sequence field
+
+ // read with sequence field
+ read = read.withProjection(new int[] {2, 1});
+ toString = r -> r.getLong(0) + "|" + r.getInt(1);
+ assertThat(getResult(read, splits, binaryRow(1), 0, toString))
+ .isEqualTo(Arrays.asList("200|10", "101|11"));
+
+ // read without sequence field
+ read = read.withProjection(new int[] {1, 0});
+ toString = r -> r.getInt(0) + "|" + r.getInt(1);
+ assertThat(getResult(read, splits, binaryRow(1), 0, toString))
+ .isEqualTo(Arrays.asList("10|1", "11|1"));
}
@Test
diff --git a/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/lookup/FullCacheLookupTable.java b/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/lookup/FullCacheLookupTable.java
index 185cf554b..8e937ffe1 100644
--- a/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/lookup/FullCacheLookupTable.java
+++ b/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/lookup/FullCacheLookupTable.java
@@ -72,7 +72,7 @@ public abstract class FullCacheLookupTable implements LookupTable {
FileStoreTable table = context.table;
List<String> sequenceFields = new ArrayList<>();
if (table.primaryKeys().size() > 0) {
- new CoreOptions(table.options()).sequenceField().ifPresent(sequenceFields::addAll);
+ sequenceFields = new CoreOptions(table.options()).sequenceField();
}
RowType projectedType = TypeUtils.project(table.rowType(), context.projection);
if (sequenceFields.size() > 0) {
diff --git a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/source/TestChangelogDataReadWrite.java b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/source/TestChangelogDataReadWrite.java
index bbd272547..7ffa94809 100644
--- a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/source/TestChangelogDataReadWrite.java
+++ b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/source/TestChangelogDataReadWrite.java
@@ -120,14 +120,15 @@ public class TestChangelogDataReadWrite {
rowDataIteratorCreator) {
SchemaManager schemaManager = new SchemaManager(LocalFileIO.create(), tablePath);
long schemaId = 0;
+ CoreOptions options = new CoreOptions(new HashMap<>());
KeyValueFileStoreRead read =
new KeyValueFileStoreRead(
+ options,
schemaManager,
schemaId,
KEY_TYPE,
VALUE_TYPE,
COMPARATOR,
- null,
DeduplicateMergeFunction.factory(),
KeyValueFileReaderFactory.builder(
LocalFileIO.create(),
@@ -138,7 +139,7 @@ public class TestChangelogDataReadWrite {
ignore -> avro,
pathFactory,
EXTRACTOR,
- new CoreOptions(new HashMap<>())));
+ options));
return new KeyValueTableRead(read, null) {
@Override