Search in sources :

Example 6 with InsertDeleteUpsertOperator

use of org.apache.hyracks.algebricks.core.algebra.operators.logical.InsertDeleteUpsertOperator in project asterixdb by apache.

the class SetupCommitExtensionOpRule method rewritePost.

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
    if (op.getOperatorTag() != LogicalOperatorTag.DELEGATE_OPERATOR) {
        return false;
    }
    DelegateOperator eOp = (DelegateOperator) op;
    if (!(eOp.getDelegate() instanceof CommitOperator)) {
        return false;
    }
    boolean isSink = ((CommitOperator) eOp.getDelegate()).isSink();
    List<Mutable<ILogicalExpression>> primaryKeyExprs = null;
    Dataset dataset = null;
    AbstractLogicalOperator descendantOp = (AbstractLogicalOperator) eOp.getInputs().get(0).getValue();
    while (descendantOp != null) {
        if (descendantOp.getOperatorTag() == LogicalOperatorTag.INDEX_INSERT_DELETE_UPSERT) {
            IndexInsertDeleteUpsertOperator operator = (IndexInsertDeleteUpsertOperator) descendantOp;
            if (!operator.isBulkload() && operator.getPrevSecondaryKeyExprs() == null) {
                primaryKeyExprs = operator.getPrimaryKeyExpressions();
                dataset = ((DatasetDataSource) operator.getDataSourceIndex().getDataSource()).getDataset();
                break;
            }
        } else if (descendantOp.getOperatorTag() == LogicalOperatorTag.INSERT_DELETE_UPSERT) {
            InsertDeleteUpsertOperator insertDeleteUpsertOperator = (InsertDeleteUpsertOperator) descendantOp;
            if (!insertDeleteUpsertOperator.isBulkload()) {
                primaryKeyExprs = insertDeleteUpsertOperator.getPrimaryKeyExpressions();
                dataset = ((DatasetDataSource) insertDeleteUpsertOperator.getDataSource()).getDataset();
                break;
            }
        }
        if (descendantOp.getInputs().isEmpty()) {
            break;
        }
        descendantOp = (AbstractLogicalOperator) descendantOp.getInputs().get(0).getValue();
    }
    if (primaryKeyExprs == null) {
        return false;
    }
    //copy primaryKeyExprs
    List<LogicalVariable> primaryKeyLogicalVars = new ArrayList<>();
    for (Mutable<ILogicalExpression> expr : primaryKeyExprs) {
        VariableReferenceExpression varRefExpr = (VariableReferenceExpression) expr.getValue();
        primaryKeyLogicalVars.add(new LogicalVariable(varRefExpr.getVariableReference().getId()));
    }
    //get JobId(TransactorId)
    MetadataProvider mp = (MetadataProvider) context.getMetadataProvider();
    JobId jobId = mp.getJobId();
    //create the logical and physical operator
    CommitOperator commitOperator = new CommitOperator(primaryKeyLogicalVars, isSink);
    CommitPOperator commitPOperator = new CommitPOperator(jobId, dataset, primaryKeyLogicalVars, isSink);
    commitOperator.setPhysicalOperator(commitPOperator);
    //create ExtensionOperator and put the commitOperator in it.
    DelegateOperator extensionOperator = new DelegateOperator(commitOperator);
    extensionOperator.setPhysicalOperator(commitPOperator);
    //update plan link
    extensionOperator.getInputs().add(eOp.getInputs().get(0));
    context.computeAndSetTypeEnvironmentForOperator(extensionOperator);
    opRef.setValue(extensionOperator);
    return true;
}
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) ArrayList(java.util.ArrayList) IndexInsertDeleteUpsertOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.IndexInsertDeleteUpsertOperator) DatasetDataSource(org.apache.asterix.metadata.declared.DatasetDataSource) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) IndexInsertDeleteUpsertOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.IndexInsertDeleteUpsertOperator) InsertDeleteUpsertOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.InsertDeleteUpsertOperator) MetadataProvider(org.apache.asterix.metadata.declared.MetadataProvider) DelegateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.DelegateOperator) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) CommitPOperator(org.apache.asterix.algebra.operators.physical.CommitPOperator) CommitOperator(org.apache.asterix.algebra.operators.CommitOperator) JobId(org.apache.asterix.common.transactions.JobId)

Example 7 with InsertDeleteUpsertOperator

use of org.apache.hyracks.algebricks.core.algebra.operators.logical.InsertDeleteUpsertOperator in project asterixdb by apache.

the class BulkloadPOperator method contributeRuntimeOperator.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void contributeRuntimeOperator(IHyracksJobBuilder builder, JobGenContext context, ILogicalOperator op, IOperatorSchema propagatedSchema, IOperatorSchema[] inputSchemas, IOperatorSchema outerPlanSchema) throws AlgebricksException {
    InsertDeleteUpsertOperator insertDeleteOp = (InsertDeleteUpsertOperator) op;
    assert insertDeleteOp.getOperation() == Kind.INSERT;
    assert insertDeleteOp.isBulkload();
    IMetadataProvider mp = context.getMetadataProvider();
    IVariableTypeEnvironment typeEnv = context.getTypeEnvironment(op);
    JobSpecification spec = builder.getJobSpec();
    RecordDescriptor inputDesc = JobGenHelper.mkRecordDescriptor(context.getTypeEnvironment(op.getInputs().get(0).getValue()), inputSchemas[0], context);
    Pair<IOperatorDescriptor, AlgebricksPartitionConstraint> runtimeAndConstraints = mp.getInsertRuntime(dataSource, propagatedSchema, typeEnv, primaryKeys, payload, additionalFilteringKeys, additionalNonFilterVars, inputDesc, context, spec, true);
    builder.contributeHyracksOperator(insertDeleteOp, runtimeAndConstraints.first);
    builder.contributeAlgebricksPartitionConstraint(runtimeAndConstraints.first, runtimeAndConstraints.second);
    ILogicalOperator src = insertDeleteOp.getInputs().get(0).getValue();
    builder.contributeGraphEdge(src, 0, insertDeleteOp, 0);
}
Also used : InsertDeleteUpsertOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.InsertDeleteUpsertOperator) RecordDescriptor(org.apache.hyracks.api.dataflow.value.RecordDescriptor) IOperatorDescriptor(org.apache.hyracks.api.dataflow.IOperatorDescriptor) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) AlgebricksPartitionConstraint(org.apache.hyracks.algebricks.common.constraints.AlgebricksPartitionConstraint) JobSpecification(org.apache.hyracks.api.job.JobSpecification) IMetadataProvider(org.apache.hyracks.algebricks.core.algebra.metadata.IMetadataProvider) IVariableTypeEnvironment(org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment)

Example 8 with InsertDeleteUpsertOperator

use of org.apache.hyracks.algebricks.core.algebra.operators.logical.InsertDeleteUpsertOperator in project asterixdb by apache.

the class IsomorphismOperatorVisitor method visitInsertDeleteUpsertOperator.

@Override
public Boolean visitInsertDeleteUpsertOperator(InsertDeleteUpsertOperator op, ILogicalOperator arg) throws AlgebricksException {
    AbstractLogicalOperator aop = (AbstractLogicalOperator) arg;
    if (aop.getOperatorTag() != LogicalOperatorTag.INSERT_DELETE_UPSERT) {
        return Boolean.FALSE;
    }
    InsertDeleteUpsertOperator insertOpArg = (InsertDeleteUpsertOperator) copyAndSubstituteVar(op, arg);
    boolean isomorphic = VariableUtilities.varListEqualUnordered(op.getSchema(), insertOpArg.getSchema());
    if (!op.getDataSource().equals(insertOpArg.getDataSource())) {
        isomorphic = false;
    }
    if (!op.getPayloadExpression().equals(insertOpArg.getPayloadExpression())) {
        isomorphic = false;
    }
    return isomorphic;
}
Also used : AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) IndexInsertDeleteUpsertOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.IndexInsertDeleteUpsertOperator) InsertDeleteUpsertOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.InsertDeleteUpsertOperator)

Example 9 with InsertDeleteUpsertOperator

use of org.apache.hyracks.algebricks.core.algebra.operators.logical.InsertDeleteUpsertOperator in project asterixdb by apache.

the class IntroduceStaticTypeCastForInsertRule method rewritePost.

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    /**
         * pattern match: sink/insert/assign record type is propagated from
         * insert data source to the record-constructor expression
         */
    if (context.checkIfInDontApplySet(this, opRef.getValue())) {
        return false;
    }
    context.addToDontApplySet(this, opRef.getValue());
    AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue();
    List<LogicalVariable> producedVariables = new ArrayList<LogicalVariable>();
    LogicalVariable oldRecordVariable;
    if (op1.getOperatorTag() != LogicalOperatorTag.DELEGATE_OPERATOR && op1.getOperatorTag() != LogicalOperatorTag.SINK) {
        return false;
    }
    if (op1.getOperatorTag() == LogicalOperatorTag.DELEGATE_OPERATOR) {
        DelegateOperator eOp = (DelegateOperator) op1;
        if (!(eOp.getDelegate() instanceof CommitOperator)) {
            return false;
        }
    }
    AbstractLogicalOperator op2 = (AbstractLogicalOperator) op1.getInputs().get(0).getValue();
    if (op2.getOperatorTag() != LogicalOperatorTag.INSERT_DELETE_UPSERT) {
        return false;
    }
    InsertDeleteUpsertOperator insertDeleteOp = (InsertDeleteUpsertOperator) op2;
    if (insertDeleteOp.getOperation() == InsertDeleteUpsertOperator.Kind.DELETE) {
        return false;
    }
    /**
         * get required record type
         */
    InsertDeleteUpsertOperator insertDeleteOperator = (InsertDeleteUpsertOperator) op2;
    DataSource dataSource = (DataSource) insertDeleteOperator.getDataSource();
    IAType requiredRecordType = dataSource.getItemType();
    List<LogicalVariable> usedVariables = new ArrayList<LogicalVariable>();
    insertDeleteOperator.getPayloadExpression().getValue().getUsedVariables(usedVariables);
    // empty
    if (usedVariables.size() == 0) {
        return false;
    }
    oldRecordVariable = usedVariables.get(0);
    LogicalVariable inputRecordVar = usedVariables.get(0);
    IVariableTypeEnvironment env = insertDeleteOperator.computeOutputTypeEnvironment(context);
    IAType inputRecordType = (IAType) env.getVarType(inputRecordVar);
    AbstractLogicalOperator currentOperator = (AbstractLogicalOperator) op2.getInputs().get(0).getValue();
    /**
         * find the assign operator for the "input record" to the insert_delete
         * operator
         */
    do {
        context.addToDontApplySet(this, currentOperator);
        if (currentOperator.getOperatorTag() == LogicalOperatorTag.ASSIGN) {
            AssignOperator assignOp = (AssignOperator) currentOperator;
            producedVariables.clear();
            VariableUtilities.getProducedVariables(currentOperator, producedVariables);
            int position = producedVariables.indexOf(oldRecordVariable);
            /**
                 * set the top-down propagated type
                 */
            if (position >= 0) {
                AssignOperator originalAssign = (AssignOperator) currentOperator;
                List<Mutable<ILogicalExpression>> expressionRefs = originalAssign.getExpressions();
                ILogicalExpression expr = expressionRefs.get(position).getValue();
                if (expr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
                    AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) expr;
                    // fail but just return false
                    if (TypeCastUtils.getRequiredType(funcExpr) != null) {
                        context.computeAndSetTypeEnvironmentForOperator(assignOp);
                        return false;
                    }
                    IVariableTypeEnvironment assignEnv = assignOp.computeOutputTypeEnvironment(context);
                    StaticTypeCastUtil.rewriteFuncExpr(funcExpr, requiredRecordType, inputRecordType, assignEnv);
                }
                context.computeAndSetTypeEnvironmentForOperator(originalAssign);
            }
        }
        if (currentOperator.getInputs().size() > 0) {
            currentOperator = (AbstractLogicalOperator) currentOperator.getInputs().get(0).getValue();
        } else {
            break;
        }
    } while (currentOperator != null);
    return true;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ArrayList(java.util.ArrayList) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) DataSource(org.apache.asterix.metadata.declared.DataSource) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) InsertDeleteUpsertOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.InsertDeleteUpsertOperator) DelegateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.DelegateOperator) CommitOperator(org.apache.asterix.algebra.operators.CommitOperator) IVariableTypeEnvironment(org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment) IAType(org.apache.asterix.om.types.IAType)

Example 10 with InsertDeleteUpsertOperator

use of org.apache.hyracks.algebricks.core.algebra.operators.logical.InsertDeleteUpsertOperator in project asterixdb by apache.

the class IntroduceMaterializationForInsertWithSelfScanRule method rewritePost.

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
    if (op.getOperatorTag() != LogicalOperatorTag.INSERT_DELETE_UPSERT) {
        return false;
    }
    InsertDeleteUpsertOperator insertOp = (InsertDeleteUpsertOperator) op;
    boolean sameDataset = checkIfInsertAndScanDatasetsSame(op, ((DatasetDataSource) insertOp.getDataSource()).getDataset().getDatasetName());
    if (sameDataset) {
        MaterializeOperator materializeOperator = new MaterializeOperator();
        MaterializePOperator materializePOperator = new MaterializePOperator(true);
        materializeOperator.setPhysicalOperator(materializePOperator);
        materializeOperator.getInputs().add(new MutableObject<ILogicalOperator>(insertOp.getInputs().get(0).getValue()));
        context.computeAndSetTypeEnvironmentForOperator(materializeOperator);
        insertOp.getInputs().clear();
        insertOp.getInputs().add(new MutableObject<ILogicalOperator>(materializeOperator));
        context.computeAndSetTypeEnvironmentForOperator(insertOp);
        return true;
    } else {
        return false;
    }
}
Also used : MaterializeOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.MaterializeOperator) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) InsertDeleteUpsertOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.InsertDeleteUpsertOperator) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) DatasetDataSource(org.apache.asterix.metadata.declared.DatasetDataSource) MaterializePOperator(org.apache.hyracks.algebricks.core.algebra.operators.physical.MaterializePOperator)

Aggregations

InsertDeleteUpsertOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.InsertDeleteUpsertOperator)16 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)11 CommitOperator (org.apache.asterix.algebra.operators.CommitOperator)10 DelegateOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.DelegateOperator)10 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)9 ArrayList (java.util.ArrayList)8 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)8 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)8 Mutable (org.apache.commons.lang3.mutable.Mutable)7 AbstractLogicalOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)7 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)6 AssignOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)6 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)5 DatasetDataSource (org.apache.asterix.metadata.declared.DatasetDataSource)4 IAType (org.apache.asterix.om.types.IAType)4 MutableObject (org.apache.commons.lang3.mutable.MutableObject)4 IVariableTypeEnvironment (org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment)4 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)4 IndexInsertDeleteUpsertOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.IndexInsertDeleteUpsertOperator)4 List (java.util.List)3