Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class HBaseTestsSuite {
protected static final String TEST_TABLE_FLOAT_OB_DESC = "TestTableFloatOBDesc";
protected static final String TEST_TABLE_BIGINT_OB_DESC = "TestTableBigIntOBDesc";
protected static final String TEST_TABLE_INT_OB_DESC = "TestTableIntOBDesc";
protected static final String TEST_TABLE_NULL_STR = "TestTableNullStr";

private static Configuration conf;

Expand Down Expand Up @@ -159,7 +160,8 @@ private static boolean tablesExist() throws IOException {
&& admin.tableExists(TEST_TABLE_DOUBLE_OB_DESC)
&& admin.tableExists(TEST_TABLE_FLOAT_OB_DESC)
&& admin.tableExists(TEST_TABLE_BIGINT_OB_DESC)
&& admin.tableExists(TEST_TABLE_INT_OB_DESC);
&& admin.tableExists(TEST_TABLE_INT_OB_DESC)
&& admin.tableExists(TEST_TABLE_NULL_STR);
}

private static void createTestTables() throws Exception {
Expand All @@ -183,6 +185,7 @@ private static void createTestTables() throws Exception {
TestTableGenerator.generateHBaseDatasetFloatOBDesc(admin, TEST_TABLE_FLOAT_OB_DESC, 1);
TestTableGenerator.generateHBaseDatasetBigIntOBDesc(admin, TEST_TABLE_BIGINT_OB_DESC, 1);
TestTableGenerator.generateHBaseDatasetIntOBDesc(admin, TEST_TABLE_INT_OB_DESC, 1);
TestTableGenerator.generateHBaseDatasetNullStr(admin, TEST_TABLE_NULL_STR, 1);
}

private static void cleanupTestTables() throws IOException {
Expand Down Expand Up @@ -212,6 +215,8 @@ private static void cleanupTestTables() throws IOException {
admin.deleteTable(TEST_TABLE_BIGINT_OB_DESC);
admin.disableTable(TEST_TABLE_INT_OB_DESC);
admin.deleteTable(TEST_TABLE_INT_OB_DESC);
admin.disableTable(TEST_TABLE_NULL_STR);
admin.deleteTable(TEST_TABLE_NULL_STR);
}

public static int getZookeeperPort() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
package org.apache.drill.hbase;

import java.util.Arrays;
import java.util.List;

import org.apache.drill.exec.rpc.user.QueryDataBatch;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
Expand Down Expand Up @@ -90,4 +92,18 @@ public void testWithEmptyTable() throws Exception {
} catch (Exception e) { } // ignore
}
}
@Test
public void testCastEmptyStrings() throws Exception {
try {
test("alter system set `drill.exec.functions.cast_empty_string_to_null` = true;");
setColumnWidths(new int[] {5, 4});
List<QueryDataBatch> resultList = runHBaseSQLlWithResults("SELECT row_key,\n"
+ " CAST(t.f.c1 as INT) c1, CAST(t.f.c2 as BIGINT) c2, CAST(t.f.c3 as INT) c3,\n"
+ " CAST(t.f.c4 as INT) c4 FROM hbase.TestTableNullStr t where row_key='a1'");
printResult(resultList);
}
finally {
test("alter system reset `drill.exec.functions.cast_empty_string_to_null`;");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -610,4 +610,31 @@ public static void generateHBaseDatasetIntOBDesc(HBaseAdmin admin, String tableN

admin.flush(tableName);
}

public static void generateHBaseDatasetNullStr(HBaseAdmin admin, String tableName, int numberRegions) throws Exception {
if (admin.tableExists(tableName)) {
admin.disableTable(tableName);
admin.deleteTable(tableName);
}

HTableDescriptor desc = new HTableDescriptor(tableName);
desc.addFamily(new HColumnDescriptor("f"));
if (numberRegions > 1) {
admin.createTable(desc, Arrays.copyOfRange(SPLIT_KEYS, 0, numberRegions-1));
} else {
admin.createTable(desc);
}

HTable table = new HTable(admin.getConfiguration(), tableName);

Put p = new Put("a1".getBytes());
p.add("f".getBytes(), "c1".getBytes(), "".getBytes());
p.add("f".getBytes(), "c2".getBytes(), "".getBytes());
p.add("f".getBytes(), "c3".getBytes(), "5".getBytes());
p.add("f".getBytes(), "c4".getBytes(), "".getBytes());
table.put(p);

table.flushCommits();
table.close();
}
}
59 changes: 39 additions & 20 deletions exec/java-exec/src/main/codegen/data/Casts.tdd
Original file line number Diff line number Diff line change
Expand Up @@ -168,25 +168,44 @@
{from: "Decimal38Sparse", to: "Decimal18", major: "DownwardDecimalComplexDecimalSimple", javatype: "long"},
{from: "Decimal38Sparse", to: "Decimal9", major: "DownwardDecimalComplexDecimalSimple", javatype: "int"},

{from: "VarChar", to: "Int", major: "EmptyString", javaType:"Integer", primeType:"int"},
{from: "VarChar", to: "BigInt", major: "EmptyString", javaType: "Long", primeType: "long"},
{from: "VarChar", to: "Float4", major: "EmptyString", javaType:"Float", parse:"Float"},
{from: "VarChar", to: "Float8", major: "EmptyString", javaType:"Double", parse:"Double"},

{from: "VarChar", to: "Decimal9", major: "EmptyStringVarCharDecimalSimple", javatype: "int"},
{from: "VarChar", to: "Decimal18", major: "EmptyStringVarCharDecimalSimple", javatype: "long"},
{from: "VarChar", to: "Decimal28Sparse", major: "EmptyStringVarCharDecimalComplex", arraySize: "5"},
{from: "VarChar", to: "Decimal38Sparse", major: "EmptyStringVarCharDecimalComplex", arraySize: "6"},

{from: "NullableVarChar", to: "Int", major: "EmptyString", javaType:"Integer", primeType:"int"},
{from: "NullableVarChar", to: "BigInt", major: "EmptyString", javaType: "Long", primeType: "long"},
{from: "NullableVarChar", to: "Float4", major: "EmptyString", javaType:"Float", parse:"Float"},
{from: "NullableVarChar", to: "Float8", major: "EmptyString", javaType:"Double", parse:"Double"},

{from: "NullableVarChar", to: "Decimal9", major: "EmptyStringVarCharDecimalSimple", javatype: "int"},
{from: "NullableVarChar", to: "Decimal18", major: "EmptyStringVarCharDecimalSimple", javatype: "long"},
{from: "NullableVarChar", to: "Decimal28Sparse", major: "EmptyStringVarCharDecimalComplex", arraySize: "5"},
{from: "NullableVarChar", to: "Decimal38Sparse", major: "EmptyStringVarCharDecimalComplex", arraySize: "6"},

{from: "VarChar", to: "NullableInt", major: "EmptyString", javaType:"Integer", primeType:"int"},
{from: "VarChar", to: "NullableBigInt", major: "EmptyString", javaType: "Long", primeType: "long"},
{from: "VarChar", to: "NullableFloat4", major: "EmptyString", javaType:"Float", parse:"Float"},
{from: "VarChar", to: "NullableFloat8", major: "EmptyString", javaType:"Double", parse:"Double"},

{from: "VarChar", to: "NullableDecimal9", major: "EmptyStringVarCharDecimalSimple", javatype: "int"},
{from: "VarChar", to: "NullableDecimal18", major: "EmptyStringVarCharDecimalSimple", javatype: "long"},
{from: "VarChar", to: "NullableDecimal28Sparse", major: "EmptyStringVarCharDecimalComplex", arraySize: "5"},
{from: "VarChar", to: "NullableDecimal38Sparse", major: "EmptyStringVarCharDecimalComplex", arraySize: "6"},

{from: "NullableVarChar", to: "NullableInt", major: "EmptyString", javaType:"Integer", primeType:"int"},
{from: "NullableVarChar", to: "NullableBigInt", major: "EmptyString", javaType: "Long", primeType: "long"},
{from: "NullableVarChar", to: "NullableFloat4", major: "EmptyString", javaType:"Float", parse:"Float"},
{from: "NullableVarChar", to: "NullableFloat8", major: "EmptyString", javaType:"Double", parse:"Double"},

{from: "NullableVarChar", to: "NullableDecimal9", major: "EmptyStringVarCharDecimalSimple", javatype: "int"},
{from: "NullableVarChar", to: "NullableDecimal18", major: "EmptyStringVarCharDecimalSimple", javatype: "long"},
{from: "NullableVarChar", to: "NullableDecimal28Sparse", major: "EmptyStringVarCharDecimalComplex", arraySize: "5"},
{from: "NullableVarChar", to: "NullableDecimal38Sparse", major: "EmptyStringVarCharDecimalComplex", arraySize: "6"},

{from: "NullableVar16Char", to: "NullableInt", major: "EmptyString", javaType:"Integer", primeType:"int"},
{from: "NullableVar16Char", to: "NullableBigInt", major: "EmptyString", javaType: "Long", primeType: "long"},
{from: "NullableVar16Char", to: "NullableFloat4", major: "EmptyString", javaType:"Float", parse:"Float"},
{from: "NullableVar16Char", to: "NullableFloat8", major: "EmptyString", javaType:"Double", parse:"Double"},

{from: "NullableVar16Char", to: "NullableDecimal9", major: "EmptyStringVarCharDecimalSimple", javatype: "int"},
{from: "NullableVar16Char", to: "NullableDecimal18", major: "EmptyStringVarCharDecimalSimple", javatype: "long"},
{from: "NullableVar16Char", to: "NullableDecimal28Sparse", major: "EmptyStringVarCharDecimalComplex", arraySize: "5"},
{from: "NullableVar16Char", to: "NullableDecimal38Sparse", major: "EmptyStringVarCharDecimalComplex", arraySize: "6"},

{from: "NullableVarBinary", to: "NullableInt", major: "EmptyString", javaType:"Integer", primeType:"int"},
{from: "NullableVarBinary", to: "NullableBigInt", major: "EmptyString", javaType: "Long", primeType: "long"},
{from: "NullableVarBinary", to: "NullableFloat4", major: "EmptyString", javaType:"Float", parse:"Float"},
{from: "NullableVarBinary", to: "NullableFloat8", major: "EmptyString", javaType:"Double", parse:"Double"},

{from: "NullableVarBinary", to: "NullableDecimal9", major: "EmptyStringVarCharDecimalSimple", javatype: "int"},
{from: "NullableVarBinary", to: "NullableDecimal18", major: "EmptyStringVarCharDecimalSimple", javatype: "long"},
{from: "NullableVarBinary", to: "NullableDecimal28Sparse", major: "EmptyStringVarCharDecimalComplex", arraySize: "5"},
{from: "NullableVarBinary", to: "NullableDecimal38Sparse", major: "EmptyStringVarCharDecimalComplex", arraySize: "6"},
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* 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.
*/
<@pp.dropOutputFile />

<#macro doError>
{
byte[] buf = new byte[in.end - in.start];
in.buffer.getBytes(in.start, buf, 0, in.end - in.start);
throw new NumberFormatException(new String(buf, com.google.common.base.Charsets.UTF_8));
}
</#macro>

<#list cast.types as type>
<#if type.major == "EmptyString">

<@pp.changeOutputFile name="/org/apache/drill/exec/expr/fn/impl/gcast/CastEmptyString${type.from}To${type.to}.java" />
<#include "/@includes/license.ftl" />

package org.apache.drill.exec.expr.fn.impl.gcast;

import org.apache.drill.exec.expr.DrillSimpleFunc;
import org.apache.drill.exec.expr.annotations.FunctionTemplate;
import org.apache.drill.exec.expr.annotations.FunctionTemplate.NullHandling;
import org.apache.drill.exec.expr.annotations.Output;
import org.apache.drill.exec.expr.annotations.Param;
import org.apache.drill.exec.expr.holders.*;
import org.apache.drill.exec.record.RecordBatch;
import javax.inject.Inject;
import io.netty.buffer.DrillBuf;

/**
* This file is generated with Freemarker using the template exec/java-exec/src/main/codegen/templates/CastEmptyStringVarTypesToNullableNumeric.java
*/

@SuppressWarnings("unused")
@FunctionTemplate(name = "castEmptyString${type.from}To${type.to?upper_case}", scope = FunctionTemplate.FunctionScope.SIMPLE, nulls=NullHandling.INTERNAL)
public class CastEmptyString${type.from}To${type.to} implements DrillSimpleFunc{

@Param ${type.from}Holder in;
@Output ${type.to}Holder out;

public void setup() {}

public void eval() {
<#if type.to == "NullableFloat4" || type.to == "NullableFloat8">
if(<#if type.from == "NullableVarChar" || type.from == "NullableVar16Char" ||
type.from == "NullableVarBinary">in.isSet == 0 || </#if>in.end == in.start) {
out.isSet = 0;
} else{
out.isSet = 1;
byte[]buf=new byte[in.end-in.start];
in.buffer.getBytes(in.start,buf,0,in.end-in.start);
out.value=${type.javaType}.parse${type.parse}(new String(buf,com.google.common.base.Charsets.UTF_8));
}
<#elseif type.to=="NullableInt">
if(<#if type.from == "NullableVarChar" || type.from == "NullableVar16Char" ||
type.from == "NullableVarBinary">in.isSet == 0 || </#if>in.end == in.start) {
out.isSet = 0;
} else {
out.isSet = 1;
out.value = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.varTypesToInt(in.start, in.end, in.buffer);
}
<#elseif type.to == "NullableBigInt">
if(<#if type.from == "NullableVarChar" || type.from == "NullableVar16Char" ||
type.from == "NullableVarBinary">in.isSet == 0 || </#if>
in.end == in.start) {
out.isSet = 0;
} else {
out.isSet = 1;
out.value = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.varTypesToLong(in.start, in.end, in.buffer);
}
</#if>
}
}

</#if> <#-- type.major -->
</#list>
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ public void eval() {
out.value = ${type.javaType}.parse${type.parse}(new String(buf, com.google.common.base.Charsets.UTF_8));

<#elseif type.to=="Int" >
out.value = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.varCharToInt(in.start, in.end, in.buffer);
out.value = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.varTypesToInt(in.start, in.end, in.buffer);

<#elseif type.to == "BigInt">
out.value = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.varCharToLong(in.start, in.end, in.buffer);
out.value = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.varTypesToLong(in.start, in.end, in.buffer);
</#if>
}
}
Expand Down

This file was deleted.

Loading