You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2018/12/10 15:31:08 UTC

[GitHub] hepin1989 commented on a change in pull request #23276: [SPARK-26321][SQL] Split a SQL in correct way

hepin1989 commented on a change in pull request #23276: [SPARK-26321][SQL] Split a SQL in correct way
URL: https://github.com/apache/spark/pull/23276#discussion_r240256699
 
 

 ##########
 File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/StringUtils.scala
 ##########
 @@ -87,4 +90,50 @@ object StringUtils {
     }
     funcNames.toSeq
   }
+
+  def split(sql: String): Array[String] = {
+    val dQuote: Char = '"'
+    val sQuote: Char = '\''
+    val semicolon: Char = ';'
+    val escape: Char = '\\'
+    val dot = '.'
+    val inlineComment = "--"
+
+    var cursor: Int = 0
+    var inStr: Char = dot // dot means that the cursor is not in a quoted string
+    val ret: mutable.ArrayBuffer[String] = mutable.ArrayBuffer()
+    var currentSQL: mutable.StringBuilder = mutable.StringBuilder.newBuilder
+
+    while (cursor < sql.length) {
+      val current: Char = sql(cursor)
+      sql.substring(cursor) match {
+        // if it is comment, move cursor at the end of this line
+        case remaining if inStr == dot && remaining.startsWith(inlineComment) =>
+          cursor += remaining.takeWhile(x => x != '\n').length
+        // end of the sql
+        case remaining if inStr == dot && current == semicolon =>
+          ret += currentSQL.toString.trim
+          currentSQL.clear()
+          cursor += 1
+        // start of single/double quote
+        case remaining if inStr == dot && (List(dQuote, sQuote) contains current) =>
+          inStr = current
+          currentSQL += current
+          cursor += 1
+        case remaining if remaining.length >= 2 && inStr != dot && current == escape =>
+          currentSQL.append(remaining.take(2))
+          cursor += 2
+        // end of single/double quote
+        case remaining if (inStr == dQuote || inStr == sQuote) && current == inStr =>
+          inStr = '.'
 
 Review comment:
   dot

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org