Search in sources :

Example 36 with ValueDate

use of org.gridgain.internal.h2.value.ValueDate in project gridgain by gridgain.

the class Function method truncate.

private static Value truncate(Session session, Value v0, Value v1) {
    Value result;
    int t = v0.getValueType();
    switch(t) {
        case Value.TIMESTAMP:
            result = ValueTimestamp.fromDateValueAndNanos(((ValueTimestamp) v0).getDateValue(), 0);
            break;
        case Value.DATE:
            result = ValueTimestamp.fromDateValueAndNanos(((ValueDate) v0).getDateValue(), 0);
            break;
        case Value.TIMESTAMP_TZ:
            {
                ValueTimestampTimeZone ts = (ValueTimestampTimeZone) v0;
                result = ValueTimestampTimeZone.fromDateValueAndNanos(ts.getDateValue(), 0, ts.getTimeZoneOffsetMins());
                break;
            }
        case Value.STRING:
            result = ValueTimestamp.fromDateValueAndNanos(ValueTimestamp.parse(v0.getString(), session.getDatabase().getMode()).getDateValue(), 0);
            break;
        default:
            int scale = v1 == null ? 0 : v1.getInt();
            if (t == Value.DOUBLE || t == Value.FLOAT) {
                double d = v0.getDouble();
                if (scale == 0) {
                    d = d < 0 ? Math.ceil(d) : Math.floor(d);
                } else {
                    double f = Math.pow(10, scale);
                    d *= f;
                    d = (d < 0 ? Math.ceil(d) : Math.floor(d)) / f;
                }
                result = t == Value.DOUBLE ? ValueDouble.get(d) : ValueFloat.get((float) d);
            } else {
                result = ValueDecimal.get(v0.getBigDecimal().setScale(scale, RoundingMode.DOWN));
            }
            break;
    }
    return result;
}
Also used : ValueTimestamp(org.gridgain.internal.h2.value.ValueTimestamp) Value(org.gridgain.internal.h2.value.Value) SequenceValue(org.gridgain.internal.h2.expression.SequenceValue) ValueTimestampTimeZone(org.gridgain.internal.h2.value.ValueTimestampTimeZone) ValueDate(org.gridgain.internal.h2.value.ValueDate)

Example 37 with ValueDate

use of org.gridgain.internal.h2.value.ValueDate in project gridgain by gridgain.

the class Data method writeValue.

/**
 * Append a value.
 *
 * @param o the value
 */
public void writeValue(Object o) {
    int start = pos;
    if (o instanceof Value) {
        Value v = (Value) o;
        if (v == ValueNull.INSTANCE) {
            data[pos++] = NULL;
            return;
        }
        int type = v.getValueType();
        switch(type) {
            case Value.BOOLEAN:
                writeByte(v.getBoolean() ? BOOLEAN_TRUE : BOOLEAN_FALSE);
                break;
            case Value.BYTE:
                writeByte(BYTE);
                writeByte(v.getByte());
                break;
            case Value.SHORT:
                writeByte(SHORT);
                writeShortInt(v.getShort());
                break;
            case Value.ENUM:
            case Value.INT:
                {
                    int x = v.getInt();
                    if (x < 0) {
                        writeByte(INT_NEG);
                        writeVarInt(-x);
                    } else if (x < 16) {
                        writeByte((byte) (INT_0_15 + x));
                    } else {
                        writeByte(type == Value.INT ? INT : ENUM);
                        writeVarInt(x);
                    }
                    break;
                }
            case Value.LONG:
                {
                    long x = v.getLong();
                    if (x < 0) {
                        writeByte(LONG_NEG);
                        writeVarLong(-x);
                    } else if (x < 8) {
                        writeByte((byte) (LONG_0_7 + x));
                    } else {
                        writeByte(LONG);
                        writeVarLong(x);
                    }
                    break;
                }
            case Value.DECIMAL:
                {
                    BigDecimal x = v.getBigDecimal();
                    if (BigDecimal.ZERO.equals(x)) {
                        writeByte(DECIMAL_0_1);
                    } else if (BigDecimal.ONE.equals(x)) {
                        writeByte((byte) (DECIMAL_0_1 + 1));
                    } else {
                        int scale = x.scale();
                        BigInteger b = x.unscaledValue();
                        int bits = b.bitLength();
                        if (bits <= 63) {
                            if (scale == 0) {
                                writeByte(DECIMAL_SMALL_0);
                                writeVarLong(b.longValue());
                            } else {
                                writeByte(DECIMAL_SMALL);
                                writeVarInt(scale);
                                writeVarLong(b.longValue());
                            }
                        } else {
                            writeByte(DECIMAL);
                            writeVarInt(scale);
                            byte[] bytes = b.toByteArray();
                            writeVarInt(bytes.length);
                            write(bytes, 0, bytes.length);
                        }
                    }
                    break;
                }
            case Value.TIME:
                if (storeLocalTime) {
                    writeByte((byte) LOCAL_TIME);
                    ValueTime t = (ValueTime) v;
                    long nanos = t.getNanos();
                    long millis = nanos / 1_000_000;
                    nanos -= millis * 1_000_000;
                    writeVarLong(millis);
                    writeVarLong(nanos);
                } else {
                    writeByte(TIME);
                    writeVarLong(DateTimeUtils.getTimeLocalWithoutDst(v.getTime()));
                }
                break;
            case Value.DATE:
                {
                    if (storeLocalTime) {
                        writeByte((byte) LOCAL_DATE);
                        long x = ((ValueDate) v).getDateValue();
                        writeVarLong(x);
                    } else {
                        writeByte(DATE);
                        long x = DateTimeUtils.getTimeLocalWithoutDst(v.getDate());
                        writeVarLong(x / MILLIS_PER_MINUTE);
                    }
                    break;
                }
            case Value.TIMESTAMP:
                {
                    if (storeLocalTime) {
                        writeByte((byte) LOCAL_TIMESTAMP);
                        ValueTimestamp ts = (ValueTimestamp) v;
                        long dateValue = ts.getDateValue();
                        writeVarLong(dateValue);
                        long nanos = ts.getTimeNanos();
                        long millis = nanos / 1_000_000;
                        nanos -= millis * 1_000_000;
                        writeVarLong(millis);
                        writeVarLong(nanos);
                    } else {
                        Timestamp ts = v.getTimestamp();
                        writeByte(TIMESTAMP);
                        writeVarLong(DateTimeUtils.getTimeLocalWithoutDst(ts));
                        writeVarInt(ts.getNanos() % 1_000_000);
                    }
                    break;
                }
            case Value.TIMESTAMP_TZ:
                {
                    ValueTimestampTimeZone ts = (ValueTimestampTimeZone) v;
                    writeByte(TIMESTAMP_TZ);
                    writeVarLong(ts.getDateValue());
                    writeVarLong(ts.getTimeNanos());
                    writeVarInt(ts.getTimeZoneOffsetMins());
                    break;
                }
            case Value.GEOMETRY:
            // fall though
            case Value.JAVA_OBJECT:
                {
                    writeByte(type == Value.GEOMETRY ? GEOMETRY : JAVA_OBJECT);
                    byte[] b = v.getBytesNoCopy();
                    int len = b.length;
                    writeVarInt(len);
                    write(b, 0, len);
                    break;
                }
            case Value.BYTES:
                {
                    byte[] b = v.getBytesNoCopy();
                    int len = b.length;
                    if (len < 32) {
                        writeByte((byte) (BYTES_0_31 + len));
                        write(b, 0, len);
                    } else {
                        writeByte(BYTES);
                        writeVarInt(len);
                        write(b, 0, len);
                    }
                    break;
                }
            case Value.UUID:
                {
                    writeByte(UUID);
                    ValueUuid uuid = (ValueUuid) v;
                    writeLong(uuid.getHigh());
                    writeLong(uuid.getLow());
                    break;
                }
            case Value.STRING:
                {
                    String s = v.getString();
                    int len = s.length();
                    if (len < 32) {
                        writeByte((byte) (STRING_0_31 + len));
                        writeStringWithoutLength(s, len);
                    } else {
                        writeByte(STRING);
                        writeString(s);
                    }
                    break;
                }
            case Value.STRING_IGNORECASE:
                writeByte(STRING_IGNORECASE);
                writeString(v.getString());
                break;
            case Value.STRING_FIXED:
                writeByte(STRING_FIXED);
                writeString(v.getString());
                break;
            case Value.DOUBLE:
                {
                    double x = v.getDouble();
                    if (x == 1.0d) {
                        writeByte((byte) (DOUBLE_0_1 + 1));
                    } else {
                        long d = Double.doubleToLongBits(x);
                        if (d == ValueDouble.ZERO_BITS) {
                            writeByte(DOUBLE_0_1);
                        } else {
                            writeByte(DOUBLE);
                            writeVarLong(Long.reverse(d));
                        }
                    }
                    break;
                }
            case Value.FLOAT:
                {
                    float x = v.getFloat();
                    if (x == 1.0f) {
                        writeByte((byte) (FLOAT_0_1 + 1));
                    } else {
                        int f = Float.floatToIntBits(x);
                        if (f == ValueFloat.ZERO_BITS) {
                            writeByte(FLOAT_0_1);
                        } else {
                            writeByte(FLOAT);
                            writeVarInt(Integer.reverse(f));
                        }
                    }
                    break;
                }
            case Value.BLOB:
            case Value.CLOB:
                {
                    writeByte(type == Value.BLOB ? BLOB : CLOB);
                    if (v instanceof ValueLob) {
                        ValueLob lob = (ValueLob) v;
                        byte[] small = lob.getSmall();
                        if (small == null) {
                            int t = -1;
                            if (!lob.isLinkedToTable()) {
                                t = -2;
                            }
                            writeVarInt(t);
                            writeVarInt(lob.getTableId());
                            writeVarInt(lob.getObjectId());
                            writeVarLong(lob.getType().getPrecision());
                            writeByte((byte) (lob.isCompressed() ? 1 : 0));
                            if (t == -2) {
                                writeString(lob.getFileName());
                            }
                        } else {
                            writeVarInt(small.length);
                            write(small, 0, small.length);
                        }
                    } else {
                        ValueLobDb lob = (ValueLobDb) v;
                        byte[] small = lob.getSmall();
                        if (small == null) {
                            writeVarInt(-3);
                            writeVarInt(lob.getTableId());
                            writeVarLong(lob.getLobId());
                            writeVarLong(lob.getType().getPrecision());
                        } else {
                            writeVarInt(small.length);
                            write(small, 0, small.length);
                        }
                    }
                    break;
                }
            case Value.ARRAY:
            case Value.ROW:
                {
                    writeByte(type == Value.ARRAY ? ARRAY : ROW);
                    Value[] list = ((ValueCollectionBase) v).getList();
                    writeVarInt(list.length);
                    for (Value x : list) {
                        writeValue(x);
                    }
                    break;
                }
            case Value.RESULT_SET:
                {
                    writeByte(RESULT_SET);
                    ResultInterface result = ((ValueResultSet) v).getResult();
                    result.reset();
                    int columnCount = result.getVisibleColumnCount();
                    writeVarInt(columnCount);
                    for (int i = 0; i < columnCount; i++) {
                        writeString(result.getAlias(i));
                        writeString(result.getColumnName(i));
                        TypeInfo columnType = result.getColumnType(i);
                        writeVarInt(columnType.getValueType());
                        writeVarLong(columnType.getPrecision());
                        writeVarInt(columnType.getScale());
                    }
                    while (result.next()) {
                        writeByte((byte) 1);
                        Value[] row = result.currentRow();
                        for (int i = 0; i < columnCount; i++) {
                            writeValue(row[i]);
                        }
                    }
                    writeByte((byte) 0);
                    break;
                }
            case Value.INTERVAL_YEAR:
            case Value.INTERVAL_MONTH:
            case Value.INTERVAL_DAY:
            case Value.INTERVAL_HOUR:
            case Value.INTERVAL_MINUTE:
                {
                    ValueInterval interval = (ValueInterval) v;
                    int ordinal = type - Value.INTERVAL_YEAR;
                    if (interval.isNegative()) {
                        ordinal = ~ordinal;
                    }
                    writeByte(INTERVAL);
                    writeByte((byte) ordinal);
                    writeVarLong(interval.getLeading());
                    break;
                }
            case Value.INTERVAL_SECOND:
            case Value.INTERVAL_YEAR_TO_MONTH:
            case Value.INTERVAL_DAY_TO_HOUR:
            case Value.INTERVAL_DAY_TO_MINUTE:
            case Value.INTERVAL_DAY_TO_SECOND:
            case Value.INTERVAL_HOUR_TO_MINUTE:
            case Value.INTERVAL_HOUR_TO_SECOND:
            case Value.INTERVAL_MINUTE_TO_SECOND:
                {
                    ValueInterval interval = (ValueInterval) v;
                    int ordinal = type - Value.INTERVAL_YEAR;
                    if (interval.isNegative()) {
                        ordinal = ~ordinal;
                    }
                    writeByte(INTERVAL);
                    writeByte((byte) ordinal);
                    writeVarLong(interval.getLeading());
                    writeVarLong(interval.getRemaining());
                    break;
                }
            default:
                if (JdbcUtils.customDataTypesHandler != null) {
                    byte[] b = v.getBytesNoCopy();
                    writeByte((byte) CUSTOM_DATA_TYPE);
                    writeVarInt(type);
                    writeVarInt(b.length);
                    write(b, 0, b.length);
                    break;
                }
                DbException.throwInternalError("type=" + v.getValueType());
        }
        assert pos - start == getValueLen(v) : "value size error: got " + (pos - start) + " expected " + getValueLen(v);
    } else if (o instanceof AggregateDataCount) {
        AggregateDataCount a = (AggregateDataCount) o;
        writeByte((byte) AGG_DATA_COUNT);
        writeByte(a.isAll() ? BOOLEAN_TRUE : BOOLEAN_FALSE);
        writeLong(a.count());
    } else if (o instanceof AggregateDataDefault) {
        AggregateDataDefault a = (AggregateDataDefault) o;
        writeByte((byte) AGG_DATA_DEFAULT);
        writeInt(a.aggregateType().ordinal());
        writeInt(a.dataType());
        writeLong(a.count());
        writeValue(a.value() == null ? ValueNull.INSTANCE : a.value());
        writeLong(Double.doubleToLongBits(a.mean()));
        writeLong(Double.doubleToLongBits(a.m2()));
    } else if (o instanceof AggregateDataCollecting) {
        AggregateDataCollecting a = (AggregateDataCollecting) o;
        writeByte((byte) AGG_DATA_COLLECTION);
        writeByte(a.isDistinct() ? BOOLEAN_TRUE : BOOLEAN_FALSE);
        writeValue(a.getSharedArgument() == null ? ValueNull.INSTANCE : a.getSharedArgument());
        Collection<Value> values = a.values();
        if (values == null)
            writeInt(0);
        else {
            writeInt(values.size());
            for (Value v : values) writeValue(v);
        }
    } else
        throw DbException.throwInternalError("unknown type=" + o.getClass());
}
Also used : AggregateDataCount(org.gridgain.internal.h2.expression.aggregate.AggregateDataCount) ValueUuid(org.gridgain.internal.h2.value.ValueUuid) ValueLob(org.gridgain.internal.h2.value.ValueLob) ValueLobDb(org.gridgain.internal.h2.value.ValueLobDb) ResultInterface(org.gridgain.internal.h2.result.ResultInterface) ValueTimestampTimeZone(org.gridgain.internal.h2.value.ValueTimestampTimeZone) ValueString(org.gridgain.internal.h2.value.ValueString) ValueInterval(org.gridgain.internal.h2.value.ValueInterval) Timestamp(java.sql.Timestamp) ValueTimestamp(org.gridgain.internal.h2.value.ValueTimestamp) TypeInfo(org.gridgain.internal.h2.value.TypeInfo) BigDecimal(java.math.BigDecimal) ValueTime(org.gridgain.internal.h2.value.ValueTime) AggregateDataCollecting(org.gridgain.internal.h2.expression.aggregate.AggregateDataCollecting) ValueTimestamp(org.gridgain.internal.h2.value.ValueTimestamp) Value(org.gridgain.internal.h2.value.Value) BigInteger(java.math.BigInteger) Collection(java.util.Collection) AggregateDataDefault(org.gridgain.internal.h2.expression.aggregate.AggregateDataDefault)

Aggregations

ValueDate (org.h2.value.ValueDate)23 ValueTime (org.h2.value.ValueTime)18 ValueTimestampTimeZone (org.h2.value.ValueTimestampTimeZone)16 ValueTimestamp (org.h2.value.ValueTimestamp)15 BigDecimal (java.math.BigDecimal)10 ValueTimeTimeZone (org.h2.value.ValueTimeTimeZone)10 BigInteger (java.math.BigInteger)9 ValueDate (org.gridgain.internal.h2.value.ValueDate)9 ValueTime (org.gridgain.internal.h2.value.ValueTime)9 Value (org.gridgain.internal.h2.value.Value)8 ValueTimestamp (org.gridgain.internal.h2.value.ValueTimestamp)8 ValueTimestampTimeZone (org.gridgain.internal.h2.value.ValueTimestampTimeZone)8 ValueInterval (org.gridgain.internal.h2.value.ValueInterval)6 TypeInfo (org.gridgain.internal.h2.value.TypeInfo)4 ValueInterval (org.h2.value.ValueInterval)4 ResultInterface (org.gridgain.internal.h2.result.ResultInterface)3 ValueLobDb (org.gridgain.internal.h2.value.ValueLobDb)3 ValueString (org.gridgain.internal.h2.value.ValueString)3 Value (org.h2.value.Value)3 Date (java.sql.Date)2