Search in sources :

Example 11 with AOrderedListType

use of org.apache.asterix.om.types.AOrderedListType in project asterixdb by apache.

the class JSONDeserializerForTypes method convertFromJSON.

/**
     * Deserialize an arbitrary JSON representation of a type.
     *
     * @param typeInJSON
     *            the JSON representation of the type.
     * @return an valid AsterixDB type.
     * @throws Exception
     */
public static IAType convertFromJSON(JsonNode typeInJSON) throws Exception {
    String typeName = typeInJSON.get("type").asText();
    // Deals with ordered list.
    if (typeName.equals(AOrderedListType.class.getName())) {
        IAType itemType = convertFromJSON(typeInJSON.get("item-type"));
        return new AOrderedListType(itemType, "ordered-list");
    }
    // Deals with unordered list.
    if (typeName.equals(AUnorderedListType.class.getName())) {
        IAType itemType = convertFromJSON(typeInJSON.get("item-type"));
        return new AUnorderedListType(itemType, "unordered-list");
    }
    // Deals with Union Type.
    if (typeName.equals(AUnionType.class.getName())) {
        List<IAType> unionTypes = new ArrayList<IAType>();
        JsonNode fields = typeInJSON.get("fields");
        for (int i = 0; i < fields.size(); i++) {
            JsonNode fieldType = fields.get(i);
            unionTypes.add(convertFromJSON(fieldType));
        }
        return new AUnionType(unionTypes, "union");
    }
    // Deals with record types.
    if (typeName.equals(ARecordType.class.getName())) {
        String name = typeInJSON.get("name").asText();
        boolean openType = typeInJSON.get("open").asBoolean();
        JsonNode fields = typeInJSON.get("fields");
        String[] fieldNames = new String[fields.size()];
        IAType[] fieldTypes = new IAType[fields.size()];
        for (int i = 0; i < fields.size(); ++i) {
            JsonNode field = fields.get(i);
            List<String> names = Lists.newArrayList(field.fieldNames());
            String fieldName = names.get(0);
            fieldNames[i] = fieldName;
            fieldTypes[i] = convertFromJSON(field.get(fieldName));
        }
        return new ARecordType(name, fieldNames, fieldTypes, openType);
    }
    // Deals with primitive types.
    Class<?> cl = BuiltinType.class;
    Field typeField = cl.getDeclaredField(typeName.toUpperCase());
    return (IAType) typeField.get(null);
}
Also used : AUnionType(org.apache.asterix.om.types.AUnionType) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) AUnorderedListType(org.apache.asterix.om.types.AUnorderedListType) BuiltinType(org.apache.asterix.om.types.BuiltinType) Field(java.lang.reflect.Field) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType)

Example 12 with AOrderedListType

use of org.apache.asterix.om.types.AOrderedListType in project asterixdb by apache.

the class RecordRemoveFieldsTypeComputer 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());
    List<List<String>> pathList = new ArrayList<>();
    Set<String> fieldNameSet = new HashSet<>();
    Deque<String> fieldPathStack = new ArrayDeque<>();
    ARecordType inputRecordType = getRecordTypeFromType(funcName, type0);
    if (inputRecordType == null) {
        return BuiltinType.ANY;
    }
    AbstractLogicalExpression arg1 = (AbstractLogicalExpression) funcExpr.getArguments().get(1).getValue();
    IAType inputListType = (IAType) env.getType(arg1);
    AOrderedListType inputOrderedListType = TypeComputeUtils.extractOrderedListType(inputListType);
    if (inputOrderedListType == null) {
        throw new TypeMismatchException(funcName, 1, inputListType.getTypeTag(), ATypeTag.ARRAY);
    }
    ATypeTag tt = inputOrderedListType.getItemType().getTypeTag();
    if (tt == ATypeTag.STRING) {
        // If top-fieldlist
        if (setFieldNameSet(arg1, fieldNameSet)) {
            return buildOutputType(fieldPathStack, inputRecordType, fieldNameSet, pathList);
        } else {
            return DefaultOpenFieldType.NESTED_OPEN_RECORD_TYPE;
        }
    } else {
        // tt == ATypeTag.ANY, meaning the list is nested
        computeTypeFromNonConstantExpression(funcName, arg1, fieldNameSet, pathList);
        IAType resultType = buildOutputType(fieldPathStack, inputRecordType, fieldNameSet, pathList);
        return resultType;
    }
}
Also used : AbstractLogicalExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractLogicalExpression) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) TypeMismatchException(org.apache.asterix.om.exceptions.TypeMismatchException) ArrayList(java.util.ArrayList) AString(org.apache.asterix.om.base.AString) ArrayDeque(java.util.ArrayDeque) ATypeTag(org.apache.asterix.om.types.ATypeTag) AOrderedList(org.apache.asterix.om.base.AOrderedList) ArrayList(java.util.ArrayList) List(java.util.List) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType) HashSet(java.util.HashSet)

Example 13 with AOrderedListType

use of org.apache.asterix.om.types.AOrderedListType in project asterixdb by apache.

the class FieldAccessNestedResultType method checkOrderedList.

private void checkOrderedList(String funcName, IAType type) throws AlgebricksException {
    AOrderedListType listType = (AOrderedListType) type;
    ATypeTag itemTypeTag = listType.getItemType().getTypeTag();
    if (itemTypeTag != ATypeTag.STRING && itemTypeTag != ATypeTag.ANY) {
        throw new UnsupportedItemTypeException(funcName, itemTypeTag);
    }
}
Also used : UnsupportedItemTypeException(org.apache.asterix.om.exceptions.UnsupportedItemTypeException) ATypeTag(org.apache.asterix.om.types.ATypeTag) AOrderedListType(org.apache.asterix.om.types.AOrderedListType)

Example 14 with AOrderedListType

use of org.apache.asterix.om.types.AOrderedListType in project asterixdb by apache.

the class TypeResolverUtilTest method testOrderedListType.

@Test
public void testOrderedListType() {
    // Constructs input types.
    ARecordType leftRecordType = new ARecordType("null", new String[] { "a", "b" }, new IAType[] { BuiltinType.ASTRING, BuiltinType.AINT32 }, true, Collections.singleton("d"));
    AOrderedListType leftListType = new AOrderedListType(leftRecordType, "null");
    ARecordType rightRecordType = new ARecordType("null", new String[] { "b", "c" }, new IAType[] { BuiltinType.AINT32, BuiltinType.ABINARY }, true, Collections.singleton("e"));
    AOrderedListType rightListType = new AOrderedListType(rightRecordType, "null");
    // Gets the actual resolved type.
    List<IAType> inputTypes = new ArrayList<>();
    inputTypes.add(leftListType);
    inputTypes.add(rightListType);
    AbstractCollectionType resolvedType = (AbstractCollectionType) TypeResolverUtil.resolve(inputTypes);
    ARecordType resolvedRecordType = (ARecordType) resolvedType.getItemType();
    // Gets the expected generalized type.
    Set<String> possibleAdditionalFields = new HashSet<>();
    possibleAdditionalFields.add("a");
    possibleAdditionalFields.add("c");
    possibleAdditionalFields.add("d");
    possibleAdditionalFields.add("e");
    ARecordType expectedRecordType = new ARecordType(null, new String[] { "b" }, new IAType[] { BuiltinType.AINT32 }, true, possibleAdditionalFields);
    AOrderedListType expectedListType = new AOrderedListType(expectedRecordType, null);
    // Compares the resolved type and the expected type.
    Assert.assertEquals(resolvedType, expectedListType);
    Assert.assertEquals(resolvedRecordType.getAllPossibleAdditonalFieldNames(), expectedRecordType.getAllPossibleAdditonalFieldNames());
}
Also used : AOrderedListType(org.apache.asterix.om.types.AOrderedListType) ArrayList(java.util.ArrayList) AbstractCollectionType(org.apache.asterix.om.types.AbstractCollectionType) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 15 with AOrderedListType

use of org.apache.asterix.om.types.AOrderedListType in project asterixdb by apache.

the class ClassAdParser method writeFieldValueToBuffer.

private void writeFieldValueToBuffer(IAType fieldType, DataOutput out, String name, ExprTree tree, ClassAd pAd) throws IOException, AsterixException {
    Value val;
    switch(tree.getKind()) {
        case ATTRREF_NODE:
        case CLASSAD_NODE:
        case EXPR_ENVELOPE:
        case EXPR_LIST_NODE:
        case FN_CALL_NODE:
        case OP_NODE:
            val = objectPool.valuePool.get();
            if (pAd.evaluateAttr(name, val)) {
            } else {
                // just write the expr
                val = ((Literal) pAd.getAttrList().get(name + "Expr")).getValue();
            }
            break;
        case LITERAL_NODE:
            val = ((Literal) tree.getTree()).getValue();
            break;
        default:
            throw new HyracksDataException("Unknown Expression type detected: " + tree.getKind());
    }
    if (fieldType != null) {
        if (NonTaggedFormatUtil.isOptional(fieldType)) {
            fieldType = ((AUnionType) fieldType).getActualType();
        }
    }
    switch(val.getValueType()) {
        case ABSOLUTE_TIME_VALUE:
            if (checkType(ATypeTag.DATETIME, fieldType)) {
                parseDateTime(val, out);
            } else {
                throw new HyracksDataException(mismatchErrorMessage + fieldType.getTypeTag());
            }
            break;
        case BOOLEAN_VALUE:
            if (checkType(ATypeTag.BOOLEAN, fieldType)) {
                booleanSerde.serialize(val.getBoolVal() ? ABoolean.TRUE : ABoolean.FALSE, out);
            } else {
                throw new HyracksDataException(mismatchErrorMessage + fieldType.getTypeTag());
            }
            break;
        case CLASSAD_VALUE:
            if (checkType(ATypeTag.OBJECT, fieldType)) {
                IAType objectType = getComplexType(fieldType, ATypeTag.OBJECT);
                ClassAd classad = val.getClassadVal();
                parseRecord((ARecordType) objectType, classad, out);
            } else {
                throw new HyracksDataException(mismatchErrorMessage + fieldType.getTypeTag());
            }
            break;
        case ERROR_VALUE:
        case STRING_VALUE:
        case UNDEFINED_VALUE:
            if (checkType(ATypeTag.STRING, fieldType)) {
                parseString(val, out);
            } else {
                throw new HyracksDataException(mismatchErrorMessage + fieldType.getTypeTag());
            }
            break;
        case INTEGER_VALUE:
            if (checkType(ATypeTag.BIGINT, fieldType)) {
                if (fieldType == null || fieldType.getTypeTag() == ATypeTag.BIGINT) {
                    aInt64.setValue(val.getLongVal());
                    int64Serde.serialize(aInt64, out);
                } else if (fieldType.getTypeTag() == ATypeTag.INTEGER) {
                    aInt32.setValue((int) val.getLongVal());
                    int32Serde.serialize(aInt32, out);
                } else if (fieldType.getTypeTag() == ATypeTag.DOUBLE) {
                    aDouble.setValue(val.getLongVal());
                    doubleSerde.serialize(aDouble, out);
                } else if (fieldType.getTypeTag() == ATypeTag.SMALLINT) {
                    aInt16.setValue((short) val.getLongVal());
                    int16Serde.serialize(aInt16, out);
                } else if (fieldType.getTypeTag() == ATypeTag.TINYINT) {
                    aInt8.setValue((byte) val.getLongVal());
                    int8Serde.serialize(aInt8, out);
                } else if (fieldType.getTypeTag() == ATypeTag.FLOAT) {
                    aFloat.setValue(val.getLongVal());
                    floatSerde.serialize(aFloat, out);
                }
            } else if (checkType(ATypeTag.DATETIME, fieldType)) {
                // Classad uses Linux Timestamps (s instead of ms)
                aDateTime.setValue(val.getLongVal() * 1000);
                datetimeSerde.serialize(aDateTime, out);
            } else if (checkType(ATypeTag.DURATION, fieldType)) {
                // Classad uses Linux Timestamps (s instead of ms)
                aDuration.setValue(0, val.getLongVal() * 1000);
                durationSerde.serialize(aDuration, out);
            } else if (checkType(ATypeTag.INTEGER, fieldType)) {
                aInt32.setValue((int) val.getLongVal());
                int32Serde.serialize(aInt32, out);
            } else if (checkType(ATypeTag.DOUBLE, fieldType)) {
                aDouble.setValue(val.getLongVal());
                doubleSerde.serialize(aDouble, out);
            } else {
                throw new HyracksDataException(mismatchErrorMessage + fieldType.getTypeTag());
            }
            break;
        case LIST_VALUE:
        case SLIST_VALUE:
            IAType objectType;
            if (checkType(ATypeTag.MULTISET, fieldType)) {
                objectType = getComplexType(fieldType, ATypeTag.MULTISET);
                parseUnorderedList((AUnorderedListType) objectType, val, out);
            } else if (checkType(ATypeTag.ARRAY, fieldType)) {
                objectType = getComplexType(fieldType, ATypeTag.ARRAY);
                parseOrderedList((AOrderedListType) objectType, val, out);
            } else {
                throw new HyracksDataException(mismatchErrorMessage + fieldType.getTypeTag());
            }
            break;
        case REAL_VALUE:
            if (checkType(ATypeTag.DOUBLE, fieldType)) {
                if (fieldType == null || fieldType.getTypeTag() == ATypeTag.DOUBLE) {
                    aDouble.setValue(val.getDoubleVal());
                    doubleSerde.serialize(aDouble, out);
                } else if (fieldType.getTypeTag() == ATypeTag.INTEGER) {
                    aInt32.setValue((int) val.getDoubleVal());
                    int32Serde.serialize(aInt32, out);
                } else if (fieldType.getTypeTag() == ATypeTag.BIGINT) {
                    aInt64.setValue((long) val.getDoubleVal());
                    int64Serde.serialize(aInt64, out);
                } else if (fieldType.getTypeTag() == ATypeTag.SMALLINT) {
                    aInt16.setValue((short) val.getDoubleVal());
                    int16Serde.serialize(aInt16, out);
                } else if (fieldType.getTypeTag() == ATypeTag.TINYINT) {
                    aInt8.setValue((byte) val.getDoubleVal());
                    int8Serde.serialize(aInt8, out);
                } else if (fieldType.getTypeTag() == ATypeTag.FLOAT) {
                    aFloat.setValue((float) val.getDoubleVal());
                    floatSerde.serialize(aFloat, out);
                }
            } else if (checkType(ATypeTag.INTEGER, fieldType)) {
                aInt32.setValue((int) val.getDoubleVal());
                int32Serde.serialize(aInt32, out);
            } else if (checkType(ATypeTag.BIGINT, fieldType)) {
                aInt64.setValue((long) val.getDoubleVal());
                int64Serde.serialize(aInt64, out);
            } else if (checkType(ATypeTag.DATETIME, fieldType)) {
                // Classad uses Linux Timestamps (s instead of ms)
                aDateTime.setValue(val.getLongVal() * 1000);
                datetimeSerde.serialize(aDateTime, out);
            } else if (checkType(ATypeTag.DURATION, fieldType)) {
                // Classad uses Linux Timestamps (s instead of ms)
                aDuration.setValue(0, (long) (val.getDoubleVal() * 1000.0));
                durationSerde.serialize(aDuration, out);
            } else {
                throw new HyracksDataException(mismatchErrorMessage + fieldType.getTypeTag());
            }
            break;
        case RELATIVE_TIME_VALUE:
            if (checkType(ATypeTag.DURATION, fieldType)) {
                parseDuration(val, out);
            } else {
                throw new HyracksDataException(mismatchErrorMessage + fieldType.getTypeTag());
            }
            break;
        default:
            throw new HyracksDataException("unknown data type " + val.getValueType());
    }
}
Also used : ClassAd(org.apache.asterix.external.classad.ClassAd) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) TokenValue(org.apache.asterix.external.classad.TokenValue) Value(org.apache.asterix.external.classad.Value) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IAType(org.apache.asterix.om.types.IAType)

Aggregations

AOrderedListType (org.apache.asterix.om.types.AOrderedListType)26 IAType (org.apache.asterix.om.types.IAType)16 ARecordType (org.apache.asterix.om.types.ARecordType)13 ArrayList (java.util.ArrayList)9 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)8 OrderedListBuilder (org.apache.asterix.builders.OrderedListBuilder)7 DataOutput (java.io.DataOutput)6 AString (org.apache.asterix.om.base.AString)6 ArrayBackedValueStorage (org.apache.hyracks.data.std.util.ArrayBackedValueStorage)6 AOrderedList (org.apache.asterix.om.base.AOrderedList)5 AUnionType (org.apache.asterix.om.types.AUnionType)5 Test (org.junit.Test)5 IOException (java.io.IOException)4 ATypeTag (org.apache.asterix.om.types.ATypeTag)4 AUnorderedListType (org.apache.asterix.om.types.AUnorderedListType)4 TypeMismatchException (org.apache.asterix.runtime.exceptions.TypeMismatchException)4 IScalarEvaluator (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator)4 IScalarEvaluatorFactory (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)4 IHyracksTaskContext (org.apache.hyracks.api.context.IHyracksTaskContext)4 IPointable (org.apache.hyracks.data.std.api.IPointable)4