For some time now, the Derby code has taken advantage of the Visitor Pattern to organize various code which manipulates query trees during compilation. For example, in DERBY-2085, I enhanced the visitor which examines aggregate functions (SUM, MAX, COUNT, etc.) in the query tree; the visitor pattern made it straightforward to issue a clear error message for an invalid query.
Recently, a Derby user discovered that the following two queries were processed differently:
WHERE 1=1 AND c1 >= ?
WHERE 2>1 AND c1 >= ?
Since 1=1 and 2>1 both are constant expressions which evaluate to TRUE, it seems reasonable to expect that both would behave the same, but this was not the case, which led to the filing of DERBY-4416.
Knut Anders Hatlen, who has been doing some great work on Derby recently, was studying DERBY-4416 when he realized that this whole issue of examining the query tree for constant expressions (which could be simplified and pre-computed at compile time) was complicated, in part, because the Derby visitor pattern was visiting the tree top-down, and if we instead had a way to visit the tree bottom-up, the expression simplification would be much easier to implement.
So now we have a proposed enhancement to the query tree visitor which allows for visiting the tree in either fashion: bottom-up, or top-down. Both styles appear to be valuable, and it's quite straightforward to make the visitor implementation handle both, with a simple ordering flag which controls the behavior.