Github user vvysotskyi commented on a diff in the pull request:
https://github.com/apache/drill/pull/1242#discussion_r185464800
--- Diff:
exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/StringFunctionHelpers.java
---
@@ -202,6 +204,127 @@ public static String toStringFromUTF16(int start, int
end, DrillBuf buffer) {
return new String(buf, Charsets.UTF_16);
}
+ /**
+ * Convert a non-nullable VarChar input to a Java string, assuming UTF-8
encoding.
+ *
+ * @param input the VarChar holder for the input parameter
+ * @return Java String that holds the value
+ */
+
+ public static String varCharToString(VarCharHolder input) {
+ return
org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.getStringFromVarCharHolder(input);
+ }
+
+ /**
+ * Convert a nullable VarChar input to a Java string, assuming UTF-8
encoding.
+ *
+ * @param input the VarChar holder for the input parameter
+ * @return Java String that holds the value, or null if the input
+ * is null
+ */
+
+ public static String varCharToString(NullableVarCharHolder input) {
+ if (input.isSet == 0) {
+ return null;
+ }
+ return
org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.getStringFromVarCharHolder(input);
+ }
+
+ /**
+ * Convert a non-nullable VarBinary to a Java string, assuming that the
+ * VarBinary is actually a UTF-8 encoded string, as is often the case
+ * with HBase.
+ *
+ * @param input the input VarBinary parameter
+ * @return a Java string
+ */
+
+ public static String varBinaryToString(VarBinaryHolder input) {
+ int len = input.end - input.start;
+ byte buf[] = new byte[len];
+ input.buffer.getBytes(input.start, buf);
+ return new String(buf, Charsets.UTF_8);
+ }
+
+ /**
+ * Convert a nullable VarBinary to a Java string, assuming that the
+ * VarBinary is actually a UTF-8 encoded string, as is often the case
+ * with HBase.
+ *
+ * @param input the input VarBinary parameter
+ * @return a Java string, or null if the parameter was NULL
+ */
+
+ public static String varBinaryToString(NullableVarBinaryHolder input) {
+ if (input.isSet == 0) {
+ return null;
+ }
+ int len = input.end - input.start;
+ byte buf[] = new byte[len];
+ input.buffer.getBytes(input.start, buf);
+ return new String(buf, Charsets.UTF_8);
+ }
+
+ /**
+ * Convert a Java string to a Drill non-nullable output. Encodes the
+ * Java string into a set of UTF-8 bytes. Resizes the working
+ * buffer if needed. For this reason, you <b>must</b> assign the result
of
+ * this function to your <tt>@Inject</tt> <tt>DrillBuf</tt>. That is:
+ * <pre><code>
+ * {@literal @}Output VarCharHolder output;
+ * {@literal @}Inject DrillBuf outputBuf;
+ * ...
+ * String result = ...
+ * outputBuf = varCharOutput(result, outputBuf, output);
+ * </code></pre>
+ *
+ * @param result the (non-null) string value to return
+ * @param outputBuf the output buffer identified by the
+ * {@literal @}Inject annotation
+ * @param output the non-nullable VarChar holder for the function output
+ * identified by the {@literal @}Output annotation
+ * @return the (possibly new) output buffer
+ */
+
+ public static DrillBuf varCharOutput(String result, DrillBuf outputBuf,
VarCharHolder output) {
+ byte outBytes[] =
result.toString().getBytes(com.google.common.base.Charsets.UTF_8);
--- End diff --
Please remove `toString()` call: `result.toString().getBytes` ->
`result.getBytes`
---