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 @@ -165,10 +165,14 @@ case class SchemaOfCsv(
@transient
private lazy val csv = child.eval().asInstanceOf[UTF8String]

override def checkInputDataTypes(): TypeCheckResult = child match {
case Literal(s, StringType) if s != null => super.checkInputDataTypes()
case _ => TypeCheckResult.TypeCheckFailure(
s"The input csv should be a string literal and not null; however, got ${child.sql}.")
override def checkInputDataTypes(): TypeCheckResult = {
if (child.foldable && csv != null) {
super.checkInputDataTypes()
} else {
TypeCheckResult.TypeCheckFailure(
"The input csv should be a foldable string expression and not null; " +
s"however, got ${child.sql}.")
}
}

override def eval(v: InternalRow): Any = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ select schema_of_csv(null)
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
cannot resolve 'schema_of_csv(NULL)' due to data type mismatch: The input csv should be a string literal and not null; however, got NULL.; line 1 pos 7
cannot resolve 'schema_of_csv(NULL)' due to data type mismatch: The input csv should be a foldable string expression and not null; however, got NULL.; line 1 pos 7


-- !query
Expand All @@ -108,7 +108,7 @@ SELECT schema_of_csv(csvField) FROM csvTable
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
cannot resolve 'schema_of_csv(csvtable.`csvField`)' due to data type mismatch: The input csv should be a string literal and not null; however, got csvtable.`csvField`.; line 1 pos 7
cannot resolve 'schema_of_csv(csvtable.`csvField`)' due to data type mismatch: The input csv should be a foldable string expression and not null; however, got csvtable.`csvField`.; line 1 pos 7


-- !query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,11 @@ class CsvFunctionsSuite extends QueryTest with SharedSparkSession {
assert(readback(0).getAs[Row](0).getAs[Date](0).getTime >= 0)
}
}

test("schema_of_csv - infers the schema of foldable CSV string") {
val input = concat_ws(",", lit(0.1), lit(1))
checkAnswer(
spark.range(1).select(schema_of_csv(input)),
Seq(Row("struct<_c0:double,_c1:int>")))
}
}