Sedna Query Parser & Optimizing Rewriter  Dmitry Lizorkin [email_address] Ph.D., software developer Sedna team
Goals Wide range of queries/statements support: XQuery queries, XML update statements, data definition language statements High performance for both query evaluation and updates execution Query optimization strategies designed in correspondence with Sedna internal data representation design
Query processing steps query execution plan (QEP): to Sedna executor operation tree Physical Plan Generator Optimizing Rewriter Static Analyzer Parser XML update statement Data Definition Language statement XQuery query
Query parser Input: 3 types of queries/statements XQuery queries XML update statements Data Definition Language statements (e.g. create document statement) Output: uniform operation tree for all above 3 query/statement types
Static analyzer Query static analysis phase Static context is initialized with XQuery Functions and Operators and augmented with query prolog declarations Query operation tree is expanded with imported XQuery modules All namespace prefixes, function names and variable names are resolved XQuery static errors are reported, if any
Optimizing rewriter step Physical Plan Generator Optimizing Rewriter query execution plan Static Analyzer Parser operation tree XML update statement Data Definition Language statement XQuery query
Optimizing Rewriter Optimization based on query rewriting Removing unnecessary ordering operations Combining abbreviated descendant-or-self path step with a next path step Removing unnecessary node copies for constructed content Analyzing nested for-clauses
Ordering operations: challenges Many XQuery expressions have the semantics for their resulting sequences to be ordered in document order with duplicate nodes removed “ Distinct-Document Order” semantics is expressed via explicit DDO operations in Sedna query operation tree DDO operations decrease query execution performance Require the whole argument sequence to be evaluated before a first result item could be produced Break execution pipeline
Ordering operations: optimization Idea: removing unnecessary ordering operations Analysis: for each operation in the query operation tree, the following properties for the resulting sequence are recursively found out whether in DDO whether consists of no more than a single item whether consists of nodes on a common level of an XML tree Result: a DDO operation is removed if either its argument is known to be in DDO, or DDO is not required for the resulting sequence
Descendant-or-self: challenges The  “//”  abbreviated path step (expanded into  descendant-or-self::node() ) is frequently used in practical XQuery queries Expensive to evaluate Bad selectivity: generally, selects almost all nodes in an XML document Does not allow to use benefits of Sedna descriptive schema-driven storage strategy
Descendant-or-self: optimization idea Idea: combining the  //  step with a next path step E.g.,  //para  transformed to  /descendant::para Better intermediate selectivity Benefits of Sedna schema-driven storage Technical issue: context position/size “ The path expression  //para[1]  does  not  mean the same as the path expression  /descendant::para[1] ” (XQuery Spec., Subsect. 3.2.4)
Descendant-or-self: solution For the  //  path step, its next step predicate expressions are analyzed (if any) If predicate expressions results do not depend on context position and size (neither explicitly, nor implicitly), than the  //  step can be combined with its next step while keeping the original query semantics
Removing unnecessary node copies An XQuery constructor semantics implies constructor content being new nodes, “ even if they are copies of existing nodes ” Problem: making a deep copy of an XML subtree is expensive to evaluate Idea: avoiding node copies that do not affect query result semantics Algorithm: a constructed node needs not be copied if it is a part of the query result sequence, or it becomes a direct child of another constructed node
Analyzing nested for-clauses FLWOR-expressions generally contain multiple iteration variables in for-clauses for $u in doc('users')//user_tuple, $i in doc('items')//item_tuple where ... return ... Binding sequences with nested loop semantics An expression associated with an inner iteration variable is analyzed If the associated expression does not depend on outer iteration variables, it is marked as “lazy” Lazy associated expression value can be evaluated just once, with the query semantics preserved
Physical plan generation step Physical Plan Generator Optimizing Rewriter query execution plan Static Analyzer Parser operation tree XML update statement Data Definition Language statement XQuery query
Query physical plan generation Query execution plan (QEP) for the query operation tree is constructed Structural location path fragments are extracted A path that starts from an XML document node and contains only descending axes and no predicates Mapped to Sedna descriptive schema access operations For & order-by clauses are mapped to tuple stream generation & reordering operations respectively
Implementation details Query parser is implemented with ANTLR parser generator (www.antlr.org) ANTLR native representation for an operation tree is an S-expression S-expression is a native data representation for Scheme programming language Scheme provides extensive native features for S-expressions rewriting purposes Optimizing rewriter is implemented in Scheme The Scheme-to-C compiler produces high-performance code
Summary Complete query static analysis phase and rewriting-based optimization processing (i.e. from query textual representation to physical plan)
Thank you for your attention!
Future work: Cost-based optimization Problem analysis Join operations are primary candidates to benefit from cost-based optimization implemented In XML, the problem of join evaluation is not as vital as for relational data One-to-many entity relationships can often be modeled via XML elements nesting mechanism For many-to-many relationships, Sedna explicit indexes can be used
Cost-based optimization: plans Join operations extraction in a query operation tree Selectivity estimation mechanism for XQuery expressions A storage for selectivity statistics is required (XML?) Cost-based physical plan selection is a complicated task in general; however, a lot of related work exists can be relatively easily implemented for simple cases Hints can be used for complex cases

Sedna XML Database: Query Parser & Optimizing Rewriter

  • 1.
    Sedna Query Parser& Optimizing Rewriter Dmitry Lizorkin [email_address] Ph.D., software developer Sedna team
  • 2.
    Goals Wide rangeof queries/statements support: XQuery queries, XML update statements, data definition language statements High performance for both query evaluation and updates execution Query optimization strategies designed in correspondence with Sedna internal data representation design
  • 3.
    Query processing stepsquery execution plan (QEP): to Sedna executor operation tree Physical Plan Generator Optimizing Rewriter Static Analyzer Parser XML update statement Data Definition Language statement XQuery query
  • 4.
    Query parser Input:3 types of queries/statements XQuery queries XML update statements Data Definition Language statements (e.g. create document statement) Output: uniform operation tree for all above 3 query/statement types
  • 5.
    Static analyzer Querystatic analysis phase Static context is initialized with XQuery Functions and Operators and augmented with query prolog declarations Query operation tree is expanded with imported XQuery modules All namespace prefixes, function names and variable names are resolved XQuery static errors are reported, if any
  • 6.
    Optimizing rewriter stepPhysical Plan Generator Optimizing Rewriter query execution plan Static Analyzer Parser operation tree XML update statement Data Definition Language statement XQuery query
  • 7.
    Optimizing Rewriter Optimizationbased on query rewriting Removing unnecessary ordering operations Combining abbreviated descendant-or-self path step with a next path step Removing unnecessary node copies for constructed content Analyzing nested for-clauses
  • 8.
    Ordering operations: challengesMany XQuery expressions have the semantics for their resulting sequences to be ordered in document order with duplicate nodes removed “ Distinct-Document Order” semantics is expressed via explicit DDO operations in Sedna query operation tree DDO operations decrease query execution performance Require the whole argument sequence to be evaluated before a first result item could be produced Break execution pipeline
  • 9.
    Ordering operations: optimizationIdea: removing unnecessary ordering operations Analysis: for each operation in the query operation tree, the following properties for the resulting sequence are recursively found out whether in DDO whether consists of no more than a single item whether consists of nodes on a common level of an XML tree Result: a DDO operation is removed if either its argument is known to be in DDO, or DDO is not required for the resulting sequence
  • 10.
    Descendant-or-self: challenges The “//” abbreviated path step (expanded into descendant-or-self::node() ) is frequently used in practical XQuery queries Expensive to evaluate Bad selectivity: generally, selects almost all nodes in an XML document Does not allow to use benefits of Sedna descriptive schema-driven storage strategy
  • 11.
    Descendant-or-self: optimization ideaIdea: combining the // step with a next path step E.g., //para transformed to /descendant::para Better intermediate selectivity Benefits of Sedna schema-driven storage Technical issue: context position/size “ The path expression //para[1] does not mean the same as the path expression /descendant::para[1] ” (XQuery Spec., Subsect. 3.2.4)
  • 12.
    Descendant-or-self: solution Forthe // path step, its next step predicate expressions are analyzed (if any) If predicate expressions results do not depend on context position and size (neither explicitly, nor implicitly), than the // step can be combined with its next step while keeping the original query semantics
  • 13.
    Removing unnecessary nodecopies An XQuery constructor semantics implies constructor content being new nodes, “ even if they are copies of existing nodes ” Problem: making a deep copy of an XML subtree is expensive to evaluate Idea: avoiding node copies that do not affect query result semantics Algorithm: a constructed node needs not be copied if it is a part of the query result sequence, or it becomes a direct child of another constructed node
  • 14.
    Analyzing nested for-clausesFLWOR-expressions generally contain multiple iteration variables in for-clauses for $u in doc('users')//user_tuple, $i in doc('items')//item_tuple where ... return ... Binding sequences with nested loop semantics An expression associated with an inner iteration variable is analyzed If the associated expression does not depend on outer iteration variables, it is marked as “lazy” Lazy associated expression value can be evaluated just once, with the query semantics preserved
  • 15.
    Physical plan generationstep Physical Plan Generator Optimizing Rewriter query execution plan Static Analyzer Parser operation tree XML update statement Data Definition Language statement XQuery query
  • 16.
    Query physical plangeneration Query execution plan (QEP) for the query operation tree is constructed Structural location path fragments are extracted A path that starts from an XML document node and contains only descending axes and no predicates Mapped to Sedna descriptive schema access operations For & order-by clauses are mapped to tuple stream generation & reordering operations respectively
  • 17.
    Implementation details Queryparser is implemented with ANTLR parser generator (www.antlr.org) ANTLR native representation for an operation tree is an S-expression S-expression is a native data representation for Scheme programming language Scheme provides extensive native features for S-expressions rewriting purposes Optimizing rewriter is implemented in Scheme The Scheme-to-C compiler produces high-performance code
  • 18.
    Summary Complete querystatic analysis phase and rewriting-based optimization processing (i.e. from query textual representation to physical plan)
  • 19.
    Thank you foryour attention!
  • 20.
    Future work: Cost-basedoptimization Problem analysis Join operations are primary candidates to benefit from cost-based optimization implemented In XML, the problem of join evaluation is not as vital as for relational data One-to-many entity relationships can often be modeled via XML elements nesting mechanism For many-to-many relationships, Sedna explicit indexes can be used
  • 21.
    Cost-based optimization: plansJoin operations extraction in a query operation tree Selectivity estimation mechanism for XQuery expressions A storage for selectivity statistics is required (XML?) Cost-based physical plan selection is a complicated task in general; however, a lot of related work exists can be relatively easily implemented for simple cases Hints can be used for complex cases