Skip to content

Commit

Permalink
DRILL-6361: Revised typeOf() function versions
Browse files Browse the repository at this point in the history
Added more unit tests.

Updated to handle VARDECIMAL
The VARDECIMAL type was recently added to Drill. Added support for this type. The sqlTypeOf() function now returns DECIMAL(p, s) for precision p, scale s.

closes #1242
  • Loading branch information
Paul Rogers authored and vvysotskyi committed May 6, 2018
1 parent 6cbba28 commit e0727a2
Show file tree
Hide file tree
Showing 4 changed files with 357 additions and 18 deletions.
49 changes: 48 additions & 1 deletion common/src/main/java/org/apache/drill/common/types/Types.java
Expand Up @@ -28,9 +28,9 @@
import org.apache.drill.common.types.TypeProtos.DataMode;
import org.apache.drill.common.types.TypeProtos.MajorType;
import org.apache.drill.common.types.TypeProtos.MinorType;
import org.apache.drill.common.util.CoreDecimalUtility;

import com.google.protobuf.TextFormat;
import org.apache.drill.common.util.CoreDecimalUtility;

public class Types {
static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(Types.class);
Expand Down Expand Up @@ -107,6 +107,10 @@ public static String getSqlTypeName(final MajorType type) {
if (type.getMode() == DataMode.REPEATED || type.getMinorType() == MinorType.LIST) {
return "ARRAY";
}
return getBaseSqlTypeName(type);
}

public static String getBaseSqlTypeName(final MajorType type) {

switch (type.getMinorType()) {

Expand Down Expand Up @@ -175,6 +179,49 @@ public static String getSqlTypeName(final MajorType type) {
}
}

/**
* Extend decimal type with precision and scale.
*
* @param type major type
* @param typeName type converted to a string
* @return type name augmented with precision and scale,
* if type is a decimal
*/

public static String getExtendedSqlTypeName(MajorType type) {

String typeName = getSqlTypeName(type);
switch (type.getMinorType()) {
case DECIMAL9:
case DECIMAL18:
case DECIMAL28SPARSE:
case DECIMAL28DENSE:
case DECIMAL38SPARSE:
case DECIMAL38DENSE:
case VARDECIMAL:
// Disabled for now. See DRILL-6378
if (type.getPrecision() > 0) {
typeName += String.format("(%d, %d)",
type.getPrecision(), type.getScale());
}
default:
}
return typeName;
}

public static String getSqlModeName(final MajorType type) {
switch (type.getMode()) {
case REQUIRED:
return "NOT NULL";
case OPTIONAL:
return "NULLABLE";
case REPEATED:
return "ARRAY";
default:
return "UNKNOWN";
}
}

/***
* Gets JDBC type code for given SQL data type name.
*/
Expand Down
Expand Up @@ -17,32 +17,22 @@
*/
package org.apache.drill.exec.expr.fn.impl;

import com.google.common.collect.Sets;
import io.netty.buffer.DrillBuf;
import javax.inject.Inject;

import org.apache.drill.common.types.TypeProtos.MinorType;
import org.apache.drill.common.types.Types;
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.BigIntHolder;
import org.apache.drill.exec.expr.holders.BitHolder;
import org.apache.drill.exec.expr.holders.NullableIntHolder;
import org.apache.drill.exec.expr.holders.NullableTinyIntHolder;
import org.apache.drill.exec.expr.holders.NullableUInt1Holder;
import org.apache.drill.exec.expr.holders.UnionHolder;
import org.apache.drill.exec.expr.holders.IntHolder;
import org.apache.drill.exec.expr.holders.UnionHolder;
import org.apache.drill.exec.expr.holders.VarCharHolder;
import org.apache.drill.exec.resolver.TypeCastRules;
import org.apache.drill.exec.vector.complex.impl.UnionReader;
import org.apache.drill.exec.vector.complex.reader.FieldReader;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
import io.netty.buffer.DrillBuf;

/**
* The class contains additional functions for union types in addition to those in GUnionFunctions
Expand All @@ -65,8 +55,10 @@ public static class CompareType implements DrillSimpleFunc {
@Output
IntHolder out;

@Override
public void setup() {}

@Override
public void eval() {
org.apache.drill.common.types.TypeProtos.MinorType type1;
if (input1.isSet()) {
Expand Down Expand Up @@ -147,16 +139,104 @@ public static class GetType implements DrillSimpleFunc {
@Inject
DrillBuf buf;

@Override
public void setup() {}

@Override
public void eval() {

byte[] type;
String typeName;
if (input.isSet()) {
type = input.getType().getMinorType().name().getBytes();
typeName = input.getType().getMinorType().name();
} else {
type = org.apache.drill.common.types.TypeProtos.MinorType.NULL.name().getBytes();
typeName = org.apache.drill.common.types.TypeProtos.MinorType.NULL.name();
}
byte[] type = typeName.getBytes();
buf = buf.reallocIfNeeded(type.length);
buf.setBytes(0, type);
out.buffer = buf;
out.start = 0;
out.end = type.length;
}
}

@FunctionTemplate(name = "sqlTypeOf",
scope = FunctionTemplate.FunctionScope.SIMPLE,
nulls = NullHandling.INTERNAL)
public static class GetSqlType implements DrillSimpleFunc {

@Param
FieldReader input;
@Output
VarCharHolder out;
@Inject
DrillBuf buf;

@Override
public void setup() {}

@Override
public void eval() {

String typeName = org.apache.drill.common.types.Types.getExtendedSqlTypeName(input.getType());
byte[] type = typeName.getBytes();
buf = buf.reallocIfNeeded(type.length);
buf.setBytes(0, type);
out.buffer = buf;
out.start = 0;
out.end = type.length;
}
}

@FunctionTemplate(name = "drillTypeOf",
scope = FunctionTemplate.FunctionScope.SIMPLE,
nulls = NullHandling.INTERNAL)
public static class GetDrillType implements DrillSimpleFunc {

@Param
FieldReader input;
@Output
VarCharHolder out;
@Inject
DrillBuf buf;

@Override
public void setup() {}

@Override
public void eval() {

String typeName = input.getType().getMinorType().name();
byte[] type = typeName.getBytes();
buf = buf.reallocIfNeeded(type.length);
buf.setBytes(0, type);
out.buffer = buf;
out.start = 0;
out.end = type.length;
}
}

@FunctionTemplate(name = "modeOf",
scope = FunctionTemplate.FunctionScope.SIMPLE,
nulls = NullHandling.INTERNAL)
public static class GetMode implements DrillSimpleFunc {

@Param
FieldReader input;
@Output
VarCharHolder out;
@Inject
DrillBuf buf;

@Override
public void setup() {}

@Override
public void eval() {

String typeName = org.apache.drill.common.types.Types.getSqlModeName(
input.getType());
byte[] type = typeName.getBytes();
buf = buf.reallocIfNeeded(type.length);
buf.setBytes(0, type);
out.buffer = buf;
Expand All @@ -173,8 +253,10 @@ public static class CastUnionToUnion implements DrillSimpleFunc{
@Output
UnionHolder out;

@Override
public void setup() {}

@Override
public void eval() {
out.reader = in;
out.isSet = in.isSet() ? 1 : 0;
Expand All @@ -188,8 +270,10 @@ public static class CastUnionList implements DrillSimpleFunc {
@Param UnionHolder in;
@Output UnionHolder out;

@Override
public void setup() {}

@Override
public void eval() {
if (in.isSet == 1) {
if (in.reader.getType().getMinorType() != org.apache.drill.common.types.TypeProtos.MinorType.LIST) {
Expand All @@ -209,8 +293,10 @@ public static class UnionIsList implements DrillSimpleFunc {
@Param UnionHolder in;
@Output BitHolder out;

@Override
public void setup() {}

@Override
public void eval() {
if (in.isSet == 1) {
out.value = in.getType().getMinorType() == org.apache.drill.common.types.TypeProtos.MinorType.LIST ? 1 : 0;
Expand All @@ -227,8 +313,10 @@ public static class CastUnionMap implements DrillSimpleFunc {
@Param UnionHolder in;
@Output UnionHolder out;

@Override
public void setup() {}

@Override
public void eval() {
if (in.isSet == 1) {
if (in.reader.getType().getMinorType() != org.apache.drill.common.types.TypeProtos.MinorType.MAP) {
Expand All @@ -248,8 +336,10 @@ public static class UnionIsMap implements DrillSimpleFunc {
@Param UnionHolder in;
@Output BitHolder out;

@Override
public void setup() {}

@Override
public void eval() {
if (in.isSet == 1) {
out.value = in.getType().getMinorType() == org.apache.drill.common.types.TypeProtos.MinorType.MAP ? 1 : 0;
Expand All @@ -265,8 +355,10 @@ public static class IsNotNull implements DrillSimpleFunc {
@Param UnionHolder input;
@Output BitHolder out;

@Override
public void setup() { }

@Override
public void eval() {
out.value = input.isSet == 1 ? 1 : 0;
}
Expand All @@ -278,8 +370,10 @@ public static class IsNull implements DrillSimpleFunc {
@Param UnionHolder input;
@Output BitHolder out;

@Override
public void setup() { }

@Override
public void eval() {
out.value = input.isSet == 1 ? 0 : 1;
}
Expand Down

0 comments on commit e0727a2

Please sign in to comment.