vlsi commented on a change in pull request #800: [CALCITE-2460][CALCITE-2459]
Add implementation of To_Base64 and From_Base64 to SqlFunctions
URL: https://github.com/apache/calcite/pull/800#discussion_r297186609
##########
File path: core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
##########
@@ -123,6 +129,39 @@
private SqlFunctions() {
}
+ /** SQL TO_BASE64(string) function. */
+ public static String toBase64(String string) {
+ return toBase64_(string.getBytes(UTF_8));
+ }
+
+ /** SQL TO_BASE64(string) function for binary string. */
+ public static String toBase64(ByteString string) {
+ return toBase64_(string.getBytes());
+ }
+
+ private static String toBase64_(byte[] bytes) {
+ String base64 = Base64.getEncoder().encodeToString(bytes);
+ StringBuilder str = new StringBuilder(base64.length() + base64.length() /
76);
+ if (base64.length() > 76) {
+ Splitter.fixedLength(76).split(base64).iterator().forEachRemaining(s -> {
+ str.append(s);
+ str.append("\n");
+ });
+ return str.substring(0, str.length() - 1);
+ }
+ return base64;
+ }
+
+ /** SQL FROM_BASE64(string) function. */
+ public static ByteString fromBase64(String base64) {
+ try {
+ base64 = FROM_BASE64_REGEXP.matcher(base64).replaceAll("");
Review comment:
Just wondering: could you please explain why
`java.util.Base64#getMimeDecoder` does not work here?
It looks like `java.util.Base64#getMimeDecoder` is just enough, and it does
not require regexp preprocessing
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services