From 484bb3c6bda1eee6f0a10208de8df9b5989b32ea Mon Sep 17 00:00:00 2001 From: Baoqi Wu Date: Sat, 21 Aug 2021 00:17:54 +0800 Subject: [PATCH] [KYUUBI #956] Fail to get table list if there is _ in schema's name Just write some basic tests out in other projects. Since this toJavaRegex is not a static function, which is hard to write unit test if don't move it out as as static function in object. (But in that case, the change may be too large for this). ### _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/latest/develop_tools/testing.html#running-tests) locally before make a pull request Closes #964 from Baoqi/bwu_fix_956. Closes #956 985a73b5 [Baoqi Wu] [KYUUBI #956] add a unit test for this cb111893 [Baoqi Wu] [KYUUBI #956] Fail to get table list if there is _ in schema's name Authored-by: Baoqi Wu Signed-off-by: Kent Yao --- .../spark/operation/SparkOperation.scala | 20 ++++++------------- .../kyuubi/operation/BasicJDBCTests.scala | 2 ++ 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/operation/SparkOperation.scala b/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/operation/SparkOperation.scala index 646afe65b..67d1ecd69 100644 --- a/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/operation/SparkOperation.scala +++ b/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/operation/SparkOperation.scala @@ -18,7 +18,6 @@ package org.apache.kyuubi.engine.spark.operation import java.time.ZoneId -import java.util.regex.Pattern import org.apache.commons.lang3.StringUtils import org.apache.hive.service.rpc.thrift.{TRowSet, TTableSchema} @@ -62,6 +61,8 @@ abstract class SparkOperation(spark: SparkSession, opType: OperationType, sessio * * Underscores (_) are converted to '.' and percent signs (%) are converted to '.*'. * + * (referred to Spark's implementation: convertPattern function in file MetadataOperation.java) + * * @param input the SQL pattern to convert * @return the equivalent Java regular expression of the pattern */ @@ -71,19 +72,10 @@ abstract class SparkOperation(spark: SparkSession, opType: OperationType, sessio } else { input } - val in = res.toIterator - val out = new StringBuilder() - - while (in.hasNext) { - in.next match { - case c if c == '\\' && in.hasNext => Pattern.quote(Character.toString(in.next())) - case c if c == '\\' && !in.hasNext => Pattern.quote(Character.toString(c)) - case '_' => out ++= "." - case '%' => out ++= ".*" - case c => out ++= Character.toString(c) - } - } - out.result() + val wStr = ".*" + res + .replaceAll("([^\\\\])%", "$1" + wStr).replaceAll("\\\\%", "%").replaceAll("^%", wStr) + .replaceAll("([^\\\\])_", "$1.").replaceAll("\\\\_", "_").replaceAll("^_", ".") } protected def onError(cancel: Boolean = false): PartialFunction[Throwable, Unit] = { diff --git a/kyuubi-common/src/test/scala/org/apache/kyuubi/operation/BasicJDBCTests.scala b/kyuubi-common/src/test/scala/org/apache/kyuubi/operation/BasicJDBCTests.scala index 07ff7518a..c05078f84 100644 --- a/kyuubi-common/src/test/scala/org/apache/kyuubi/operation/BasicJDBCTests.scala +++ b/kyuubi-common/src/test/scala/org/apache/kyuubi/operation/BasicJDBCTests.scala @@ -53,6 +53,8 @@ trait BasicJDBCTests extends JDBCTestUtils { checkGetSchemas(metaData.getSchemas(catalog, "db1"), Seq("db1"), catalog) checkGetSchemas(metaData.getSchemas(catalog, "db_not_exist"), Seq.empty, catalog) + + checkGetSchemas(metaData.getSchemas(catalog, "global\\_temp"), Seq("global_temp"), catalog) } }