diff --git a/sql-plugin/src/main/scala/com/nvidia/spark/rapids/GpuCast.scala b/sql-plugin/src/main/scala/com/nvidia/spark/rapids/GpuCast.scala index 2f59cfba072..a732d912b8e 100644 --- a/sql-plugin/src/main/scala/com/nvidia/spark/rapids/GpuCast.scala +++ b/sql-plugin/src/main/scala/com/nvidia/spark/rapids/GpuCast.scala @@ -1113,13 +1113,15 @@ object GpuCast { } if (options.ignoreNullFieldsInStructs) { // write the value - val attrValue = castToString(cv, inputSchema(fieldIndex).dataType, options) - if (needsQuoting) { - attrColumns += quote.incRefCount() - attrColumns += escapeJsonString(attrValue) - attrColumns += quote.incRefCount() - } else { - attrColumns += attrValue + withResource(castToString(cv, inputSchema(fieldIndex).dataType, options)) { + attrValue => + if (needsQuoting) { + attrColumns += quote.incRefCount() + attrColumns += escapeJsonString(attrValue) + attrColumns += quote.incRefCount() + } else { + attrColumns += attrValue.incRefCount() + } } // now concatenate val jsonAttr = withResource(Scalar.fromString("")) { emptyString =>