-
Notifications
You must be signed in to change notification settings - Fork 625
[GLUTEN-12426][FLINK] Feat: Add columnar StreamRecordTimestampInserter operator #12428
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
ggjh-159
wants to merge
7
commits into
apache:main
Choose a base branch
from
ggjh-159:feat/stateful-stream-record-timestamp-inserter
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.
+319
−5
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ef38250
feat(flink): add columnar StreamRecordTimestampInserter operator
ggjh-159 c2fd991
test(flink): add GlutenRowtimeInserterHelper unit tests
ggjh-159 b0062d7
Merge branch 'main' into feat/stateful-stream-record-timestamp-inserter
ggjh-159 cb649b0
fix review: remove inserter when sink does not consume StreamRecord.t…
ggjh-159 ae4ffe6
Merge remote-tracking branch 'origin/feat/stateful-stream-record-time…
ggjh-159 b686da9
Merge remote-tracking branch 'upstream/main' into feat/stateful-strea…
ggjh-159 a7565e6
fix: replace em-dash with ASCII hyphen in PrintSinkFactory comment
ggjh-159 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
174 changes: 174 additions & 0 deletions
174
gluten-flink/planner/src/main/java/org/apache/gluten/velox/GlutenRowtimeInserterHelper.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,174 @@ | ||
| /* | ||
| * 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.gluten.velox; | ||
|
|
||
| import org.apache.gluten.table.runtime.operators.GlutenOneInputOperator; | ||
| import org.apache.gluten.util.LogicalTypeConverter; | ||
| import org.apache.gluten.util.PlanNodeIdGenerator; | ||
| import org.apache.gluten.util.ReflectUtils; | ||
|
|
||
| import io.github.zhztheplayer.velox4j.expression.FieldAccessTypedExpr; | ||
| import io.github.zhztheplayer.velox4j.expression.InputTypedExpr; | ||
| import io.github.zhztheplayer.velox4j.plan.EmptyNode; | ||
| import io.github.zhztheplayer.velox4j.plan.ProjectNode; | ||
| import io.github.zhztheplayer.velox4j.plan.StatefulPlanNode; | ||
| import io.github.zhztheplayer.velox4j.plan.StreamRecordTimestampInserterNode; | ||
| import io.github.zhztheplayer.velox4j.stateful.StatefulRecord; | ||
| import io.github.zhztheplayer.velox4j.type.RowType; | ||
|
|
||
| import org.apache.flink.api.dag.Transformation; | ||
| import org.apache.flink.streaming.api.datastream.DataStream; | ||
| import org.apache.flink.streaming.api.operators.OneInputStreamOperator; | ||
| import org.apache.flink.streaming.api.operators.SimpleOperatorFactory; | ||
| import org.apache.flink.streaming.api.transformations.OneInputTransformation; | ||
| import org.apache.flink.table.data.RowData; | ||
| import org.apache.flink.table.runtime.operators.sink.StreamRecordTimestampInserter; | ||
| import org.apache.flink.table.runtime.typeutils.InternalTypeInfo; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Inspects a sink input chain for the native Flink {@link StreamRecordTimestampInserter} (per-row | ||
| * timestamp) added by {@code CommonExecSink.applyRowtimeTransformation}, and either removes or | ||
| * replaces it depending on whether the downstream sink actually consumes the timestamp. | ||
| * | ||
| * <p>The native inserter stamps each row's rowtime onto the surrounding {@code StreamRecord} so | ||
| * that downstream {@code SinkFunction.Context.timestamp()} readers (or sinks that read {@code | ||
| * StreamRecord.timestamp} directly) can access it. Sinks that never read the timestamp therefore | ||
| * don't need the inserter at all. | ||
| * | ||
| * <p>Callers declare this via {@code requiresTimestamp}: | ||
| * | ||
| * <ul> | ||
| * <li>{@code false}: the sink does not consume {@code StreamRecord.timestamp}. The inserter is | ||
| * removed from the op chain and its upstream is wired directly to the sink. This is the | ||
| * correct behavior for every sink currently wired through the helper (Print, Fuzzer/Discard, | ||
| * FileSystem): Print reads rowtime from RowData via {@code SinkOperator.timestamp()}; | ||
| * SinkV2-based sinks (DiscardingSink, etc.) cannot read the timestamp at all; the velox file | ||
| * writer doesn't consult StreamRecord.timestamp for partition/roll/commit. | ||
| * <li>{@code true}: the sink does consume {@code StreamRecord.timestamp}. The inserter is rebuilt | ||
| * as a Gluten columnar inserter (batch-max timestamp on a {@link StatefulRecord}) so the | ||
| * columnar chain stays intact end-to-end. No current caller uses this branch; it's kept for | ||
| * future sinks that read the timestamp directly. | ||
| * </ul> | ||
| * | ||
| * <p>If no inserter is present (e.g., {@code rowtimeFieldIndex == -1}), the helper is a no-op and | ||
| * returns the input unchanged regardless of {@code requiresTimestamp}. | ||
| */ | ||
| public final class GlutenRowtimeInserterHelper { | ||
|
|
||
| private GlutenRowtimeInserterHelper() {} | ||
|
|
||
| /** | ||
| * Convenience overload that accepts a {@link DataStream} (typical entry point for factories that | ||
| * use {@code sinkTransformation.getInputStream()}). Inspects the underlying transformation and | ||
| * removes or replaces the inserter per {@code requiresTimestamp}; returns a new DataStream whose | ||
| * terminal node reflects the result, or the inputStream unchanged when no replacement happened. | ||
| */ | ||
| public static DataStream<RowData> process( | ||
| DataStream<RowData> inputStream, boolean requiresTimestamp) { | ||
| Transformation<RowData> inputTrans = inputStream.getTransformation(); | ||
| Transformation<RowData> newTrans = processTransformation(inputTrans, requiresTimestamp); | ||
| if (newTrans == inputTrans) { | ||
| return inputStream; | ||
| } | ||
| return new DataStream<>(inputStream.getExecutionEnvironment(), newTrans); | ||
| } | ||
|
|
||
| /** | ||
| * Inspect {@code inputTrans}. If it is a native {@link StreamRecordTimestampInserter}: | ||
| * | ||
| * <ul> | ||
| * <li>when {@code requiresTimestamp} is false, return the inserter's upstream, removing the | ||
| * inserter from the op chain; | ||
| * <li>when {@code requiresTimestamp} is true, rebuild it as a Gluten columnar inserter whose | ||
| * input is the native inserter's upstream. | ||
| * </ul> | ||
| * | ||
| * <p>Returns the original inputTrans when no inserter is present. | ||
| */ | ||
| public static Transformation<RowData> processTransformation( | ||
| Transformation<RowData> inputTrans, boolean requiresTimestamp) { | ||
| if (!(inputTrans instanceof OneInputTransformation)) { | ||
| return inputTrans; | ||
| } | ||
| OneInputTransformation<?, ?> oneInput = (OneInputTransformation<?, ?>) inputTrans; | ||
| if (!(oneInput.getOperatorFactory() instanceof SimpleOperatorFactory)) { | ||
| return inputTrans; | ||
| } | ||
| @SuppressWarnings("rawtypes") | ||
| Object op = ((SimpleOperatorFactory) oneInput.getOperatorFactory()).getOperator(); | ||
| if (!(op instanceof StreamRecordTimestampInserter)) { | ||
| return inputTrans; | ||
| } | ||
| List<Transformation<?>> inputs = oneInput.getInputs(); | ||
| if (inputs.isEmpty()) { | ||
| return inputTrans; | ||
| } | ||
| @SuppressWarnings("unchecked") | ||
| Transformation<RowData> aboveInserter = (Transformation<RowData>) inputs.get(0); | ||
| if (!requiresTimestamp) { | ||
| return aboveInserter; | ||
| } | ||
| int rowtimeIndex = | ||
| (int) ReflectUtils.getObjectField(StreamRecordTimestampInserter.class, op, "rowtimeIndex"); | ||
| return buildGlutenInserter(aboveInserter, rowtimeIndex, oneInput.getParallelism()); | ||
| } | ||
|
|
||
| private static Transformation<RowData> buildGlutenInserter( | ||
| Transformation<RowData> aboveInserter, int rowtimeFieldIndex, int parallelism) { | ||
| @SuppressWarnings("unchecked") | ||
| InternalTypeInfo<RowData> internalTypeInfo = | ||
| (InternalTypeInfo<RowData>) aboveInserter.getOutputType(); | ||
| final org.apache.flink.table.types.logical.RowType inputRowType = | ||
| (org.apache.flink.table.types.logical.RowType) internalTypeInfo.toLogicalType(); | ||
| final RowType vlInputType = (RowType) LogicalTypeConverter.toVLType(inputRowType); | ||
| final List<String> fieldNames = inputRowType.getFieldNames(); | ||
| final String rowtimeFieldName = fieldNames.get(rowtimeFieldIndex); | ||
| final InputTypedExpr inputExpr = new InputTypedExpr(vlInputType); | ||
| final ProjectNode project = | ||
| new ProjectNode( | ||
| PlanNodeIdGenerator.newId(), | ||
| List.of(new EmptyNode(vlInputType)), | ||
| List.of(rowtimeFieldName), | ||
| List.of(FieldAccessTypedExpr.create(inputExpr, rowtimeFieldName))); | ||
| final StreamRecordTimestampInserterNode inserterNode = | ||
| new StreamRecordTimestampInserterNode( | ||
| PlanNodeIdGenerator.newId(), null, project, rowtimeFieldIndex); | ||
| final StatefulPlanNode statefulPlan = new StatefulPlanNode(inserterNode.getId(), inserterNode); | ||
|
|
||
| final GlutenOneInputOperator<StatefulRecord, StatefulRecord> operator = | ||
| new GlutenOneInputOperator<>( | ||
| statefulPlan, | ||
| PlanNodeIdGenerator.newId(), | ||
| vlInputType, | ||
| Map.of(inserterNode.getId(), vlInputType), | ||
| StatefulRecord.class, | ||
| StatefulRecord.class, | ||
| "StreamRecordTimestampInserter"); | ||
|
|
||
| @SuppressWarnings({"rawtypes", "unchecked"}) | ||
| final OneInputStreamOperator rawOperator = (OneInputStreamOperator) operator; | ||
| return new OneInputTransformation<>( | ||
| aboveInserter, | ||
| "StreamRecordTimestampInserter", | ||
| SimpleOperatorFactory.of(rawOperator), | ||
| aboveInserter.getOutputType(), | ||
| parallelism); | ||
| } | ||
| } |
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
131 changes: 131 additions & 0 deletions
131
gluten-flink/ut/src/test/java/org/apache/gluten/velox/GlutenRowtimeInserterHelperTest.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,131 @@ | ||
| /* | ||
| * 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.gluten.velox; | ||
|
|
||
| import org.apache.gluten.table.runtime.operators.GlutenOneInputOperator; | ||
|
|
||
| import org.apache.flink.api.common.functions.MapFunction; | ||
| import org.apache.flink.api.dag.Transformation; | ||
| import org.apache.flink.streaming.api.operators.SimpleOperatorFactory; | ||
| import org.apache.flink.streaming.api.operators.StreamMap; | ||
| import org.apache.flink.streaming.api.transformations.OneInputTransformation; | ||
| import org.apache.flink.table.data.RowData; | ||
| import org.apache.flink.table.runtime.operators.sink.StreamRecordTimestampInserter; | ||
| import org.apache.flink.table.runtime.typeutils.InternalTypeInfo; | ||
| import org.apache.flink.table.types.logical.BigIntType; | ||
| import org.apache.flink.table.types.logical.RowType; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertNotSame; | ||
| import static org.junit.jupiter.api.Assertions.assertSame; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| class GlutenRowtimeInserterHelperTest { | ||
|
|
||
| private static final RowType UPSTREAM_ROW_TYPE = RowType.of(new BigIntType()); | ||
|
|
||
| private static Transformation<RowData> newUpstream() { | ||
| return new StubTransformation("upstream", InternalTypeInfo.of(UPSTREAM_ROW_TYPE)); | ||
| } | ||
|
|
||
| private static OneInputTransformation<RowData, RowData> newNativeInserterTx( | ||
| Transformation<RowData> upstream, int rowtimeIndex) { | ||
| StreamRecordTimestampInserter op = new StreamRecordTimestampInserter(rowtimeIndex); | ||
| return new OneInputTransformation<>( | ||
| upstream, "native-inserter", op, upstream.getOutputType(), 1); | ||
| } | ||
|
|
||
| private static OneInputTransformation<RowData, RowData> newOtherOperatorTx( | ||
| Transformation<RowData> upstream) { | ||
| StreamMap<RowData, RowData> other = new StreamMap<>(new IdentityMapFunction()); | ||
| return new OneInputTransformation<>(upstream, "other-op", other, upstream.getOutputType(), 1); | ||
| } | ||
|
|
||
| @Test | ||
| void testNoOpForNonOneInputTransformation() { | ||
| Transformation<RowData> stub = | ||
| new StubTransformation("stub", InternalTypeInfo.of(UPSTREAM_ROW_TYPE)); | ||
| assertSame(stub, GlutenRowtimeInserterHelper.processTransformation(stub, false)); | ||
| assertSame(stub, GlutenRowtimeInserterHelper.processTransformation(stub, true)); | ||
| } | ||
|
|
||
| @Test | ||
| void testNoOpForNonInserterOperator() { | ||
| Transformation<RowData> upstream = newUpstream(); | ||
| OneInputTransformation<RowData, RowData> tx = newOtherOperatorTx(upstream); | ||
| assertSame(tx, GlutenRowtimeInserterHelper.processTransformation(tx, false)); | ||
| assertSame(tx, GlutenRowtimeInserterHelper.processTransformation(tx, true)); | ||
| } | ||
|
|
||
| @Test | ||
| void testReplacesNativeInserterWhenTimestampRequired() { | ||
| Transformation<RowData> upstream = newUpstream(); | ||
| OneInputTransformation<RowData, RowData> nativeTx = newNativeInserterTx(upstream, 0); | ||
|
|
||
| Transformation<RowData> result = | ||
| GlutenRowtimeInserterHelper.processTransformation(nativeTx, true); | ||
|
|
||
| assertNotSame(nativeTx, result); | ||
| assertTrue(result instanceof OneInputTransformation); | ||
| @SuppressWarnings("unchecked") | ||
| OneInputTransformation<RowData, RowData> out = | ||
| (OneInputTransformation<RowData, RowData>) result; | ||
| assertTrue(out.getOperatorFactory() instanceof SimpleOperatorFactory); | ||
| Object op = ((SimpleOperatorFactory<?>) out.getOperatorFactory()).getOperator(); | ||
| assertTrue(op instanceof GlutenOneInputOperator); | ||
| assertSame(upstream, out.getInputs().get(0)); | ||
| assertSame(1, out.getParallelism()); | ||
| } | ||
|
|
||
| @Test | ||
| void testRemovesNativeInserterWhenTimestampNotRequired() { | ||
| Transformation<RowData> upstream = newUpstream(); | ||
| OneInputTransformation<RowData, RowData> nativeTx = newNativeInserterTx(upstream, 0); | ||
|
|
||
| Transformation<RowData> result = | ||
| GlutenRowtimeInserterHelper.processTransformation(nativeTx, false); | ||
|
|
||
| assertSame(upstream, result); | ||
| } | ||
|
|
||
| private static final class StubTransformation extends Transformation<RowData> { | ||
| StubTransformation(String name, InternalTypeInfo<RowData> typeInfo) { | ||
| super(name, typeInfo, 1); | ||
| } | ||
|
|
||
| @Override | ||
| public List<Transformation<?>> getInputs() { | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| @Override | ||
| protected List<Transformation<?>> getTransitivePredecessorsInternal() { | ||
| return Collections.emptyList(); | ||
| } | ||
| } | ||
|
|
||
| private static final class IdentityMapFunction implements MapFunction<RowData, RowData> { | ||
| @Override | ||
| public RowData map(RowData value) { | ||
| return value; | ||
| } | ||
| } | ||
| } | ||
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.
We use max timestamp in a batch record to replace the per-row timestamp, which is a different semantics,can we add a test for this?
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.
All three sink factories (Print, Fuzzer/Discard, FileSystem) wire
requiresTimestamp = false, so the inserter is removed from the op chain and the batch-max path inGlutenRowtimeInserterHelperhas no runtime caller in this PR. A test here would exercise a path no real sink reaches.Batch-max computation itself is covered by velox C++ UT
StreamRecordTimestampInserterTest.emitsBatchMaxTimestamp(PR bigo-sg/velox#64). The replacement/removal branches are covered byGlutenRowtimeInserterHelperTest.