cloud-fan commented on code in PR #39747:
URL: https://github.com/apache/spark/pull/39747#discussion_r1089961244
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringExpressions.scala:
##########
@@ -3039,3 +3039,100 @@ case class SplitPart (
partNum = newChildren.apply(2))
}
}
+
+/**
+ * Function to check if a given number string is a valid Luhn number. Returns
true, if the number
+ * string is a valid Luhn number, false otherwise.
+ */
+@ExpressionDescription(
+ usage = """
+ _FUNC_(str ) - Checks that a string of digits is valid according to the
Luhn algorithm.
+ This checksum function is widely applied on credit card numbers and
government identification
+ numbers to distinguish valid numbers from mistyped, incorrect numbers.
+ """,
+ examples = """
+ Examples:
+ > SELECT _FUNC_('8112189876');
+ true
+ > SELECT _FUNC_('79927398713');
+ true
+ > SELECT _FUNC_('79927398714');
+ false
+ """,
+ since = "3.5.0",
+ group = "string_funcs")
+case class Luhncheck(child: Expression) extends UnaryExpression with
ExpectsInputTypes {
+
+ override def nullable: Boolean = false
+
+ override protected def withNewChildInternal(newChild: Expression): Luhncheck
=
+ copy(child = newChild)
+
+ override def inputTypes: Seq[AbstractDataType] = Seq(StringType)
+
+ override def dataType: DataType = BooleanType
+
+ /**
+ * Default behavior of evaluation according to the default nullability of
UnaryExpression. If
+ * subclass of UnaryExpression override nullable, probably should also
override this.
+ */
+ override def eval(input: InternalRow): Any =
Luhncheck.isLuhnNumber(child.eval(input))
+
+ /**
+ * Returns Java source code that can be compiled to evaluate this
expression. The default
+ * behavior is to call the eval method of the expression. Concrete
expression implementations
+ * should override this to do actual code generation.
+ *
+ * @param ctx
+ * a [[CodegenContext]]
+ * @param ev
+ * an [[ExprCode]] with unique terms.
+ * @return
+ * an [[ExprCode]] containing the Java source code to generate the given
expression
+ */
+ override protected def doGenCode(ctx: CodegenContext, ev: ExprCode):
ExprCode = {
+ defineCodeGen(
+ ctx,
+ ev,
+ c => {
+
s"""org.apache.spark.sql.catalyst.expressions.Luhncheck.isLuhnNumber($c)"""
+ })
+ }
+}
+
+object Luhncheck {
Review Comment:
`AesDecrypt` is a good example. We can also move the code to
`ExpressionImplUtils`
--
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]