Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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 @@ -21,7 +21,7 @@ import org.apache.spark.SparkException
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.analysis.{TypeCheckResult, UnresolvedSeed}
import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.DataTypeMismatch
import org.apache.spark.sql.catalyst.expressions.ExpectsInputTypes.{toSQLExpr, toSQLId}
import org.apache.spark.sql.catalyst.expressions.ExpectsInputTypes.{toSQLExpr, toSQLId, toSQLValue}
import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, CodeGenerator, ExprCode, FalseLiteral}
import org.apache.spark.sql.catalyst.expressions.codegen.Block._
import org.apache.spark.sql.catalyst.trees.{BinaryLike, TernaryLike, UnaryLike}
Expand Down Expand Up @@ -425,6 +425,18 @@ case class RandStr(
"inputExpr" -> toSQLExpr(expr)))
}
}
if (result == TypeCheckResult.TypeCheckSuccess) {
val lengthValue = length.eval()
// randstr(NULL, 0) is valid (treated as 0), so only reject a negative length.
if (lengthValue != null && lengthValue.asInstanceOf[Int] < 0) {
Comment thread
uros-b marked this conversation as resolved.
result = DataTypeMismatch(
errorSubClass = "VALUE_OUT_OF_RANGE",
messageParameters = Map(
"exprName" -> toSQLId("length"),
"valueRange" -> s"[0, ${Int.MaxValue}]",
"currentValue" -> toSQLValue(lengthValue, IntegerType)))
}
}
result
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,24 @@ org.apache.spark.sql.AnalysisException
-- !query
SELECT randstr(-1, 0) AS result
-- !query analysis
[Analyzer test output redacted due to nondeterminism]
org.apache.spark.sql.catalyst.ExtendedAnalysisException
{
"errorClass" : "DATATYPE_MISMATCH.VALUE_OUT_OF_RANGE",
"sqlState" : "42K09",
"messageParameters" : {
"currentValue" : "-1",
"exprName" : "`length`",
"sqlExpr" : "\"randstr(-1, 0)\"",
"valueRange" : "[0, 2147483647]"
},
"queryContext" : [ {
"objectType" : "",
"objectName" : "",
"startIndex" : 8,
"stopIndex" : 21,
"fragment" : "randstr(-1, 0)"
} ]
}


-- !query
Expand Down
22 changes: 15 additions & 7 deletions sql/core/src/test/resources/sql-tests/results/random.sql.out
Original file line number Diff line number Diff line change
Expand Up @@ -857,15 +857,23 @@ SELECT randstr(-1, 0) AS result
-- !query schema
struct<>
-- !query output
org.apache.spark.SparkRuntimeException
org.apache.spark.sql.catalyst.ExtendedAnalysisException
{
"errorClass" : "INVALID_PARAMETER_VALUE.LENGTH",
"sqlState" : "22023",
"errorClass" : "DATATYPE_MISMATCH.VALUE_OUT_OF_RANGE",
"sqlState" : "42K09",
"messageParameters" : {
"functionName" : "`randstr`",
"length" : "-1",
"parameter" : "`length`"
}
"currentValue" : "-1",
"exprName" : "`length`",
"sqlExpr" : "\"randstr(-1, 0)\"",
"valueRange" : "[0, 2147483647]"
},
"queryContext" : [ {
"objectType" : "",
"objectName" : "",
"startIndex" : 8,
"stopIndex" : 21,
"fragment" : "randstr(-1, 0)"
} ]
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,23 @@ class DataFrameFunctionsSuite extends SharedSparkSession {
callSitePattern = "",
startIndex = 0,
stopIndex = 0))
expr = randstr(lit(-1), lit(0))
checkError(
intercept[AnalysisException](df.select(expr)),
condition = "DATATYPE_MISMATCH.VALUE_OUT_OF_RANGE",
parameters = Map(
"sqlExpr" -> "\"randstr(-1, 0)\"",
"exprName" -> "`length`",
"valueRange" -> "[0, 2147483647]",
"currentValue" -> "-1"),
context = ExpectedContext(
contextType = QueryContextType.DataFrame,
fragment = "randstr",
objectType = "",
objectName = "",
callSitePattern = "",
startIndex = 0,
stopIndex = 0))
}

test("uniform function") {
Expand Down