Skip to content
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

fix merging null multi value in partial upsert #13031

Merged
merged 2 commits into from
May 1, 2024
Merged
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,7 @@
*/
package org.apache.pinot.segment.local.upsert;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
Expand Down Expand Up @@ -47,12 +48,24 @@ public class PartialUpsertHandler {
private final TreeMap<String, FieldSpec> _fieldSpecMap;
private final PartialUpsertMerger _partialUpsertMerger;

private final Map<String, Object> _defaultNullValues = new HashMap<>();

public PartialUpsertHandler(Schema schema, List<String> comparisonColumns, UpsertConfig upsertConfig) {
_primaryKeyColumns = schema.getPrimaryKeyColumns();
_comparisonColumns = comparisonColumns;
_fieldSpecMap = schema.getFieldSpecMap();
_partialUpsertMerger =
PartialUpsertMergerFactory.getPartialUpsertMerger(_primaryKeyColumns, comparisonColumns, upsertConfig);
// cache default null values to handle null merger results
for (Map.Entry<String, FieldSpec> entry : schema.getFieldSpecMap().entrySet()) {
String column = entry.getKey();
FieldSpec fieldSpec = entry.getValue();
if (fieldSpec.isSingleValueField()) {
_defaultNullValues.put(column, fieldSpec.getDefaultNullValue());
} else {
_defaultNullValues.put(column, new Object[]{fieldSpec.getDefaultNullValue()});
}
}
}

public void merge(LazyRow previousRow, GenericRow newRow, Map<String, Object> resultHolder) {
Expand Down Expand Up @@ -83,8 +96,7 @@ private void setMergedValue(GenericRow row, String column, @Nullable Object merg
row.removeNullValueField(column);
row.putValue(column, mergedValue);
} else {
// if column exists but mapped to a null value then merger result was a null value
row.putDefaultNullValue(column, _fieldSpecMap.get(column).getDefaultNullValue());
row.putDefaultNullValue(column, _defaultNullValues.get(column));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,14 @@ public void testCustomPartialUpsertMergerWithNullResult() {
newRowData.put("hoursSinceEpoch", null); // testing null comparison column
GenericRow newRecord = initGenericRow(new GenericRow(), newRowData);
LazyRow prevRecord = mock(LazyRow.class);
mockLazyRow(prevRecord, Map.of("pk", "pk1", "field1", 5L, "field2", "set", "hoursSinceEpoch", 2L));
Map<String, Object> expectedData = new HashMap<>(Map.of("pk", "pk1", "field2", "reset", "hoursSinceEpoch", 2L));
mockLazyRow(prevRecord,
Map.of("pk", "pk1", "field1", 5L, "field2", "set", "field3", new Integer[]{0}, "hoursSinceEpoch", 2L));
Map<String, Object> expectedData = new HashMap<>(
Map.of("pk", "pk1", "field2", "reset", "hoursSinceEpoch", 2L));
expectedData.put("field1", Long.MIN_VALUE);
GenericRow expectedRecord = initGenericRow(new GenericRow(), expectedData);
expectedRecord.addNullValueField("field1");
expectedRecord.putDefaultNullValue("field3", new Object[]{Integer.MIN_VALUE});

testCustomMerge(prevRecord, newRecord, expectedRecord, getCustomMerger());
}
Expand Down Expand Up @@ -138,6 +141,7 @@ private void testCustomMerge(LazyRow prevRecord, GenericRow newRecord, GenericRo
Schema schema = new Schema.SchemaBuilder().addSingleValueDimension("pk", FieldSpec.DataType.STRING)
.addSingleValueDimension("field1", FieldSpec.DataType.LONG)
.addSingleValueDimension("field2", FieldSpec.DataType.STRING)
.addMultiValueDimension("field3", FieldSpec.DataType.INT)
.addDateTime("hoursSinceEpoch", FieldSpec.DataType.LONG, "1:HOURS:EPOCH", "1:HOURS")
.setPrimaryKeyColumns(Arrays.asList("pk")).build();

Expand Down Expand Up @@ -169,6 +173,7 @@ public PartialUpsertMerger getCustomMerger() {
}
if ((newRow.getValue("field2")).equals("reset")) {
resultHolder.put("field1", null);
resultHolder.put("field3", null);
}
};
}
Expand Down
Loading