db-derby-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rick Hillegas <rick.hille...@oracle.com>
Subject Re: Using ASTParser and TreeWalker for parsing SQL query
Date Mon, 01 Jul 2013 13:35:11 GMT
On 6/30/13 9:09 PM, venu wrote:
> Hi Rick,
> Thanks for spending your valuable time on this issue.
> Sorry if I ask the same question again.
>
> Could you please help me on this ?
> ex:
> select colName from tableName where id=100 having COUNT(*)>  1
> When I go through the nodes, I can see the where clause and having clause
> both have defined in a same way (node tree structure).
>
> Both doesn't have parent node. Both are using BinaryRelationalOperatorNode
> as parent node if, both have the condition with some operator (like = in
> where clause and>  in having clause).
>
> But, for differentiate these two we don't have any parent node.
> For example if we take a look at from or groupby or orderby they have root
> nodes like fromlist, groupbylist and orderbylist.
>
> for where clause and having clause both doesn't have parent node.
> -----------------
> 	whereClause: 		
> 		org.apache.derby.impl.sql.compile.BinaryRelationalOperatorNode@35b835b8
> 		operator: =
> 		methodName: equals
> 		dataTypeServices: null
> 		leftOperand: 			
> 			org.apache.derby.impl.sql.compile.ColumnReference@33c433c4
> 			columnName: ID
> 			tableNumber: -1
> 			columnNumber: 0
> 			replacesAggregate: false
> 			replacesWindowFunctionCall: false
> 			tableName: null
> 			nestingLevel: -1
> 			sourceLevel: -1
> 			dataTypeServices: null
> 		rightOperand: 			
> 			org.apache.derby.impl.sql.compile.NumericConstantNode@34a434a4
> 			value: 100
> 			dataTypeServices: INTEGER NOT NULL
> 	havingClause:		
> 		org.apache.derby.impl.sql.compile.BinaryRelationalOperatorNode@24622462
> 		operator:>
> 		methodName: greaterThan
> 		dataTypeServices: null
> 		leftOperand: 			
> 			org.apache.derby.impl.sql.compile.AggregateNode@73c273c2
> 			aggregateName: COUNT(*)
> 			distinct: false
> 			operator: null
> 			methodName: null
> 			dataTypeServices: null
> 		rightOperand: 			
> 			org.apache.derby.impl.sql.compile.NumericConstantNode@23722372
> 			value: 1
> 			dataTypeServices: INTEGER NOT NULL
> -----------------------------
>
> If I provide any conditional operator like AND or OR to where clause or
> having clause, then the structure is different for both of these two.
>
> My problem is, I want to take having clause data and where clause data from
> the query.
>
> So, not able to get the data properly due to the structure is changing for
> these two items every time.
>
> Is there any way to differentiate these two elements ?
> or can we add any parent node for these two like others?
> If not possible could you please give some advice on 'where I need to change
> or which way I need to move' for solving my issue ?
>
> Thanks in advance,
> Venu.
Hi Venu,

If I understand correctly, the problem you are facing is that your 
Visitor can't tell the difference between a whereClause and a 
havingClause. They are both ValueNodes rather some more refined types 
specific to each kind of clause. And they don't contain backpointers to 
their parent SelectNodes so you can't get more information out of the 
parent node when processing the whereClause and havingClause.

I don't have any clever solution to this problem. If I were tackling 
this problem, I would maintain some extra state in the Visitor, say a 
HashSet of whereClause references and another HashSet of havingClause 
references. I would fill in these HashSets as I processed the 
SelectNodes. Then when the Visitor got to the actual whereClause and 
havingClause, it could look them up in the HashSets in order to figure 
out what it was handing at the moment. Because the clause fields are 
package private, the Visitor would need to live in the same package as 
SelectNode. So in order to not incur a sealing violation, the Visitor 
would have to be injected into derby.jar.

That's a little complicated, but it might help.

Hope that's useful,
-Rick
>
>
>
> --
> View this message in context: http://apache-database.10148.n7.nabble.com/Using-ASTParser-and-TreeWalker-for-parsing-SQL-query-tp131219p132167.html
> Sent from the Apache Derby Users mailing list archive at Nabble.com.
>


Mime
View raw message