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 2022/03/22 12:48:39 UTC

[GitHub] [spark] gengliangwang opened a new pull request #35936: [SPARK-38616][SQL] Keep track of SQL query text in Catalyst TreeNode

gengliangwang opened a new pull request #35936:
URL: https://github.com/apache/spark/pull/35936


   <!--
   Thanks for sending a pull request!  Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
     2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
     3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
     4. Be sure to keep the PR description updated to reflect all changes.
     5. Please write your PR title to summarize what this PR proposes.
     6. If possible, provide a concise example to reproduce the issue for a faster review.
     7. If you want to add a new configuration, please read the guideline first for naming configurations in
        'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
     8. If you want to add or modify an error type or message, please read the guideline first in
        'core/src/main/resources/error/README.md'.
   -->
   
   ### What changes were proposed in this pull request?
   <!--
   Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue. 
   If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
     1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
     2. If you fix some SQL features, you can provide some references of other DBMSes.
     3. If there is design documentation, please add the link.
     4. If there is a discussion in the mailing list, please add the link.
   -->
   Spark SQL uses the class Origin for tracking the position of each TreeNode in the SQL query text. When there is a parser error, we can show the position info in the error message:
   ```
   > sql("create tabe foo(i int)")
   org.apache.spark.sql.catalyst.parser.ParseException:
   no viable alternative at input 'create tabe'(line 1, pos 7)
   
   
   == SQL ==
   create tabe foo(i int)
   -------^^^ 
   ```
   It contains two fields: line and startPosition. This is enough for the parser since the SQL query text is known.
   
   However, the SQL query text is unknown in the execution phase. Spark SQL can't show the problematic SQL clause on ANSI runtime failures. 
   This PR is to include the query text in Origin. After this, we can provide details in the error messages of Expressions which can throw runtime exceptions when ANSI mode is on.
   
   ### Why are the changes needed?
   <!--
   Please clarify why the changes are needed. For instance,
     1. If you propose a new API, clarify the use case for a new API.
     2. If you fix a bug, you can clarify why it is a bug.
   -->
   Currently,  there is not enough error context for runtime ANSI failures.
   
   In the following example, the error message only tells that there is a "divide by zero" error, without pointing out where the exact SQL statement is.
   ```
   > SELECT
     ss1.ca_county,
     ss1.d_year,
     ws2.web_sales / ws1.web_sales web_q1_q2_increase,
     ss2.store_sales / ss1.store_sales store_q1_q2_increase,
     ws3.web_sales / ws2.web_sales web_q2_q3_increase,
     ss3.store_sales / ss2.store_sales store_q2_q3_increase
   FROM
     ss ss1, ss ss2, ss ss3, ws ws1, ws ws2, ws ws3
   WHERE
     ss1.d_qoy = 1
       AND ss1.d_year = 2000
       AND ss1.ca_county = ss2.ca_county
       AND ss2.d_qoy = 2
       AND ss2.d_year = 2000
       AND ss2.ca_county = ss3.ca_county
       AND ss3.d_qoy = 3
       AND ss3.d_year = 2000
       AND ss1.ca_county = ws1.ca_county
       AND ws1.d_qoy = 1
       AND ws1.d_year = 2000
       AND ws1.ca_county = ws2.ca_county
       AND ws2.d_qoy = 2
       AND ws2.d_year = 2000
       AND ws1.ca_county = ws3.ca_county
       AND ws3.d_qoy = 3
       AND ws3.d_year = 2000
       AND CASE WHEN ws1.web_sales > 0
       THEN ws2.web_sales / ws1.web_sales
           ELSE NULL END
       > CASE WHEN ss1.store_sales > 0
       THEN ss2.store_sales / ss1.store_sales
         ELSE NULL END
       AND CASE WHEN ws2.web_sales > 0
       THEN ws3.web_sales / ws2.web_sales
           ELSE NULL END
       > CASE WHEN ss2.store_sales > 0
       THEN ss3.store_sales / ss2.store_sales
         ELSE NULL END
   ORDER BY ss1.ca_county
   ```
   
   ```
   org.apache.spark.SparkArithmeticException: divide by zero at 
   org.apache.spark.sql.errors.QueryExecutionErrors$.divideByZeroError(QueryExecutionErrors.scala:140) at 
   org.apache.spark.sql.catalyst.expressions.DivModLike.eval(arithmetic.scala:437) at 
   org.apache.spark.sql.catalyst.expressions.DivModLike.eval$(arithmetic.scala:425) at 
   org.apache.spark.sql.catalyst.expressions.Divide.eval(arithmetic.scala:534)
   ```
   This PR is the initial PR for the project https://issues.apache.org/jira/browse/SPARK-38615
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as the documentation fix.
   If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   No
   
   ### How was this patch tested?
   <!--
   If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
   If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why it was difficult to add.
   If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
   -->
   UT


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



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


[GitHub] [spark] cloud-fan commented on a change in pull request #35936: [SPARK-38616][SQL] Keep track of SQL query text in Catalyst TreeNode

Posted by GitBox <gi...@apache.org>.
cloud-fan commented on a change in pull request #35936:
URL: https://github.com/apache/spark/pull/35936#discussion_r835337969



##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalog.scala
##########
@@ -873,9 +874,20 @@ class SessionCatalog(
       throw new IllegalStateException("Invalid view without text.")
     }
     val viewConfigs = metadata.viewSQLConfigs
+    val origin = Origin(
+      line = Some(0),
+      startPosition = Some(0),
+      startIndex = Some(0),
+      stopIndex = Some(viewText.length - 1),
+      sqlText = Some(viewText),
+      objectType = Some("VIEW"),
+      objectName = Some(metadata.qualifiedName)

Review comment:
       I think we only need to set `objectType` and `objectName` here? The parser should take care of the other fields.




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



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


[GitHub] [spark] cloud-fan commented on a change in pull request #35936: [SPARK-38616][SQL] Keep track of SQL query text in Catalyst TreeNode

Posted by GitBox <gi...@apache.org>.
cloud-fan commented on a change in pull request #35936:
URL: https://github.com/apache/spark/pull/35936#discussion_r832873829



##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/ParseDriver.scala
##########
@@ -80,11 +81,14 @@ abstract class AbstractSqlParser extends ParserInterface with SQLConfHelper with
 
   /** Creates LogicalPlan for a given SQL string. */
   override def parsePlan(sqlText: String): LogicalPlan = parse(sqlText) { parser =>

Review comment:
       Is it the only place we need to take care? There are `parseQuery` and `parseExpression` methods in this file




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



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


[GitHub] [spark] gengliangwang closed pull request #35936: [SPARK-38616][SQL] Keep track of SQL query text in Catalyst TreeNode

Posted by GitBox <gi...@apache.org>.
gengliangwang closed pull request #35936:
URL: https://github.com/apache/spark/pull/35936


   


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



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


[GitHub] [spark] gengliangwang commented on a change in pull request #35936: [SPARK-38616][SQL] Keep track of SQL query text in Catalyst TreeNode

Posted by GitBox <gi...@apache.org>.
gengliangwang commented on a change in pull request #35936:
URL: https://github.com/apache/spark/pull/35936#discussion_r832882900



##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/ParseDriver.scala
##########
@@ -80,11 +81,14 @@ abstract class AbstractSqlParser extends ParserInterface with SQLConfHelper with
 
   /** Creates LogicalPlan for a given SQL string. */
   override def parsePlan(sqlText: String): LogicalPlan = parse(sqlText) { parser =>

Review comment:
       You are right. Updated




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



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


[GitHub] [spark] gengliangwang commented on pull request #35936: [SPARK-38616][SQL] Keep track of SQL query text in Catalyst TreeNode

Posted by GitBox <gi...@apache.org>.
gengliangwang commented on pull request #35936:
URL: https://github.com/apache/spark/pull/35936#issuecomment-1080150293


   Since this is for improving error message, I am merging it to master/3.3
   cc @MaxGekk 


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



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


[GitHub] [spark] gengliangwang commented on a change in pull request #35936: [SPARK-38616][SQL] Keep track of SQL query text in Catalyst TreeNode

Posted by GitBox <gi...@apache.org>.
gengliangwang commented on a change in pull request #35936:
URL: https://github.com/apache/spark/pull/35936#discussion_r832884518



##########
File path: sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/ParserUtilsSuite.scala
##########
@@ -199,4 +217,42 @@ class ParserUtilsSuite extends SparkFunSuite {
     assert(origin == Origin(Some(3), Some(27)))
     assert(CurrentOrigin.get == current)
   }
+
+  private def findCastContext(ctx: ParserRuleContext): Option[CastContext] = {
+    ctx match {
+      case context: CastContext =>
+        Some(context)
+      case _ =>
+        val it = ctx.children.iterator()
+        while(it.hasNext) {
+          it.next() match {
+            case p: ParserRuleContext =>
+              val childResult = findCastContext(p)
+              if (childResult.isDefined) {
+                return childResult
+              }
+            case _ =>
+          }
+        }
+        None
+    }
+  }
+
+  test("withOrigin: setting SQL text") {
+    withOrigin(castQueryContext, Some(castQuery)) {
+      assert(CurrentOrigin.get.sqlText.contains(castQuery))
+      val castContext = findCastContext(castQueryContext)
+      assert(castContext.isDefined)
+      withOrigin(castContext.get) {
+        val current = CurrentOrigin.get
+        assert(current.sqlText.contains(castQuery))
+        assert(current.startIndex.isDefined)
+        assert(current.stopIndex.isDefined)
+        // With sqlText, startIndex, stopIndex, we can get the corresponding SQL text of the
+        // Cast clause.
+        assert(current.sqlText.get.substring(current.startIndex.get, current.stopIndex.get + 1) ==

Review comment:
       Yeah I am thinking about having a method for building the whole error message like:
   ```
   (line 1, pos 7)
   == SQL ==
   select cast('a' as int)
   -------^^^ ^^^^^^^
   ```
   This will be added after we decide the error message format in another PR.




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



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


[GitHub] [spark] cloud-fan commented on a change in pull request #35936: [SPARK-38616][SQL] Keep track of SQL query text in Catalyst TreeNode

Posted by GitBox <gi...@apache.org>.
cloud-fan commented on a change in pull request #35936:
URL: https://github.com/apache/spark/pull/35936#discussion_r835342575



##########
File path: sql/core/src/test/scala/org/apache/spark/sql/execution/SQLViewSuite.scala
##########
@@ -909,6 +912,53 @@ abstract class SQLViewSuite extends QueryTest with SQLTestUtils {
     }
   }
 
+  test("CurrentOrigin is correctly set in and out of the View") {
+    withTable("t") {
+      Seq((1, 1), (2, 2)).toDF("a", "b").write.format("parquet").saveAsTable("t")
+      Seq("", "temp")
+      withView("v") {
+        val viewText = "SELECT a + b c FROM t"
+        sql(
+          s"""
+            |CREATE VIEW v AS
+            |-- the body of the view
+            |$viewText
+            |""".stripMargin)
+        val plan = sql("select c / 2.0D d from v").logicalPlan
+        val add = plan.collectFirst {
+          case Project(Seq(Alias(a: Add, _)), _) =>
+              a
+        }
+        assert(add.isDefined)
+        val expectedAddOrigin = Origin(
+          line = Some(1),
+          startPosition = Some(7),
+          startIndex = Some(7),
+          stopIndex = Some(11),
+          sqlText = Some("SELECT a + b c FROM t"),
+          objectType = Some("VIEW"),
+          objectName = Some("default.v")
+        )
+        assert(add.get.origin == expectedAddOrigin)
+
+        val divide = plan.collectFirst {
+          case Project(Seq(Alias(d: Divide, _)), _) =>
+            d

Review comment:
       ```suggestion
             case Project(Seq(Alias(d: Divide, _)), _) => d
   ```




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



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


[GitHub] [spark] cloud-fan commented on a change in pull request #35936: [SPARK-38616][SQL] Keep track of SQL query text in Catalyst TreeNode

Posted by GitBox <gi...@apache.org>.
cloud-fan commented on a change in pull request #35936:
URL: https://github.com/apache/spark/pull/35936#discussion_r835343829



##########
File path: sql/core/src/test/scala/org/apache/spark/sql/execution/SQLViewSuite.scala
##########
@@ -909,6 +912,53 @@ abstract class SQLViewSuite extends QueryTest with SQLTestUtils {
     }
   }
 
+  test("CurrentOrigin is correctly set in and out of the View") {

Review comment:
       A better place to put this test is `PersistedViewTestSuite`




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



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


[GitHub] [spark] gengliangwang commented on pull request #35936: [SPARK-38616][SQL] Keep track of SQL query text in Catalyst TreeNode

Posted by GitBox <gi...@apache.org>.
gengliangwang commented on pull request #35936:
URL: https://github.com/apache/spark/pull/35936#issuecomment-1075141041


   I am still deciding whether to show the whole original query text or just the problematic part in the runtime exception message. Thus I didn't change the error message of an `Expression` as an example in this PR. 
   
   cc @entong @cloud-fan let me know if you think we need an example and end-to-end test.


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



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


[GitHub] [spark] gengliangwang commented on a change in pull request #35936: [SPARK-38616][SQL] Keep track of SQL query text in Catalyst TreeNode

Posted by GitBox <gi...@apache.org>.
gengliangwang commented on a change in pull request #35936:
URL: https://github.com/apache/spark/pull/35936#discussion_r835393934



##########
File path: sql/core/src/test/scala/org/apache/spark/sql/execution/SQLViewSuite.scala
##########
@@ -909,6 +912,53 @@ abstract class SQLViewSuite extends QueryTest with SQLTestUtils {
     }
   }
 
+  test("CurrentOrigin is correctly set in and out of the View") {
+    withTable("t") {
+      Seq((1, 1), (2, 2)).toDF("a", "b").write.format("parquet").saveAsTable("t")
+      Seq("", "temp")

Review comment:
       I just updated.




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



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


[GitHub] [spark] cloud-fan commented on a change in pull request #35936: [SPARK-38616][SQL] Keep track of SQL query text in Catalyst TreeNode

Posted by GitBox <gi...@apache.org>.
cloud-fan commented on a change in pull request #35936:
URL: https://github.com/apache/spark/pull/35936#discussion_r835342162



##########
File path: sql/core/src/test/scala/org/apache/spark/sql/execution/SQLViewSuite.scala
##########
@@ -909,6 +912,53 @@ abstract class SQLViewSuite extends QueryTest with SQLTestUtils {
     }
   }
 
+  test("CurrentOrigin is correctly set in and out of the View") {
+    withTable("t") {
+      Seq((1, 1), (2, 2)).toDF("a", "b").write.format("parquet").saveAsTable("t")
+      Seq("", "temp")
+      withView("v") {
+        val viewText = "SELECT a + b c FROM t"
+        sql(
+          s"""
+            |CREATE VIEW v AS
+            |-- the body of the view
+            |$viewText
+            |""".stripMargin)
+        val plan = sql("select c / 2.0D d from v").logicalPlan
+        val add = plan.collectFirst {
+          case Project(Seq(Alias(a: Add, _)), _) =>
+              a

Review comment:
       ```suggestion
             case Project(Seq(Alias(a: Add, _)), _) => a
   ```




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



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


[GitHub] [spark] cloud-fan commented on a change in pull request #35936: [SPARK-38616][SQL] Keep track of SQL query text in Catalyst TreeNode

Posted by GitBox <gi...@apache.org>.
cloud-fan commented on a change in pull request #35936:
URL: https://github.com/apache/spark/pull/35936#discussion_r835341900



##########
File path: sql/core/src/test/scala/org/apache/spark/sql/execution/SQLViewSuite.scala
##########
@@ -909,6 +912,53 @@ abstract class SQLViewSuite extends QueryTest with SQLTestUtils {
     }
   }
 
+  test("CurrentOrigin is correctly set in and out of the View") {
+    withTable("t") {
+      Seq((1, 1), (2, 2)).toDF("a", "b").write.format("parquet").saveAsTable("t")
+      Seq("", "temp")
+      withView("v") {
+        val viewText = "SELECT a + b c FROM t"
+        sql(
+          s"""
+            |CREATE VIEW v AS
+            |-- the body of the view
+            |$viewText
+            |""".stripMargin)
+        val plan = sql("select c / 2.0D d from v").logicalPlan
+        val add = plan.collectFirst {
+          case Project(Seq(Alias(a: Add, _)), _) =>
+              a

Review comment:
       ```suggestion
               a
   ```




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



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


[GitHub] [spark] gengliangwang commented on a change in pull request #35936: [SPARK-38616][SQL] Keep track of SQL query text in Catalyst TreeNode

Posted by GitBox <gi...@apache.org>.
gengliangwang commented on a change in pull request #35936:
URL: https://github.com/apache/spark/pull/35936#discussion_r835394563



##########
File path: sql/core/src/test/scala/org/apache/spark/sql/execution/SQLViewSuite.scala
##########
@@ -909,6 +912,53 @@ abstract class SQLViewSuite extends QueryTest with SQLTestUtils {
     }
   }
 
+  test("CurrentOrigin is correctly set in and out of the View") {

Review comment:
       `HiveSQLViewSuite` inherits this one and we can test Hive as well.




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



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


[GitHub] [spark] gengliangwang commented on a change in pull request #35936: [SPARK-38616][SQL] Keep track of SQL query text in Catalyst TreeNode

Posted by GitBox <gi...@apache.org>.
gengliangwang commented on a change in pull request #35936:
URL: https://github.com/apache/spark/pull/35936#discussion_r832136166



##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/ParseDriver.scala
##########
@@ -80,11 +81,14 @@ abstract class AbstractSqlParser extends ParserInterface with SQLConfHelper with
 
   /** Creates LogicalPlan for a given SQL string. */
   override def parsePlan(sqlText: String): LogicalPlan = parse(sqlText) { parser =>
-    astBuilder.visitSingleStatement(parser.singleStatement()) match {
-      case plan: LogicalPlan => plan
-      case _ =>
-        val position = Origin(None, None)
-        throw QueryParsingErrors.sqlStatementUnsupportedError(sqlText, position)
+    val ctx = parser.singleStatement()
+    withOrigin(ctx, Some(sqlText)) {

Review comment:
       Different from https://github.com/apache/spark/pull/35926, here it passes the original SQL command to all the `Origin`s in the LogicalPlan. This can save memory usage, especially when the query is long.




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



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


[GitHub] [spark] cloud-fan commented on a change in pull request #35936: [SPARK-38616][SQL] Keep track of SQL query text in Catalyst TreeNode

Posted by GitBox <gi...@apache.org>.
cloud-fan commented on a change in pull request #35936:
URL: https://github.com/apache/spark/pull/35936#discussion_r832875864



##########
File path: sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/ParserUtilsSuite.scala
##########
@@ -199,4 +217,42 @@ class ParserUtilsSuite extends SparkFunSuite {
     assert(origin == Origin(Some(3), Some(27)))
     assert(CurrentOrigin.get == current)
   }
+
+  private def findCastContext(ctx: ParserRuleContext): Option[CastContext] = {
+    ctx match {
+      case context: CastContext =>
+        Some(context)
+      case _ =>
+        val it = ctx.children.iterator()
+        while(it.hasNext) {
+          it.next() match {
+            case p: ParserRuleContext =>
+              val childResult = findCastContext(p)
+              if (childResult.isDefined) {
+                return childResult
+              }
+            case _ =>
+          }
+        }
+        None
+    }
+  }
+
+  test("withOrigin: setting SQL text") {
+    withOrigin(castQueryContext, Some(castQuery)) {
+      assert(CurrentOrigin.get.sqlText.contains(castQuery))
+      val castContext = findCastContext(castQueryContext)
+      assert(castContext.isDefined)
+      withOrigin(castContext.get) {
+        val current = CurrentOrigin.get
+        assert(current.sqlText.contains(castQuery))
+        assert(current.startIndex.isDefined)
+        assert(current.stopIndex.isDefined)
+        // With sqlText, startIndex, stopIndex, we can get the corresponding SQL text of the
+        // Cast clause.
+        assert(current.sqlText.get.substring(current.startIndex.get, current.stopIndex.get + 1) ==

Review comment:
       shall we put the substring calculation in `Origin` as a method?




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



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


[GitHub] [spark] cloud-fan commented on a change in pull request #35936: [SPARK-38616][SQL] Keep track of SQL query text in Catalyst TreeNode

Posted by GitBox <gi...@apache.org>.
cloud-fan commented on a change in pull request #35936:
URL: https://github.com/apache/spark/pull/35936#discussion_r832873829



##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/ParseDriver.scala
##########
@@ -80,11 +81,14 @@ abstract class AbstractSqlParser extends ParserInterface with SQLConfHelper with
 
   /** Creates LogicalPlan for a given SQL string. */
   override def parsePlan(sqlText: String): LogicalPlan = parse(sqlText) { parser =>

Review comment:
       Is it the only place we need to take care? There is a `parseQuery` method in this file




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



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


[GitHub] [spark] gengliangwang commented on a change in pull request #35936: [SPARK-38616][SQL] Keep track of SQL query text in Catalyst TreeNode

Posted by GitBox <gi...@apache.org>.
gengliangwang commented on a change in pull request #35936:
URL: https://github.com/apache/spark/pull/35936#discussion_r835376637



##########
File path: sql/core/src/test/scala/org/apache/spark/sql/execution/SQLViewSuite.scala
##########
@@ -909,6 +912,53 @@ abstract class SQLViewSuite extends QueryTest with SQLTestUtils {
     }
   }
 
+  test("CurrentOrigin is correctly set in and out of the View") {
+    withTable("t") {
+      Seq((1, 1), (2, 2)).toDF("a", "b").write.format("parquet").saveAsTable("t")
+      Seq("", "temp")

Review comment:
       I intended to try both view/temp view...forgot to update it here.




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



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


[GitHub] [spark] cloud-fan commented on a change in pull request #35936: [SPARK-38616][SQL] Keep track of SQL query text in Catalyst TreeNode

Posted by GitBox <gi...@apache.org>.
cloud-fan commented on a change in pull request #35936:
URL: https://github.com/apache/spark/pull/35936#discussion_r835341324



##########
File path: sql/core/src/test/scala/org/apache/spark/sql/execution/SQLViewSuite.scala
##########
@@ -909,6 +912,53 @@ abstract class SQLViewSuite extends QueryTest with SQLTestUtils {
     }
   }
 
+  test("CurrentOrigin is correctly set in and out of the View") {
+    withTable("t") {
+      Seq((1, 1), (2, 2)).toDF("a", "b").write.format("parquet").saveAsTable("t")
+      Seq("", "temp")

Review comment:
       What does this line do?




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



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


[GitHub] [spark] gengliangwang commented on pull request #35936: [WIP][SPARK-38616][SQL] Keep track of SQL query text in Catalyst TreeNode

Posted by GitBox <gi...@apache.org>.
gengliangwang commented on pull request #35936:
URL: https://github.com/apache/spark/pull/35936#issuecomment-1076393568


   Marking this PR as WIP for now. I am thinking about the following two approaches:
   * Extend Origin(this PR): Extend and keep track of the user query text in Expression.origin and show it in the runtime error message.  Currently,  Expression.origin contains only the starting line number and position.
   Example error message:
   == SQL ==
   select cast('a' as int)
   --------^^^^^^^^^^^^^
   
   * Use sql() + Origin: Show Expression.sql and Expression.orgin(starting line number + position) in the runtime error message directly. Both APIs exist for years and they are well maintained.
   Example error message:
   == SQL(line 1, pos 7) ==
   cast('a' as int)
   
   


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



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


[GitHub] [spark] gengliangwang commented on a change in pull request #35936: [SPARK-38616][SQL] Keep track of SQL query text in Catalyst TreeNode

Posted by GitBox <gi...@apache.org>.
gengliangwang commented on a change in pull request #35936:
URL: https://github.com/apache/spark/pull/35936#discussion_r832143695



##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/trees/TreeNode.scala
##########
@@ -54,7 +54,10 @@ private class MutableInt(var i: Int)
 
 case class Origin(
   line: Option[Int] = None,

Review comment:
       With startIndex & stopIndex, we can actually remove line & startPosition.
   To make sure this can merge into 3.3, I will do the code clean up later.




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



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