JSqlParser icon indicating copy to clipboard operation
JSqlParser copied to clipboard

Get the position of an Where/From AST item in the input

Open Wisser opened this issue 4 years ago • 12 comments

(Sorry for opening a new issue. I could not find a way to reopen the previous one.)

Unfortunately, this does not work for all items. Or am I missing something?

For example, I encountered the following problems:

FromItem (PlainSelect#getFromItem) is not an ASTNodeAccess. Expression (PlainSelect#getWhere) is null for complex expressions.

Originally posted by @Wisser in https://github.com/JSQLParser/JSqlParser/issues/1219#issuecomment-922805082

Wisser avatar Sep 20 '21 10:09 Wisser

Greetings.

Apologies, I am just guessing:

FromItem is an interface and you would need to cast it to one of its implementations, which will give the ASTNode.

Same for the Expression.

Please post your particular query and the code you have written so far. I would like to experiment a bit with it.

manticore-projects avatar Sep 20 '21 23:09 manticore-projects

Although SelectItem is also an interface but extends ASTNodeAccess.

To me, it looks like some housekeeping was needed on these old parts. Post your samples and I will see, if I can do anything.

manticore-projects avatar Sep 21 '21 00:09 manticore-projects

Hello,

thank you very much for your effort.

It looks like not all implementations of FromItem also implement ASTNodeAccess. Also, ASTNodeAccess.getASTNode() is null in some cases.

You'll find some test code demonstrating both in the attachment. It produces for me:

'1 + 2': is ASTNodeAccess but ASTNode is null
'Values(1)': is no ASTNodeAccess. It's a net.sf.jsqlparser.statement.select.TableFunction
'x IS NULL': is ASTNodeAccess but ASTNode is null
'x + y': is ASTNodeAccess but ASTNode is null

Issue1339.zip

Wisser avatar Sep 21 '21 06:09 Wisser

Thank you for the examples, I will try to look at it and fix it -- but no promises.

manticore-projects avatar Sep 21 '21 07:09 manticore-projects

As always the connection to ASTNodes was request driven (and is). Unfortunately JavaCC does not provide some kind of automatism to achieve that. Therefore we have to construct this manually.

Another point is, that not all kind of productions create ASTNodes. I tested this and found a performance degradation of nearly 50%. However, maybe newer versions of JavaCC does provide this in a more performant way.

wumpz avatar Sep 30 '21 05:09 wumpz

Thanks for the clarification. Would it be possible to adjust the parser so that every Expression (which is in fact an ASTNodeAccess), always returns a non-zero ASTNode?

Wisser avatar Sep 30 '21 06:09 Wisser

Would it not be better to write a Visitor, which builds the AST with additional information on each node.

manticore-projects avatar Sep 30 '21 06:09 manticore-projects

I'm afraid I don't understand what you mean.

Wisser avatar Sep 30 '21 06:09 Wisser

JSQLParser supports the Visitor Pattern to travers through all the elements of a Statement or Expression. This is used for the Deparser and the Validator already.

Now, instead of Deparsing into a String you could use the same mechanic to write a Tree or a XML representing a Tree.

manticore-projects avatar Sep 30 '21 06:09 manticore-projects

I already use a Visitor. That is not the problem. I want to find the exact position of Expressions (and other phrases) in the SQL statement underlying the AST (the parser's input). As mentioned here: https://github.com/JSQLParser/JSqlParser/issues/1219

Wisser avatar Sep 30 '21 06:09 Wisser

I want to find the exact position of Expressions (and other phrases) in the SQL statement underlying the AST (the parser's input). As mentioned here: #1219

Yes, I understand that. Please look into the Deparser Visitor and imagine to write Nodes into a Tree instead of Strings into a StringBuilder. Each Node could hold the Start/End Position and the Caption and the Node Class Name as additional attribute.

manticore-projects avatar Sep 30 '21 06:09 manticore-projects

I still don't get it, I'm afraid. The two statements:

Select 1

and

Select
1

(two lines)

lead to the same AST. Only the position information differs. But I do not get this information.

Wisser avatar Sep 30 '21 09:09 Wisser