You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "zhenlineo (via GitHub)" <gi...@apache.org> on 2023/07/26 15:37:37 UTC

[GitHub] [spark] zhenlineo commented on a diff in pull request #42164: [SPARK-44538][CONNECT][SQL] Reinstate Row.jsonValue and friends

zhenlineo commented on code in PR #42164:
URL: https://github.com/apache/spark/pull/42164#discussion_r1275146511


##########
sql/api/src/main/scala/org/apache/spark/sql/errors/ExecutionErrors.scala:
##########
@@ -0,0 +1,275 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.spark.sql.errors
+
+import java.time.LocalDate
+import java.time.temporal.ChronoField
+
+import org.apache.arrow.vector.types.pojo.ArrowType
+
+import org.apache.spark.{SparkArithmeticException, SparkBuildInfo, SparkDateTimeException, SparkException, SparkIllegalArgumentException, SparkRuntimeException, SparkUnsupportedOperationException, SparkUpgradeException}
+import org.apache.spark.sql.catalyst.WalkedTypePath
+import org.apache.spark.sql.catalyst.trees.SQLQueryContext
+import org.apache.spark.sql.catalyst.util.DateTimeUtils
+import org.apache.spark.sql.internal.SqlApiConf
+import org.apache.spark.sql.types.{DataType, DoubleType, FloatType, LongType, StringType, UserDefinedType}
+import org.apache.spark.unsafe.types.UTF8String
+
+private[sql] trait ExecutionErrors extends DataTypeErrorsBase {
+  def fieldDiffersFromDerivedLocalDateError(
+      field: ChronoField,
+      actual: Int,
+      expected: Int,
+      candidate: LocalDate): SparkDateTimeException = {
+    new SparkDateTimeException(
+      errorClass = "_LEGACY_ERROR_TEMP_2129",
+      messageParameters = Map(
+        "field" -> field.toString,
+        "actual" -> actual.toString,
+        "expected" -> expected.toString,
+        "candidate" -> candidate.toString),
+      context = Array.empty,
+      summary = "")
+  }
+
+  def failToParseDateTimeInNewParserError(s: String, e: Throwable): SparkUpgradeException = {
+    new SparkUpgradeException(
+      errorClass = "INCONSISTENT_BEHAVIOR_CROSS_VERSION.PARSE_DATETIME_BY_NEW_PARSER",
+      messageParameters = Map(
+        "datetime" -> toSQLValue(s),
+        "config" -> toSQLConf(SqlApiConf.LEGACY_TIME_PARSER_POLICY_KEY)),
+      e)
+  }
+
+  def failToRecognizePatternAfterUpgradeError(
+      pattern: String, e: Throwable): SparkUpgradeException = {
+    new SparkUpgradeException(
+      errorClass = "INCONSISTENT_BEHAVIOR_CROSS_VERSION.DATETIME_PATTERN_RECOGNITION",
+      messageParameters = Map(
+        "pattern" -> toSQLValue(pattern),
+        "config" -> toSQLConf(SqlApiConf.LEGACY_TIME_PARSER_POLICY_KEY),
+        "docroot" -> SparkBuildInfo.spark_doc_root),
+      e)
+  }
+
+  def failToRecognizePatternError(pattern: String, e: Throwable): SparkRuntimeException = {
+    new SparkRuntimeException(
+      errorClass = "_LEGACY_ERROR_TEMP_2130",
+      messageParameters = Map(
+        "pattern" -> toSQLValue(pattern),
+        "docroot" -> SparkBuildInfo.spark_doc_root),
+      cause = e)
+  }
+
+  def unreachableError(err: String = ""): SparkRuntimeException = {
+    new SparkRuntimeException(
+      errorClass = "_LEGACY_ERROR_TEMP_2028",
+      messageParameters = Map("err" -> err))
+  }
+
+  def invalidInputInCastToDatetimeError(
+      value: UTF8String,
+      to: DataType,
+      context: SQLQueryContext): SparkDateTimeException = {
+    invalidInputInCastToDatetimeErrorInternal(toSQLValue(value), StringType, to, context)
+  }
+
+  def invalidInputInCastToDatetimeError(
+     value: Double,
+     to: DataType,
+     context: SQLQueryContext): SparkDateTimeException = {
+    invalidInputInCastToDatetimeErrorInternal(toSQLValue(value), DoubleType, to, context)
+  }
+
+  protected def invalidInputInCastToDatetimeErrorInternal(
+      sqlValue: String,
+      from: DataType,
+      to: DataType,
+      context: SQLQueryContext): SparkDateTimeException = {
+    new SparkDateTimeException(
+      errorClass = "CAST_INVALID_INPUT",
+      messageParameters = Map(
+        "expression" -> sqlValue,
+        "sourceType" -> toSQLType(from),
+        "targetType" -> toSQLType(to),
+        "ansiConfig" -> toSQLConf(SqlApiConf.ANSI_ENABLED_KEY)),
+      context = getQueryContext(context),
+      summary = getSummary(context))
+  }
+
+  def ansiIllegalArgumentError(message: String): SparkIllegalArgumentException = {
+    new SparkIllegalArgumentException(
+      errorClass = "_LEGACY_ERROR_TEMP_2000",
+      messageParameters = Map(
+        "message" -> message,
+        "ansiConfig" -> toSQLConf(SqlApiConf.ANSI_ENABLED_KEY)))
+  }
+
+  def timestampAddOverflowError(micros: Long, amount: Int, unit: String): ArithmeticException = {
+    new SparkArithmeticException(
+      errorClass = "DATETIME_OVERFLOW",
+      messageParameters = Map(
+        "operation" -> (s"add ${toSQLValue(amount)} $unit to " +
+          s"${DateTimeUtils.microsToInstant(micros)}")),
+      context = Array.empty,
+      summary = "")
+  }
+
+  def arithmeticOverflowError(
+      message: String,
+      hint: String = "",
+      context: SQLQueryContext = null): ArithmeticException = {
+    val alternative = if (hint.nonEmpty) {
+      s" Use '$hint' to tolerate overflow and return NULL instead."
+    } else ""
+    new SparkArithmeticException(
+      errorClass = "ARITHMETIC_OVERFLOW",
+      messageParameters = Map(
+        "message" -> message,
+        "alternative" -> alternative,
+        "config" -> toSQLConf(SqlApiConf.ANSI_ENABLED_KEY)),
+      context = getQueryContext(context),
+      summary = getSummary(context))
+  }
+
+  def cannotParseStringAsDataTypeError(pattern: String, value: String, dataType: DataType)
+  : SparkRuntimeException = {
+    new SparkRuntimeException(
+      errorClass = "_LEGACY_ERROR_TEMP_2134",
+      messageParameters = Map(
+        "value" -> toSQLValue(value),
+        "pattern" -> toSQLValue(pattern),
+        "dataType" -> dataType.toString))
+  }
+
+  def binaryArithmeticCauseOverflowError(
+      eval1: Short, symbol: String, eval2: Short): SparkArithmeticException = {
+    new SparkArithmeticException(
+      errorClass = "BINARY_ARITHMETIC_OVERFLOW",
+      messageParameters = Map(
+        "value1" -> toSQLValue(eval1),
+        "symbol" -> symbol,
+        "value2" -> toSQLValue(eval2)),
+      context = Array.empty,
+      summary = "")
+  }
+
+  def unaryMinusCauseOverflowError(originValue: Int): SparkArithmeticException = {
+    new SparkArithmeticException(
+      errorClass = "_LEGACY_ERROR_TEMP_2043",
+      messageParameters = Map("sqlValue" -> toSQLValue(originValue)),
+      context = Array.empty,
+      summary = "")
+  }
+
+  def castingCauseOverflowError(t: Long, to: DataType): ArithmeticException = {
+    castingCauseOverflowErrorInternal(toSQLValue(t), LongType, to)

Review Comment:
   Smart.



##########
sql/api/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeFormatterHelper.scala:
##########
@@ -185,20 +178,24 @@ trait DateTimeFormatterHelper {
       } catch {
         case _: Throwable => throw e
       }
-      throw QueryExecutionErrors.failToRecognizePatternAfterUpgradeError(
-        pattern, e, SPARK_DOC_ROOT)
+      throw ExecutionErrors.failToRecognizePatternAfterUpgradeError(pattern, e)
   }
 
   protected def checkInvalidPattern(pattern: String): PartialFunction[Throwable, Nothing] = {
     case e: IllegalArgumentException =>
-      throw QueryExecutionErrors.failToRecognizePatternError(pattern, e, SPARK_DOC_ROOT)
+      throw ExecutionErrors.failToRecognizePatternError(pattern, e)
   }
 }
 
 private object DateTimeFormatterHelper {
-  val cache = CacheBuilder.newBuilder()
-    .maximumSize(128)
-    .build[(String, Locale, Boolean), DateTimeFormatter]()
+  private type CacheKey = (String, Locale, Boolean)
+  private val cache: util.Map[CacheKey, DateTimeFormatter] =
+    Collections.synchronizedMap(new util.LinkedHashMap[CacheKey, DateTimeFormatter] {
+      override def removeEldestEntry(

Review Comment:
   Nice.



##########
sql/api/src/main/scala/org/apache/spark/sql/errors/ExecutionErrors.scala:
##########
@@ -0,0 +1,275 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.spark.sql.errors
+
+import java.time.LocalDate
+import java.time.temporal.ChronoField
+
+import org.apache.arrow.vector.types.pojo.ArrowType
+
+import org.apache.spark.{SparkArithmeticException, SparkBuildInfo, SparkDateTimeException, SparkException, SparkIllegalArgumentException, SparkRuntimeException, SparkUnsupportedOperationException, SparkUpgradeException}
+import org.apache.spark.sql.catalyst.WalkedTypePath
+import org.apache.spark.sql.catalyst.trees.SQLQueryContext
+import org.apache.spark.sql.catalyst.util.DateTimeUtils
+import org.apache.spark.sql.internal.SqlApiConf
+import org.apache.spark.sql.types.{DataType, DoubleType, FloatType, LongType, StringType, UserDefinedType}
+import org.apache.spark.unsafe.types.UTF8String
+
+private[sql] trait ExecutionErrors extends DataTypeErrorsBase {

Review Comment:
   nit: Maybe clarify what goes to here and what goes to DataTypeErrors?
   e.g. Not super clear to me why `unsupportedDataTypeError` belongs to here rather than `DataTypeErrors`?



-- 
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