Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-29444] Add configuration to support JacksonGenrator to keep fields with null values #26098

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.fasterxml.jackson.core.{JsonFactory, JsonParser}

import org.apache.spark.internal.Logging
import org.apache.spark.sql.catalyst.util._
import org.apache.spark.sql.internal.SQLConf

/**
* Options for parsing JSON data into Spark SQL rows.
Expand Down Expand Up @@ -76,6 +77,10 @@ private[sql] class JSONOptions(
// Whether to ignore column of all null values or empty array/struct during schema inference
val dropFieldIfAllNull = parameters.get("dropFieldIfAllNull").map(_.toBoolean).getOrElse(false)

// Whether to ignore null fields during json generating
val ignoreNullFields = parameters.getOrElse("ignoreNullFields",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, you should document this in DataFrameWrtier, DataStreamWrtier, readwriter.py

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okey, I'll try to follow up this PR, and add this to document

SQLConf.get.jsonGeneratorIgnoreNullFields).toBoolean

// A language tag in IETF BCP 47 format
val locale: Locale = parameters.get("locale").map(Locale.forLanguageTag).getOrElse(Locale.US)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ private[sql] class JacksonGenerator(
if (!row.isNullAt(i)) {
gen.writeFieldName(field.name)
fieldWriters(i).apply(row, i)
} else if (!options.ignoreNullFields) {
gen.writeFieldName(field.name)
gen.writeNull()
}
i += 1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,12 @@ object SQLConf {
.booleanConf
.createWithDefault(true)

val JSON_GENERATOR_IGNORE_NULL_FIELDS =
buildConf("spark.sql.jsonGenerator.ignoreNullFields")
.doc("If false, JacksonGenerator will generate null for null fields in Struct.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I were a user, I would have no idea what JacksonGenerator is ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I can describe this in a better way.

.stringConf
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it a string conf? shouldn't it be a boolean conf?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is only used in JasonOptions, where we need string value, and use toBoolean to get boolean conf

.createWithDefault("true")

val FILE_SINK_LOG_DELETION = buildConf("spark.sql.streaming.fileSink.log.deletion")
.internal()
.doc("Whether to delete the expired log files in file stream sink.")
Expand Down Expand Up @@ -2323,6 +2329,8 @@ class SQLConf extends Serializable with Logging {

def sessionLocalTimeZone: String = getConf(SQLConf.SESSION_LOCAL_TIMEZONE)

def jsonGeneratorIgnoreNullFields: String = getConf(SQLConf.JSON_GENERATOR_IGNORE_NULL_FIELDS)

def parallelFileListingInStatsComputation: Boolean =
getConf(SQLConf.PARALLEL_FILE_LISTING_IN_STATS_COMPUTATION)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,33 @@ class JacksonGeneratorSuite extends SparkFunSuite {
assert(writer.toString === """{"a":1}""")
}

test("SPARK-29444: initial with StructType and write out an empty row " +
"with ignoreNullFields=false") {
val dataType = StructType(StructField("a", IntegerType) :: Nil)
val input = InternalRow(null)
val writer = new CharArrayWriter()
val allowNullOption =
new JSONOptions(Map("ignoreNullFields" -> "false"), gmtId)
val gen = new JacksonGenerator(dataType, writer, allowNullOption)
gen.write(input)
gen.flush()
assert(writer.toString === """{"a":null}""")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we also test null inner field? e.g. {"a": {"b": null}}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I have added a test for this

}

test("SPARK-29444: initial with StructType field and write out a row " +
"with ignoreNullFields=false and struct inner null") {
val fieldType = StructType(StructField("b", IntegerType) :: Nil)
val dataType = StructType(StructField("a", fieldType) :: Nil)
val input = InternalRow(InternalRow(null))
val writer = new CharArrayWriter()
val allowNullOption =
new JSONOptions(Map("ignoreNullFields" -> "false"), gmtId)
val gen = new JacksonGenerator(dataType, writer, allowNullOption)
gen.write(input)
gen.flush()
assert(writer.toString === """{"a":{"b":null}}""")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit but I would call close with a try-catch for a best practice in a followup.

}

test("initial with StructType and write out rows") {
val dataType = StructType(StructField("a", IntegerType) :: Nil)
val input = new GenericArrayData(InternalRow(1) :: InternalRow(2) :: Nil)
Expand Down