[KYUUBI #1886] Add GetSchemas for trino engine

<!--
Thanks for sending a pull request!

Here are some tips for you:
  1. If this is your first time, please read our contributor guidelines: https://kyuubi.readthedocs.io/en/latest/community/contributions.html
  2. If the PR is related to an issue in https://github.com/apache/incubator-kyuubi/issues, add '[KYUUBI #XXXX]' in your PR title, e.g., '[KYUUBI #XXXX] Your PR title ...'.
  3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][KYUUBI #XXXX] Your PR title ...'.
-->

### _Why are the changes needed?_
<!--
Please clarify why the changes are needed. For instance,
  1. If you add a feature, you can talk about the use case of it.
  2. If you fix a bug, you can clarify why it is a bug.
-->
Add GetSchemas for trino engine

### _How was this patch tested?_
- [X] Add some test cases that check the changes thoroughly including negative and positive cases if possible

- [ ] Add screenshots for manual tests if appropriate

- [ ] [Run test](https://kyuubi.apache.org/docs/latest/develop_tools/testing.html#running-tests) locally before make a pull request

Closes #1902 from hddong/add-trino-get-schemas.

Closes #1886

d117f710 [hongdongdong] [KYUUBI #1886] Add GetSchemas for trino engine

Authored-by: hongdongdong <hongdongdong@cmss.chinamobile.com>
Signed-off-by: Kent Yao <yao@apache.org>
This commit is contained in:
hongdongdong 2022-02-15 10:56:17 +08:00 committed by Kent Yao
parent 7f10a2325a
commit 03edbce014
No known key found for this signature in database
GPG Key ID: F7051850A0AF904D
3 changed files with 133 additions and 2 deletions

View File

@ -0,0 +1,62 @@
/*
* 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.kyuubi.engine.trino.operation
import scala.collection.mutable.ArrayBuffer
import org.apache.commons.lang3.StringUtils
import org.apache.kyuubi.engine.trino.TrinoStatement
import org.apache.kyuubi.operation.IterableFetchIterator
import org.apache.kyuubi.operation.OperationType
import org.apache.kyuubi.operation.meta.ResultSetSchemaConstant.TABLE_CATALOG
import org.apache.kyuubi.operation.meta.ResultSetSchemaConstant.TABLE_SCHEM
import org.apache.kyuubi.session.Session
class GetSchemas(session: Session, catalogName: String, schemaPattern: String)
extends TrinoOperation(OperationType.GET_SCHEMAS, session) {
private val SEARCH_STRING_ESCAPE: String = "\\"
override protected def runInternal(): Unit = {
val query = new StringBuilder("SELECT TABLE_SCHEM, TABLE_CATALOG FROM system.jdbc.schemas")
val filters = ArrayBuffer[String]()
if (StringUtils.isNotEmpty(catalogName)) {
filters += s"$TABLE_CATALOG = '$catalogName'"
}
if (StringUtils.isNotEmpty(schemaPattern)) {
filters += s"$TABLE_SCHEM LIKE '$schemaPattern' ESCAPE '$SEARCH_STRING_ESCAPE'"
}
if (filters.nonEmpty) {
query.append(" WHERE ")
query.append(filters.mkString(" AND "))
}
try {
val trinoStatement = TrinoStatement(
trinoContext,
session.sessionManager.getConf,
query.toString)
schema = trinoStatement.getColumns
val resultSet = trinoStatement.execute()
iter = new IterableFetchIterator(resultSet)
} catch onError()
}
}

View File

@ -47,7 +47,10 @@ class TrinoOperationManager extends OperationManager("TrinoOperationManager") {
override def newGetSchemasOperation(
session: Session,
catalog: String,
schema: String): Operation = null
schema: String): Operation = {
val op = new GetSchemas(session, catalog, schema)
addOperation(op)
}
override def newGetTablesOperation(
session: Session,

View File

@ -34,7 +34,7 @@ import org.apache.hive.service.rpc.thrift.TStatusCode
import org.apache.kyuubi.config.KyuubiConf.ENGINE_TRINO_CONNECTION_CATALOG
import org.apache.kyuubi.engine.trino.WithTrinoEngine
import org.apache.kyuubi.operation.HiveJDBCTestHelper
import org.apache.kyuubi.operation.meta.ResultSetSchemaConstant.{TABLE_CAT, TABLE_TYPE}
import org.apache.kyuubi.operation.meta.ResultSetSchemaConstant._
class TrinoOperationSuite extends WithTrinoEngine with HiveJDBCTestHelper {
override def withKyuubiConf: Map[String, String] = Map(
@ -71,6 +71,72 @@ class TrinoOperationSuite extends WithTrinoEngine with HiveJDBCTestHelper {
}
}
test("trino - get schemas") {
case class SchemaWithCatalog(catalog: String, schema: String)
withJdbcStatement() { statement =>
statement.execute("CREATE SCHEMA IF NOT EXISTS memory.test_escape_1")
statement.execute("CREATE SCHEMA IF NOT EXISTS memory.test2escape_1")
statement.execute("CREATE SCHEMA IF NOT EXISTS memory.test_escape11")
val meta = statement.getConnection.getMetaData
val resultSetBuffer = ArrayBuffer[SchemaWithCatalog]()
val schemas1 = meta.getSchemas(null, null)
while (schemas1.next()) {
resultSetBuffer +=
SchemaWithCatalog(schemas1.getString(TABLE_CATALOG), schemas1.getString(TABLE_SCHEM))
}
assert(resultSetBuffer.contains(SchemaWithCatalog("memory", "information_schema")))
assert(resultSetBuffer.contains(SchemaWithCatalog("system", "information_schema")))
val schemas2 = meta.getSchemas("memory", null)
resultSetBuffer.clear()
while (schemas2.next()) {
resultSetBuffer +=
SchemaWithCatalog(schemas2.getString(TABLE_CATALOG), schemas2.getString(TABLE_SCHEM))
}
assert(resultSetBuffer.contains(SchemaWithCatalog("memory", "default")))
assert(resultSetBuffer.contains(SchemaWithCatalog("memory", "information_schema")))
assert(!resultSetBuffer.exists(f => f.catalog == "system"))
val schemas3 = meta.getSchemas(null, "sf_")
resultSetBuffer.clear()
while (schemas3.next()) {
resultSetBuffer +=
SchemaWithCatalog(schemas3.getString(TABLE_CATALOG), schemas3.getString(TABLE_SCHEM))
}
assert(resultSetBuffer.contains(SchemaWithCatalog("tpcds", "sf1")))
assert(!resultSetBuffer.contains(SchemaWithCatalog("tpcds", "sf10")))
val schemas4 = meta.getSchemas(null, "sf%")
resultSetBuffer.clear()
while (schemas4.next()) {
resultSetBuffer +=
SchemaWithCatalog(schemas4.getString(TABLE_CATALOG), schemas4.getString(TABLE_SCHEM))
}
assert(resultSetBuffer.contains(SchemaWithCatalog("tpcds", "sf1")))
assert(resultSetBuffer.contains(SchemaWithCatalog("tpcds", "sf10")))
assert(resultSetBuffer.contains(SchemaWithCatalog("tpcds", "sf100")))
assert(resultSetBuffer.contains(SchemaWithCatalog("tpcds", "sf1000")))
// test escape the second '_'
val schemas5 = meta.getSchemas("memory", "test_escape\\_1")
resultSetBuffer.clear()
while (schemas5.next()) {
resultSetBuffer +=
SchemaWithCatalog(schemas5.getString(TABLE_CATALOG), schemas5.getString(TABLE_SCHEM))
}
assert(resultSetBuffer.contains(SchemaWithCatalog("memory", "test_escape_1")))
assert(resultSetBuffer.contains(SchemaWithCatalog("memory", "test2escape_1")))
assert(!resultSetBuffer.contains(SchemaWithCatalog("memory", "test_escape11")))
statement.execute("DROP SCHEMA memory.test_escape_1")
statement.execute("DROP SCHEMA memory.test2escape_1")
statement.execute("DROP SCHEMA memory.test_escape11")
}
}
test("execute statement - select decimal") {
withJdbcStatement() { statement =>
val resultSet = statement.executeQuery("SELECT DECIMAL '1.2' as col1, DECIMAL '1.23' AS col2")