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

    https://github.com/apache/spark/pull/13775#discussion_r74180849
  
    --- Diff: 
sql/hive/src/main/java/org/apache/hadoop/hive/ql/io/orc/VectorizedSparkOrcNewRecordReader.java
 ---
    @@ -0,0 +1,317 @@
    +/*
    + * 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.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 = columnIDs;
    +    this.internalValue = this.reader.createValue();
    +    this.progress = reader.getProgress();
    +    this.row = new Row(this.internalValue.cols, 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 && progress < 1.0f) {
    +      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(); }
    --- End diff --
    
    I've done some profiling of this code (repeated jstack traces on a job that 
scans the table), and was surprised to see this call taking around 30% of CPU 
time. Each time .size() is called, the scala sequence to java list conversion 
has to take place. Other calls that involve columnIDs also took about 9% of CPU 
time.
    
    I made a change to avoid this conversion, and saw the CPU time spent in 
these calls reduced to effectively zero. The change was to make a (java) copy 
of the columnIDs in the constructor of VectorizedSparkOrcNewRecordReader. I 
replaced `this.columnIDs = columnIDs` at L75 with `this.columnIDs = new 
ArrayList<>(columnIDs)` and `columnIDs` at L78 with `this.columnIDs`.


---
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