cloud-fan commented on code in PR #37209:
URL: https://github.com/apache/spark/pull/37209#discussion_r928706692


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/trees/TreeNode.scala:
##########
@@ -58,95 +58,16 @@ private class MutableInt(var i: Int)
  * objects which contain SQL text.
  */
 case class Origin(
-  line: Option[Int] = None,
-  startPosition: Option[Int] = None,
-  startIndex: Option[Int] = None,
-  stopIndex: Option[Int] = None,
-  sqlText: Option[String] = None,
-  objectType: Option[String] = None,
-  objectName: Option[String] = None) {
-
-  /**
-   * The SQL query context of current node. For example:
-   * == SQL of VIEW v1(line 1, position 25) ==
-   * SELECT '' AS five, i.f1, i.f1 - int('2') AS x FROM INT4_TBL i
-   *                          ^^^^^^^^^^^^^^^
-   */
-  lazy val context: String = {
-    // If the query context is missing or incorrect, simply return an empty 
string.
-    if (sqlText.isEmpty || startIndex.isEmpty || stopIndex.isEmpty ||
-      startIndex.get < 0 || stopIndex.get >= sqlText.get.length || 
startIndex.get > stopIndex.get) {
-      ""
-    } else {
-      val positionContext = if (line.isDefined && startPosition.isDefined) {
-        // Note that the line number starts from 1, while the start position 
starts from 0.
-        // Here we increase the start position by 1 for consistency.
-        s"(line ${line.get}, position ${startPosition.get + 1})"
-      } else {
-        ""
-      }
-      val objectContext = if (objectType.isDefined && objectName.isDefined) {
-        s" of ${objectType.get} ${objectName.get}"
-      } else {
-        ""
-      }
-      val builder = new StringBuilder
-      builder ++= s"== SQL$objectContext$positionContext ==\n"
-
-      val text = sqlText.get
-      val start = math.max(startIndex.get, 0)
-      val stop = math.min(stopIndex.getOrElse(text.length - 1), text.length - 
1)
-      // Ideally we should show all the lines which contains the SQL text 
context of the current
-      // node:
-      // [additional text] [current tree node] [additional text]
-      // However, we need to truncate the additional text in case it is too 
long. The following
-      // variable is to define the max length of additional text.
-      val maxExtraContextLength = 32
-      val truncatedText = "..."
-      var lineStartIndex = start
-      // Collect the SQL text within the starting line of current Node.
-      // The text is truncated if it is too long.
-      while (lineStartIndex >= 0 &&
-        start - lineStartIndex <= maxExtraContextLength &&
-        text.charAt(lineStartIndex) != '\n') {
-        lineStartIndex -= 1
-      }
-      val startTruncated = start - lineStartIndex > maxExtraContextLength
-      var currentIndex = lineStartIndex
-      if (startTruncated) {
-        currentIndex -= truncatedText.length
-      }
-
-      var lineStopIndex = stop
-      // Collect the SQL text within the ending line of current Node.
-      // The text is truncated if it is too long.
-      while (lineStopIndex < text.length &&
-        lineStopIndex - stop <= maxExtraContextLength &&
-        text.charAt(lineStopIndex) != '\n') {
-        lineStopIndex += 1
-      }
-      val stopTruncated = lineStopIndex - stop > maxExtraContextLength
-
-      val truncatedSubText = (if (startTruncated) truncatedText else "") +
-        text.substring(lineStartIndex + 1, lineStopIndex) +
-        (if (stopTruncated) truncatedText else "")
-      val lines = truncatedSubText.split("\n")
-      lines.foreach { lineText =>
-        builder ++= lineText + "\n"
-        currentIndex += 1
-        (0 until lineText.length).foreach { _ =>
-          if (currentIndex < start) {
-            builder ++= " "
-          } else if (currentIndex >= start && currentIndex <= stop) {
-            builder ++= "^"
-          }
-          currentIndex += 1
-        }
-        builder ++= "\n"
-      }
-      builder.result()
-    }
-  }
+    line: Option[Int] = None,
+    startPosition: Option[Int] = None,
+    startIndex: Option[Int] = None,
+    stopIndex: Option[Int] = None,
+    sqlText: Option[String] = None,
+    objectType: Option[String] = None,
+    objectName: Option[String] = None) {
+
+  lazy val context: SqlQueryContext = SqlQueryContext(

Review Comment:
   this can be a val now. `SqlQueryContext` instantiation is cheap.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to