Search in sources :

Example 16 with AUnionType

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

the class InvertedIndexAccessMethod method addSearchKeyType.

private void addSearchKeyType(IOptimizableFuncExpr optFuncExpr, OptimizableOperatorSubTree indexSubTree, IOptimizationContext context, InvertedIndexJobGenParams jobGenParams) throws AlgebricksException {
    // If we have two variables in the optFunxExpr, then we are optimizing a join.
    IAType type = null;
    ATypeTag typeTag = null;
    if (optFuncExpr.getNumLogicalVars() == 2) {
        // Find the type of the variable that is going to feed into the index search.
        if (optFuncExpr.getOperatorSubTree(0) == indexSubTree) {
            // If the index is on a dataset in subtree 0, then subtree 1 will feed.
            type = optFuncExpr.getFieldType(1);
        } else {
            // If the index is on a dataset in subtree 1, then subtree 0 will feed.
            type = optFuncExpr.getFieldType(0);
        }
        typeTag = type.getTypeTag();
        if (type.getTypeTag() == ATypeTag.UNION) {
            // If this is a nullable field, then we need to get the actual type tag.
            typeTag = ((AUnionType) type).getActualType().getTypeTag();
        }
    } else {
        // We are optimizing a selection query. Add the type of the search key constant.
        type = optFuncExpr.getConstantType(0);
        typeTag = type.getTypeTag();
        if (typeTag == ATypeTag.UNION) {
            // If this is a nullable field, then we need to get the actual type tag.
            typeTag = ((AUnionType) type).getActualType().getTypeTag();
        }
        if (typeTag != ATypeTag.ARRAY && typeTag != ATypeTag.STRING && typeTag != ATypeTag.MULTISET) {
            throw CompilationException.create(ErrorCode.NO_SUPPORTED_TYPE);
        }
    }
    jobGenParams.setSearchKeyType(typeTag);
}
Also used : ATypeTag(org.apache.asterix.om.types.ATypeTag) AUnionType(org.apache.asterix.om.types.AUnionType) IAType(org.apache.asterix.om.types.IAType)

Example 17 with AUnionType

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

the class TypeTranslator method secondPass.

private static void secondPass(MetadataTransactionContext mdTxnCtx, Map<TypeSignature, IAType> typeMap, Map<String, Map<ARecordType, List<Integer>>> incompleteFieldTypes, Map<TypeSignature, List<AbstractCollectionType>> incompleteItemTypes, Map<TypeSignature, List<TypeSignature>> incompleteTopLevelTypeReferences, String typeDataverse) throws AlgebricksException {
    // solve remaining top level references
    for (TypeSignature typeSignature : incompleteTopLevelTypeReferences.keySet()) {
        IAType t;
        Datatype dt = MetadataManager.INSTANCE.getDatatype(mdTxnCtx, typeSignature.getNamespace(), typeSignature.getName());
        if (dt == null) {
            throw new AlgebricksException("Could not resolve type " + typeSignature);
        } else {
            t = dt.getDatatype();
        }
        for (TypeSignature sign : incompleteTopLevelTypeReferences.get(typeSignature)) {
            typeMap.put(sign, t);
        }
    }
    // solve remaining field type references
    for (String trefName : incompleteFieldTypes.keySet()) {
        IAType t;
        Datatype dt = MetadataManager.INSTANCE.getDatatype(mdTxnCtx, typeDataverse, trefName);
        if (dt == null) {
            dt = MetadataManager.INSTANCE.getDatatype(mdTxnCtx, MetadataConstants.METADATA_DATAVERSE_NAME, trefName);
        }
        if (dt == null) {
            throw new AlgebricksException("Could not resolve type " + trefName);
        } else {
            t = dt.getDatatype();
        }
        Map<ARecordType, List<Integer>> fieldsToFix = incompleteFieldTypes.get(trefName);
        for (ARecordType recType : fieldsToFix.keySet()) {
            List<Integer> positions = fieldsToFix.get(recType);
            IAType[] fldTypes = recType.getFieldTypes();
            for (Integer pos : positions) {
                if (fldTypes[pos] == null) {
                    fldTypes[pos] = t;
                } else {
                    // nullable
                    AUnionType nullableUnion = (AUnionType) fldTypes[pos];
                    nullableUnion.setActualType(t);
                }
            }
        }
    }
    // solve remaining item type references
    for (TypeSignature typeSignature : incompleteItemTypes.keySet()) {
        IAType t;
        Datatype dt;
        if (MetadataManager.INSTANCE != null) {
            dt = MetadataManager.INSTANCE.getDatatype(mdTxnCtx, typeSignature.getNamespace(), typeSignature.getName());
            if (dt == null) {
                throw new AlgebricksException("Could not resolve type " + typeSignature);
            }
            t = dt.getDatatype();
        } else {
            t = typeMap.get(typeSignature);
        }
        for (AbstractCollectionType act : incompleteItemTypes.get(typeSignature)) {
            act.setItemType(t);
        }
    }
}
Also used : AUnionType(org.apache.asterix.om.types.AUnionType) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) Datatype(org.apache.asterix.metadata.entities.Datatype) TypeSignature(org.apache.asterix.om.types.TypeSignature) AbstractCollectionType(org.apache.asterix.om.types.AbstractCollectionType) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType)

Example 18 with AUnionType

use of org.apache.asterix.om.types.AUnionType 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;
}
Also used : JRecord(org.apache.asterix.external.library.java.JObjects.JRecord) JPoint(org.apache.asterix.external.library.java.JObjects.JPoint) AUnionType(org.apache.asterix.om.types.AUnionType) JRectangle(org.apache.asterix.external.library.java.JObjects.JRectangle) JPolygon(org.apache.asterix.external.library.java.JObjects.JPolygon) JInterval(org.apache.asterix.external.library.java.JObjects.JInterval) JTime(org.apache.asterix.external.library.java.JObjects.JTime) JLong(org.apache.asterix.external.library.java.JObjects.JLong) JDouble(org.apache.asterix.external.library.java.JObjects.JDouble) JBoolean(org.apache.asterix.external.library.java.JObjects.JBoolean) JDuration(org.apache.asterix.external.library.java.JObjects.JDuration) JFloat(org.apache.asterix.external.library.java.JObjects.JFloat) JCircle(org.apache.asterix.external.library.java.JObjects.JCircle) JInt(org.apache.asterix.external.library.java.JObjects.JInt) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) IJObject(org.apache.asterix.external.api.IJObject) JLine(org.apache.asterix.external.library.java.JObjects.JLine) JDate(org.apache.asterix.external.library.java.JObjects.JDate) AUnorderedListType(org.apache.asterix.om.types.AUnorderedListType) JPoint(org.apache.asterix.external.library.java.JObjects.JPoint) JUnorderedList(org.apache.asterix.external.library.java.JObjects.JUnorderedList) JDateTime(org.apache.asterix.external.library.java.JObjects.JDateTime) JPoint3D(org.apache.asterix.external.library.java.JObjects.JPoint3D) JOrderedList(org.apache.asterix.external.library.java.JObjects.JOrderedList) JString(org.apache.asterix.external.library.java.JObjects.JString) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType)

Example 19 with AUnionType

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

the class AbstractIfMissingOrNullTypeComputer method computeType.

@Override
public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env, IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
    AbstractFunctionCallExpression fce = (AbstractFunctionCallExpression) expression;
    IAType outPrimeType = null;
    // could be 'missing' or 'null'
    ATypeTag outQuantifier = null;
    for (Mutable<ILogicalExpression> argRef : fce.getArguments()) {
        ILogicalExpression arg = argRef.getValue();
        IAType argType = (IAType) env.getType(arg);
        ATypeTag argTypeTag = argType.getTypeTag();
        if (equalsIfType(argTypeTag)) {
            continue;
        }
        if (argTypeTag == ATypeTag.UNION) {
            AUnionType unionType = (AUnionType) argType;
            if (intersectsIfType(unionType)) {
                IAType primeType = getOutputPrimeType(unionType);
                outPrimeType = outPrimeType == null ? primeType : TypeResolverUtil.resolve(outPrimeType, primeType);
                if (outQuantifier == null) {
                    outQuantifier = getOutputQuantifier(unionType);
                }
            } else {
                // no intersection
                if (outPrimeType == null) {
                    return argType;
                } else {
                    IAType primeType = getOutputPrimeType(unionType);
                    ATypeTag quantifier = outQuantifier != null ? outQuantifier : getOutputQuantifier(unionType);
                    return createOutputType(TypeResolverUtil.resolve(outPrimeType, primeType), quantifier);
                }
            }
        } else {
            // ANY or no intersection
            return outPrimeType == null ? argType : createOutputType(TypeResolverUtil.resolve(outPrimeType, argType), outQuantifier);
        }
    }
    if (outPrimeType == null) {
        return BuiltinType.ANULL;
    }
    IAType outType = createOutputType(outPrimeType, ATypeTag.NULL);
    if (outQuantifier == ATypeTag.MISSING) {
        outType = createOutputType(outType, ATypeTag.MISSING);
    }
    return outType;
}
Also used : ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) ATypeTag(org.apache.asterix.om.types.ATypeTag) AUnionType(org.apache.asterix.om.types.AUnionType) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) IAType(org.apache.asterix.om.types.IAType)

Example 20 with AUnionType

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

the class ClosedRecordConstructorResultType method computeType.

@Override
public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env, IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
    AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) expression;
    String funcName = f.getFunctionIdentifier().getName();
    /**
         * if type has been top-down propagated, use the enforced type
         */
    ARecordType type = (ARecordType) TypeCastUtils.getRequiredType(f);
    if (type != null) {
        return type;
    }
    int n = f.getArguments().size() / 2;
    String[] fieldNames = new String[n];
    IAType[] fieldTypes = new IAType[n];
    int i = 0;
    Iterator<Mutable<ILogicalExpression>> argIter = f.getArguments().iterator();
    while (argIter.hasNext()) {
        ILogicalExpression e1 = argIter.next().getValue();
        ILogicalExpression e2 = argIter.next().getValue();
        IAType e2Type = (IAType) env.getType(e2);
        if (e2Type.getTypeTag() == ATypeTag.UNION) {
            AUnionType unionType = (AUnionType) e2Type;
            e2Type = AUnionType.createUnknownableType(unionType.getActualType());
        }
        fieldTypes[i] = e2Type;
        fieldNames[i] = ConstantExpressionUtil.getStringConstant(e1);
        if (fieldNames[i] == null) {
            throw new InvalidExpressionException(funcName, 2 * i, e1, LogicalExpressionTag.CONSTANT);
        }
        i++;
    }
    return new ARecordType(null, fieldNames, fieldTypes, false);
}
Also used : Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) InvalidExpressionException(org.apache.asterix.om.exceptions.InvalidExpressionException) AUnionType(org.apache.asterix.om.types.AUnionType) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType)

Aggregations

AUnionType (org.apache.asterix.om.types.AUnionType)32 IAType (org.apache.asterix.om.types.IAType)31 ATypeTag (org.apache.asterix.om.types.ATypeTag)13 ARecordType (org.apache.asterix.om.types.ARecordType)11 ArrayList (java.util.ArrayList)10 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)7 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)6 AOrderedListType (org.apache.asterix.om.types.AOrderedListType)5 AUnorderedListType (org.apache.asterix.om.types.AUnorderedListType)5 Mutable (org.apache.commons.lang3.mutable.Mutable)5 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)5 DataOutput (java.io.DataOutput)4 AString (org.apache.asterix.om.base.AString)4 IOException (java.io.IOException)3 List (java.util.List)3 RuntimeDataException (org.apache.asterix.common.exceptions.RuntimeDataException)3 IVisitablePointable (org.apache.asterix.om.pointables.base.IVisitablePointable)3 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)3 HashSet (java.util.HashSet)2 AsterixException (org.apache.asterix.common.exceptions.AsterixException)2