cj-zhukov commented on code in PR #395:
URL: https://github.com/apache/datafusion-spark/pull/395#discussion_r4677849803
##########
crates/spark/src/function/json/to_json.rs:
##########
@@ -145,8 +186,183 @@ impl ScalarUDFImpl for SparkToJson {
}
}
+/// The options of Spark's to_json that are supported.
+///
+/// The names and what they mean are Spark's: to_json takes the options of the
+/// JSON data source, which are listed in
+///
<https://spark.apache.org/docs/latest/sql-data-sources-json.html#data-source-option>
+///
+/// Supporting another of those options starts with a variant here. A name that
+/// is not a variant is an error: ignoring an option that Spark applies would
+/// give a different result without saying so.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+enum ToJsonOption {
+ /// `ignoreNullFields`: whether NULL struct fields are omitted
+ IgnoreNullFields,
+}
+
+impl ToJsonOption {
+ /// Every supported option.
+ const ALL: [Self; 1] = [Self::IgnoreNullFields];
+
+ /// The option's name in Spark.
+ fn name(self) -> &'static str {
+ match self {
+ Self::IgnoreNullFields => "ignoreNullFields",
+ }
+ }
+
+ /// Finds the option called `name`. Spark's option names are
+ /// case-insensitive.
+ fn parse(name: &str) -> Option<Self> {
+ Self::ALL
+ .into_iter()
+ .find(|option| option.name().eq_ignore_ascii_case(name))
+ }
+}
+
+/// The values of the supported options (see `ToJsonOption`), with Spark's
+/// defaults.
+#[derive(Debug, Clone, PartialEq)]
+struct ToJsonOptions {
+ /// `ignoreNullFields`: NULL struct fields are omitted rather than written
+ /// as `null`. It is on by default, as Spark's
+ /// `spark.sql.jsonGenerator.ignoreNullFields` is.
+ ignore_null_fields: bool,
+}
+
+impl Default for ToJsonOptions {
+ fn default() -> Self {
+ Self {
+ ignore_null_fields: true,
+ }
+ }
+}
+
+impl ToJsonOptions {
+ /// Reads the options argument when the function runs, if there is one.
+ /// Spark only takes a `map(...)` call of strings there, so the argument
+ /// has to be a constant.
+ fn try_new(options: Option<&ColumnarValue>) -> Result<Self> {
+ match options {
+ None => Ok(Self::default()),
+ Some(ColumnarValue::Scalar(scalar)) =>
Self::try_from_scalar(scalar),
+ Some(ColumnarValue::Array(_)) => plan_err!(
+ "to_json: the options argument must be a constant map of
strings"
+ ),
+ }
+ }
+
+ /// Reads a constant options map. `return_field_from_args` calls it while
+ /// the query is planned, when the argument is a constant by then, and
+ /// `try_new` calls it when the function runs.
+ fn try_from_scalar(options: &ScalarValue) -> Result<Self> {
+ let ScalarValue::Map(map) = options else {
+ return plan_err!(
+ "to_json: the options argument must be a constant map of
strings"
+ );
+ };
+ check_options_type(map.data_type())?;
+ if map.len() != 1 || map.is_null(0) {
+ return plan_err!("to_json: the options argument must not be NULL");
+ }
+
+ let mut parsed = Self::default();
+ let entries = map.value(0);
+ let keys = cast(entries.column(0), &DataType::Utf8)?;
+ let values = cast(entries.column(1), &DataType::Utf8)?;
+ let (keys, values) = (keys.as_string::<i32>(),
values.as_string::<i32>());
+ for i in 0..keys.len() {
+ // Spark reads a NULL value as the string "null". The NULL option
+ // records in json/to_json.slt cover this, checked against Spark
+ // 4.2.0.
Review Comment:
@andygrove thanks for checking this on Spark 3.5 as well, I had only tested
4.2.
I'll update the code comment and the comments above the two `.slt` records
to say Spark 4.2 instead of Spark in general.
And thanks for offering to handle the `spark>=4.0` condition on the
`noSuchOption` marker once #391 lands.
--
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]