Class BooleanOperator
- java.lang.Object
-
- edu.caltech.nanodb.expressions.Expression
-
- edu.caltech.nanodb.expressions.BooleanOperator
-
- All Implemented Interfaces:
java.lang.Cloneable
public class BooleanOperator extends Expression
This class provides the standard Boolean logical operators AND, OR, and NOT, for two or more terms (or exactly one term, in the case of NOT). In addition, there are several methods provided for manipulating Boolean expressions, since many plan equivalence-rules involve manipulations of the Boolean logical expressions associated with Select and Theta-Join plan nodes.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
BooleanOperator.Type
This enumeration specifies the different kinds of Boolean operators that this class can implement.
-
Field Summary
Fields Modifier and Type Field Description private java.util.ArrayList<Expression>
terms
The list of one or more terms in this expression.private BooleanOperator.Type
type
Specifies the type of this Boolean operator.
-
Constructor Summary
Constructors Constructor Description BooleanOperator(BooleanOperator.Type type)
BooleanOperator(BooleanOperator.Type type, java.util.Collection<Expression> terms)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private java.util.List<Expression>
_getTermsReferencingAllTables(java.lang.String[] tableNames, boolean remove)
This is a private helper function used by both thegetTermsReferencingAllTables(java.lang.String...)
andremoveTermsReferencingAllTables(java.lang.String...)
methods.void
addTerm(Expression term)
protected java.lang.Object
clone()
Creates a copy of expression.boolean
equals(java.lang.Object obj)
Performs a value-eqality test for whether the specified object is an expression with the same structure and contents.java.lang.Object
evaluate(Environment env)
Evaluates this expression object in the context of the specified environment.ColumnInfo
getColumnInfo(Schema schema)
Returns aColumnInfo
object describing the type (and possibly the name) of the expression's result.int
getNumTerms()
Returns the number of terms in the boolean expression.Expression
getTerm(int i)
java.util.List<Expression>
getTermsReferencingAllTables(java.lang.String... tableNames)
This method finds and returns a list of all terms in this Boolean expression that reference the exact set of tables specified in the arguments.BooleanOperator.Type
getType()
Returns the type of this Boolean expression.int
hashCode()
Computes the hash-code of an Expression.boolean
hasTermsReferencingAllTables(java.lang.String... tableNames)
This method returns true if this Boolean expression contains any terms that reference the exact set of tables specified in the arguments.void
removeTerm(int i)
Removes the ith term, starting from 0.java.util.List<Expression>
removeTermsReferencingAllTables(java.lang.String... tableNames)
This method finds, removes, and returns a list of all terms in this Boolean expression that reference the exact set of tables specified in the arguments.Expression
simplify()
Simplifies a Boolean expression by eliminating and de-nesting as much of the expression as possible.java.lang.String
toString()
Returns a string representation of this Boolean logical expression and its subexpressions, including parentheses where necessary to specify precedence.Expression
traverse(ExpressionProcessor p)
This method allows the entire expression tree to be traversed node by node, either for analysis or for transformation.-
Methods inherited from class edu.caltech.nanodb.expressions.Expression
duplicate, evaluate, evaluatePredicate, evaluatePredicate, getAllSymbols, hasSymbols
-
-
-
-
Field Detail
-
type
private BooleanOperator.Type type
Specifies the type of this Boolean operator.
-
terms
private java.util.ArrayList<Expression> terms
The list of one or more terms in this expression. If the expression type isBooleanOperator.Type.NOT_EXPR
then this will contain exactly one term.
-
-
Constructor Detail
-
BooleanOperator
public BooleanOperator(BooleanOperator.Type type, java.util.Collection<Expression> terms)
-
BooleanOperator
public BooleanOperator(BooleanOperator.Type type)
-
-
Method Detail
-
getType
public BooleanOperator.Type getType()
Returns the type of this Boolean expression.- Returns:
- the type of this Boolean expression.
-
addTerm
public void addTerm(Expression term)
-
getTerm
public Expression getTerm(int i)
-
getColumnInfo
public ColumnInfo getColumnInfo(Schema schema) throws SchemaNameException
Description copied from class:Expression
Returns aColumnInfo
object describing the type (and possibly the name) of the expression's result.- Specified by:
getColumnInfo
in classExpression
- Parameters:
schema
- a schema object that can be used to look up name and type details for symbols referenced by the expression.- Returns:
- a column-information object describing the type (and possibly the name and table-name) of this expression's result
- Throws:
SchemaNameException
- if a symbol cannot be resolved, either because it doesn't appear in the schema, or because the name is ambiguous.
-
evaluate
public java.lang.Object evaluate(Environment env) throws ExpressionException
Description copied from class:Expression
Evaluates this expression object in the context of the specified environment. The environment provides any external information necessary to evaluate the expression, such as the current tuples loaded from tables referenced within the expression.- Specified by:
evaluate
in classExpression
- Parameters:
env
- the environment to look up symbol-values from, when evaluating the expression- Returns:
- the result of the expression evaluation
- Throws:
ExpressionException
- if the expression cannot be evaluated for some reason.
-
traverse
public Expression traverse(ExpressionProcessor p)
Description copied from class:Expression
This method allows the entire expression tree to be traversed node by node, either for analysis or for transformation. The
ExpressionProcessor
instance receives notifications as each node in the expression is entered and left.The expression tree can also be manipulated by this traversal process, depending on what the
ExpressionProcessor
wants to do. If the expression node thattraverse()
is invoked on, needs to be replaced with a new expression node, the replacement is returned by thetraverse()
method. (TheExpressionProcessor
specifies the replacement as the return-value from theExpressionProcessor.leave(Expression)
method.)- Specified by:
traverse
in classExpression
- Parameters:
p
- the object that performs analysis or transformation of the expression tree- Returns:
- an
Expression
node to replace this node, ornull
if no changes are to be made.
-
toString
public java.lang.String toString()
Returns a string representation of this Boolean logical expression and its subexpressions, including parentheses where necessary to specify precedence.- Overrides:
toString
in classjava.lang.Object
-
hasTermsReferencingAllTables
public boolean hasTermsReferencingAllTables(java.lang.String... tableNames)
This method returns true if this Boolean expression contains any terms that reference the exact set of tables specified in the arguments. See the documentation forgetTermsReferencingAllTables(java.lang.String...)
for more details on what is considered to be a "matching" term by this method.- Parameters:
tableNames
- a collection of table-names to look for in the terms.- Returns:
- A list of terms that reference all of the tables specified in the input list, and no others.
- Design Note:
- This method is slightly more efficient than the get/remove methods, since it stops after it has found the first term that satisfies the specified conditions. However, if there will be a subsequent call to the get or remove method, it's probably most efficient to just call it directly.
-
getTermsReferencingAllTables
public java.util.List<Expression> getTermsReferencingAllTables(java.lang.String... tableNames)
This method finds and returns a list of all terms in this Boolean expression that reference the exact set of tables specified in the arguments. If a term references other tables outside of this set then it will not be returned. If a term doesn't reference some table in this set then it will not be returned.- Parameters:
tableNames
- a collection of table-names to look for in the terms.- Returns:
- A list of terms that reference all of the tables specified in the input list, and no others.
-
removeTermsReferencingAllTables
public java.util.List<Expression> removeTermsReferencingAllTables(java.lang.String... tableNames)
This method finds, removes, and returns a list of all terms in this Boolean expression that reference the exact set of tables specified in the arguments. If a term references other tables outside of this set then it will not be returned. If a term doesn't reference some table in this set then it will not be returned.The sole difference between this and
getTermsReferencingAllTables(java.lang.String...)
is that this method also removes the found terms from this Boolean expression object.- Parameters:
tableNames
- a collection of table-names to look for in the terms.- Returns:
- A list of terms that reference all of the tables specified in the input list, and no others. These terms are also removed from the Boolean expression object.
-
_getTermsReferencingAllTables
private java.util.List<Expression> _getTermsReferencingAllTables(java.lang.String[] tableNames, boolean remove)
This is a private helper function used by both thegetTermsReferencingAllTables(java.lang.String...)
andremoveTermsReferencingAllTables(java.lang.String...)
methods.- Parameters:
tableNames
- an array of table-names to look for in the terms.remove
- if true then matching terms will be removed from this Boolean operator's list of terms.- Returns:
- A list of terms that reference all of the tables specified in the input list, and no others.
-
getNumTerms
public int getNumTerms()
Returns the number of terms in the boolean expression.- Returns:
- the number of terms in the boolean expression.
-
removeTerm
public void removeTerm(int i)
Removes the ith term, starting from 0.- Parameters:
i
- the index of the term to remove.- Throws:
java.lang.IllegalArgumentException
- if the specified index is invalid.
-
simplify
public Expression simplify()
Simplifies a Boolean expression by eliminating and de-nesting as much of the expression as possible.- Overrides:
simplify
in classExpression
- Returns:
- a reference to an expression, either a simplified version of this expression, or the original unmodified expression
-
equals
public boolean equals(java.lang.Object obj)
Performs a value-eqality test for whether the specified object is an expression with the same structure and contents.- Specified by:
equals
in classExpression
- Parameters:
obj
- the object to which we are comparing
-
hashCode
public int hashCode()
Computes the hash-code of an Expression.- Specified by:
hashCode
in classExpression
-
clone
protected java.lang.Object clone() throws java.lang.CloneNotSupportedException
Creates a copy of expression.- Overrides:
clone
in classExpression
- Throws:
java.lang.CloneNotSupportedException
-
-