harshmotw-db commented on code in PR #59669:
URL: https://github.com/apache/spark/pull/59669#discussion_r4591876433
##########
common/variant/src/main/java/org/apache/spark/types/variant/VariantBuilder.java:
##########
@@ -205,6 +205,317 @@ public static Variant pickAtPaths(Variant v, PickNode
tree) {
return builder.result();
}
+ /**
+ * Return {@code v} in canonical form, or {@code v} itself if it is already
canonical.
+ * <p>
+ * The variant binary encoding allows the same logical value to be encoded
in several ways
+ * (dictionary order, unused keys, integer and offset widths, decimal scale,
...). Canonical form
+ * picks one encoding per value, so that values which are equal under the
variant equality rules
+ * have identical {@code value} and {@code metadata} bytes and can be
compared, hashed and
+ * grouped bytewise. It is a Spark-internal normalization, not part of the
variant spec, and
+ * nothing outside Spark is expected to produce or require it. Only valid
variants are supported.
+ * Malformed bytes fail with {@code MALFORMED_VARIANT} where they are
detected; repeated field
+ * names within an object fail with {@code VARIANT_DUPLICATE_KEY}.
+ * <p>
+ * A variant is canonical when its {@code metadata} and {@code value}
satisfy all of the following
+ * rules. The scalar and container encoding rules apply recursively to every
nested value.
+ * <ul>
+ * <li>Metadata header: version {@link VariantUtil#VERSION}, {@code
sorted_strings} and the
+ * reserved bit 5 clear, and the smallest offset size (1 to 4 bytes)
that holds both the
+ * key count and the total key length, as the key count is stored with
the same size.
+ * {@code sorted_strings} stays clear although the keys are sorted:
this builder never sets
+ * it, and keeping it clear avoids rebuilding otherwise canonical
builder output only to
+ * set it.</li>
+ * <li>Metadata dictionary: the first offset is 0 and the offsets are
non-decreasing, the key
+ * bytes end exactly at the last offset, the keys are strictly
increasing under unsigned
+ * byte comparison (so each key appears once), and every key is used
by some object
+ * field.</li>
+ * <li>Value: {@code value} holds exactly one encoded value, with no bytes
after it.</li>
+ * <li>Object: the field ids are strictly increasing, which stores the
fields in key order and
+ * rules out a repeated key; the field values are canonical and stored
contiguously in
+ * field order, so each offset is the sum of the preceding value sizes
and the last offset
+ * is the total size; the header uses the smallest id size for the
largest id (one byte for
+ * an empty object), the smallest offset size for the total size,
{@code is_large} only for
+ * more than 255 fields, and has the reserved bit 7 clear.</li>
+ * <li>Array: the elements are canonical and stored contiguously in
element order, so each
+ * offset is the sum of the preceding element sizes and the last
offset is the total size;
+ * the header uses the smallest offset size for the total size, {@code
is_large} only for
+ * more than 255 elements, and has the reserved bits 5 to 7 clear.</li>
+ * <li>Integer: the smallest of INT1, INT2, INT4 and INT8 that holds the
value.</li>
+ * <li>Decimal: trailing zeros stripped, and a scale that stripping made
negative (1E+20 for
+ * 10^20) set back to 0. An integral value that fits in a signed
64-bit integer is stored
+ * as an integer instead. Otherwise the smallest of DECIMAL4
(precision and scale at most
+ * 9), DECIMAL8 (at most 18) and DECIMAL16 that holds the value.</li>
+ * <li>Float and double: -0.0 is stored as 0.0 and NaN as the canonical
NaN bit pattern of
+ * {@link Float#floatToIntBits} and {@link Double#doubleToLongBits}.
The type is kept, so
+ * a float and a double are never identical.</li>
+ * <li>String: strings of at most {@link VariantUtil#MAX_SHORT_STR_SIZE}
bytes use the
+ * short-string encoding, longer strings the long-string encoding.</li>
+ * <li>Null, boolean, date, microsecond timestamp and timestamp_ntz,
binary and uuid have a
+ * single encoding in the spec and are kept as they are.</li>
+ * </ul>
+ * These are all the supported types; a value of any other type fails with
+ * {@code UNKNOWN_PRIMITIVE_TYPE_IN_VARIANT}.
Review Comment:
Do we have tests for this?
--
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]