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 @@ -20,6 +20,7 @@ package org.apache.spark.sql.catalyst.expressions
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, CodeGenerator, ExprCode}
import org.apache.spark.sql.catalyst.expressions.codegen.Block._
import org.apache.spark.sql.errors.QueryCompilationErrors
import org.apache.spark.sql.types.{DataType, NumericType}

case class TryEval(child: Expression) extends UnaryExpression with NullIntolerant {
Expand Down Expand Up @@ -227,7 +228,8 @@ case class TryToBinary(

def this(expr: Expression, formatExpression: Expression) =
this(expr, Some(formatExpression),
TryEval(ToBinary(expr, Some(formatExpression), nullOnInvalidFormat = true)))
TryEval(ToBinary(expr, Some(TryToBinary.checkFormat(formatExpression)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you perform the check in checkInputDataTypes() as we do that in other expression, please.

BTW, exceptions from expressions constructors are wrapped by AnalysisException additionally which is not convenient to users.

nullOnInvalidFormat = true)))

override def prettyName: String = "try_to_binary"

Expand All @@ -236,3 +238,13 @@ case class TryToBinary(
override protected def withNewChildInternal(newChild: Expression): Expression =
this.copy(replacement = newChild)
}

object TryToBinary {
def checkFormat(format: Expression): Expression = {
if (format.foldable) {
format
} else {
throw QueryCompilationErrors.requireLiteralParameter("try_to_binary", "format", "string")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,7 @@ select to_binary(null, cast(null as string));
-- invalid format
select to_binary('abc', 1);
select to_binary('abc', 'invalidFormat');
-- format must be foldable
SELECT to_binary(col1, col2) from values ('abc', 'utf-8') as data(col1, col2);
-- non-foldable input string
SELECT to_binary(col1, 'utf-8') from values ('abc') as data(col1);
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ select try_to_binary(null, null);
select try_to_binary(null, cast(null as string));
-- invalid format
select try_to_binary('abc', 1);
select try_to_binary('abc', 'invalidFormat');
select try_to_binary('abc', 'invalidFormat');
-- format must be foldable
SELECT try_to_binary(col1, col2) from values ('abc', 'utf-8') as data(col1, col2);
-- non-foldable input string
SELECT try_to_binary(col1, 'utf-8') from values ('abc') as data(col1);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not strictly relevant to this issue, but I noticed that there were no tests for non-foldable input values, so I added one.

Original file line number Diff line number Diff line change
Expand Up @@ -1597,3 +1597,20 @@ org.apache.spark.sql.AnalysisException
"invalidValue" : "invalidformat"
}
}


-- !query
SELECT to_binary(col1, col2) from values ('abc', 'utf-8') as data(col1, col2)
-- !query schema
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
The 'format' parameter of function 'to_binary' needs to be a string literal.; line 1 pos 7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you move the exception from constructor, you should see AnalysisException w/ an error class.



-- !query
SELECT to_binary(col1, 'utf-8') from values ('abc') as data(col1)
-- !query schema
struct<to_binary(col1, utf-8):binary>
-- !query output
abc
Original file line number Diff line number Diff line change
Expand Up @@ -1529,3 +1529,20 @@ org.apache.spark.sql.AnalysisException
"invalidValue" : "invalidformat"
}
}


-- !query
SELECT to_binary(col1, col2) from values ('abc', 'utf-8') as data(col1, col2)
-- !query schema
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
The 'format' parameter of function 'to_binary' needs to be a string literal.; line 1 pos 7


-- !query
SELECT to_binary(col1, 'utf-8') from values ('abc') as data(col1)
-- !query schema
struct<to_binary(col1, utf-8):binary>
-- !query output
abc
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,20 @@ select try_to_binary('abc', 'invalidFormat')
struct<try_to_binary(abc, invalidFormat):binary>
-- !query output
NULL


-- !query
SELECT try_to_binary(col1, col2) from values ('abc', 'utf-8') as data(col1, col2)
-- !query schema
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
The 'format' parameter of function 'try_to_binary' needs to be a string literal.; line 1 pos 7


-- !query
SELECT try_to_binary(col1, 'utf-8') from values ('abc') as data(col1)
-- !query schema
struct<try_to_binary(col1, utf-8):binary>
-- !query output
abc