Github user mallman commented on a diff in the pull request:
https://github.com/apache/spark/pull/17633#discussion_r126022861
--- Diff:
sql/hive/src/main/scala/org/apache/spark/sql/hive/client/HiveShim.scala ---
@@ -589,18 +590,34 @@ private[client] class Shim_v0_13 extends Shim_v0_12 {
col.getType.startsWith(serdeConstants.CHAR_TYPE_NAME))
.map(col => col.getName).toSet
- filters.collect {
- case op @ BinaryComparison(a: Attribute, Literal(v, _:
IntegralType)) =>
- s"${a.name} ${op.symbol} $v"
- case op @ BinaryComparison(Literal(v, _: IntegralType), a:
Attribute) =>
- s"$v ${op.symbol} ${a.name}"
- case op @ BinaryComparison(a: Attribute, Literal(v, _: StringType))
- if !varcharKeys.contains(a.name) =>
- s"""${a.name} ${op.symbol} ${quoteStringLiteral(v.toString)}"""
- case op @ BinaryComparison(Literal(v, _: StringType), a: Attribute)
- if !varcharKeys.contains(a.name) =>
- s"""${quoteStringLiteral(v.toString)} ${op.symbol} ${a.name}"""
- }.mkString(" and ")
+ def isFoldable(expr: Expression): Boolean =
+ (expr.dataType.isInstanceOf[IntegralType] ||
expr.dataType.isInstanceOf[StringType]) &&
+ expr.foldable &&
+ expr.deterministic
+
+ def convertFoldable(expr: Expression): String = expr.dataType match {
+ case _: IntegralType => expr.eval(null).toString
+ case _: StringType => quoteStringLiteral(expr.eval(null).toString)
+ }
+
+ def convert(filter: Expression): String =
+ filter match {
+ case In(a: Attribute, exprs) if exprs.forall(isFoldable) =>
+ val or = exprs.map(expr => s"${a.name} =
${convertFoldable(expr)}").reduce(_ + " or " + _)
+ "(" + or + ")"
+ case op @ BinaryComparison(a: Attribute, expr2)
+ if !varcharKeys.contains(a.name) && isFoldable(expr2) =>
+ s"(${a.name} ${op.symbol} ${convertFoldable(expr2)})"
+ case op @ BinaryComparison(expr1, a: Attribute)
+ if !varcharKeys.contains(a.name) && isFoldable(expr1) =>
+ s"(${convertFoldable(expr1)} ${op.symbol} ${a.name})"
+ case op @ And(expr1, expr2) =>
+ s"(${convert(expr1)} and ${convert(expr2)})"
+ case op @ Or(expr1, expr2) =>
+ s"(${convert(expr1)} or ${convert(expr2)})"
+ }
+
+ filters.flatMap(f => Try(convert(f)).toOption).mkString(" and ")
--- End diff --
Both `filters.flatMap(f => convert.lift(f))` and `filters.collect(convert)`
throw a `MatchError` on input
```
ds=(20170101 + 1) and h=0
```
Now I know that in practice foldable expressions such as `20170101 + 1` are
converted to literals, but the point I'm making here is that the alternatives
proposed here will throw a `MatchError` if the filter expression is parsed into
a tree with a leaf that does not match a pattern defined by the `convert`
partial function.
Hence the use of `filters.flatMap(f => Try(convert(f)).toOption).mkString("
and ")`.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]