DRILL-764: Add support for 'convert_to()' and 'convert_from()' functions from SQL
Project: http://git-wip-us.apache.org/repos/asf/incubator-drill/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-drill/commit/73881506 Tree: http://git-wip-us.apache.org/repos/asf/incubator-drill/tree/73881506 Diff: http://git-wip-us.apache.org/repos/asf/incubator-drill/diff/73881506 Branch: refs/heads/master Commit: 738815061432fb610962bc72bebda3f52d5b148a Parents: 4b0d060 Author: Aditya Kishore <[email protected]> Authored: Fri May 16 17:13:44 2014 -0700 Committer: Jacques Nadeau <[email protected]> Committed: Mon May 19 18:06:13 2014 -0700 ---------------------------------------------------------------------- .../expr/fn/impl/conv/DummyConvertFrom.java | 43 ++++++ .../exec/expr/fn/impl/conv/DummyConvertTo.java | 43 ++++++ .../drill/exec/planner/logical/DrillOptiq.java | 3 + .../java/org/apache/drill/BaseTestQuery.java | 2 +- .../physical/impl/TestConvertFunctions.java | 147 +++++++++++-------- 5 files changed, 176 insertions(+), 62 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/73881506/exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/conv/DummyConvertFrom.java ---------------------------------------------------------------------- diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/conv/DummyConvertFrom.java b/exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/conv/DummyConvertFrom.java new file mode 100644 index 0000000..ac75f48 --- /dev/null +++ b/exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/conv/DummyConvertFrom.java @@ -0,0 +1,43 @@ +/******************************************************************************* + + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************/ +package org.apache.drill.exec.expr.fn.impl.conv; + +import org.apache.drill.exec.expr.DrillSimpleFunc; +import org.apache.drill.exec.expr.annotations.FunctionTemplate; +import org.apache.drill.exec.expr.annotations.FunctionTemplate.FunctionScope; +import org.apache.drill.exec.expr.annotations.FunctionTemplate.NullHandling; +import org.apache.drill.exec.expr.annotations.Output; +import org.apache.drill.exec.expr.holders.VarBinaryHolder; +import org.apache.drill.exec.record.RecordBatch; + +/** + * This and {@link DummyConvertTo} class merely act as a placeholder so that Optiq + * allows 'convert_to()' and 'convert_from()' functions in SQL. + */ +@FunctionTemplate(name = "convert_from", scope = FunctionScope.SIMPLE, nulls = NullHandling.NULL_IF_NULL) +public class DummyConvertFrom implements DrillSimpleFunc { + + @Output VarBinaryHolder out; + + @Override + public void setup(RecordBatch incoming) { } + + @Override + public void eval() { } +} http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/73881506/exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/conv/DummyConvertTo.java ---------------------------------------------------------------------- diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/conv/DummyConvertTo.java b/exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/conv/DummyConvertTo.java new file mode 100644 index 0000000..36ddf6d --- /dev/null +++ b/exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/conv/DummyConvertTo.java @@ -0,0 +1,43 @@ +/******************************************************************************* + + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************/ +package org.apache.drill.exec.expr.fn.impl.conv; + +import org.apache.drill.exec.expr.DrillSimpleFunc; +import org.apache.drill.exec.expr.annotations.FunctionTemplate; +import org.apache.drill.exec.expr.annotations.FunctionTemplate.FunctionScope; +import org.apache.drill.exec.expr.annotations.FunctionTemplate.NullHandling; +import org.apache.drill.exec.expr.annotations.Output; +import org.apache.drill.exec.expr.holders.VarBinaryHolder; +import org.apache.drill.exec.record.RecordBatch; + +/** + * This and {@link DummyConvertFrom} class merely act as a placeholder so that Optiq + * allows 'convert_to()' and 'convert_from()' functions in SQL. + */ +@FunctionTemplate(name = "convert_to", scope = FunctionScope.SIMPLE, nulls = NullHandling.NULL_IF_NULL) +public class DummyConvertTo implements DrillSimpleFunc { + + @Output VarBinaryHolder out; + + @Override + public void setup(RecordBatch incoming) { } + + @Override + public void eval() { } +} http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/73881506/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillOptiq.java ---------------------------------------------------------------------- diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillOptiq.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillOptiq.java index e900fc2..1e0f7ea 100644 --- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillOptiq.java +++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillOptiq.java @@ -326,6 +326,9 @@ public class DrillOptiq { return FunctionCallFactory.createExpression(functionName, args.subList(0, 1)); } + } else if ((functionName.equals("convert_from") || functionName.equals("convert_to")) + && args.get(1) instanceof QuotedString) { + return FunctionCallFactory.createConvert(functionName, ((QuotedString)args.get(1)).value, args.get(0), ExpressionPosition.UNKNOWN); } return FunctionCallFactory.createExpression(functionName, args); http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/73881506/exec/java-exec/src/test/java/org/apache/drill/BaseTestQuery.java ---------------------------------------------------------------------- diff --git a/exec/java-exec/src/test/java/org/apache/drill/BaseTestQuery.java b/exec/java-exec/src/test/java/org/apache/drill/BaseTestQuery.java index 062511e..8121a54 100644 --- a/exec/java-exec/src/test/java/org/apache/drill/BaseTestQuery.java +++ b/exec/java-exec/src/test/java/org/apache/drill/BaseTestQuery.java @@ -113,7 +113,7 @@ public class BaseTestQuery extends ExecTest{ return testRunAndReturn(QueryType.PHYSICAL, physical); } - private List<QueryResultBatch> testRunAndReturn(QueryType type, String query) throws Exception{ + protected List<QueryResultBatch> testRunAndReturn(QueryType type, String query) throws Exception{ query = query.replace("[WORKING_PATH]", TestTools.getWorkingPath()); return client.runQuery(type, query); } http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/73881506/exec/java-exec/src/test/java/org/apache/drill/exec/physical/impl/TestConvertFunctions.java ---------------------------------------------------------------------- diff --git a/exec/java-exec/src/test/java/org/apache/drill/exec/physical/impl/TestConvertFunctions.java b/exec/java-exec/src/test/java/org/apache/drill/exec/physical/impl/TestConvertFunctions.java index 4f9e8e9..e2935a9 100644 --- a/exec/java-exec/src/test/java/org/apache/drill/exec/physical/impl/TestConvertFunctions.java +++ b/exec/java-exec/src/test/java/org/apache/drill/exec/physical/impl/TestConvertFunctions.java @@ -30,8 +30,8 @@ import java.util.List; import mockit.Injectable; import org.apache.drill.BaseTestQuery; -import org.apache.drill.exec.exception.SchemaChangeException; import org.apache.drill.exec.expr.fn.impl.DateUtility; +import org.apache.drill.exec.proto.UserProtos.QueryType; import org.apache.drill.exec.record.RecordBatchLoader; import org.apache.drill.exec.rpc.user.QueryResultBatch; import org.apache.drill.exec.rpc.user.UserServer; @@ -66,172 +66,190 @@ public class TestConvertFunctions extends BaseTestQuery { @Test public void testDateTime1() throws Throwable { - runTest("(convert_from(binary_string('" + DATE_TIME_BE + "'), 'TIME_EPOCH_BE'))", time); + verifyPhysicalPlan("(convert_from(binary_string('" + DATE_TIME_BE + "'), 'TIME_EPOCH_BE'))", time); } @Test public void testDateTime2() throws Throwable { - runTest("convert_from(binary_string('" + DATE_TIME_LE + "'), 'TIME_EPOCH')", time); + verifyPhysicalPlan("convert_from(binary_string('" + DATE_TIME_LE + "'), 'TIME_EPOCH')", time); } @Test public void testDateTime3() throws Throwable { - runTest("convert_from(binary_string('" + DATE_TIME_BE + "'), 'DATE_EPOCH_BE')", date ); + verifyPhysicalPlan("convert_from(binary_string('" + DATE_TIME_BE + "'), 'DATE_EPOCH_BE')", date ); } @Test public void testDateTime4() throws Throwable { - runTest("convert_from(binary_string('" + DATE_TIME_LE + "'), 'DATE_EPOCH')", date); + verifyPhysicalPlan("convert_from(binary_string('" + DATE_TIME_LE + "'), 'DATE_EPOCH')", date); } @Test public void testFixedInts1() throws Throwable { - runTest("convert_from(binary_string('\\xAD'), 'TINYINT')", (byte) 0xAD); + verifyPhysicalPlan("convert_from(binary_string('\\xAD'), 'TINYINT')", (byte) 0xAD); } @Test public void testFixedInts2() throws Throwable { - runTest("convert_from(binary_string('\\xFE\\xCA'), 'SMALLINT')", (short) 0xCAFE); + verifyPhysicalPlan("convert_from(binary_string('\\xFE\\xCA'), 'SMALLINT')", (short) 0xCAFE); } @Test public void testFixedInts3() throws Throwable { - runTest("convert_from(binary_string('\\xCA\\xFE'), 'SMALLINT_BE')", (short) 0xCAFE); + verifyPhysicalPlan("convert_from(binary_string('\\xCA\\xFE'), 'SMALLINT_BE')", (short) 0xCAFE); } @Test public void testFixedInts4() throws Throwable { - runTest("convert_from(binary_string('\\xBE\\xBA\\xFE\\xCA'), 'INT')", 0xCAFEBABE); + verifyPhysicalPlan("convert_from(binary_string('\\xBE\\xBA\\xFE\\xCA'), 'INT')", 0xCAFEBABE); + } + + @Test + public void testFixedInts4SQL_from() throws Throwable { + verifySQL("select" + + " convert_from(binary_string('\\xBE\\xBA\\xFE\\xCA'), 'INT')" + + " from" + + " cp.`employee.json` LIMIT 1", + 0xCAFEBABE); + } + + @Test + public void testFixedInts4SQL_to() throws Throwable { + verifySQL("select" + + " convert_to(-889275714, 'INT')" + + " from" + + " cp.`employee.json` LIMIT 1", + new byte[] {(byte) 0xBE, (byte) 0xBA, (byte) 0xFE, (byte) 0xCA}); } @Test public void testFixedInts5() throws Throwable { - runTest("convert_from(binary_string('\\xCA\\xFE\\xBA\\xBE'), 'INT_BE')", 0xCAFEBABE); + verifyPhysicalPlan("convert_from(binary_string('\\xCA\\xFE\\xBA\\xBE'), 'INT_BE')", 0xCAFEBABE); } @Test public void testFixedInts6() throws Throwable { - runTest("convert_from(binary_string('\\xEF\\xBE\\xAD\\xDE\\xBE\\xBA\\xFE\\xCA'), 'BIGINT')", 0xCAFEBABEDEADBEEFL); + verifyPhysicalPlan("convert_from(binary_string('\\xEF\\xBE\\xAD\\xDE\\xBE\\xBA\\xFE\\xCA'), 'BIGINT')", 0xCAFEBABEDEADBEEFL); } @Test public void testFixedInts7() throws Throwable { - runTest("convert_from(binary_string('\\xCA\\xFE\\xBA\\xBE\\xDE\\xAD\\xBE\\xEF'), 'BIGINT_BE')", 0xCAFEBABEDEADBEEFL); + verifyPhysicalPlan("convert_from(binary_string('\\xCA\\xFE\\xBA\\xBE\\xDE\\xAD\\xBE\\xEF'), 'BIGINT_BE')", 0xCAFEBABEDEADBEEFL); } @Test public void testFixedInts8() throws Throwable { - runTest("convert_from(convert_to(cast(77 as varchar(2)), 'INT_BE'), 'INT_BE')", 77); + verifyPhysicalPlan("convert_from(convert_to(cast(77 as varchar(2)), 'INT_BE'), 'INT_BE')", 77); } @Test public void testFixedInts9() throws Throwable { - runTest("convert_to(cast(77 as varchar(2)), 'INT_BE')", new byte[] {0, 0, 0, 77}); + verifyPhysicalPlan("convert_to(cast(77 as varchar(2)), 'INT_BE')", new byte[] {0, 0, 0, 77}); } @Test public void testFixedInts10() throws Throwable { - runTest("convert_to(cast(77 as varchar(2)), 'INT')", new byte[] {77, 0, 0, 0}); + verifyPhysicalPlan("convert_to(cast(77 as varchar(2)), 'INT')", new byte[] {77, 0, 0, 0}); } @Test public void testFixedInts11() throws Throwable { - runTest("convert_to(77, 'BIGINT_BE')", new byte[] {0, 0, 0, 0, 0, 0, 0, 77}); + verifyPhysicalPlan("convert_to(77, 'BIGINT_BE')", new byte[] {0, 0, 0, 0, 0, 0, 0, 77}); } @Test public void testFixedInts12() throws Throwable { - runTest("convert_to(9223372036854775807, 'BIGINT')", new byte[] {-1, -1, -1, -1, -1, -1, -1, 0x7f}); + verifyPhysicalPlan("convert_to(9223372036854775807, 'BIGINT')", new byte[] {-1, -1, -1, -1, -1, -1, -1, 0x7f}); } @Test public void testFixedInts13() throws Throwable { - runTest("convert_to(-9223372036854775808, 'BIGINT')", new byte[] {0, 0, 0, 0, 0, 0, 0, (byte)0x80}); + verifyPhysicalPlan("convert_to(-9223372036854775808, 'BIGINT')", new byte[] {0, 0, 0, 0, 0, 0, 0, (byte)0x80}); } @Test public void testVInts1() throws Throwable { - runTest("convert_to(cast(0 as int), 'INT_HADOOPV')", new byte[] {0}); + verifyPhysicalPlan("convert_to(cast(0 as int), 'INT_HADOOPV')", new byte[] {0}); } @Test public void testVInts2() throws Throwable { - runTest("convert_to(cast(128 as int), 'INT_HADOOPV')", new byte[] {-113, -128}); + verifyPhysicalPlan("convert_to(cast(128 as int), 'INT_HADOOPV')", new byte[] {-113, -128}); } @Test public void testVInts3() throws Throwable { - runTest("convert_to(cast(256 as int), 'INT_HADOOPV')", new byte[] {-114, 1, 0}); + verifyPhysicalPlan("convert_to(cast(256 as int), 'INT_HADOOPV')", new byte[] {-114, 1, 0}); } @Test public void testVInts4() throws Throwable { - runTest("convert_to(cast(65536 as int), 'INT_HADOOPV')", new byte[] {-115, 1, 0, 0}); + verifyPhysicalPlan("convert_to(cast(65536 as int), 'INT_HADOOPV')", new byte[] {-115, 1, 0, 0}); } @Test public void testVInts5() throws Throwable { - runTest("convert_to(cast(16777216 as int), 'INT_HADOOPV')", new byte[] {-116, 1, 0, 0, 0}); + verifyPhysicalPlan("convert_to(cast(16777216 as int), 'INT_HADOOPV')", new byte[] {-116, 1, 0, 0, 0}); } @Test public void testVInts6() throws Throwable { - runTest("convert_to(4294967296, 'BIGINT_HADOOPV')", new byte[] {-117, 1, 0, 0, 0, 0}); + verifyPhysicalPlan("convert_to(4294967296, 'BIGINT_HADOOPV')", new byte[] {-117, 1, 0, 0, 0, 0}); } @Test public void testVInts7() throws Throwable { - runTest("convert_to(1099511627776, 'BIGINT_HADOOPV')", new byte[] {-118, 1, 0, 0, 0, 0, 0}); + verifyPhysicalPlan("convert_to(1099511627776, 'BIGINT_HADOOPV')", new byte[] {-118, 1, 0, 0, 0, 0, 0}); } @Test public void testVInts8() throws Throwable { - runTest("convert_to(281474976710656, 'BIGINT_HADOOPV')", new byte[] {-119, 1, 0, 0, 0, 0, 0, 0}); + verifyPhysicalPlan("convert_to(281474976710656, 'BIGINT_HADOOPV')", new byte[] {-119, 1, 0, 0, 0, 0, 0, 0}); } @Test public void testVInts9() throws Throwable { - runTest("convert_to(72057594037927936, 'BIGINT_HADOOPV')", new byte[] {-120, 1, 0, 0, 0, 0, 0, 0, 0}); + verifyPhysicalPlan("convert_to(72057594037927936, 'BIGINT_HADOOPV')", new byte[] {-120, 1, 0, 0, 0, 0, 0, 0, 0}); } @Test public void testVInts10() throws Throwable { - runTest("convert_to(9223372036854775807, 'BIGINT_HADOOPV')", new byte[] {-120, 127, -1, -1, -1, -1, -1, -1, -1}); + verifyPhysicalPlan("convert_to(9223372036854775807, 'BIGINT_HADOOPV')", new byte[] {-120, 127, -1, -1, -1, -1, -1, -1, -1}); } @Test public void testVInts11() throws Throwable { - runTest("convert_from(binary_string('\\x88\\x7f\\xFF\\xFF\\xFF\\xFF\\xFF\\xFF\\xFF'), 'BIGINT_HADOOPV')", 9223372036854775807L); + verifyPhysicalPlan("convert_from(binary_string('\\x88\\x7f\\xFF\\xFF\\xFF\\xFF\\xFF\\xFF\\xFF'), 'BIGINT_HADOOPV')", 9223372036854775807L); } @Test public void testVInts12() throws Throwable { - runTest("convert_to(-9223372036854775808, 'BIGINT_HADOOPV')", new byte[] {-128, 127, -1, -1, -1, -1, -1, -1, -1}); + verifyPhysicalPlan("convert_to(-9223372036854775808, 'BIGINT_HADOOPV')", new byte[] {-128, 127, -1, -1, -1, -1, -1, -1, -1}); } @Test public void testVInts13() throws Throwable { - runTest("convert_from(binary_string('\\x80\\x7f\\xFF\\xFF\\xFF\\xFF\\xFF\\xFF\\xFF'), 'BIGINT_HADOOPV')", -9223372036854775808L); + verifyPhysicalPlan("convert_from(binary_string('\\x80\\x7f\\xFF\\xFF\\xFF\\xFF\\xFF\\xFF\\xFF'), 'BIGINT_HADOOPV')", -9223372036854775808L); } @Test public void testBool1() throws Throwable { - runTest("convert_from(binary_string('\\x01'), 'BOOLEAN_BYTE')", true); + verifyPhysicalPlan("convert_from(binary_string('\\x01'), 'BOOLEAN_BYTE')", true); } @Test public void testBool2() throws Throwable { - runTest("convert_from(binary_string('\\x00'), 'BOOLEAN_BYTE')", false); + verifyPhysicalPlan("convert_from(binary_string('\\x00'), 'BOOLEAN_BYTE')", false); } @Test public void testBool3() throws Throwable { - runTest("convert_to(true, 'BOOLEAN_BYTE')", new byte[] {1}); + verifyPhysicalPlan("convert_to(true, 'BOOLEAN_BYTE')", new byte[] {1}); } @Test public void testBool4() throws Throwable { - runTest("convert_to(false, 'BOOLEAN_BYTE')", new byte[] {0}); + verifyPhysicalPlan("convert_to(false, 'BOOLEAN_BYTE')", new byte[] {0}); } @Test @@ -240,47 +258,47 @@ public class TestConvertFunctions extends BaseTestQuery { @Test public void testFloats2() throws Throwable { - runTest("convert_from(convert_to(cast(77 as float4), 'FLOAT'), 'FLOAT')", new Float(77.0)); + verifyPhysicalPlan("convert_from(convert_to(cast(77 as float4), 'FLOAT'), 'FLOAT')", new Float(77.0)); } @Test public void testFloats3() throws Throwable { - runTest("convert_to(cast(1.4e-45 as float4), 'FLOAT')", new byte[] {1, 0, 0, 0}); + verifyPhysicalPlan("convert_to(cast(1.4e-45 as float4), 'FLOAT')", new byte[] {1, 0, 0, 0}); } @Test public void testFloats4() throws Throwable { - runTest("convert_to(cast(3.4028235e+38 as float4), 'FLOAT')", new byte[] {-1, -1, 127, 127}); + verifyPhysicalPlan("convert_to(cast(3.4028235e+38 as float4), 'FLOAT')", new byte[] {-1, -1, 127, 127}); } @Test public void testFloats5(@Injectable final DrillbitContext bitContext, @Injectable UserServer.UserClientConnection connection) throws Throwable { - runTest("convert_from(convert_to(cast(77 as float8), 'DOUBLE'), 'DOUBLE')", 77.0); + verifyPhysicalPlan("convert_from(convert_to(cast(77 as float8), 'DOUBLE'), 'DOUBLE')", 77.0); } @Test public void testFloats6(@Injectable final DrillbitContext bitContext, @Injectable UserServer.UserClientConnection connection) throws Throwable { - runTest("convert_to(cast(77 as float8), 'DOUBLE')", new byte[] {0, 0, 0, 0, 0, 64, 83, 64}); + verifyPhysicalPlan("convert_to(cast(77 as float8), 'DOUBLE')", new byte[] {0, 0, 0, 0, 0, 64, 83, 64}); } @Test public void testFloats7(@Injectable final DrillbitContext bitContext, @Injectable UserServer.UserClientConnection connection) throws Throwable { - runTest("convert_to(4.9e-324, 'DOUBLE')", new byte[] {1, 0, 0, 0, 0, 0, 0, 0}); + verifyPhysicalPlan("convert_to(4.9e-324, 'DOUBLE')", new byte[] {1, 0, 0, 0, 0, 0, 0, 0}); } @Test public void testFloats8(@Injectable final DrillbitContext bitContext, @Injectable UserServer.UserClientConnection connection) throws Throwable { - runTest("convert_to(1.7976931348623157e+308, 'DOUBLE')", new byte[] {-1, -1, -1, -1, -1, -1, -17, 127}); + verifyPhysicalPlan("convert_to(1.7976931348623157e+308, 'DOUBLE')", new byte[] {-1, -1, -1, -1, -1, -1, -17, 127}); } @Test public void testUTF8() throws Throwable { - runTest("convert_from(binary_string('apache_drill'), 'UTF8')", "apache_drill"); - runTest("convert_to('apache_drill', 'UTF8')", new byte[] {'a', 'p', 'a', 'c', 'h', 'e', '_', 'd', 'r', 'i', 'l', 'l'}); + verifyPhysicalPlan("convert_from(binary_string('apache_drill'), 'UTF8')", "apache_drill"); + verifyPhysicalPlan("convert_to('apache_drill', 'UTF8')", new byte[] {'a', 'p', 'a', 'c', 'h', 'e', '_', 'd', 'r', 'i', 'l', 'l'}); } @Test @@ -340,31 +358,27 @@ public class TestConvertFunctions extends BaseTestQuery { assertEquals(intVal, Integer.MIN_VALUE); } - protected <T> void runTest(String expression, T expectedResults) throws Throwable { - String testName = String.format("Expression: %s.", expression); + protected <T> void verifySQL(String sql, T expectedResults) throws Throwable { + verifyResults(sql, expectedResults, getRunResult(QueryType.SQL, sql)); + } + + protected <T> void verifyPhysicalPlan(String expression, T expectedResults) throws Throwable { expression = expression.replace("\\", "\\\\\\\\"); // "\\\\\\\\" => Java => "\\\\" => JsonParser => "\\" => AntlrParser "\" if (textFileContent == null) textFileContent = Resources.toString(Resources.getResource(CONVERSION_TEST_PHYSICAL_PLAN), Charsets.UTF_8); String planString = textFileContent.replace("__CONVERT_EXPRESSION__", expression); - Object[] results = getRunResult(planString); - assertEquals(testName, 1, results.length); - assertNotNull(testName, results[0]); - if (expectedResults.getClass().isArray()) { - assertArraysEquals(testName, expectedResults, results[0]); - } else { - assertEquals(testName, expectedResults, results[0]); - } + verifyResults(expression, expectedResults, getRunResult(QueryType.PHYSICAL, planString)); } - protected Object[] getRunResult(String planString) throws Exception { + protected Object[] getRunResult(QueryType queryType, String planString) throws Exception { + List<QueryResultBatch> resultList = testRunAndReturn(queryType, planString); + List<Object> res = new ArrayList<Object>(); RecordBatchLoader loader = new RecordBatchLoader(getAllocator()); - - List<QueryResultBatch> resultList = testPhysicalWithResults(planString); for(QueryResultBatch result : resultList) { - loader.load(result.getHeader().getDef(), result.getData()); - if (loader.getRecordCount() > 0) { + if (result.getData() != null) { + loader.load(result.getHeader().getDef(), result.getData()); ValueVector v = loader.iterator().next().getValueVector(); for (int j = 0; j < v.getAccessor().getValueCount(); j++) { if (v instanceof VarCharVector) { @@ -373,14 +387,25 @@ public class TestConvertFunctions extends BaseTestQuery { res.add(v.getAccessor().getObject(j)); } } + loader.clear(); + result.release(); } - loader.clear(); - result.release(); } return res.toArray(); } + protected <T> void verifyResults(String expression, T expectedResults, Object[] actualResults) throws Throwable { + String testName = String.format("Expression: %s.", expression); + assertEquals(testName, 1, actualResults.length); + assertNotNull(testName, actualResults[0]); + if (expectedResults.getClass().isArray()) { + assertArraysEquals(testName, expectedResults, actualResults[0]); + } else { + assertEquals(testName, expectedResults, actualResults[0]); + } + } + protected void assertArraysEquals(Object expected, Object actual) { assertArraysEquals(null, expected, actual); }
