Derby
  1. Derby
  2. DERBY-4421

Allow Visitors to process the nodes bottom-up

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 10.6.1.0
    • Fix Version/s: 10.6.1.0
    • Component/s: SQL
    • Labels:
      None

      Description

      Currently, QueryTreeNode.accept() walks the tree top-down, always calling
      visit() on the parent before it calls visit() on the children. Although this
      is fine in most cases, there are use cases where visiting the nodes
      bottom-up would be better. One example is mentioned in DERBY-4416. The
      visitor posted there looks for binary comparison operators and checks
      whether both operands are constants. If they are, the operator is replaced
      with a boolean constant.

      Take this expression as an example: (1<2)=(2>1)

      The query tree looks like this:

      =
      / \
      / \
      < >
      / \ / \
      / \ / \
      1 2 2 1

      If we walk the tree top-down with the said visitor, the = node doesn't have
      constant operands when it's visited. The < and > operators do have constant
      operands, and they're both replaced with constant TRUE. This means the
      expression (1<2)=(2>1) is rewritten to TRUE=TRUE, and that's how far the
      transformation goes.

      If the tree had been processed bottom-up, we would start with the < and >
      operators, and again replace them with TRUE. The query tree would therefore
      have been transformed to this intermediate form when the = operator was
      visited:

      =
      / \
      / \
      TRUE TRUE

      This is the same as the end result when visiting top-down, but now the =
      operator hasn't been visited yet. Since both the operands of the = operator
      are constants, the visitor will perform yet another transformation so the
      tree is simplified further and ends up as:

      TRUE

      1. d4421-2a.stat
        2 kB
        Knut Anders Hatlen
      2. d4421-2a.diff
        16 kB
        Knut Anders Hatlen
      3. d4421-1a.stat
        3 kB
        Knut Anders Hatlen
      4. d4421-1a.diff
        40 kB
        Knut Anders Hatlen

        Issue Links

          Activity

            People

            • Assignee:
              Knut Anders Hatlen
              Reporter:
              Knut Anders Hatlen
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development