sedna xml database: query parser & optimizing rewriter

21
Sedna Query Parser & Optimizing Rewriter Dmitry Lizorkin [email protected] Ph.D., software developer Sedna team

Upload: ivan-shcheklein

Post on 12-May-2015

2.676 views

Category:

Technology


3 download

DESCRIPTION

Describes parser implementaion and query rewriting techniques in Sedna Native XML Database

TRANSCRIPT

Page 1: Sedna XML Database: Query Parser & Optimizing Rewriter

Sedna Query Parser& Optimizing Rewriter

Dmitry [email protected]

Ph.D., software developerSedna team

Page 2: Sedna XML Database: Query Parser & Optimizing Rewriter

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

Page 3: Sedna XML Database: Query Parser & Optimizing Rewriter

Query processing steps

PhysicalPlan

Generator

OptimizingRewriter

StaticAnalyzer

ParserXMLupdate

statement

DataDefinitionLanguagestatement

XQueryquery

query execution plan (QEP):to Sedna executor

operation tree

Page 4: Sedna XML Database: Query Parser & Optimizing Rewriter

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

Page 5: Sedna XML Database: Query Parser & Optimizing Rewriter

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

Page 6: Sedna XML Database: Query Parser & Optimizing Rewriter

Optimizing rewriter step

PhysicalPlan

Generator

OptimizingRewriter

query execution plan

StaticAnalyzer

ParserXMLupdate

statement

DataDefinitionLanguagestatement

XQueryquery

operation tree

Page 7: Sedna XML Database: Query Parser & Optimizing Rewriter

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

Page 8: Sedna XML Database: Query Parser & Optimizing Rewriter

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

Page 9: Sedna XML Database: Query Parser & Optimizing Rewriter

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

Page 10: Sedna XML Database: Query Parser & Optimizing Rewriter

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

Page 11: Sedna XML Database: Query Parser & Optimizing Rewriter

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)

Page 12: Sedna XML Database: Query Parser & Optimizing Rewriter

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

Page 13: Sedna XML Database: Query Parser & Optimizing Rewriter

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

Page 14: Sedna XML Database: Query Parser & Optimizing Rewriter

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_tuplewhere ... 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

Page 15: Sedna XML Database: Query Parser & Optimizing Rewriter

Physical plan generation step

PhysicalPlan

Generator

OptimizingRewriter

query execution plan

StaticAnalyzer

ParserXMLupdate

statement

DataDefinitionLanguagestatement

XQueryquery

operation tree

Page 16: Sedna XML Database: Query Parser & Optimizing Rewriter

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

Page 17: Sedna XML Database: Query Parser & Optimizing Rewriter

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

Page 18: Sedna XML Database: Query Parser & Optimizing Rewriter

Summary

• Complete query static analysis phase and rewriting-based optimization processing (i.e. from query textual representation to physical plan)

Page 19: Sedna XML Database: Query Parser & Optimizing Rewriter

Thank you for your attention!

Page 20: Sedna XML Database: Query Parser & Optimizing Rewriter

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

Page 21: Sedna XML Database: Query Parser & Optimizing Rewriter

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