Search in sources :

Example 1 with VarCharHolder

use of org.apache.drill.exec.expr.holders.VarCharHolder in project drill by apache.

the class TestCastFunctions method testCastVarChar.

@Test
public //cast to varchar(length)
void testCastVarChar(@Injectable final DrillbitContext bitContext, @Injectable UserClientConnection connection) throws Throwable {
    mockDrillbitContext(bitContext);
    final PhysicalPlanReader reader = PhysicalPlanReaderTestFactory.defaultPhysicalPlanReader(CONFIG);
    final PhysicalPlan plan = reader.readPhysicalPlan(Files.toString(FileUtils.getResourceAsFile("/functions/cast/testCastVarChar.json"), Charsets.UTF_8));
    final FunctionImplementationRegistry registry = new FunctionImplementationRegistry(CONFIG);
    final FragmentContext context = new FragmentContext(bitContext, PlanFragment.getDefaultInstance(), connection, registry);
    final SimpleRootExec exec = new SimpleRootExec(ImplCreator.getExec(context, (FragmentRoot) plan.getSortedOperators(false).iterator().next()));
    while (exec.next()) {
        final VarCharVector c0 = exec.getValueVectorById(new SchemaPath("int_lit_cast", ExpressionPosition.UNKNOWN), VarCharVector.class);
        final VarCharVector.Accessor a0 = c0.getAccessor();
        int count = 0;
        for (int i = 0; i < c0.getAccessor().getValueCount(); i++) {
            final VarCharHolder holder0 = new VarCharHolder();
            a0.get(i, holder0);
            assertEquals("123", StringFunctionHelpers.toStringFromUTF8(holder0.start, holder0.end, holder0.buffer));
            ++count;
        }
        assertEquals(5, count);
    }
    exec.close();
    context.close();
    if (context.getFailureCause() != null) {
        throw context.getFailureCause();
    }
    assertTrue(!context.isFailed());
}
Also used : PhysicalPlan(org.apache.drill.exec.physical.PhysicalPlan) FragmentContext(org.apache.drill.exec.ops.FragmentContext) SchemaPath(org.apache.drill.common.expression.SchemaPath) PhysicalPlanReader(org.apache.drill.exec.planner.PhysicalPlanReader) VarCharVector(org.apache.drill.exec.vector.VarCharVector) FragmentRoot(org.apache.drill.exec.physical.base.FragmentRoot) VarCharHolder(org.apache.drill.exec.expr.holders.VarCharHolder) FunctionImplementationRegistry(org.apache.drill.exec.expr.fn.FunctionImplementationRegistry) Test(org.junit.Test)

Example 2 with VarCharHolder

use of org.apache.drill.exec.expr.holders.VarCharHolder in project drill by apache.

the class MappifyUtility method mappify.

public static DrillBuf mappify(FieldReader reader, BaseWriter.ComplexWriter writer, DrillBuf buffer) {
    // Currently we expect single map as input
    if (DataMode.REPEATED == reader.getType().getMode() || !(reader.getType().getMinorType() == TypeProtos.MinorType.MAP)) {
        throw new DrillRuntimeException("kvgen function only supports Simple maps as input");
    }
    BaseWriter.ListWriter listWriter = writer.rootAsList();
    listWriter.startList();
    BaseWriter.MapWriter mapWriter = listWriter.map();
    // Iterate over the fields in the map
    Iterator<String> fieldIterator = reader.iterator();
    while (fieldIterator.hasNext()) {
        String str = fieldIterator.next();
        FieldReader fieldReader = reader.reader(str);
        // Skip the field if its null
        if (fieldReader.isSet() == false) {
            mapWriter.end();
            continue;
        }
        // writing a new field, start a new map
        mapWriter.start();
        // write "key":"columnname" into the map
        VarCharHolder vh = new VarCharHolder();
        byte[] b = str.getBytes(Charsets.UTF_8);
        buffer = buffer.reallocIfNeeded(b.length);
        buffer.setBytes(0, b);
        vh.start = 0;
        vh.end = b.length;
        vh.buffer = buffer;
        mapWriter.varChar(fieldKey).write(vh);
        // Write the value to the map
        MapUtility.writeToMapFromReader(fieldReader, mapWriter);
        mapWriter.end();
    }
    listWriter.endList();
    return buffer;
}
Also used : BaseWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) VarCharHolder(org.apache.drill.exec.expr.holders.VarCharHolder) FieldReader(org.apache.drill.exec.vector.complex.reader.FieldReader)

Example 3 with VarCharHolder

use of org.apache.drill.exec.expr.holders.VarCharHolder in project drill by axbaretto.

the class MappifyUtility method mappify.

public static DrillBuf mappify(FieldReader reader, BaseWriter.ComplexWriter writer, DrillBuf buffer) {
    // Currently we expect single map as input
    if (DataMode.REPEATED == reader.getType().getMode() || !(reader.getType().getMinorType() == TypeProtos.MinorType.MAP)) {
        throw new DrillRuntimeException("kvgen function only supports Simple maps as input");
    }
    BaseWriter.ListWriter listWriter = writer.rootAsList();
    listWriter.startList();
    BaseWriter.MapWriter mapWriter = listWriter.map();
    // Iterate over the fields in the map
    Iterator<String> fieldIterator = reader.iterator();
    while (fieldIterator.hasNext()) {
        String str = fieldIterator.next();
        FieldReader fieldReader = reader.reader(str);
        // Skip the field if its null
        if (fieldReader.isSet() == false) {
            mapWriter.end();
            continue;
        }
        // writing a new field, start a new map
        mapWriter.start();
        // write "key":"columnname" into the map
        VarCharHolder vh = new VarCharHolder();
        byte[] b = str.getBytes(Charsets.UTF_8);
        buffer = buffer.reallocIfNeeded(b.length);
        buffer.setBytes(0, b);
        vh.start = 0;
        vh.end = b.length;
        vh.buffer = buffer;
        mapWriter.varChar(fieldKey).write(vh);
        // Write the value to the map
        MapUtility.writeToMapFromReader(fieldReader, mapWriter);
        mapWriter.end();
    }
    listWriter.endList();
    return buffer;
}
Also used : BaseWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) VarCharHolder(org.apache.drill.exec.expr.holders.VarCharHolder) FieldReader(org.apache.drill.exec.vector.complex.reader.FieldReader)

Example 4 with VarCharHolder

use of org.apache.drill.exec.expr.holders.VarCharHolder in project drill by axbaretto.

the class BsonRecordReader method writeString.

private void writeString(String readString, final MapOrListWriterImpl writer, String fieldName, boolean isList) {
    int length;
    byte[] strBytes;
    try {
        strBytes = readString.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new DrillRuntimeException("Unable to read string value for field: " + fieldName, e);
    }
    length = strBytes.length;
    ensure(length);
    workBuf.setBytes(0, strBytes);
    final VarCharHolder vh = new VarCharHolder();
    vh.buffer = workBuf;
    vh.start = 0;
    vh.end = length;
    if (isList == false) {
        writer.varChar(fieldName).write(vh);
    } else {
        writer.list.varChar().write(vh);
    }
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) VarCharHolder(org.apache.drill.exec.expr.holders.VarCharHolder)

Example 5 with VarCharHolder

use of org.apache.drill.exec.expr.holders.VarCharHolder in project drill by axbaretto.

the class TestByteComparisonFunctions method testEqual.

@Test
public void testEqual() {
    final VarCharHolder left = hello;
    final VarCharHolder right = hello;
    assertTrue(ByteFunctionHelpers.equal(left.buffer, left.start, left.end, right.buffer, right.start, right.end) == 1);
}
Also used : VarCharHolder(org.apache.drill.exec.expr.holders.VarCharHolder) Test(org.junit.Test) ExecTest(org.apache.drill.exec.ExecTest) UnlikelyTest(org.apache.drill.categories.UnlikelyTest) VectorTest(org.apache.drill.categories.VectorTest)

Aggregations

VarCharHolder (org.apache.drill.exec.expr.holders.VarCharHolder)36 Test (org.junit.Test)23 UnlikelyTest (org.apache.drill.categories.UnlikelyTest)20 VectorTest (org.apache.drill.categories.VectorTest)20 ExecTest (org.apache.drill.exec.ExecTest)20 DrillRuntimeException (org.apache.drill.common.exceptions.DrillRuntimeException)5 FunctionImplementationRegistry (org.apache.drill.exec.expr.fn.FunctionImplementationRegistry)4 FieldReader (org.apache.drill.exec.vector.complex.reader.FieldReader)4 BaseWriter (org.apache.drill.exec.vector.complex.writer.BaseWriter)4 SchemaPath (org.apache.drill.common.expression.SchemaPath)3 PhysicalPlan (org.apache.drill.exec.physical.PhysicalPlan)3 FragmentRoot (org.apache.drill.exec.physical.base.FragmentRoot)3 PhysicalPlanReader (org.apache.drill.exec.planner.PhysicalPlanReader)3 VarCharVector (org.apache.drill.exec.vector.VarCharVector)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 BitHolder (org.apache.drill.exec.expr.holders.BitHolder)2 VarDecimalHolder (org.apache.drill.exec.expr.holders.VarDecimalHolder)2 DrillBuf (io.netty.buffer.DrillBuf)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1