beliefer commented on a change in pull request #29999:
URL: https://github.com/apache/spark/pull/29999#discussion_r505132544



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/regexpExpressions.scala
##########
@@ -176,6 +177,195 @@ case class Like(left: Expression, right: Expression, 
escapeChar: Char)
   }
 }
 
+abstract class LikeAllBase extends Expression with ImplicitCastInputTypes with 
NullIntolerant {
+  def value: Expression = children.head
+  def list: Seq[Expression] = children.tail
+  def isNot: Boolean
+
+  override def inputTypes: Seq[AbstractDataType] = {
+    val arrayOrStr = TypeCollection(ArrayType(StringType), StringType)
+    StringType +: Seq.fill(children.size - 1)(arrayOrStr)
+  }
+
+  override def dataType: DataType = BooleanType
+
+  override def foldable: Boolean = value.foldable && list.forall(_.foldable)
+
+  override def nullable: Boolean = true
+
+  def escape(v: String): String = StringUtils.escapeLikeRegex(v, '\\')
+
+  def matches(regex: Pattern, str: String): Boolean = 
regex.matcher(str).matches()
+
+  override def eval(input: InternalRow): Any = {
+    val evaluatedValue = value.eval(input)
+    if (evaluatedValue == null) {
+      null
+    } else {
+      list.foreach { e =>
+        val str = e.eval(input)
+        if (str == null) {
+          return null
+        }
+        val regex = 
Pattern.compile(escape(str.asInstanceOf[UTF8String].toString))
+        if(regex == null) {
+          return null
+        } else if (isNot && matches(regex, 
evaluatedValue.asInstanceOf[UTF8String].toString)) {
+          return false
+        } else if (!isNot && !matches(regex, 
evaluatedValue.asInstanceOf[UTF8String].toString)) {
+          return false
+        }
+      }
+      return true
+    }
+  }
+
+  override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
+    val patternClass = classOf[Pattern].getName
+    val escapeFunc = StringUtils.getClass.getName.stripSuffix("$") + 
".escapeLikeRegex"
+    val javaDataType = CodeGenerator.javaType(value.dataType)
+    val valueGen = value.genCode(ctx)
+    val listGen = list.map(_.genCode(ctx))
+    val pattern = ctx.freshName("pattern")
+    val rightStr = ctx.freshName("rightStr")
+    val escapedEscapeChar = StringEscapeUtils.escapeJava("\\")
+    val hasNull = ctx.freshName("hasNull")
+    val matched = ctx.freshName("matched")
+    val valueArg = ctx.freshName("valueArg")
+    val listCode = listGen.map(x =>
+      s"""
+         |${x.code}
+         |if (${x.isNull}) {
+         |  $hasNull = true; // ${ev.isNull} = true;
+         |} else if (!$hasNull && $matched) {
+         |  String $rightStr = ${x.value}.toString();
+         |  $patternClass $pattern =
+         |    $patternClass.compile($escapeFunc($rightStr, 
'$escapedEscapeChar'));
+         |  if ($isNot && $pattern.matcher($valueArg.toString()).matches()) {
+         |    $matched = false;
+         |  } else if (!$isNot && 
!$pattern.matcher($valueArg.toString()).matches()) {
+         |    $matched = false;
+         |  }
+         |}
+       """.stripMargin)
+
+    val resultType = CodeGenerator.javaType(dataType)
+    val codes = ctx.splitExpressionsWithCurrentInputs(
+      expressions = listCode,
+      funcName = "likeAll",
+      extraArguments = (javaDataType, valueArg) :: 
(CodeGenerator.JAVA_BOOLEAN, hasNull) ::
+        (resultType, matched) :: Nil,
+      returnType = resultType,
+      makeSplitFunction = body =>
+        s"""
+           |if (!$hasNull && $matched) {
+           |  $body;
+           |}
+         """.stripMargin,
+      foldFunctions = _.map { funcCall =>
+        s"""
+           |if (!$hasNull && $matched) {
+           |  $funcCall;
+           |}
+         """.stripMargin
+      }.mkString("\n"))
+    ev.copy(code =
+      code"""
+            |${valueGen.code}
+            |boolean $hasNull = false;
+            |boolean $matched = true;
+            |if (${valueGen.isNull}) {
+            |  $hasNull = true;
+            |} else {
+            |  $javaDataType $valueArg = ${valueGen.value};
+            |  $codes
+            |}
+            |final boolean ${ev.isNull} = ($hasNull == true);
+            |final boolean ${ev.value} = ($matched == true);

Review comment:
       I got it.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to