chenhao-db commented on code in PR #49609:
URL: https://github.com/apache/spark/pull/49609#discussion_r1927417508
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/variant/variantExpressions.scala:
##########
@@ -288,30 +287,84 @@ case class VariantGet(
zoneId)
protected override def nullSafeEval(input: Any, path: Any): Any = {
- VariantGet.variantGet(input.asInstanceOf[VariantVal], parsedPath,
dataType, castArgs)
+ if (pathIsFoldable) {
+ VariantGet.variantGet(input.asInstanceOf[VariantVal], parsedPath,
dataType, castArgs)
+ } else {
+ val pathValue = path.toString
+ val parsedRowPath = VariantPathParser.parse(pathValue).getOrElse {
+ throw QueryExecutionErrors.invalidVariantGetPath(pathValue, prettyName)
+ }
+ VariantGet.variantGet(input.asInstanceOf[VariantVal], parsedRowPath,
dataType, castArgs)
+ }
}
protected override def doGenCode(ctx: CodegenContext, ev: ExprCode):
ExprCode = {
- val childCode = child.genCode(ctx)
- val tmp = ctx.freshVariable("tmp", classOf[Object])
- val parsedPathArg = ctx.addReferenceObj("parsedPath", parsedPath)
- val dataTypeArg = ctx.addReferenceObj("dataType", dataType)
- val castArgsArg = ctx.addReferenceObj("castArgs", castArgs)
- val code = code"""
- ${childCode.code}
- boolean ${ev.isNull} = ${childCode.isNull};
- ${CodeGenerator.javaType(dataType)} ${ev.value} =
${CodeGenerator.defaultValue(dataType)};
- if (!${ev.isNull}) {
- Object $tmp =
org.apache.spark.sql.catalyst.expressions.variant.VariantGet.variantGet(
- ${childCode.value}, $parsedPathArg, $dataTypeArg, $castArgsArg);
- if ($tmp == null) {
- ${ev.isNull} = true;
- } else {
- ${ev.value} = (${CodeGenerator.boxedType(dataType)})$tmp;
+ if (pathIsFoldable) {
+ val childCode = child.genCode(ctx)
+ val tmp = ctx.freshVariable("tmp", classOf[Object])
+ val parsedPathArg = ctx.addReferenceObj("parsedPath", parsedPath)
+ val dataTypeArg = ctx.addReferenceObj("dataType", dataType)
+ val castArgsArg = ctx.addReferenceObj("castArgs", castArgs)
+ val code = code"""
+ ${childCode.code}
+ boolean ${ev.isNull} = ${childCode.isNull};
+ ${CodeGenerator.javaType(dataType)} ${ev.value} =
${CodeGenerator.defaultValue(dataType)};
+ if (!${ev.isNull}) {
+ Object $tmp =
org.apache.spark.sql.catalyst.expressions.variant.VariantGet.variantGet(
+ ${childCode.value}, $parsedPathArg, $dataTypeArg, $castArgsArg);
+ if ($tmp == null) {
+ ${ev.isNull} = true;
+ } else {
+ ${ev.value} = (${CodeGenerator.boxedType(dataType)})$tmp;
+ }
}
- }
- """
- ev.copy(code = code)
+ """
+ ev.copy(code = code)
+ } else {
+ val tmp = ctx.freshVariable("tmp", classOf[Object])
+ val childCode = child.genCode(ctx)
+ val pathCode = path.genCode(ctx)
+ val dataTypeArg = ctx.addReferenceObj("dataType", dataType)
+ val castArgsArg = ctx.addReferenceObj("castArgs", castArgs)
+ val parsedPathVar = ctx.freshName("parsedPath")
+ val ensureNonEmpty = ctx.freshName("ensureNonEmpty")
+ val optionalParsedPathType =
+ CodeGenerator.typeName(classOf[Option[Array[VariantPathSegment]]])
+ val parsedPathType =
CodeGenerator.typeName(classOf[Array[VariantPathSegment]])
+ ctx.addNewFunction(ensureNonEmpty,
+ s"""
+ private void $ensureNonEmpty($optionalParsedPathType p, String
pathString)
+ throws Throwable {
+ if (p.isEmpty()) {
+ throw QueryExecutionErrors.invalidVariantGetPath(pathString,
"$prettyName");
+ }
+ }
+ """)
+ val code = code"""
+ ${childCode.code}
+ ${pathCode.code}
+ boolean ${ev.isNull} = ${childCode.isNull} || ${pathCode.isNull};
+ ${CodeGenerator.javaType(dataType)} ${ev.value} =
${CodeGenerator.defaultValue(dataType)};
+ if (!${ev.isNull}) {
+ $optionalParsedPathType $parsedPathVar = ($optionalParsedPathType)
+
org.apache.spark.sql.catalyst.expressions.variant.VariantPathParser.parse(
+ ${pathCode.value}.toString());
+ try {
Review Comment:
It will work if you wrap
```
VariantPathParser.parse(pathValue).getOrElse {
throw QueryExecutionErrors.invalidVariantGetPath(pathValue,
prettyName)
}
```
in a scala helper function, and call it in the generated code. You can
remove the try-catch then.
--
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]