use of org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory in project asterixdb by apache.
the class CalendarDurationFromDateTimeDescriptor 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 argPtr0 = new VoidPointable();
private IPointable argPtr1 = new VoidPointable();
private IScalarEvaluator eval0 = args[0].createScalarEvaluator(ctx);
private IScalarEvaluator eval1 = args[1].createScalarEvaluator(ctx);
@SuppressWarnings("unchecked")
private ISerializerDeserializer<ADuration> durationSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADURATION);
private AMutableDuration aDuration = new AMutableDuration(0, 0);
private GregorianCalendarSystem calInstanct = GregorianCalendarSystem.getInstance();
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
resultStorage.reset();
eval0.evaluate(tuple, argPtr0);
eval1.evaluate(tuple, argPtr1);
byte[] bytes0 = argPtr0.getByteArray();
int offset0 = argPtr0.getStartOffset();
byte[] bytes1 = argPtr1.getByteArray();
int offset1 = argPtr1.getStartOffset();
if (bytes0[offset0] != ATypeTag.SERIALIZED_DATETIME_TYPE_TAG) {
throw new TypeMismatchException(getIdentifier(), 0, bytes0[offset0], ATypeTag.SERIALIZED_DATETIME_TYPE_TAG);
}
if (bytes1[offset1] != ATypeTag.SERIALIZED_DURATION_TYPE_TAG) {
throw new TypeMismatchException(getIdentifier(), 1, bytes1[offset1], ATypeTag.SERIALIZED_DURATION_TYPE_TAG);
}
int yearMonthDurationInMonths = ADurationSerializerDeserializer.getYearMonth(bytes1, offset1 + 1);
long dayTimeDurationInMs = ADurationSerializerDeserializer.getDayTime(bytes1, offset1 + 1);
long startingTimePoint = ADateTimeSerializerDeserializer.getChronon(bytes0, offset0 + 1);
long endingTimePoint = DurationArithmeticOperations.addDuration(startingTimePoint, yearMonthDurationInMonths, dayTimeDurationInMs, false);
if (startingTimePoint == endingTimePoint) {
aDuration.setValue(0, 0);
} else {
boolean negative = false;
if (endingTimePoint < startingTimePoint) {
negative = true;
// swap the starting and ending time, so that ending time is always larger than the
// starting time.
long tmpTime = endingTimePoint;
endingTimePoint = startingTimePoint;
startingTimePoint = tmpTime;
}
int year0 = calInstanct.getYear(startingTimePoint);
int month0 = calInstanct.getMonthOfYear(startingTimePoint, year0);
int year1 = calInstanct.getYear(endingTimePoint);
int month1 = calInstanct.getMonthOfYear(endingTimePoint, year1);
int year = year1 - year0;
int month = month1 - month0;
int day = calInstanct.getDayOfMonthYear(endingTimePoint, year1, month1) - calInstanct.getDayOfMonthYear(startingTimePoint, year0, month0);
int hour = calInstanct.getHourOfDay(endingTimePoint) - calInstanct.getHourOfDay(startingTimePoint);
int min = calInstanct.getMinOfHour(endingTimePoint) - calInstanct.getMinOfHour(startingTimePoint);
int sec = calInstanct.getSecOfMin(endingTimePoint) - calInstanct.getSecOfMin(startingTimePoint);
int ms = calInstanct.getMillisOfSec(endingTimePoint) - calInstanct.getMillisOfSec(startingTimePoint);
if (ms < 0) {
ms += GregorianCalendarSystem.CHRONON_OF_SECOND;
sec -= 1;
}
if (sec < 0) {
sec += GregorianCalendarSystem.CHRONON_OF_MINUTE / GregorianCalendarSystem.CHRONON_OF_SECOND;
min -= 1;
}
if (min < 0) {
min += GregorianCalendarSystem.CHRONON_OF_HOUR / GregorianCalendarSystem.CHRONON_OF_MINUTE;
hour -= 1;
}
if (hour < 0) {
hour += GregorianCalendarSystem.CHRONON_OF_DAY / GregorianCalendarSystem.CHRONON_OF_HOUR;
day -= 1;
}
if (day < 0) {
boolean isLeapYear = calInstanct.isLeapYear(year1);
// need to "borrow" the days in previous month to make the day positive; when month is
// 1 (Jan), Dec will be borrowed
day += isLeapYear ? (GregorianCalendarSystem.DAYS_OF_MONTH_LEAP[(12 + month1 - 2) % 12]) : (GregorianCalendarSystem.DAYS_OF_MONTH_ORDI[(12 + month1 - 2) % 12]);
month -= 1;
}
if (month < 0) {
month += GregorianCalendarSystem.MONTHS_IN_A_YEAR;
year -= 1;
}
if (negative) {
aDuration.setValue(-1 * (year * GregorianCalendarSystem.MONTHS_IN_A_YEAR + month), -1 * (day * GregorianCalendarSystem.CHRONON_OF_DAY + hour * GregorianCalendarSystem.CHRONON_OF_HOUR + min * GregorianCalendarSystem.CHRONON_OF_MINUTE + sec * GregorianCalendarSystem.CHRONON_OF_SECOND + ms));
} else {
aDuration.setValue(year * GregorianCalendarSystem.MONTHS_IN_A_YEAR + month, day * GregorianCalendarSystem.CHRONON_OF_DAY + hour * GregorianCalendarSystem.CHRONON_OF_HOUR + min * GregorianCalendarSystem.CHRONON_OF_MINUTE + sec * GregorianCalendarSystem.CHRONON_OF_SECOND + ms);
}
}
durationSerde.serialize(aDuration, out);
result.set(resultStorage);
}
};
}
};
}
use of org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory in project asterixdb by apache.
the class StringSplitDescriptor 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() {
// Argument evaluators.
private final IScalarEvaluator stringEval = args[0].createScalarEvaluator(ctx);
private final IScalarEvaluator patternEval = args[1].createScalarEvaluator(ctx);
// Argument pointers.
private final IPointable argString = new VoidPointable();
private final IPointable argPattern = new VoidPointable();
private final UTF8StringPointable argStrPtr = new UTF8StringPointable();
private final UTF8StringPointable argPatternPtr = new UTF8StringPointable();
// For an output string item.
private final ArrayBackedValueStorage itemStorge = new ArrayBackedValueStorage();
private final DataOutput itemOut = itemStorge.getDataOutput();
private final byte[] tempLengthArray = new byte[5];
// For the output list of strings.
private final AOrderedListType intListType = new AOrderedListType(BuiltinType.ASTRING, null);
private final OrderedListBuilder listBuilder = new OrderedListBuilder();
private final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
private final DataOutput out = resultStorage.getDataOutput();
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
try {
resultStorage.reset();
// Calls argument evaluators.
stringEval.evaluate(tuple, argString);
patternEval.evaluate(tuple, argPattern);
// Gets the bytes of the source string.
byte[] srcString = argString.getByteArray();
int srcOffset = argString.getStartOffset();
int srcLen = argString.getLength();
// Type check for the first argument.
if (srcString[srcOffset] != ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
throw new TypeMismatchException(getIdentifier(), 0, srcString[srcOffset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
}
// Gets the bytes of the pattern string.
byte[] patternString = argPattern.getByteArray();
int patternOffset = argPattern.getStartOffset();
int patternLen = argPattern.getLength();
// Type check for the second argument.
if (patternString[patternOffset] != ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
throw new TypeMismatchException(getIdentifier(), 1, patternString[patternOffset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
}
// Sets the UTF8 String pointables.
argStrPtr.set(srcString, srcOffset + 1, srcLen - 1);
argPatternPtr.set(patternString, patternOffset + 1, patternLen - 1);
// Gets the string length of the source string.
int inputStringLen = UTF8StringUtil.getUTFLength(srcString, srcOffset + 1);
int inputStringStart = srcOffset + 1 + UTF8StringUtil.getNumBytesToStoreLength(inputStringLen);
// Gets the string length of the pattern string.
int inputPatternLen = UTF8StringUtil.getUTFLength(patternString, patternOffset + 1);
// Handles the case that the pattern is "".
boolean emptyStringPattern = inputPatternLen == 0;
// Builds a list of strings.
listBuilder.reset(intListType);
int itemStrStart = 0;
int nextMatchStart;
while (itemStrStart < inputStringLen && (nextMatchStart = UTF8StringPointable.find(argStrPtr, argPatternPtr, false, itemStrStart)) >= 0) {
// Adds an item string.
addItemString(srcString, inputStringStart, itemStrStart, emptyStringPattern ? nextMatchStart + 1 : nextMatchStart);
itemStrStart = nextMatchStart + (emptyStringPattern ? 1 : inputPatternLen);
}
if (!emptyStringPattern) {
addItemString(srcString, inputStringStart, itemStrStart, inputStringLen);
}
listBuilder.write(out, true);
result.set(resultStorage);
} catch (IOException e1) {
throw new HyracksDataException(e1);
}
}
private void addItemString(byte[] srcString, int inputStringStart, int itemStartOffset, int nextMatchStart) throws IOException {
int itemLen = nextMatchStart - itemStartOffset;
int cbytes = UTF8StringUtil.encodeUTF8Length(itemLen, tempLengthArray, 0);
itemStorge.reset();
itemOut.writeByte(ATypeTag.SERIALIZED_STRING_TYPE_TAG);
itemOut.write(tempLengthArray, 0, cbytes);
if (itemLen > 0) {
itemOut.write(srcString, inputStringStart + itemStartOffset, itemLen);
}
listBuilder.addItem(itemStorge);
}
};
}
};
}
use of org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory in project asterixdb by apache.
the class StringToCodePointDescriptor 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() {
protected final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
protected final DataOutput out = resultStorage.getDataOutput();
protected final IPointable argPtr = new VoidPointable();
protected final IScalarEvaluator stringEval = args[0].createScalarEvaluator(ctx);
protected final AOrderedListType intListType = new AOrderedListType(BuiltinType.AINT64, null);
private OrderedListBuilder listBuilder = new OrderedListBuilder();
private ArrayBackedValueStorage inputVal = new ArrayBackedValueStorage();
@SuppressWarnings("unchecked")
private final ISerializerDeserializer<AInt64> int64Serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT64);
private final AMutableInt64 aInt64 = new AMutableInt64(0);
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
try {
resultStorage.reset();
stringEval.evaluate(tuple, argPtr);
byte[] serString = argPtr.getByteArray();
int offset = argPtr.getStartOffset();
if (serString[offset] == ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
int len = UTF8StringUtil.getUTFLength(serString, offset + 1);
int start = offset + 1 + UTF8StringUtil.getNumBytesToStoreLength(len);
int pos = 0;
listBuilder.reset(intListType);
while (pos < len) {
int codePoint = UTF8StringUtil.UTF8ToCodePoint(serString, start + pos);
pos += UTF8StringUtil.charSize(serString, start + pos);
inputVal.reset();
aInt64.setValue(codePoint);
int64Serde.serialize(aInt64, inputVal.getDataOutput());
listBuilder.addItem(inputVal);
}
listBuilder.write(out, true);
} else {
throw new TypeMismatchException(getIdentifier(), 0, serString[offset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
}
result.set(resultStorage);
} catch (IOException e1) {
throw new HyracksDataException(e1);
}
}
};
}
};
}
use of org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory in project asterixdb by apache.
the class StringTrim2Descriptor 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 {
return new AbstractBinaryStringStringEval(ctx, args[0], args[1], StringTrim2Descriptor.this.getIdentifier()) {
private StringTrimmer stringTrimmer = new StringTrimmer(resultBuilder, resultArray);
@Override
protected void compute(UTF8StringPointable srcPtr, UTF8StringPointable patternPtr, UTF8StringPointable resultStrPtr) throws IOException {
stringTrimmer.build(patternPtr);
stringTrimmer.trim(srcPtr, resultStrPtr, true, true);
}
};
}
};
}
use of org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory in project asterixdb by apache.
the class RecordAddFieldsDescriptor 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 allocator = new PointableAllocator();
final IVisitablePointable vp0 = allocator.allocateRecordValue(inRecType);
final IVisitablePointable vp1 = allocator.allocateListValue(inListType);
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 ArrayBackedValueStorage fieldNamePointable = new ArrayBackedValueStorage();
final ArrayBackedValueStorage fieldValuePointer = new ArrayBackedValueStorage();
final PointableHelper pointableHelper = new PointableHelper();
try {
pointableHelper.serializeString("field-name", fieldNamePointable, true);
pointableHelper.serializeString("field-value", fieldValuePointer, true);
} catch (AsterixException e) {
throw new HyracksDataException(e);
}
return new IScalarEvaluator() {
// the default 32k frame size
public static final int TABLE_FRAME_SIZE = 32768;
// the default 32k frame size
public static final int TABLE_SIZE = 100;
private final RecordBuilder recordBuilder = new RecordBuilder();
private final RuntimeRecordTypeInfo requiredRecordTypeInfo = new RuntimeRecordTypeInfo();
private final IBinaryHashFunction putHashFunc = ListItemBinaryHashFunctionFactory.INSTANCE.createBinaryHashFunction();
private final IBinaryHashFunction getHashFunc = ListItemBinaryHashFunctionFactory.INSTANCE.createBinaryHashFunction();
private final BinaryEntry keyEntry = new BinaryEntry();
private final BinaryEntry valEntry = new BinaryEntry();
private final IVisitablePointable tempValReference = allocator.allocateEmpty();
private final IBinaryComparator cmp = ListItemBinaryComparatorFactory.INSTANCE.createBinaryComparator();
private BinaryHashMap hashMap = new BinaryHashMap(TABLE_SIZE, TABLE_FRAME_SIZE, putHashFunc, getHashFunc, cmp);
private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
private DataOutput out = resultStorage.getDataOutput();
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
resultStorage.reset();
recordBuilder.reset(outRecType);
requiredRecordTypeInfo.reset(outRecType);
eval0.evaluate(tuple, argPtr0);
eval1.evaluate(tuple, argPtr1);
// Make sure we get a valid record
byte typeTag0 = argPtr0.getByteArray()[argPtr0.getStartOffset()];
if (typeTag0 != ATypeTag.SERIALIZED_RECORD_TYPE_TAG) {
throw new TypeMismatchException(getIdentifier(), 0, typeTag0, ATypeTag.SERIALIZED_RECORD_TYPE_TAG);
}
// Make sure we get a valid list
byte typeTag1 = argPtr1.getByteArray()[argPtr1.getStartOffset()];
if (typeTag1 != ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG) {
throw new TypeMismatchException(getIdentifier(), 1, typeTag1, ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG);
}
vp0.set(argPtr0);
vp1.set(argPtr1);
ARecordVisitablePointable recordPointable = (ARecordVisitablePointable) vp0;
AListVisitablePointable listPointable = (AListVisitablePointable) vp1;
// Initialize our hashmap
int tableSize = recordPointable.getFieldNames().size() + listPointable.getItems().size();
// Thus avoiding unnecessary object construction
if (hashMap == null || tableSize > TABLE_SIZE) {
hashMap = new BinaryHashMap(tableSize, TABLE_FRAME_SIZE, putHashFunc, getHashFunc, cmp);
} else {
hashMap.clear();
}
addFields(recordPointable, listPointable);
recordBuilder.write(out, true);
result.set(resultStorage);
}
private void addFields(ARecordVisitablePointable inputRecordPointer, AListVisitablePointable listPointable) throws HyracksDataException {
List<IVisitablePointable> inputRecordFieldNames = inputRecordPointer.getFieldNames();
List<IVisitablePointable> inputRecordFieldValues = inputRecordPointer.getFieldValues();
List<IVisitablePointable> inputFields = listPointable.getItems();
IVisitablePointable namePointable = null;
IVisitablePointable valuePointable = null;
int numInputRecordFields = inputRecordFieldNames.size();
try {
// Add original record without duplicate checking
for (int i = 0; i < numInputRecordFields; ++i) {
IVisitablePointable fnp = inputRecordFieldNames.get(i);
IVisitablePointable fvp = inputRecordFieldValues.get(i);
int pos = requiredRecordTypeInfo.getFieldIndex(fnp.getByteArray(), fnp.getStartOffset() + 1, fnp.getLength() - 1);
if (pos >= 0) {
recordBuilder.addField(pos, fvp);
} else {
recordBuilder.addField(fnp, fvp);
}
keyEntry.set(fnp.getByteArray(), fnp.getStartOffset(), fnp.getLength());
valEntry.set(fvp.getByteArray(), fvp.getStartOffset(), fvp.getLength());
hashMap.put(keyEntry, valEntry);
}
// Get the fields from a list of records
for (int i = 0; i < inputFields.size(); i++) {
if (!PointableHelper.sameType(ATypeTag.OBJECT, inputFields.get(i))) {
throw new AsterixException("Expected list of record, got " + PointableHelper.getTypeTag(inputFields.get(i)));
}
List<IVisitablePointable> names = ((ARecordVisitablePointable) inputFields.get(i)).getFieldNames();
List<IVisitablePointable> values = ((ARecordVisitablePointable) inputFields.get(i)).getFieldValues();
// Get name and value of the field to be added
// Use loop to account for the cases where users switches the order of the fields
IVisitablePointable fieldName;
for (int j = 0; j < names.size(); j++) {
fieldName = names.get(j);
// if fieldName is "field-name" then read the name
if (PointableHelper.byteArrayEqual(fieldNamePointable, fieldName)) {
namePointable = values.get(j);
} else {
// otherwise the fieldName is "field-value". Thus, read the value
valuePointable = values.get(j);
}
}
if (namePointable == null || valuePointable == null) {
throw new InvalidDataFormatException(getIdentifier(), "fields to be added");
}
// Check that the field being added is a valid field
int pos = requiredRecordTypeInfo.getFieldIndex(namePointable.getByteArray(), namePointable.getStartOffset() + 1, namePointable.getLength() - 1);
keyEntry.set(namePointable.getByteArray(), namePointable.getStartOffset(), namePointable.getLength());
// Check if already in our built record
BinaryEntry entry = hashMap.get(keyEntry);
if (entry != null) {
tempValReference.set(entry.getBuf(), entry.getOffset(), entry.getLength());
// If value is not equal throw conflicting duplicate field, otherwise ignore
if (!PointableHelper.byteArrayEqual(valuePointable, tempValReference)) {
throw new RuntimeDataException(ErrorCode.DUPLICATE_FIELD_NAME, getIdentifier());
}
} else {
if (pos > -1) {
recordBuilder.addField(pos, valuePointable);
} else {
recordBuilder.addField(namePointable, valuePointable);
}
valEntry.set(valuePointable.getByteArray(), valuePointable.getStartOffset(), valuePointable.getLength());
hashMap.put(keyEntry, valEntry);
}
}
} catch (AsterixException e) {
throw new HyracksDataException(e);
}
}
};
}
};
}
Aggregations