You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kyuubi.apache.org by ya...@apache.org on 2022/02/15 02:56:25 UTC

[incubator-kyuubi] branch master updated: [KYUUBI #1886] Add GetSchemas for trino engine

This is an automated email from the ASF dual-hosted git repository.

yao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-kyuubi.git


The following commit(s) were added to refs/heads/master by this push:
     new 03edbce  [KYUUBI #1886] Add GetSchemas for trino engine
03edbce is described below

commit 03edbce014414fd26915e04cfad2cae69fea9c28
Author: hongdongdong <ho...@cmss.chinamobile.com>
AuthorDate: Tue Feb 15 10:56:17 2022 +0800

    [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 <ho...@cmss.chinamobile.com>
    Signed-off-by: Kent Yao <ya...@apache.org>
---
 .../kyuubi/engine/trino/operation/GetSchemas.scala | 62 ++++++++++++++++++++
 .../trino/operation/TrinoOperationManager.scala    |  5 +-
 .../trino/operation/TrinoOperationSuite.scala      | 68 +++++++++++++++++++++-
 3 files changed, 133 insertions(+), 2 deletions(-)

diff --git a/externals/kyuubi-trino-engine/src/main/scala/org/apache/kyuubi/engine/trino/operation/GetSchemas.scala b/externals/kyuubi-trino-engine/src/main/scala/org/apache/kyuubi/engine/trino/operation/GetSchemas.scala
new file mode 100644
index 0000000..b4547ec
--- /dev/null
+++ b/externals/kyuubi-trino-engine/src/main/scala/org/apache/kyuubi/engine/trino/operation/GetSchemas.scala
@@ -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()
+  }
+}
diff --git a/externals/kyuubi-trino-engine/src/main/scala/org/apache/kyuubi/engine/trino/operation/TrinoOperationManager.scala b/externals/kyuubi-trino-engine/src/main/scala/org/apache/kyuubi/engine/trino/operation/TrinoOperationManager.scala
index 8f1505c..939fbe3 100644
--- a/externals/kyuubi-trino-engine/src/main/scala/org/apache/kyuubi/engine/trino/operation/TrinoOperationManager.scala
+++ b/externals/kyuubi-trino-engine/src/main/scala/org/apache/kyuubi/engine/trino/operation/TrinoOperationManager.scala
@@ -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,
diff --git a/externals/kyuubi-trino-engine/src/test/scala/org/apache/kyuubi/engine/trino/operation/TrinoOperationSuite.scala b/externals/kyuubi-trino-engine/src/test/scala/org/apache/kyuubi/engine/trino/operation/TrinoOperationSuite.scala
index 7020a82..ac56352 100644
--- a/externals/kyuubi-trino-engine/src/test/scala/org/apache/kyuubi/engine/trino/operation/TrinoOperationSuite.scala
+++ b/externals/kyuubi-trino-engine/src/test/scala/org/apache/kyuubi/engine/trino/operation/TrinoOperationSuite.scala
@@ -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")