Search in sources :

Example 21 with IAObject

use of org.apache.asterix.om.base.IAObject in project asterixdb by apache.

the class RecordRemoveFieldsTypeComputer method getListFromExpression.

private List<String> getListFromExpression(String funcName, ILogicalExpression expression) throws AlgebricksException {
    AbstractFunctionCallExpression funcExp = (AbstractFunctionCallExpression) expression;
    List<Mutable<ILogicalExpression>> args = funcExp.getArguments();
    List<String> list = new ArrayList<>();
    for (Mutable<ILogicalExpression> arg : args) {
        // At this point all elements has to be a constant
        // Input list has only one level of nesting (list of list or list of strings)
        ConstantExpression ce = (ConstantExpression) arg.getValue();
        if (!(ce.getValue() instanceof AsterixConstantValue)) {
            throw new InvalidExpressionException(funcName, 1, ce, LogicalExpressionTag.CONSTANT);
        }
        IAObject item = ((AsterixConstantValue) ce.getValue()).getObject();
        ATypeTag type = item.getType().getTypeTag();
        if (type == ATypeTag.STRING) {
            list.add(((AString) item).getStringValue());
        } else {
            throw new UnsupportedTypeException(funcName, type);
        }
    }
    return list;
}
Also used : AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) IAObject(org.apache.asterix.om.base.IAObject) ArrayList(java.util.ArrayList) AString(org.apache.asterix.om.base.AString) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) ATypeTag(org.apache.asterix.om.types.ATypeTag) InvalidExpressionException(org.apache.asterix.om.exceptions.InvalidExpressionException) UnsupportedTypeException(org.apache.asterix.om.exceptions.UnsupportedTypeException)

Example 22 with IAObject

use of org.apache.asterix.om.base.IAObject in project asterixdb by apache.

the class SerializerDeserializerTestUtils method concurrentSerDeTestRun.

@SuppressWarnings("rawtypes")
public static void concurrentSerDeTestRun(ISerializerDeserializer serde, IAObject[] records) {
    Thread[] threads = new Thread[records.length];
    AtomicInteger errorCount = new AtomicInteger(0);
    for (int i = 0; i < threads.length; ++i) {
        final int index = i;
        threads[i] = new Thread(new Runnable() {

            @SuppressWarnings("unchecked")
            @Override
            public void run() {
                try {
                    int round = 0;
                    while (round++ < 100000) {
                        // serialize
                        ByteArrayOutputStream bos = new ByteArrayOutputStream();
                        DataOutput dos = new DataOutputStream(bos);
                        serde.serialize(records[index], dos);
                        bos.close();
                        // deserialize
                        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
                        DataInput dis = new DataInputStream(bis);
                        IAObject object = (IAObject) serde.deserialize(dis);
                        bis.close();
                        // asserts the equivalence of objects before and after serde.
                        Assert.assertTrue(object.deepEqual(records[index]));
                        Assert.assertTrue(records[index].deepEqual(object));
                    }
                } catch (Exception e) {
                    errorCount.incrementAndGet();
                    e.printStackTrace();
                }
            }
        });
        // Kicks off test threads.
        threads[i].start();
    }
    // Joins all the threads.
    try {
        for (int i = 0; i < threads.length; ++i) {
            threads[i].join();
        }
    } catch (InterruptedException e) {
        throw new IllegalStateException(e);
    }
    // Asserts no failure.
    Assert.assertTrue(errorCount.get() == 0);
}
Also used : DataOutput(java.io.DataOutput) DataOutputStream(java.io.DataOutputStream) IAObject(org.apache.asterix.om.base.IAObject) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) DataInput(java.io.DataInput) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 23 with IAObject

use of org.apache.asterix.om.base.IAObject in project asterixdb by apache.

the class ATypeHierarchy method getAsterixConstantValueFromNumericTypeObject.

// Get an AsterixConstantValue from a source Object
public static AsterixConstantValue getAsterixConstantValueFromNumericTypeObject(IAObject sourceObject, ATypeTag targetTypeTag, boolean strictDemote) throws HyracksDataException {
    ATypeTag sourceTypeTag = sourceObject.getType().getTypeTag();
    if (sourceTypeTag == targetTypeTag) {
        return new AsterixConstantValue(sourceObject);
    }
    // if the constant type and target type does not match, we do a type conversion
    ITypeConvertComputer convertComputer = null;
    if (canPromote(sourceTypeTag, targetTypeTag)) {
        convertComputer = ATypeHierarchy.getTypePromoteComputer(sourceTypeTag, targetTypeTag);
    } else if (canDemote(sourceTypeTag, targetTypeTag)) {
        convertComputer = ATypeHierarchy.getTypeDemoteComputer(sourceTypeTag, targetTypeTag, strictDemote);
    }
    if (convertComputer == null) {
        return null;
    }
    IAObject targetObject = convertComputer.convertType(sourceObject);
    return new AsterixConstantValue(targetObject);
}
Also used : ATypeTag(org.apache.asterix.om.types.ATypeTag) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) IAObject(org.apache.asterix.om.base.IAObject)

Example 24 with IAObject

use of org.apache.asterix.om.base.IAObject in project asterixdb by apache.

the class SerializerDeserializerProvider method addTag.

@SuppressWarnings("rawtypes")
private ISerializerDeserializer addTag(final ISerializerDeserializer nonTaggedSerde, final ATypeTag typeTag) {
    return new ISerializerDeserializer<IAObject>() {

        private static final long serialVersionUID = 1L;

        @Override
        public IAObject deserialize(DataInput in) throws HyracksDataException {
            try {
                ATypeTag tag = SerializerDeserializerUtil.deserializeTag(in);
                //deserialize the tag (move input cursor forward) and check if it's not NULL tag
                if (tag == ATypeTag.MISSING) {
                    return AMissing.MISSING;
                }
                if (tag == ATypeTag.NULL) {
                    return ANull.NULL;
                }
            } catch (IOException e) {
                throw new HyracksDataException(e);
            }
            return (IAObject) nonTaggedSerde.deserialize(in);
        }

        @SuppressWarnings("unchecked")
        @Override
        public void serialize(IAObject instance, DataOutput out) throws HyracksDataException {
            SerializerDeserializerUtil.serializeTag(instance, out);
            nonTaggedSerde.serialize(instance, out);
        }
    };
}
Also used : DataInput(java.io.DataInput) DataOutput(java.io.DataOutput) ATypeTag(org.apache.asterix.om.types.ATypeTag) IAObject(org.apache.asterix.om.base.IAObject) IOException(java.io.IOException) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Example 25 with IAObject

use of org.apache.asterix.om.base.IAObject in project asterixdb by apache.

the class AOrderedListSerializerDeserializer method deserialize.

@Override
public AOrderedList deserialize(DataInput in) throws HyracksDataException {
    try {
        ATypeTag typeTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(in.readByte());
        IAType currentItemType = itemType;
        @SuppressWarnings("rawtypes") ISerializerDeserializer currentDeserializer = deserializer;
        if (itemType.getTypeTag() == ATypeTag.ANY && typeTag != ATypeTag.ANY) {
            currentItemType = TypeTagUtil.getBuiltinTypeByTag(typeTag);
            currentDeserializer = SerializerDeserializerProvider.INSTANCE.getNonTaggedSerializerDeserializer(currentItemType);
        }
        List<IAObject> items = new ArrayList<IAObject>();
        // list size
        in.readInt();
        int numberOfitems;
        numberOfitems = in.readInt();
        if (numberOfitems > 0) {
            if (!NonTaggedFormatUtil.isFixedSizedCollection(currentItemType)) {
                for (int i = 0; i < numberOfitems; i++) in.readInt();
            }
            for (int i = 0; i < numberOfitems; i++) {
                IAObject v = (IAObject) currentDeserializer.deserialize(in);
                items.add(v);
            }
        }
        AOrderedListType type = new AOrderedListType(currentItemType, "orderedlist");
        return new AOrderedList(type, items);
    } catch (Exception e) {
        throw new HyracksDataException(e);
    }
}
Also used : AOrderedList(org.apache.asterix.om.base.AOrderedList) ATypeTag(org.apache.asterix.om.types.ATypeTag) IAObject(org.apache.asterix.om.base.IAObject) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) ArrayList(java.util.ArrayList) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) AsterixException(org.apache.asterix.common.exceptions.AsterixException) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IAType(org.apache.asterix.om.types.IAType)

Aggregations

IAObject (org.apache.asterix.om.base.IAObject)27 AsterixConstantValue (org.apache.asterix.om.constants.AsterixConstantValue)16 AString (org.apache.asterix.om.base.AString)12 ConstantExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression)10 ATypeTag (org.apache.asterix.om.types.ATypeTag)9 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)8 ArrayList (java.util.ArrayList)7 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)7 AOrderedList (org.apache.asterix.om.base.AOrderedList)5 IAType (org.apache.asterix.om.types.IAType)5 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)5 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)5 AInt32 (org.apache.asterix.om.base.AInt32)4 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)4 DataOutput (java.io.DataOutput)3 ARecordType (org.apache.asterix.om.types.ARecordType)3 Mutable (org.apache.commons.lang3.mutable.Mutable)3 ISerializerDeserializer (org.apache.hyracks.api.dataflow.value.ISerializerDeserializer)3 DataInput (java.io.DataInput)2 IOException (java.io.IOException)2