public ITreePredicate Contract() { Dictionary <int, ITreePredicate> contractedPreds = new Dictionary <int, ITreePredicate>(); for (int i = 0; i < _predciates.Count; i++) { ITreePredicate contractedPred = _predciates[i].Contract(); if (contractedPred != null) { while (contractedPred.Contract() != null) { contractedPred = contractedPred.Contract(); } contractedPreds[i] = contractedPred; } } if (contractedPreds.Count.Equals(0)) { return(null); } AndTreePredicate returnAnd = new AndTreePredicate(); for (int i = 0; i < _predciates.Count; i++) { if (contractedPreds.ContainsKey(i)) { returnAnd.Add(contractedPreds[i]); } else { returnAnd.Add(_predciates[i]); } } ITreePredicate contractedExpr = returnAnd; while (contractedExpr.Contract() != null) { contractedExpr = contractedExpr.Contract(); } return(contractedExpr); }
public ITreePredicate Contract() { if (!IsTerminal) { Dictionary <int, ITreePredicate> contractedPreds = new Dictionary <int, ITreePredicate>(); for (int i = 0; i < _predciates.Count; i++) { ITreePredicate contracted = _predciates[i].Contract(); if (contracted != null) { while (contracted.Contract() != null) { contracted = contracted.Contract(); } contractedPreds[i] = contracted; } } if (contractedPreds.Count.Equals(0)) { return(null); } OrTreePredicate returnOr = new OrTreePredicate(); for (int i = 0; i < _predciates.Count; i++) { if (contractedPreds.ContainsKey(i)) { returnOr.Add(contractedPreds[i]); } else { returnOr.Add(_predciates[i]); } } ITreePredicate contractedPred = returnOr; while (contractedPred.Contract() != null) { contractedPred = contractedPred.Contract(); } return(contractedPred); } List <AndTreePredicate> ands = new List <AndTreePredicate>(); foreach (var predciate in _predciates) { if (!(predciate is AndTreePredicate)) { return(null); } ands.Add((AndTreePredicate)(predciate)); } if (ands.Count < 2) { return(null); } ITreePredicate commonPred = null; foreach (var expression in ands[0].TreePredicates) { commonPred = expression; for (int i = 1; i < ands.Count; i++) { if (ands[i].TreePredicates.Count < 2 || !ands[i].TreePredicates.Contains(expression)) { return(null); } } } AndTreePredicate returnPred = new AndTreePredicate(); returnPred.Add(commonPred); OrTreePredicate orChild = new OrTreePredicate(); foreach (var and in ands) { foreach (var predciate in and.TreePredicates) { if (!predciate.Equals(commonPred)) { orChild.Add(predciate); } } } returnPred.Add(orChild); ITreePredicate contractPred = returnPred; while (contractPred.Contract() != null) { contractPred = contractPred.Contract(); } return(contractPred); }
public QueryPlan GetQueryPlan(IDmObject parsedQuery, IQuery query, IQueryStore queryStore, MetadataIndex rowsEnumerator) { //Todo: Plan's cache's key decision (query-string based plan cache/optimizable-section based cache). OrderedList <double, IProxyPredicate> sortedPlans = new OrderedList <double, IProxyPredicate>(); var optimizableQuery = parsedQuery as IFilterObject; var criteria = AddQueryCriteria(parsedQuery, query.Parameters, queryStore); var queryPlan = new QueryPlan { Criteria = criteria }; if (optimizableQuery != null && optimizableQuery.WherePredicate != null) { ITreePredicate whereExpression = optimizableQuery.WherePredicate; ITreePredicate contractedExpression = whereExpression.Contract(); if (contractedExpression == null) { contractedExpression = whereExpression; } List <ITreePredicate> distribCombinations = new List <ITreePredicate>(); distribCombinations.Add(contractedExpression); //Todo: Restrict this call if there doesn't exist a compound index. while (contractedExpression.Expand() != null) { distribCombinations.Add(contractedExpression.Expand()); contractedExpression = contractedExpression.Expand(); } foreach (var treePredicate in distribCombinations) { if (treePredicate is OrTreePredicate || treePredicate is AndTreePredicate) { break; } if (treePredicate is ComparisonPredicate) { DocumentKey documentKey = null; if (((ComparisonPredicate)treePredicate).TryGetProxyKeyPredicate(rowsEnumerator, out documentKey)) { IProxyPredicate optimizablePredicate = new ProxyPredicate(new KeyPredicate(documentKey, rowsEnumerator), treePredicate); sortedPlans.Add(optimizablePredicate.Statistics[Statistic.ExpectedIO], optimizablePredicate); queryPlan.Predicate = sortedPlans.FirstValues[0].GetExecutionPredicate(queryStore); return(queryPlan); } } } foreach (var expressionState in distribCombinations) { IProxyPredicate optimizablePredicate = expressionState.GetProxyExecutionPredicate(_indexManager, queryStore, rowsEnumerator); sortedPlans.Add(optimizablePredicate.Statistics[Statistic.ExpectedIO], optimizablePredicate); //Todo: Add optimizedPredicate to the SortedList by the cost. } queryPlan.Predicate = sortedPlans.FirstValues[0].GetExecutionPredicate(queryStore); } else { if (criteria.GroupFields != null && criteria.GroupFields.Count == 1 && criteria.GroupFields[0] is AllField && criteria.ContainsAggregations) { if (criteria.Aggregations.Count == 1 && criteria.Aggregations[0].Aggregation is COUNT && criteria.Aggregations[0].Evaluation is AllEvaluable) { queryPlan.Criteria.GroupByField = null; queryPlan.Predicate = new SpecialCountPredicate(rowsEnumerator.KeyCount); queryPlan.IsSpecialExecution = true; return(queryPlan); } } //Todo:1 Projection variable-based index assigning (Functions' arguments + attributes). queryPlan.Predicate = GetSelectAllPredicate(criteria, rowsEnumerator); } return(queryPlan); }