Search in sources :

Example 56 with ARecordType

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

the class LoadRecordFieldsRule method findFieldByIndexFromRecordConstructor.

// Resolves field expression by index-based access.
private static ILogicalExpression findFieldByIndexFromRecordConstructor(Object index, AbstractFunctionCallExpression fce, IVariableTypeEnvironment typeEnvironment) throws AlgebricksException {
    Integer fieldIndex = (Integer) index;
    ARecordType recordType = (ARecordType) typeEnvironment.getType(fce);
    String[] closedFieldNames = recordType.getFieldNames();
    return closedFieldNames.length > fieldIndex ? findFieldByNameFromRecordConstructor(closedFieldNames[fieldIndex], fce) : null;
}
Also used : AString(org.apache.asterix.om.base.AString) ARecordType(org.apache.asterix.om.types.ARecordType)

Example 57 with ARecordType

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

the class PushFieldAccessRule method isAccessToIndexedField.

@SuppressWarnings("unchecked")
private boolean isAccessToIndexedField(AssignOperator assign, IOptimizationContext context) throws AlgebricksException {
    AbstractFunctionCallExpression accessFun = (AbstractFunctionCallExpression) assign.getExpressions().get(0).getValue();
    ILogicalExpression e0 = accessFun.getArguments().get(0).getValue();
    if (e0.getExpressionTag() != LogicalExpressionTag.VARIABLE) {
        return false;
    }
    LogicalVariable var = ((VariableReferenceExpression) e0).getVariableReference();
    if (context.findPrimaryKey(var) == null) {
        // not referring to a dataset record
        return false;
    }
    AbstractLogicalOperator op = assign;
    while (op.getInputs().size() == 1 && op.getOperatorTag() != LogicalOperatorTag.DATASOURCESCAN) {
        op = (AbstractLogicalOperator) op.getInputs().get(0).getValue();
    }
    if (op.getOperatorTag() != LogicalOperatorTag.DATASOURCESCAN) {
        return false;
    }
    DataSourceScanOperator scan = (DataSourceScanOperator) op;
    LogicalVariable recVar = scan.getVariables().get(scan.getVariables().size() - 1);
    if (recVar != var) {
        return false;
    }
    MetadataProvider mp = (MetadataProvider) context.getMetadataProvider();
    DataSourceId asid = ((IDataSource<DataSourceId>) scan.getDataSource()).getId();
    Dataset dataset = mp.findDataset(asid.getDataverseName(), asid.getDatasourceName());
    if (dataset == null) {
        throw new AlgebricksException("Dataset " + asid.getDatasourceName() + " not found.");
    }
    if (dataset.getDatasetType() != DatasetType.INTERNAL) {
        return false;
    }
    final Integer pos = ConstantExpressionUtil.getIntConstant(accessFun.getArguments().get(1).getValue());
    if (pos != null) {
        String tName = dataset.getItemTypeName();
        IAType t = mp.findType(dataset.getItemTypeDataverseName(), tName);
        if (t.getTypeTag() != ATypeTag.OBJECT) {
            return false;
        }
        ARecordType rt = (ARecordType) t;
        if (pos >= rt.getFieldNames().length) {
            return false;
        }
    }
    List<Index> datasetIndexes = mp.getDatasetIndexes(dataset.getDataverseName(), dataset.getDatasetName());
    boolean hasSecondaryIndex = false;
    for (Index index : datasetIndexes) {
        if (index.isSecondaryIndex()) {
            hasSecondaryIndex = true;
            break;
        }
    }
    return hasSecondaryIndex;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) Dataset(org.apache.asterix.metadata.entities.Dataset) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) Index(org.apache.asterix.metadata.entities.Index) AString(org.apache.asterix.om.base.AString) IDataSource(org.apache.hyracks.algebricks.core.algebra.metadata.IDataSource) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) DataSourceScanOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.DataSourceScanOperator) MetadataProvider(org.apache.asterix.metadata.declared.MetadataProvider) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) ARecordType(org.apache.asterix.om.types.ARecordType) DataSourceId(org.apache.asterix.metadata.declared.DataSourceId) IAType(org.apache.asterix.om.types.IAType)

Example 58 with ARecordType

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

the class IntroduceDynamicTypeCastRule method compatible.

/**
     * Check whether the required record type and the input type is compatible
     *
     * @param reqType
     * @param inputType
     * @return true if compatible; false otherwise
     * @throws AlgebricksException
     */
public static boolean compatible(ARecordType reqType, IAType inputType) throws AlgebricksException {
    if (inputType.getTypeTag() == ATypeTag.ANY) {
        return false;
    }
    if (inputType.getTypeTag() != ATypeTag.OBJECT) {
        throw new AlgebricksException("The input type " + inputType + " is not a valid record type!");
    }
    ARecordType inputRecType = (ARecordType) inputType;
    if (reqType.isOpen() != inputRecType.isOpen()) {
        return false;
    }
    IAType[] reqTypes = reqType.getFieldTypes();
    String[] reqFieldNames = reqType.getFieldNames();
    IAType[] inputTypes = inputRecType.getFieldTypes();
    String[] inputFieldNames = ((ARecordType) inputType).getFieldNames();
    if (reqTypes.length != inputTypes.length) {
        return false;
    }
    for (int i = 0; i < reqTypes.length; i++) {
        if (!reqFieldNames[i].equals(inputFieldNames[i])) {
            return false;
        }
        IAType reqTypeInside = reqTypes[i];
        if (NonTaggedFormatUtil.isOptional(reqTypes[i])) {
            reqTypeInside = ((AUnionType) reqTypes[i]).getActualType();
        }
        IAType inputTypeInside = inputTypes[i];
        if (NonTaggedFormatUtil.isOptional(inputTypes[i])) {
            if (!NonTaggedFormatUtil.isOptional(reqTypes[i])) {
                /** if the required type is not optional, the two types are incompatible */
                return false;
            }
            inputTypeInside = ((AUnionType) inputTypes[i]).getActualType();
        }
        if (inputTypeInside.getTypeTag() != ATypeTag.MISSING && !reqTypeInside.equals(inputTypeInside)) {
            return false;
        }
    }
    return true;
}
Also used : AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType)

Example 59 with ARecordType

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

the class AccessMethodUtils method appendPrimaryIndexTypes.

public static void appendPrimaryIndexTypes(Dataset dataset, IAType itemType, IAType metaItemType, List<Object> target) throws AlgebricksException {
    ARecordType recordType = (ARecordType) itemType;
    ARecordType metaRecordType = (ARecordType) metaItemType;
    target.addAll(KeyFieldTypeUtil.getPartitoningKeyTypes(dataset, recordType, metaRecordType));
    // Adds data record type.
    target.add(itemType);
    // Adds meta record type if any.
    if (dataset.hasMetaPart()) {
        target.add(metaItemType);
    }
}
Also used : ARecordType(org.apache.asterix.om.types.ARecordType)

Example 60 with ARecordType

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

the class ResolveVariableRule method findCandidatePathsForExpr.

// Recursively finds candidate paths under an expression.
private Set<Pair<ILogicalExpression, List<String>>> findCandidatePathsForExpr(String unresolvedVarName, IAType pathType, ILogicalExpression expr, List<String> parentPath) throws AlgebricksException {
    Set<Pair<ILogicalExpression, List<String>>> varAccessCandidates = new HashSet<>();
    IAType type = pathType;
    if (type.getTypeTag() == ATypeTag.UNION) {
        type = ((AUnionType) type).getActualType();
    }
    ATypeTag tag = type.getTypeTag();
    if (tag == ATypeTag.ANY) {
        List<String> path = new ArrayList<>(parentPath);
        path.add(unresolvedVarName);
        varAccessCandidates.add(new Pair<>(expr, path));
    }
    if (tag == ATypeTag.OBJECT) {
        ARecordType recordType = (ARecordType) type;
        if (recordType.canContainField(unresolvedVarName)) {
            // If the field name is possible.
            List<String> path = new ArrayList<>(parentPath);
            path.add(unresolvedVarName);
            varAccessCandidates.add(new Pair<>(expr, path));
        } else {
            // Recursively identified possible paths.
            String[] fieldNames = recordType.getFieldNames();
            IAType[] fieldTypes = recordType.getFieldTypes();
            for (int index = 0; index < fieldNames.length; ++index) {
                List<String> path = new ArrayList<>(parentPath);
                path.add(fieldNames[index]);
                varAccessCandidates.addAll(findCandidatePathsForExpr(unresolvedVarName, fieldTypes[index], expr, path));
            }
        }
    }
    return varAccessCandidates;
}
Also used : ArrayList(java.util.ArrayList) AString(org.apache.asterix.om.base.AString) ATypeTag(org.apache.asterix.om.types.ATypeTag) ARecordType(org.apache.asterix.om.types.ARecordType) HashSet(java.util.HashSet) Pair(org.apache.hyracks.algebricks.common.utils.Pair) IAType(org.apache.asterix.om.types.IAType)

Aggregations

ARecordType (org.apache.asterix.om.types.ARecordType)105 IAType (org.apache.asterix.om.types.IAType)73 ArrayList (java.util.ArrayList)48 List (java.util.List)24 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)22 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)20 Dataset (org.apache.asterix.metadata.entities.Dataset)19 AString (org.apache.asterix.om.base.AString)19 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)19 Test (org.junit.Test)16 AsterixException (org.apache.asterix.common.exceptions.AsterixException)15 Index (org.apache.asterix.metadata.entities.Index)15 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)15 CompilationException (org.apache.asterix.common.exceptions.CompilationException)13 AOrderedListType (org.apache.asterix.om.types.AOrderedListType)13 Mutable (org.apache.commons.lang3.mutable.Mutable)13 IOException (java.io.IOException)12 MetadataException (org.apache.asterix.metadata.MetadataException)12 AUnionType (org.apache.asterix.om.types.AUnionType)11 Pair (org.apache.hyracks.algebricks.common.utils.Pair)10