|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.gluten.velox; |
| 18 | + |
| 19 | +import org.apache.gluten.table.runtime.operators.GlutenStreamRecordTimestampInserter; |
| 20 | +import org.apache.gluten.util.LogicalTypeConverter; |
| 21 | +import org.apache.gluten.util.PlanNodeIdGenerator; |
| 22 | +import org.apache.gluten.util.ReflectUtils; |
| 23 | + |
| 24 | +import io.github.zhztheplayer.velox4j.expression.FieldAccessTypedExpr; |
| 25 | +import io.github.zhztheplayer.velox4j.expression.InputTypedExpr; |
| 26 | +import io.github.zhztheplayer.velox4j.plan.EmptyNode; |
| 27 | +import io.github.zhztheplayer.velox4j.plan.ProjectNode; |
| 28 | +import io.github.zhztheplayer.velox4j.plan.StatefulPlanNode; |
| 29 | +import io.github.zhztheplayer.velox4j.plan.StreamRecordTimestampInserterNode; |
| 30 | +import io.github.zhztheplayer.velox4j.type.RowType; |
| 31 | + |
| 32 | +import org.apache.flink.api.dag.Transformation; |
| 33 | +import org.apache.flink.streaming.api.datastream.DataStream; |
| 34 | +import org.apache.flink.streaming.api.operators.OneInputStreamOperator; |
| 35 | +import org.apache.flink.streaming.api.operators.SimpleOperatorFactory; |
| 36 | +import org.apache.flink.streaming.api.transformations.OneInputTransformation; |
| 37 | +import org.apache.flink.table.data.RowData; |
| 38 | +import org.apache.flink.table.runtime.operators.sink.StreamRecordTimestampInserter; |
| 39 | +import org.apache.flink.table.runtime.typeutils.InternalTypeInfo; |
| 40 | + |
| 41 | +import java.util.List; |
| 42 | +import java.util.Map; |
| 43 | + |
| 44 | +/** |
| 45 | + * Replaces a native Flink {@link StreamRecordTimestampInserter} (per-row timestamp) with a Gluten |
| 46 | + * columnar inserter (batch-max timestamp) so the columnar chain stays intact when the downstream |
| 47 | + * sink is offloaded to Velox. |
| 48 | + * |
| 49 | + * <p>The native inserter is constructed by {@code CommonExecSink.applyRowtimeTransformation} and |
| 50 | + * sits somewhere on the sink's input chain (position depends on the sink: for a simple sink like |
| 51 | + * Fuzzer/DiscardingSink it is the direct input; for FileSystem it sits below the |
| 52 | + * StreamingFileWriter and PartitionCommitter). Each sink factory's {@code buildVeloxSink} invokes |
| 53 | + * this helper on the inserter-bearing transformation it is about to replace; if no inserter is |
| 54 | + * present (e.g., {@code rowtimeFieldIndex == -1}), the helper is a no-op and returns the input |
| 55 | + * unchanged. |
| 56 | + */ |
| 57 | +public final class GlutenRowtimeInserterHelper { |
| 58 | + |
| 59 | + private GlutenRowtimeInserterHelper() {} |
| 60 | + |
| 61 | + /** |
| 62 | + * Convenience overload that accepts a {@link DataStream} (typical entry point for factories that |
| 63 | + * use {@code sinkTransformation.getInputStream()}). Inspects the underlying transformation; if it |
| 64 | + * is a native inserter, rebuilds it as a Gluten columnar inserter and returns a new DataStream |
| 65 | + * whose terminal node is the Gluten inserter. Otherwise returns the inputStream unchanged. |
| 66 | + */ |
| 67 | + public static DataStream<RowData> process(DataStream<RowData> inputStream) { |
| 68 | + Transformation<RowData> inputTrans = inputStream.getTransformation(); |
| 69 | + Transformation<RowData> newTrans = processTransformation(inputTrans); |
| 70 | + if (newTrans == inputTrans) { |
| 71 | + return inputStream; |
| 72 | + } |
| 73 | + return new DataStream<>(inputStream.getExecutionEnvironment(), newTrans); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Inspect {@code inputTrans}; if it is a native {@link StreamRecordTimestampInserter}, rebuild it |
| 78 | + * as a Gluten columnar inserter whose input is the native inserter's upstream. Returns the new |
| 79 | + * transformation, or the original inputTrans when no replacement happened. |
| 80 | + */ |
| 81 | + public static Transformation<RowData> processTransformation(Transformation<RowData> inputTrans) { |
| 82 | + if (!(inputTrans instanceof OneInputTransformation)) { |
| 83 | + return inputTrans; |
| 84 | + } |
| 85 | + OneInputTransformation<?, ?> oneInput = (OneInputTransformation<?, ?>) inputTrans; |
| 86 | + if (!(oneInput.getOperatorFactory() instanceof SimpleOperatorFactory)) { |
| 87 | + return inputTrans; |
| 88 | + } |
| 89 | + @SuppressWarnings("rawtypes") |
| 90 | + Object op = ((SimpleOperatorFactory) oneInput.getOperatorFactory()).getOperator(); |
| 91 | + if (!(op instanceof StreamRecordTimestampInserter)) { |
| 92 | + return inputTrans; |
| 93 | + } |
| 94 | + int rowtimeIndex = |
| 95 | + (int) ReflectUtils.getObjectField(StreamRecordTimestampInserter.class, op, "rowtimeIndex"); |
| 96 | + List<Transformation<?>> inputs = oneInput.getInputs(); |
| 97 | + if (inputs.isEmpty()) { |
| 98 | + return inputTrans; |
| 99 | + } |
| 100 | + @SuppressWarnings("unchecked") |
| 101 | + Transformation<RowData> aboveInserter = (Transformation<RowData>) inputs.get(0); |
| 102 | + return buildGlutenInserter(aboveInserter, rowtimeIndex, oneInput.getParallelism()); |
| 103 | + } |
| 104 | + |
| 105 | + private static Transformation<RowData> buildGlutenInserter( |
| 106 | + Transformation<RowData> aboveInserter, int rowtimeFieldIndex, int parallelism) { |
| 107 | + @SuppressWarnings("unchecked") |
| 108 | + InternalTypeInfo<RowData> internalTypeInfo = |
| 109 | + (InternalTypeInfo<RowData>) aboveInserter.getOutputType(); |
| 110 | + final org.apache.flink.table.types.logical.RowType inputRowType = |
| 111 | + (org.apache.flink.table.types.logical.RowType) internalTypeInfo.toLogicalType(); |
| 112 | + final RowType vlInputType = (RowType) LogicalTypeConverter.toVLType(inputRowType); |
| 113 | + final List<String> fieldNames = inputRowType.getFieldNames(); |
| 114 | + final String rowtimeFieldName = fieldNames.get(rowtimeFieldIndex); |
| 115 | + final InputTypedExpr inputExpr = new InputTypedExpr(vlInputType); |
| 116 | + final ProjectNode project = |
| 117 | + new ProjectNode( |
| 118 | + PlanNodeIdGenerator.newId(), |
| 119 | + List.of(new EmptyNode(vlInputType)), |
| 120 | + List.of(rowtimeFieldName), |
| 121 | + List.of(FieldAccessTypedExpr.create(inputExpr, rowtimeFieldName))); |
| 122 | + final StreamRecordTimestampInserterNode inserterNode = |
| 123 | + new StreamRecordTimestampInserterNode( |
| 124 | + PlanNodeIdGenerator.newId(), null, project, rowtimeFieldIndex); |
| 125 | + final StatefulPlanNode statefulPlan = new StatefulPlanNode(inserterNode.getId(), inserterNode); |
| 126 | + |
| 127 | + final GlutenStreamRecordTimestampInserter operator = |
| 128 | + new GlutenStreamRecordTimestampInserter( |
| 129 | + statefulPlan, |
| 130 | + PlanNodeIdGenerator.newId(), |
| 131 | + vlInputType, |
| 132 | + Map.of(inserterNode.getId(), vlInputType), |
| 133 | + rowtimeFieldIndex, |
| 134 | + "GlutenStreamRecordTimestampInserter"); |
| 135 | + |
| 136 | + @SuppressWarnings({"rawtypes", "unchecked"}) |
| 137 | + final OneInputStreamOperator rawOperator = (OneInputStreamOperator) operator; |
| 138 | + return new OneInputTransformation<>( |
| 139 | + aboveInserter, |
| 140 | + "GlutenStreamRecordTimestampInserter", |
| 141 | + SimpleOperatorFactory.of(rawOperator), |
| 142 | + aboveInserter.getOutputType(), |
| 143 | + parallelism); |
| 144 | + } |
| 145 | +} |
0 commit comments