
org.lealone.sql.expression.condition.ConditionInConstantSet Maven / Gradle / Ivy
/*
* Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.lealone.sql.expression.condition;
import java.util.ArrayList;
import java.util.HashSet;
import org.lealone.common.util.StatementBuilder;
import org.lealone.db.session.ServerSession;
import org.lealone.db.value.Value;
import org.lealone.db.value.ValueBoolean;
import org.lealone.db.value.ValueNull;
import org.lealone.sql.expression.Expression;
import org.lealone.sql.expression.ExpressionColumn;
import org.lealone.sql.expression.visitor.ExpressionVisitor;
import org.lealone.sql.optimizer.IndexCondition;
import org.lealone.sql.optimizer.TableFilter;
/**
* Used for optimised IN(...) queries where the contents of the IN list are all
* constant and of the same type.
*
* Checking using a HashSet is has time complexity O(1), instead of O(n) for
* checking using an array.
*/
public class ConditionInConstantSet extends Condition {
private Expression left;
private int queryLevel;
private final ArrayList valueList;
private final HashSet valueSet;
/**
* Create a new IN(..) condition.
*
* @param session the session
* @param left the expression before IN
* @param valueList the value list (at least two elements)
*/
public ConditionInConstantSet(ServerSession session, Expression left,
ArrayList valueList) {
this.left = left;
this.valueList = valueList;
this.valueSet = new HashSet(valueList.size());
int type = left.getType();
for (Expression expression : valueList) {
valueSet.add(expression.getValue(session).convertTo(type));
}
}
public Expression getLeft() {
return left;
}
public HashSet getValueSet() {
return valueSet;
}
public void setQueryLevel(int level) {
this.queryLevel = Math.max(level, this.queryLevel);
}
@Override
public Value getValue(ServerSession session) {
Value x = left.getValue(session);
if (x == ValueNull.INSTANCE) {
return x;
}
boolean result = valueSet.contains(x);
if (!result) {
boolean setHasNull = valueSet.contains(ValueNull.INSTANCE);
if (setHasNull) {
return ValueNull.INSTANCE;
}
}
return ValueBoolean.get(result);
}
@Override
public Expression optimize(ServerSession session) {
left = left.optimize(session);
return this;
}
@Override
public void createIndexConditions(ServerSession session, TableFilter filter) {
if (!(left instanceof ExpressionColumn)) {
return;
}
ExpressionColumn l = (ExpressionColumn) left;
if (filter != l.getTableFilter()) {
return;
}
if (session.getDatabase().getSettings().optimizeInList) {
filter.addIndexCondition(IndexCondition.getInList(l, valueList));
return;
}
}
@Override
public String getSQL() {
StatementBuilder buff = new StatementBuilder("(");
buff.append(left.getSQL()).append(" IN(");
for (Expression e : valueList) {
buff.appendExceptFirst(", ");
buff.append(e.getSQL());
}
return buff.append("))").toString();
}
@Override
public int getCost() {
int cost = left.getCost();
return cost;
}
/**
* Add an additional element if possible. Example: given two conditions
* A IN(1, 2) OR A=3, the constant 3 is added: A IN(1, 2, 3).
*
* @param other the second condition
* @return null if the condition was not added, or the new condition
*/
Expression getAdditional(ServerSession session, Comparison other) {
Expression add = other.getIfEquals(left);
if (add != null) {
if (add.isConstant()) {
valueList.add(add);
valueSet.add(add.getValue(session).convertTo(left.getType()));
return this;
}
}
return null;
}
@Override
public R accept(ExpressionVisitor visitor) {
return visitor.visitConditionInConstantSet(this);
}
}