use of org.apache.hyracks.algebricks.common.exceptions.AlgebricksException in project asterixdb by apache.
the class SetAlgebricksPhysicalOperatorsRule method generateMergeAggregationExpressions.
private static boolean generateMergeAggregationExpressions(GroupByOperator gby, IOptimizationContext context) throws AlgebricksException {
if (gby.getNestedPlans().size() != 1) {
//an aggregate and a nested-tuple-source.
throw new AlgebricksException("External group-by currently works only for one nested plan with one root containing" + "an aggregate and a nested-tuple-source.");
}
ILogicalPlan p0 = gby.getNestedPlans().get(0);
if (p0.getRoots().size() != 1) {
//an aggregate and a nested-tuple-source.
throw new AlgebricksException("External group-by currently works only for one nested plan with one root containing" + "an aggregate and a nested-tuple-source.");
}
IMergeAggregationExpressionFactory mergeAggregationExpressionFactory = context.getMergeAggregationExpressionFactory();
Mutable<ILogicalOperator> r0 = p0.getRoots().get(0);
AbstractLogicalOperator r0Logical = (AbstractLogicalOperator) r0.getValue();
if (r0Logical.getOperatorTag() != LogicalOperatorTag.AGGREGATE) {
return false;
}
// Check whether there are multiple aggregates in the sub plan.
ILogicalOperator r1Logical = r0Logical;
while (r1Logical.hasInputs()) {
r1Logical = r1Logical.getInputs().get(0).getValue();
if (r1Logical.getOperatorTag() == LogicalOperatorTag.AGGREGATE) {
return false;
}
}
AggregateOperator aggOp = (AggregateOperator) r0.getValue();
List<Mutable<ILogicalExpression>> aggFuncRefs = aggOp.getExpressions();
List<LogicalVariable> originalAggVars = aggOp.getVariables();
int n = aggOp.getExpressions().size();
List<Mutable<ILogicalExpression>> mergeExpressionRefs = new ArrayList<Mutable<ILogicalExpression>>();
for (int i = 0; i < n; i++) {
ILogicalExpression mergeExpr = mergeAggregationExpressionFactory.createMergeAggregation(originalAggVars.get(i), aggFuncRefs.get(i).getValue(), context);
if (mergeExpr == null) {
return false;
}
mergeExpressionRefs.add(new MutableObject<ILogicalExpression>(mergeExpr));
}
aggOp.setMergeExpressions(mergeExpressionRefs);
return true;
}
use of org.apache.hyracks.algebricks.common.exceptions.AlgebricksException in project asterixdb by apache.
the class PushGroupByIntoSortRule method generateMergeAggregationExpressions.
private boolean generateMergeAggregationExpressions(GroupByOperator gby, IOptimizationContext context) throws AlgebricksException {
if (gby.getNestedPlans().size() != 1) {
throw new AlgebricksException("External/sort group-by currently works only for one nested plan with one root containing" + "an aggregate and a nested-tuple-source.");
}
ILogicalPlan p0 = gby.getNestedPlans().get(0);
if (p0.getRoots().size() != 1) {
throw new AlgebricksException("External/sort group-by currently works only for one nested plan with one root containing" + "an aggregate and a nested-tuple-source.");
}
IMergeAggregationExpressionFactory mergeAggregationExpressionFactory = context.getMergeAggregationExpressionFactory();
Mutable<ILogicalOperator> r0 = p0.getRoots().get(0);
AggregateOperator aggOp = (AggregateOperator) r0.getValue();
List<Mutable<ILogicalExpression>> aggFuncRefs = aggOp.getExpressions();
List<LogicalVariable> originalAggVars = aggOp.getVariables();
int n = aggOp.getExpressions().size();
List<Mutable<ILogicalExpression>> mergeExpressionRefs = new ArrayList<Mutable<ILogicalExpression>>();
for (int i = 0; i < n; i++) {
ILogicalExpression mergeExpr = mergeAggregationExpressionFactory.createMergeAggregation(originalAggVars.get(i), aggFuncRefs.get(i).getValue(), context);
if (mergeExpr == null) {
return false;
}
mergeExpressionRefs.add(new MutableObject<ILogicalExpression>(mergeExpr));
}
aggOp.setMergeExpressions(mergeExpressionRefs);
return true;
}
use of org.apache.hyracks.algebricks.common.exceptions.AlgebricksException in project asterixdb by apache.
the class LoadRecordFieldsRule method pushFieldAssign.
private static void pushFieldAssign(AssignOperator a2, AbstractLogicalOperator topOp, IOptimizationContext context) throws AlgebricksException {
if (topOp.getInputs().size() == 1 && !topOp.hasNestedPlans()) {
Mutable<ILogicalOperator> topChild = topOp.getInputs().get(0);
// plugAccessAboveOp(a2, topChild, context);
List<Mutable<ILogicalOperator>> a2InptList = a2.getInputs();
a2InptList.clear();
a2InptList.add(topChild);
// and link it as child in the op. tree
topOp.getInputs().set(0, new MutableObject<>(a2));
findAndEliminateRedundantFieldAccess(a2, context);
} else {
// e.g., a join
LinkedList<LogicalVariable> usedInAccess = new LinkedList<LogicalVariable>();
VariableUtilities.getUsedVariables(a2, usedInAccess);
LinkedList<LogicalVariable> produced2 = new LinkedList<LogicalVariable>();
VariableUtilities.getProducedVariables(topOp, produced2);
if (OperatorPropertiesUtil.disjoint(produced2, usedInAccess)) {
for (Mutable<ILogicalOperator> inp : topOp.getInputs()) {
HashSet<LogicalVariable> v2 = new HashSet<LogicalVariable>();
VariableUtilities.getLiveVariables(inp.getValue(), v2);
if (!OperatorPropertiesUtil.disjoint(usedInAccess, v2)) {
pushAccessAboveOpRef(a2, inp, context);
return;
}
}
if (topOp.hasNestedPlans()) {
AbstractOperatorWithNestedPlans nestedOp = (AbstractOperatorWithNestedPlans) topOp;
for (ILogicalPlan plan : nestedOp.getNestedPlans()) {
for (Mutable<ILogicalOperator> root : plan.getRoots()) {
HashSet<LogicalVariable> v2 = new HashSet<LogicalVariable>();
VariableUtilities.getLiveVariables(root.getValue(), v2);
if (!OperatorPropertiesUtil.disjoint(usedInAccess, v2)) {
pushAccessAboveOpRef(a2, root, context);
return;
}
}
}
}
throw new AlgebricksException("Field access " + getFirstExpr(a2) + " does not correspond to any input of operator " + topOp);
}
}
}
use of org.apache.hyracks.algebricks.common.exceptions.AlgebricksException in project asterixdb by apache.
the class PushFieldAccessRule method isAccessToIndexedField.
@SuppressWarnings("unchecked")
private boolean isAccessToIndexedField(AssignOperator assign, IOptimizationContext context) throws AlgebricksException {
AbstractFunctionCallExpression accessFun = (AbstractFunctionCallExpression) assign.getExpressions().get(0).getValue();
ILogicalExpression e0 = accessFun.getArguments().get(0).getValue();
if (e0.getExpressionTag() != LogicalExpressionTag.VARIABLE) {
return false;
}
LogicalVariable var = ((VariableReferenceExpression) e0).getVariableReference();
if (context.findPrimaryKey(var) == null) {
// not referring to a dataset record
return false;
}
AbstractLogicalOperator op = assign;
while (op.getInputs().size() == 1 && op.getOperatorTag() != LogicalOperatorTag.DATASOURCESCAN) {
op = (AbstractLogicalOperator) op.getInputs().get(0).getValue();
}
if (op.getOperatorTag() != LogicalOperatorTag.DATASOURCESCAN) {
return false;
}
DataSourceScanOperator scan = (DataSourceScanOperator) op;
LogicalVariable recVar = scan.getVariables().get(scan.getVariables().size() - 1);
if (recVar != var) {
return false;
}
MetadataProvider mp = (MetadataProvider) context.getMetadataProvider();
DataSourceId asid = ((IDataSource<DataSourceId>) scan.getDataSource()).getId();
Dataset dataset = mp.findDataset(asid.getDataverseName(), asid.getDatasourceName());
if (dataset == null) {
throw new AlgebricksException("Dataset " + asid.getDatasourceName() + " not found.");
}
if (dataset.getDatasetType() != DatasetType.INTERNAL) {
return false;
}
final Integer pos = ConstantExpressionUtil.getIntConstant(accessFun.getArguments().get(1).getValue());
if (pos != null) {
String tName = dataset.getItemTypeName();
IAType t = mp.findType(dataset.getItemTypeDataverseName(), tName);
if (t.getTypeTag() != ATypeTag.OBJECT) {
return false;
}
ARecordType rt = (ARecordType) t;
if (pos >= rt.getFieldNames().length) {
return false;
}
}
List<Index> datasetIndexes = mp.getDatasetIndexes(dataset.getDataverseName(), dataset.getDatasetName());
boolean hasSecondaryIndex = false;
for (Index index : datasetIndexes) {
if (index.isSecondaryIndex()) {
hasSecondaryIndex = true;
break;
}
}
return hasSecondaryIndex;
}
use of org.apache.hyracks.algebricks.common.exceptions.AlgebricksException in project asterixdb by apache.
the class IntroduceDynamicTypeCastRule method compatible.
/**
* Check whether the required record type and the input type is compatible
*
* @param reqType
* @param inputType
* @return true if compatible; false otherwise
* @throws AlgebricksException
*/
public static boolean compatible(ARecordType reqType, IAType inputType) throws AlgebricksException {
if (inputType.getTypeTag() == ATypeTag.ANY) {
return false;
}
if (inputType.getTypeTag() != ATypeTag.OBJECT) {
throw new AlgebricksException("The input type " + inputType + " is not a valid record type!");
}
ARecordType inputRecType = (ARecordType) inputType;
if (reqType.isOpen() != inputRecType.isOpen()) {
return false;
}
IAType[] reqTypes = reqType.getFieldTypes();
String[] reqFieldNames = reqType.getFieldNames();
IAType[] inputTypes = inputRecType.getFieldTypes();
String[] inputFieldNames = ((ARecordType) inputType).getFieldNames();
if (reqTypes.length != inputTypes.length) {
return false;
}
for (int i = 0; i < reqTypes.length; i++) {
if (!reqFieldNames[i].equals(inputFieldNames[i])) {
return false;
}
IAType reqTypeInside = reqTypes[i];
if (NonTaggedFormatUtil.isOptional(reqTypes[i])) {
reqTypeInside = ((AUnionType) reqTypes[i]).getActualType();
}
IAType inputTypeInside = inputTypes[i];
if (NonTaggedFormatUtil.isOptional(inputTypes[i])) {
if (!NonTaggedFormatUtil.isOptional(reqTypes[i])) {
/** if the required type is not optional, the two types are incompatible */
return false;
}
inputTypeInside = ((AUnionType) inputTypes[i]).getActualType();
}
if (inputTypeInside.getTypeTag() != ATypeTag.MISSING && !reqTypeInside.equals(inputTypeInside)) {
return false;
}
}
return true;
}
Aggregations