You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-user@db.apache.org by venu <VE...@GMAIL.COM> on 2014/01/10 03:47:26 UTC

Re: Using ASTParser and TreeWalker for parsing SQL query

Hi Rick,
I tried your solution (regarding AST parser) and its working perfectly.
I have some problems could you please give me some advice on these issues.

1. We added TreeWalker.java file in
java\engine\org\apache\derby\impl\sql\compile folder.
While generating derby.jar the class file was generated but class file was
not inserted into the jar file.
How can I add  TreeWalker.class into derby.jar file ?
I added the class into DBMSnodes.properties file but still the class file
not found in jar.
Could you please tell me how to add this? or if possible add this file in
the source then I can modify it.

        2. I did some changes on these files for solving order by and group
by issue. If you have some time, could you please look into these codes ?
        If you feel these codes are correct could you please add those into
the official version or even development version? If not, advice me on
'where to change or strategy which I need to follow'.

\java\engine\org\apache\derby\impl\sql\compile\ValueNodeList.java   -->
added below method

public ArrayList getValueNodes(){
        ArrayList valueNode = new ArrayList();
        for (int index = 0; index < size(); index++) {
              valueNode.add((QueryTreeNode)elementAt(index));
        }
        return valueNode;
}

\java\engine\org\apache\derby\impl\sql\compile\CastNode.java  --> added
below method

public ValueNode getCastOperand(){
        return castOperand;
}


\java\engine\org\apache\derby\impl\sql\compile\SelectNode.java   -->
modified below method

        void acceptChildren(Visitor v)  throws StandardException
{
        super.acceptChildren(v);

        if (fromList != null)
        {
                NodeFilterSingletone.getInstance().setFromListState(true);  
--> Added this line
                fromList = (FromList)fromList.accept(v);
                NodeFilterSingletone.getInstance().setFromListState(false); 
-->  Added this line

        }

        if (whereClause != null)
        {
               
NodeFilterSingletone.getInstance().setWhereClauseState(true);  -->  Added
this line
                whereClause = (ValueNode)whereClause.accept(v);
               
NodeFilterSingletone.getInstance().setWhereClauseState(false);  --> Added
this line
        }

        if (wherePredicates != null)
        {
                wherePredicates = (PredicateList)wherePredicates.accept(v);
        }

        if (havingClause != null) {
               
NodeFilterSingletone.getInstance().setHavingClauseState(true);  --> Added
this line
                havingClause = (ValueNode)havingClause.accept(v);
               
NodeFilterSingletone.getInstance().setHavingClauseState(false);  --> Added
this line
        }

        // visiting these clauses was added as part of DERBY-6263. a better
fix might be to fix the
        // visitor rather than skip it.
        if ( !(v instanceof HasCorrelatedCRsVisitor) )
        {
            if (selectSubquerys != null)
            {
                selectSubquerys = (SubqueryList) selectSubquerys.accept( v
);
            }

            if (whereSubquerys != null)
            {
                whereSubquerys = (SubqueryList) whereSubquerys.accept( v );
            }

            if (groupByList != null) {
                NodeFilterSingletone.getInstance().setGroupbyState(true); 
--> Added this line
                groupByList = (GroupByList) groupByList.accept( v );
                NodeFilterSingletone.getInstance().setGroupbyState(false); 
--> Added this line
            }

            if (orderByLists[0] != null) {
                for (int i = 0; i < orderByLists.length; i++) {
                       
NodeFilterSingletone.getInstance().setOrderbyState(true);  --> Added this
line
                        orderByLists[i] = (OrderByList) orderByLists[ i
].accept( v );
                       
NodeFilterSingletone.getInstance().setOrderbyState(false);  --> Added this
line
                }

            }

            if (offset != null) {
                offset = (ValueNode) offset.accept( v );
            }

            if (fetchFirst != null) {
                fetchFirst = (ValueNode) fetchFirst.accept( v );
            }

            if (preJoinFL != null)
            {
                preJoinFL = (FromList) preJoinFL.accept( v );
            }

            if (windows != null)
            {
                windows = (WindowList) windows.accept( v );
            }
        }
}

\java\engine\org\apache\derby\impl\sql\compile\ NodeFilterSingletone.java  
--> Added this class along with TreeWalker.java class

public class NodeFilterSingletone {

        private static NodeFilterSingletone instance = null;

        private boolean fromListState = false;
        private boolean whereClauseState = false;
        private boolean havingClauseState = false;
        private boolean groupbyState = false;
        private boolean orderbyState = false;

        protected NodeFilterSingletone() {
        }

        public static NodeFilterSingletone getInstance() {
                if (instance == null) {
                        instance = new NodeFilterSingletone();
                }
                return instance;
        }

        public boolean isFromListState() {
                return fromListState;
        }

        public void setFromListState(boolean fromListState) {
                this.fromListState = fromListState;
        }

        public boolean isWhereClauseState() {
                return whereClauseState;
        }

        public void setWhereClauseState(boolean whereClauseState) {
                this.whereClauseState = whereClauseState;
        }

        public boolean isHavingClauseState() {
                return havingClauseState;
        }

        public void setHavingClauseState(boolean havingClauseState) {
                this.havingClauseState = havingClauseState;
        }

        public boolean isGroupbyState() {
                return groupbyState;
        }

        public void setGroupbyState(boolean groupbyState) {
                this.groupbyState = groupbyState;
        }

        public boolean isOrderbyState() {
                return orderbyState;
        }

        public void setOrderbyState(boolean orderbyState) {
                this.orderbyState = orderbyState;
        }
}


Thanks,
Venu.
(Sorry for adding so much code in the post)



--
View this message in context: http://apache-database.10148.n7.nabble.com/Using-ASTParser-and-TreeWalker-for-parsing-SQL-query-tp131219p136426.html
Sent from the Apache Derby Users mailing list archive at Nabble.com.

Re: Using ASTParser and TreeWalker for parsing SQL query

Posted by Rick Hillegas <ri...@oracle.com>.
Hi Venu,

Glad to hear that this technique is working for you. A couple general 
comments:

A) If you need code added to the Derby engine, the best approach would 
be to file a JIRA and attach a patch file. That makes it easier for 
committers to evaluate your improvements and track changes to the codebase.

B) I see that you want to introduce a new NodeFilterSingletone class, 
whose purpose seems to be to mark the boundaries of SQL clauses. Keep an 
eye on https://issues.apache.org/jira/browse/DERBY-6434. I expect to 
propose a different solution to that problem in an upcoming patch. You 
may be able to use it.

C) It's probably best to move this discussion to derby-dev. That's a 
better forum for discussing changes to the Derby codebase.

One other comment inline...

On 1/9/14 6:47 PM, venu wrote:
> Hi Rick,
> I tried your solution (regarding AST parser) and its working perfectly.
> I have some problems could you please give me some advice on these issues.
>
> 1. We added TreeWalker.java file in
> java\engine\org\apache\derby\impl\sql\compile folder.
> While generating derby.jar the class file was generated but class file was
> not inserted into the jar file.
> How can I add  TreeWalker.class into derby.jar file ?
> I added the class into DBMSnodes.properties file but still the class file
> not found in jar.
> Could you please tell me how to add this? or if possible add this file in
> the source then I can modify it.
Try adding the class to tools/jar/extraDBMSclasses.properties.

Hope this helps,
-Rick
>          2. I did some changes on these files for solving order by and group
> by issue. If you have some time, could you please look into these codes ?
>          If you feel these codes are correct could you please add those into
> the official version or even development version? If not, advice me on
> 'where to change or strategy which I need to follow'.
>
> \java\engine\org\apache\derby\impl\sql\compile\ValueNodeList.java   -->
> added below method
>
> public ArrayList getValueNodes(){
>          ArrayList valueNode = new ArrayList();
>          for (int index = 0; index<  size(); index++) {
>                valueNode.add((QueryTreeNode)elementAt(index));
>          }
>          return valueNode;
> }
>
> \java\engine\org\apache\derby\impl\sql\compile\CastNode.java  -->  added
> below method
>
> public ValueNode getCastOperand(){
>          return castOperand;
> }
>
>
> \java\engine\org\apache\derby\impl\sql\compile\SelectNode.java   -->
> modified below method
>
>          void acceptChildren(Visitor v)  throws StandardException
> {
>          super.acceptChildren(v);
>
>          if (fromList != null)
>          {
>                  NodeFilterSingletone.getInstance().setFromListState(true);
> -->  Added this line
>                  fromList = (FromList)fromList.accept(v);
>                  NodeFilterSingletone.getInstance().setFromListState(false);
> -->   Added this line
>
>          }
>
>          if (whereClause != null)
>          {
>
> NodeFilterSingletone.getInstance().setWhereClauseState(true);  -->   Added
> this line
>                  whereClause = (ValueNode)whereClause.accept(v);
>
> NodeFilterSingletone.getInstance().setWhereClauseState(false);  -->  Added
> this line
>          }
>
>          if (wherePredicates != null)
>          {
>                  wherePredicates = (PredicateList)wherePredicates.accept(v);
>          }
>
>          if (havingClause != null) {
>
> NodeFilterSingletone.getInstance().setHavingClauseState(true);  -->  Added
> this line
>                  havingClause = (ValueNode)havingClause.accept(v);
>
> NodeFilterSingletone.getInstance().setHavingClauseState(false);  -->  Added
> this line
>          }
>
>          // visiting these clauses was added as part of DERBY-6263. a better
> fix might be to fix the
>          // visitor rather than skip it.
>          if ( !(v instanceof HasCorrelatedCRsVisitor) )
>          {
>              if (selectSubquerys != null)
>              {
>                  selectSubquerys = (SubqueryList) selectSubquerys.accept( v
> );
>              }
>
>              if (whereSubquerys != null)
>              {
>                  whereSubquerys = (SubqueryList) whereSubquerys.accept( v );
>              }
>
>              if (groupByList != null) {
>                  NodeFilterSingletone.getInstance().setGroupbyState(true);
> -->  Added this line
>                  groupByList = (GroupByList) groupByList.accept( v );
>                  NodeFilterSingletone.getInstance().setGroupbyState(false);
> -->  Added this line
>              }
>
>              if (orderByLists[0] != null) {
>                  for (int i = 0; i<  orderByLists.length; i++) {
>
> NodeFilterSingletone.getInstance().setOrderbyState(true);  -->  Added this
> line
>                          orderByLists[i] = (OrderByList) orderByLists[ i
> ].accept( v );
>
> NodeFilterSingletone.getInstance().setOrderbyState(false);  -->  Added this
> line
>                  }
>
>              }
>
>              if (offset != null) {
>                  offset = (ValueNode) offset.accept( v );
>              }
>
>              if (fetchFirst != null) {
>                  fetchFirst = (ValueNode) fetchFirst.accept( v );
>              }
>
>              if (preJoinFL != null)
>              {
>                  preJoinFL = (FromList) preJoinFL.accept( v );
>              }
>
>              if (windows != null)
>              {
>                  windows = (WindowList) windows.accept( v );
>              }
>          }
> }
>
> \java\engine\org\apache\derby\impl\sql\compile\ NodeFilterSingletone.java
> -->  Added this class along with TreeWalker.java class
>
> public class NodeFilterSingletone {
>
>          private static NodeFilterSingletone instance = null;
>
>          private boolean fromListState = false;
>          private boolean whereClauseState = false;
>          private boolean havingClauseState = false;
>          private boolean groupbyState = false;
>          private boolean orderbyState = false;
>
>          protected NodeFilterSingletone() {
>          }
>
>          public static NodeFilterSingletone getInstance() {
>                  if (instance == null) {
>                          instance = new NodeFilterSingletone();
>                  }
>                  return instance;
>          }
>
>          public boolean isFromListState() {
>                  return fromListState;
>          }
>
>          public void setFromListState(boolean fromListState) {
>                  this.fromListState = fromListState;
>          }
>
>          public boolean isWhereClauseState() {
>                  return whereClauseState;
>          }
>
>          public void setWhereClauseState(boolean whereClauseState) {
>                  this.whereClauseState = whereClauseState;
>          }
>
>          public boolean isHavingClauseState() {
>                  return havingClauseState;
>          }
>
>          public void setHavingClauseState(boolean havingClauseState) {
>                  this.havingClauseState = havingClauseState;
>          }
>
>          public boolean isGroupbyState() {
>                  return groupbyState;
>          }
>
>          public void setGroupbyState(boolean groupbyState) {
>                  this.groupbyState = groupbyState;
>          }
>
>          public boolean isOrderbyState() {
>                  return orderbyState;
>          }
>
>          public void setOrderbyState(boolean orderbyState) {
>                  this.orderbyState = orderbyState;
>          }
> }
>
>
> Thanks,
> Venu.
> (Sorry for adding so much code in the post)
>
>
>
> --
> View this message in context: http://apache-database.10148.n7.nabble.com/Using-ASTParser-and-TreeWalker-for-parsing-SQL-query-tp131219p136426.html
> Sent from the Apache Derby Users mailing list archive at Nabble.com.
>