mihaibudiu commented on code in PR #5191:
URL: https://github.com/apache/calcite/pull/5191#discussion_r3815614187
##########
core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java:
##########
@@ -351,9 +351,74 @@ public static String uuidToString(UUID uuid) {
return uuid.toString();
}
+ /** Converts a string to a UUID: 32 hexadecimal digits. All of the following
give
+ * the UUID {@code 123e4567-e89b-12d3-a456-426655440000}:
+ *
+ * <blockquote><pre>
+ * 123e4567-e89b-12d3-a456-426655440000
+ * 123E4567-E89B-12D3-A456-426655440000
+ * 123e4567e89b12d3a456426655440000
+ * {123e4567-e89b-12d3-a456-426655440000}
+ * {123e4567e89b12d3a456426655440000}
+ * 123e-4567-e89b-12d3-a456-4266-5544-0000
+ * 123e4567-e89b12d3-a4564266-55440000
+ * 123e-4567e89b-12d3a456426655440000
+ * </pre></blockquote>
+ *
+ * <p>and each of the following is an error:
+ *
+ * <blockquote><pre>
+ * 1-2-3-4-5 a group is not four digits wide
+ * 123e456-7e89b-12d3-a456-426655440000 as above, though 36 characters long
+ * 123e4567--e89b-12d3-a456-426655440000 empty group
+ * -123e4567e89b12d3a456426655440000 leading hyphen
+ * 123e4567e89b12d3a456426655440000- trailing hyphen
+ * {123e4567-e89b-12d3-a456-426655440000 unbalanced brace
+ * 123e4567-e89b-12d3-a456-42665544000 31 digits
+ * </pre></blockquote>
+ */
+ public static UUID stringToUuid(String s) {
+ String body = s;
+ if (body.length() > 1
+ && body.charAt(0) == '{'
+ && body.charAt(body.length() - 1) == '}') {
+ body = body.substring(1, body.length() - 1);
+ }
+ final StringBuilder digits = new StringBuilder(32);
+ for (int i = 0; i < body.length(); i++) {
+ final char c = body.charAt(i);
+ if (c == '-') {
+ // A hyphen separates groups, so it must follow a complete group of
four
+ // digits and cannot be the last character
+ if (digits.length() == 0
+ || digits.length() % 4 != 0
+ || digits.length() == 32
+ || body.charAt(i - 1) == '-') {
+ throw new IllegalArgumentException("Invalid UUID string: " + s);
+ }
+ } else if (isHexDigit(c) && digits.length() < 32) {
+ digits.append(c);
+ } else {
+ throw new IllegalArgumentException("Invalid UUID string: " + s);
+ }
+ }
+ if (digits.length() != 32) {
+ throw new IllegalArgumentException("Invalid UUID string: " + s);
+ }
+ return new UUID(
+ Long.parseUnsignedLong(digits.substring(0, 16), 16),
+ Long.parseUnsignedLong(digits.substring(16), 16));
+ }
+
+ private static boolean isHexDigit(char c) {
Review Comment:
I will remove the duplicate
--
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]