Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,8 @@ object CatalogTableType {
val EXTERNAL = new CatalogTableType("EXTERNAL")
val MANAGED = new CatalogTableType("MANAGED")
val VIEW = new CatalogTableType("VIEW")

val tableTypes = Seq(EXTERNAL, MANAGED, VIEW)
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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.thriftserver

import java.util.UUID

import org.apache.hadoop.hive.ql.security.authorization.plugin.HiveOperationType
import org.apache.hive.service.cli._
import org.apache.hive.service.cli.operation.GetTableTypesOperation
import org.apache.hive.service.cli.session.HiveSession

import org.apache.spark.internal.Logging
import org.apache.spark.sql.SQLContext
import org.apache.spark.sql.catalyst.catalog.CatalogTableType
import org.apache.spark.util.{Utils => SparkUtils}

/**
* Spark's own GetTableTypesOperation
*
* @param sqlContext SQLContext to use
* @param parentSession a HiveSession from SessionManager
*/
private[hive] class SparkGetTableTypesOperation(
sqlContext: SQLContext,
parentSession: HiveSession)
extends GetTableTypesOperation(parentSession) with SparkMetadataOperationUtils with Logging {

private var statementId: String = _

override def close(): Unit = {
super.close()
HiveThriftServer2.listener.onOperationClosed(statementId)
}

override def runInternal(): Unit = {
statementId = UUID.randomUUID().toString
val logMsg = "Listing table types"
logInfo(s"$logMsg with $statementId")
setState(OperationState.RUNNING)
// Always use the latest class loader provided by executionHive's state.
val executionHiveClassLoader = sqlContext.sharedState.jarClassLoader
Thread.currentThread().setContextClassLoader(executionHiveClassLoader)

if (isAuthV2Enabled) {
authorizeMetaGets(HiveOperationType.GET_TABLETYPES, null)
}

HiveThriftServer2.listener.onStatementStart(
statementId,
parentSession.getSessionHandle.getSessionId.toString,
logMsg,
statementId,
parentSession.getUsername)

try {
val tableTypes = CatalogTableType.tableTypes.map(tableTypeString).toSet
tableTypes.foreach { tableType =>
rowSet.addRow(Array[AnyRef](tableType))
}
setState(OperationState.FINISHED)
} catch {
case e: HiveSQLException =>
setState(OperationState.ERROR)
HiveThriftServer2.listener.onStatementError(
statementId, e.getMessage, SparkUtils.exceptionString(e))
throw e
}
HiveThriftServer2.listener.onStatementFinish(statementId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private[hive] class SparkGetTablesOperation(
tableName: String,
tableTypes: JList[String])
extends GetTablesOperation(parentSession, catalogName, schemaName, tableName, tableTypes)
with Logging{
with SparkMetadataOperationUtils with Logging {

private var statementId: String = _

Expand Down Expand Up @@ -146,11 +146,4 @@ private[hive] class SparkGetTablesOperation(
rowSet.addRow(rowData)
}
}

private def tableTypeString(tableType: CatalogTableType): String = tableType match {
case EXTERNAL | MANAGED => "TABLE"
case VIEW => "VIEW"
case t =>
throw new IllegalArgumentException(s"Unknown table type is found at showCreateHiveTable: $t")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.thriftserver

import org.apache.spark.sql.catalyst.catalog.CatalogTableType
import org.apache.spark.sql.catalyst.catalog.CatalogTableType.{EXTERNAL, MANAGED, VIEW}

/**
* Utils for metadata operations.
*/
private[hive] trait SparkMetadataOperationUtils {

def tableTypeString(tableType: CatalogTableType): String = tableType match {
case EXTERNAL | MANAGED => "TABLE"
case VIEW => "VIEW"
case t =>
throw new IllegalArgumentException(s"Unknown table type is found: $t")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ import java.util.{List => JList, Map => JMap}
import java.util.concurrent.ConcurrentHashMap

import org.apache.hive.service.cli._
import org.apache.hive.service.cli.operation.{ExecuteStatementOperation, GetColumnsOperation, GetSchemasOperation, MetadataOperation, Operation, OperationManager}
import org.apache.hive.service.cli.operation._
import org.apache.hive.service.cli.session.HiveSession

import org.apache.spark.internal.Logging
import org.apache.spark.sql.SQLContext
import org.apache.spark.sql.hive.HiveUtils
import org.apache.spark.sql.hive.thriftserver.{ReflectionUtils, SparkExecuteStatementOperation, SparkGetColumnsOperation, SparkGetSchemasOperation, SparkGetTablesOperation}
import org.apache.spark.sql.hive.thriftserver._
import org.apache.spark.sql.internal.SQLConf

/**
Expand Down Expand Up @@ -100,14 +100,25 @@ private[thriftserver] class SparkSQLOperationManager()
columnName: String): GetColumnsOperation = synchronized {
val sqlContext = sessionToContexts.get(parentSession.getSessionHandle)
require(sqlContext != null, s"Session handle: ${parentSession.getSessionHandle} has not been" +
s" initialized or had already closed.")
" initialized or had already closed.")
val operation = new SparkGetColumnsOperation(sqlContext, parentSession,
catalogName, schemaName, tableName, columnName)
handleToOperation.put(operation.getHandle, operation)
logDebug(s"Created GetColumnsOperation with session=$parentSession.")
operation
}

override def newGetTableTypesOperation(
parentSession: HiveSession): GetTableTypesOperation = synchronized {
val sqlContext = sessionToContexts.get(parentSession.getSessionHandle)
require(sqlContext != null, s"Session handle: ${parentSession.getSessionHandle} has not been" +
" initialized or had already closed.")
val operation = new SparkGetTableTypesOperation(sqlContext, parentSession)
handleToOperation.put(operation.getHandle, operation)
logDebug(s"Created GetTableTypesOperation with session=$parentSession.")
operation
}

def setConfMap(conf: SQLConf, confMap: java.util.Map[String, String]): Unit = {
val iterator = confMap.entrySet().iterator()
while (iterator.hasNext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,20 @@ class SparkMetadataOperationSuite extends HiveThriftJdbcTest {
checkResult(metaData.getColumns(null, "%", "table_not_exist", null), Seq.empty)
}
}

test("Spark's own GetTableTypesOperation(SparkGetTableTypesOperation)") {
def checkResult(rs: ResultSet, tableTypes: Seq[String]): Unit = {
for (i <- tableTypes.indices) {
assert(rs.next())
assert(rs.getString("TABLE_TYPE") === tableTypes(i))
}
// Make sure there are no more elements
assert(!rs.next())
}

withJdbcStatement() { statement =>
val metaData = statement.getConnection.getMetaData
checkResult(metaData.getTableTypes, Seq("TABLE", "VIEW"))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class GetTableTypesOperation extends MetadataOperation {
protected static TableSchema RESULT_SET_SCHEMA = new TableSchema()
.addStringColumn("TABLE_TYPE", "Table type name.");

private final RowSet rowSet;
protected final RowSet rowSet;
private final TableTypeMapping tableTypeMapping;

protected GetTableTypesOperation(HiveSession parentSession) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class GetTableTypesOperation extends MetadataOperation {
protected static TableSchema RESULT_SET_SCHEMA = new TableSchema()
.addStringColumn("TABLE_TYPE", "Table type name.");

private final RowSet rowSet;
protected final RowSet rowSet;
private final TableTypeMapping tableTypeMapping;

protected GetTableTypesOperation(HiveSession parentSession) {
Expand Down