Class TransformEliminateAssignments

All Implemented Interfaces:

public class TransformEliminateAssignments extends TransformCopy
A transform that tries to in-line/eliminate assignments

There are two classes of assignments that we can try and in-line/eliminate:

  1. Assignments where the assigned variable is used only once in a subsequent expression can be in-lined
  2. Assignments where the assigned value is never used elsewhere can be eliminated

Eligibility for In-lining

Both of these changes can only happen inside of projections as otherwise we have to assume that the user may need the resulting variable and thus we leave the assignment alone. Assignments to be in-lined must also be deterministic i.e. moving their placement in the query and thus the possible solutions they might operate must not change their outputs. Whether an expression is deterministic is defined by ExprLib.isStable(Expr).

In-lining must also respect variable scope, it is possible with a nested query to have an assignment in-lined out through a projection that projects it provided that the projection is appropriately modified.

There are also various other conditions on assignments that might be eligible for in-lining:

  • They cannot occur inside a n-ary operator (e.g. join, UNION, OPTIONAL etc.) because then in-lining would change semantics because an expression that previously was only valid for part of the query might become valid for a larger part of the query
  • They cannot be in-lined into an EXISTS or NOT EXISTS in a filter
  • They cannot be in-lined out of a DISTINCT or REDUCED because the assignment would be relevant for the purposes of distinctness

Please see JENA-780 for more information on this.

In-lining Application

Assignments may be in-lined in the following places:

  • FILTER Expressions
  • BIND and Project Expressions
  • ORDER BY Expressions if aggressive in-lining is enabled or the assigned expression is a constant

In the case of ORDER BY we only in-line assignments when aggressive mode is set unless the assignment is a constant value. This is because during order by evaluation expressions may be recomputed multiple times and so in-lining may actually hurt performance in those cases unless the expression to be in-lined is itself a constant.