use of org.apache.asterix.runtime.evaluators.comparisons.DeepEqualAssessor in project asterixdb by apache.
the class DeepEqualityDescriptor method createEvaluatorFactory.
@Override
public IScalarEvaluatorFactory createEvaluatorFactory(final IScalarEvaluatorFactory[] args) {
final IScalarEvaluatorFactory evalFactoryLeft = args[0];
final IScalarEvaluatorFactory evalFactoryRight = args[1];
return new IScalarEvaluatorFactory() {
private static final long serialVersionUID = 1L;
@SuppressWarnings("unchecked")
private final ISerializerDeserializer<ABoolean> boolSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ABOOLEAN);
@Override
public IScalarEvaluator createScalarEvaluator(IHyracksTaskContext ctx) throws HyracksDataException {
final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
final DataOutput out = resultStorage.getDataOutput();
final IScalarEvaluator evalLeft = evalFactoryLeft.createScalarEvaluator(ctx);
final IScalarEvaluator evalRight = evalFactoryRight.createScalarEvaluator(ctx);
return new IScalarEvaluator() {
private final DeepEqualAssessor deepEqualAssessor = new DeepEqualAssessor();
private final PointableAllocator allocator = new PointableAllocator();
private final IVisitablePointable pointableLeft = allocator.allocateFieldValue(inputTypeLeft);
private final IVisitablePointable pointableRight = allocator.allocateFieldValue(inputTypeRight);
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
try {
evalLeft.evaluate(tuple, pointableLeft);
evalRight.evaluate(tuple, pointableRight);
// Using deep equality assessment to assess the equality of the two values
boolean isEqual = deepEqualAssessor.isEqual(pointableLeft, pointableRight);
ABoolean resultBit = isEqual ? ABoolean.TRUE : ABoolean.FALSE;
resultStorage.reset();
boolSerde.serialize(resultBit, out);
result.set(resultStorage);
} catch (Exception ioe) {
throw new HyracksDataException(ioe);
}
}
};
}
};
}
use of org.apache.asterix.runtime.evaluators.comparisons.DeepEqualAssessor in project asterixdb by apache.
the class RecordMergeDescriptor method createEvaluatorFactory.
@Override
public IScalarEvaluatorFactory createEvaluatorFactory(final IScalarEvaluatorFactory[] args) {
return new IScalarEvaluatorFactory() {
private static final long serialVersionUID = 1L;
@Override
public IScalarEvaluator createScalarEvaluator(final IHyracksTaskContext ctx) throws HyracksDataException {
final PointableAllocator pa = new PointableAllocator();
final IVisitablePointable vp0 = pa.allocateRecordValue(inRecType0);
final IVisitablePointable vp1 = pa.allocateRecordValue(inRecType1);
final IPointable argPtr0 = new VoidPointable();
final IPointable argPtr1 = new VoidPointable();
final IScalarEvaluator eval0 = args[0].createScalarEvaluator(ctx);
final IScalarEvaluator eval1 = args[1].createScalarEvaluator(ctx);
final List<RecordBuilder> rbStack = new ArrayList<>();
final ArrayBackedValueStorage tabvs = new ArrayBackedValueStorage();
return new IScalarEvaluator() {
private final RuntimeRecordTypeInfo runtimeRecordTypeInfo = new RuntimeRecordTypeInfo();
private final DeepEqualAssessor deepEqualAssesor = new DeepEqualAssessor();
private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
private DataOutput out = resultStorage.getDataOutput();
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
resultStorage.reset();
eval0.evaluate(tuple, argPtr0);
eval1.evaluate(tuple, argPtr1);
vp0.set(argPtr0);
vp1.set(argPtr1);
ARecordVisitablePointable rp0 = (ARecordVisitablePointable) vp0;
ARecordVisitablePointable rp1 = (ARecordVisitablePointable) vp1;
try {
mergeFields(outRecType, rp0, rp1, true, 0);
rbStack.get(0).write(out, true);
} catch (IOException | AsterixException e) {
throw new HyracksDataException(e);
}
result.set(resultStorage);
}
private void mergeFields(ARecordType combinedType, ARecordVisitablePointable leftRecord, ARecordVisitablePointable rightRecord, boolean openFromParent, int nestedLevel) throws IOException, AsterixException, HyracksDataException {
if (rbStack.size() < (nestedLevel + 1)) {
rbStack.add(new RecordBuilder());
}
rbStack.get(nestedLevel).reset(combinedType);
rbStack.get(nestedLevel).init();
//Add all fields from left record
for (int i = 0; i < leftRecord.getFieldNames().size(); i++) {
IVisitablePointable leftName = leftRecord.getFieldNames().get(i);
IVisitablePointable leftValue = leftRecord.getFieldValues().get(i);
IVisitablePointable leftType = leftRecord.getFieldTypeTags().get(i);
boolean foundMatch = false;
for (int j = 0; j < rightRecord.getFieldNames().size(); j++) {
IVisitablePointable rightName = rightRecord.getFieldNames().get(j);
IVisitablePointable rightValue = rightRecord.getFieldValues().get(j);
IVisitablePointable rightType = rightRecord.getFieldTypeTags().get(j);
// Check if same fieldname
if (PointableHelper.isEqual(leftName, rightName) && !deepEqualAssesor.isEqual(leftValue, rightValue)) {
//Field was found on the right and are subrecords, merge them
if (PointableHelper.sameType(ATypeTag.OBJECT, rightType) && PointableHelper.sameType(ATypeTag.OBJECT, leftType)) {
//We are merging two sub records
addFieldToSubRecord(combinedType, leftName, leftValue, rightValue, openFromParent, nestedLevel);
foundMatch = true;
} else {
throw new RuntimeDataException(ErrorCode.DUPLICATE_FIELD_NAME, getIdentifier());
}
}
}
if (!foundMatch) {
addFieldToSubRecord(combinedType, leftName, leftValue, null, openFromParent, nestedLevel);
}
}
//Repeat for right side (ignoring duplicates this time)
for (int j = 0; j < rightRecord.getFieldNames().size(); j++) {
IVisitablePointable rightName = rightRecord.getFieldNames().get(j);
IVisitablePointable rightValue = rightRecord.getFieldValues().get(j);
boolean foundMatch = false;
for (int i = 0; i < leftRecord.getFieldNames().size(); i++) {
IVisitablePointable leftName = leftRecord.getFieldNames().get(i);
if (rightName.equals(leftName)) {
foundMatch = true;
}
}
if (!foundMatch) {
addFieldToSubRecord(combinedType, rightName, rightValue, null, openFromParent, nestedLevel);
}
}
}
/*
* Takes in a record type, field name, and the field values (which are record) from two records
* Merges them into one record of combinedType
* And adds that record as a field to the Record in subrb
* the second value can be null, indicated that you just add the value of left as a field to subrb
*
*/
private void addFieldToSubRecord(ARecordType combinedType, IVisitablePointable fieldNamePointable, IVisitablePointable leftValue, IVisitablePointable rightValue, boolean openFromParent, int nestedLevel) throws IOException, AsterixException, HyracksDataException {
runtimeRecordTypeInfo.reset(combinedType);
int pos = runtimeRecordTypeInfo.getFieldIndex(fieldNamePointable.getByteArray(), fieldNamePointable.getStartOffset() + 1, fieldNamePointable.getLength() - 1);
//Add the merged field
if (combinedType != null && pos >= 0) {
if (rightValue == null) {
rbStack.get(nestedLevel).addField(pos, leftValue);
} else {
mergeFields((ARecordType) combinedType.getFieldTypes()[pos], (ARecordVisitablePointable) leftValue, (ARecordVisitablePointable) rightValue, false, nestedLevel + 1);
tabvs.reset();
rbStack.get(nestedLevel + 1).write(tabvs.getDataOutput(), true);
rbStack.get(nestedLevel).addField(pos, tabvs);
}
} else {
if (rightValue == null) {
rbStack.get(nestedLevel).addField(fieldNamePointable, leftValue);
} else {
mergeFields(DefaultOpenFieldType.NESTED_OPEN_RECORD_TYPE, (ARecordVisitablePointable) leftValue, (ARecordVisitablePointable) rightValue, false, nestedLevel + 1);
tabvs.reset();
rbStack.get(nestedLevel + 1).write(tabvs.getDataOutput(), true);
rbStack.get(nestedLevel).addField(fieldNamePointable, tabvs);
}
}
}
};
}
};
}
Aggregations