Search in sources :

Example 11 with TimeString

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.TimeString in project calcite by apache.

the class RexLiteral method printAsJava.

/**
 * Prints a value as a Java string. The value must be consistent with the
 * type, as per {@link #valueMatchesType}.
 *
 * <p>Typical return values:
 *
 * <ul>
 * <li>true</li>
 * <li>null</li>
 * <li>"Hello, world!"</li>
 * <li>1.25</li>
 * <li>1234ABCD</li>
 * </ul>
 *
 * @param value    Value
 * @param pw       Writer to write to
 * @param typeName Type family
 */
private static void printAsJava(Comparable value, PrintWriter pw, SqlTypeName typeName, boolean java) {
    switch(typeName) {
        case CHAR:
            NlsString nlsString = (NlsString) value;
            if (java) {
                Util.printJavaString(pw, nlsString.getValue(), true);
            } else {
                boolean includeCharset = (nlsString.getCharsetName() != null) && !nlsString.getCharsetName().equals(SaffronProperties.INSTANCE.defaultCharset().get());
                pw.print(nlsString.asSql(includeCharset, false));
            }
            break;
        case BOOLEAN:
            assert value instanceof Boolean;
            pw.print(((Boolean) value).booleanValue());
            break;
        case DECIMAL:
            assert value instanceof BigDecimal;
            pw.print(value.toString());
            break;
        case DOUBLE:
            assert value instanceof BigDecimal;
            pw.print(Util.toScientificNotation((BigDecimal) value));
            break;
        case BIGINT:
            assert value instanceof BigDecimal;
            pw.print(((BigDecimal) value).longValue());
            pw.print('L');
            break;
        case BINARY:
            assert value instanceof ByteString;
            pw.print("X'");
            pw.print(((ByteString) value).toString(16));
            pw.print("'");
            break;
        case NULL:
            assert value == null;
            pw.print("null");
            break;
        case SYMBOL:
            assert value instanceof Enum;
            pw.print("FLAG(");
            pw.print(value);
            pw.print(")");
            break;
        case DATE:
            assert value instanceof DateString;
            pw.print(value);
            break;
        case TIME:
            assert value instanceof TimeString;
            pw.print(value);
            break;
        case TIME_WITH_LOCAL_TIME_ZONE:
            assert value instanceof TimeString;
            pw.print(value);
            break;
        case TIMESTAMP:
            assert value instanceof TimestampString;
            pw.print(value);
            break;
        case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
            assert value instanceof TimestampString;
            pw.print(value);
            break;
        case INTERVAL_YEAR:
        case INTERVAL_YEAR_MONTH:
        case INTERVAL_MONTH:
        case INTERVAL_DAY:
        case INTERVAL_DAY_HOUR:
        case INTERVAL_DAY_MINUTE:
        case INTERVAL_DAY_SECOND:
        case INTERVAL_HOUR:
        case INTERVAL_HOUR_MINUTE:
        case INTERVAL_HOUR_SECOND:
        case INTERVAL_MINUTE:
        case INTERVAL_MINUTE_SECOND:
        case INTERVAL_SECOND:
            if (value instanceof BigDecimal) {
                pw.print(value.toString());
            } else {
                assert value == null;
                pw.print("null");
            }
            break;
        case MULTISET:
        case ROW:
            @SuppressWarnings("unchecked") final List<RexLiteral> list = (List) value;
            pw.print(new AbstractList<String>() {

                public String get(int index) {
                    return list.get(index).digest;
                }

                public int size() {
                    return list.size();
                }
            });
            break;
        default:
            assert valueMatchesType(value, typeName, true);
            throw Util.needToImplement(typeName);
    }
}
Also used : TimeString(org.apache.calcite.util.TimeString) ByteString(org.apache.calcite.avatica.util.ByteString) TimeString(org.apache.calcite.util.TimeString) DateString(org.apache.calcite.util.DateString) NlsString(org.apache.calcite.util.NlsString) ByteString(org.apache.calcite.avatica.util.ByteString) TimestampString(org.apache.calcite.util.TimestampString) BigDecimal(java.math.BigDecimal) DateString(org.apache.calcite.util.DateString) NlsString(org.apache.calcite.util.NlsString) AbstractList(java.util.AbstractList) ImmutableList(com.google.common.collect.ImmutableList) CompositeList(org.apache.calcite.util.CompositeList) List(java.util.List) TimestampString(org.apache.calcite.util.TimestampString)

Example 12 with TimeString

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.TimeString in project calcite by apache.

the class RexBuilderTest method testTimeStringSecondError.

/**
 * Tests {@link TimeString} second range.
 */
@Test
public void testTimeStringSecondError() {
    try {
        final TimeString timeString = new TimeString(12, 34, 567);
        fail("expected exception, got " + timeString);
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("Second out of range: [567]"));
    }
    try {
        final TimeString timeString = new TimeString(12, 34, -4);
        fail("expected exception, got " + timeString);
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("Second out of range: [-4]"));
    }
    try {
        final TimeString timeString = new TimeString("12:34:60");
        fail("expected exception, got " + timeString);
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("Second out of range: [60]"));
    }
}
Also used : TimeString(org.apache.calcite.util.TimeString) Test(org.junit.Test)

Example 13 with TimeString

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.TimeString in project calcite by apache.

the class RexBuilderTest method testTimeStringHourError.

/**
 * Tests {@link TimeString} hour range.
 */
@Test
public void testTimeStringHourError() {
    try {
        final TimeString timeString = new TimeString(111, 34, 56);
        fail("expected exception, got " + timeString);
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("Hour out of range: [111]"));
    }
    try {
        final TimeString timeString = new TimeString("24:00:00");
        fail("expected exception, got " + timeString);
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("Hour out of range: [24]"));
    }
    try {
        final TimeString timeString = new TimeString("24:00");
        fail("expected exception, got " + timeString);
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("Invalid time format: [24:00]"));
    }
}
Also used : TimeString(org.apache.calcite.util.TimeString) Test(org.junit.Test)

Example 14 with TimeString

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.TimeString in project calcite by apache.

the class RexBuilderTest method testTimeLiteral.

/**
 * Tests {@link RexBuilder#makeTimeLiteral(TimeString, int)}.
 */
@Test
public void testTimeLiteral() {
    final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
    RelDataType timeType = typeFactory.createSqlType(SqlTypeName.TIME);
    final RelDataType timeType3 = typeFactory.createSqlType(SqlTypeName.TIME, 3);
    final RelDataType timeType9 = typeFactory.createSqlType(SqlTypeName.TIME, 9);
    final RelDataType timeType18 = typeFactory.createSqlType(SqlTypeName.TIME, 18);
    final RexBuilder builder = new RexBuilder(typeFactory);
    // Old way: provide a Calendar
    final Calendar calendar = Util.calendar();
    // one small step
    calendar.set(1969, Calendar.JULY, 21, 2, 56, 15);
    calendar.set(Calendar.MILLISECOND, 0);
    checkTime(builder.makeLiteral(calendar, timeType, false));
    // Old way #2: Provide a Long
    checkTime(builder.makeLiteral(MOON_TIME, timeType, false));
    // The new way
    final TimeString t = new TimeString(2, 56, 15);
    assertThat(t.getMillisOfDay(), is(10575000));
    checkTime(builder.makeLiteral(t, timeType, false));
    // Now with milliseconds
    final TimeString t2 = t.withMillis(56);
    assertThat(t2.getMillisOfDay(), is(10575056));
    assertThat(t2.toString(), is("02:56:15.056"));
    final RexNode literal2 = builder.makeLiteral(t2, timeType3, false);
    assertThat(((RexLiteral) literal2).getValueAs(TimeString.class).toString(), is("02:56:15.056"));
    // Now with nanoseconds
    final TimeString t3 = t.withNanos(2345678);
    assertThat(t3.getMillisOfDay(), is(10575002));
    final RexNode literal3 = builder.makeLiteral(t3, timeType9, false);
    assertThat(((RexLiteral) literal3).getValueAs(TimeString.class).toString(), is("02:56:15.002"));
    // Now with a very long fraction
    final TimeString t4 = t.withFraction("102030405060708090102");
    assertThat(t4.getMillisOfDay(), is(10575102));
    final RexNode literal4 = builder.makeLiteral(t4, timeType18, false);
    assertThat(((RexLiteral) literal4).getValueAs(TimeString.class).toString(), is("02:56:15.102"));
    // toString
    assertThat(t2.round(1).toString(), is("02:56:15"));
    assertThat(t2.round(2).toString(), is("02:56:15.05"));
    assertThat(t2.round(3).toString(), is("02:56:15.056"));
    assertThat(t2.round(4).toString(), is("02:56:15.056"));
    assertThat(t2.toString(6), is("02:56:15.056000"));
    assertThat(t2.toString(1), is("02:56:15.0"));
    assertThat(t2.toString(0), is("02:56:15"));
    assertThat(t2.round(0).toString(), is("02:56:15"));
    assertThat(t2.round(0).toString(0), is("02:56:15"));
    assertThat(t2.round(0).toString(1), is("02:56:15.0"));
    assertThat(t2.round(0).toString(2), is("02:56:15.00"));
    assertThat(TimeString.fromMillisOfDay(53560123).toString(), is("14:52:40.123"));
}
Also used : SqlTypeFactoryImpl(org.apache.calcite.sql.type.SqlTypeFactoryImpl) TimeString(org.apache.calcite.util.TimeString) Calendar(java.util.Calendar) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) Test(org.junit.Test)

Example 15 with TimeString

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.TimeString in project flink by apache.

the class RexNodeJsonDeserializer method deserializeLiteralValue.

@Nullable
private static Object deserializeLiteralValue(JsonNode literalNode, SqlTypeName sqlTypeName, SerdeContext serdeContext) {
    final JsonNode valueNode = literalNode.required(FIELD_NAME_VALUE);
    if (valueNode.isNull()) {
        return null;
    }
    switch(sqlTypeName) {
        case BOOLEAN:
            return valueNode.booleanValue();
        case TINYINT:
        case SMALLINT:
        case INTEGER:
        case BIGINT:
        case FLOAT:
        case DOUBLE:
        case DECIMAL:
        case INTERVAL_YEAR:
        case INTERVAL_YEAR_MONTH:
        case INTERVAL_MONTH:
        case INTERVAL_DAY:
        case INTERVAL_DAY_HOUR:
        case INTERVAL_DAY_MINUTE:
        case INTERVAL_DAY_SECOND:
        case INTERVAL_HOUR:
        case INTERVAL_HOUR_MINUTE:
        case INTERVAL_HOUR_SECOND:
        case INTERVAL_MINUTE:
        case INTERVAL_MINUTE_SECOND:
        case INTERVAL_SECOND:
            return new BigDecimal(valueNode.asText());
        case DATE:
            return new DateString(valueNode.asText());
        case TIME:
            return new TimeString(valueNode.asText());
        case TIMESTAMP:
        case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
            return new TimestampString(valueNode.asText());
        case BINARY:
        case VARBINARY:
            return ByteString.ofBase64(valueNode.asText());
        case CHAR:
        case VARCHAR:
            return serdeContext.getRexBuilder().makeLiteral(valueNode.asText()).getValue();
        case SYMBOL:
            final JsonNode symbolNode = literalNode.required(FIELD_NAME_SYMBOL);
            final SerializableSymbol symbol = SerializableSymbol.of(symbolNode.asText(), valueNode.asText());
            return serializableToCalcite(symbol);
        default:
            throw new TableException("Unknown literal: " + valueNode);
    }
}
Also used : SerializableSymbol(org.apache.flink.table.planner.typeutils.SymbolUtil.SerializableSymbol) TableException(org.apache.flink.table.api.TableException) TimeString(org.apache.calcite.util.TimeString) DateString(org.apache.calcite.util.DateString) JsonNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode) TimestampString(org.apache.calcite.util.TimestampString) BigDecimal(java.math.BigDecimal) Nullable(javax.annotation.Nullable)

Aggregations

TimeString (org.apache.calcite.util.TimeString)19 TimestampString (org.apache.calcite.util.TimestampString)10 Test (org.junit.Test)10 DateString (org.apache.calcite.util.DateString)9 RelDataType (org.apache.calcite.rel.type.RelDataType)6 BigDecimal (java.math.BigDecimal)5 RexNode (org.apache.calcite.rex.RexNode)5 RexLiteral (org.apache.calcite.rex.RexLiteral)4 NlsString (org.apache.calcite.util.NlsString)4 ByteString (org.apache.calcite.avatica.util.ByteString)3 ImmutableList (com.google.common.collect.ImmutableList)2 Calendar (java.util.Calendar)2 List (java.util.List)2 DateTimeUtils (org.apache.calcite.avatica.util.DateTimeUtils)2 CompositeList (org.apache.calcite.util.CompositeList)2 Preconditions (com.google.common.base.Preconditions)1 Literal (com.hazelcast.jet.sql.impl.validate.literal.Literal)1 QueryException (com.hazelcast.sql.impl.QueryException)1 QueryDataType (com.hazelcast.sql.impl.type.QueryDataType)1 Converter (com.hazelcast.sql.impl.type.converter.Converter)1