zhztheplayer commented on code in PR #8839: URL: https://github.com/apache/incubator-gluten/pull/8839#discussion_r2037780957
########## gluten-flink/runtime/src/main/java/org/apache/gluten/table/runtime/operators/GlutenCalOperator.java: ########## @@ -0,0 +1,125 @@ +/* + * 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.table.runtime.operators; + +import io.github.zhztheplayer.velox4j.connector.ExternalStream; +import io.github.zhztheplayer.velox4j.connector.ExternalStreamConnectorSplit; +import io.github.zhztheplayer.velox4j.iterator.DownIterator; +import io.github.zhztheplayer.velox4j.iterator.UpIterator; +import io.github.zhztheplayer.velox4j.query.BoundSplit; +import io.github.zhztheplayer.velox4j.serde.Serde; +import io.github.zhztheplayer.velox4j.type.RowType; +import org.apache.gluten.streaming.api.operators.GlutenOperator; +import org.apache.gluten.vectorized.VLVectorIterator; +import org.apache.gluten.vectorized.FlinkRowToVLVectorConvertor; + +import io.github.zhztheplayer.velox4j.Velox4j; +import io.github.zhztheplayer.velox4j.config.Config; +import io.github.zhztheplayer.velox4j.config.ConnectorConfig; +import io.github.zhztheplayer.velox4j.memory.AllocationListener; +import io.github.zhztheplayer.velox4j.memory.MemoryManager; +import io.github.zhztheplayer.velox4j.plan.PlanNode; +import io.github.zhztheplayer.velox4j.query.Query; +import io.github.zhztheplayer.velox4j.session.Session; +import org.apache.arrow.memory.BufferAllocator; +import org.apache.arrow.memory.RootAllocator; +import org.apache.flink.streaming.api.operators.OneInputStreamOperator; +import org.apache.flink.streaming.runtime.streamrecord.StreamRecord; +import org.apache.flink.table.data.RowData; +import org.apache.flink.table.runtime.operators.TableStreamOperator; + +import java.util.List; + +/** Calculate operator in gluten, which will call Velox to run. */ +public class GlutenCalOperator extends TableStreamOperator<RowData> + implements OneInputStreamOperator<RowData, RowData>, GlutenOperator { + + private final String glutenPlan; + private final String id; + private final String inputType; + private final String outputType; + + private StreamRecord outElement = null; + + private Session session; + private Query query; + private VLVectorIterator inputIterator; + BufferAllocator allocator; + + public GlutenCalOperator(String plan, String id, String inputType, String outputType) { + this.glutenPlan = plan; + this.id = id; + this.inputType = inputType; + this.outputType = outputType; + } + + @Override + public void open() throws Exception { + super.open(); + outElement = new StreamRecord(null); + session = Velox4j.newSession(MemoryManager.create(AllocationListener.NOOP)); + + inputIterator = new VLVectorIterator(); + ExternalStream es = session.externalStreamOps().bind(new DownIterator(inputIterator)); + List<BoundSplit> splits = List.of( + new BoundSplit( + id, + -1, + new ExternalStreamConnectorSplit("connector-external-stream", es.id()))); + PlanNode filter = Serde.fromJson(glutenPlan, PlanNode.class); + query = new Query(filter, splits, Config.empty(), ConnectorConfig.empty()); + allocator = new RootAllocator(Long.MAX_VALUE); + + } + + @Override + public void processElement(StreamRecord<RowData> element) { + inputIterator.addRow( + FlinkRowToVLVectorConvertor.fromRowData( + element.getValue(), + allocator, + session, + Serde.fromJson(inputType, RowType.class))); + UpIterator result = session.queryOps().execute(query); Review Comment: `execute` is a very heavy operation because it will recreate the Velox task and all essential components. Given [non-blocking execution is now supported in Velox4J](https://github.com/velox4j/velox4j/pull/93), could try with that approach instead. So only a single time of `execute` call is required then. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
