CSDL filter with parentheses


#1

Both of the filters below validate. When are parentheses required in a CSDL fillter?

fb.language == “en” and fb.author.country == “United States” and fb.content any “coca-cola, fanta, sprite” or fb.parent.language == “en” and fb.parent.author.country == “United States” and fb.parent.content any “coca-cola, fanta, sprite”

(fb.language == “en” and fb.author.country == “United States” and fb.content any “coca-cola, fanta, sprite”) or (fb.parent.language == “en” and fb.parent.author.country == “United States” and fb.parent.content any “coca-cola, fanta, sprite”)


#2

Hi @kkim

As you note, both of these are valid CSDL, so, in general, whether or not you need parentheses will depend on the the logic you want the CSDL to represent.

The key thing to note is that AND takes precedence over OR.

Taking your example, where you have:
a AND b AND c OR d AND e AND f
since the ANDs are always evaluated first, it is logically equivalent to
(a AND b AND c) OR (d AND e AND f)
So, in your case, you can do either – they are both valid CSDL and both logically equivalent.

Note that if you had CSDL that was something like
fb.author.gender == “female” and (fb.language == “en” or fb.author.country == “United States”)
your parentheses become necessary.
In this case, omitting the parentheses would still be valid CSDL, but would result in different data being returned. With the parenthesis, we are getting all stories from women that are either in English or from the US. If we did not have parentheses, we would be getting all stories from women in English, or stories from authors of any gender in the United States.

Hope that helps!