From aa88c376b7c3fe8d190460d0e373e033009e116b Mon Sep 17 00:00:00 2001 From: hongdongdong Date: Thu, 10 Feb 2022 20:58:27 +0800 Subject: [PATCH] [KYUUBI #1885] Add GetCatalogs for trino engine ### _Why are the changes needed?_ Add `GetCatalogs` 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 #1888 from hddong/add-trino-get-catalogs. Closes #1885 f09dcbc8 [hongdongdong] [KYUUBI #1885] Add GetCatalogs for trino engine Authored-by: hongdongdong Signed-off-by: Cheng Pan --- .../engine/trino/operation/GetCatalogs.scala | 39 +++++++++++++++++++ .../operation/TrinoOperationManager.scala | 5 ++- .../trino/operation/TrinoOperationSuite.scala | 15 +++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 externals/kyuubi-trino-engine/src/main/scala/org/apache/kyuubi/engine/trino/operation/GetCatalogs.scala diff --git a/externals/kyuubi-trino-engine/src/main/scala/org/apache/kyuubi/engine/trino/operation/GetCatalogs.scala b/externals/kyuubi-trino-engine/src/main/scala/org/apache/kyuubi/engine/trino/operation/GetCatalogs.scala new file mode 100644 index 000000000..fa75aa397 --- /dev/null +++ b/externals/kyuubi-trino-engine/src/main/scala/org/apache/kyuubi/engine/trino/operation/GetCatalogs.scala @@ -0,0 +1,39 @@ +/* + * 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 org.apache.kyuubi.engine.trino.TrinoStatement +import org.apache.kyuubi.operation.IterableFetchIterator +import org.apache.kyuubi.operation.OperationType +import org.apache.kyuubi.session.Session + +class GetCatalogs(session: Session) + extends TrinoOperation(OperationType.GET_CATALOGS, session) { + + override protected def runInternal(): Unit = { + try { + val trinoStatement = TrinoStatement( + trinoContext, + session.sessionManager.getConf, + "SELECT TABLE_CAT FROM system.jdbc.catalogs") + 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 0f3a12d5b..823f4631c 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 @@ -39,7 +39,10 @@ class TrinoOperationManager extends OperationManager("TrinoOperationManager") { override def newGetTypeInfoOperation(session: Session): Operation = null - override def newGetCatalogsOperation(session: Session): Operation = null + override def newGetCatalogsOperation(session: Session): Operation = { + val op = new GetCatalogs(session) + addOperation(op) + } override def newGetSchemasOperation( 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 4e2289f8e..d985ef51d 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 @@ -18,6 +18,7 @@ package org.apache.kyuubi.engine.trino.operation import scala.collection.JavaConverters._ +import scala.collection.mutable.ArrayBuffer import org.apache.hive.service.rpc.thrift.TCancelOperationReq import org.apache.hive.service.rpc.thrift.TCloseOperationReq @@ -33,6 +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 class TrinoOperationSuite extends WithTrinoEngine with HiveJDBCTestHelper { override def withKyuubiConf: Map[String, String] = Map( @@ -43,6 +45,19 @@ class TrinoOperationSuite extends WithTrinoEngine with HiveJDBCTestHelper { override protected def jdbcUrl: String = getJdbcUrl + test("trino - get catalogs") { + withJdbcStatement() { statement => + val meta = statement.getConnection.getMetaData + val catalogs = meta.getCatalogs + val resultSetBuffer = ArrayBuffer[String]() + while (catalogs.next()) { + resultSetBuffer += catalogs.getString(TABLE_CAT) + } + assert(resultSetBuffer.contains("memory")) + assert(resultSetBuffer.contains("system")) + } + } + test("execute statement - select decimal") { withJdbcStatement() { statement => val resultSet = statement.executeQuery("SELECT DECIMAL '1.2' as col1, DECIMAL '1.23' AS col2")