From 6c2f29c847e2b3b528374500cc39c814f0e300f8 Mon Sep 17 00:00:00 2001 From: sjgllgh Date: Wed, 15 Nov 2023 15:13:10 +0800 Subject: [PATCH] sql field comment semicolon with escape --- .../interceptor/impl/CommentInterceptor.scala | 24 +++++++++++++++++++ .../entrance/interceptor/impl/Explain.scala | 3 ++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/linkis-computation-governance/linkis-entrance/src/main/scala/org/apache/linkis/entrance/interceptor/impl/CommentInterceptor.scala b/linkis-computation-governance/linkis-entrance/src/main/scala/org/apache/linkis/entrance/interceptor/impl/CommentInterceptor.scala index 627ab82b8e..f1d7fb1e1a 100644 --- a/linkis-computation-governance/linkis-entrance/src/main/scala/org/apache/linkis/entrance/interceptor/impl/CommentInterceptor.scala +++ b/linkis-computation-governance/linkis-entrance/src/main/scala/org/apache/linkis/entrance/interceptor/impl/CommentInterceptor.scala @@ -70,8 +70,32 @@ trait CommentHelper { object SQLCommentHelper extends CommentHelper { override val commentPattern: Regex = """\s*--.+\s*""".r.unanchored private val comment = "(?ms)('(?:''|[^'])*')|--.*?$|/\\*.*?\\*/|#.*?$|" + private val comment_sem = "(?i)(comment)\\s+'([^']*)'" private val logger: Logger = LoggerFactory.getLogger(getClass) + def replaceComment(code: String): String = { + try { + val pattern = Pattern.compile(comment_sem) + val matcher = pattern.matcher(code) + val sb = new StringBuffer + while (matcher.find()) { + val commentKeyword = matcher.group(1) + val comment = matcher.group(2) + val escapedComment = comment.replaceAll(";", "\\\\\\\\;") + matcher.appendReplacement(sb, commentKeyword + " '" + escapedComment + "'") + } + matcher.appendTail(sb) + sb.toString + } catch { + case e: Exception => + logger.warn("sql comment semicolon replace failed") + code + case t: Throwable => + logger.warn("sql comment semicolon replace failed") + code + } + } + override def dealComment(code: String): String = { try { val p = Pattern.compile(comment) diff --git a/linkis-computation-governance/linkis-entrance/src/main/scala/org/apache/linkis/entrance/interceptor/impl/Explain.scala b/linkis-computation-governance/linkis-entrance/src/main/scala/org/apache/linkis/entrance/interceptor/impl/Explain.scala index 8436ccc711..d0ad33c53f 100644 --- a/linkis-computation-governance/linkis-entrance/src/main/scala/org/apache/linkis/entrance/interceptor/impl/Explain.scala +++ b/linkis-computation-governance/linkis-entrance/src/main/scala/org/apache/linkis/entrance/interceptor/impl/Explain.scala @@ -118,7 +118,8 @@ object SQLExplain extends Explain { logAppender: java.lang.StringBuilder ): Unit = { val fixedCode: ArrayBuffer[String] = new ArrayBuffer[String]() - val tempCode = SQLCommentHelper.dealComment(executionCode) + val tempCode1 = SQLCommentHelper.dealComment(executionCode) + val tempCode = SQLCommentHelper.replaceComment(tempCode1) val isNoLimitAllowed = Utils.tryCatch { IDE_ALLOW_NO_LIMIT_REGEX.findFirstIn(executionCode).isDefined } { case e: Exception =>