[
https://issues.apache.org/jira/browse/DRILL-6361?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16464288#comment-16464288
]
ASF GitHub Bot commented on DRILL-6361:
---------------------------------------
vvysotskyi commented on a change in pull request #1242: DRILL-6361: Added
typeOf() function variations
URL: https://github.com/apache/drill/pull/1242#discussion_r186186793
##########
File path:
exec/java-exec/src/test/java/org/apache/drill/exec/expr/fn/impl/TestTypeFns.java
##########
@@ -0,0 +1,197 @@
+/*
+ * 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;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.drill.exec.planner.physical.PlannerSettings;
+import org.apache.drill.exec.rpc.RpcException;
+import org.apache.drill.test.ClusterFixture;
+import org.apache.drill.test.ClusterFixtureBuilder;
+import org.apache.drill.test.ClusterTest;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class TestTypeFns extends ClusterTest {
+
+ @BeforeClass
+ public static void setup() throws Exception {
+ // Use the following three lines if you add a function
+ // to avoid the need for a full Drill build.
+ ClusterFixtureBuilder builder = ClusterFixture.builder(dirTestWatcher)
+ .configProperty("drill.classpath.scanning.cache.enabled", false);
+ startCluster(builder);
+
+ // Use the following line if a full Drill build has been
+ // done since adding new functions.
+//
startCluster(ClusterFixture.builder(dirTestWatcher).maxParallelization(1));
+ }
+
+ @Test
+ public void testTypeOf() throws RpcException {
+ // SMALLINT not supported in CAST
+ //doTypeOfTest("SMALLINT");
+ doTypeOfTest("INT");
+ doTypeOfTest("BIGINT");
+ doTypeOfTest("VARCHAR");
+ doTypeOfTest("FLOAT", "FLOAT4");
+ doTypeOfTest("DOUBLE", "FLOAT8");
+ doTypeOfTestSpecial("a", "true", "BIT");
+ doTypeOfTestSpecial("a", "CURRENT_DATE", "DATE");
+ doTypeOfTestSpecial("a", "CURRENT_TIME", "TIME");
+ doTypeOfTestSpecial("a", "CURRENT_TIMESTAMP", "TIMESTAMP");
+ doTypeOfTestSpecial("a", "AGE(CURRENT_TIMESTAMP)", "INTERVAL");
+ doTypeOfTestSpecial("BINARY_STRING(a)", "'\\xde\\xad\\xbe\\xef'",
"VARBINARY");
+ try {
+ client.alterSession(PlannerSettings.ENABLE_DECIMAL_DATA_TYPE_KEY, true);
+ doTypeOfTestSpecial("CAST(a AS DECIMAL)", "1", "DECIMAL38SPARSE");
+ doTypeOfTestSpecial("CAST(a AS DECIMAL(6, 3))", "1", "DECIMAL9");
+ } finally {
+ client.resetSession("planner.enable_decimal_data_type");
+ }
+ }
+
+ private void doTypeOfTest(String type) throws RpcException {
+ doTypeOfTest(type, type);
+ }
+
+ private void doTypeOfTest(String castType, String resultType) throws
RpcException {
+
+ // typeof() returns types using the internal names.
+
+ String sql = "SELECT typeof(CAST(a AS " + castType + ")) FROM (VALUES (1))
AS T(a)";
+ String result = client.queryBuilder().sql(sql).singletonString();
+ assertEquals(resultType, result);
+
+ // For typeof(), null values annoyingly report a type of "NULL"
+
+ sql = "SELECT typeof(CAST(a AS " + castType + ")) FROM
cp.`functions/null.json`";
+ result = client.queryBuilder().sql(sql).singletonString();
+ assertEquals("NULL", result);
+ }
+
+ private void doTypeOfTestSpecial(String expr, String value, String
resultType) throws RpcException {
+ String sql = "SELECT typeof(" + expr + ") FROM (VALUES (" + value + ")) AS
T(a)";
+ String result = client.queryBuilder().sql(sql).singletonString();
+ assertEquals(resultType, result);
+ }
+
+ @Test
+ public void testSqlTypeOf() throws RpcException {
+ // SMALLINT not supported in CAST
+ //doSqlTypeOfTest("SMALLINT");
+ doSqlTypeOfTest("INTEGER");
+ doSqlTypeOfTest("BIGINT");
+ doSqlTypeOfTest("CHARACTER VARYING");
+ doSqlTypeOfTest("FLOAT");
+ doSqlTypeOfTest("DOUBLE");
+ doSqlTypeOfTestSpecial("a", "true", "BOOLEAN");
+ doSqlTypeOfTestSpecial("a", "CURRENT_DATE", "DATE");
+ doSqlTypeOfTestSpecial("a", "CURRENT_TIME", "TIME");
+ doSqlTypeOfTestSpecial("a", "CURRENT_TIMESTAMP", "TIMESTAMP");
+ doSqlTypeOfTestSpecial("a", "AGE(CURRENT_TIMESTAMP)", "INTERVAL");
+ doSqlTypeOfTestSpecial("BINARY_STRING(a)", "'\\xde\\xad\\xbe\\xef'",
"BINARY VARYING");
+ try {
+ client.alterSession(PlannerSettings.ENABLE_DECIMAL_DATA_TYPE_KEY, true);
+
+ // These should include precision and scale: DECIMAL(p, s)
+ // But, see DRILL-6378
+
+ doSqlTypeOfTestSpecial("CAST(a AS DECIMAL)", "1", "DECIMAL");
+ doSqlTypeOfTestSpecial("CAST(a AS DECIMAL(6, 3))", "1", "DECIMAL");
+ } finally {
+ client.resetSession("planner.enable_decimal_data_type");
Review comment:
And there
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> 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)