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

    https://github.com/apache/spark/pull/6627#discussion_r32282420
  
    --- Diff: 
sql/hive/src/main/scala/org/apache/spark/sql/hive/client/HiveShim.scala ---
    @@ -0,0 +1,267 @@
    +/*
    + * 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.client
    +
    +import java.lang.{Boolean => JBoolean, Integer => JInteger}
    +import java.lang.reflect.{Method, Modifier}
    +import java.net.URI
    +import java.util.{ArrayList => JArrayList, List => JList, Map => JMap, Set 
=> JSet}
    +
    +import scala.collection.JavaConversions._
    +
    +import org.apache.hadoop.fs.Path
    +import org.apache.hadoop.hive.conf.HiveConf
    +import org.apache.hadoop.hive.ql.Driver
    +import org.apache.hadoop.hive.ql.metadata.{Hive, Partition, Table}
    +import org.apache.hadoop.hive.ql.processors.{CommandProcessor, 
CommandProcessorFactory}
    +import org.apache.hadoop.hive.ql.session.SessionState
    +
    +/**
    + * A shim that defines the interface between ClientWrapper and the 
underlying Hive library used to
    + * talk to the metastore. Each Hive version has its own implementation of 
this class, defining
    + * version-specific version of needed functions.
    + *
    + * The guideline for writing shims is:
    + * - always extend from the previous version unless really not possible
    + * - initialize methods in lazy vals, both for quicker access for multiple 
invocations, and to
    + *   avoid runtime errors due to the above guideline.
    + */
    +private[client] sealed abstract class Shim {
    +
    +  def setCurrentSessionState(state: SessionState): Unit
    +
    +  /**
    +   * This shim is necessary because the return type is different on 
different versions of Hive.
    +   * All parameters are the same, though.
    +   */
    +  def getDataLocation(table: Table): Option[String]
    +
    +  def setDataLocation(table: Table, loc: String): Unit
    +
    +  def getAllPartitions(hive: Hive, table: Table): Seq[Partition]
    +
    +  def getCommandProcessor(token: String, conf: HiveConf): CommandProcessor
    +
    +  def getDriverResults(driver: Driver): Seq[String]
    +
    +  def loadPartition(
    +      hive: Hive,
    +      loadPath: Path,
    +      tableName: String,
    +      partSpec: JMap[String, String],
    +      replace: Boolean,
    +      holdDDLTime: Boolean,
    +      inheritTableSpecs: Boolean,
    +      isSkewedStoreAsSubdir: Boolean): Unit
    +
    +  def loadTable(
    +      hive: Hive,
    +      loadPath: Path,
    +      tableName: String,
    +      replace: Boolean,
    +      holdDDLTime: Boolean): Unit
    +
    +  def loadDynamicPartitions(
    +      hive: Hive,
    +      loadPath: Path,
    +      tableName: String,
    +      partSpec: JMap[String, String],
    +      replace: Boolean,
    +      numDP: Int,
    +      holdDDLTime: Boolean,
    +      listBucketingEnabled: Boolean): Unit
    +
    +  protected def findStaticMethod(klass: Class[_], name: String, args: 
Class[_]*): Method = {
    +    val method = findMethod(klass, name, args: _*)
    +    require(Modifier.isStatic(method.getModifiers()),
    +      s"Method $name of class $klass is not static.")
    +    method
    +  }
    +
    +  protected def findMethod(klass: Class[_], name: String, args: 
Class[_]*): Method = {
    +    klass.getMethod(name, args: _*)
    +  }
    +
    +}
    +
    +private[client] class Shim_v0_12 extends Shim {
    +
    +  private lazy val startMethod = findStaticMethod(classOf[SessionState], 
"start",
    +    classOf[SessionState])
    +  private lazy val getDataLocationMethod = findMethod(classOf[Table], 
"getDataLocation")
    +  private lazy val setDataLocationMethod = findMethod(classOf[Table], 
"setDataLocation",
    +    classOf[URI])
    +  private lazy val getAllPartitionsMethod = findMethod(classOf[Hive], 
"getAllPartitionsForPruner",
    +    classOf[Table])
    +  private lazy val getCommandProcessorMethod = 
findStaticMethod(classOf[CommandProcessorFactory],
    +    "get", classOf[String], classOf[HiveConf])
    +  private lazy val getDriverResultsMethod = findMethod(classOf[Driver], 
"getResults",
    +    classOf[JArrayList[String]])
    +  private lazy val loadPartitionMethod = findMethod(classOf[Hive], 
"loadPartition", classOf[Path],
    +    classOf[String], classOf[JMap[String, String]], JBoolean.TYPE, 
JBoolean.TYPE,
    +    JBoolean.TYPE, JBoolean.TYPE)
    +  private lazy val loadTableMethod = findMethod(classOf[Hive], 
"loadTable", classOf[Path],
    +    classOf[String], JBoolean.TYPE, JBoolean.TYPE)
    +  private lazy val loadDynamicPartitionsMethod = findMethod(classOf[Hive], 
"loadDynamicPartitions",
    +    classOf[Path], classOf[String], classOf[JMap[String, String]], 
JBoolean.TYPE,
    +    JInteger.TYPE, JBoolean.TYPE, JBoolean.TYPE)
    +
    +  override def setCurrentSessionState(state: SessionState): Unit = 
startMethod.invoke(null, state)
    +
    +  override def getDataLocation(table: Table): Option[String] =
    +    Option(getDataLocationMethod.invoke(table)).map(_.toString())
    +
    +  override def setDataLocation(table: Table, loc: String): Unit =
    +    setDataLocationMethod.invoke(table, new URI(loc))
    +
    +  override def getAllPartitions(hive: Hive, table: Table): Seq[Partition] =
    +    getAllPartitionsMethod.invoke(hive, 
table).asInstanceOf[JSet[Partition]].toSeq
    +
    +  override def getCommandProcessor(token: String, conf: HiveConf): 
CommandProcessor =
    +    getCommandProcessorMethod.invoke(null, token, 
conf).asInstanceOf[CommandProcessor]
    +
    +  override def getDriverResults(driver: Driver): Seq[String] = {
    +    val res = new JArrayList[String]()
    +    getDriverResultsMethod.invoke(driver, res)
    +    res.toSeq
    +  }
    +
    +  override def loadPartition(
    +      hive: Hive,
    +      loadPath: Path,
    +      tableName: String,
    +      partSpec: JMap[String, String],
    +      replace: Boolean,
    +      holdDDLTime: Boolean,
    +      inheritTableSpecs: Boolean,
    +      isSkewedStoreAsSubdir: Boolean): Unit = {
    +    loadPartitionMethod.invoke(hive, loadPath, tableName, partSpec, 
replace: JBoolean,
    +      holdDDLTime: JBoolean, inheritTableSpecs: JBoolean, 
isSkewedStoreAsSubdir: JBoolean)
    +  }
    +
    +  override def loadTable(
    +      hive: Hive,
    +      loadPath: Path,
    +      tableName: String,
    +      replace: Boolean,
    +      holdDDLTime: Boolean): Unit = {
    +    loadTableMethod.invoke(hive, loadPath, tableName, replace: JBoolean, 
holdDDLTime: JBoolean)
    +  }
    +
    +  override def loadDynamicPartitions(
    +      hive: Hive,
    +      loadPath: Path,
    +      tableName: String,
    +      partSpec: JMap[String, String],
    +      replace: Boolean,
    +      numDP: Int,
    +      holdDDLTime: Boolean,
    +      listBucketingEnabled: Boolean): Unit = {
    +    loadDynamicPartitionsMethod.invoke(hive, loadPath, tableName, 
partSpec, replace: JBoolean,
    +      numDP: JInteger, holdDDLTime: JBoolean, listBucketingEnabled: 
JBoolean)
    +  }
    +
    +}
    +
    +private[client] class Shim_v0_13 extends Shim_v0_12 {
    +
    +  private lazy val setCurrentSessionStateMethod =
    +    findStaticMethod(classOf[SessionState], "setCurrentSessionState", 
classOf[SessionState])
    +  private lazy val setDataLocationMethod = findMethod(classOf[Table], 
"setDataLocation",
    +    classOf[Path])
    --- End diff --
    
    Changes it, hope I got the style right. Looks a bit noisy though.


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