Search in sources :

Example 21 with DataType

use of org.h2.value.DataType in project ignite by apache.

the class GridSqlQueryParser method parseColumn.

/**
 * Turn H2 column to grid column and check requested features.
 * @param col H2 column.
 * @return Grid column.
 */
private static GridSqlColumn parseColumn(Column col) {
    if (col.isAutoIncrement()) {
        throw new IgniteSQLException("AUTO_INCREMENT columns are not supported [colName=" + col.getName() + ']', IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
    }
    if (COLUMN_IS_COMPUTED.get(col)) {
        throw new IgniteSQLException("Computed columns are not supported [colName=" + col.getName() + ']', IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
    }
    checkTypeSupported(col.getType(), "[colName=" + col.getName() + ']');
    if (col.getDefaultExpression() != null) {
        if (!col.getDefaultExpression().isConstant()) {
            throw new IgniteSQLException("Non-constant DEFAULT expressions are not supported [colName=" + col.getName() + ']', IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
        }
        DataType colType = DataType.getDataType(col.getType());
        DataType dfltType = DataType.getDataType(col.getDefaultExpression().getType());
        if ((DataType.isStringType(colType.type) && !DataType.isStringType(dfltType.type)) || (DataType.supportsAdd(colType.type) && !DataType.supportsAdd(dfltType.type))) {
            throw new IgniteSQLException("Invalid default value for column. [colName=" + col.getName() + ", colType=" + colType.name + ", dfltValueType=" + dfltType.name + ']', IgniteQueryErrorCode.UNEXPECTED_ELEMENT_TYPE);
        }
    }
    if (col.getSequence() != null)
        throw new IgniteSQLException("SEQUENCE columns are not supported [colName=" + col.getName() + ']', IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
    if (col.getSelectivity() != Constants.SELECTIVITY_DEFAULT) {
        throw new IgniteSQLException("SELECTIVITY column attribute is not supported [colName=" + col.getName() + ']', IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
    }
    if (COLUMN_CHECK_CONSTRAINT.get(col) != null) {
        throw new IgniteSQLException("Column CHECK constraints are not supported [colName=" + col.getName() + ']', IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
    }
    GridSqlColumn gridCol = new GridSqlColumn(col, null, col.getName());
    gridCol.resultType(GridSqlType.fromColumn(col));
    return gridCol;
}
Also used : IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) DataType(org.h2.value.DataType)

Example 22 with DataType

use of org.h2.value.DataType in project ignite by apache.

the class DmlAstUtils method elementOrDefault.

/**
     * Do what we can to compute default value for this column (mimics H2 behavior).
     * @see Table#getDefaultValue
     * @see Column#validateConvertUpdateSequence
     * @param el SQL element.
     * @param col Column.
     * @return {@link GridSqlConst#NULL}, if {@code el} is null, or {@code el} if
     * it's not {@link GridSqlKeyword#DEFAULT}, or computed default value.
     */
private static GridSqlElement elementOrDefault(GridSqlElement el, GridSqlColumn col) {
    if (el == null)
        return GridSqlConst.NULL;
    if (el != GridSqlKeyword.DEFAULT)
        return el;
    Column h2Col = col.column();
    Expression dfltExpr = h2Col.getDefaultExpression();
    Value dfltVal;
    try {
        dfltVal = dfltExpr != null ? dfltExpr.getValue(null) : null;
    } catch (Exception ignored) {
        throw new IgniteSQLException("Failed to evaluate default value for a column " + col.columnName());
    }
    if (dfltVal != null)
        return new GridSqlConst(dfltVal);
    int type = h2Col.getType();
    DataType dt = DataType.getDataType(type);
    if (dt.decimal)
        dfltVal = ValueInt.get(0).convertTo(type);
    else if (dt.type == Value.TIMESTAMP)
        dfltVal = ValueTimestamp.fromMillis(U.currentTimeMillis());
    else if (dt.type == Value.TIME)
        dfltVal = ValueTime.fromNanos(0);
    else if (dt.type == Value.DATE)
        dfltVal = ValueDate.fromMillis(U.currentTimeMillis());
    else
        dfltVal = ValueString.get("").convertTo(type);
    return new GridSqlConst(dfltVal);
}
Also used : Column(org.h2.table.Column) Expression(org.h2.expression.Expression) Value(org.h2.value.Value) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) DataType(org.h2.value.DataType) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException)

Example 23 with DataType

use of org.h2.value.DataType in project h2database by h2database.

the class AggregateDataSelectivity method getValue.

@Override
Value getValue(Database database, int dataType, boolean distinct) {
    if (distinct) {
        count = 0;
    }
    Value v = null;
    int s = 0;
    if (count == 0) {
        s = 0;
    } else {
        m2 += distinctHashes.size();
        m2 = 100 * m2 / count;
        s = (int) m2;
        s = s <= 0 ? 1 : s > 100 ? 100 : s;
    }
    v = ValueInt.get(s);
    return v.convertTo(dataType);
}
Also used : Value(org.h2.value.Value)

Example 24 with DataType

use of org.h2.value.DataType in project h2database by h2database.

the class CompareLike method createIndexConditions.

@Override
public void createIndexConditions(Session session, TableFilter filter) {
    if (regexp) {
        return;
    }
    if (!(left instanceof ExpressionColumn)) {
        return;
    }
    ExpressionColumn l = (ExpressionColumn) left;
    if (filter != l.getTableFilter()) {
        return;
    }
    // which may be slower (possibly not in this case)
    if (!right.isEverything(ExpressionVisitor.INDEPENDENT_VISITOR)) {
        return;
    }
    if (escape != null && !escape.isEverything(ExpressionVisitor.INDEPENDENT_VISITOR)) {
        return;
    }
    String p = right.getValue(session).getString();
    if (!isInit) {
        Value e = escape == null ? null : escape.getValue(session);
        if (e == ValueNull.INSTANCE) {
            // should already be optimized
            DbException.throwInternalError();
        }
        initPattern(p, getEscapeChar(e));
    }
    if (invalidPattern) {
        return;
    }
    if (patternLength <= 0 || patternTypes[0] != MATCH) {
        // can't use an index
        return;
    }
    int dataType = l.getColumn().getType();
    if (dataType != Value.STRING && dataType != Value.STRING_IGNORECASE && dataType != Value.STRING_FIXED) {
        // column is not a varchar - can't use the index
        return;
    }
    // Get the MATCH prefix and see if we can create an index condition from
    // that.
    int maxMatch = 0;
    StringBuilder buff = new StringBuilder();
    while (maxMatch < patternLength && patternTypes[maxMatch] == MATCH) {
        buff.append(patternChars[maxMatch++]);
    }
    String begin = buff.toString();
    if (maxMatch == patternLength) {
        filter.addIndexCondition(IndexCondition.get(Comparison.EQUAL, l, ValueExpression.get(ValueString.get(begin))));
    } else {
        // TODO check if this is correct according to Unicode rules
        // (code points)
        String end;
        if (begin.length() > 0) {
            filter.addIndexCondition(IndexCondition.get(Comparison.BIGGER_EQUAL, l, ValueExpression.get(ValueString.get(begin))));
            char next = begin.charAt(begin.length() - 1);
            // that is higher)
            for (int i = 1; i < 2000; i++) {
                end = begin.substring(0, begin.length() - 1) + (char) (next + i);
                if (compareMode.compareString(begin, end, ignoreCase) == -1) {
                    filter.addIndexCondition(IndexCondition.get(Comparison.SMALLER, l, ValueExpression.get(ValueString.get(end))));
                    break;
                }
            }
        }
    }
}
Also used : Value(org.h2.value.Value) ValueString(org.h2.value.ValueString)

Example 25 with DataType

use of org.h2.value.DataType in project h2database by h2database.

the class Aggregate method optimize.

@Override
public Expression optimize(Session session) {
    if (on != null) {
        on = on.optimize(session);
        dataType = on.getType();
        scale = on.getScale();
        precision = on.getPrecision();
        displaySize = on.getDisplaySize();
    }
    if (orderByList != null) {
        for (SelectOrderBy o : orderByList) {
            o.expression = o.expression.optimize(session);
        }
        orderBySort = initOrder(session);
    }
    if (groupConcatSeparator != null) {
        groupConcatSeparator = groupConcatSeparator.optimize(session);
    }
    if (filterCondition != null) {
        filterCondition = filterCondition.optimize(session);
    }
    switch(type) {
        case GROUP_CONCAT:
            dataType = Value.STRING;
            scale = 0;
            precision = displaySize = Integer.MAX_VALUE;
            break;
        case COUNT_ALL:
        case COUNT:
            dataType = Value.LONG;
            scale = 0;
            precision = ValueLong.PRECISION;
            displaySize = ValueLong.DISPLAY_SIZE;
            break;
        case SELECTIVITY:
            dataType = Value.INT;
            scale = 0;
            precision = ValueInt.PRECISION;
            displaySize = ValueInt.DISPLAY_SIZE;
            break;
        case HISTOGRAM:
            dataType = Value.ARRAY;
            scale = 0;
            precision = displaySize = Integer.MAX_VALUE;
            break;
        case SUM:
            if (dataType == Value.BOOLEAN) {
                // example: sum(id > 3) (count the rows)
                dataType = Value.LONG;
            } else if (!DataType.supportsAdd(dataType)) {
                throw DbException.get(ErrorCode.SUM_OR_AVG_ON_WRONG_DATATYPE_1, getSQL());
            } else {
                dataType = DataType.getAddProofType(dataType);
            }
            break;
        case AVG:
            if (!DataType.supportsAdd(dataType)) {
                throw DbException.get(ErrorCode.SUM_OR_AVG_ON_WRONG_DATATYPE_1, getSQL());
            }
            break;
        case MIN:
        case MAX:
        case MEDIAN:
            break;
        case STDDEV_POP:
        case STDDEV_SAMP:
        case VAR_POP:
        case VAR_SAMP:
            dataType = Value.DOUBLE;
            precision = ValueDouble.PRECISION;
            displaySize = ValueDouble.DISPLAY_SIZE;
            scale = 0;
            break;
        case BOOL_AND:
        case BOOL_OR:
            dataType = Value.BOOLEAN;
            precision = ValueBoolean.PRECISION;
            displaySize = ValueBoolean.DISPLAY_SIZE;
            scale = 0;
            break;
        case BIT_AND:
        case BIT_OR:
            if (!DataType.supportsAdd(dataType)) {
                throw DbException.get(ErrorCode.SUM_OR_AVG_ON_WRONG_DATATYPE_1, getSQL());
            }
            break;
        case ARRAY_AGG:
            dataType = Value.ARRAY;
            scale = 0;
            precision = displaySize = Integer.MAX_VALUE;
            break;
        default:
            DbException.throwInternalError("type=" + type);
    }
    return this;
}
Also used : SelectOrderBy(org.h2.command.dml.SelectOrderBy)

Aggregations

Value (org.h2.value.Value)16 ValueString (org.h2.value.ValueString)13 DataType (org.h2.value.DataType)12 Expression (org.h2.expression.Expression)6 Column (org.h2.table.Column)6 Constraint (org.h2.constraint.Constraint)5 UserDataType (org.h2.engine.UserDataType)5 UserAggregate (org.h2.engine.UserAggregate)4 Index (org.h2.index.Index)4 Table (org.h2.table.Table)4 ArrayList (java.util.ArrayList)3 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)3 Mode (org.h2.engine.Mode)3 ValueExpression (org.h2.expression.ValueExpression)3 SearchRow (org.h2.result.SearchRow)3 Schema (org.h2.schema.Schema)3 SimpleResultSet (org.h2.tools.SimpleResultSet)3 StatementBuilder (org.h2.util.StatementBuilder)3 CompareMode (org.h2.value.CompareMode)3 IOException (java.io.IOException)2