Github user tejasapatil commented on a diff in the pull request:

    https://github.com/apache/spark/pull/13775#discussion_r83761710
  
    --- Diff: 
sql/hive/src/main/java/org/apache/hadoop/hive/ql/io/orc/VectorizedSparkOrcNewRecordReader.java
 ---
    @@ -0,0 +1,318 @@
    +/*
    + * 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.hadoop.hive.ql.io.orc;
    +
    +import java.io.IOException;
    +import java.nio.charset.StandardCharsets;
    +import java.util.ArrayList;
    +import java.util.List;
    +
    +import org.apache.commons.lang.NotImplementedException;
    +
    +import org.apache.hadoop.conf.Configuration;
    +import org.apache.hadoop.hive.ql.exec.vector.ColumnVector;
    +import org.apache.hadoop.hive.ql.exec.vector.BytesColumnVector;
    +import org.apache.hadoop.hive.ql.exec.vector.DecimalColumnVector;
    +import org.apache.hadoop.hive.ql.exec.vector.DoubleColumnVector;
    +import org.apache.hadoop.hive.ql.exec.vector.LongColumnVector;
    +import org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch;
    +import org.apache.hadoop.io.NullWritable;
    +import org.apache.hadoop.mapred.JobConf;
    +import org.apache.hadoop.mapreduce.InputSplit;
    +import org.apache.hadoop.mapreduce.TaskAttemptContext;
    +import org.apache.hadoop.mapreduce.lib.input.FileSplit;
    +
    +import org.apache.spark.sql.catalyst.InternalRow;
    +import org.apache.spark.sql.catalyst.util.ArrayData;
    +import org.apache.spark.sql.catalyst.util.MapData;
    +import org.apache.spark.sql.types.DataType;
    +import org.apache.spark.sql.types.Decimal;
    +import org.apache.spark.unsafe.types.CalendarInterval;
    +import org.apache.spark.unsafe.types.UTF8String;
    +
    +/**
    + * A RecordReader that returns InternalRow for Spark SQL execution.
    + * This reader uses an internal reader that returns Hive's 
VectorizedRowBatch. An adapter
    + * class is used to return internal row by directly accessing data in 
column vectors.
    + */
    +public class VectorizedSparkOrcNewRecordReader
    +    extends org.apache.hadoop.mapreduce.RecordReader<NullWritable, 
InternalRow> {
    +  private final org.apache.hadoop.mapred.RecordReader<NullWritable, 
VectorizedRowBatch> reader;
    +  private final int numColumns;
    +  private VectorizedRowBatch internalValue;
    +  private float progress = 0.0f;
    +  private List<Integer> columnIDs;
    +
    +  private long numRowsOfBatch = 0;
    +  private int indexOfRow = 0;
    +
    +  private final Row row;
    +
    +  public VectorizedSparkOrcNewRecordReader(
    +      Reader file,
    +      JobConf conf,
    +      FileSplit fileSplit,
    +      List<Integer> columnIDs) throws IOException {
    +    List<OrcProto.Type> types = file.getTypes();
    +    numColumns = (types.size() == 0) ? 0 : types.get(0).getSubtypesCount();
    +    this.reader = new SparkVectorizedOrcRecordReader(file, conf,
    +      new org.apache.hadoop.mapred.FileSplit(fileSplit));
    +
    +    this.columnIDs = new ArrayList<>(columnIDs);
    +    this.internalValue = this.reader.createValue();
    +    this.progress = reader.getProgress();
    +    this.row = new Row(this.internalValue.cols, this.columnIDs);
    +  }
    +
    +  @Override
    +  public void close() throws IOException {
    +    reader.close();
    +  }
    +
    +  @Override
    +  public NullWritable getCurrentKey() throws IOException,
    +      InterruptedException {
    +    return NullWritable.get();
    +  }
    +
    +  @Override
    +  public InternalRow getCurrentValue() throws IOException,
    +      InterruptedException {
    +    if (indexOfRow >= numRowsOfBatch) {
    +      return null;
    +    }
    +    row.rowId = indexOfRow;
    +    indexOfRow++;
    +
    +    return row;
    +  }
    +
    +  @Override
    +  public float getProgress() throws IOException, InterruptedException {
    +    return progress;
    +  }
    +
    +  @Override
    +  public void initialize(InputSplit split, TaskAttemptContext context)
    +      throws IOException, InterruptedException {
    +  }
    +
    +  @Override
    +  public boolean nextKeyValue() throws IOException, InterruptedException {
    +    if (indexOfRow == numRowsOfBatch) {
    +      if (reader.next(NullWritable.get(), internalValue)) {
    +        if (internalValue.endOfFile) {
    +          progress = 1.0f;
    +          numRowsOfBatch = 0;
    +          indexOfRow = 0;
    +          return false;
    +        } else {
    +          assert internalValue.numCols == numColumns : "Incorrect number 
of columns in OrcBatch";
    +          numRowsOfBatch = internalValue.count();
    +          indexOfRow = 0;
    +          progress = reader.getProgress();
    +        }
    +        return true;
    +      } else {
    +        return false;
    +      }
    +    } else {
    +      if (indexOfRow < numRowsOfBatch) {
    +        return true;
    +      } else {
    +        return false;
    +      }
    +    }
    +  }
    +
    +  /**
    +   * Adapter class to return an internal row.
    +   */
    +  public static final class Row extends InternalRow {
    +    protected int rowId;
    +    private List<Integer> columnIDs;
    +    private final ColumnVector[] columns;
    +
    +    private Row(ColumnVector[] columns, List<Integer> columnIDs) {
    +      this.columns = columns;
    +      this.columnIDs = columnIDs;
    +    }
    +
    +    @Override
    +    public int numFields() { return columnIDs.size(); }
    +
    +    @Override
    +    public boolean anyNull() {
    +      for (int i = 0; i < columns.length; i++) {
    +        if (columnIDs.contains(i)) {
    +          if (columns[i].isRepeating && columns[i].isNull[0]) {
    +            return true;
    +          } else if (!columns[i].isRepeating && columns[i].isNull[rowId]) {
    +            return true;
    +          }
    +        }
    +      }
    +      return false;
    +    }
    +
    +    @Override
    +    public boolean isNullAt(int ordinal) {
    +      ColumnVector col = columns[columnIDs.get(ordinal)];
    +      if (col.isRepeating) {
    +        return col.isNull[0];
    +      } else {
    +        return col.isNull[rowId];
    +      }
    +    }
    +
    +    @Override
    +    public boolean getBoolean(int ordinal) {
    +      LongColumnVector col = 
(LongColumnVector)columns[columnIDs.get(ordinal)];
    +      if (col.isRepeating) {
    +        return col.vector[0] > 0;
    +      } else {
    +        return col.vector[rowId] > 0;
    +      }
    +    }
    +
    +    @Override
    +    public byte getByte(int ordinal) {
    +      LongColumnVector col = 
(LongColumnVector)columns[columnIDs.get(ordinal)];
    +      if (col.isRepeating) {
    +        return (byte)col.vector[0];
    +      } else {
    +        return (byte)col.vector[rowId];
    +      }
    +    }
    +
    +    @Override
    +    public short getShort(int ordinal) {
    +      LongColumnVector col = 
(LongColumnVector)columns[columnIDs.get(ordinal)];
    +      if (col.isRepeating) {
    +        return (short)col.vector[0];
    +      } else {
    +        return (short)col.vector[rowId];
    +      }
    +    }
    +
    +    @Override
    +    public int getInt(int ordinal) {
    +      LongColumnVector col = 
(LongColumnVector)columns[columnIDs.get(ordinal)];
    +      if (col.isRepeating) {
    +        return (int)col.vector[0];
    +      } else {
    +        return (int)col.vector[rowId];
    +      }
    +    }
    +
    +    @Override
    +    public long getLong(int ordinal) {
    +      LongColumnVector col = 
(LongColumnVector)columns[columnIDs.get(ordinal)];
    +      if (col.isRepeating) {
    +        return (long)col.vector[0];
    +      } else {
    +        return (long)col.vector[rowId];
    +      }
    +    }
    +
    +    @Override
    +    public float getFloat(int ordinal) {
    +      DoubleColumnVector col = 
(DoubleColumnVector)columns[columnIDs.get(ordinal)];
    +      if (col.isRepeating) {
    +        return (float)col.vector[0];
    +      } else {
    +        return (float)col.vector[rowId];
    +      }
    +    }
    +
    +    @Override
    +    public double getDouble(int ordinal) {
    +      DoubleColumnVector col = 
(DoubleColumnVector)columns[columnIDs.get(ordinal)];
    +      if (col.isRepeating) {
    +        return (double)col.vector[0];
    +      } else {
    +        return (double)col.vector[rowId];
    +      }
    +    }
    +
    +    @Override
    +    public Decimal getDecimal(int ordinal, int precision, int scale) {
    +      DecimalColumnVector col = 
(DecimalColumnVector)columns[columnIDs.get(ordinal)];
    +      if (col.isRepeating) {
    +        return 
Decimal.apply(col.vector[0].getHiveDecimal().bigDecimalValue(), precision, 
scale);
    +      } else {
    +        return 
Decimal.apply(col.vector[rowId].getHiveDecimal().bigDecimalValue(),
    +          precision, scale);
    +      }
    +    }
    +
    +    @Override
    +    public UTF8String getUTF8String(int ordinal) {
    +      BytesColumnVector bv = 
((BytesColumnVector)columns[columnIDs.get(ordinal)]);
    +      if (bv.isRepeating) {
    +        return UTF8String.fromBytes(bv.vector[0], bv.start[0], 
bv.length[0]);
    +      } else {
    +        return UTF8String.fromBytes(bv.vector[rowId], bv.start[rowId], 
bv.length[rowId]);
    +      }
    +    }
    +
    +    @Override
    +    public byte[] getBinary(int ordinal) {
    +      BytesColumnVector col = 
(BytesColumnVector)columns[columnIDs.get(ordinal)];
    +      if (col.isRepeating) {
    +        byte[] binary = new byte[col.length[0]];
    +        System.arraycopy(col.vector[0], col.start[0], binary, 0, 
binary.length);
    +        return binary;
    +      } else {
    +        byte[] binary = new byte[col.length[rowId]];
    +        System.arraycopy(col.vector[rowId], col.start[rowId], binary, 0, 
binary.length);
    +        return binary;
    +      }
    +    }
    +
    +    @Override
    +    public CalendarInterval getInterval(int ordinal) {
    +      throw new NotImplementedException();
    --- End diff --
    
    I think the hive artifact used by Spark does not support vectorisation for 
all the datatypes and thats why this is not being implemented. In future, 
upgrading hive in Spark might make it possible to add it. Can you add a comment 
about this ?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

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

Reply via email to