Class QueryOptimizer

  • Direct Known Subclasses:
    DBSQueryOptimizer

    public abstract class QueryOptimizer
    extends Object
    Generic optimizer for a NXQL query.
    Since:
    5.9.4
    • Constructor Detail

      • QueryOptimizer

        public QueryOptimizer()
    • Method Detail

      • 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.
      • getPredicatePrefix

        protected static String getPredicatePrefix​(Predicate predicate)
      • 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 input string.

        strings and withPrefix must both be ArrayLists as they will be mutated and queried by index.

        Parameters:
        strings - the input strings
        withPrefix - (return value) the strings that have the found prefix as a prefix
        Returns:
        the prefix if found, or null if not
        Since:
        9.3
      • getDocumentTypeNamesForFacet

        protected Set<String> getDocumentTypeNamesForFacet​(String mixin)
      • getDocumentTypeNamesExtending

        protected Set<String> getDocumentTypeNamesExtending​(String typeName)
      • 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.
      • addWildcardNotNullClauses

        protected SQLQuery addWildcardNotNullClauses​(SQLQuery query)
        If we have a query like SELECT dc:subjects/* FROM ... then we must make sure we don't match documents that don't have a dc:subjects at all, as this would make the evaluator return one null 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.

      • 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