-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-24196][SQL] Implement Spark's own GetSchemasOperation #22903
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...rver/src/main/scala/org/apache/spark/sql/hive/thriftserver/SparkGetSchemasOperation.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /* | ||
| * 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.hadoop.hive.ql.security.authorization.plugin.HiveOperationType | ||
| import org.apache.hive.service.cli._ | ||
| import org.apache.hive.service.cli.operation.GetSchemasOperation | ||
| import org.apache.hive.service.cli.operation.MetadataOperation.DEFAULT_HIVE_CATALOG | ||
| import org.apache.hive.service.cli.session.HiveSession | ||
|
|
||
| import org.apache.spark.sql.SQLContext | ||
|
|
||
| /** | ||
| * Spark's own GetSchemasOperation | ||
| * | ||
| * @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 | ||
| */ | ||
| private[hive] class SparkGetSchemasOperation( | ||
| sqlContext: SQLContext, | ||
| parentSession: HiveSession, | ||
| catalogName: String, | ||
| schemaName: String) | ||
| extends GetSchemasOperation(parentSession, catalogName, schemaName) { | ||
|
|
||
| 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) | ||
|
|
||
| if (isAuthV2Enabled) { | ||
| val cmdStr = s"catalog : $catalogName, schemaPattern : $schemaName" | ||
| authorizeMetaGets(HiveOperationType.GET_TABLES, null, cmdStr) | ||
| } | ||
|
|
||
| try { | ||
| val schemaPattern = convertSchemaPattern(schemaName) | ||
| sqlContext.sessionState.catalog.listDatabases(schemaPattern).foreach { dbName => | ||
| rowSet.addRow(Array[AnyRef](dbName, DEFAULT_HIVE_CATALOG)) | ||
| } | ||
| setState(OperationState.FINISHED) | ||
| } catch { | ||
| case e: HiveSQLException => | ||
| setState(OperationState.ERROR) | ||
| throw e | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
...r/src/test/scala/org/apache/spark/sql/hive/thriftserver/SparkMetadataOperationSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /* | ||
| * 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.Properties | ||
|
|
||
| import org.apache.hive.jdbc.{HiveConnection, HiveQueryResultSet, Utils => JdbcUtils} | ||
| import org.apache.hive.service.auth.PlainSaslHelper | ||
| import org.apache.hive.service.cli.thrift._ | ||
| import org.apache.thrift.protocol.TBinaryProtocol | ||
| import org.apache.thrift.transport.TSocket | ||
|
|
||
| class SparkMetadataOperationSuite extends HiveThriftJdbcTest { | ||
|
|
||
| override def mode: ServerMode.Value = ServerMode.binary | ||
|
|
||
| test("Spark's own GetSchemasOperation(SparkGetSchemasOperation)") { | ||
| def testGetSchemasOperation( | ||
|
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 HiveDatabaseMetaData. getSchemas(). |
||
| catalog: String, | ||
| schemaPattern: 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 schemaReq = new TGetSchemasReq(sessHandle) | ||
|
|
||
| if (catalog != null) { | ||
| schemaReq.setCatalogName(catalog) | ||
| } | ||
|
|
||
| if (schemaPattern == null) { | ||
| schemaReq.setSchemaName("%") | ||
| } else { | ||
| schemaReq.setSchemaName(schemaPattern) | ||
| } | ||
|
|
||
| val schemaResp = client.GetSchemas(schemaReq) | ||
| JdbcUtils.verifySuccess(schemaResp.getStatus) | ||
|
|
||
| rs = new HiveQueryResultSet.Builder(connection) | ||
| .setClient(client) | ||
| .setSessionHandle(sessHandle) | ||
| .setStmtHandle(schemaResp.getOperationHandle) | ||
| .build() | ||
| f(rs) | ||
| } finally { | ||
| rs.close() | ||
| connection.close() | ||
| transport.close() | ||
| rawTransport.close() | ||
| } | ||
| } | ||
|
|
||
| def checkResult(dbNames: Seq[String], rs: HiveQueryResultSet): Unit = { | ||
| if (dbNames.nonEmpty) { | ||
| for (i <- dbNames.indices) { | ||
| assert(rs.next()) | ||
| assert(rs.getString("TABLE_SCHEM") === dbNames(i)) | ||
| } | ||
| } else { | ||
| assert(!rs.next()) | ||
| } | ||
| } | ||
|
|
||
| withDatabase("db1", "db2") { statement => | ||
| Seq("CREATE DATABASE db1", "CREATE DATABASE db2").foreach(statement.execute) | ||
|
|
||
| testGetSchemasOperation(null, "%") { rs => | ||
| checkResult(Seq("db1", "db2"), rs) | ||
| } | ||
| testGetSchemasOperation(null, "db1") { rs => | ||
| checkResult(Seq("db1"), rs) | ||
| } | ||
| testGetSchemasOperation(null, "db_not_exist") { rs => | ||
| checkResult(Seq.empty, rs) | ||
| } | ||
| testGetSchemasOperation(null, "db*") { rs => | ||
| checkResult(Seq("db1", "db2"), rs) | ||
| } | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Hm ........ but I noticed here is
o.a.hiveside code, @gatorsmile. Wouldn't it be better to avoid some changes here to prepare to get rid of Hive stuff later completely?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.
We do not have a plan to remove the thrift-server and use the Hive jar. Instead, I think we need to enhance the current thrift-server implementation.
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.
I actually meant getting rid of Hive fork stuff by upgrading Hive to another version later. Those changes would be some conflicts when upgrading from 1.2.1 to upper Hive versions basically.
Considering it's one line change, okie. I already see some changes are made there. Ok to me.
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.
7feeb82 shows we want to further cleanup and improve the thrift-server. Even if https://issues.apache.org/jira/browse/HIVE-16391 is resolved, we will still keep the Hive thrift-server.