Package org.nuxeo.ecm.core.storage
Class QueryOptimizer
- java.lang.Object
-
- org.nuxeo.ecm.core.storage.QueryOptimizer
-
- Direct Known Subclasses:
DBSQueryOptimizer
public abstract class QueryOptimizer extends Object
Generic optimizer for a NXQL query.- Since:
- 5.9.4
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
QueryOptimizer.PrefixInfo
Info about a prefix: the prefix, and how many times it was encountered.protected static class
QueryOptimizer.ProjectionReferenceRenamer
Renames references if they are in the projection part.protected static class
QueryOptimizer.ProjectionWildcardsFinder
class
QueryOptimizer.ReferencePrefixAnalyzer
Analyzes references to compute common prefix info in order to later factor them in a parent expression.
-
Field Summary
Fields Modifier and Type Field Description protected static int
CORR_BASE
protected int
correlationCounter
protected FacetFilter
facetFilter
protected static Collector<Predicate,?,Map<String,List<Predicate>>>
GROUPING_BY_EXPR_PREFIX
protected Set<String>
neverPerInstanceMixins
protected boolean
onlyRelations
Do we match only relations?protected SchemaManager
schemaManager
static String
TYPE_DOCUMENT
static String
TYPE_RELATION
static String
TYPE_ROOT
-
Constructor Summary
Constructors Constructor Description QueryOptimizer()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
addFacetFilters(List<Predicate> clauses, FacetFilter facetFilter)
protected SQLQuery
addIsNotNullClauses(SQLQuery query, Collection<String> names)
protected void
addTypes(List<Predicate> clauses, FromClause node)
Finds all the types to take into account (all concrete types being a subtype of the passed types) based on the FROM list.protected void
addWhere(List<Predicate> clauses, Predicate expr)
protected void
addWhere(List<Predicate> clauses, WhereClause where)
Adds a flattened version of all toplevel ANDed WHERE clauses.protected SQLQuery
addWildcardNotNullClauses(SQLQuery query)
If we have a query likeSELECT dc:subjects/* FROM ...
then we must make sure we don't match documents that don't have adc:subjects
at all, as this would make the evaluator return onenull
value for each due to its semantics of doing the equivalent of LEFT JOINs.static String
findPrefix(List<String> strings, List<String> withPrefix)
Finds a non-empty prefix in the strings.abstract String
getCorrelatedWildcardPrefix(String name)
Gets the prefix to use for this reference name (NXQL) if it contains a correlated wildcard.protected Set<String>
getDocumentTypeNamesExtending(String typeName)
protected Set<String>
getDocumentTypeNamesForFacet(String mixin)
protected static int
getExpressionCount(Expression expr)
protected static String
getPredicatePrefix(Predicate predicate)
protected static Set<String>
getStringLiterals(LiteralList list)
protected boolean
isTypeRelation(String typeName)
static Predicate
makeSingleAndPredicate(String prefix, List<Predicate> predicates)
Makes a single AND predicate from several expressions known to have a common prefix.SQLQuery
optimize(SQLQuery query)
Optimizes a query to provide a WHERE clause containing facet filters, primary and mixin types.static void
reorganizeGroupedExpressions(Map<String,Predicate> groupedExpressions)
Reorganizes the grouped expressions in order to have 2-level nesting in case a group is a prefix of another.protected void
simplifyTypes(List<Predicate> clauses)
Simplify ecm:primaryType positive references, and non-per-instance mixin types.QueryOptimizer
withFacetFilter(FacetFilter facetFilter)
-
-
-
Field Detail
-
TYPE_ROOT
public static final String TYPE_ROOT
- See Also:
- Constant Field Values
-
TYPE_DOCUMENT
public static final String TYPE_DOCUMENT
- See Also:
- Constant Field Values
-
TYPE_RELATION
public static final String TYPE_RELATION
- See Also:
- Constant Field Values
-
CORR_BASE
protected static final int CORR_BASE
- See Also:
- Constant Field Values
-
facetFilter
protected FacetFilter facetFilter
-
schemaManager
protected final SchemaManager schemaManager
-
correlationCounter
protected int correlationCounter
-
onlyRelations
protected boolean onlyRelations
Do we match only relations?
-
-
Method Detail
-
withFacetFilter
public QueryOptimizer withFacetFilter(FacetFilter facetFilter)
-
optimize
public SQLQuery optimize(SQLQuery query)
Optimizes a query to provide a WHERE clause containing facet filters, primary and mixin types. In addition, the top-level AND clauses are analyzed to provide prefix info.
-
makeSingleAndPredicate
public static Predicate makeSingleAndPredicate(String prefix, List<Predicate> predicates)
Makes a single AND predicate from several expressions known to have a common prefix.
-
getExpressionCount
protected static int getExpressionCount(Expression expr)
-
reorganizeGroupedExpressions
public static void reorganizeGroupedExpressions(Map<String,Predicate> groupedExpressions)
Reorganizes the grouped expressions in order to have 2-level nesting in case a group is a prefix of another.- Since:
- 9.3
-
findPrefix
public static String findPrefix(List<String> strings, List<String> withPrefix)
Finds a non-empty prefix in the strings.If a prefix is found, the other strings having it as a prefix are collected in
withPrefix
, and the prefix and the found strings are moved from the inputstring
.strings
andwithPrefix
must both be ArrayLists as they will be mutated and queried by index.- Parameters:
strings
- the input stringswithPrefix
- (return value) the strings that have the found prefix as a prefix- Returns:
- the prefix if found, or null if not
- Since:
- 9.3
-
addFacetFilters
protected void addFacetFilters(List<Predicate> clauses, FacetFilter facetFilter)
-
isTypeRelation
protected boolean isTypeRelation(String typeName)
-
addTypes
protected void addTypes(List<Predicate> clauses, FromClause node)
Finds all the types to take into account (all concrete types being a subtype of the passed types) based on the FROM list.Adds them as a ecm:primaryType match in the toplevel operands.
-
addWhere
protected void addWhere(List<Predicate> clauses, WhereClause where)
Adds a flattened version of all toplevel ANDed WHERE clauses.
-
simplifyTypes
protected void simplifyTypes(List<Predicate> clauses)
Simplify ecm:primaryType positive references, and non-per-instance mixin types.
-
getStringLiterals
protected static Set<String> getStringLiterals(LiteralList list)
-
addWildcardNotNullClauses
protected SQLQuery addWildcardNotNullClauses(SQLQuery query)
If we have a query likeSELECT dc:subjects/* FROM ...
then we must make sure we don't match documents that don't have adc:subjects
at all, as this would make the evaluator return onenull
value for each due to its semantics of doing the equivalent of LEFT JOINs.To prevent this, we add a clause
... AND dc:subjects/* IS NOT NULL
.For correlated wildcards this is enough, but for uncorrelated wildcards we must avoid adding extra JOINs, so we must artificially correlated them. This requires rewriting the query with correlated wildcards instead of uncorrelated ones.
-
addIsNotNullClauses
protected SQLQuery addIsNotNullClauses(SQLQuery query, Collection<String> names)
-
getCorrelatedWildcardPrefix
public abstract String getCorrelatedWildcardPrefix(String name)
Gets the prefix to use for this reference name (NXQL) if it contains a correlated wildcard.The prefix is used to group together sets of expression that all use references with the same prefix.
- Parameters:
name
- the reference name (NXQL)- Returns:
- the prefix, or an empty string if there is no correlated wildcard
- Since:
- 9.3
-
-