osipovartem commented on code in PR #11409:
URL: https://github.com/apache/arrow-rs/pull/11409#discussion_r4702981060
##########
parquet-variant-json/src/from_json.rs:
##########
@@ -667,4 +1043,136 @@ mod test {
}
.run()
}
+
+ #[test]
+ fn test_json_to_variant_escaped_strings() -> Result<(), ArrowError> {
+ let json = r#"{"line\nkey":"quote: \"; slash: \\; unicode: \u2764"}"#;
+ let mut variant_builder = VariantBuilder::new();
+ variant_builder.append_json(json)?;
+ let (metadata, value) = variant_builder.finish();
+ let variant = Variant::try_new(&metadata, &value)?;
+ assert_eq!(
+ variant.to_json_string()?,
+ "{\"line\\nkey\":\"quote: \\\"; slash: \\\\; unicode: ❤\"}"
+ );
+ Ok(())
+ }
+
+ #[test]
+ fn test_json_to_variant_rejects_invalid_json() {
+ let invalid = [
+ "",
+ "nul",
+ "True",
+ "[",
+ "{",
+ "[1,]",
+ "[1 2]",
+ "[1,,2]",
+ "{\"a\":1,}",
+ "{\"a\" 1}",
+ "{\"a\":1 \"b\":2}",
+ concat!("{", "a:1}"),
+ "01",
+ "-01",
+ "+1",
+ ".1",
+ "1.",
+ "1e",
+ "1e+",
+ "true false",
+ "\"unterminated",
+ "\"bad\\xescape\"",
+ "\"bad\\u12x4\"",
+ "\"lone high surrogate: \\ud800\"",
+ "\"lone low surrogate: \\udc00\"",
+ ];
+
+ for json in invalid {
+ let mut builder = VariantBuilder::new();
+ assert!(builder.append_json(json).is_err(), "accepted {json:?}");
+ }
+ }
+
+ #[test]
+ fn test_json_to_variant_rejects_non_finite_double() {
+ let mut builder = VariantBuilder::new();
+ let error = builder.append_json("1e400").unwrap_err().to_string();
+ assert!(error.contains("finite number"), "{error}");
+ }
+
+ #[test]
+ fn test_json_to_variant_limits_nesting_depth() {
+ let accepted = format!(
+ "{}0{}",
+ "[".repeat(MAX_JSON_DEPTH),
+ "]".repeat(MAX_JSON_DEPTH)
+ );
+ let mut builder = VariantBuilder::new();
+ builder.append_json(&accepted).unwrap();
+
+ let rejected = format!(
+ "{}0{}",
+ "[".repeat(MAX_JSON_DEPTH + 1),
+ "]".repeat(MAX_JSON_DEPTH + 1)
+ );
+ let mut builder = VariantBuilder::new();
+ let error = builder.append_json(&rejected).unwrap_err().to_string();
+ assert!(error.contains("recursion limit"), "{error}");
+ }
Review Comment:
Added boundary cases for scalar, empty array, and empty object leaves. The
accepted values also pass `Variant::try_new`; a separate test covers appending
inside an existing builder.
##########
parquet-variant-json/src/from_json.rs:
##########
@@ -407,6 +756,33 @@ mod test {
.run()
}
+ #[test]
+ fn test_json_to_variant_preserves_existing_number_kinds() -> Result<(),
ArrowError> {
+ for (json, expected) in [
+ ("-0", Variant::Double(-0.0)),
+ ("1.23", Variant::Double(1.23)),
+ ("18446744073709551615", Variant::Double(2_f64.powi(64))),
+ ] {
+ JsonToVariantTest { json, expected }.run()?;
+ }
+ Ok(())
+ }
+
+ #[test]
+ fn test_json_to_variant_preserves_serde_float_bits() -> Result<(),
ArrowError> {
+ let json = "0.9999999999999999";
+ let parsed: Number = serde_json::from_str(json).unwrap();
+ let expected = parsed.as_f64().unwrap();
+ let mut builder = VariantBuilder::new();
+ builder.append_json(json)?;
+ let (metadata, value) = builder.finish();
+ let Variant::Double(actual) = Variant::try_new(&metadata, &value)?
else {
+ panic!("expected a Double")
+ };
+ assert_eq!(actual.to_bits(), expected.to_bits());
+ Ok(())
+ }
Review Comment:
Both rounding cases are now checked by their exact `f64` bits, alongside the
sign bit of `-0.0`. Integer `-0` is covered separately.
##########
parquet-variant-json/src/from_json.rs:
##########
@@ -356,35 +729,11 @@ mod test {
.run()
}
- #[ignore]
- #[test]
- fn test_json_to_variant_decimal16_max_value() -> Result<(), ArrowError> {
- JsonToVariantTest {
- json: "79228162514264337593543950335", // 2 ^ 96 - 1
- expected:
Variant::from(VariantDecimal16::try_new(79228162514264337593543950335, 0)?),
- }
- .run()
- }
-
- #[ignore]
- #[test]
- fn test_json_to_variant_decimal16_max_scale() -> Result<(), ArrowError> {
- JsonToVariantTest {
- json: "7.9228162514264337593543950335", // using scale higher than
this falls into double
- // since the max scale is 28.
- expected: Variant::from(VariantDecimal16::try_new(
- 79228162514264337593543950335,
- 28,
- )?),
- }
- .run()
- }
-
#[test]
fn test_json_to_variant_double_precision() -> Result<(), ArrowError> {
JsonToVariantTest {
- json: "0.79228162514264337593543950335",
- expected: Variant::Double(0.792_281_625_142_643_4_f64),
+ json: "0.100000000000000000000000000000000000000",
+ expected: Variant::Double(0.1_f64),
Review Comment:
The ignored decimal tests now match `main`, including the two Decimal16
cases. Exact-decimal encoding and its tests stay in #11415.
--
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]