james-willis commented on code in PR #2077:
URL: https://github.com/apache/sedona/pull/2077#discussion_r2205841105
##########
spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/Functions.scala:
##########
@@ -1831,3 +1835,140 @@ case class ST_InterpolatePoint(inputExpressions:
Seq[Expression])
protected def withNewChildrenInternal(newChildren: IndexedSeq[Expression]) =
copy(inputExpressions = newChildren)
}
+
+case class ExpandAddress(address: Expression)
+ extends UnaryExpression
+ with ImplicitCastInputTypes
+ with CodegenFallback
+ with FoldableExpression
+ with Serializable {
+
+ def this(children: Seq[Expression]) = this(children.head)
+
+ lazy private final val expander = {
+ val conf = SedonaConf.fromSparkEnv
+ getExpanderFromConf(conf.getLibPostalDataDir, conf.getLibPostalUseSenzing)
+ }
+
+ override def nullable: Boolean = true
+
+ override def inputTypes: Seq[AbstractDataType] = Seq(StringType)
+
+ override def dataType: DataType = ArrayType(StringType)
+
+ override def eval(input: InternalRow): Any = {
+ val addressVal = address.eval(input)
+ if (addressVal == null) {
+ null
+ } else {
+ new GenericArrayData(
+ expander
+ .expandAddress(addressVal.asInstanceOf[UTF8String].toString)
+ .map(UTF8String.fromString))
+ }
+ }
+
+ override protected def doGenCode(ctx: CodegenContext, ev: ExprCode):
ExprCode = {
+ val expanderRef = ctx.addReferenceObj("expander", expander,
classOf[AddressExpander].getName)
+ val utf8StringClass = "org.apache.spark.unsafe.types.UTF8String"
+ val arrayDataClass = "org.apache.spark.sql.catalyst.util.GenericArrayData"
+ val addressRef = child.genCode(ctx)
+ val address = addressRef.value
+ ev.copy(code = code"""
+ ${addressRef.code}
+ boolean ${ev.isNull} = ${addressRef.isNull};
+ $arrayDataClass ${ev.value} = null;
+
+ if (!${ev.isNull}) {
+ String[] expandedAddressArray =
$expanderRef.expandAddress($address.toString());
+ $utf8StringClass[] utf8Strings = new
$utf8StringClass[expandedAddressArray.length];
+ for (int j = 0; j < expandedAddressArray.length; j++) {
+ utf8Strings[j] =
$utf8StringClass.fromString(expandedAddressArray[j]);
+ }
+ ${ev.value} = new $arrayDataClass(utf8Strings);
+ } else {
+ ${ev.value} = new $arrayDataClass(new $utf8StringClass[0]);
Review Comment:
practically this isnt true because of `boolean ${ev.isNull} =
${addressRef.isNull};`. I will update the code anyway
--
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]