use of org.h2.expression.Comparison in project h2database by h2database.
the class IndexCondition method getSQL.
/**
* Get the SQL snippet of this comparison.
*
* @return the SQL snippet
*/
public String getSQL() {
if (compareType == Comparison.FALSE) {
return "FALSE";
}
StatementBuilder buff = new StatementBuilder();
buff.append(column.getSQL());
switch(compareType) {
case Comparison.EQUAL:
buff.append(" = ");
break;
case Comparison.EQUAL_NULL_SAFE:
buff.append(" IS ");
break;
case Comparison.BIGGER_EQUAL:
buff.append(" >= ");
break;
case Comparison.BIGGER:
buff.append(" > ");
break;
case Comparison.SMALLER_EQUAL:
buff.append(" <= ");
break;
case Comparison.SMALLER:
buff.append(" < ");
break;
case Comparison.IN_LIST:
buff.append(" IN(");
for (Expression e : expressionList) {
buff.appendExceptFirst(", ");
buff.append(e.getSQL());
}
buff.append(')');
break;
case Comparison.IN_QUERY:
buff.append(" IN(");
buff.append(expressionQuery.getPlanSQL());
buff.append(')');
break;
case Comparison.SPATIAL_INTERSECTS:
buff.append(" && ");
break;
default:
DbException.throwInternalError("type=" + compareType);
}
if (expression != null) {
buff.append(expression.getSQL());
}
return buff.toString();
}
use of org.h2.expression.Comparison in project h2database by h2database.
the class SortOrder method compare.
/**
* Compare two expression lists.
*
* @param a the first expression list
* @param b the second expression list
* @return the result of the comparison
*/
@Override
public int compare(Value[] a, Value[] b) {
for (int i = 0, len = queryColumnIndexes.length; i < len; i++) {
int idx = queryColumnIndexes[i];
int type = sortTypes[i];
Value ao = a[idx];
Value bo = b[idx];
boolean aNull = ao == ValueNull.INSTANCE, bNull = bo == ValueNull.INSTANCE;
if (aNull || bNull) {
if (aNull == bNull) {
continue;
}
return compareNull(aNull, type);
}
int comp = database.compare(ao, bo);
if (comp != 0) {
return (type & DESCENDING) == 0 ? comp : -comp;
}
}
return 0;
}
use of org.h2.expression.Comparison in project h2database by h2database.
the class CompareLike method optimize.
@Override
public Expression optimize(Session session) {
left = left.optimize(session);
right = right.optimize(session);
if (left.getType() == Value.STRING_IGNORECASE) {
ignoreCase = true;
}
if (left.isValueSet()) {
Value l = left.getValue(session);
if (l == ValueNull.INSTANCE) {
// NULL LIKE something > NULL
return ValueExpression.getNull();
}
}
if (escape != null) {
escape = escape.optimize(session);
}
if (right.isValueSet() && (escape == null || escape.isValueSet())) {
if (left.isValueSet()) {
return ValueExpression.get(getValue(session));
}
Value r = right.getValue(session);
if (r == ValueNull.INSTANCE) {
// something LIKE NULL > NULL
return ValueExpression.getNull();
}
Value e = escape == null ? null : escape.getValue(session);
if (e == ValueNull.INSTANCE) {
return ValueExpression.getNull();
}
String p = r.getString();
initPattern(p, getEscapeChar(e));
if (invalidPattern) {
return ValueExpression.getNull();
}
if ("%".equals(p)) {
// optimization for X LIKE '%': convert to X IS NOT NULL
return new Comparison(session, Comparison.IS_NOT_NULL, left, null).optimize(session);
}
if (isFullMatch()) {
// optimization for X LIKE 'Hello': convert to X = 'Hello'
Value value = ValueString.get(patternString);
Expression expr = ValueExpression.get(value);
return new Comparison(session, Comparison.EQUAL, left, expr).optimize(session);
}
isInit = true;
}
return this;
}
use of org.h2.expression.Comparison in project h2database by h2database.
the class Comparison method getAdditional.
/**
* Get an additional condition if possible. Example: given two conditions
* A=B AND B=C, the new condition A=C is returned. Given the two conditions
* A=1 OR A=2, the new condition A IN(1, 2) is returned.
*
* @param session the session
* @param other the second condition
* @param and true for AND, false for OR
* @return null or the third condition
*/
Expression getAdditional(Session session, Comparison other, boolean and) {
if (compareType == other.compareType && compareType == EQUAL) {
boolean lc = left.isConstant();
boolean rc = right.isConstant();
boolean l2c = other.left.isConstant();
boolean r2c = other.right.isConstant();
String l = left.getSQL();
String l2 = other.left.getSQL();
String r = right.getSQL();
String r2 = other.right.getSQL();
if (and) {
// must not compare constants. example: NOT(B=2 AND B=3)
if (!(rc && r2c) && l.equals(l2)) {
return new Comparison(session, EQUAL, right, other.right);
} else if (!(rc && l2c) && l.equals(r2)) {
return new Comparison(session, EQUAL, right, other.left);
} else if (!(lc && r2c) && r.equals(l2)) {
return new Comparison(session, EQUAL, left, other.right);
} else if (!(lc && l2c) && r.equals(r2)) {
return new Comparison(session, EQUAL, left, other.left);
}
} else {
// a=b OR a=c
Database db = session.getDatabase();
if (rc && r2c && l.equals(l2)) {
return new ConditionIn(db, left, new ArrayList<>(Arrays.asList(right, other.right)));
} else if (rc && l2c && l.equals(r2)) {
return new ConditionIn(db, left, new ArrayList<>(Arrays.asList(right, other.left)));
} else if (lc && r2c && r.equals(l2)) {
return new ConditionIn(db, right, new ArrayList<>(Arrays.asList(left, other.right)));
} else if (lc && l2c && r.equals(r2)) {
return new ConditionIn(db, right, new ArrayList<>(Arrays.asList(left, other.left)));
}
}
}
return null;
}
use of org.h2.expression.Comparison in project h2database by h2database.
the class Table method getBestPlanItem.
/**
* Get the best plan for the given search mask.
*
* @param session the session
* @param masks per-column comparison bit masks, null means 'always false',
* see constants in IndexCondition
* @param filters all joined table filters
* @param filter the current table filter index
* @param sortOrder the sort order
* @param allColumnsSet the set of all columns
* @return the plan item
*/
public PlanItem getBestPlanItem(Session session, int[] masks, TableFilter[] filters, int filter, SortOrder sortOrder, HashSet<Column> allColumnsSet) {
PlanItem item = new PlanItem();
item.setIndex(getScanIndex(session));
item.cost = item.getIndex().getCost(session, null, filters, filter, null, allColumnsSet);
Trace t = session.getTrace();
if (t.isDebugEnabled()) {
t.debug("Table : potential plan item cost {0} index {1}", item.cost, item.getIndex().getPlanSQL());
}
ArrayList<Index> indexes = getIndexes();
IndexHints indexHints = getIndexHints(filters, filter);
if (indexes != null && masks != null) {
for (int i = 1, size = indexes.size(); i < size; i++) {
Index index = indexes.get(i);
if (isIndexExcludedByHints(indexHints, index)) {
continue;
}
double cost = index.getCost(session, masks, filters, filter, sortOrder, allColumnsSet);
if (t.isDebugEnabled()) {
t.debug("Table : potential plan item cost {0} index {1}", cost, index.getPlanSQL());
}
if (cost < item.cost) {
item.cost = cost;
item.setIndex(index);
}
}
}
return item;
}
Aggregations