use of org.apache.asterix.om.types.AOrderedListType in project asterixdb by apache.
the class TweetParserTest method closedRecordTypeTest.
@Test
public void closedRecordTypeTest() throws IOException, URISyntaxException {
// contruct type
IAType geoFieldType = new ARecordType("GeoType", new String[] { "coordinates" }, new IAType[] { new AOrderedListType(AFLOAT, "point") }, true);
ARecordType tweetRecordType = new ARecordType("TweetType", new String[] { "id", "geo" }, new IAType[] { AINT64, geoFieldType }, true);
TweetParser parser = new TweetParser(tweetRecordType);
List<String> lines = Files.readAllLines(Paths.get(getClass().getResource("/test_tweets.txt").toURI()));
ByteArrayOutputStream is = new ByteArrayOutputStream();
DataOutput output = new DataOutputStream(is);
int regularCount = 0;
for (int iter1 = 0; iter1 < lines.size(); iter1++) {
GenericRecord<String> record = new GenericRecord<>();
record.set(lines.get(iter1));
try {
parser.parse(record, output);
regularCount++;
} catch (HyracksDataException e) {
Assert.assertTrue(e.toString().contains("Non-null") && (iter1 == 0 || iter1 == 1));
}
}
Assert.assertTrue(regularCount == 4);
}
use of org.apache.asterix.om.types.AOrderedListType in project asterixdb by apache.
the class LineRectanglePolygonAccessor 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 IScalarEvaluator() {
private final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
private final DataOutput out = resultStorage.getDataOutput();
private final IPointable argPtr = new VoidPointable();
private final IScalarEvaluator eval = args[0].createScalarEvaluator(ctx);
private final OrderedListBuilder listBuilder = new OrderedListBuilder();
private final ArrayBackedValueStorage inputVal = new ArrayBackedValueStorage();
private final AOrderedListType pointListType = new AOrderedListType(BuiltinType.APOINT, null);
private final AMutablePoint aPoint = new AMutablePoint(0, 0);
@SuppressWarnings("unchecked")
private final ISerializerDeserializer<APoint> pointSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.APOINT);
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
eval.evaluate(tuple, argPtr);
byte[] bytes = argPtr.getByteArray();
int startOffset = argPtr.getStartOffset();
resultStorage.reset();
try {
if (bytes[startOffset] == ATypeTag.SERIALIZED_LINE_TYPE_TAG) {
listBuilder.reset(pointListType);
inputVal.reset();
double startX = ADoubleSerializerDeserializer.getDouble(bytes, startOffset + ALineSerializerDeserializer.getStartPointCoordinateOffset(Coordinate.X));
double startY = ADoubleSerializerDeserializer.getDouble(bytes, startOffset + ALineSerializerDeserializer.getStartPointCoordinateOffset(Coordinate.Y));
aPoint.setValue(startX, startY);
pointSerde.serialize(aPoint, inputVal.getDataOutput());
listBuilder.addItem(inputVal);
inputVal.reset();
double endX = ADoubleSerializerDeserializer.getDouble(bytes, startOffset + ALineSerializerDeserializer.getEndPointCoordinateOffset(Coordinate.X));
double endY = ADoubleSerializerDeserializer.getDouble(bytes, startOffset + ALineSerializerDeserializer.getEndPointCoordinateOffset(Coordinate.Y));
aPoint.setValue(endX, endY);
pointSerde.serialize(aPoint, inputVal.getDataOutput());
listBuilder.addItem(inputVal);
listBuilder.write(out, true);
} else if (bytes[startOffset] == ATypeTag.SERIALIZED_RECTANGLE_TYPE_TAG) {
listBuilder.reset(pointListType);
inputVal.reset();
double x1 = ADoubleSerializerDeserializer.getDouble(bytes, startOffset + ARectangleSerializerDeserializer.getBottomLeftCoordinateOffset(Coordinate.X));
double y1 = ADoubleSerializerDeserializer.getDouble(bytes, startOffset + ARectangleSerializerDeserializer.getBottomLeftCoordinateOffset(Coordinate.Y));
aPoint.setValue(x1, y1);
pointSerde.serialize(aPoint, inputVal.getDataOutput());
listBuilder.addItem(inputVal);
inputVal.reset();
double x2 = ADoubleSerializerDeserializer.getDouble(bytes, startOffset + ARectangleSerializerDeserializer.getUpperRightCoordinateOffset(Coordinate.X));
double y2 = ADoubleSerializerDeserializer.getDouble(bytes, startOffset + ARectangleSerializerDeserializer.getUpperRightCoordinateOffset(Coordinate.Y));
aPoint.setValue(x2, y2);
pointSerde.serialize(aPoint, inputVal.getDataOutput());
listBuilder.addItem(inputVal);
listBuilder.write(out, true);
} else if (bytes[startOffset] == ATypeTag.SERIALIZED_POLYGON_TYPE_TAG) {
int numOfPoints = AInt16SerializerDeserializer.getShort(bytes, startOffset + APolygonSerializerDeserializer.getNumberOfPointsOffset());
if (numOfPoints < 3) {
throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_POLYGON_TYPE_TAG);
}
listBuilder.reset(pointListType);
for (int i = 0; i < numOfPoints; ++i) {
inputVal.reset();
double x = ADoubleSerializerDeserializer.getDouble(bytes, startOffset + APolygonSerializerDeserializer.getCoordinateOffset(i, Coordinate.X));
double y = ADoubleSerializerDeserializer.getDouble(bytes, startOffset + APolygonSerializerDeserializer.getCoordinateOffset(i, Coordinate.Y));
aPoint.setValue(x, y);
pointSerde.serialize(aPoint, inputVal.getDataOutput());
listBuilder.addItem(inputVal);
}
listBuilder.write(out, true);
} else {
throw new TypeMismatchException(getIdentifier(), 0, bytes[startOffset], ATypeTag.SERIALIZED_LINE_TYPE_TAG, ATypeTag.SERIALIZED_RECTANGLE_TYPE_TAG, ATypeTag.SERIALIZED_POLYGON_TYPE_TAG);
}
} catch (IOException e) {
throw new HyracksDataException(e);
}
result.set(resultStorage);
}
};
}
};
}
use of org.apache.asterix.om.types.AOrderedListType in project asterixdb by apache.
the class DisjunctivePredicateToJoinRule method rewritePost.
@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
MetadataProvider metadataProvider = (MetadataProvider) context.getMetadataProvider();
if (metadataProvider.isBlockingOperatorDisabled()) {
return false;
}
SelectOperator select;
if ((select = asSelectOperator(opRef)) == null) {
return false;
}
AbstractFunctionCallExpression condEx;
if ((condEx = asFunctionCallExpression(select.getCondition(), AlgebricksBuiltinFunctions.OR)) == null) {
return false;
}
List<Mutable<ILogicalExpression>> args = condEx.getArguments();
VariableReferenceExpression varEx = null;
IAType valType = null;
HashSet<AsterixConstantValue> values = new HashSet<AsterixConstantValue>();
for (Mutable<ILogicalExpression> arg : args) {
AbstractFunctionCallExpression fctCall;
if ((fctCall = asFunctionCallExpression(arg, AlgebricksBuiltinFunctions.EQ)) == null) {
return false;
}
boolean haveConst = false;
boolean haveVar = false;
List<Mutable<ILogicalExpression>> fctArgs = fctCall.getArguments();
for (Mutable<ILogicalExpression> fctArg : fctArgs) {
final ILogicalExpression argExpr = fctArg.getValue();
switch(argExpr.getExpressionTag()) {
case CONSTANT:
haveConst = true;
AsterixConstantValue value = (AsterixConstantValue) ((ConstantExpression) argExpr).getValue();
if (valType == null) {
valType = value.getObject().getType();
} else if (!isCompatible(valType, value.getObject().getType())) {
return false;
}
values.add(value);
break;
case VARIABLE:
haveVar = true;
final VariableReferenceExpression varArg = (VariableReferenceExpression) argExpr;
if (varEx == null) {
varEx = varArg;
} else if (!varEx.getVariableReference().equals(varArg.getVariableReference())) {
return false;
}
break;
default:
return false;
}
}
if (!(haveVar && haveConst)) {
return false;
}
}
AOrderedList list = new AOrderedList(new AOrderedListType(valType, "orderedlist"));
for (AsterixConstantValue value : values) {
list.add(value.getObject());
}
EmptyTupleSourceOperator ets = new EmptyTupleSourceOperator();
context.computeAndSetTypeEnvironmentForOperator(ets);
ILogicalExpression cExp = new ConstantExpression(new AsterixConstantValue(list));
Mutable<ILogicalExpression> mutCExp = new MutableObject<>(cExp);
IFunctionInfo scanFctInfo = BuiltinFunctions.getAsterixFunctionInfo(BuiltinFunctions.SCAN_COLLECTION);
UnnestingFunctionCallExpression scanExp = new UnnestingFunctionCallExpression(scanFctInfo, mutCExp);
LogicalVariable scanVar = context.newVar();
UnnestOperator unn = new UnnestOperator(scanVar, new MutableObject<>(scanExp));
unn.getInputs().add(new MutableObject<>(ets));
context.computeAndSetTypeEnvironmentForOperator(unn);
IFunctionInfo eqFctInfo = BuiltinFunctions.getAsterixFunctionInfo(AlgebricksBuiltinFunctions.EQ);
AbstractFunctionCallExpression eqExp = new ScalarFunctionCallExpression(eqFctInfo);
eqExp.getArguments().add(new MutableObject<>(new VariableReferenceExpression(scanVar)));
eqExp.getArguments().add(new MutableObject<>(varEx.cloneExpression()));
eqExp.getAnnotations().put(IndexedNLJoinExpressionAnnotation.INSTANCE, IndexedNLJoinExpressionAnnotation.INSTANCE);
BroadcastExpressionAnnotation bcast = new BroadcastExpressionAnnotation();
// Broadcast the OR predicates branch.
bcast.setObject(BroadcastExpressionAnnotation.BroadcastSide.LEFT);
eqExp.getAnnotations().put(BroadcastExpressionAnnotation.BROADCAST_ANNOTATION_KEY, bcast);
InnerJoinOperator jOp = new InnerJoinOperator(new MutableObject<>(eqExp));
jOp.getInputs().add(new MutableObject<>(unn));
jOp.getInputs().add(select.getInputs().get(0));
opRef.setValue(jOp);
context.computeAndSetTypeEnvironmentForOperator(jOp);
return true;
}
use of org.apache.asterix.om.types.AOrderedListType in project asterixdb by apache.
the class JTypeObjectFactory method create.
@Override
public IJObject create(IAType type) {
IJObject retValue = null;
switch(type.getTypeTag()) {
case INTEGER:
retValue = new JInt(0);
break;
case STRING:
retValue = new JString("");
break;
case FLOAT:
retValue = new JFloat(0);
break;
case DOUBLE:
retValue = new JDouble(0);
break;
case BOOLEAN:
retValue = new JBoolean(false);
break;
case CIRCLE:
retValue = new JCircle(new JPoint(0, 0), 0);
break;
case POINT:
retValue = new JPoint(0, 0);
break;
case POINT3D:
retValue = new JPoint3D(0, 0, 0);
break;
case POLYGON:
retValue = new JPolygon(new JPoint[] {});
break;
case LINE:
retValue = new JLine(new JPoint(0, 0), new JPoint(0, 0));
break;
case RECTANGLE:
retValue = new JRectangle(new JPoint(0, 0), new JPoint(1, 1));
break;
case DATE:
retValue = new JDate(0);
break;
case DATETIME:
retValue = new JDateTime(0);
break;
case DURATION:
retValue = new JDuration(0, 0);
break;
case INTERVAL:
retValue = new JInterval(0, 0);
break;
case TIME:
retValue = new JTime(0);
break;
case BIGINT:
retValue = new JLong(0);
break;
case NULL:
retValue = JObjects.JNull.INSTANCE;
break;
case MISSING:
retValue = JObjects.JMissing.INSTANCE;
break;
case ARRAY:
AOrderedListType ot = (AOrderedListType) type;
IAType orderedItemType = ot.getItemType();
IJObject orderedItemObject = create(orderedItemType);
retValue = new JOrderedList(orderedItemObject);
break;
case MULTISET:
AUnorderedListType ut = (AUnorderedListType) type;
IAType unorderedItemType = ut.getItemType();
IJObject unorderedItemObject = create(unorderedItemType);
retValue = new JUnorderedList(unorderedItemObject);
break;
case OBJECT:
IAType[] fieldTypes = ((ARecordType) type).getFieldTypes();
IJObject[] fieldObjects = new IJObject[fieldTypes.length];
int index = 0;
for (IAType fieldType : fieldTypes) {
fieldObjects[index] = create(fieldType);
index++;
}
retValue = new JRecord((ARecordType) type, fieldObjects);
break;
case UNION:
AUnionType unionType = (AUnionType) type;
IJObject itemObject = null;
if (unionType.isMissableType()) {
itemObject = create(unionType);
}
retValue = itemObject;
break;
default:
break;
}
return retValue;
}
use of org.apache.asterix.om.types.AOrderedListType in project asterixdb by apache.
the class RecordAddFieldsTypeComputer method computeType.
@Override
public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env, IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) expression;
String funcName = funcExpr.getFunctionIdentifier().getName();
IAType type0 = (IAType) env.getType(funcExpr.getArguments().get(0).getValue());
ARecordType inputRecordType = TypeComputeUtils.extractRecordType(type0);
if (inputRecordType == null) {
throw new TypeMismatchException(funcName, 0, type0.getTypeTag(), ATypeTag.OBJECT);
}
ILogicalExpression arg1 = funcExpr.getArguments().get(1).getValue();
IAType type1 = (IAType) env.getType(arg1);
AOrderedListType inputOrderedListType = TypeComputeUtils.extractOrderedListType(type1);
if (inputOrderedListType == null) {
return inputRecordType;
}
boolean unknownable = TypeHelper.canBeUnknown(type0) || TypeHelper.canBeUnknown(type1);
Map<String, IAType> additionalFields = new HashMap<>();
List<String> resultFieldNames = new ArrayList<>();
List<IAType> resultFieldTypes = new ArrayList<>();
resultFieldNames.addAll(Arrays.asList(inputRecordType.getFieldNames()));
Collections.sort(resultFieldNames);
for (String fieldName : resultFieldNames) {
if (inputRecordType.getFieldType(fieldName).getTypeTag() == ATypeTag.OBJECT) {
ARecordType nestedType = (ARecordType) inputRecordType.getFieldType(fieldName);
//Deep Copy prevents altering of input types
resultFieldTypes.add(nestedType.deepCopy(nestedType));
} else {
resultFieldTypes.add(inputRecordType.getFieldType(fieldName));
}
}
if (!containsVariable(arg1)) {
AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) arg1;
List<Mutable<ILogicalExpression>> args = f.getArguments();
String fieldName = null;
IAType fieldType = null;
// Iterating through the orderlist input
for (Mutable<ILogicalExpression> arg : args) {
AbstractFunctionCallExpression recConsExpr = (AbstractFunctionCallExpression) arg.getValue();
ARecordType rtype = TypeComputeUtils.extractRecordType((IAType) env.getType(recConsExpr));
if (rtype != null) {
String[] fn = rtype.getFieldNames();
IAType[] ft = rtype.getFieldTypes();
for (int j = 0; j < fn.length; j++) {
if (fn[j].equals(FIELD_NAME_NAME)) {
ILogicalExpression fieldNameExpr = recConsExpr.getArguments().get(j).getValue();
if (ConstantExpressionUtil.getStringConstant(fieldNameExpr) == null) {
throw new InvalidExpressionException(funcName, 1, fieldNameExpr, LogicalExpressionTag.CONSTANT);
}
// Get the actual "field-name" string
fieldName = ConstantExpressionUtil.getStringArgument(recConsExpr, j + 1);
} else if (fn[j].equals(FIELD_VALUE_VALUE)) {
fieldType = ft[j];
}
}
if (fieldName != null) {
additionalFields.put(fieldName, fieldType);
}
}
}
if (!additionalFields.isEmpty()) {
Iterator<Map.Entry<String, IAType>> it = additionalFields.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, IAType> entry = it.next();
resultFieldNames.add(entry.getKey());
resultFieldTypes.add(entry.getValue());
}
}
}
// If variable ignore, deal with the addition at runtime
String resultTypeName = "appended(" + inputRecordType.getTypeName() + ")";
int n = resultFieldNames.size();
IAType resultType = new ARecordType(resultTypeName, resultFieldNames.toArray(new String[n]), resultFieldTypes.toArray(new IAType[n]), true);
if (unknownable) {
resultType = AUnionType.createUnknownableType(resultType);
}
return resultType;
}
Aggregations