use of org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment in project asterixdb by apache.
the class StreamLimitPOperator method contributeRuntimeOperator.
@Override
public void contributeRuntimeOperator(IHyracksJobBuilder builder, JobGenContext context, ILogicalOperator op, IOperatorSchema propagatedSchema, IOperatorSchema[] inputSchemas, IOperatorSchema outerPlanSchema) throws AlgebricksException {
LimitOperator limit = (LimitOperator) op;
IExpressionRuntimeProvider expressionRuntimeProvider = context.getExpressionRuntimeProvider();
IVariableTypeEnvironment env = context.getTypeEnvironment(op);
IScalarEvaluatorFactory maxObjectsFact = expressionRuntimeProvider.createEvaluatorFactory(limit.getMaxObjects().getValue(), env, inputSchemas, context);
ILogicalExpression offsetExpr = limit.getOffset().getValue();
IScalarEvaluatorFactory offsetFact = (offsetExpr == null) ? null : expressionRuntimeProvider.createEvaluatorFactory(offsetExpr, env, inputSchemas, context);
RecordDescriptor recDesc = JobGenHelper.mkRecordDescriptor(context.getTypeEnvironment(op), propagatedSchema, context);
StreamLimitRuntimeFactory runtime = new StreamLimitRuntimeFactory(maxObjectsFact, offsetFact, null, context.getBinaryIntegerInspectorFactory());
builder.contributeMicroOperator(limit, runtime, recDesc);
// and contribute one edge from its child
ILogicalOperator src = limit.getInputs().get(0).getValue();
builder.contributeGraphEdge(src, 0, limit, 0);
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment in project asterixdb by apache.
the class TypeComputerTest method testTypeComputer.
private boolean testTypeComputer(Class<? extends IResultTypeComputer> c) throws Exception {
// Mocks the type environment.
IVariableTypeEnvironment mockTypeEnv = mock(IVariableTypeEnvironment.class);
// Mocks the metadata provider.
IMetadataProvider<?, ?> mockMetadataProvider = mock(IMetadataProvider.class);
// Mocks function expression.
AbstractFunctionCallExpression mockExpr = mock(AbstractFunctionCallExpression.class);
FunctionIdentifier fid = mock(FunctionIdentifier.class);
when(mockExpr.getFunctionIdentifier()).thenReturn(fid);
when(fid.getName()).thenReturn("testFunction");
// A function at most has six argument.
List<Mutable<ILogicalExpression>> argRefs = new ArrayList<>();
for (int argIndex = 0; argIndex < 6; ++argIndex) {
ILogicalExpression mockArg = mock(ILogicalExpression.class);
argRefs.add(new MutableObject<>(mockArg));
when(mockTypeEnv.getType(mockArg)).thenReturn(BuiltinType.ANY);
}
// Sets up arguments for the mocked expression.
when(mockExpr.getArguments()).thenReturn(argRefs);
// Sets up required/actual types of the mocked expression.
Object[] opaqueParameters = new Object[2];
opaqueParameters[0] = BuiltinType.ANY;
opaqueParameters[1] = BuiltinType.ANY;
when(mockExpr.getOpaqueParameters()).thenReturn(opaqueParameters);
// Tests the return type. It should be either ANY or NULLABLE/MISSABLE.
IResultTypeComputer instance = (IResultTypeComputer) c.getField("INSTANCE").get(null);
IAType resultType = instance.computeType(mockExpr, mockTypeEnv, mockMetadataProvider);
ATypeTag typeTag = resultType.getTypeTag();
if (typeTag == ATypeTag.ANY) {
return true;
}
if (typeTag == ATypeTag.UNION) {
AUnionType unionType = (AUnionType) resultType;
return unionType.isMissableType() && unionType.isNullableType();
}
return false;
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment in project asterixdb by apache.
the class MetadataBuiltinFunctions method addMetadataBuiltinFunctions.
public static void addMetadataBuiltinFunctions() {
BuiltinFunctions.addFunction(BuiltinFunctions.DATASET, new IResultTypeComputer() {
@Override
public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env, IMetadataProvider<?, ?> mp) throws AlgebricksException {
AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) expression;
if (f.getArguments().size() != 1) {
throw new AlgebricksException("dataset arity is 1, not " + f.getArguments().size());
}
ILogicalExpression a1 = f.getArguments().get(0).getValue();
IAType t1 = (IAType) env.getType(a1);
if (t1.getTypeTag() == ATypeTag.ANY) {
return BuiltinType.ANY;
}
if (t1.getTypeTag() != ATypeTag.STRING) {
throw new AlgebricksException("Illegal type " + t1 + " for dataset() argument.");
}
String datasetArg = ConstantExpressionUtil.getStringConstant(a1);
if (datasetArg == null) {
return BuiltinType.ANY;
}
MetadataProvider metadata = (MetadataProvider) mp;
Pair<String, String> datasetInfo = getDatasetInfo(metadata, datasetArg);
String dataverseName = datasetInfo.first;
String datasetName = datasetInfo.second;
if (dataverseName == null) {
throw new AlgebricksException("Unspecified dataverse!");
}
Dataset dataset = metadata.findDataset(dataverseName, datasetName);
if (dataset == null) {
throw new AlgebricksException("Could not find dataset " + datasetName + " in dataverse " + dataverseName);
}
String tn = dataset.getItemTypeName();
IAType t2 = metadata.findType(dataset.getItemTypeDataverseName(), tn);
if (t2 == null) {
throw new AlgebricksException("No type for dataset " + datasetName);
}
return t2;
}
}, true);
BuiltinFunctions.addPrivateFunction(BuiltinFunctions.FEED_COLLECT, new IResultTypeComputer() {
@Override
public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env, IMetadataProvider<?, ?> mp) throws AlgebricksException {
AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) expression;
if (f.getArguments().size() != BuiltinFunctions.FEED_COLLECT.getArity()) {
throw new AlgebricksException("Incorrect number of arguments -> arity is " + BuiltinFunctions.FEED_COLLECT.getArity() + ", not " + f.getArguments().size());
}
ILogicalExpression a1 = f.getArguments().get(5).getValue();
IAType t1 = (IAType) env.getType(a1);
if (t1.getTypeTag() == ATypeTag.ANY) {
return BuiltinType.ANY;
}
if (t1.getTypeTag() != ATypeTag.STRING) {
throw new AlgebricksException("Illegal type " + t1 + " for feed-ingest argument.");
}
String typeArg = ConstantExpressionUtil.getStringConstant(a1);
if (typeArg == null) {
return BuiltinType.ANY;
}
MetadataProvider metadata = (MetadataProvider) mp;
Pair<String, String> argInfo = getDatasetInfo(metadata, typeArg);
String dataverseName = argInfo.first;
String typeName = argInfo.second;
if (dataverseName == null) {
throw new AlgebricksException("Unspecified dataverse!");
}
IAType t2 = metadata.findType(dataverseName, typeName);
if (t2 == null) {
throw new AlgebricksException("Unknown type " + typeName);
}
return t2;
}
}, true);
BuiltinFunctions.addFunction(BuiltinFunctions.FEED_INTERCEPT, new IResultTypeComputer() {
@Override
public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env, IMetadataProvider<?, ?> mp) throws AlgebricksException {
AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) expression;
if (f.getArguments().size() != 1) {
throw new AlgebricksException("dataset arity is 1, not " + f.getArguments().size());
}
ILogicalExpression a1 = f.getArguments().get(0).getValue();
IAType t1 = (IAType) env.getType(a1);
if (t1.getTypeTag() == ATypeTag.ANY) {
return BuiltinType.ANY;
}
if (t1.getTypeTag() != ATypeTag.STRING) {
throw new AlgebricksException("Illegal type " + t1 + " for dataset() argument.");
}
String datasetArg = ConstantExpressionUtil.getStringConstant(a1);
if (datasetArg == null) {
return BuiltinType.ANY;
}
MetadataProvider metadata = (MetadataProvider) mp;
Pair<String, String> datasetInfo = getDatasetInfo(metadata, datasetArg);
String dataverseName = datasetInfo.first;
String datasetName = datasetInfo.second;
if (dataverseName == null) {
throw new AlgebricksException("Unspecified dataverse!");
}
Dataset dataset = metadata.findDataset(dataverseName, datasetName);
if (dataset == null) {
throw new AlgebricksException("Could not find dataset " + datasetName + " in dataverse " + dataverseName);
}
String tn = dataset.getItemTypeName();
IAType t2 = metadata.findType(dataset.getItemTypeDataverseName(), tn);
if (t2 == null) {
throw new AlgebricksException("No type for dataset " + datasetName);
}
return t2;
}
}, true);
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment in project asterixdb by apache.
the class CheckFilterExpressionTypeRule 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 select = (SelectOperator) op;
ILogicalExpression condition = select.getCondition().getValue();
IVariableTypeEnvironment env = select.computeOutputTypeEnvironment(context);
IAType condType = (IAType) env.getType(condition);
if (condType.getTypeTag() != ATypeTag.BOOLEAN && condType.getTypeTag() != ATypeTag.ANY && !isPossibleBoolean(condType)) {
throw new AlgebricksException("The select condition " + condition.toString() + " should be of the boolean type.");
}
return false;
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment in project asterixdb by apache.
the class RTreeSearchPOperator method contributeRuntimeOperator.
@Override
public void contributeRuntimeOperator(IHyracksJobBuilder builder, JobGenContext context, ILogicalOperator op, IOperatorSchema opSchema, IOperatorSchema[] inputSchemas, IOperatorSchema outerPlanSchema) throws AlgebricksException {
AbstractUnnestMapOperator unnestMap = (AbstractUnnestMapOperator) op;
ILogicalExpression unnestExpr = unnestMap.getExpressionRef().getValue();
if (unnestExpr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
throw new IllegalStateException();
}
AbstractFunctionCallExpression unnestFuncExpr = (AbstractFunctionCallExpression) unnestExpr;
FunctionIdentifier funcIdent = unnestFuncExpr.getFunctionIdentifier();
if (!funcIdent.equals(BuiltinFunctions.INDEX_SEARCH)) {
return;
}
RTreeJobGenParams jobGenParams = new RTreeJobGenParams();
jobGenParams.readFromFuncArgs(unnestFuncExpr.getArguments());
int[] keyIndexes = getKeyIndexes(jobGenParams.getKeyVarList(), inputSchemas);
int[] minFilterFieldIndexes = getKeyIndexes(unnestMap.getMinFilterVars(), inputSchemas);
int[] maxFilterFieldIndexes = getKeyIndexes(unnestMap.getMaxFilterVars(), inputSchemas);
MetadataProvider mp = (MetadataProvider) context.getMetadataProvider();
Dataset dataset = mp.findDataset(jobGenParams.getDataverseName(), jobGenParams.getDatasetName());
IVariableTypeEnvironment typeEnv = context.getTypeEnvironment(unnestMap);
List<LogicalVariable> outputVars = unnestMap.getVariables();
if (jobGenParams.getRetainInput()) {
outputVars = new ArrayList<LogicalVariable>();
VariableUtilities.getLiveVariables(unnestMap, outputVars);
}
boolean retainNull = false;
if (op.getOperatorTag() == LogicalOperatorTag.LEFT_OUTER_UNNEST_MAP) {
// By nature, LEFT_OUTER_UNNEST_MAP should generate null values for non-matching tuples.
retainNull = true;
}
Pair<IOperatorDescriptor, AlgebricksPartitionConstraint> rtreeSearch = mp.buildRtreeRuntime(builder.getJobSpec(), outputVars, opSchema, typeEnv, context, jobGenParams.getRetainInput(), retainNull, dataset, jobGenParams.getIndexName(), keyIndexes, minFilterFieldIndexes, maxFilterFieldIndexes);
builder.contributeHyracksOperator(unnestMap, rtreeSearch.first);
builder.contributeAlgebricksPartitionConstraint(rtreeSearch.first, rtreeSearch.second);
ILogicalOperator srcExchange = unnestMap.getInputs().get(0).getValue();
builder.contributeGraphEdge(srcExchange, 0, unnestMap, 0);
}
Aggregations