forked from facebookincubator/velox
-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Add StreamRecordTimestampInserter operator #64
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
lgbo-ustc
merged 5 commits into
bigo-sg:gluten-0530
from
ggjh-159:feat/stateful-stream-record-timestamp-inserter
Jul 14, 2026
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
25d342d
feat(stateful): add StreamRecordTimestampInserter operator
ggjh-159 0156d06
test(stateful): add StreamRecordTimestampInserter unit tests
ggjh-159 871b713
fix review: emit StreamRecord without timestamp on empty batch
ggjh-159 3e92895
Merge remote-tracking branch 'origin/gluten-0530' into feat/stateful-…
ggjh-159 56af361
style: fix clang-format violations in StreamRecordTimestampInserter p…
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
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
79 changes: 79 additions & 0 deletions
79
velox/experimental/stateful/StreamRecordTimestampInserter.cpp
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,79 @@ | ||
| /* | ||
| * Copyright (c) Facebook, Inc. and its affiliates. | ||
| * | ||
| * Licensed 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. | ||
| */ | ||
| #include "velox/experimental/stateful/StreamRecordTimestampInserter.h" | ||
|
|
||
| #include "velox/experimental/stateful/WatermarkGenerator.h" | ||
| #include "velox/type/Timestamp.h" | ||
| #include "velox/vector/SimpleVector.h" | ||
|
|
||
| namespace facebook::velox::stateful { | ||
|
|
||
| StreamRecordTimestampInserter::StreamRecordTimestampInserter( | ||
| std::unique_ptr<exec::Operator> op, | ||
| std::vector<std::unique_ptr<StatefulOperator>> targets, | ||
| int rowtimeFieldIndex) | ||
| : StatefulOperator(std::move(op), std::move(targets)), | ||
| rowtimeFieldIndex_(rowtimeFieldIndex) {} | ||
|
|
||
| void StreamRecordTimestampInserter::addInput(StreamElementPtr input) { | ||
| auto record = std::static_pointer_cast<StreamRecord>(input); | ||
| input_ = record->record(); | ||
| op()->addInput(input_); | ||
| } | ||
|
|
||
| void StreamRecordTimestampInserter::advance() { | ||
| if (!input_) { | ||
| return; | ||
| } | ||
| watermark::validateRowtimeNoNulls(input_, rowtimeFieldIndex_); | ||
| // The wrapped FilterProject projects the rowtime column into a single-column | ||
| // RowVector; drain it to read timestamps. | ||
| RowVectorPtr timestampVector = | ||
| watermark::getTimestampVector(op().get(), input_); | ||
|
|
||
| VELOX_CHECK_EQ( | ||
| timestampVector->childrenSize(), | ||
| 1, | ||
| "Expected single-column timestamp vector from wrapped FilterProject"); | ||
| auto child = timestampVector->childAt(0); | ||
| auto tsVector = child->as<SimpleVector<Timestamp>>(); | ||
| VELOX_CHECK_NOT_NULL(tsVector, "Rowtime column is not a SimpleVector<Timestamp>"); | ||
| const vector_size_t timestampSize = timestampVector->size(); | ||
| if (timestampSize == 0) { | ||
| pushOutput(std::make_shared<StreamRecord>( | ||
| getPlanNodeId(), std::move(input_))); | ||
| input_.reset(); | ||
| return; | ||
| } | ||
| int64_t maxTs = std::numeric_limits<int64_t>::min(); | ||
| for (vector_size_t i = 0; i < timestampSize; ++i) { | ||
| const int64_t millis = tsVector->valueAt(i).toMillis(); | ||
| if (millis > maxTs) { | ||
| maxTs = millis; | ||
| } | ||
| } | ||
|
|
||
| pushOutput(std::make_shared<StreamRecord>( | ||
| getPlanNodeId(), std::move(input_), maxTs)); | ||
|
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. if input.size == 0, then maxTs = std::numeric_limits<int64_t>::min(), |
||
| input_.reset(); | ||
| } | ||
|
|
||
| void StreamRecordTimestampInserter::close() { | ||
| StatefulOperator::close(); | ||
| input_.reset(); | ||
| } | ||
|
|
||
| } // namespace facebook::velox::stateful | ||
52 changes: 52 additions & 0 deletions
52
velox/experimental/stateful/StreamRecordTimestampInserter.h
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,52 @@ | ||
| /* | ||
| * Copyright (c) Facebook, Inc. and its affiliates. | ||
| * | ||
| * Licensed 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. | ||
| */ | ||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
|
|
||
| #include "velox/experimental/stateful/StatefulOperator.h" | ||
| #include "velox/experimental/stateful/StreamElement.h" | ||
|
|
||
| namespace facebook::velox::stateful { | ||
|
|
||
| /// Native counterpart of Flink's | ||
| /// org.apache.flink.table.runtime.operators.sink.StreamRecordTimestampInserter. | ||
| /// Reads the rowtime column of each input batch, takes the max timestamp, and | ||
| /// emits the original RowVector wrapped in a StreamRecord carrying that | ||
| /// timestamp. | ||
| class StreamRecordTimestampInserter : public StatefulOperator { | ||
| public: | ||
| StreamRecordTimestampInserter( | ||
| std::unique_ptr<exec::Operator> op, | ||
| std::vector<std::unique_ptr<StatefulOperator>> targets, | ||
| int rowtimeFieldIndex); | ||
|
|
||
| void addInput(StreamElementPtr input) override; | ||
|
|
||
| void advance() override; | ||
|
|
||
| std::string name() const override { | ||
| return "StreamRecordTimestampInserter"; | ||
| } | ||
|
|
||
| void close() override; | ||
|
|
||
| private: | ||
| RowVectorPtr input_; | ||
| const int rowtimeFieldIndex_; | ||
| }; | ||
|
|
||
| } // namespace facebook::velox::stateful |
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
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.
The result of
watermark::getTimestampVector(...)should beSimpleVector<int64_t>? and this can be seen fromWatermarkGeneratorandWatermarkAssignerThere 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.
In Flink file
org/apache/flink/table/runtime/operators/sink/StreamRecordTimestampInserter.javaThe type of the column vector at index
rowtimeIndexshould be Timestamp.