aboutsummaryrefslogtreecommitdiff
path: root/exec/java-exec
diff options
context:
space:
mode:
authorAditya Kishore <adi@apache.org>2015-01-01 02:56:44 -0800
committerAditya Kishore <adi@apache.org>2015-01-20 22:55:52 -0800
commit50c0949ee76753de5360fe158e9b923e45be4d83 (patch)
tree8a447be5a19c8caa37ebdd9f4ab790a3ad4111fd /exec/java-exec
parenta8036d29e0d50e4753b152803b13e1337800c67c (diff)
DRILL-1922: Add a string_binary() function to pretty print a VARBINARY type as VARCHAR
Diffstat (limited to 'exec/java-exec')
-rw-r--r--exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/StringFunctions.java27
1 files changed, 25 insertions, 2 deletions
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/StringFunctions.java b/exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/StringFunctions.java
index bad394ce9..83e812dd0 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/StringFunctions.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/StringFunctions.java
@@ -970,11 +970,34 @@ public class StringFunctions{
out.buffer = in.buffer;
out.start = in.start;
out.end = org.apache.drill.common.util.DrillStringUtils.parseBinaryString(in.buffer, in.start, in.end);
- out.buffer.readerIndex(out.start);
- out.buffer.writerIndex(out.end);
+ out.buffer.setIndex(out.start, out.end);
}
}
+ // Converts a varbinary type into a hex encoded string.
+ // (byte[]) {(byte)0xca, (byte)0xfe, (byte)0xba, (byte)0xbe} => "\xca\xfe\xba\xbe"
+ @FunctionTemplate(name = "string_binary", scope = FunctionScope.SIMPLE, nulls = NullHandling.NULL_IF_NULL)
+ public static class StringBinary implements DrillSimpleFunc {
+
+ @Param VarBinaryHolder in;
+ @Output VarCharHolder out;
+ @Workspace Charset charset;
+ @Inject DrillBuf buffer;
+
+ public void setup(RecordBatch incoming) {
+ charset = java.nio.charset.Charset.forName("UTF-8");
+ }
+
+ public void eval() {
+ byte[] buf = org.apache.drill.common.util.DrillStringUtils.toBinaryString(in.buffer, in.start, in.end).getBytes(charset);
+ buffer.setBytes(0, buf);
+ buffer.setIndex(0, buf.length);
+
+ out.start = 0;
+ out.end = buf.length;
+ out.buffer = buffer;
+ }
+ }
/**
* Returns the ASCII code of the first character of input string