@Override
protected IScalarEvaluator createEvaluator(IHyracksTaskContext ctx, IScalarEvaluator[] args)
throws AlgebricksException {
final AbstractValueComparisonOperation aOp = createValueComparisonOperation();
final ArrayBackedValueStorage abvsInner1 = new ArrayBackedValueStorage();
final DataOutput dOutInner1 = abvsInner1.getDataOutput();
final ArrayBackedValueStorage abvsInner2 = new ArrayBackedValueStorage();
final DataOutput dOutInner2 = abvsInner2.getDataOutput();
final FunctionHelper.TypedPointables tp1 = new FunctionHelper.TypedPointables();
final FunctionHelper.TypedPointables tp2 = new FunctionHelper.TypedPointables();
final DynamicContext dCtx = (DynamicContext) ctx.getJobletContext().getGlobalJobData();
final SequencePointable seqp1 = (SequencePointable) SequencePointable.FACTORY.createPointable();
final SequencePointable seqp2 = (SequencePointable) SequencePointable.FACTORY.createPointable();
final VoidPointable p1 = (VoidPointable) VoidPointable.FACTORY.createPointable();
final VoidPointable p2 = (VoidPointable) VoidPointable.FACTORY.createPointable();
final TaggedValuePointable tvpSeq1 = (TaggedValuePointable) TaggedValuePointable.FACTORY.createPointable();
final TaggedValuePointable tvpSeq2 = (TaggedValuePointable) TaggedValuePointable.FACTORY.createPointable();
return new AbstractTaggedValueArgumentScalarEvaluator(args) {
AbstractCastToOperation aCastToOp = new CastToStringOperation();
@Override
protected void evaluate(TaggedValuePointable[] args, IPointable result) throws SystemException {
boolean booleanResult = false;
TaggedValuePointable tvpArg1 = args[0];
TaggedValuePointable tvpArg2 = args[1];
try {
if (tvpArg1.getTag() == ValueTag.SEQUENCE_TAG) {
tvpArg1.getValue(seqp1);
int seqLen = seqp1.getEntryCount();
for (int j = 0; j < seqLen; ++j) {
seqp1.getEntry(j, p1);
tvpSeq1.set(p1.getByteArray(), p1.getStartOffset(), p1.getLength());
if (evaluateTaggedValueArgument2(aOp, tvpSeq1, tvpArg2, dCtx)) {
booleanResult = true;
break;
}
}
} else {
booleanResult = evaluateTaggedValueArgument2(aOp, tvpArg1, tvpArg2, dCtx);
}
byte[] byteResult = new byte[2];
byteResult[0] = ValueTag.XS_BOOLEAN_TAG;
byteResult[1] = (byte) (booleanResult ? 1 : 0);
result.set(byteResult, 0, 2);
} catch (SystemException se) {
throw se;
} catch (Exception e) {
throw new SystemException(ErrorCode.SYSE0001, e);
}
}
/**
* Check the second argument for a sequence and loop if required.
*
* @param aOp
* @param tvpArg1
* @param tvpArg2
* @param dCtx
* @return
* @throws SystemException
*/
protected boolean evaluateTaggedValueArgument2(AbstractValueComparisonOperation aOp,
TaggedValuePointable tvpArg1, TaggedValuePointable tvpArg2, DynamicContext dCtx)
throws SystemException {
try {
if (tvpArg2.getTag() == ValueTag.SEQUENCE_TAG) {
tvpArg2.getValue(seqp2);
int seqLen = seqp2.getEntryCount();
for (int j = 0; j < seqLen; ++j) {
seqp2.getEntry(j, p2);
tvpSeq2.set(p2.getByteArray(), p2.getStartOffset(), p2.getLength());
if (transformThenCompareTaggedValues(aOp, tvpArg1, tvpSeq2, dCtx)) {
return true;
}
}
} else {
return transformThenCompareTaggedValues(aOp, tvpArg1, tvpArg2, dCtx);
}
} catch (SystemException se) {
throw se;
} catch (Exception e) {
throw new SystemException(ErrorCode.SYSE0001, e);
}
return false;
}
/**
* Transform the values into values supported for general comparison.
*
* @param aOp
* @param tvpArg1
* @param tvpArg2
* @param dCtx
* @return
* @throws SystemException
*/
protected boolean transformThenCompareTaggedValues(AbstractValueComparisonOperation aOp,
TaggedValuePointable tvpArg1, TaggedValuePointable tvpArg2, DynamicContext dCtx)
throws SystemException {
boolean tagTransformed1 = false, tagTransformed2 = false;
int tid1 = FunctionHelper.getBaseTypeForGeneralComparisons(tvpArg1.getTag());
int tid2 = FunctionHelper.getBaseTypeForGeneralComparisons(tvpArg2.getTag());
abvsInner1.reset();
abvsInner2.reset();
// Converted tags
TaggedValuePointable tvp1 = (TaggedValuePointable) TaggedValuePointable.FACTORY.createPointable();
TaggedValuePointable tvp2 = (TaggedValuePointable) TaggedValuePointable.FACTORY.createPointable();
try {
// Set up value comparison tagged value pointables.
if (tid1 == ValueTag.XS_UNTYPED_ATOMIC_TAG && tid2 == ValueTag.XS_UNTYPED_ATOMIC_TAG) {
// Only need to change tag since the storage is the same for untyped atomic and string.
tvp1.getByteArray()[0] = ValueTag.XS_STRING_TAG;
tvp2.getByteArray()[0] = ValueTag.XS_STRING_TAG;
} else if (tid1 == ValueTag.XS_UNTYPED_ATOMIC_TAG) {
tid1 = tid2;
getCastToOperator(tid2);
tvpArg1.getValue(tp1.utf8sp);
aCastToOp.convertUntypedAtomic(tp1.utf8sp, dOutInner1);
tvp1.set(abvsInner1.getByteArray(), abvsInner1.getStartOffset(), abvsInner1.getLength());
tagTransformed1 = true;
} else if (tid2 == ValueTag.XS_UNTYPED_ATOMIC_TAG) {
tid2 = tid1;
getCastToOperator(tid1);
tvpArg2.getValue(tp2.utf8sp);
aCastToOp.convertUntypedAtomic(tp2.utf8sp, dOutInner2);
tvp2.set(abvsInner2.getByteArray(), abvsInner2.getStartOffset(), abvsInner2.getLength());
tagTransformed2 = true;
}
// Copy over the values not changed and upgrade numeric values to double.
if (!tagTransformed1) {
tvp1 = tvpArg1;
if (FunctionHelper.isDerivedFromDouble(tvp1.getTag())) {
FunctionHelper.getDoublePointable(tvpArg1, dOutInner1);
tvp1.set(abvsInner1.getByteArray(), abvsInner1.getStartOffset(),
DoublePointable.TYPE_TRAITS.getFixedLength() + 1);
tagTransformed1 = true;
}
}
if (!tagTransformed2) {
tvp2 = tvpArg2;
if (FunctionHelper.isDerivedFromDouble(tvp2.getTag())) {
FunctionHelper.getDoublePointable(tvpArg2, dOutInner2);
tvp2.set(abvsInner2.getByteArray(), abvsInner2.getStartOffset(),
DoublePointable.TYPE_TRAITS.getFixedLength() + 1);
tagTransformed2 = true;
}
}
} catch (SystemException se) {