use of org.apache.drill.exec.vector.NullableVarCharVector in project drill by axbaretto.
the class TestHiveUDFs method testGenericUDF.
@Test
public void testGenericUDF() throws Throwable {
int numRecords = 0;
String planString = Resources.toString(Resources.getResource("functions/hive/GenericUDF.json"), Charsets.UTF_8);
List<QueryDataBatch> results = testPhysicalWithResults(planString);
RecordBatchLoader batchLoader = new RecordBatchLoader(getAllocator());
for (QueryDataBatch result : results) {
batchLoader.load(result.getHeader().getDef(), result.getData());
if (batchLoader.getRecordCount() <= 0) {
result.release();
batchLoader.clear();
continue;
}
// Output columns and types
// 1. str1 : VarChar
// 2. upperStr1 : NullableVarChar
// 3. concat : NullableVarChar
// 4. flt1 : Float4
// 5. format_number : NullableFloat8
// 6. nullableStr1 : NullableVarChar
// 7. upperNullableStr1 : NullableVarChar
VarCharVector str1V = (VarCharVector) batchLoader.getValueAccessorById(VarCharVector.class, 0).getValueVector();
NullableVarCharVector upperStr1V = (NullableVarCharVector) batchLoader.getValueAccessorById(NullableVarCharVector.class, 1).getValueVector();
NullableVarCharVector concatV = (NullableVarCharVector) batchLoader.getValueAccessorById(NullableVarCharVector.class, 2).getValueVector();
Float4Vector flt1V = (Float4Vector) batchLoader.getValueAccessorById(Float4Vector.class, 3).getValueVector();
NullableVarCharVector format_numberV = (NullableVarCharVector) batchLoader.getValueAccessorById(NullableVarCharVector.class, 4).getValueVector();
NullableVarCharVector nullableStr1V = (NullableVarCharVector) batchLoader.getValueAccessorById(NullableVarCharVector.class, 5).getValueVector();
NullableVarCharVector upperNullableStr1V = (NullableVarCharVector) batchLoader.getValueAccessorById(NullableVarCharVector.class, 6).getValueVector();
for (int i = 0; i < batchLoader.getRecordCount(); i++) {
String in = new String(str1V.getAccessor().get(i), Charsets.UTF_8);
String upper = new String(upperStr1V.getAccessor().get(i), Charsets.UTF_8);
assertTrue(in.toUpperCase().equals(upper));
String concat = new String(concatV.getAccessor().get(i), Charsets.UTF_8);
assertTrue(concat.equals(in + "-" + in));
float flt1 = flt1V.getAccessor().get(i);
String format_number = new String(format_numberV.getAccessor().get(i), Charsets.UTF_8);
String nullableStr1 = null;
if (!nullableStr1V.getAccessor().isNull(i)) {
nullableStr1 = new String(nullableStr1V.getAccessor().get(i), Charsets.UTF_8);
}
String upperNullableStr1 = null;
if (!upperNullableStr1V.getAccessor().isNull(i)) {
upperNullableStr1 = new String(upperNullableStr1V.getAccessor().get(i), Charsets.UTF_8);
}
assertEquals(nullableStr1 != null, upperNullableStr1 != null);
if (nullableStr1 != null) {
assertEquals(nullableStr1.toUpperCase(), upperNullableStr1);
}
System.out.println(in + ", " + upper + ", " + concat + ", " + flt1 + ", " + format_number + ", " + nullableStr1 + ", " + upperNullableStr1);
numRecords++;
}
result.release();
batchLoader.clear();
}
System.out.println("Processed " + numRecords + " records");
}
use of org.apache.drill.exec.vector.NullableVarCharVector in project drill by axbaretto.
the class BatchValidator method validateNullableVector.
private void validateNullableVector(String name, NullableVector vector) {
if (vector instanceof NullableVarCharVector) {
@SuppressWarnings("resource") VarCharVector values = ((NullableVarCharVector) vector).getValuesVector();
validateVarCharVector(name + "-values", values, rowCount);
}
}
use of org.apache.drill.exec.vector.NullableVarCharVector in project drill by axbaretto.
the class ParquetGroupScan method populatePruningVector.
public void populatePruningVector(ValueVector v, int index, SchemaPath column, String file) {
String f = Path.getPathWithoutSchemeAndAuthority(new Path(file)).toString();
MajorType majorType = getTypeForColumn(column);
MinorType type = majorType.getMinorType();
switch(type) {
case BIT:
{
NullableBitVector bitVector = (NullableBitVector) v;
Boolean value = (Boolean) partitionValueMap.get(f).get(column);
if (value == null) {
bitVector.getMutator().setNull(index);
} else {
bitVector.getMutator().setSafe(index, value ? 1 : 0);
}
return;
}
case INT:
{
NullableIntVector intVector = (NullableIntVector) v;
Integer value = (Integer) partitionValueMap.get(f).get(column);
if (value == null) {
intVector.getMutator().setNull(index);
} else {
intVector.getMutator().setSafe(index, value);
}
return;
}
case SMALLINT:
{
NullableSmallIntVector smallIntVector = (NullableSmallIntVector) v;
Integer value = (Integer) partitionValueMap.get(f).get(column);
if (value == null) {
smallIntVector.getMutator().setNull(index);
} else {
smallIntVector.getMutator().setSafe(index, value.shortValue());
}
return;
}
case TINYINT:
{
NullableTinyIntVector tinyIntVector = (NullableTinyIntVector) v;
Integer value = (Integer) partitionValueMap.get(f).get(column);
if (value == null) {
tinyIntVector.getMutator().setNull(index);
} else {
tinyIntVector.getMutator().setSafe(index, value.byteValue());
}
return;
}
case UINT1:
{
NullableUInt1Vector intVector = (NullableUInt1Vector) v;
Integer value = (Integer) partitionValueMap.get(f).get(column);
if (value == null) {
intVector.getMutator().setNull(index);
} else {
intVector.getMutator().setSafe(index, value.byteValue());
}
return;
}
case UINT2:
{
NullableUInt2Vector intVector = (NullableUInt2Vector) v;
Integer value = (Integer) partitionValueMap.get(f).get(column);
if (value == null) {
intVector.getMutator().setNull(index);
} else {
intVector.getMutator().setSafe(index, (char) value.shortValue());
}
return;
}
case UINT4:
{
NullableUInt4Vector intVector = (NullableUInt4Vector) v;
Integer value = (Integer) partitionValueMap.get(f).get(column);
if (value == null) {
intVector.getMutator().setNull(index);
} else {
intVector.getMutator().setSafe(index, value);
}
return;
}
case BIGINT:
{
NullableBigIntVector bigIntVector = (NullableBigIntVector) v;
Long value = (Long) partitionValueMap.get(f).get(column);
if (value == null) {
bigIntVector.getMutator().setNull(index);
} else {
bigIntVector.getMutator().setSafe(index, value);
}
return;
}
case FLOAT4:
{
NullableFloat4Vector float4Vector = (NullableFloat4Vector) v;
Float value = (Float) partitionValueMap.get(f).get(column);
if (value == null) {
float4Vector.getMutator().setNull(index);
} else {
float4Vector.getMutator().setSafe(index, value);
}
return;
}
case FLOAT8:
{
NullableFloat8Vector float8Vector = (NullableFloat8Vector) v;
Double value = (Double) partitionValueMap.get(f).get(column);
if (value == null) {
float8Vector.getMutator().setNull(index);
} else {
float8Vector.getMutator().setSafe(index, value);
}
return;
}
case VARBINARY:
{
NullableVarBinaryVector varBinaryVector = (NullableVarBinaryVector) v;
Object s = partitionValueMap.get(f).get(column);
byte[] bytes;
if (s == null) {
varBinaryVector.getMutator().setNull(index);
return;
} else {
bytes = getBytes(type, s);
}
varBinaryVector.getMutator().setSafe(index, bytes, 0, bytes.length);
return;
}
case DECIMAL18:
{
NullableDecimal18Vector decimalVector = (NullableDecimal18Vector) v;
Object s = partitionValueMap.get(f).get(column);
byte[] bytes;
if (s == null) {
decimalVector.getMutator().setNull(index);
return;
} else if (s instanceof Integer) {
long value = DecimalUtility.getBigDecimalFromPrimitiveTypes((Integer) s, majorType.getScale(), majorType.getPrecision()).longValue();
decimalVector.getMutator().setSafe(index, value);
return;
} else if (s instanceof Long) {
long value = DecimalUtility.getBigDecimalFromPrimitiveTypes((Long) s, majorType.getScale(), majorType.getPrecision()).longValue();
decimalVector.getMutator().setSafe(index, value);
return;
} else {
bytes = getBytes(type, s);
}
long value = DecimalUtility.getBigDecimalFromByteArray(bytes, 0, bytes.length, majorType.getScale()).longValue();
decimalVector.getMutator().setSafe(index, value);
return;
}
case DATE:
{
NullableDateVector dateVector = (NullableDateVector) v;
Integer value = (Integer) partitionValueMap.get(f).get(column);
if (value == null) {
dateVector.getMutator().setNull(index);
} else {
dateVector.getMutator().setSafe(index, value * (long) DateTimeConstants.MILLIS_PER_DAY);
}
return;
}
case TIME:
{
NullableTimeVector timeVector = (NullableTimeVector) v;
Integer value = (Integer) partitionValueMap.get(f).get(column);
if (value == null) {
timeVector.getMutator().setNull(index);
} else {
timeVector.getMutator().setSafe(index, value);
}
return;
}
case TIMESTAMP:
{
NullableTimeStampVector timeStampVector = (NullableTimeStampVector) v;
Long value = (Long) partitionValueMap.get(f).get(column);
if (value == null) {
timeStampVector.getMutator().setNull(index);
} else {
timeStampVector.getMutator().setSafe(index, value);
}
return;
}
case VARCHAR:
{
NullableVarCharVector varCharVector = (NullableVarCharVector) v;
Object s = partitionValueMap.get(f).get(column);
byte[] bytes;
if (s == null) {
varCharVector.getMutator().setNull(index);
return;
} else {
bytes = getBytes(type, s);
}
varCharVector.getMutator().setSafe(index, bytes, 0, bytes.length);
return;
}
case INTERVAL:
{
NullableIntervalVector intervalVector = (NullableIntervalVector) v;
Object s = partitionValueMap.get(f).get(column);
byte[] bytes;
if (s == null) {
intervalVector.getMutator().setNull(index);
return;
} else {
bytes = getBytes(type, s);
}
intervalVector.getMutator().setSafe(index, 1, ParquetReaderUtility.getIntFromLEBytes(bytes, 0), ParquetReaderUtility.getIntFromLEBytes(bytes, 4), ParquetReaderUtility.getIntFromLEBytes(bytes, 8));
return;
}
default:
throw new UnsupportedOperationException("Unsupported type: " + type);
}
}
use of org.apache.drill.exec.vector.NullableVarCharVector in project drill by axbaretto.
the class TestSimpleFunctions method testSubstringNegative.
@Test
public void testSubstringNegative() throws Throwable {
final DrillbitContext bitContext = mockDrillbitContext();
final UserClientConnection connection = Mockito.mock(UserClientConnection.class);
final PhysicalPlanReader reader = PhysicalPlanReaderTestFactory.defaultPhysicalPlanReader(c);
final PhysicalPlan plan = reader.readPhysicalPlan(Files.toString(DrillFileUtils.getResourceAsFile("/functions/testSubstringNegative.json"), Charsets.UTF_8));
final FunctionImplementationRegistry registry = new FunctionImplementationRegistry(c);
final FragmentContextImpl context = new FragmentContextImpl(bitContext, PlanFragment.getDefaultInstance(), connection, registry);
final SimpleRootExec exec = new SimpleRootExec(ImplCreator.getExec(context, (FragmentRoot) plan.getSortedOperators(false).iterator().next()));
while (exec.next()) {
final NullableVarCharVector c1 = exec.getValueVectorById(new SchemaPath("col3", ExpressionPosition.UNKNOWN), NullableVarCharVector.class);
final NullableVarCharVector.Accessor a1 = c1.getAccessor();
int count = 0;
for (int i = 0; i < c1.getAccessor().getValueCount(); i++) {
if (!a1.isNull(i)) {
final NullableVarCharHolder holder = new NullableVarCharHolder();
a1.get(i, holder);
// when offset is negative, substring return empty string.
assertEquals("", StringFunctionHelpers.toStringFromUTF8(holder.start, holder.end, holder.buffer));
++count;
}
}
assertEquals(50, count);
}
if (context.getExecutorState().getFailureCause() != null) {
throw context.getExecutorState().getFailureCause();
}
assertTrue(!context.getExecutorState().isFailed());
}
use of org.apache.drill.exec.vector.NullableVarCharVector in project drill by axbaretto.
the class TestValueVector method testVectors.
/**
* Convenience method that allows running tests on various {@link ValueVector vector} instances.
*
* @param test test function to execute
*/
@SuppressWarnings("resource")
private void testVectors(VectorVerifier test) throws Exception {
final MaterializedField[] fields = { MaterializedField.create(EMPTY_SCHEMA_PATH, UInt4Holder.TYPE), MaterializedField.create(EMPTY_SCHEMA_PATH, BitHolder.TYPE), MaterializedField.create(EMPTY_SCHEMA_PATH, VarCharHolder.TYPE), MaterializedField.create(EMPTY_SCHEMA_PATH, NullableVarCharHolder.TYPE), MaterializedField.create(EMPTY_SCHEMA_PATH, RepeatedListVector.TYPE), MaterializedField.create(EMPTY_SCHEMA_PATH, MapVector.TYPE), MaterializedField.create(EMPTY_SCHEMA_PATH, RepeatedMapVector.TYPE) };
final ValueVector[] vectors = { new UInt4Vector(fields[0], allocator), new BitVector(fields[1], allocator), new VarCharVector(fields[2], allocator), new NullableVarCharVector(fields[3], allocator), new RepeatedListVector(fields[4], allocator, null), new MapVector(fields[5], allocator, null), new RepeatedMapVector(fields[6], allocator, null) };
try {
for (final ValueVector vector : vectors) {
test.verify(vector);
}
} finally {
AutoCloseables.close(vectors);
}
}
Aggregations