-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-24570][SQL] Implement Spark own GetTablesOperation to fix SQL client tools cannot show tables #22794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-24570][SQL] Implement Spark own GetTablesOperation to fix SQL client tools cannot show tables #22794
Changes from all commits
9f528bc
2939178
8449cdf
e9a2a93
80a8d21
80bddb8
4f5cd27
fb7e0a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| /* | ||
| * 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.{List => JList} | ||
|
|
||
| import scala.collection.JavaConverters.seqAsJavaListConverter | ||
|
|
||
| import org.apache.hadoop.hive.ql.security.authorization.plugin.HiveOperationType | ||
| import org.apache.hadoop.hive.ql.security.authorization.plugin.HivePrivilegeObjectUtils | ||
| import org.apache.hive.service.cli._ | ||
| import org.apache.hive.service.cli.operation.GetTablesOperation | ||
| import org.apache.hive.service.cli.session.HiveSession | ||
|
|
||
| import org.apache.spark.sql.SQLContext | ||
| import org.apache.spark.sql.catalyst.catalog.CatalogTableType | ||
| import org.apache.spark.sql.catalyst.catalog.CatalogTableType._ | ||
|
|
||
| /** | ||
| * Spark's own GetTablesOperation | ||
| * | ||
| * @param sqlContext SQLContext to use | ||
| * @param parentSession a HiveSession from SessionManager | ||
| * @param catalogName catalog name. null if not applicable | ||
| * @param schemaName database name, null or a concrete database name | ||
| * @param tableName table name pattern | ||
| * @param tableTypes list of allowed table types, e.g. "TABLE", "VIEW" | ||
| */ | ||
| private[hive] class SparkGetTablesOperation( | ||
| sqlContext: SQLContext, | ||
| parentSession: HiveSession, | ||
| catalogName: String, | ||
| schemaName: String, | ||
| tableName: String, | ||
| tableTypes: JList[String]) | ||
| extends GetTablesOperation(parentSession, catalogName, schemaName, tableName, tableTypes) { | ||
|
|
||
| if (tableTypes != null) { | ||
| this.tableTypes.addAll(tableTypes) | ||
| } | ||
|
|
||
| override def runInternal(): Unit = { | ||
| setState(OperationState.RUNNING) | ||
| // Always use the latest class loader provided by executionHive's state. | ||
| val executionHiveClassLoader = sqlContext.sharedState.jarClassLoader | ||
| Thread.currentThread().setContextClassLoader(executionHiveClassLoader) | ||
|
|
||
| val catalog = sqlContext.sessionState.catalog | ||
| val schemaPattern = convertSchemaPattern(schemaName) | ||
| val matchingDbs = catalog.listDatabases(schemaPattern) | ||
|
|
||
| if (isAuthV2Enabled) { | ||
| val privObjs = | ||
| HivePrivilegeObjectUtils.getHivePrivDbObjects(seqAsJavaListConverter(matchingDbs).asJava) | ||
| val cmdStr = s"catalog : $catalogName, schemaPattern : $schemaName" | ||
| authorizeMetaGets(HiveOperationType.GET_TABLES, privObjs, cmdStr) | ||
| } | ||
|
|
||
| val tablePattern = convertIdentifierPattern(tableName, true) | ||
| matchingDbs.foreach { dbName => | ||
| catalog.listTables(dbName, tablePattern).foreach { tableIdentifier => | ||
| val catalogTable = catalog.getTableMetadata(tableIdentifier) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be very slow for big schemas. Calling The underlying Hive Thriftserver GetTables uses MetastoreClient.getTableObjectsByName (https://hive.apache.org/javadocs/r2.1.1/api/org/apache/hadoop/hive/metastore/HiveMetaStoreClient.html#getTableObjectsByName-java.lang.String-java.util.List-) call to bulk-list the tables, but we don't expose that through our SessionCatalog / ExternalCatalog / HiveClientImpl Would it be possible to thread that bulk getTableObjectsByName operation through our catalog APIs, to be able to retrieve the tables efficiently here? @wangyum @gatorsmile - what do you think?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I raised https://issues.apache.org/jira/browse/SPARK-27899 with this idea.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @LantaoJin @wangyum Could either of you submit a PR to resolve the issue raised by @juliuszsompolski ?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, I will take this issue. |
||
| val tableType = tableTypeString(catalogTable.tableType) | ||
| if (tableTypes == null || tableTypes.isEmpty || tableTypes.contains(tableType)) { | ||
| val rowData = Array[AnyRef]( | ||
| "", | ||
| catalogTable.database, | ||
| catalogTable.identifier.table, | ||
| tableType, | ||
| catalogTable.comment.getOrElse("")) | ||
| rowSet.addRow(rowData) | ||
| } | ||
| } | ||
| } | ||
| setState(OperationState.FINISHED) | ||
| } | ||
|
|
||
| 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 |
|---|---|---|
|
|
@@ -17,7 +17,7 @@ | |
|
|
||
| package org.apache.spark.sql.hive.thriftserver | ||
|
|
||
| import java.util.Properties | ||
| import java.util.{Arrays => JArrays, List => JList, Properties} | ||
|
|
||
| import org.apache.hive.jdbc.{HiveConnection, HiveQueryResultSet, Utils => JdbcUtils} | ||
| import org.apache.hive.service.auth.PlainSaslHelper | ||
|
|
@@ -100,4 +100,89 @@ class SparkMetadataOperationSuite extends HiveThriftJdbcTest { | |
| } | ||
| } | ||
| } | ||
|
|
||
| test("Spark's own GetTablesOperation(SparkGetTablesOperation)") { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test mimic |
||
| def testGetTablesOperation( | ||
| schema: String, | ||
| tableNamePattern: String, | ||
| tableTypes: JList[String])(f: HiveQueryResultSet => Unit): Unit = { | ||
| val rawTransport = new TSocket("localhost", serverPort) | ||
| val connection = new HiveConnection(s"jdbc:hive2://localhost:$serverPort", new Properties) | ||
| val user = System.getProperty("user.name") | ||
| val transport = PlainSaslHelper.getPlainTransport(user, "anonymous", rawTransport) | ||
| val client = new TCLIService.Client(new TBinaryProtocol(transport)) | ||
| transport.open() | ||
|
|
||
| var rs: HiveQueryResultSet = null | ||
|
|
||
| try { | ||
| val openResp = client.OpenSession(new TOpenSessionReq) | ||
| val sessHandle = openResp.getSessionHandle | ||
|
|
||
| val getTableReq = new TGetTablesReq(sessHandle) | ||
| getTableReq.setSchemaName(schema) | ||
| getTableReq.setTableName(tableNamePattern) | ||
| getTableReq.setTableTypes(tableTypes) | ||
|
|
||
| val getTableResp = client.GetTables(getTableReq) | ||
|
|
||
| JdbcUtils.verifySuccess(getTableResp.getStatus) | ||
|
|
||
| rs = new HiveQueryResultSet.Builder(connection) | ||
| .setClient(client) | ||
| .setSessionHandle(sessHandle) | ||
| .setStmtHandle(getTableResp.getOperationHandle) | ||
| .build() | ||
|
|
||
| f(rs) | ||
| } finally { | ||
| rs.close() | ||
| connection.close() | ||
| transport.close() | ||
| rawTransport.close() | ||
| } | ||
| } | ||
|
|
||
| def checkResult(tableNames: Seq[String], rs: HiveQueryResultSet): Unit = { | ||
| if (tableNames.nonEmpty) { | ||
| for (i <- tableNames.indices) { | ||
| assert(rs.next()) | ||
| assert(rs.getString("TABLE_NAME") === tableNames(i)) | ||
| } | ||
| } else { | ||
| assert(!rs.next()) | ||
| } | ||
| } | ||
|
|
||
| withJdbcStatement("table1", "table2") { statement => | ||
| Seq( | ||
| "CREATE TABLE table1(key INT, val STRING)", | ||
| "CREATE TABLE table2(key INT, val STRING)", | ||
| "CREATE VIEW view1 AS SELECT * FROM table2").foreach(statement.execute) | ||
|
|
||
| testGetTablesOperation("%", "%", null) { rs => | ||
| checkResult(Seq("table1", "table2", "view1"), rs) | ||
| } | ||
|
|
||
| testGetTablesOperation("%", "table1", null) { rs => | ||
| checkResult(Seq("table1"), rs) | ||
| } | ||
|
|
||
| testGetTablesOperation("%", "table_not_exist", null) { rs => | ||
| checkResult(Seq.empty, rs) | ||
| } | ||
|
|
||
| testGetTablesOperation("%", "%", JArrays.asList("TABLE")) { rs => | ||
| checkResult(Seq("table1", "table2"), rs) | ||
| } | ||
|
|
||
| testGetTablesOperation("%", "%", JArrays.asList("VIEW")) { rs => | ||
| checkResult(Seq("view1"), rs) | ||
| } | ||
|
|
||
| testGetTablesOperation("%", "%", JArrays.asList("TABLE", "VIEW")) { rs => | ||
| checkResult(Seq("table1", "table2", "view1"), rs) | ||
| } | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.