Search in sources :

Example 26 with IAType

use of org.apache.asterix.om.types.IAType 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 27 with IAType

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

the class NonTaggedFormatUtil method getTokenType.

public static IAType getTokenType(IAType keyType) throws AlgebricksException {
    IAType type = keyType;
    ATypeTag typeTag = keyType.getTypeTag();
    // Extract item type from list.
    if (typeTag == ATypeTag.MULTISET || typeTag == ATypeTag.ARRAY) {
        AbstractCollectionType listType = (AbstractCollectionType) keyType;
        if (!listType.isTyped()) {
            throw new AlgebricksException("Cannot build an inverted index on untyped lists.)");
        }
        type = listType.getItemType();
    }
    return type;
}
Also used : ATypeTag(org.apache.asterix.om.types.ATypeTag) AbstractCollectionType(org.apache.asterix.om.types.AbstractCollectionType) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) IAType(org.apache.asterix.om.types.IAType)

Example 28 with IAType

use of org.apache.asterix.om.types.IAType 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 29 with IAType

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

the class RecordRemoveFieldsTypeComputer method buildOutputType.

private IAType buildOutputType(Deque<String> fieldPathStack, ARecordType inputRecordType, Set<String> fieldNameSet, List<List<String>> pathList) throws AlgebricksException {
    List<String> resultFieldNames = new ArrayList<>();
    List<IAType> resultFieldTypes = new ArrayList<>();
    String[] fieldNames = inputRecordType.getFieldNames();
    IAType[] fieldTypes = inputRecordType.getFieldTypes();
    for (int i = 0; i < fieldNames.length; i++) {
        if (!fieldNameSet.contains(fieldNames[i])) {
            // The main field is to be kept
            addField(inputRecordType, fieldNames[i], resultFieldNames, resultFieldTypes);
        } else if (!pathList.isEmpty() && fieldTypes[i].getTypeTag() == ATypeTag.OBJECT) {
            ARecordType subRecord = (ARecordType) fieldTypes[i];
            fieldPathStack.push(fieldNames[i]);
            subRecord = deepCheckAndCopy(fieldPathStack, subRecord, pathList, inputRecordType.isOpen());
            fieldPathStack.pop();
            if (subRecord != null) {
                resultFieldNames.add(fieldNames[i]);
                resultFieldTypes.add(subRecord);
            }
        }
    }
    int n = resultFieldNames.size();
    String resultTypeName = "result-record(" + inputRecordType.getTypeName() + ")";
    return new ARecordType(resultTypeName, resultFieldNames.toArray(new String[n]), resultFieldTypes.toArray(new IAType[n]), // Make the output type open always
    true);
}
Also used : ArrayList(java.util.ArrayList) AString(org.apache.asterix.om.base.AString) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType)

Example 30 with IAType

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

the class RecordRemoveFieldsTypeComputer method deepCheckAndCopy.

/*
        A method to deep copy a record the path validation
             i.e., keep only fields that are valid
     */
private ARecordType deepCheckAndCopy(Deque<String> fieldPath, ARecordType srcRecType, List<List<String>> pathList, boolean isOpen) throws AlgebricksException {
    // Make sure the current path is valid before going further
    if (isRemovePath(fieldPath, pathList)) {
        return null;
    }
    String[] srcFieldNames = srcRecType.getFieldNames();
    IAType[] srcFieldTypes = srcRecType.getFieldTypes();
    List<IAType> destFieldTypes = new ArrayList<>();
    List<String> destFieldNames = new ArrayList<>();
    for (int i = 0; i < srcFieldNames.length; i++) {
        fieldPath.push(srcFieldNames[i]);
        if (!isRemovePath(fieldPath, pathList)) {
            if (srcFieldTypes[i].getTypeTag() == ATypeTag.OBJECT) {
                ARecordType subRecord = (ARecordType) srcFieldTypes[i];
                subRecord = deepCheckAndCopy(fieldPath, subRecord, pathList, isOpen);
                if (subRecord != null) {
                    destFieldNames.add(srcFieldNames[i]);
                    destFieldTypes.add(subRecord);
                }
            } else {
                destFieldNames.add(srcFieldNames[i]);
                destFieldTypes.add(srcFieldTypes[i]);
            }
        }
        fieldPath.pop();
    }
    int n = destFieldNames.size();
    if (n == 0) {
        return null;
    }
    return new ARecordType(srcRecType.getTypeName(), destFieldNames.toArray(new String[n]), destFieldTypes.toArray(new IAType[n]), isOpen);
}
Also used : ArrayList(java.util.ArrayList) AString(org.apache.asterix.om.base.AString) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType)

Aggregations

IAType (org.apache.asterix.om.types.IAType)190 ARecordType (org.apache.asterix.om.types.ARecordType)73 ArrayList (java.util.ArrayList)64 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)42 ATypeTag (org.apache.asterix.om.types.ATypeTag)40 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)37 List (java.util.List)32 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)32 AUnionType (org.apache.asterix.om.types.AUnionType)31 AString (org.apache.asterix.om.base.AString)28 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)27 Mutable (org.apache.commons.lang3.mutable.Mutable)25 Pair (org.apache.hyracks.algebricks.common.utils.Pair)24 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)20 Dataset (org.apache.asterix.metadata.entities.Dataset)18 AsterixException (org.apache.asterix.common.exceptions.AsterixException)17 AOrderedListType (org.apache.asterix.om.types.AOrderedListType)16 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)16 IVisitablePointable (org.apache.asterix.om.pointables.base.IVisitablePointable)15 IVariableTypeEnvironment (org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment)15