use of org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression in project asterixdb by apache.
the class IntroduceSecondaryIndexInsertDeleteRule method getRecordVar.
private LogicalVariable getRecordVar(IOptimizationContext context, AbstractLogicalOperator inputOp, ILogicalExpression recordExpr, int expectedRecordIndex) throws AlgebricksException {
if (exprIsRecord(context.getOutputTypeEnvironment(inputOp), recordExpr)) {
return ((VariableReferenceExpression) recordExpr).getVariableReference();
} else {
/**
* For the case primary key-assignment expressions are constant
* expressions, find assign op that creates record to be
* inserted/deleted.
*/
FunctionIdentifier fid = null;
AbstractLogicalOperator currentInputOp = inputOp;
while (fid != BuiltinFunctions.OPEN_RECORD_CONSTRUCTOR) {
if (currentInputOp.getInputs().isEmpty()) {
return null;
}
currentInputOp = (AbstractLogicalOperator) currentInputOp.getInputs().get(0).getValue();
if (currentInputOp.getOperatorTag() != LogicalOperatorTag.ASSIGN) {
continue;
}
AssignOperator assignOp = (AssignOperator) currentInputOp;
ILogicalExpression assignExpr = assignOp.getExpressions().get(expectedRecordIndex).getValue();
if (assignExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
ScalarFunctionCallExpression funcExpr = (ScalarFunctionCallExpression) assignOp.getExpressions().get(expectedRecordIndex).getValue();
fid = funcExpr.getFunctionIdentifier();
}
}
return ((AssignOperator) currentInputOp).getVariables().get(0);
}
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression in project asterixdb by apache.
the class IntroduceDynamicTypeCastRule method addWrapperFunction.
/**
* Inject a function to wrap a variable when necessary
*
* @param requiredRecordType
* the required record type
* @param recordVar
* the record variable
* @param parent
* the current parent operator to be rewritten
* @param context
* the optimization context
* @param fd
* the function to be injected
* @return true if cast is injected; false otherwise.
* @throws AlgebricksException
*/
public static LogicalVariable addWrapperFunction(ARecordType requiredRecordType, LogicalVariable recordVar, ILogicalOperator parent, IOptimizationContext context, FunctionIdentifier fd) throws AlgebricksException {
List<Mutable<ILogicalOperator>> opRefs = parent.getInputs();
for (int index = 0; index < opRefs.size(); index++) {
Mutable<ILogicalOperator> opRef = opRefs.get(index);
ILogicalOperator op = opRef.getValue();
/** get produced vars */
List<LogicalVariable> producedVars = new ArrayList<LogicalVariable>();
VariableUtilities.getProducedVariables(op, producedVars);
IVariableTypeEnvironment env = op.computeOutputTypeEnvironment(context);
for (int i = 0; i < producedVars.size(); i++) {
LogicalVariable var = producedVars.get(i);
if (var.equals(recordVar)) {
/** insert an assign operator to call the function on-top-of the variable */
IAType actualType = (IAType) env.getVarType(var);
AbstractFunctionCallExpression cast = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(fd));
cast.getArguments().add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(var)));
/** enforce the required record type */
TypeCastUtils.setRequiredAndInputTypes(cast, requiredRecordType, actualType);
LogicalVariable newAssignVar = context.newVar();
AssignOperator newAssignOperator = new AssignOperator(newAssignVar, new MutableObject<ILogicalExpression>(cast));
newAssignOperator.getInputs().add(new MutableObject<ILogicalOperator>(op));
opRef.setValue(newAssignOperator);
context.computeAndSetTypeEnvironmentForOperator(newAssignOperator);
newAssignOperator.computeOutputTypeEnvironment(context);
VariableUtilities.substituteVariables(parent, recordVar, newAssignVar, context);
return newAssignVar;
}
}
/** recursive descend to the operator who produced the recordVar */
LogicalVariable replacedVar = addWrapperFunction(requiredRecordType, recordVar, op, context, fd);
if (replacedVar != null) {
/** substitute the recordVar by the replacedVar for operators who uses recordVar */
VariableUtilities.substituteVariables(parent, recordVar, replacedVar, context);
return replacedVar;
}
}
return null;
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression in project asterixdb by apache.
the class AccessMethodUtils method createExternalDataLookupUnnestMap.
public static UnnestMapOperator createExternalDataLookupUnnestMap(AbstractDataSourceOperator dataSourceOp, Dataset dataset, ARecordType recordType, ILogicalOperator inputOp, IOptimizationContext context, boolean retainInput, boolean retainNull) throws AlgebricksException {
List<LogicalVariable> primaryKeyVars = AccessMethodUtils.getPrimaryKeyVarsFromSecondaryUnnestMap(dataset, inputOp);
// add a sort on the RID fields before fetching external data.
OrderOperator order = new OrderOperator();
for (LogicalVariable pkVar : primaryKeyVars) {
Mutable<ILogicalExpression> vRef = new MutableObject<>(new VariableReferenceExpression(pkVar));
order.getOrderExpressions().add(new Pair<>(OrderOperator.ASC_ORDER, vRef));
}
// The secondary-index search feeds into the sort.
order.getInputs().add(new MutableObject<>(inputOp));
order.setExecutionMode(ExecutionMode.LOCAL);
context.computeAndSetTypeEnvironmentForOperator(order);
List<Mutable<ILogicalExpression>> externalLookupArgs = new ArrayList<>();
//Add dataverse to the arguments
AccessMethodUtils.addStringArg(dataset.getDataverseName(), externalLookupArgs);
//Add dataset to the arguments
AccessMethodUtils.addStringArg(dataset.getDatasetName(), externalLookupArgs);
//Add PK vars to the arguments
AccessMethodUtils.writeVarList(primaryKeyVars, externalLookupArgs);
// Variables and types coming out of the external access.
List<LogicalVariable> externalUnnestVars = new ArrayList<>();
List<Object> outputTypes = new ArrayList<>();
// Append output variables/types generated by the data scan (not forwarded from input).
externalUnnestVars.addAll(dataSourceOp.getVariables());
appendExternalRecTypes(dataset, recordType, outputTypes);
IFunctionInfo externalLookup = FunctionUtil.getFunctionInfo(BuiltinFunctions.EXTERNAL_LOOKUP);
AbstractFunctionCallExpression externalLookupFunc = new ScalarFunctionCallExpression(externalLookup, externalLookupArgs);
UnnestMapOperator unnestOp = new UnnestMapOperator(externalUnnestVars, new MutableObject<ILogicalExpression>(externalLookupFunc), outputTypes, retainInput);
// Fed by the order operator or the secondaryIndexUnnestOp.
unnestOp.getInputs().add(new MutableObject<ILogicalOperator>(order));
context.computeAndSetTypeEnvironmentForOperator(unnestOp);
unnestOp.setExecutionMode(ExecutionMode.PARTITIONED);
//set the physical operator
DataSourceId dataSourceId = new DataSourceId(dataset.getDataverseName(), dataset.getDatasetName());
unnestOp.setPhysicalOperator(new ExternalDataLookupPOperator(dataSourceId, dataset, recordType, primaryKeyVars, false, retainInput, retainNull));
return unnestOp;
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression in project asterixdb by apache.
the class AccessMethodUtils method resetLOJNullPlaceholderVariableInGroupByOp.
public static void resetLOJNullPlaceholderVariableInGroupByOp(AccessMethodAnalysisContext analysisCtx, LogicalVariable newNullPlaceholderVaraible, IOptimizationContext context) throws AlgebricksException {
//reset the null placeholder variable in groupby operator
ScalarFunctionCallExpression isNullFuncExpr = analysisCtx.getLOJIsNullFuncInGroupBy();
isNullFuncExpr.getArguments().clear();
isNullFuncExpr.getArguments().add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(newNullPlaceholderVaraible)));
//recompute type environment.
OperatorPropertiesUtil.typeOpRec(analysisCtx.getLOJGroupbyOpRef(), context);
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression in project asterixdb by apache.
the class ResolveVariableRule method resolveAsFieldAccess.
// Resolves a "resolve" function call as a field access.
private void resolveAsFieldAccess(Mutable<ILogicalExpression> funcRef, Pair<ILogicalExpression, List<String>> varAndPath) {
// Rewrites to field-access-by-names.
ILogicalExpression expr = varAndPath.first;
List<String> path = varAndPath.second;
Mutable<ILogicalExpression> firstArgRef = new MutableObject<>(expr);
ILogicalExpression newFunc = null;
for (String fieldName : path) {
List<Mutable<ILogicalExpression>> args = new ArrayList<>();
args.add(firstArgRef);
args.add(new MutableObject<>(new ConstantExpression(new AsterixConstantValue(new AString(fieldName)))));
newFunc = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.FIELD_ACCESS_BY_NAME), args);
firstArgRef = new MutableObject<>(newFunc);
}
funcRef.setValue(newFunc);
}
Aggregations