Skip to content
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 @@ -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 @@ -345,13 +345,13 @@ object Uniform {
usage = """
_FUNC_(length[, seed]) - Returns a string of the specified length whose characters are chosen
uniformly at random from the following pool of characters: 0-9, a-z, A-Z. The random seed is
optional. The string length must be a constant two-byte or four-byte integer (SMALLINT or INT,
respectively).
optional. The string length must be a non-negative constant two-byte or four-byte integer
(SMALLINT or INT, respectively).
""",
arguments = """
Arguments:
* length - The length of the random string to generate.
An expression that evaluates to an integer. Must be a constant.
An expression that evaluates to a non-negative integer. Must be a constant.
* seed - The seed used to produce reproducible random results.
An expression that evaluates to an integer or long. Must be a constant.
""",
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