From b9049843b7c0f8f63a6ec39fd086c5718fe1f277 Mon Sep 17 00:00:00 2001 From: AgenticSpark Date: Wed, 1 Jul 2026 16:37:07 +0200 Subject: [PATCH] [SPARK-21529][SQL] Improve the error message for unsupported Hive union type Detect unsupported Hive `uniontype<...>` values when converting Hive `FieldSchema` types to Spark SQL types and raise a dedicated `UNSUPPORTED_HIVE_TYPE` error instead of the generic `CANNOT_RECOGNIZE_HIVE_TYPE` parser error. Spark SQL does not support Hive union types. Today the failure message comes from the parser path and does not clearly identify that the Hive union type is unsupported. Yes. Reading a Hive table column that uses `uniontype<...>` now reports `UNSUPPORTED_HIVE_TYPE` with the offending Hive type and column name. - `SPARK_GENERATE_GOLDEN_FILES=1 build/sbt "core/testOnly *SparkThrowableSuite -- -t \"Error conditions are correctly formatted\""` - `build/sbt "hive/testOnly *HiveClientImplSuite"` Yes. GitHub Copilot assisted with preparing and validating this change. Closes #56775 from AgenticSpark/agenticspark/SPARK-21529-uniontype-error. Authored-by: AgenticSpark Signed-off-by: Max Gekk (cherry picked from commit c90cad6ad2d1c70e630f55aab6b652f4ba9fcfb4) --- .../resources/error/error-conditions.json | 6 +++ .../sql/errors/QueryExecutionErrors.scala | 7 +++ .../sql/hive/client/HiveClientImpl.scala | 5 ++ .../sql/hive/client/HiveClientImplSuite.scala | 49 +++++++++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 sql/hive/src/test/scala/org/apache/spark/sql/hive/client/HiveClientImplSuite.scala diff --git a/common/utils/src/main/resources/error/error-conditions.json b/common/utils/src/main/resources/error/error-conditions.json index 08666ad314fca..5cec6248e1f96 100644 --- a/common/utils/src/main/resources/error/error-conditions.json +++ b/common/utils/src/main/resources/error/error-conditions.json @@ -8641,6 +8641,12 @@ ], "sqlState" : "42K0E" }, + "UNSUPPORTED_HIVE_TYPE" : { + "message" : [ + "Cannot read the Hive type of the column because Spark SQL does not support this data type." + ], + "sqlState" : "0A000" + }, "UNSUPPORTED_INSERT" : { "message" : [ "Can't insert into the target." diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala index fda9cc9b21810..41930cafdd2d9 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala @@ -1696,6 +1696,13 @@ private[sql] object QueryExecutionErrors extends QueryErrorsBase with ExecutionE cause = e) } + def unsupportedHiveTypeError(fieldType: String, fieldName: String): Throwable = { + new SparkUnsupportedOperationException( + errorClass = "UNSUPPORTED_HIVE_TYPE", + messageParameters = Map( + "fieldType" -> toSQLType(fieldType), + "fieldName" -> toSQLId(fieldName))) + } def getTablesByTypeUnsupportedByHiveVersionError(): SparkUnsupportedOperationException = { new SparkUnsupportedOperationException( errorClass = "GET_TABLES_BY_TYPE_UNSUPPORTED_BY_HIVE_VERSION") diff --git a/sql/hive/src/main/scala/org/apache/spark/sql/hive/client/HiveClientImpl.scala b/sql/hive/src/main/scala/org/apache/spark/sql/hive/client/HiveClientImpl.scala index 21db79116b52e..f09f076707be2 100644 --- a/sql/hive/src/main/scala/org/apache/spark/sql/hive/client/HiveClientImpl.scala +++ b/sql/hive/src/main/scala/org/apache/spark/sql/hive/client/HiveClientImpl.scala @@ -1139,6 +1139,11 @@ private[hive] object HiveClientImpl extends Logging { CatalystSqlParser.parseDataType(typeStr) } catch { case e: ParseException => + // Hive's union type (uniontype<...>) is not supported by Spark SQL and makes the parser + // fail with a generic message. Detect it and report a clearer error (SPARK-21529). + if (hc.getType.toLowerCase(Locale.ROOT).contains("uniontype<")) { + throw QueryExecutionErrors.unsupportedHiveTypeError(hc.getType, hc.getName) + } throw QueryExecutionErrors.cannotRecognizeHiveTypeError(e, typeStr, hc.getName) } } diff --git a/sql/hive/src/test/scala/org/apache/spark/sql/hive/client/HiveClientImplSuite.scala b/sql/hive/src/test/scala/org/apache/spark/sql/hive/client/HiveClientImplSuite.scala new file mode 100644 index 0000000000000..9bee44f8f57e9 --- /dev/null +++ b/sql/hive/src/test/scala/org/apache/spark/sql/hive/client/HiveClientImplSuite.scala @@ -0,0 +1,49 @@ +/* + * 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.hive.client + +import org.apache.hadoop.hive.metastore.api.FieldSchema + +import org.apache.spark.{SparkFunSuite, SparkUnsupportedOperationException} + +class HiveClientImplSuite extends SparkFunSuite { + + test("SPARK-21529: a clear error is raised for an unsupported Hive union type") { + val column = new FieldSchema("c", "uniontype", null) + checkError( + exception = intercept[SparkUnsupportedOperationException] { + HiveClientImpl.fromHiveColumn(column) + }, + condition = "UNSUPPORTED_HIVE_TYPE", + parameters = Map( + "fieldType" -> "\"UNIONTYPE\"", + "fieldName" -> "`c`")) + } + + test("SPARK-21529: a Hive union type nested in a struct is detected") { + val column = new FieldSchema("c", "struct>", null) + checkError( + exception = intercept[SparkUnsupportedOperationException] { + HiveClientImpl.fromHiveColumn(column) + }, + condition = "UNSUPPORTED_HIVE_TYPE", + parameters = Map( + "fieldType" -> "\"STRUCT>\"", + "fieldName" -> "`c`")) + } +}