SavicStefan commented on code in PR #59669:
URL: https://github.com/apache/spark/pull/59669#discussion_r4564190975


##########
common/variant/src/main/java/org/apache/spark/types/variant/VariantBuilder.java:
##########
@@ -205,6 +205,231 @@ 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.
+   * <p>
+   * A variant is canonical when all of the following hold.
+   * <ul>
+   *   <li>Metadata header: version {@link VariantUtil#VERSION}, the minimal 
offset size for
+   *       {@code max(lastOffset, numKeys)}, i.e. 1, 2, 3 or 4 bytes as given 
by
+   *       {@code getIntegerSize}, and {@code sorted_strings} and the unused 
bit 5 clear.
+   *       {@code sorted_strings} stays clear although canonical keys are 
sorted, so that builder
+   *       output, which never sets it, is already canonical.</li>
+   *   <li>Metadata dictionary: the first offset is 0, offsets are 
non-decreasing, the key bytes
+   *       end exactly at the last offset (no trailing bytes), keys are 
strictly increasing under
+   *       unsigned byte comparison (so they are unique), and every key is 
referenced by at least
+   *       one object field. Key bytes are preserved as is, including invalid 
UTF-8.</li>
+   *   <li>Value root: starts at position 0 and spans exactly {@code 
value.length} bytes.</li>

Review Comment:
   For a root value yes, it was there for a view into a larger variant (pos != 
0, e.g. from `getFieldByKey`), which `isCanonical` rejects, I dropped it from 
the rules, the value rule now only says value holds exactly one encoded value 
with nothing after it and views are mentioned separately as never canonical and 
rebuilt into a new root value



-- 
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]

Reply via email to