use of org.h2.value.TypeInfo in project h2database by h2database.
the class Comparison method optimize.
@Override
public Expression optimize(SessionLocal session) {
left = left.optimize(session);
right = right.optimize(session);
check: {
TypeInfo leftType = left.getType(), rightType = right.getType();
if (session.getMode().numericWithBooleanComparison) {
switch(compareType) {
case EQUAL:
case NOT_EQUAL:
case EQUAL_NULL_SAFE:
case NOT_EQUAL_NULL_SAFE:
int lValueType = leftType.getValueType();
if (lValueType == Value.BOOLEAN) {
if (DataType.isNumericType(rightType.getValueType())) {
break check;
}
} else if (DataType.isNumericType(lValueType) && rightType.getValueType() == Value.BOOLEAN) {
break check;
}
}
}
TypeInfo.checkComparable(leftType, rightType);
}
if (whenOperand) {
return this;
}
if (right instanceof ExpressionColumn) {
if (left.isConstant() || left instanceof Parameter) {
Expression temp = left;
left = right;
right = temp;
compareType = getReversedCompareType(compareType);
}
}
if (left instanceof ExpressionColumn) {
if (right.isConstant()) {
Value r = right.getValue(session);
if (r == ValueNull.INSTANCE) {
if ((compareType & ~1) != EQUAL_NULL_SAFE) {
return TypedValueExpression.UNKNOWN;
}
}
TypeInfo colType = left.getType(), constType = r.getType();
int constValueType = constType.getValueType();
if (constValueType != colType.getValueType() || constValueType >= Value.ARRAY) {
TypeInfo resType = TypeInfo.getHigherType(colType, constType);
// once.
if (constValueType != resType.getValueType() || constValueType >= Value.ARRAY) {
Column column = ((ExpressionColumn) left).getColumn();
right = ValueExpression.get(r.convertTo(resType, session, column));
}
}
} else if (right instanceof Parameter) {
((Parameter) right).setColumn(((ExpressionColumn) left).getColumn());
}
}
if (left.isConstant() && right.isConstant()) {
return ValueExpression.getBoolean(getValue(session));
}
if (left.isNullConstant() || right.isNullConstant()) {
// a NULL constants
if ((compareType & ~1) != EQUAL_NULL_SAFE) {
return TypedValueExpression.UNKNOWN;
} else {
Expression e = left.isNullConstant() ? right : left;
int type = e.getType().getValueType();
if (type != Value.UNKNOWN && type != Value.ROW) {
return new NullPredicate(e, compareType == NOT_EQUAL_NULL_SAFE, false);
}
}
}
return this;
}
use of org.h2.value.TypeInfo in project h2database by h2database.
the class ConditionIn method createIndexConditions.
@Override
public void createIndexConditions(SessionLocal session, TableFilter filter) {
if (not || whenOperand || !(left instanceof ExpressionColumn)) {
return;
}
ExpressionColumn l = (ExpressionColumn) left;
if (filter != l.getTableFilter()) {
return;
}
if (session.getDatabase().getSettings().optimizeInList) {
ExpressionVisitor visitor = ExpressionVisitor.getNotFromResolverVisitor(filter);
TypeInfo colType = l.getType();
for (Expression e : valueList) {
if (!e.isEverything(visitor) || !TypeInfo.haveSameOrdering(colType, TypeInfo.getHigherType(colType, e.getType()))) {
return;
}
}
filter.addIndexCondition(IndexCondition.getInList(l, valueList));
}
}
use of org.h2.value.TypeInfo in project h2database by h2database.
the class ConditionIn method optimize.
@Override
public Expression optimize(SessionLocal session) {
left = left.optimize(session);
boolean constant = !whenOperand && left.isConstant();
if (constant && left.isNullConstant()) {
return TypedValueExpression.UNKNOWN;
}
boolean allValuesConstant = true;
boolean allValuesNull = true;
TypeInfo leftType = left.getType();
for (int i = 0, l = valueList.size(); i < l; i++) {
Expression e = valueList.get(i);
e = e.optimize(session);
TypeInfo.checkComparable(leftType, e.getType());
if (e.isConstant() && !e.getValue(session).containsNull()) {
allValuesNull = false;
}
if (allValuesConstant && !e.isConstant()) {
allValuesConstant = false;
}
if (left instanceof ExpressionColumn && e instanceof Parameter) {
((Parameter) e).setColumn(((ExpressionColumn) left).getColumn());
}
valueList.set(i, e);
}
return optimize2(session, constant, allValuesConstant, allValuesNull, valueList);
}
use of org.h2.value.TypeInfo in project h2database by h2database.
the class ConditionInQuery method getValue.
private Value getValue(SessionLocal session, Value left) {
query.setSession(session);
LocalResult rows = (LocalResult) query.query(0);
if (!rows.hasNext()) {
return ValueBoolean.get(not ^ all);
}
if ((compareType & ~1) == Comparison.EQUAL_NULL_SAFE) {
return getNullSafeValueSlow(session, rows, left);
}
if (left.containsNull()) {
return ValueNull.INSTANCE;
}
if (all || compareType != Comparison.EQUAL || !session.getDatabase().getSettings().optimizeInSelect) {
return getValueSlow(session, rows, left);
}
int columnCount = query.getColumnCount();
if (columnCount != 1) {
Value[] leftValue = left.convertToAnyRow().getList();
if (columnCount == leftValue.length && rows.containsDistinct(leftValue)) {
return ValueBoolean.get(!not);
}
} else {
TypeInfo colType = rows.getColumnType(0);
if (colType.getValueType() == Value.NULL) {
return ValueNull.INSTANCE;
}
if (left.getValueType() == Value.ROW) {
left = ((ValueRow) left).getList()[0];
}
if (rows.containsDistinct(new Value[] { left })) {
return ValueBoolean.get(!not);
}
}
if (rows.containsNull()) {
return ValueNull.INSTANCE;
}
return ValueBoolean.get(not);
}
use of org.h2.value.TypeInfo in project h2database by h2database.
the class ConcatFunction method getPrecision.
private long getPrecision(int i) {
TypeInfo t = args[i].getType();
int valueType = t.getValueType();
if (valueType == Value.NULL) {
return 0L;
} else if (DataType.isCharacterStringType(valueType)) {
return t.getPrecision();
} else {
return Long.MAX_VALUE;
}
}
Aggregations