Search in sources :

Example 11 with ConstantExpression

use of org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression in project asterixdb by apache.

the class UnnestToDataScanRule method handleFunction.

protected boolean handleFunction(Mutable<ILogicalOperator> opRef, IOptimizationContext context, UnnestOperator unnest, AbstractFunctionCallExpression f) throws AlgebricksException {
    FunctionIdentifier fid = f.getFunctionIdentifier();
    if (fid.equals(BuiltinFunctions.DATASET)) {
        if (unnest.getPositionalVariable() != null) {
            // TODO remove this after enabling the support of positional variables in data scan
            throw new AlgebricksException("No positional variables are allowed over datasets.");
        }
        ILogicalExpression expr = f.getArguments().get(0).getValue();
        if (expr.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
            return false;
        }
        ConstantExpression ce = (ConstantExpression) expr;
        IAlgebricksConstantValue acv = ce.getValue();
        if (!(acv instanceof AsterixConstantValue)) {
            return false;
        }
        AsterixConstantValue acv2 = (AsterixConstantValue) acv;
        if (acv2.getObject().getType().getTypeTag() != ATypeTag.STRING) {
            return false;
        }
        String datasetArg = ((AString) acv2.getObject()).getStringValue();
        MetadataProvider metadataProvider = (MetadataProvider) context.getMetadataProvider();
        Pair<String, String> datasetReference = parseDatasetReference(metadataProvider, datasetArg);
        String dataverseName = datasetReference.first;
        String datasetName = datasetReference.second;
        Dataset dataset = metadataProvider.findDataset(dataverseName, datasetName);
        if (dataset == null) {
            throw new AlgebricksException("Could not find dataset " + datasetName + " in dataverse " + dataverseName);
        }
        DataSourceId asid = new DataSourceId(dataverseName, datasetName);
        List<LogicalVariable> variables = new ArrayList<>();
        if (dataset.getDatasetType() == DatasetType.INTERNAL) {
            int numPrimaryKeys = dataset.getPrimaryKeys().size();
            for (int i = 0; i < numPrimaryKeys; i++) {
                variables.add(context.newVar());
            }
        }
        variables.add(unnest.getVariable());
        DataSource dataSource = metadataProvider.findDataSource(asid);
        boolean hasMeta = dataSource.hasMeta();
        if (hasMeta) {
            variables.add(context.newVar());
        }
        DataSourceScanOperator scan = new DataSourceScanOperator(variables, dataSource);
        List<Mutable<ILogicalOperator>> scanInpList = scan.getInputs();
        scanInpList.addAll(unnest.getInputs());
        opRef.setValue(scan);
        addPrimaryKey(variables, dataSource, context);
        context.computeAndSetTypeEnvironmentForOperator(scan);
        // Adds equivalence classes --- one equivalent class between a primary key
        // variable and a record field-access expression.
        IAType[] schemaTypes = dataSource.getSchemaTypes();
        ARecordType recordType = (ARecordType) (hasMeta ? schemaTypes[schemaTypes.length - 2] : schemaTypes[schemaTypes.length - 1]);
        ARecordType metaRecordType = (ARecordType) (hasMeta ? schemaTypes[schemaTypes.length - 1] : null);
        EquivalenceClassUtils.addEquivalenceClassesForPrimaryIndexAccess(scan, variables, recordType, metaRecordType, dataset, context);
        return true;
    } else if (fid.equals(BuiltinFunctions.FEED_COLLECT)) {
        if (unnest.getPositionalVariable() != null) {
            throw new AlgebricksException("No positional variables are allowed over feeds.");
        }
        String dataverse = ConstantExpressionUtil.getStringArgument(f, 0);
        String sourceFeedName = ConstantExpressionUtil.getStringArgument(f, 1);
        String getTargetFeed = ConstantExpressionUtil.getStringArgument(f, 2);
        String subscriptionLocation = ConstantExpressionUtil.getStringArgument(f, 3);
        String targetDataset = ConstantExpressionUtil.getStringArgument(f, 4);
        String outputType = ConstantExpressionUtil.getStringArgument(f, 5);
        MetadataProvider metadataProvider = (MetadataProvider) context.getMetadataProvider();
        DataSourceId asid = new DataSourceId(dataverse, getTargetFeed);
        String policyName = metadataProvider.getConfig().get(FeedActivityDetails.FEED_POLICY_NAME);
        FeedPolicyEntity policy = metadataProvider.findFeedPolicy(dataverse, policyName);
        if (policy == null) {
            policy = BuiltinFeedPolicies.getFeedPolicy(policyName);
            if (policy == null) {
                throw new AlgebricksException("Unknown feed policy:" + policyName);
            }
        }
        ArrayList<LogicalVariable> feedDataScanOutputVariables = new ArrayList<>();
        String csLocations = metadataProvider.getConfig().get(FeedActivityDetails.COLLECT_LOCATIONS);
        List<LogicalVariable> pkVars = new ArrayList<>();
        FeedDataSource ds = createFeedDataSource(asid, targetDataset, sourceFeedName, subscriptionLocation, metadataProvider, policy, outputType, csLocations, unnest.getVariable(), context, pkVars);
        // The order for feeds is <Record-Meta-PK>
        feedDataScanOutputVariables.add(unnest.getVariable());
        // Does it produce meta?
        if (ds.hasMeta()) {
            feedDataScanOutputVariables.add(context.newVar());
        }
        // Does it produce pk?
        if (ds.isChange()) {
            feedDataScanOutputVariables.addAll(pkVars);
        }
        DataSourceScanOperator scan = new DataSourceScanOperator(feedDataScanOutputVariables, ds);
        List<Mutable<ILogicalOperator>> scanInpList = scan.getInputs();
        scanInpList.addAll(unnest.getInputs());
        opRef.setValue(scan);
        context.computeAndSetTypeEnvironmentForOperator(scan);
        return true;
    }
    return false;
}
Also used : ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) ArrayList(java.util.ArrayList) AString(org.apache.asterix.om.base.AString) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) DataSourceScanOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.DataSourceScanOperator) FeedPolicyEntity(org.apache.asterix.metadata.entities.FeedPolicyEntity) ArrayList(java.util.ArrayList) List(java.util.List) AString(org.apache.asterix.om.base.AString) LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) Dataset(org.apache.asterix.metadata.entities.Dataset) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) IAlgebricksConstantValue(org.apache.hyracks.algebricks.core.algebra.expressions.IAlgebricksConstantValue) FeedDataSource(org.apache.asterix.metadata.declared.FeedDataSource) DataSource(org.apache.asterix.metadata.declared.DataSource) FeedDataSource(org.apache.asterix.metadata.declared.FeedDataSource) FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) MetadataProvider(org.apache.asterix.metadata.declared.MetadataProvider) ARecordType(org.apache.asterix.om.types.ARecordType) DataSourceId(org.apache.asterix.metadata.declared.DataSourceId) IAType(org.apache.asterix.om.types.IAType)

Example 12 with ConstantExpression

use of org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression in project asterixdb by apache.

the class ConstantExpressionUtil method getConstantIaObject.

public static IAObject getConstantIaObject(ILogicalExpression expr, ATypeTag typeTag) {
    if (expr.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
        return null;
    }
    final IAlgebricksConstantValue acv = ((ConstantExpression) expr).getValue();
    if (!(acv instanceof AsterixConstantValue)) {
        return null;
    }
    final IAObject iaObject = ((AsterixConstantValue) acv).getObject();
    if (typeTag != null) {
        return iaObject.getType().getTypeTag() == typeTag ? iaObject : null;
    } else {
        return iaObject;
    }
}
Also used : AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) IAObject(org.apache.asterix.om.base.IAObject) IAlgebricksConstantValue(org.apache.hyracks.algebricks.core.algebra.expressions.IAlgebricksConstantValue)

Example 13 with ConstantExpression

use of org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression in project asterixdb by apache.

the class RecordRemoveFieldsTypeComputer method getPathFromConstantExpression.

private void getPathFromConstantExpression(String funcName, ILogicalExpression expression, Set<String> fieldNameSet, List<List<String>> pathList) throws AlgebricksException {
    ConstantExpression ce = (ConstantExpression) expression;
    if (!(ce.getValue() instanceof AsterixConstantValue)) {
        throw new InvalidExpressionException(funcName, 1, ce, LogicalExpressionTag.CONSTANT);
    }
    IAObject item = ((AsterixConstantValue) ce.getValue()).getObject();
    ATypeTag type = item.getType().getTypeTag();
    switch(type) {
        case STRING:
            String fn = ((AString) item).getStringValue();
            fieldNameSet.add(fn);
            break;
        case ARRAY:
            AOrderedList pathOrdereList = (AOrderedList) item;
            String fieldName = ((AString) pathOrdereList.getItem(0)).getStringValue();
            fieldNameSet.add(fieldName);
            List<String> path = new ArrayList<>();
            for (int i = 0; i < pathOrdereList.size(); i++) {
                path.add(((AString) pathOrdereList.getItem(i)).getStringValue());
            }
            pathList.add(path);
            break;
        default:
            throw new UnsupportedTypeException(funcName, type);
    }
}
Also used : AOrderedList(org.apache.asterix.om.base.AOrderedList) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) ATypeTag(org.apache.asterix.om.types.ATypeTag) InvalidExpressionException(org.apache.asterix.om.exceptions.InvalidExpressionException) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) IAObject(org.apache.asterix.om.base.IAObject) ArrayList(java.util.ArrayList) UnsupportedTypeException(org.apache.asterix.om.exceptions.UnsupportedTypeException) AString(org.apache.asterix.om.base.AString) AString(org.apache.asterix.om.base.AString)

Example 14 with ConstantExpression

use of org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression in project asterixdb by apache.

the class IntroduceAutogenerateIDRule method createPrimaryKeyRecordExpression.

private AbstractFunctionCallExpression createPrimaryKeyRecordExpression(List<String> pkFieldName) {
    //Create lowest level of nested uuid
    AbstractFunctionCallExpression uuidFn = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.CREATE_UUID));
    List<Mutable<ILogicalExpression>> openRecordConsArgs = new ArrayList<>();
    Mutable<ILogicalExpression> pkFieldNameExpression = new MutableObject<ILogicalExpression>(new ConstantExpression(new AsterixConstantValue(new AString(pkFieldName.get(pkFieldName.size() - 1)))));
    openRecordConsArgs.add(pkFieldNameExpression);
    Mutable<ILogicalExpression> pkFieldValueExpression = new MutableObject<ILogicalExpression>(uuidFn);
    openRecordConsArgs.add(pkFieldValueExpression);
    AbstractFunctionCallExpression openRecFn = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.OPEN_RECORD_CONSTRUCTOR), openRecordConsArgs);
    //Create higher levels
    for (int i = pkFieldName.size() - 2; i > -1; i--) {
        AString fieldName = new AString(pkFieldName.get(i));
        openRecordConsArgs = new ArrayList<>();
        openRecordConsArgs.add(new MutableObject<ILogicalExpression>(new ConstantExpression(new AsterixConstantValue(fieldName))));
        openRecordConsArgs.add(new MutableObject<ILogicalExpression>(openRecFn));
        openRecFn = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.OPEN_RECORD_CONSTRUCTOR), openRecordConsArgs);
    }
    return openRecFn;
}
Also used : AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) ArrayList(java.util.ArrayList) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) AString(org.apache.asterix.om.base.AString) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression) MutableObject(org.apache.commons.lang3.mutable.MutableObject)

Example 15 with ConstantExpression

use of org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression in project asterixdb by apache.

the class LoadRecordFieldsRule method findAndEliminateRedundantFieldAccess.

/**
     * Rewrite
     * assign $x := field-access($y, "field")
     * assign $y := record-constructor { "field": Expr, ... }
     * into
     * assign $x := Expr
     * assign $y := record-constructor { "field": Expr, ... }
     */
private static boolean findAndEliminateRedundantFieldAccess(AssignOperator assign, IOptimizationContext context) throws AlgebricksException {
    ILogicalExpression expr = getFirstExpr(assign);
    AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) expr;
    ILogicalExpression arg0 = f.getArguments().get(0).getValue();
    if (arg0.getExpressionTag() != LogicalExpressionTag.VARIABLE) {
        return false;
    }
    VariableReferenceExpression vre = (VariableReferenceExpression) arg0;
    LogicalVariable recordVar = vre.getVariableReference();
    ILogicalExpression arg1 = f.getArguments().get(1).getValue();
    if (arg1.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
        return false;
    }
    IVariableTypeEnvironment typeEnvironment = context.getOutputTypeEnvironment(assign);
    ConstantExpression ce = (ConstantExpression) arg1;
    ILogicalExpression fldExpr;
    if (f.getFunctionIdentifier().equals(BuiltinFunctions.FIELD_ACCESS_BY_NAME)) {
        String fldName = ((AString) ((AsterixConstantValue) ce.getValue()).getObject()).getStringValue();
        fldExpr = findFieldExpression(assign, recordVar, fldName, typeEnvironment, (name, expression, env) -> findFieldByNameFromRecordConstructor(name, expression));
    } else if (f.getFunctionIdentifier().equals(BuiltinFunctions.FIELD_ACCESS_BY_INDEX)) {
        Integer fldIdx = ((AInt32) ((AsterixConstantValue) ce.getValue()).getObject()).getIntegerValue();
        fldExpr = findFieldExpression(assign, recordVar, fldIdx, typeEnvironment, LoadRecordFieldsRule::findFieldByIndexFromRecordConstructor);
    } else if (f.getFunctionIdentifier().equals(BuiltinFunctions.FIELD_ACCESS_NESTED)) {
        return false;
    } else {
        throw new IllegalStateException();
    }
    if (fldExpr == null) {
        return false;
    }
    // check the liveness of the new expression
    List<LogicalVariable> usedVariables = new ArrayList<>();
    fldExpr.getUsedVariables(usedVariables);
    List<LogicalVariable> liveInputVars = new ArrayList<>();
    VariableUtilities.getLiveVariables(assign, liveInputVars);
    usedVariables.removeAll(liveInputVars);
    if (usedVariables.isEmpty()) {
        assign.getExpressions().get(0).setValue(fldExpr);
        return true;
    } else {
        return false;
    }
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) OperatorPropertiesUtil(org.apache.hyracks.algebricks.core.algebra.util.OperatorPropertiesUtil) IOptimizationContext(org.apache.hyracks.algebricks.core.algebra.base.IOptimizationContext) NestedTupleSourceOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.NestedTupleSourceOperator) AbstractOperatorWithNestedPlans(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractOperatorWithNestedPlans) ConstantExpressionUtil(org.apache.asterix.om.utils.ConstantExpressionUtil) LogicalExpressionTag(org.apache.hyracks.algebricks.core.algebra.base.LogicalExpressionTag) AbstractLogicalExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractLogicalExpression) VariableUtilities(org.apache.hyracks.algebricks.core.algebra.operators.logical.visitors.VariableUtilities) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ILogicalExpressionReferenceTransform(org.apache.hyracks.algebricks.core.algebra.visitors.ILogicalExpressionReferenceTransform) OperatorAnnotation(org.apache.asterix.algebra.base.OperatorAnnotation) ARecordType(org.apache.asterix.om.types.ARecordType) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) AlgebricksBuiltinFunctions(org.apache.hyracks.algebricks.core.algebra.functions.AlgebricksBuiltinFunctions) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan) IAlgebraicRewriteRule(org.apache.hyracks.algebricks.core.rewriter.base.IAlgebraicRewriteRule) FunctionalDependency(org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency) LinkedList(java.util.LinkedList) BuiltinFunctions(org.apache.asterix.om.functions.BuiltinFunctions) MutableObject(org.apache.commons.lang3.mutable.MutableObject) LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AString(org.apache.asterix.om.base.AString) Iterator(java.util.Iterator) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) LogicalOperatorTag(org.apache.hyracks.algebricks.core.algebra.base.LogicalOperatorTag) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) AInt32(org.apache.asterix.om.base.AInt32) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) List(java.util.List) AnalysisUtil(org.apache.asterix.optimizer.base.AnalysisUtil) IVariableTypeEnvironment(org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) SelectOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator) Mutable(org.apache.commons.lang3.mutable.Mutable) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) ArrayList(java.util.ArrayList) AString(org.apache.asterix.om.base.AString) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) AString(org.apache.asterix.om.base.AString) IVariableTypeEnvironment(org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment)

Aggregations

ConstantExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression)43 AsterixConstantValue (org.apache.asterix.om.constants.AsterixConstantValue)41 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)36 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)23 ArrayList (java.util.ArrayList)22 AString (org.apache.asterix.om.base.AString)22 Mutable (org.apache.commons.lang3.mutable.Mutable)22 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)21 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)20 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)19 AssignOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)15 AInt32 (org.apache.asterix.om.base.AInt32)14 MutableObject (org.apache.commons.lang3.mutable.MutableObject)13 IAObject (org.apache.asterix.om.base.IAObject)11 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)10 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)10 AOrderedList (org.apache.asterix.om.base.AOrderedList)9 IAType (org.apache.asterix.om.types.IAType)9 Pair (org.apache.hyracks.algebricks.common.utils.Pair)9 ARecordType (org.apache.asterix.om.types.ARecordType)7