Search in sources :

Example 1 with TypeInfo

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;
}
Also used : TypedValueExpression(org.h2.expression.TypedValueExpression) ValueExpression(org.h2.expression.ValueExpression) Expression(org.h2.expression.Expression) ExpressionColumn(org.h2.expression.ExpressionColumn) Column(org.h2.table.Column) Value(org.h2.value.Value) Parameter(org.h2.expression.Parameter) TypeInfo(org.h2.value.TypeInfo) ExpressionColumn(org.h2.expression.ExpressionColumn)

Example 2 with TypeInfo

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));
    }
}
Also used : TypedValueExpression(org.h2.expression.TypedValueExpression) ValueExpression(org.h2.expression.ValueExpression) Expression(org.h2.expression.Expression) ExpressionVisitor(org.h2.expression.ExpressionVisitor) TypeInfo(org.h2.value.TypeInfo) ExpressionColumn(org.h2.expression.ExpressionColumn)

Example 3 with TypeInfo

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);
}
Also used : TypedValueExpression(org.h2.expression.TypedValueExpression) ValueExpression(org.h2.expression.ValueExpression) Expression(org.h2.expression.Expression) Parameter(org.h2.expression.Parameter) TypeInfo(org.h2.value.TypeInfo) ExpressionColumn(org.h2.expression.ExpressionColumn)

Example 4 with TypeInfo

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);
}
Also used : LocalResult(org.h2.result.LocalResult) ValueRow(org.h2.value.ValueRow) Value(org.h2.value.Value) TypeInfo(org.h2.value.TypeInfo)

Example 5 with TypeInfo

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;
    }
}
Also used : TypeInfo(org.h2.value.TypeInfo)

Aggregations

TypeInfo (org.h2.value.TypeInfo)125 ValueBigint (org.h2.value.ValueBigint)65 Value (org.h2.value.Value)35 ValueSmallint (org.h2.value.ValueSmallint)27 AlterDomainAddConstraint (org.h2.command.ddl.AlterDomainAddConstraint)26 AlterDomainDropConstraint (org.h2.command.ddl.AlterDomainDropConstraint)24 AlterDomainRenameConstraint (org.h2.command.ddl.AlterDomainRenameConstraint)24 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)24 AlterTableDropConstraint (org.h2.command.ddl.AlterTableDropConstraint)24 AlterTableRenameConstraint (org.h2.command.ddl.AlterTableRenameConstraint)24 DbException (org.h2.message.DbException)23 Expression (org.h2.expression.Expression)21 Schema (org.h2.schema.Schema)18 ExpressionColumn (org.h2.expression.ExpressionColumn)17 Constraint (org.h2.constraint.Constraint)16 ValueExpression (org.h2.expression.ValueExpression)16 Column (org.h2.table.Column)15 TypedValueExpression (org.h2.expression.TypedValueExpression)14 ValueTinyint (org.h2.value.ValueTinyint)13 Domain (org.h2.schema.Domain)12