Github user kiszk commented on a diff in the pull request:
https://github.com/apache/spark/pull/21031#discussion_r181038144
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala
---
@@ -34,28 +66,38 @@ import org.apache.spark.sql.types._
> SELECT _FUNC_(array('b', 'd', 'c', 'a'));
4
""")
-case class Size(child: Expression) extends UnaryExpression with
ExpectsInputTypes {
+case class Size(child: Expression) extends SizeUtil {
override def dataType: DataType = IntegerType
- override def inputTypes: Seq[AbstractDataType] =
Seq(TypeCollection(ArrayType, MapType))
- override def nullable: Boolean = false
override def eval(input: InternalRow): Any = {
- val value = child.eval(input)
- if (value == null) {
- -1
- } else child.dataType match {
- case _: ArrayType => value.asInstanceOf[ArrayData].numElements()
- case _: MapType => value.asInstanceOf[MapData].numElements()
- }
+ sizeEval(child, input)
}
override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
- val childGen = child.genCode(ctx)
- ev.copy(code = s"""
- boolean ${ev.isNull} = false;
- ${childGen.code}
- ${CodeGenerator.javaType(dataType)} ${ev.value} = ${childGen.isNull}
? -1 :
- (${childGen.value}).numElements();""", isNull = FalseLiteral)
+ doSizeGenCode(ctx, ev)
+ }
+}
+
+/**
+ * Given an array or map, returns its size as BigInt. Returns -1 if null.
+ */
+@ExpressionDescription(
+ usage = "_FUNC_(expr) - Returns the size of an array or a map as long.
Returns -1 if null.",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array('b', 'd', 'c', 'a'));
+ 4
+ """,
+ since = "2.4.0")
+case class Cardinality(child: Expression) extends SizeUtil {
+ override def dataType: DataType = LongType
+
+ override def eval(input: InternalRow): Any = {
--- End diff --
Good catch, thanks
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]