use of org.apache.hyracks.api.dataflow.value.IMissingWriter in project asterixdb by apache.
the class StreamSelectRuntimeFactory method createOneOutputPushRuntime.
@Override
public AbstractOneInputOneOutputOneFramePushRuntime createOneOutputPushRuntime(final IHyracksTaskContext ctx) {
final IBinaryBooleanInspector bbi = binaryBooleanInspectorFactory.createBinaryBooleanInspector(ctx);
return new AbstractOneInputOneOutputOneFieldFramePushRuntime() {
private IPointable p = VoidPointable.FACTORY.createPointable();
private IScalarEvaluator eval;
private IMissingWriter missingWriter = null;
private ArrayTupleBuilder missingTupleBuilder = null;
private boolean isOpen = false;
@Override
public void open() throws HyracksDataException {
if (eval == null) {
initAccessAppendFieldRef(ctx);
eval = cond.createScalarEvaluator(ctx);
}
isOpen = true;
writer.open();
//prepare nullTupleBuilder
if (retainMissing && missingWriter == null) {
missingWriter = missingWriterFactory.createMissingWriter();
missingTupleBuilder = new ArrayTupleBuilder(1);
DataOutput out = missingTupleBuilder.getDataOutput();
missingWriter.writeMissing(out);
missingTupleBuilder.addFieldEndOffset();
}
}
@Override
public void fail() throws HyracksDataException {
if (isOpen) {
super.fail();
}
}
@Override
public void close() throws HyracksDataException {
if (isOpen) {
try {
flushIfNotFailed();
} finally {
writer.close();
}
}
}
@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);
eval.evaluate(tRef, p);
if (bbi.getBooleanValue(p.getByteArray(), p.getStartOffset(), p.getLength())) {
if (projectionList != null) {
appendProjectionToFrame(t, projectionList);
} else {
appendTupleToFrame(t);
}
} else {
if (retainMissing) {
for (int i = 0; i < tRef.getFieldCount(); i++) {
if (i == missingPlaceholderVariableIndex) {
appendField(missingTupleBuilder.getByteArray(), 0, missingTupleBuilder.getSize());
} else {
appendField(tAccess, t, i);
}
}
}
}
}
}
@Override
public void flush() throws HyracksDataException {
appender.flush(writer);
}
};
}
use of org.apache.hyracks.api.dataflow.value.IMissingWriter in project asterixdb by apache.
the class SubplanRuntimeFactory method createOneOutputPushRuntime.
@Override
public AbstractOneInputOneOutputPushRuntime createOneOutputPushRuntime(final IHyracksTaskContext ctx) throws HyracksDataException {
RecordDescriptor pipelineOutputRecordDescriptor = null;
final PipelineAssembler pa = new PipelineAssembler(pipeline, 1, 1, inputRecordDesc, pipelineOutputRecordDescriptor);
final IMissingWriter[] nullWriters = new IMissingWriter[missingWriterFactories.length];
for (int i = 0; i < missingWriterFactories.length; i++) {
nullWriters[i] = missingWriterFactories[i].createMissingWriter();
}
return new AbstractOneInputOneOutputOneFramePushRuntime() {
/**
* Computes the outer product between a given tuple and the frames
* passed.
*/
class TupleOuterProduct implements IFrameWriter {
private boolean smthWasWritten = false;
private FrameTupleAccessor ta = new FrameTupleAccessor(pipeline.getRecordDescriptors()[pipeline.getRecordDescriptors().length - 1]);
private ArrayTupleBuilder tb = new ArrayTupleBuilder(nullWriters.length);
@Override
public void open() throws HyracksDataException {
smthWasWritten = false;
}
@Override
public void nextFrame(ByteBuffer buffer) throws HyracksDataException {
ta.reset(buffer);
int nTuple = ta.getTupleCount();
for (int t = 0; t < nTuple; t++) {
appendConcat(tRef.getFrameTupleAccessor(), tRef.getTupleIndex(), ta, t);
}
smthWasWritten = true;
}
@Override
public void close() throws HyracksDataException {
if (!smthWasWritten) {
// the case when we need to write nulls
appendNullsToTuple();
appendToFrameFromTupleBuilder(tb);
}
}
@Override
public void fail() throws HyracksDataException {
writer.fail();
}
private void appendNullsToTuple() throws HyracksDataException {
tb.reset();
int n0 = tRef.getFieldCount();
for (int f = 0; f < n0; f++) {
tb.addField(tRef.getFrameTupleAccessor(), tRef.getTupleIndex(), f);
}
DataOutput dos = tb.getDataOutput();
for (int i = 0; i < nullWriters.length; i++) {
nullWriters[i].writeMissing(dos);
tb.addFieldEndOffset();
}
}
}
IFrameWriter endPipe = new TupleOuterProduct();
NestedTupleSourceRuntime startOfPipeline = (NestedTupleSourceRuntime) pa.assemblePipeline(endPipe, ctx);
boolean first = true;
@Override
public void open() throws HyracksDataException {
writer.open();
if (first) {
first = false;
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);
startOfPipeline.writeTuple(buffer, t);
try {
startOfPipeline.open();
} catch (Exception e) {
startOfPipeline.fail();
throw e;
} finally {
startOfPipeline.close();
}
}
}
@Override
public void flush() throws HyracksDataException {
writer.flush();
}
};
}
use of org.apache.hyracks.api.dataflow.value.IMissingWriter 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);
}
};
}
use of org.apache.hyracks.api.dataflow.value.IMissingWriter in project asterixdb by apache.
the class LookupAdapter method configurePropagation.
private void configurePropagation(IMissingWriterFactory iNullWriterFactory) {
if (propagateInput) {
// This LookupAdapter generates an external record as its output.
// Thus, we add 1.
tb = new ArrayTupleBuilder(tupleAccessor.getFieldCount() + 1);
frameTuple = new FrameTupleReference();
} else {
tb = new ArrayTupleBuilder(1);
}
if (retainNull) {
IMissingWriter missingWriter = iNullWriterFactory.createMissingWriter();
missingTupleBuild = new ArrayTupleBuilder(1);
DataOutput out = missingTupleBuild.getDataOutput();
try {
missingWriter.writeMissing(out);
} catch (IOException e) {
e.printStackTrace();
}
} else {
missingTupleBuild = null;
}
}
Aggregations