[
https://issues.apache.org/jira/browse/DRILL-6361?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16460978#comment-16460978
]
ASF GitHub Bot commented on DRILL-6361:
---------------------------------------
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`
> Provide sqlTypeOf() and modeOf() functions
> ------------------------------------------
>
> Key: DRILL-6361
> URL: https://issues.apache.org/jira/browse/DRILL-6361
> Project: Apache Drill
> Issue Type: Improvement
> Affects Versions: 1.13.0
> Reporter: Paul Rogers
> Assignee: Paul Rogers
> Priority: Minor
> Labels: doc-impacting
> Fix For: 1.14.0
>
>
> Drill provides a {{typeof()}} function to return the type of a column. The
> returned string, however, has only the base data type. A Drill data type (a
> "major type") also includes a cardinality (a "mode"). For example, {{Optional
> Int}} or {{Required VarChar}}.
> This type information is useful for handling data conversions. For example,
> if I could tell that a column value was a {{Nullable Int}}, I could guess
> that it is one Drill invented, and I could merge it, by hand, with the type
> from another file that had actual values.
> The two options are equivalent. Either provide a {{modeOf()}} to just return
> cardinality, or a {{dataTypeOf()}} that returns both. (Maybe the {{modeOf()}}
> might be more useful.)
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)