2

I worked with different kind of auto generated sql statements like MS Access and Firebird sql. When I used some query builders to generate this sql snippets (Access or IBExpert) they often generate more parenthesis than needed.

I don't think about extra parenthesis around some boolean operations, but take for example the following:

select id, name from table as t
where ((t.id = @id))

When I remove them the query works perfectly fine. But why do they get generated that often?

Gustav
  • 53,498
  • 7
  • 29
  • 55
Booser
  • 576
  • 2
  • 9
  • 25

1 Answers1

3

In this case, there is no difference to the query having or not having brackets.

I've seen this kind of thing before: The parser just throws them in because it does no harm but makes the parsing code a lot simpler. When rendering a node in an AST, wrap it in brackets - simple.

Otherwise you may have to backtrack to correctly parenthesise OR conditions for example:

WHERE ((A OR B) AND (C OR D)) // correct

vs

WHERE A OR B AND C OR D // incorrect
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • It is so strange that in my given case are two pairs of them when it would be enough to add one pair. But sometimes there are some more complex meanings behind such simple things. So I started to think about them more than needed. – Booser Feb 16 '16 at 08:09
  • 1
    There's two brackets probably because the root node of the AST gets brackets too! – Bohemian Feb 16 '16 at 08:10