Skip to content

Commit 26aa92a

Browse files
yikfyanghua
authored andcommitted
[KYUUBI apache#4320] Support GetPrimaryKeys for Trino Fe
### _Why are the changes needed?_ Support GetPrimaryKeys for Trino Fe, close apache#4320 ### _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 - [x] [Run test](https://kyuubi.readthedocs.io/en/master/develop_tools/testing.html#running-tests) locally before make a pull request Closes apache#4321 from Yikf/primaryKeys. Closes apache#4320 3690a2c [Yikf] Support GetPrimaryKeys for Trino Fe Authored-by: Yikf <[email protected]> Signed-off-by: ulyssesyou <[email protected]>
1 parent b27c140 commit 26aa92a

File tree

6 files changed

+44
-3
lines changed

6 files changed

+44
-3
lines changed

kyuubi-server/src/main/antlr4/org/apache/kyuubi/sql/KyuubiTrinoFeBaseLexer.g4

+6
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ SCOPE_TABLE: 'SCOPE_TABLE';
9797
SOURCE_DATA_TYPE: 'SOURCE_DATA_TYPE';
9898
IS_AUTOINCREMENT: 'IS_AUTOINCREMENT';
9999
IS_GENERATEDCOLUMN: 'IS_GENERATEDCOLUMN';
100+
VARCHAR: 'VARCHAR';
101+
SMALLINT: 'SMALLINT';
102+
CAST: 'CAST';
103+
AS: 'AS';
104+
KEY_SEQ: 'KEY_SEQ';
105+
PK_NAME: 'PK_NAME';
100106

101107
fragment SEARCH_STRING_ESCAPE: '\'' '\\' '\'';
102108

kyuubi-server/src/main/antlr4/org/apache/kyuubi/sql/KyuubiTrinoFeBaseParser.g4

+7
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ statement
4747
SOURCE_DATA_TYPE COMMA IS_AUTOINCREMENT COMMA IS_GENERATEDCOLUMN FROM SYSTEM_JDBC_COLUMNS
4848
(WHERE tableCatalogFilter? AND? tableSchemaFilter? AND? tableNameFilter? AND? colNameFilter?)?
4949
ORDER BY TABLE_CAT COMMA TABLE_SCHEM COMMA TABLE_NAME COMMA ORDINAL_POSITION #getColumns
50+
| SELECT CAST LEFT_PAREN NULL AS VARCHAR RIGHT_PAREN TABLE_CAT COMMA
51+
CAST LEFT_PAREN NULL AS VARCHAR RIGHT_PAREN TABLE_SCHEM COMMA
52+
CAST LEFT_PAREN NULL AS VARCHAR RIGHT_PAREN TABLE_NAME COMMA
53+
CAST LEFT_PAREN NULL AS VARCHAR RIGHT_PAREN COLUMN_NAME COMMA
54+
CAST LEFT_PAREN NULL AS SMALLINT RIGHT_PAREN KEY_SEQ COMMA
55+
CAST LEFT_PAREN NULL AS VARCHAR RIGHT_PAREN PK_NAME
56+
WHERE FALSE #getPrimaryKeys
5057
| .*? #passThrough
5158
;
5259

kyuubi-server/src/main/scala/org/apache/kyuubi/server/trino/api/KyuubiTrinoOperationTranslator.scala

+6-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import org.apache.kyuubi.operation.OperationHandle
2525
import org.apache.kyuubi.service.BackendService
2626
import org.apache.kyuubi.sql.parser.trino.KyuubiTrinoFeParser
2727
import org.apache.kyuubi.sql.plan.PassThroughNode
28-
import org.apache.kyuubi.sql.plan.trino.{GetCatalogs, GetColumns, GetSchemas, GetTables, GetTableTypes, GetTypeInfo}
28+
import org.apache.kyuubi.sql.plan.trino.{GetCatalogs, GetColumns, GetPrimaryKeys, GetSchemas, GetTables, GetTableTypes, GetTypeInfo}
2929

3030
class KyuubiTrinoOperationTranslator(backendService: BackendService) {
3131
lazy val parser = new KyuubiTrinoFeParser()
@@ -68,6 +68,11 @@ class KyuubiTrinoOperationTranslator(backendService: BackendService) {
6868
schemaPattern,
6969
tableNamePattern,
7070
colNamePattern)
71+
case GetPrimaryKeys() =>
72+
val operationHandle = backendService.getPrimaryKeys(sessionHandle, null, null, null)
73+
// The trino implementation always returns empty.
74+
operationHandle.setHasResultSet(false)
75+
operationHandle
7176
case PassThroughNode() =>
7277
backendService.executeStatement(sessionHandle, statement, configs, runAsync, queryTimeout)
7378
}

kyuubi-server/src/main/scala/org/apache/kyuubi/sql/parser/trino/KyuubiTrinoFeAstBuilder.scala

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import org.apache.kyuubi.sql.KyuubiTrinoFeBaseParser._
2525
import org.apache.kyuubi.sql.KyuubiTrinoFeBaseParserBaseVisitor
2626
import org.apache.kyuubi.sql.parser.KyuubiParser.unescapeSQLString
2727
import org.apache.kyuubi.sql.plan.{KyuubiTreeNode, PassThroughNode}
28-
import org.apache.kyuubi.sql.plan.trino.{GetCatalogs, GetColumns, GetSchemas, GetTables, GetTableTypes, GetTypeInfo}
28+
import org.apache.kyuubi.sql.plan.trino.{GetCatalogs, GetColumns, GetPrimaryKeys, GetSchemas, GetTables, GetTableTypes, GetTypeInfo}
2929

3030
class KyuubiTrinoFeAstBuilder extends KyuubiTrinoFeBaseParserBaseVisitor[AnyRef] {
3131

@@ -92,6 +92,10 @@ class KyuubiTrinoFeAstBuilder extends KyuubiTrinoFeBaseParserBaseVisitor[AnyRef]
9292
GetColumns(catalog, schemaPattern, tableNamePattern, colNamePattern)
9393
}
9494

95+
override def visitGetPrimaryKeys(ctx: GetPrimaryKeysContext): KyuubiTreeNode = {
96+
GetPrimaryKeys()
97+
}
98+
9599
override def visitNullCatalog(ctx: NullCatalogContext): AnyRef = {
96100
null
97101
}

kyuubi-server/src/main/scala/org/apache/kyuubi/sql/plan/trino/TrinoFeOperations.scala

+4
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,7 @@ case class GetColumns(
5555
colNamePattern: String) extends KyuubiTreeNode {
5656
override def name(): String = "Get Columns"
5757
}
58+
59+
case class GetPrimaryKeys() extends KyuubiTreeNode {
60+
override def name(): String = "Get Primary Keys"
61+
}

kyuubi-server/src/test/scala/org/apache/kyuubi/parser/trino/KyuubiTrinoFeParserSuite.scala

+16-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ package org.apache.kyuubi.parser.trino
2020
import org.apache.kyuubi.KyuubiFunSuite
2121
import org.apache.kyuubi.sql.parser.trino.KyuubiTrinoFeParser
2222
import org.apache.kyuubi.sql.plan.{KyuubiTreeNode, PassThroughNode}
23-
import org.apache.kyuubi.sql.plan.trino.{GetCatalogs, GetColumns, GetSchemas, GetTables, GetTableTypes, GetTypeInfo}
23+
import org.apache.kyuubi.sql.plan.trino.{GetCatalogs, GetColumns, GetPrimaryKeys, GetSchemas, GetTables, GetTableTypes, GetTypeInfo}
2424

2525
class KyuubiTrinoFeParserSuite extends KyuubiFunSuite {
2626
val parser = new KyuubiTrinoFeParser()
@@ -354,4 +354,19 @@ class KyuubiTrinoFeParserSuite extends KyuubiFunSuite {
354354
tableName = "%aa",
355355
colName = "%bb")
356356
}
357+
358+
test("Support GetPrimaryKeys for Trino Fe") {
359+
val kyuubiTreeNode = parse(
360+
"""
361+
| SELECT CAST(NULL AS varchar) TABLE_CAT,
362+
| CAST(NULL AS varchar) TABLE_SCHEM,
363+
| CAST(NULL AS varchar) TABLE_NAME,
364+
| CAST(NULL AS varchar) COLUMN_NAME,
365+
| CAST(NULL AS smallint) KEY_SEQ,
366+
| CAST(NULL AS varchar) PK_NAME
367+
| WHERE false
368+
|""".stripMargin)
369+
370+
assert(kyuubiTreeNode.isInstanceOf[GetPrimaryKeys])
371+
}
357372
}

0 commit comments

Comments
 (0)