Github user cloud-fan commented on a diff in the pull request: https://github.com/apache/spark/pull/17633#discussion_r126588744 --- Diff: sql/hive/src/main/scala/org/apache/spark/sql/hive/client/HiveShim.scala --- @@ -589,18 +591,67 @@ 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)) + object ExtractableLiteral { + def unapply(expr: Expression): Option[String] = expr match { + case Literal(value, _: IntegralType) => Some(value.toString) + case Literal(value, _: StringType) => Some(quoteStringLiteral(value.toString)) + case _ => None + } + } + + object ExtractableLiterals { + def unapply(exprs: Seq[Expression]): Option[Seq[String]] = { + exprs.map(ExtractableLiteral.unapply).foldLeft(Option(Seq.empty[String])) { + case (Some(accum), Some(value)) => Some(accum :+ value) + case _ => None + } + } + } + + object ExtractableValues { + private lazy val valueToLiteralString: PartialFunction[Any, String] = { + case value: Byte => value.toString + case value: Short => value.toString + case value: Int => value.toString + case value: Long => value.toString + case value: UTF8String => quoteStringLiteral(value.toString) + } + + def unapply(values: Set[Any]): Option[Seq[String]] = { + values.toSeq.foldLeft(Option(Seq.empty[String])) { + case (Some(accum), value) if valueToLiteralString.isDefinedAt(value) => + Some(accum :+ valueToLiteralString(value)) + case _ => None + } + } + } + + def convertInToOr(a: Attribute, values: Seq[String]): String = { + values.map(value => s"${a.name} = $value").mkString("(", " or ", ")") + } + + lazy val convert: PartialFunction[Expression, String] = { + case In(a: Attribute, ExtractableLiterals(values)) + if !varcharKeys.contains(a.name) && values.nonEmpty => + convertInToOr(a, values) + case InSet(a: Attribute, ExtractableValues(values)) + if !varcharKeys.contains(a.name) && values.nonEmpty => + convertInToOr(a, values) + case op @ BinaryComparison(a: Attribute, ExtractableLiteral(value)) if !varcharKeys.contains(a.name) => - s"""${a.name} ${op.symbol} ${quoteStringLiteral(v.toString)}""" - case op @ BinaryComparison(Literal(v, _: StringType), a: Attribute) + s"${a.name} ${op.symbol} $value" + case op @ BinaryComparison(ExtractableLiteral(value), a: Attribute) if !varcharKeys.contains(a.name) => - s"""${quoteStringLiteral(v.toString)} ${op.symbol} ${a.name}""" - }.mkString(" and ") + s"$value ${op.symbol} ${a.name}" + case op @ And(expr1, expr2) + if convert.isDefinedAt(expr1) || convert.isDefinedAt(expr2) => + (convert.lift(expr1) ++ convert.lift(expr2)).mkString("(", " and ", ")") + case op @ Or(expr1, expr2) + if convert.isDefinedAt(expr1) && convert.isDefinedAt(expr2) => + (convert.lift(expr1) ++ convert.lift(expr2)).mkString("(", " or ", ")") --- End diff -- nit: s"(convert(expr1) or convert(expr))"
--- 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 infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org