Github user liancheng commented on a diff in the pull request:
https://github.com/apache/spark/pull/2576#discussion_r18769955
--- Diff:
sql/hive/src/main/scala/org/apache/spark/sql/hive/orc/OrcRelation.scala ---
@@ -0,0 +1,248 @@
+/*
+ * 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.hive.orc
+
+import java.util.Properties
+import java.io.IOException
+import org.apache.hadoop.hive.ql.stats.StatsSetupConst
+
+import scala.collection.mutable
+
+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.hive.ql.io.orc.OrcProto.Type.Kind
+
+import org.apache.spark.sql.parquet.FileSystemHelper
+import org.apache.spark.sql.SQLContext
+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._
+
+
+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
+
+ override lazy val statistics = Statistics(sizeInBytes =
sqlContext.defaultSizeInBytes)
+
+ def orcSchema: Seq[Attribute] = {
+ val origPath = new Path(path)
+ val reader = OrcFileOperator.readMetaData(origPath, conf)
+
+ 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 {
+ /**
+ * 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,
+ child: LogicalPlan,
+ conf: Configuration,
+ sqlContext: SQLContext): OrcRelation = {
+ if (!child.resolved) {
+ throw new UnresolvedException[LogicalPlan](
+ child,
+ "Attempt to create Orc table from unresolved child")
+ }
+ createEmpty(pathString, child.output, false, conf, sqlContext)
+ }
+
+ /**
+ * Creates an empty OrcRelation and underlying Orcfile that only
+ * consists of the Metadata for the given schema.
+ *
+ * @param pathString The directory the Orcfile will be stored in.
+ * @param attributes The schema of the relation.
+ * @param conf A configuration to be used.
+ * @return An empty OrcRelation.
+ */
+ def createEmpty(
+ pathString: String,
+ attributes: Seq[Attribute],
+ allowExisting: Boolean,
+ conf: Configuration,
+ sqlContext: SQLContext): OrcRelation = {
+ val path = checkPath(pathString, allowExisting, conf)
+
+ /** set compression kind in hive 0.13.1
+ * conf.set(
+ * HiveConf.ConfVars.OHIVE_ORC_DEFAULT_COMPRESS.varname,
+ * shortOrcCompressionCodecNames.getOrElse(
+ * sqlContext.orcCompressionCodec.toUpperCase,
CompressionKind.NONE).name)
+ */
+ val orcRelation = new OrcRelation(path.toString, Some(conf),
sqlContext)
+
+ orcRelation
--- End diff --
Don't need this temporary variable.
---
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 [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]