Skip to content

Commit

Permalink
[KYUUBI #4029] [FOLLOWUP] Make getSchemas use the parser pattern
Browse files Browse the repository at this point in the history
### _Why are the changes needed?_

This par aims to make `getSchemas` use the parser pattern.

### _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.apache.org/docs/latest/develop_tools/testing.html#running-tests) locally before make a pull request

Closes #4099 from Yikf/getSchema-pattern.

Closes #4029

590a571 [Yikf] Make getSchemas use the parser pattern

Authored-by: Yikf <yikaifei1@gmail.com>
Signed-off-by: ulysses-you <ulyssesyou@apache.org>
  • Loading branch information
yikf authored and ulysses-you committed Jan 9, 2023
1 parent 65b393c commit b1ad75b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ singleStatement

statement
: SELECT TABLE_SCHEM COMMA TABLE_CATALOG FROM SYSTEM_JDBC_SCHEMAS
(WHERE (TABLE_CATALOG EQ catalog=stringLit)? AND? (TABLE_SCHEM LIKE schema=stringLit)?)?
(WHERE tableCatalogFilter? AND? tableSchemaFilter?)?
ORDER BY TABLE_CATALOG COMMA TABLE_SCHEM #getSchemas
| SELECT TABLE_CAT FROM SYSTEM_JDBC_CATALOGS ORDER BY TABLE_CAT #getCatalogs
| SELECT TABLE_TYPE FROM SYSTEM_JDBC_TABLE_TYPES ORDER BY TABLE_TYPE #getTableTypes
Expand All @@ -43,9 +43,9 @@ statement
;

tableCatalogFilter
: TABLE_CAT IS NULL #nullCatalog
| TABLE_CAT EQ catalog=stringLit #catalogFilter
;
: (TABLE_CAT | TABLE_CATALOG) IS NULL #nullCatalog
| (TABLE_CAT | TABLE_CATALOG) EQ catalog=STRING+ #catalogFilter
;

tableSchemaFilter
: TABLE_SCHEM IS NULL #nulTableSchema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,10 @@ class KyuubiTrinoFeAstBuilder extends KyuubiTrinoFeBaseParserBaseVisitor[AnyRef]
}

override def visitGetSchemas(ctx: GetSchemasContext): KyuubiTreeNode = {
val catalog = if (ctx.catalog == null) {
null
} else {
unescapeSQLString(ctx.catalog.getText)
}
val schema = if (ctx.schema == null) {
null
} else {
unescapeSQLString(ctx.schema.getText)
}
val catalog = visit(ctx.tableCatalogFilter()).asInstanceOf[String]
val schemaPattern = visit(ctx.tableSchemaFilter()).asInstanceOf[String]

GetSchemas(catalog, schema)
GetSchemas(catalog, schemaPattern)
}

override def visitGetCatalogs(ctx: GetCatalogsContext): KyuubiTreeNode = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class KyuubiTrinoFeParserSuite extends KyuubiFunSuite {
case GetSchemas(catalogName, schemaPattern) =>
assert(catalogName == catalog)
assert(schemaPattern == schema)
case _ => throw new IllegalStateException()
case _ => fail(s"Query $query parse failed. ")
}
}

Expand All @@ -56,15 +56,22 @@ class KyuubiTrinoFeParserSuite extends KyuubiFunSuite {
check(
"""
|SELECT TABLE_SCHEM, TABLE_CATALOG FROM system.jdbc.schemas
|WHERE TABLE_SCHEM LIKE 'aa%'
|WHERE TABLE_CATALOG IS NULL
|ORDER BY TABLE_CATALOG, TABLE_SCHEM
|""".stripMargin)

check(
"""
|SELECT TABLE_SCHEM, TABLE_CATALOG FROM system.jdbc.schemas
|WHERE TABLE_SCHEM LIKE 'aa%' ESCAPE '\'
|ORDER BY TABLE_CATALOG, TABLE_SCHEM
|""".stripMargin,
schema = "aa%")

check(
"""
|SELECT TABLE_SCHEM, TABLE_CATALOG FROM system.jdbc.schemas
|WHERE TABLE_CATALOG='bb' and TABLE_SCHEM LIKE 'bb%'
|WHERE TABLE_CATALOG='bb' and TABLE_SCHEM LIKE 'bb%' ESCAPE '\'
|ORDER BY TABLE_CATALOG, TABLE_SCHEM
|""".stripMargin,
catalog = "bb",
Expand Down

0 comments on commit b1ad75b

Please sign in to comment.