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;
}
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);
}
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;
}
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;
}
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;
}
}
Aggregations