Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-34086][SQL][3.1] RaiseError generates too much code and may fails codegen in length check for char varchar #31168

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ case class PrintToStderr(child: Expression) extends UnaryExpression {
custom error message
""",
since = "3.1.0")
case class RaiseError(child: Expression) extends UnaryExpression with ImplicitCastInputTypes {
case class RaiseError(child: Expression, dataType: DataType)
extends UnaryExpression with ImplicitCastInputTypes {

def this(child: Expression) = this(child, NullType)

override def foldable: Boolean = false
override def nullable: Boolean = true
override def dataType: DataType = NullType
override def inputTypes: Seq[AbstractDataType] = Seq(StringType)

override def prettyName: String = "raise_error"
Expand Down Expand Up @@ -98,6 +100,10 @@ case class RaiseError(child: Expression) extends UnaryExpression with ImplicitCa
}
}

object RaiseError {
def apply(child: Expression): RaiseError = new RaiseError(child)
}

/**
* A function that throws an exception if 'condition' is not true.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.parser.CatalystSqlParser
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types._
import org.apache.spark.unsafe.types.UTF8String

object CharVarcharUtils extends Logging {

Expand Down Expand Up @@ -202,12 +203,9 @@ object CharVarcharUtils extends Logging {
}.getOrElse(expr)
}

private def raiseError(expr: Expression, typeName: String, length: Int): Expression = {
val errorMsg = Concat(Seq(
Literal("input string of length "),
Cast(Length(expr), StringType),
Literal(s" exceeds $typeName type length limitation: $length")))
Cast(RaiseError(errorMsg), StringType)
private def raiseError(typeName: String, length: Int): Expression = {
val errMsg = UTF8String.fromString(s"Exceeds $typeName type length limitation: $length")
RaiseError(Literal(errMsg, StringType), StringType)
}

private def stringLengthCheck(expr: Expression, dt: DataType): Expression = dt match {
Expand All @@ -217,7 +215,7 @@ object CharVarcharUtils extends Logging {
// spaces, as we will pad char type columns/fields at read time.
If(
GreaterThan(Length(trimmed), Literal(length)),
raiseError(expr, "char", length),
raiseError("char", length),
trimmed)

case VarcharType(length) =>
Expand All @@ -230,7 +228,7 @@ object CharVarcharUtils extends Logging {
expr,
If(
GreaterThan(Length(trimmed), Literal(length)),
raiseError(expr, "varchar", length),
raiseError("varchar", length),
StringRPad(trimmed, Literal(length))))

case StructType(fields) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,7 @@ trait CharVarcharTestSuite extends QueryTest with SQLTestUtils {
sql("INSERT INTO t VALUES (null)")
checkAnswer(spark.table("t"), Row(null))
val e = intercept[SparkException](sql("INSERT INTO t VALUES ('123456')"))
assert(e.getCause.getMessage.contains(
s"input string of length 6 exceeds $typeName type length limitation: 5"))
assert(e.getCause.getMessage.contains(s"Exceeds $typeName type length limitation: 5"))
}
}

Expand All @@ -202,8 +201,7 @@ trait CharVarcharTestSuite extends QueryTest with SQLTestUtils {
sql("INSERT INTO t VALUES (1, null)")
checkAnswer(spark.table("t"), Row(1, null))
val e = intercept[SparkException](sql("INSERT INTO t VALUES (1, '123456')"))
assert(e.getCause.getMessage.contains(
s"input string of length 6 exceeds $typeName type length limitation: 5"))
assert(e.getCause.getMessage.contains(s"Exceeds $typeName type length limitation: 5"))
}
}
}
Expand All @@ -214,8 +212,7 @@ trait CharVarcharTestSuite extends QueryTest with SQLTestUtils {
sql("INSERT INTO t SELECT struct(null)")
checkAnswer(spark.table("t"), Row(Row(null)))
val e = intercept[SparkException](sql("INSERT INTO t SELECT struct('123456')"))
assert(e.getCause.getMessage.contains(
s"input string of length 6 exceeds $typeName type length limitation: 5"))
assert(e.getCause.getMessage.contains(s"Exceeds $typeName type length limitation: 5"))
}
}

Expand All @@ -225,17 +222,15 @@ trait CharVarcharTestSuite extends QueryTest with SQLTestUtils {
sql("INSERT INTO t VALUES (array(null))")
checkAnswer(spark.table("t"), Row(Seq(null)))
val e = intercept[SparkException](sql("INSERT INTO t VALUES (array('a', '123456'))"))
assert(e.getCause.getMessage.contains(
s"input string of length 6 exceeds $typeName type length limitation: 5"))
assert(e.getCause.getMessage.contains(s"Exceeds $typeName type length limitation: 5"))
}
}

test("length check for input string values: nested in map key") {
testTableWrite { typeName =>
sql(s"CREATE TABLE t(c MAP<$typeName(5), STRING>) USING $format")
val e = intercept[SparkException](sql("INSERT INTO t VALUES (map('123456', 'a'))"))
assert(e.getCause.getMessage.contains(
s"input string of length 6 exceeds $typeName type length limitation: 5"))
assert(e.getCause.getMessage.contains(s"Exceeds $typeName type length limitation: 5"))
}
}

Expand All @@ -245,20 +240,17 @@ trait CharVarcharTestSuite extends QueryTest with SQLTestUtils {
sql("INSERT INTO t VALUES (map('a', null))")
checkAnswer(spark.table("t"), Row(Map("a" -> null)))
val e = intercept[SparkException](sql("INSERT INTO t VALUES (map('a', '123456'))"))
assert(e.getCause.getMessage.contains(
s"input string of length 6 exceeds $typeName type length limitation: 5"))
assert(e.getCause.getMessage.contains(s"Exceeds $typeName type length limitation: 5"))
}
}

test("length check for input string values: nested in both map key and value") {
testTableWrite { typeName =>
sql(s"CREATE TABLE t(c MAP<$typeName(5), $typeName(5)>) USING $format")
val e1 = intercept[SparkException](sql("INSERT INTO t VALUES (map('123456', 'a'))"))
assert(e1.getCause.getMessage.contains(
s"input string of length 6 exceeds $typeName type length limitation: 5"))
assert(e1.getCause.getMessage.contains(s"Exceeds $typeName type length limitation: 5"))
val e2 = intercept[SparkException](sql("INSERT INTO t VALUES (map('a', '123456'))"))
assert(e2.getCause.getMessage.contains(
s"input string of length 6 exceeds $typeName type length limitation: 5"))
assert(e2.getCause.getMessage.contains(s"Exceeds $typeName type length limitation: 5"))
}
}

Expand All @@ -268,8 +260,7 @@ trait CharVarcharTestSuite extends QueryTest with SQLTestUtils {
sql("INSERT INTO t SELECT struct(array(null))")
checkAnswer(spark.table("t"), Row(Row(Seq(null))))
val e = intercept[SparkException](sql("INSERT INTO t SELECT struct(array('123456'))"))
assert(e.getCause.getMessage.contains(
s"input string of length 6 exceeds $typeName type length limitation: 5"))
assert(e.getCause.getMessage.contains(s"Exceeds $typeName type length limitation: 5"))
}
}

Expand All @@ -279,8 +270,7 @@ trait CharVarcharTestSuite extends QueryTest with SQLTestUtils {
sql("INSERT INTO t VALUES (array(struct(null)))")
checkAnswer(spark.table("t"), Row(Seq(Row(null))))
val e = intercept[SparkException](sql("INSERT INTO t VALUES (array(struct('123456')))"))
assert(e.getCause.getMessage.contains(
s"input string of length 6 exceeds $typeName type length limitation: 5"))
assert(e.getCause.getMessage.contains(s"Exceeds $typeName type length limitation: 5"))
}
}

Expand All @@ -290,8 +280,7 @@ trait CharVarcharTestSuite extends QueryTest with SQLTestUtils {
sql("INSERT INTO t VALUES (array(array(null)))")
checkAnswer(spark.table("t"), Row(Seq(Seq(null))))
val e = intercept[SparkException](sql("INSERT INTO t VALUES (array(array('123456')))"))
assert(e.getCause.getMessage.contains(
s"input string of length 6 exceeds $typeName type length limitation: 5"))
assert(e.getCause.getMessage.contains(s"Exceeds $typeName type length limitation: 5"))
}
}

Expand All @@ -312,11 +301,9 @@ trait CharVarcharTestSuite extends QueryTest with SQLTestUtils {
sql("INSERT INTO t VALUES (1234, 1234)")
checkAnswer(spark.table("t"), Row("1234 ", "1234"))
val e1 = intercept[SparkException](sql("INSERT INTO t VALUES (123456, 1)"))
assert(e1.getCause.getMessage.contains(
"input string of length 6 exceeds char type length limitation: 5"))
assert(e1.getCause.getMessage.contains("Exceeds char type length limitation: 5"))
val e2 = intercept[SparkException](sql("INSERT INTO t VALUES (1, 123456)"))
assert(e2.getCause.getMessage.contains(
"input string of length 6 exceeds varchar type length limitation: 5"))
assert(e2.getCause.getMessage.contains("Exceeds varchar type length limitation: 5"))
}
}

Expand Down Expand Up @@ -626,8 +613,7 @@ class FileSourceCharVarcharTestSuite extends CharVarcharTestSuite with SharedSpa
sql("SELECT '123456' as col").write.format(format).save(dir.toString)
sql(s"CREATE TABLE t (col $typ(2)) using $format LOCATION '$dir'")
val e = intercept[SparkException] { sql("select * from t").collect() }
assert(e.getCause.getMessage.contains(
s"input string of length 6 exceeds $typ type length limitation: 2"))
assert(e.getCause.getMessage.contains(s"Exceeds $typ type length limitation: 2"))
}
}
}
Expand All @@ -654,8 +640,7 @@ class FileSourceCharVarcharTestSuite extends CharVarcharTestSuite with SharedSpa
sql(s"CREATE TABLE t (col $typ(2)) using $format")
sql(s"ALTER TABLE t SET LOCATION '$dir'")
val e = intercept[SparkException] { spark.table("t").collect() }
assert(e.getCause.getMessage.contains(
s"input string of length 6 exceeds $typ type length limitation: 2"))
assert(e.getCause.getMessage.contains(s"Exceeds $typ type length limitation: 2"))
}
}
}
Expand Down