From dc04da150e43a217b37c1fbdd6d17544569b7f27 Mon Sep 17 00:00:00 2001 From: Tengfei Huang Date: Tue, 12 Apr 2022 19:47:11 +0800 Subject: [PATCH 1/4] Use error classes in the compilation errors of not allowed DESC PARTITION --- .../main/resources/error/error-classes.json | 6 +++ .../sql/errors/QueryCompilationErrors.scala | 14 +++--- .../sql-tests/results/describe.sql.out | 4 +- .../errors/QueryCompilationErrorsSuite.scala | 50 +++++++++++++++++++ 4 files changed, 65 insertions(+), 9 deletions(-) diff --git a/core/src/main/resources/error/error-classes.json b/core/src/main/resources/error/error-classes.json index fddfb004e1ec2..05dd857563843 100644 --- a/core/src/main/resources/error/error-classes.json +++ b/core/src/main/resources/error/error-classes.json @@ -112,6 +112,12 @@ "INVALID_JSON_SCHEMA_MAPTYPE" : { "message" : [ "Input schema %s can only contain StringType as a key type for a MapType." ] }, + "INVALID_OPERATION_ON_TEMP_VIEW" : { + "message" : [ "Operation %s is not allowed on a temporary view: %s" ] + }, + "INVALID_OPERATION_ON_VIEW" : { + "message" : [ "Operation %s is not allowed on a view: %s" ] + }, "INVALID_PARAMETER_VALUE" : { "message" : [ "The value of parameter(s) '%s' in %s is invalid: %s" ], "sqlState" : "22023" diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala index b6b64804904ee..bcbd523d16068 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala @@ -46,7 +46,7 @@ import org.apache.spark.sql.types._ * As commands are executed eagerly, this also includes errors thrown during the execution of * commands, which users can see immediately. */ -object QueryCompilationErrors { +object QueryCompilationErrors extends QueryErrorsBase { def groupingIDMismatchError(groupingID: GroupingID, groupByExprs: Seq[Expression]): Throwable = { new AnalysisException( @@ -1925,11 +1925,15 @@ object QueryCompilationErrors { } def descPartitionNotAllowedOnTempView(table: String): Throwable = { - new AnalysisException(s"DESC PARTITION is not allowed on a temporary view: $table") + new AnalysisException( + errorClass = "INVALID_OPERATION_ON_TEMP_VIEW", + messageParameters = Array(toSQLValue("DESC PARTITION"), toSQLValue(table))) } def descPartitionNotAllowedOnView(table: String): Throwable = { - new AnalysisException(s"DESC PARTITION is not allowed on a view: $table") + new AnalysisException( + errorClass = "INVALID_OPERATION_ON_VIEW", + messageParameters = Array(toSQLValue("DESC PARTITION"), toSQLValue(table))) } def showPartitionNotAllowedOnTableNotPartitionedError(tableIdentWithDB: String): Throwable = { @@ -1965,10 +1969,6 @@ object QueryCompilationErrors { ) } - def descPartitionNotAllowedOnViewError(table: String): Throwable = { - new AnalysisException(s"DESC PARTITION is not allowed on a view: $table") - } - def showCreateTableAsSerdeNotAllowedOnSparkDataSourceTableError( table: TableIdentifier): Throwable = { new AnalysisException( diff --git a/sql/core/src/test/resources/sql-tests/results/describe.sql.out b/sql/core/src/test/resources/sql-tests/results/describe.sql.out index 04259c0db857c..b7b3a763c3429 100644 --- a/sql/core/src/test/resources/sql-tests/results/describe.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/describe.sql.out @@ -462,7 +462,7 @@ DESC temp_v PARTITION (c='Us', d=1) struct<> -- !query output org.apache.spark.sql.AnalysisException -DESC PARTITION is not allowed on a temporary view: temp_v +Operation 'DESC PARTITION' is not allowed on a temporary view: 'temp_v' -- !query @@ -539,7 +539,7 @@ DESC v PARTITION (c='Us', d=1) struct<> -- !query output org.apache.spark.sql.AnalysisException -DESC PARTITION is not allowed on a view: v +Operation 'DESC PARTITION' is not allowed on a view: 'v' -- !query diff --git a/sql/core/src/test/scala/org/apache/spark/sql/errors/QueryCompilationErrorsSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/errors/QueryCompilationErrorsSuite.scala index e9a86df7f07ed..a548e06c54d9b 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/errors/QueryCompilationErrorsSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/errors/QueryCompilationErrorsSuite.scala @@ -254,6 +254,56 @@ class QueryCompilationErrorsSuite extends QueryTest with SharedSparkSession { assert(e.getSqlState === "0A000") assert(e.message === "The feature is not supported: UDF class with 24 type arguments") } + + test("INVALID_OPERATION_ON_TEMP_VIEW: desc partition on a temporary view") { + val tableName: String = "t" + val tempViewName: String = "tempView" + + withTable(tableName) { + sql( + s""" + |CREATE TABLE $tableName (a STRING, b INT, c STRING, d STRING) + |USING parquet + |PARTITIONED BY (c, d) + |""".stripMargin) + + withTempView(tempViewName) { + sql(s"CREATE TEMPORARY VIEW $tempViewName as SELECT * FROM $tableName") + + val e = intercept[AnalysisException]( + sql(s"DESC TABLE $tempViewName PARTITION (c='Us', d=1)") + ) + assert(e.errorClass === Some("INVALID_OPERATION_ON_TEMP_VIEW")) + assert(e.message === + s"Operation 'DESC PARTITION' is not allowed on a temporary view: '$tempViewName'") + } + } + } + + test("INVALID_OPERATION_ON_VIEW: desc partition on a view") { + val tableName: String = "t" + val viewName: String = "view" + + withTable(tableName) { + sql( + s""" + |CREATE TABLE $tableName (a STRING, b INT, c STRING, d STRING) + |USING parquet + |PARTITIONED BY (c, d) + |""".stripMargin) + + withView(viewName) { + sql(s"CREATE VIEW $viewName as SELECT * FROM $tableName") + + val e = intercept[AnalysisException]( + sql(s"DESC TABLE $viewName PARTITION (c='Us', d=1)") + ) + assert(e.errorClass === Some("INVALID_OPERATION_ON_VIEW")) + assert(e.message === + s"Operation 'DESC PARTITION' is not allowed on a view: '$viewName'") + } + } + } } class MyCastToString extends SparkUserDefinedFunction( From d962156096775ecfbf02d41cb7fb5e218cad86fd Mon Sep 17 00:00:00 2001 From: Tengfei Huang Date: Thu, 14 Apr 2022 22:00:12 +0800 Subject: [PATCH 2/4] address comments --- .../org/apache/spark/sql/errors/QueryCompilationErrors.scala | 4 ++-- .../scala/org/apache/spark/sql/errors/QueryErrorsBase.scala | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala index c4153af5bbca9..d0a3d352a9ade 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala @@ -1932,13 +1932,13 @@ object QueryCompilationErrors extends QueryErrorsBase { def descPartitionNotAllowedOnTempView(table: String): Throwable = { new AnalysisException( errorClass = "INVALID_OPERATION_ON_TEMP_VIEW", - messageParameters = Array(toSQLValue("DESC PARTITION"), toSQLValue(table))) + messageParameters = Array(toSQLStmt("DESC PARTITION"), toSQLValue(table))) } def descPartitionNotAllowedOnView(table: String): Throwable = { new AnalysisException( errorClass = "INVALID_OPERATION_ON_VIEW", - messageParameters = Array(toSQLValue("DESC PARTITION"), toSQLValue(table))) + messageParameters = Array(toSQLStmt("DESC PARTITION"), toSQLValue(table))) } def showPartitionNotAllowedOnTableNotPartitionedError(tableIdentWithDB: String): Throwable = { diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryErrorsBase.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryErrorsBase.scala index e69e1382ecf62..230b0e021983a 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryErrorsBase.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryErrorsBase.scala @@ -44,4 +44,9 @@ trait QueryErrorsBase { def toSQLValue(v: Any, t: DataType): String = { litToErrorValue(Literal.create(v, t)) } + + // Quote sql statements in error messages. + def toSQLStmt(text: String): String = { + s"'$text'" + } } From 81e3dc6b4d1dc67169d9bbfb8d87ac3b0c84bc97 Mon Sep 17 00:00:00 2001 From: Tengfei Huang Date: Sun, 17 Apr 2022 21:54:47 +0800 Subject: [PATCH 3/4] address comments --- core/src/main/resources/error/error-classes.json | 9 +++------ .../spark/sql/errors/QueryCompilationErrors.scala | 10 ++++++---- .../resources/sql-tests/results/describe.sql.out | 4 ++-- .../sql/errors/QueryCompilationErrorsSuite.scala | 12 ++++++------ 4 files changed, 17 insertions(+), 18 deletions(-) diff --git a/core/src/main/resources/error/error-classes.json b/core/src/main/resources/error/error-classes.json index 46f2c7379b283..6d72b793bd0e5 100644 --- a/core/src/main/resources/error/error-classes.json +++ b/core/src/main/resources/error/error-classes.json @@ -51,6 +51,9 @@ "FAILED_SET_ORIGINAL_PERMISSION_BACK" : { "message" : [ "Failed to set original permission %s back to the created path: %s. Exception: %s" ] }, + "FORBIDDEN_OPERATION" : { + "message" : [ "The operation %s is not allowed on %s: %s" ] + }, "GRAPHITE_SINK_INVALID_PROTOCOL" : { "message" : [ "Invalid Graphite protocol: %s" ] }, @@ -109,12 +112,6 @@ "INVALID_JSON_SCHEMA_MAPTYPE" : { "message" : [ "Input schema %s can only contain StringType as a key type for a MapType." ] }, - "INVALID_OPERATION_ON_TEMP_VIEW" : { - "message" : [ "Operation %s is not allowed on a temporary view: %s" ] - }, - "INVALID_OPERATION_ON_VIEW" : { - "message" : [ "Operation %s is not allowed on a view: %s" ] - }, "INVALID_PANDAS_UDF_PLACEMENT" : { "message" : [ "The group aggregate pandas UDF %s cannot be invoked together with as other, non-pandas aggregate functions." ] }, diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala index d0a3d352a9ade..05422bcb26492 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala @@ -1931,14 +1931,16 @@ object QueryCompilationErrors extends QueryErrorsBase { def descPartitionNotAllowedOnTempView(table: String): Throwable = { new AnalysisException( - errorClass = "INVALID_OPERATION_ON_TEMP_VIEW", - messageParameters = Array(toSQLStmt("DESC PARTITION"), toSQLValue(table))) + errorClass = "FORBIDDEN_OPERATION", + messageParameters = + Array(toSQLStmt("DESC PARTITION"), "the temporary view", toSQLValue(table))) } def descPartitionNotAllowedOnView(table: String): Throwable = { new AnalysisException( - errorClass = "INVALID_OPERATION_ON_VIEW", - messageParameters = Array(toSQLStmt("DESC PARTITION"), toSQLValue(table))) + errorClass = "FORBIDDEN_OPERATION", + messageParameters = Array( + toSQLStmt("DESC PARTITION"), "the view", toSQLValue(table))) } def showPartitionNotAllowedOnTableNotPartitionedError(tableIdentWithDB: String): Throwable = { diff --git a/sql/core/src/test/resources/sql-tests/results/describe.sql.out b/sql/core/src/test/resources/sql-tests/results/describe.sql.out index b7b3a763c3429..6ba22b84b6f6d 100644 --- a/sql/core/src/test/resources/sql-tests/results/describe.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/describe.sql.out @@ -462,7 +462,7 @@ DESC temp_v PARTITION (c='Us', d=1) struct<> -- !query output org.apache.spark.sql.AnalysisException -Operation 'DESC PARTITION' is not allowed on a temporary view: 'temp_v' +The operation 'DESC PARTITION' is not allowed on the temporary view: 'temp_v' -- !query @@ -539,7 +539,7 @@ DESC v PARTITION (c='Us', d=1) struct<> -- !query output org.apache.spark.sql.AnalysisException -Operation 'DESC PARTITION' is not allowed on a view: 'v' +The operation 'DESC PARTITION' is not allowed on the view: 'v' -- !query diff --git a/sql/core/src/test/scala/org/apache/spark/sql/errors/QueryCompilationErrorsSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/errors/QueryCompilationErrorsSuite.scala index 7c9642021db0e..3e262c161a8ca 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/errors/QueryCompilationErrorsSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/errors/QueryCompilationErrorsSuite.scala @@ -311,7 +311,7 @@ class QueryCompilationErrorsSuite extends QueryTest with SharedSparkSession { } } - test("INVALID_OPERATION_ON_TEMP_VIEW: desc partition on a temporary view") { + test("FORBIDDEN_OPERATION: desc partition on a temporary view") { val tableName: String = "t" val tempViewName: String = "tempView" @@ -329,14 +329,14 @@ class QueryCompilationErrorsSuite extends QueryTest with SharedSparkSession { val e = intercept[AnalysisException]( sql(s"DESC TABLE $tempViewName PARTITION (c='Us', d=1)") ) - assert(e.errorClass === Some("INVALID_OPERATION_ON_TEMP_VIEW")) + assert(e.getErrorClass === "FORBIDDEN_OPERATION") assert(e.message === - s"Operation 'DESC PARTITION' is not allowed on a temporary view: '$tempViewName'") + s"The operation 'DESC PARTITION' is not allowed on the temporary view: '$tempViewName'") } } } - test("INVALID_OPERATION_ON_VIEW: desc partition on a view") { + test("FORBIDDEN_OPERATION: desc partition on a view") { val tableName: String = "t" val viewName: String = "view" @@ -354,9 +354,9 @@ class QueryCompilationErrorsSuite extends QueryTest with SharedSparkSession { val e = intercept[AnalysisException]( sql(s"DESC TABLE $viewName PARTITION (c='Us', d=1)") ) - assert(e.errorClass === Some("INVALID_OPERATION_ON_VIEW")) + assert(e.getErrorClass === "FORBIDDEN_OPERATION") assert(e.message === - s"Operation 'DESC PARTITION' is not allowed on a view: '$viewName'") + s"The operation 'DESC PARTITION' is not allowed on the view: '$viewName'") } } } From 4bf23fd94017f029084c281adc8058d117cea8f3 Mon Sep 17 00:00:00 2001 From: Tengfei Huang Date: Mon, 18 Apr 2022 08:38:13 +0800 Subject: [PATCH 4/4] address comments --- .../org/apache/spark/sql/errors/QueryCompilationErrors.scala | 4 ++-- .../src/test/resources/sql-tests/results/describe.sql.out | 4 ++-- .../apache/spark/sql/errors/QueryCompilationErrorsSuite.scala | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala index c678f52232131..5ee64ad1b6c67 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala @@ -1934,14 +1934,14 @@ object QueryCompilationErrors extends QueryErrorsBase { new AnalysisException( errorClass = "FORBIDDEN_OPERATION", messageParameters = - Array(toSQLStmt("DESC PARTITION"), "the temporary view", toSQLValue(table))) + Array(toSQLStmt("DESC PARTITION"), "the temporary view", toSQLId(table))) } def descPartitionNotAllowedOnView(table: String): Throwable = { new AnalysisException( errorClass = "FORBIDDEN_OPERATION", messageParameters = Array( - toSQLStmt("DESC PARTITION"), "the view", toSQLValue(table))) + toSQLStmt("DESC PARTITION"), "the view", toSQLId(table))) } def showPartitionNotAllowedOnTableNotPartitionedError(tableIdentWithDB: String): Throwable = { diff --git a/sql/core/src/test/resources/sql-tests/results/describe.sql.out b/sql/core/src/test/resources/sql-tests/results/describe.sql.out index 6ba22b84b6f6d..c0db04fa5c86b 100644 --- a/sql/core/src/test/resources/sql-tests/results/describe.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/describe.sql.out @@ -462,7 +462,7 @@ DESC temp_v PARTITION (c='Us', d=1) struct<> -- !query output org.apache.spark.sql.AnalysisException -The operation 'DESC PARTITION' is not allowed on the temporary view: 'temp_v' +The operation 'DESC PARTITION' is not allowed on the temporary view: `temp_v` -- !query @@ -539,7 +539,7 @@ DESC v PARTITION (c='Us', d=1) struct<> -- !query output org.apache.spark.sql.AnalysisException -The operation 'DESC PARTITION' is not allowed on the view: 'v' +The operation 'DESC PARTITION' is not allowed on the view: `v` -- !query diff --git a/sql/core/src/test/scala/org/apache/spark/sql/errors/QueryCompilationErrorsSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/errors/QueryCompilationErrorsSuite.scala index 5091a95639f8d..297788db739f7 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/errors/QueryCompilationErrorsSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/errors/QueryCompilationErrorsSuite.scala @@ -331,7 +331,7 @@ class QueryCompilationErrorsSuite extends QueryTest with SharedSparkSession { ) assert(e.getErrorClass === "FORBIDDEN_OPERATION") assert(e.message === - s"The operation 'DESC PARTITION' is not allowed on the temporary view: '$tempViewName'") + s"The operation 'DESC PARTITION' is not allowed on the temporary view: `$tempViewName`") } } } @@ -356,7 +356,7 @@ class QueryCompilationErrorsSuite extends QueryTest with SharedSparkSession { ) assert(e.getErrorClass === "FORBIDDEN_OPERATION") assert(e.message === - s"The operation 'DESC PARTITION' is not allowed on the view: '$viewName'") + s"The operation 'DESC PARTITION' is not allowed on the view: `$viewName`") } } }