cloud-fan commented on a change in pull request #32801:
URL: https://github.com/apache/spark/pull/32801#discussion_r724883083



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/misc.scala
##########
@@ -301,3 +305,137 @@ case class CurrentUser() extends LeafExpression with 
Unevaluable {
   override def prettyName: String = "current_user"
   final override val nodePatterns: Seq[TreePattern] = Seq(CURRENT_LIKE)
 }
+
+/**
+ * The base implementation for AES encryption and decryption.
+ */
+abstract class AesBase(left: Expression, right: Expression)
+  extends BinaryExpression with ImplicitCastInputTypes with NullIntolerant 
with Serializable {
+
+  override def dataType: DataType = BinaryType
+  override def nullable: Boolean = true
+  override def inputTypes: Seq[DataType] = Seq(BinaryType, BinaryType)
+  protected val cipherMode: Int
+
+  @transient lazy protected val cipher: Cipher = try {
+    Cipher.getInstance("AES")
+  } catch {
+    case e @ (_: NoSuchPaddingException | _: NoSuchAlgorithmException) =>
+      throw new RuntimeException(e)
+  }
+
+  protected override def nullSafeEval(input1: Any, input2: Any): Any = {
+    val input = input1.asInstanceOf[Array[Byte]]
+    val key = input2.asInstanceOf[Array[Byte]]
+    val inputLength = input.length
+    val keyLength = key.length
+    val secretKey = keyLength match {
+      case 16 | 24 | 32 => new SecretKeySpec(key, 0, keyLength, "AES")
+      case _ => null
+    }
+
+    if (secretKey == null) {
+      return null
+    }
+
+    try {
+      cipher.init(cipherMode, secretKey)
+      cipher.doFinal(input, 0, inputLength)
+    } catch {
+      case _: GeneralSecurityException =>
+        null
+    }
+  }
+
+  override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {

Review comment:
       Today, I think it's more convenient to implement the expression logic 
with Java, and call the java method with `StaticInvoke` wrapped by 
`RuntimeReplaceable`. It's also easier to write UT for the Java methods if 
needed, and we don't need to implement codegen by hand.




-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to