use of org.apache.asterix.om.pointables.AListVisitablePointable in project asterixdb by apache.
the class ListDeepEqualityChecker method accessList.
public boolean accessList(IVisitablePointable listPointableLeft, IVisitablePointable listPointableRight, DeepEqualityVisitor visitor) throws HyracksDataException {
this.visitor = visitor;
AListVisitablePointable listLeft = (AListVisitablePointable) listPointableLeft;
List<IVisitablePointable> itemsLeft = listLeft.getItems();
List<IVisitablePointable> itemTagTypesLeft = listLeft.getItemTags();
AListVisitablePointable listRight = (AListVisitablePointable) listPointableRight;
List<IVisitablePointable> itemsRight = listRight.getItems();
List<IVisitablePointable> itemTagTypesRight = listRight.getItemTags();
if (itemsLeft.size() != itemsRight.size())
return false;
boolean isOrderedRight = listLeft.ordered();
if (isOrderedRight != listRight.ordered())
return false;
if (isOrderedRight) {
return processOrderedList(itemsLeft, itemTagTypesLeft, itemsRight, itemTagTypesRight);
} else {
return processUnorderedList(itemsLeft, itemTagTypesLeft, itemsRight, itemTagTypesRight);
}
}
use of org.apache.asterix.om.pointables.AListVisitablePointable in project asterixdb by apache.
the class RecordRemoveFieldsEvalFactory method createScalarEvaluator.
@Override
public IScalarEvaluator createScalarEvaluator(final IHyracksTaskContext ctx) throws HyracksDataException {
final PointableAllocator pa = new PointableAllocator();
final IVisitablePointable vp0 = pa.allocateRecordValue(inputRecType);
final IVisitablePointable vp1 = pa.allocateListValue(inputListType);
final IPointable inputArg0 = new VoidPointable();
final IPointable inputArg1 = new VoidPointable();
final IScalarEvaluator eval0 = inputRecordEvalFactory.createScalarEvaluator(ctx);
final IScalarEvaluator eval1 = removeFieldPathsFactory.createScalarEvaluator(ctx);
return new IScalarEvaluator() {
private final RuntimeRecordTypeInfo runtimeRecordTypeInfo = new RuntimeRecordTypeInfo();
private final List<RecordBuilder> rbStack = new ArrayList<>();
private final ArrayBackedValueStorage tabvs = new ArrayBackedValueStorage();
private final Deque<IVisitablePointable> recordPath = new ArrayDeque<>();
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, inputArg0);
eval1.evaluate(tuple, inputArg1);
byte inputTypeTag0 = inputArg0.getByteArray()[inputArg0.getStartOffset()];
if (inputTypeTag0 != ATypeTag.SERIALIZED_RECORD_TYPE_TAG) {
throw new TypeMismatchException(BuiltinFunctions.REMOVE_FIELDS, 0, inputTypeTag0, ATypeTag.SERIALIZED_INT32_TYPE_TAG);
}
byte inputTypeTag1 = inputArg1.getByteArray()[inputArg1.getStartOffset()];
if (inputTypeTag1 != ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG) {
throw new TypeMismatchException(BuiltinFunctions.REMOVE_FIELDS, 1, inputTypeTag1, ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG);
}
vp0.set(inputArg0);
vp1.set(inputArg1);
ARecordVisitablePointable recordPointable = (ARecordVisitablePointable) vp0;
AListVisitablePointable listPointable = (AListVisitablePointable) vp1;
try {
recordPath.clear();
rbStack.clear();
processRecord(requiredRecType, recordPointable, listPointable, 0);
rbStack.get(0).write(out, true);
} catch (IOException | AsterixException e) {
throw new HyracksDataException(e);
}
result.set(resultStorage);
}
private void processRecord(ARecordType requiredType, ARecordVisitablePointable srp, AListVisitablePointable inputList, int nestedLevel) throws IOException, AsterixException, HyracksDataException {
if (rbStack.size() < (nestedLevel + 1)) {
rbStack.add(new RecordBuilder());
}
rbStack.get(nestedLevel).reset(requiredType);
rbStack.get(nestedLevel).init();
List<IVisitablePointable> fieldNames = srp.getFieldNames();
List<IVisitablePointable> fieldValues = srp.getFieldValues();
List<IVisitablePointable> fieldTypes = srp.getFieldTypeTags();
for (int i = 0; i < fieldNames.size(); i++) {
IVisitablePointable subRecFieldName = fieldNames.get(i);
recordPath.push(subRecFieldName);
if (isValidPath(inputList)) {
if (requiredType != null && requiredType.getTypeTag() != ATypeTag.ANY) {
addKeptFieldToSubRecord(requiredType, subRecFieldName, fieldValues.get(i), fieldTypes.get(i), inputList, nestedLevel);
} else {
addKeptFieldToSubRecord(DefaultOpenFieldType.NESTED_OPEN_RECORD_TYPE, subRecFieldName, fieldValues.get(i), fieldTypes.get(i), inputList, nestedLevel);
}
}
recordPath.pop();
}
}
private void addKeptFieldToSubRecord(ARecordType requiredType, IVisitablePointable fieldNamePointable, IVisitablePointable fieldValuePointable, IVisitablePointable fieldTypePointable, AListVisitablePointable inputList, int nestedLevel) throws IOException, AsterixException, HyracksDataException {
runtimeRecordTypeInfo.reset(requiredType);
int pos = runtimeRecordTypeInfo.getFieldIndex(fieldNamePointable.getByteArray(), fieldNamePointable.getStartOffset() + 1, fieldNamePointable.getLength() - 1);
if (pos >= 0) {
// Closed field
if (PointableHelper.sameType(ATypeTag.OBJECT, fieldTypePointable)) {
processRecord((ARecordType) requiredType.getFieldTypes()[pos], (ARecordVisitablePointable) fieldValuePointable, inputList, nestedLevel + 1);
tabvs.reset();
rbStack.get(nestedLevel + 1).write(tabvs.getDataOutput(), true);
rbStack.get(nestedLevel).addField(pos, tabvs);
} else {
rbStack.get(nestedLevel).addField(pos, fieldValuePointable);
}
} else {
// Open field
if (PointableHelper.sameType(ATypeTag.OBJECT, fieldTypePointable)) {
processRecord(null, (ARecordVisitablePointable) fieldValuePointable, inputList, nestedLevel + 1);
tabvs.reset();
rbStack.get(nestedLevel + 1).write(tabvs.getDataOutput(), true);
rbStack.get(nestedLevel).addField(fieldNamePointable, tabvs);
} else {
rbStack.get(nestedLevel).addField(fieldNamePointable, fieldValuePointable);
}
}
}
private boolean isValidPath(AListVisitablePointable inputList) throws HyracksDataException {
List<IVisitablePointable> items = inputList.getItems();
List<IVisitablePointable> typeTags = inputList.getItemTags();
int pathLen = recordPath.size();
for (int i = 0; i < items.size(); i++) {
IVisitablePointable item = items.get(i);
if (PointableHelper.sameType(ATypeTag.ARRAY, typeTags.get(i))) {
List<IVisitablePointable> inputPathItems = ((AListVisitablePointable) item).getItems();
if (pathLen == inputPathItems.size()) {
boolean match = true;
Iterator<IVisitablePointable> fpi = recordPath.iterator();
for (int j = inputPathItems.size() - 1; j >= 0; j--) {
match &= PointableHelper.isEqual(inputPathItems.get(j), fpi.next());
if (!match) {
break;
}
}
if (match) {
// Not a valid path for the output record
return false;
}
}
} else {
if (PointableHelper.isEqual(recordPath.getFirst(), item)) {
return false;
}
}
}
return true;
}
};
}
Aggregations