SQL Parser - AST traversal and rewriting

50 views
Skip to first unread message

Jake Cobb

unread,
Jan 17, 2014, 1:41:08 PM1/17/14
to akiba...@akiban.com
Hello,

I have two related questions.  

First, is there any mechanism for traversing the parsed tree structure outside of the Visitor pattern?  For example, given a QueryTreeNode I want to get the parent node, if any.  Currently, I'm using a convoluted and inefficient combination of two visitors for this: one to traverse the tree from the root and another that looks ahead just a single level to see if the current node's children include the target.  The way visitors are implemented, it does not seem possible to know when siblings are being visited sequentially versus recursion into a child within a single Visitor instance.  At any rate, I'd must rather be able to say something like queryTreeNode.getParent()

Second, is there any API to facilitate rewriting the AST?  Say I have this:

x AND y AND z

I want to delete x and I find that it's the left operand of the first AND node.  That node in turn is the left operand of the second AND node.  So, I want to replace the first AND node with the y node.  Is there any API that handles this in a general way?  There are many, many cases to consider if I have to manually handle all the various types the parent node could be since they do not expose children in a uniform way.

I see that, for example, BinaryComparisonOperatorNode has a forQueryRewrite flag, but I haven't found an obvious rewriting facility.

Thanks for your time.
-Jake Cobb
Reply all
Reply to author
Forward
0 new messages