use of org.apache.asterix.runtime.exceptions.TypeMismatchException in project asterixdb by apache.
the class StringJoinDescriptor 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 {
return new IScalarEvaluator() {
private final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
private final ListAccessor listAccessor = new ListAccessor();
private final DataOutput out = resultStorage.getDataOutput();
private final IScalarEvaluatorFactory listEvalFactory = args[0];
private final IScalarEvaluatorFactory sepEvalFactory = args[1];
private final IPointable inputArgList = new VoidPointable();
private final IPointable inputArgSep = new VoidPointable();
private final IScalarEvaluator evalList = listEvalFactory.createScalarEvaluator(ctx);
private final IScalarEvaluator evalSep = sepEvalFactory.createScalarEvaluator(ctx);
@SuppressWarnings("unchecked")
private ISerializerDeserializer<ANull> nullSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ANULL);
private ISerializerDeserializer<AMissing> missingSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AMISSING);
private final byte[] tempLengthArray = new byte[5];
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
resultStorage.reset();
evalList.evaluate(tuple, inputArgList);
evalSep.evaluate(tuple, inputArgSep);
byte[] listBytes = inputArgList.getByteArray();
int listOffset = inputArgList.getStartOffset();
if (listBytes[listOffset] != ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG && listBytes[listOffset] != ATypeTag.SERIALIZED_UNORDEREDLIST_TYPE_TAG) {
throw new TypeMismatchException(getIdentifier(), 0, listBytes[listOffset], ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG, ATypeTag.SERIALIZED_UNORDEREDLIST_TYPE_TAG);
}
byte[] sepBytes = inputArgSep.getByteArray();
int sepOffset = inputArgSep.getStartOffset();
if (sepBytes[sepOffset] != ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
throw new TypeMismatchException(getIdentifier(), 1, sepBytes[sepOffset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
}
int sepLen = UTF8StringUtil.getUTFLength(sepBytes, sepOffset + 1);
int sepMetaLen = UTF8StringUtil.getNumBytesToStoreLength(sepLen);
listAccessor.reset(listBytes, listOffset);
try {
// calculate length first
int utf8Len = 0;
int size = listAccessor.size();
for (int i = 0; i < size; i++) {
int itemOffset = listAccessor.getItemOffset(i);
ATypeTag itemType = listAccessor.getItemType(itemOffset);
// since the item itself has a typetag.
if (listAccessor.itemsAreSelfDescribing()) {
itemOffset += 1;
}
if (itemType != ATypeTag.STRING) {
if (itemType == ATypeTag.NULL) {
nullSerde.serialize(ANull.NULL, out);
result.set(resultStorage);
return;
}
if (itemType == ATypeTag.MISSING) {
missingSerde.serialize(AMissing.MISSING, out);
result.set(resultStorage);
return;
}
throw new UnsupportedItemTypeException(getIdentifier(), itemType.serialize());
}
int currentSize = UTF8StringUtil.getUTFLength(listBytes, itemOffset);
if (i != size - 1 && currentSize != 0) {
utf8Len += sepLen;
}
utf8Len += currentSize;
}
out.writeByte(ATypeTag.SERIALIZED_STRING_TYPE_TAG);
int cbytes = UTF8StringUtil.encodeUTF8Length(utf8Len, tempLengthArray, 0);
out.write(tempLengthArray, 0, cbytes);
for (int i = 0; i < listAccessor.size(); i++) {
int itemOffset = listAccessor.getItemOffset(i);
if (listAccessor.itemsAreSelfDescribing()) {
itemOffset += 1;
}
utf8Len = UTF8StringUtil.getUTFLength(listBytes, itemOffset);
out.write(listBytes, UTF8StringUtil.getNumBytesToStoreLength(utf8Len) + itemOffset, utf8Len);
for (int j = 0; j < sepLen; j++) {
out.writeByte(sepBytes[sepOffset + 1 + sepMetaLen + j]);
}
}
} catch (IOException | AsterixException ex) {
throw new HyracksDataException(ex);
}
result.set(resultStorage);
}
};
}
};
}
use of org.apache.asterix.runtime.exceptions.TypeMismatchException in project asterixdb by apache.
the class Substring2Descriptor 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 {
return new IScalarEvaluator() {
private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
private DataOutput out = resultStorage.getDataOutput();
private IPointable argString = new VoidPointable();
private IPointable argStart = new VoidPointable();
private IScalarEvaluator evalString = args[0].createScalarEvaluator(ctx);
private IScalarEvaluator evalStart = args[1].createScalarEvaluator(ctx);
private final GrowableArray array = new GrowableArray();
private final UTF8StringBuilder builder = new UTF8StringBuilder();
private final UTF8StringPointable string = new UTF8StringPointable();
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
resultStorage.reset();
evalString.evaluate(tuple, argString);
evalStart.evaluate(tuple, argStart);
byte[] bytes = argStart.getByteArray();
int offset = argStart.getStartOffset();
int start = ATypeHierarchy.getIntegerValue(getIdentifier().getName(), 1, bytes, offset) - 1;
bytes = argString.getByteArray();
offset = argString.getStartOffset();
int len = argString.getLength();
if (bytes[offset] != ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
throw new TypeMismatchException(getIdentifier(), 0, bytes[offset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
}
string.set(bytes, offset + 1, len - 1);
array.reset();
try {
UTF8StringPointable.substr(string, start, Integer.MAX_VALUE, builder, array);
} catch (StringIndexOutOfBoundsException e) {
throw new RuntimeDataException(ErrorCode.OUT_OF_BOUND, getIdentifier(), 1, start);
} catch (IOException e) {
throw new HyracksDataException(e);
}
try {
out.writeByte(ATypeTag.SERIALIZED_STRING_TYPE_TAG);
out.write(array.getByteArray(), 0, array.getLength());
} catch (IOException e) {
throw new HyracksDataException(e);
}
result.set(resultStorage);
}
};
}
};
}
use of org.apache.asterix.runtime.exceptions.TypeMismatchException in project asterixdb by apache.
the class LenDescriptor 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 {
return new IScalarEvaluator() {
private final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
private final DataOutput out = resultStorage.getDataOutput();
private final IPointable inputVal = new VoidPointable();
private final IScalarEvaluator evalList = args[0].createScalarEvaluator(ctx);
// result
private final AMutableInt64 res = new AMutableInt64(0);
@SuppressWarnings("unchecked")
private final ISerializerDeserializer<AInt64> int64Serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT64);
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
resultStorage.reset();
evalList.evaluate(tuple, inputVal);
byte[] serList = inputVal.getByteArray();
int offset = inputVal.getStartOffset();
if (serList[offset] != ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG && serList[offset] != ATypeTag.SERIALIZED_UNORDEREDLIST_TYPE_TAG) {
throw new TypeMismatchException(getIdentifier(), 0, serList[offset], ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG, ATypeTag.SERIALIZED_UNORDEREDLIST_TYPE_TAG);
}
int numberOfitems = 0;
if (serList[offset] == ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG) {
numberOfitems = AOrderedListSerializerDeserializer.getNumberOfItems(serList, offset);
} else {
numberOfitems = AUnorderedListSerializerDeserializer.getNumberOfItems(serList, offset);
}
res.setValue(numberOfitems);
try {
int64Serde.serialize(res, out);
} catch (IOException e) {
throw new HyracksDataException(e);
}
result.set(resultStorage);
}
};
}
};
}
use of org.apache.asterix.runtime.exceptions.TypeMismatchException in project asterixdb by apache.
the class RecordPairsDescriptor method createEvaluatorFactory.
@Override
public IScalarEvaluatorFactory createEvaluatorFactory(final IScalarEvaluatorFactory[] args) {
return new IScalarEvaluatorFactory() {
private static final long serialVersionUID = 1L;
@Override
public IScalarEvaluator createScalarEvaluator(IHyracksTaskContext ctx) throws HyracksDataException {
// For writing each individual output record.
final ArrayBackedValueStorage itemStorage = new ArrayBackedValueStorage();
final DataOutput itemOutput = itemStorage.getDataOutput();
final RecordBuilder recBuilder = new RecordBuilder();
recBuilder.reset(RecordUtil.FULLY_OPEN_RECORD_TYPE);
// For writing the resulting list of records.
final OrderedListBuilder listBuilder = new OrderedListBuilder();
final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
final DataOutput resultOut = resultStorage.getDataOutput();
// Sets up the constant field names, "name" for the key field, "value" for the value field.
final ArrayBackedValueStorage nameStorage = new ArrayBackedValueStorage();
final ArrayBackedValueStorage valueStorage = new ArrayBackedValueStorage();
AObjectSerializerDeserializer serde = AObjectSerializerDeserializer.INSTANCE;
try {
serde.serialize(new AString("name"), nameStorage.getDataOutput());
serde.serialize(new AString("value"), valueStorage.getDataOutput());
} catch (IOException e) {
throw new HyracksDataException(e);
}
return new IScalarEvaluator() {
private final IScalarEvaluator argEvaluator = args[0].createScalarEvaluator(ctx);
private final IPointable argPtr = new VoidPointable();
private final ARecordVisitablePointable recordVisitablePointable = new ARecordVisitablePointable(recType);
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
// Resets the result storage.
resultStorage.reset();
// Gets the input record.
argEvaluator.evaluate(tuple, argPtr);
byte inputTypeTag = argPtr.getByteArray()[argPtr.getStartOffset()];
if (inputTypeTag != ATypeTag.SERIALIZED_RECORD_TYPE_TAG) {
throw new TypeMismatchException(getIdentifier(), 0, inputTypeTag, ATypeTag.SERIALIZED_RECORD_TYPE_TAG);
}
recordVisitablePointable.set(argPtr);
listBuilder.reset(AOrderedListType.FULL_OPEN_ORDEREDLIST_TYPE);
List<IVisitablePointable> fieldNames = recordVisitablePointable.getFieldNames();
List<IVisitablePointable> fieldValues = recordVisitablePointable.getFieldValues();
// Adds each field of the input record as a key-value pair into the result.
int numFields = recordVisitablePointable.getFieldNames().size();
for (int fieldIndex = 0; fieldIndex < numFields; ++fieldIndex) {
itemStorage.reset();
recBuilder.init();
recBuilder.addField(nameStorage, fieldNames.get(fieldIndex));
recBuilder.addField(valueStorage, fieldValues.get(fieldIndex));
recBuilder.write(itemOutput, true);
listBuilder.addItem(itemStorage);
}
// Writes the result and sets the result pointable.
listBuilder.write(resultOut, true);
result.set(resultStorage);
}
};
}
};
}
use of org.apache.asterix.runtime.exceptions.TypeMismatchException 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