vinson0526 commented on a change in pull request #2228: add 
spark-doris-connector extension
URL: https://github.com/apache/incubator-doris/pull/2228#discussion_r348332359
 
 

 ##########
 File path: 
extension/spark-doris-connector/src/main/java/org/apache/doris/spark/serialization/RowBatch.java
 ##########
 @@ -0,0 +1,273 @@
+// 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.doris.spark.serialization;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.sql.Date;
+import java.sql.Timestamp;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.arrow.memory.RootAllocator;
+import org.apache.arrow.vector.BigIntVector;
+import org.apache.arrow.vector.BitVector;
+import org.apache.arrow.vector.DateMilliVector;
+import org.apache.arrow.vector.DecimalVector;
+import org.apache.arrow.vector.FieldVector;
+import org.apache.arrow.vector.Float4Vector;
+import org.apache.arrow.vector.Float8Vector;
+import org.apache.arrow.vector.IntVector;
+import org.apache.arrow.vector.SmallIntVector;
+import org.apache.arrow.vector.TinyIntVector;
+import org.apache.arrow.vector.VarBinaryVector;
+import org.apache.arrow.vector.VarCharVector;
+import org.apache.arrow.vector.VectorSchemaRoot;
+import org.apache.arrow.vector.ipc.ArrowStreamReader;
+import org.apache.arrow.vector.types.Types;
+import org.apache.doris.spark.exception.DorisException;
+import org.apache.doris.spark.rest.models.Schema;
+import org.apache.doris.thrift.TScanBatchResult;
+import org.apache.spark.sql.types.Decimal;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Preconditions;
+
+/**
+ * row batch data container.
+ */
+public class RowBatch {
+    private static Logger logger = LoggerFactory.getLogger(RowBatch.class);
+
+    public static class Row {
+        private List<Object> cols;
+
+        Row(int colCount) {
+            this.cols = new ArrayList<>(colCount);
+        }
+
+        List<Object> getCols() {
+            return cols;
+        }
+
+        public void put(Object o) {
+            cols.add(o);
+        }
+    }
+
+    private int offsetInOneBatch = 0;
+    private int rowCountInOneBatch = 0;
+    private int readRowCount = 0;
+    private final ArrowStreamReader arrowStreamReader;
+    private final VectorSchemaRoot root;
+    private List<FieldVector> fieldVectors;
+    private RootAllocator rootAllocator;
+    private final Schema schema;
+
+    public RowBatch(TScanBatchResult nextResult, Schema schema) throws 
DorisException {
+        logger.trace("Generate Row Batch");
+        this.schema = schema;
+        this.rootAllocator = new RootAllocator(Integer.MAX_VALUE);
+        this.arrowStreamReader = new ArrowStreamReader(
+                new ByteArrayInputStream(nextResult.getRows()),
+                rootAllocator
+                );
+        try {
+            this.root = arrowStreamReader.getVectorSchemaRoot();
+        } catch (Exception e) {
+            logger.error("Read Doris Data failed because: ", e);
+            close();
+            throw new DorisException(e.getMessage());
+        }
+    }
+
+    public boolean hasNext() throws DorisException {
+        if (offsetInOneBatch < rowCountInOneBatch) {
+            return true;
+        }
+        try {
+            try {
+                while (arrowStreamReader.loadNextBatch()) {
+                    fieldVectors = root.getFieldVectors();
+                    readRowCount += root.getRowCount();
+                    if (fieldVectors.size() != schema.size()) {
+                        logger.error("Schema size '{}' is not equal to arrow 
field size '{}'.",
+                                fieldVectors.size(), schema.size());
+                        throw new DorisException("Load Doris data failed, 
schema size of fetch data is wrong.");
+                    }
+                    if (fieldVectors.size() == 0 || root.getRowCount() == 0) {
+                        logger.debug("One batch in arrow has no data.");
+                        continue;
+                    }
+                    offsetInOneBatch = 0;
+                    rowCountInOneBatch = root.getRowCount();
+                    return true;
+                }
+            } catch (IOException e) {
+                logger.error("Load arrow next batch failed.", e);
+                throw new DorisException("Cannot load arrow next batch 
fetching from Doris.");
+            }
+        } catch (Exception e) {
+            close();
+            throw e;
+        }
+        return false;
+    }
+
+    public List<Object> next() throws DorisException {
+        try {
+            if (!hasNext()) {
+                String errMsg = "Get row offset:" + offsetInOneBatch + " 
larger than row size: " + rowCountInOneBatch;
+                logger.error(errMsg);
+                throw new IndexOutOfBoundsException(errMsg);
 
 Review comment:
   NoSuchElementException should better than IndexOutOfBoundsException.
   HaxNext return false means no more data in RowBatch.
   RowBatch acts like an iterator. So caller should call hasNext first. if 
hasNext return true, call next to get data from RowBatch. 

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to