use of org.apache.hyracks.algebricks.runtime.base.IUnnestingEvaluator in project asterixdb by apache.
the class RangeDescriptor method createUnnestingEvaluatorFactory.
@Override
public IUnnestingEvaluatorFactory createUnnestingEvaluatorFactory(final IScalarEvaluatorFactory[] args) {
return new IUnnestingEvaluatorFactory() {
private static final long serialVersionUID = 1L;
@Override
public IUnnestingEvaluator createUnnestingEvaluator(final IHyracksTaskContext ctx) throws HyracksDataException {
return new IUnnestingEvaluator() {
private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
@SuppressWarnings("rawtypes")
private ISerializerDeserializer serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT64);
private IPointable inputVal = new VoidPointable();
private IScalarEvaluator eval0 = args[0].createScalarEvaluator(ctx);
private IScalarEvaluator eval1 = args[1].createScalarEvaluator(ctx);
private AMutableInt64 aInt64 = new AMutableInt64(0);
private long current;
private long max;
@Override
public void init(IFrameTupleReference tuple) throws HyracksDataException {
eval0.evaluate(tuple, inputVal);
current = ATypeHierarchy.getLongValue(getIdentifier().getName(), 0, inputVal.getByteArray(), inputVal.getStartOffset());
eval1.evaluate(tuple, inputVal);
max = ATypeHierarchy.getLongValue(getIdentifier().getName(), 1, inputVal.getByteArray(), inputVal.getStartOffset());
}
@SuppressWarnings("unchecked")
@Override
public boolean step(IPointable result) throws HyracksDataException {
if (current > max) {
return false;
}
aInt64.setValue(current);
resultStorage.reset();
serde.serialize(aInt64, resultStorage.getDataOutput());
result.set(resultStorage);
current++;
return true;
}
};
}
};
}
use of org.apache.hyracks.algebricks.runtime.base.IUnnestingEvaluator in project asterixdb by apache.
the class SubsetCollectionDescriptor method createUnnestingEvaluatorFactory.
@Override
public IUnnestingEvaluatorFactory createUnnestingEvaluatorFactory(final IScalarEvaluatorFactory[] args) {
return new IUnnestingEvaluatorFactory() {
private static final long serialVersionUID = 1L;
@Override
public IUnnestingEvaluator createUnnestingEvaluator(IHyracksTaskContext ctx) throws HyracksDataException {
return new IUnnestingEvaluator() {
private IPointable inputVal = new VoidPointable();
private IScalarEvaluator evalList = args[0].createScalarEvaluator(ctx);
private IScalarEvaluator evalStart = args[1].createScalarEvaluator(ctx);
private IScalarEvaluator evalLen = args[2].createScalarEvaluator(ctx);
private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
private int numItems;
private int numItemsMax;
private int posStart;
private int posCrt;
private ATypeTag itemTag;
private boolean selfDescList = false;
private boolean metUnknown = false;
@Override
public void init(IFrameTupleReference tuple) throws HyracksDataException {
try {
evalStart.evaluate(tuple, inputVal);
posStart = ATypeHierarchy.getIntegerValue(getIdentifier().getName(), 0, inputVal.getByteArray(), inputVal.getStartOffset());
evalLen.evaluate(tuple, inputVal);
numItems = ATypeHierarchy.getIntegerValue(getIdentifier().getName(), 1, inputVal.getByteArray(), inputVal.getStartOffset());
evalList.evaluate(tuple, inputVal);
byte[] serList = inputVal.getByteArray();
int offset = inputVal.getStartOffset();
metUnknown = false;
byte typeTag = serList[offset];
if (typeTag == ATypeTag.SERIALIZED_MISSING_TYPE_TAG || typeTag == ATypeTag.SERIALIZED_NULL_TYPE_TAG) {
metUnknown = true;
return;
}
if (typeTag != ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG && typeTag != ATypeTag.SERIALIZED_UNORDEREDLIST_TYPE_TAG) {
throw new RuntimeDataException(ErrorCode.COERCION, getIdentifier());
}
if (typeTag == ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG) {
numItemsMax = AOrderedListSerializerDeserializer.getNumberOfItems(serList, offset);
} else {
numItemsMax = AUnorderedListSerializerDeserializer.getNumberOfItems(serList, offset);
}
itemTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(serList[offset + 1]);
if (itemTag == ATypeTag.ANY) {
selfDescList = true;
}
posCrt = posStart;
} catch (IOException e) {
throw new HyracksDataException(e);
}
}
@Override
public boolean step(IPointable result) throws HyracksDataException {
if (!metUnknown && posCrt < posStart + numItems && posCrt < numItemsMax) {
resultStorage.reset();
byte[] serList = inputVal.getByteArray();
int offset = inputVal.getStartOffset();
int itemLength = 0;
try {
int itemOffset = AOrderedListSerializerDeserializer.getItemOffset(serList, offset, posCrt);
if (selfDescList) {
itemTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(serList[itemOffset]);
}
itemLength = NonTaggedFormatUtil.getFieldValueLength(serList, itemOffset, itemTag, selfDescList);
if (!selfDescList) {
resultStorage.getDataOutput().writeByte(itemTag.serialize());
}
resultStorage.getDataOutput().write(serList, itemOffset, itemLength + (!selfDescList ? 0 : 1));
} catch (IOException e) {
throw new HyracksDataException(e);
} catch (AsterixException e) {
throw new HyracksDataException(e);
}
result.set(resultStorage);
++posCrt;
return true;
}
return false;
}
};
}
};
}
use of org.apache.hyracks.algebricks.runtime.base.IUnnestingEvaluator in project asterixdb by apache.
the class IntArrayUnnester method createUnnestingEvaluator.
@Override
public IUnnestingEvaluator createUnnestingEvaluator(IHyracksTaskContext ctx) throws HyracksDataException {
final ArrayBackedValueStorage abvs = new ArrayBackedValueStorage();
return new IUnnestingEvaluator() {
private int pos;
@Override
public void init(IFrameTupleReference tuple) throws HyracksDataException {
pos = 0;
}
@Override
public boolean step(IPointable result) throws HyracksDataException {
try {
if (pos < x.length) {
// Writes one byte to distinguish between null
// values and end of sequence.
abvs.reset();
abvs.getDataOutput().writeInt(x[pos]);
result.set(abvs);
++pos;
return true;
} else {
return false;
}
} catch (IOException e) {
throw new HyracksDataException(e);
}
}
};
}
use of org.apache.hyracks.algebricks.runtime.base.IUnnestingEvaluator in project asterixdb by apache.
the class UnnestRuntimeFactory method createOneOutputPushRuntime.
@Override
public AbstractOneInputOneOutputOneFramePushRuntime createOneOutputPushRuntime(final IHyracksTaskContext ctx) throws HyracksDataException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutput output = new DataOutputStream(bos);
if (missingWriterFactory != null) {
IMissingWriter missingWriter = missingWriterFactory.createMissingWriter();
missingWriter.writeMissing(output);
}
byte[] missingBytes = bos.toByteArray();
int missingBytesLen = bos.size();
return new AbstractOneInputOneOutputOneFramePushRuntime() {
private IPointable p = VoidPointable.FACTORY.createPointable();
private ArrayTupleBuilder tupleBuilder = new ArrayTupleBuilder(projectionList.length);
private IUnnestingEvaluator unnest = unnestingFactory.createUnnestingEvaluator(ctx);
@Override
public void open() throws HyracksDataException {
writer.open();
if (tRef == null) {
initAccessAppendRef(ctx);
}
}
@Override
public void nextFrame(ByteBuffer buffer) throws HyracksDataException {
tAccess.reset(buffer);
int nTuple = tAccess.getTupleCount();
for (int t = 0; t < nTuple; t++) {
tRef.reset(tAccess, t);
try {
unnest.init(tRef);
unnesting(t);
} catch (IOException ae) {
throw new HyracksDataException(ae);
}
}
}
private void unnesting(int t) throws IOException {
// Assumes that when unnesting the tuple, each step() call for each element
// in the tuple will increase the positionIndex, and the positionIndex will
// be reset when a new tuple is to be processed.
int positionIndex = 1;
boolean emitted = false;
do {
if (!unnest.step(p)) {
break;
}
writeOutput(t, positionIndex++, false);
emitted = true;
} while (true);
if (leftOuter && !emitted) {
writeOutput(t, -1, true);
}
}
private void writeOutput(int t, int positionIndex, boolean missing) throws HyracksDataException, IOException {
if (!unnestColIsProjected && positionWriter == null) {
appendProjectionToFrame(t, projectionList);
appendToFrameFromTupleBuilder(tupleBuilder);
return;
}
tupleBuilder.reset();
for (int f = 0; f < outColPos; f++) {
tupleBuilder.addField(tAccess, t, f);
}
if (unnestColIsProjected) {
if (missing) {
tupleBuilder.addField(missingBytes, 0, missingBytesLen);
} else {
tupleBuilder.addField(p.getByteArray(), p.getStartOffset(), p.getLength());
}
}
for (int f = unnestColIsProjected ? outColPos + 1 : outColPos; f < (positionWriter != null ? projectionList.length - 1 : projectionList.length); f++) {
tupleBuilder.addField(tAccess, t, f);
}
if (positionWriter != null) {
// Write the positional variable
if (missing) {
tupleBuilder.addField(missingBytes, 0, missingBytesLen);
} else {
positionWriter.write(tupleBuilder.getDataOutput(), positionIndex);
tupleBuilder.addFieldEndOffset();
}
}
appendToFrameFromTupleBuilder(tupleBuilder);
}
@Override
public void flush() throws HyracksDataException {
appender.flush(writer);
}
};
}
Aggregations