-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[core] Introduce 'nested_partial_update' agg func #6924
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
108 changes: 108 additions & 0 deletions
108
.../main/java/org/apache/paimon/mergetree/compact/aggregate/FieldNestedPartialUpdateAgg.java
This file contains hidden or 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,108 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.paimon.mergetree.compact.aggregate; | ||
|
|
||
| import org.apache.paimon.codegen.Projection; | ||
| import org.apache.paimon.data.BinaryRow; | ||
| import org.apache.paimon.data.GenericArray; | ||
| import org.apache.paimon.data.GenericRow; | ||
| import org.apache.paimon.data.InternalArray; | ||
| import org.apache.paimon.data.InternalRow; | ||
| import org.apache.paimon.data.InternalRow.FieldGetter; | ||
| import org.apache.paimon.types.ArrayType; | ||
| import org.apache.paimon.types.RowType; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import static org.apache.paimon.codegen.CodeGenUtils.newProjection; | ||
| import static org.apache.paimon.utils.Preconditions.checkArgument; | ||
|
|
||
| /** | ||
| * Used to partial update a field which representing a nested table. The data type of nested table | ||
| * field is {@code ARRAY<ROW>}. | ||
| */ | ||
| public class FieldNestedPartialUpdateAgg extends FieldAggregator { | ||
|
|
||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| private final int nestedFields; | ||
| private final Projection keyProjection; | ||
| private final FieldGetter[] fieldGetters; | ||
|
|
||
| public FieldNestedPartialUpdateAgg(String name, ArrayType dataType, List<String> nestedKey) { | ||
| super(name, dataType); | ||
| RowType nestedType = (RowType) dataType.getElementType(); | ||
| this.nestedFields = nestedType.getFieldCount(); | ||
| checkArgument(!nestedKey.isEmpty()); | ||
| this.keyProjection = newProjection(nestedType, nestedKey); | ||
| this.fieldGetters = new FieldGetter[nestedFields]; | ||
| for (int i = 0; i < nestedFields; i++) { | ||
| fieldGetters[i] = InternalRow.createFieldGetter(nestedType.getTypeAt(i), i); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Object agg(Object accumulator, Object inputField) { | ||
| if (accumulator == null || inputField == null) { | ||
| return accumulator == null ? inputField : accumulator; | ||
| } | ||
|
|
||
| InternalArray acc = (InternalArray) accumulator; | ||
| InternalArray input = (InternalArray) inputField; | ||
|
|
||
| List<InternalRow> rows = new ArrayList<>(acc.size() + input.size()); | ||
| addNonNullRows(acc, rows); | ||
| addNonNullRows(input, rows); | ||
|
|
||
| if (keyProjection != null) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when keyProjection is null ? |
||
| Map<BinaryRow, GenericRow> map = new HashMap<>(); | ||
| for (InternalRow row : rows) { | ||
| BinaryRow key = keyProjection.apply(row).copy(); | ||
| GenericRow toUpdate = map.computeIfAbsent(key, k -> new GenericRow(nestedFields)); | ||
| partialUpdate(toUpdate, row); | ||
| } | ||
|
|
||
| rows = new ArrayList<>(map.values()); | ||
| } | ||
|
|
||
| return new GenericArray(rows.toArray()); | ||
| } | ||
|
|
||
| private void addNonNullRows(InternalArray array, List<InternalRow> rows) { | ||
| for (int i = 0; i < array.size(); i++) { | ||
| if (array.isNullAt(i)) { | ||
| continue; | ||
| } | ||
| rows.add(array.getRow(i, nestedFields)); | ||
| } | ||
| } | ||
|
|
||
| private void partialUpdate(GenericRow toUpdate, InternalRow input) { | ||
| for (int i = 0; i < fieldGetters.length; i++) { | ||
| FieldGetter fieldGetter = fieldGetters[i]; | ||
| Object field = fieldGetter.getFieldOrNull(input); | ||
| if (field != null) { | ||
| toUpdate.setField(i, field); | ||
| } | ||
| } | ||
| } | ||
| } | ||
58 changes: 58 additions & 0 deletions
58
...apache/paimon/mergetree/compact/aggregate/factory/FieldNestedPartialUpdateAggFactory.java
This file contains hidden or 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,58 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.paimon.mergetree.compact.aggregate.factory; | ||
|
|
||
| import org.apache.paimon.CoreOptions; | ||
| import org.apache.paimon.mergetree.compact.aggregate.FieldNestedPartialUpdateAgg; | ||
| import org.apache.paimon.types.ArrayType; | ||
| import org.apache.paimon.types.DataType; | ||
| import org.apache.paimon.types.RowType; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static org.apache.paimon.utils.Preconditions.checkArgument; | ||
|
|
||
| /** Factory for #{@link FieldNestedPartialUpdateAgg}. */ | ||
| public class FieldNestedPartialUpdateAggFactory implements FieldAggregatorFactory { | ||
|
|
||
| public static final String NAME = "nested_partial_update"; | ||
|
|
||
| @Override | ||
| public FieldNestedPartialUpdateAgg create( | ||
| DataType fieldType, CoreOptions options, String field) { | ||
| return createFieldNestedPartialUpdateAgg( | ||
| fieldType, options.fieldNestedUpdateAggNestedKey(field)); | ||
| } | ||
|
|
||
| @Override | ||
| public String identifier() { | ||
| return NAME; | ||
| } | ||
|
|
||
| private FieldNestedPartialUpdateAgg createFieldNestedPartialUpdateAgg( | ||
| DataType fieldType, List<String> nestedKey) { | ||
| checkArgument(!nestedKey.isEmpty()); | ||
| String typeErrorMsg = | ||
| "Data type for nested table column must be 'Array<Row>' but was '%s'."; | ||
| checkArgument(fieldType instanceof ArrayType, typeErrorMsg, fieldType); | ||
| ArrayType arrayType = (ArrayType) fieldType; | ||
| checkArgument(arrayType.getElementType() instanceof RowType, typeErrorMsg, fieldType); | ||
| return new FieldNestedPartialUpdateAgg(identifier(), arrayType, nestedKey); | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a null check for the key?