use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression in project asterixdb by apache.
the class IntroduceAutogenerateIDRule method createNotNullFunction.
private ILogicalExpression createNotNullFunction(ILogicalExpression mergedRec) {
List<Mutable<ILogicalExpression>> args = new ArrayList<>();
args.add(new MutableObject<ILogicalExpression>(mergedRec));
AbstractFunctionCallExpression notNullFn = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.CHECK_UNKNOWN), args);
return notNullFn;
}
use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression in project asterixdb by apache.
the class IntroduceDynamicTypeCastForExternalFunctionRule method rewriteFunctionArgs.
private boolean rewriteFunctionArgs(ILogicalOperator op, Mutable<ILogicalExpression> expRef, IOptimizationContext context) throws AlgebricksException {
ILogicalExpression expr = expRef.getValue();
if (expr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL || !(expr instanceof ScalarFunctionCallExpression)) {
return false;
}
ScalarFunctionCallExpression funcCallExpr = (ScalarFunctionCallExpression) expr;
boolean changed = false;
IAType inputRecordType;
ARecordType requiredRecordType;
for (int iter1 = 0; iter1 < funcCallExpr.getArguments().size(); iter1++) {
inputRecordType = (IAType) op.computeOutputTypeEnvironment(context).getType(funcCallExpr.getArguments().get(iter1).getValue());
if (!(((ExternalScalarFunctionInfo) funcCallExpr.getFunctionInfo()).getArgumenTypes().get(iter1) instanceof ARecordType)) {
continue;
}
requiredRecordType = (ARecordType) ((ExternalScalarFunctionInfo) funcCallExpr.getFunctionInfo()).getArgumenTypes().get(iter1);
/**
* the input record type can be an union type
* for the case when it comes from a subplan or left-outer join
*/
boolean checkUnknown = false;
while (NonTaggedFormatUtil.isOptional(inputRecordType)) {
/** while-loop for the case there is a nested multi-level union */
inputRecordType = ((AUnionType) inputRecordType).getActualType();
checkUnknown = true;
}
boolean castFlag = !IntroduceDynamicTypeCastRule.compatible(requiredRecordType, inputRecordType);
if (castFlag || checkUnknown) {
AbstractFunctionCallExpression castFunc = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.CAST_TYPE));
castFunc.getArguments().add(funcCallExpr.getArguments().get(iter1));
TypeCastUtils.setRequiredAndInputTypes(castFunc, requiredRecordType, inputRecordType);
funcCallExpr.getArguments().set(iter1, new MutableObject<>(castFunc));
changed = changed || true;
}
}
return changed;
}
use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression in project asterixdb by apache.
the class IntroduceDynamicTypeCastForExternalFunctionRule method rewritePost.
@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
if (op.getOperatorTag() != LogicalOperatorTag.ASSIGN) {
return false;
}
AssignOperator assignOp = (AssignOperator) op;
ILogicalExpression assignExpr = assignOp.getExpressions().get(0).getValue();
if (assignExpr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
return false;
}
if (BuiltinFunctions.getBuiltinFunctionIdentifier(((AbstractFunctionCallExpression) assignExpr).getFunctionIdentifier()) != null) {
return false;
}
if (op.acceptExpressionTransform(exprRef -> rewriteFunctionArgs(op, exprRef, context))) {
return true;
} else {
return false;
}
}
use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression in project asterixdb by apache.
the class IntroduceDynamicTypeCastRule method rewritePost.
@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
// Depending on the operator type, we need to extract the following pieces of information.
AbstractLogicalOperator op;
ARecordType requiredRecordType;
LogicalVariable recordVar;
// We identify INSERT and DISTRIBUTE_RESULT operators.
AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue();
switch(op1.getOperatorTag()) {
case SINK:
case DELEGATE_OPERATOR:
{
/**
* pattern match: commit insert assign
* resulting plan: commit-insert-project-assign
*/
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) {
InsertDeleteUpsertOperator insertDeleteOp = (InsertDeleteUpsertOperator) op2;
if (insertDeleteOp.getOperation() == InsertDeleteUpsertOperator.Kind.DELETE) {
return false;
}
// Remember this is the operator we need to modify
op = insertDeleteOp;
// Derive the required ARecordType based on the schema of the DataSource
InsertDeleteUpsertOperator insertDeleteOperator = (InsertDeleteUpsertOperator) op2;
DataSource dataSource = (DataSource) insertDeleteOperator.getDataSource();
requiredRecordType = (ARecordType) dataSource.getItemType();
// Derive the Variable which we will potentially wrap with cast/null functions
ILogicalExpression expr = insertDeleteOperator.getPayloadExpression().getValue();
List<LogicalVariable> payloadVars = new ArrayList<>();
expr.getUsedVariables(payloadVars);
recordVar = payloadVars.get(0);
} else {
return false;
}
break;
}
case DISTRIBUTE_RESULT:
{
// First, see if there was an output-record-type specified
requiredRecordType = (ARecordType) op1.getAnnotations().get("output-record-type");
if (requiredRecordType == null) {
return false;
}
// Remember this is the operator we need to modify
op = op1;
recordVar = ((VariableReferenceExpression) ((DistributeResultOperator) op).getExpressions().get(0).getValue()).getVariableReference();
break;
}
default:
{
return false;
}
}
// Derive the statically-computed type of the record
IVariableTypeEnvironment env = op.computeOutputTypeEnvironment(context);
IAType inputRecordType = (IAType) env.getVarType(recordVar);
/** the input record type can be an union type -- for the case when it comes from a subplan or left-outer join */
boolean checkUnknown = false;
while (NonTaggedFormatUtil.isOptional(inputRecordType)) {
/** while-loop for the case there is a nested multi-level union */
inputRecordType = ((AUnionType) inputRecordType).getActualType();
checkUnknown = true;
}
/** see whether the input record type needs to be casted */
boolean cast = !compatible(requiredRecordType, inputRecordType);
if (checkUnknown) {
recordVar = addWrapperFunction(requiredRecordType, recordVar, op, context, BuiltinFunctions.CHECK_UNKNOWN);
}
if (cast) {
addWrapperFunction(requiredRecordType, recordVar, op, context, BuiltinFunctions.CAST_TYPE);
}
return cast || checkUnknown;
}
use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression in project asterixdb by apache.
the class IntroduceTransactionCommitByAssignOpRule method rewritePost.
@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
if (op.getOperatorTag() != LogicalOperatorTag.SELECT) {
return false;
}
SelectOperator selectOperator = (SelectOperator) op;
Mutable<ILogicalOperator> childOfSelect = selectOperator.getInputs().get(0);
//[Direction] SelectOp(cond1)<--ChildOps... ==> SelectOp(booleanValue of cond1)<--NewAssignOp(cond1)<--ChildOps...
//#. Create an assign-operator with a new local variable and the condition of the select-operator.
//#. Set the input(child operator) of the new assign-operator to input(child operator) of the select-operator.
// (Later, the newly created assign-operator will apply the condition on behalf of the select-operator,
// and set the variable of the assign-operator to a boolean value according to the condition evaluation.)
//#. Give the select-operator the result boolean value created by the newly created child assign-operator.
//create an assignOp with a variable and the condition of the select-operator.
LogicalVariable v = context.newVar();
AssignOperator assignOperator = new AssignOperator(v, new MutableObject<ILogicalExpression>(selectOperator.getCondition().getValue()));
//set the input of the new assign-operator to the input of the select-operator.
assignOperator.getInputs().add(childOfSelect);
//set the result value of the assign-operator to the condition of the select-operator
//scalarFunctionCallExpression);
selectOperator.getCondition().setValue(new VariableReferenceExpression(v));
selectOperator.getInputs().set(0, new MutableObject<ILogicalOperator>(assignOperator));
context.computeAndSetTypeEnvironmentForOperator(assignOperator);
//Once this rule is fired, don't apply again.
context.addToDontApplySet(this, selectOperator);
return true;
}
Aggregations