Class Expression
- All Implemented Interfaces:
Serializable
,Cloneable
This class can represent constant expressions (expressions that have no variable input), as well as variable expressions. Optimizations may be possible when evaluating constant expressions.
Some examples of constant expressions;
( 9 + 3 ) * 90
( ? * 9 ) / 1
lower("CaPS MUMma")
40 invalid input: '&' 0x0FF != 39
Some examples of variable expressions;
upper(Part.description)
Part.id >= 50
VendorMakesPart.part_id == Part.id
Part.value_of invalid input: '<'= Part.cost_of / 4
NOTE: the expression is stored in postfix orientation. eg. "8 + 9 * 3" becomes "8,9,3,*,+"
NOTE: This class is NOT thread safe. Do not use an expression instance between threads.
- Author:
- Tobias Downer
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new Expression.Expression
(Expression exp) Constructs a copy of the given Expression.Expression
(Expression exp1, Operator op, Expression exp2) Constructs a new Expression from the concatination of expression1 and expression2 and the operator for them.Expression
(Object ob) Constructs a new Expression with a single object element. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addElement
(Object ob) Adds a new element into the expression.void
addOperator
(Operator op) Adds a new operator into the expression.Returns a complete list of all element objects that are in this expression and in the parameters of the functions of this expression.Returns a complete List of Variable objects in this expression not including correlated variables.breakByOperator
(ArrayList list, String logical_op) Breaks this expression into a list of sub-expressions that are split by the given operator.clone()
Performs a deep clone of this object, calling 'clone' on any elements that are mutable or shallow copying immutable members.concat
(Expression expr) Merges an expression with this expression.boolean
Returns true if the expression contains a NOT operator somewhere in it.void
Copies the text from the given expression.discoverCorrelatedVariables
(int level, ArrayList list) Discovers all the correlated variables in this expression.discoverTableNames
(ArrayList list) Discovers all the tables in the sub-queries of this expression.elementAt
(int n) Returns the element at the given position in the postfix list.evaluate
(GroupResolver group, VariableResolver resolver, QueryContext context) Evaluates this expression and returns an Object that represents the result of the evaluation.evaluate
(VariableResolver resolver, QueryContext context) Evaluation without a grouping table.Returns the end Expression of this expression.Returns the QueryPlanNode object in this expression if it evaluates to a single QueryPlanNode, otherwise returns null.Returns the Variable if this expression evaluates to a single variable, otherwise returns null.boolean
hasAggregateFunction
(QueryContext context) Cascades through the expression and if any aggregate functions are found returns true, otherwise returns false.boolean
Returns true if the expression has a subquery (eg 'in ( select ...boolean
Returns true if the expression doesn't include any variables or non constant functions (is constant).last()
Returns the element at the end of the postfix list (the last element).static Expression
Static method that parses the given string which contains an expression into an Expression object.void
prepare
(ExpressionPreparer preparer) A general prepare that cascades through the expression and its parents and substitutes an elements that the preparer wants to substitute.returnTType
(VariableResolver resolver, QueryContext context) Determines the type of object this expression evaluates to.void
setElementAt
(int n, Object ob) Sets the element at the given position in the postfix list.static Expression
Generates a simple expression from two objects and an operator.int
size()
Returns the number of elements and operators that are in this postfix list.split()
Returns an array of two Expression objects that represent the left hand and right and side of the last operator in the post fix notation.text()
Returns the StringBuffer that we can use to append plain text representation as we are parsing the expression.toString()
Returns a string representation of this object for diagnostic purposes.
-
Constructor Details
-
Expression
public Expression()Constructs a new Expression. -
Expression
Constructs a new Expression with a single object element. -
Expression
Constructs a copy of the given Expression. -
Expression
Constructs a new Expression from the concatination of expression1 and expression2 and the operator for them.
-
-
Method Details
-
text
Returns the StringBuffer that we can use to append plain text representation as we are parsing the expression. -
copyTextFrom
Copies the text from the given expression. -
parse
Static method that parses the given string which contains an expression into an Expression object. For example, this will parse strings such as '(a + 9) * 2 = b' or 'upper(concat('12', '56', id))'.Care should be taken to not use this method inside an inner loop because it creates a lot of objects.
-
simple
Generates a simple expression from two objects and an operator. -
addElement
Adds a new element into the expression. String, BigNumber, Boolean and Variable are the types of elements allowed.Must be added in postfix order.
-
concat
Merges an expression with this expression. For example, given the expression 'ab', if the expression 'abc+-' was added the expression would become 'ababc+-'.This method is useful when copying parts of other expressions when forming an expression.
This always returns this expression. This does not change 'text()'.
-
addOperator
Adds a new operator into the expression. Operators are represented as an Operator (eg. ">", "+", "invalid input: '<'invalid input: '<'", "!=" )Must be added in postfix order.
-
size
public int size()Returns the number of elements and operators that are in this postfix list. -
elementAt
Returns the element at the given position in the postfix list. Note, this can return Operator's. -
last
Returns the element at the end of the postfix list (the last element). -
setElementAt
Sets the element at the given position in the postfix list. This should be called after the expression has been setup to alter variable alias names, etc. -
allVariables
Returns a complete List of Variable objects in this expression not including correlated variables. -
allElements
Returns a complete list of all element objects that are in this expression and in the parameters of the functions of this expression. -
prepare
A general prepare that cascades through the expression and its parents and substitutes an elements that the preparer wants to substitute.NOTE: This will not cascade through to the parameters of Function objects however it will cascade through FunctionDef parameters. For this reason you MUST call 'prepareFunctions' after this method.
- Throws:
DatabaseException
-
isConstant
public boolean isConstant()Returns true if the expression doesn't include any variables or non constant functions (is constant). Note that a correlated variable is considered a constant. -
hasSubQuery
public boolean hasSubQuery()Returns true if the expression has a subquery (eg 'in ( select ... )') somewhere in it (this cascades through function parameters also). -
containsNotOperator
public boolean containsNotOperator()Returns true if the expression contains a NOT operator somewhere in it. -
discoverTableNames
Discovers all the tables in the sub-queries of this expression. This is used for determining all the tables that a query plan touches. -
getQueryPlanNode
Returns the QueryPlanNode object in this expression if it evaluates to a single QueryPlanNode, otherwise returns null. -
getVariable
Returns the Variable if this expression evaluates to a single variable, otherwise returns null. A correlated variable will not be returned. -
split
Returns an array of two Expression objects that represent the left hand and right and side of the last operator in the post fix notation. For example, (a + b) - (c + d) will return { (a + b), (c + d) }. Or more a more useful example is;id + 3 > part_id - 2 will return ( id + 3, part_id - 2 }
-
getEndExpression
Returns the end Expression of this expression. For example, an expression of 'ab' has an end expression of 'b'. The expression 'abc+=' has an end expression of 'abc+='.This is a useful method to call in the middle of an Expression object being formed. It allows for the last complete expression to be returned.
If this is called when an expression is completely formed it will always return the complete expression.
-
breakByOperator
Breaks this expression into a list of sub-expressions that are split by the given operator. For example, given the expression;(a = b AND b = c AND (a = 2 OR c = 1))
Calling this method with logical_op = "and" will return a list of the three expressions.
This is a common function used to split up an expressions into logical components for processing.
-
evaluate
Evaluates this expression and returns an Object that represents the result of the evaluation. The passed VariableResolver object is used to resolve the variable name to a value. The GroupResolver object is used if there are any aggregate functions in the evaluation - this can be null if evaluating an expression without grouping aggregates. The query context object contains contextual information about the environment of the query.NOTE: This method is gonna be called a lot, so we need it to be optimal.
NOTE: This method is not thread safe! The reason it's not safe is because of the evaluation stack.
-
evaluate
Evaluation without a grouping table. -
hasAggregateFunction
Cascades through the expression and if any aggregate functions are found returns true, otherwise returns false. -
returnTType
Determines the type of object this expression evaluates to. We determine this by looking at the last element of the expression. If the last element is a TType object, it returns the type. If the last element is a Function, Operator or Variable then it returns the type that these objects have set as their result type. -
clone
Performs a deep clone of this object, calling 'clone' on any elements that are mutable or shallow copying immutable members.- Overrides:
clone
in classObject
- Throws:
CloneNotSupportedException
-
toString
Returns a string representation of this object for diagnostic purposes.
-