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());
}
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;
}
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;
}
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);
}
}
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);
}
Aggregations