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

    https://github.com/apache/spark/pull/2576#discussion_r18299730
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/orc/OrcRelation.scala ---
    @@ -0,0 +1,263 @@
    +/*
    + * 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.spark.sql.orc
    +
    +import org.apache.hadoop.hive.conf.HiveConf
    +import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, LeafNode}
    +import org.apache.spark.sql.catalyst.analysis.{UnresolvedException, 
MultiInstanceRelation}
    +import org.apache.spark.sql.catalyst.expressions.Attribute
    +import org.apache.spark.sql.catalyst.expressions.AttributeReference
    +import org.apache.spark.sql.catalyst.types._
    +import org.apache.hadoop.fs.{FileSystem, Path}
    +import org.apache.hadoop.conf.Configuration
    +import org.apache.hadoop.fs.permission.FsAction
    +import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector
    +import org.apache.hadoop.hive.ql.io.orc._
    +import org.apache.hadoop.mapred.{FileInputFormat => NewFileInputFormat, 
JobConf}
    +import org.apache.hadoop.hive.ql.io.orc.OrcProto.Type.Kind
    +import org.apache.hadoop.mapreduce.Job
    +import parquet.hadoop.util.ContextUtil
    +import java.util.Properties
    +import java.io.IOException
    +import scala.collection.mutable
    +import org.apache.spark.sql.SQLContext
    +
    +
    +private[sql] case class OrcRelation(
    +    path: String,
    +    @transient conf: Option[Configuration],
    +    @transient sqlContext: SQLContext,
    +    partitioningAttributes: Seq[Attribute] = Nil)
    +  extends LeafNode with MultiInstanceRelation {
    +  self: Product =>
    +
    +  val prop: Properties = new Properties
    +
    +  var rowClass: Class[_] = null
    +
    +  val fieldIdCache: mutable.Map[String, Int] = new mutable.HashMap[String, 
Int]
    +
    +  val fieldNameTypeCache: mutable.Map[String, String] = new 
mutable.HashMap[String, String]
    +
    +  override val output = orcSchema
    +
    +  def orcSchema: Seq[Attribute] = {
    +    val origPath = new Path(path)
    +    val reader = OrcFileOperator.readMetaData(origPath)
    +
    +    if (null != reader) {
    +      val inspector = 
reader.getObjectInspector.asInstanceOf[StructObjectInspector]
    +      val fields = inspector.getAllStructFieldRefs
    +
    +      if (fields.size() == 0) {
    +        return Seq.empty
    +      }
    +
    +      val totalType = reader.getTypes.get(0)
    +      val keys = totalType.getFieldNamesList
    +      val types = totalType.getSubtypesList
    +      log.info("field name is {}", keys)
    +      log.info("types is {}", types)
    +
    +      val colBuff = new StringBuilder
    +      val typeBuff = new StringBuilder
    +      for (i <- 0 until fields.size()) {
    +        val fieldName = fields.get(i).getFieldName
    +        val typeName = fields.get(i).getFieldObjectInspector.getTypeName
    +        colBuff.append(fieldName)
    +        fieldNameTypeCache.put(fieldName, typeName)
    +        fieldIdCache.put(fieldName, i)
    +        colBuff.append(",")
    +        typeBuff.append(typeName)
    +        typeBuff.append(":")
    +      }
    +      colBuff.setLength(colBuff.length - 1)
    +      typeBuff.setLength(typeBuff.length - 1)
    +      prop.setProperty("columns", colBuff.toString())
    +      prop.setProperty("columns.types", typeBuff.toString())
    +      val attributes = convertToAttributes(reader, keys, types)
    +      attributes
    +    } else {
    +      Seq.empty
    +    }
    +  }
    +
    +  def convertToAttributes(
    +     reader: Reader,
    +     keys: java.util.List[String],
    +     types: java.util.List[Integer]): Seq[Attribute] = {
    +    val range = 0.until(keys.size())
    +    range.map {
    +      i => reader.getTypes.get(types.get(i)).getKind match {
    +        case Kind.BOOLEAN =>
    +          new AttributeReference(keys.get(i), BooleanType, false)()
    +        case Kind.STRING =>
    +          new AttributeReference(keys.get(i), StringType, true)()
    +        case Kind.BYTE =>
    +          new AttributeReference(keys.get(i), ByteType, true)()
    +        case Kind.SHORT =>
    +          new AttributeReference(keys.get(i), ShortType, true)()
    +        case Kind.INT =>
    +          new AttributeReference(keys.get(i), IntegerType, true)()
    +        case Kind.LONG =>
    +          new AttributeReference(keys.get(i), LongType, false)()
    +        case Kind.FLOAT =>
    +          new AttributeReference(keys.get(i), FloatType, false)()
    +        case Kind.DOUBLE =>
    +          new AttributeReference(keys.get(i), DoubleType, false)()
    +        case _ => {
    +          log.info("unsupported datatype")
    +          null
    +        }
    +      }
    +    }
    +  }
    +
    +  override def newInstance() = OrcRelation(path, conf, 
sqlContext).asInstanceOf[this.type]
    +}
    +
    +private[sql] object OrcRelation {
    +
    +
    +  // The orc compression short names
    +  val shortOrcCompressionCodecNames = Map(
    +    "NONE"         -> CompressionKind.NONE,
    +    "UNCOMPRESSED" -> CompressionKind.NONE,
    +    "SNAPPY"       -> CompressionKind.SNAPPY,
    +    "ZLIB"         -> CompressionKind.ZLIB,
    +    "LZO"          -> CompressionKind.LZO)
    +
    +  /**
    +   * Creates a new OrcRelation and underlying Orcfile for the given 
LogicalPlan. Note that
    +   * this is used inside 
[[org.apache.spark.sql.execution.SparkStrategies]] to
    +   * create a resolved relation as a data sink for writing to a Orcfile.
    +   *
    +   * @param pathString The directory the ORCfile will be stored in.
    +   * @param child The child node that will be used for extracting the 
schema.
    +   * @param conf A configuration to be used.
    +   * @return An empty OrcRelation with inferred metadata.
    +   */
    +  def create(pathString: String,
    --- End diff --
    
    Arguments should be indented 4 spaces from the `def`.


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