From dev-return-37878-apmail-drill-dev-archive=drill.apache.org@drill.apache.org Wed May 2 12:52:27 2018 Return-Path: X-Original-To: apmail-drill-dev-archive@www.apache.org Delivered-To: apmail-drill-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 48B2018A8C for ; Wed, 2 May 2018 12:52:26 +0000 (UTC) Received: (qmail 75778 invoked by uid 500); 2 May 2018 12:52:26 -0000 Delivered-To: apmail-drill-dev-archive@drill.apache.org Received: (qmail 75701 invoked by uid 500); 2 May 2018 12:52:25 -0000 Mailing-List: contact dev-help@drill.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@drill.apache.org Delivered-To: mailing list dev@drill.apache.org Received: (qmail 75686 invoked by uid 99); 2 May 2018 12:52:25 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 02 May 2018 12:52:25 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 1E267E0AED; Wed, 2 May 2018 12:52:25 +0000 (UTC) From: vvysotskyi To: dev@drill.apache.org Reply-To: dev@drill.apache.org References: In-Reply-To: Subject: [GitHub] drill pull request #1242: DRILL-6361: Revised typeOf() function versions Content-Type: text/plain Message-Id: <20180502125225.1E267E0AED@git1-us-west.apache.org> Date: Wed, 2 May 2018 12:52:25 +0000 (UTC) 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 must assign the result of + * this function to your @Inject DrillBuf. That is: + *

    +   * {@literal @}Output VarCharHolder output;
    +   * {@literal @}Inject DrillBuf outputBuf;
    +   * ...
    +   *   String result = ...
    +   *   outputBuf = varCharOutput(result, outputBuf, output);
    +   * 
+ * + * @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` ---