Repository navigation
[flink] Skip updates to non-projected columns #5115
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
Open
loserwang1024
wants to merge
2
commits into
apache:main
Choose a base branch
from
loserwang1024:fluss-project-read
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
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
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
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
165 changes: 165 additions & 0 deletions
165
...flink-common/src/main/java/org/apache/fluss/flink/source/reader/ProjectedRowEquality.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,165 @@ | ||
| /* | ||
| * 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.fluss.flink.source.reader; | ||
|
|
||
| import org.apache.fluss.row.InternalArray; | ||
| import org.apache.fluss.row.InternalMap; | ||
| import org.apache.fluss.row.InternalRow; | ||
| import org.apache.fluss.types.ArrayType; | ||
| import org.apache.fluss.types.BinaryType; | ||
| import org.apache.fluss.types.BytesType; | ||
| import org.apache.fluss.types.DataType; | ||
| import org.apache.fluss.types.DataTypeDefaultVisitor; | ||
| import org.apache.fluss.types.MapType; | ||
| import org.apache.fluss.types.RowType; | ||
| import org.apache.fluss.utils.ByteArrayWrapper; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.function.BiPredicate; | ||
| import java.util.function.UnaryOperator; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import static org.apache.fluss.utils.CollectionUtils.newHashMapWithExpectedSize; | ||
|
|
||
| /** Builds equality checks for projected log rows materialized by the Fluss scanner. */ | ||
| final class ProjectedRowEquality extends DataTypeDefaultVisitor<UnaryOperator<Object>> { | ||
| private static final ProjectedRowEquality INSTANCE = new ProjectedRowEquality(); | ||
| private static final UnaryOperator<Object> IDENTITY = UnaryOperator.identity(); | ||
| private static final UnaryOperator<Object> BINARY = | ||
| value -> new ByteArrayWrapper((byte[]) value); | ||
|
|
||
| private ProjectedRowEquality() {} | ||
|
|
||
| static BiPredicate<InternalRow, InternalRow> create(RowType type) { | ||
| List<UnaryOperator<Object>> fields = INSTANCE.fieldKeys(type); | ||
| if (fields.stream().allMatch(ProjectedRowEquality::supportsDeepEquals)) { | ||
| return Objects::equals; | ||
| } | ||
| InternalRow.FieldGetter[] getters = InternalRow.createFieldGetters(type); | ||
| for (int i = 0; i < getters.length; i++) { | ||
| UnaryOperator<Object> key = fields.get(i); | ||
| if (!supportsDeepEquals(key)) { | ||
| InternalRow.FieldGetter getter = getters[i]; | ||
| getters[i] = row -> comparisonKey(key, getter.getFieldOrNull(row)); | ||
| } | ||
| } | ||
| return (left, right) -> { | ||
| for (InternalRow.FieldGetter getter : getters) { | ||
| if (!Objects.deepEquals( | ||
| getter.getFieldOrNull(left), getter.getFieldOrNull(right))) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| }; | ||
| } | ||
|
|
||
| // GenericRow, GenericArray and GenericMap values already compare byte[] by content. | ||
| // Java map keys also need a content-based hash code. | ||
| private static boolean supportsDeepEquals(UnaryOperator<Object> key) { | ||
| return key == IDENTITY || key == BINARY; | ||
| } | ||
|
|
||
| @Override | ||
| protected UnaryOperator<Object> defaultMethod(DataType type) { | ||
| return IDENTITY; | ||
| } | ||
|
|
||
| @Override | ||
| public UnaryOperator<Object> visit(BinaryType type) { | ||
| return BINARY; | ||
| } | ||
|
|
||
| @Override | ||
| public UnaryOperator<Object> visit(BytesType type) { | ||
| return BINARY; | ||
| } | ||
|
|
||
| @Override | ||
| public UnaryOperator<Object> visit(MapType type) { | ||
| UnaryOperator<Object> key = type.getKeyType().accept(this); | ||
| UnaryOperator<Object> value = type.getValueType().accept(this); | ||
| if (key == IDENTITY && supportsDeepEquals(value)) { | ||
| return IDENTITY; | ||
| } | ||
| InternalArray.ElementGetter keyGetter = | ||
| InternalArray.createElementGetter(type.getKeyType()); | ||
| InternalArray.ElementGetter valueGetter = | ||
| InternalArray.createElementGetter(type.getValueType()); | ||
| return object -> { | ||
| InternalMap map = (InternalMap) object; | ||
| Map<Object, Object> entries = newHashMapWithExpectedSize(map.size()); | ||
| InternalArray keys = map.keyArray(); | ||
| InternalArray values = map.valueArray(); | ||
| for (int i = 0; i < map.size(); i++) { | ||
| entries.put( | ||
| key.apply(keyGetter.getElementOrNull(keys, i)), | ||
| comparisonKey(value, valueGetter.getElementOrNull(values, i))); | ||
| } | ||
| return entries; | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public UnaryOperator<Object> visit(ArrayType type) { | ||
| UnaryOperator<Object> element = type.getElementType().accept(this); | ||
| if (supportsDeepEquals(element)) { | ||
| return IDENTITY; | ||
| } | ||
| InternalArray.ElementGetter getter = | ||
| InternalArray.createElementGetter(type.getElementType()); | ||
| return object -> { | ||
| InternalArray array = (InternalArray) object; | ||
| List<Object> elements = new ArrayList<>(array.size()); | ||
| for (int i = 0; i < array.size(); i++) { | ||
| elements.add(comparisonKey(element, getter.getElementOrNull(array, i))); | ||
| } | ||
| return elements; | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public UnaryOperator<Object> visit(RowType type) { | ||
| List<UnaryOperator<Object>> fields = fieldKeys(type); | ||
| if (fields.stream().allMatch(ProjectedRowEquality::supportsDeepEquals)) { | ||
| return IDENTITY; | ||
| } | ||
| InternalRow.FieldGetter[] getters = InternalRow.createFieldGetters(type); | ||
| return object -> { | ||
| InternalRow row = (InternalRow) object; | ||
| List<Object> values = new ArrayList<>(fields.size()); | ||
| for (int i = 0; i < getters.length; i++) { | ||
| values.add(comparisonKey(fields.get(i), getters[i].getFieldOrNull(row))); | ||
| } | ||
| return values; | ||
| }; | ||
| } | ||
|
|
||
| private List<UnaryOperator<Object>> fieldKeys(RowType type) { | ||
| return type.getFieldTypes().stream() | ||
| .map(field -> field.accept(this)) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private static Object comparisonKey(UnaryOperator<Object> key, Object value) { | ||
| return value == null ? null : key.apply(value); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Could we also exclude
$changelogscans from this optimization?ChangelogFlinkTableSourcepasses itsdataProjectionto the same split reader, andtoFlussClientConfig()now forwards this option for that source as well, so projected virtual-table reads can satisfy this condition.I reproduced this with a primary-key table
(id, name, age): insert(1, 'Alice', 10), then update onlyageto20, and run:With the option disabled, the query returns the insert plus both update events. With it enabled, only the insert remains. The physical projected values are equal, but
_change_typeand_log_offsetdistinguish the two update events; they are filtered before that metadata is materialized. Since$changelogrepresents each event as a separate INSERT row, this drops change-history records, contrary to the documented exclusion of virtual tables.Would it make sense to restrict filtering to ordinary primary-key table scans and add a regression test combining projected data and metadata columns?