Search in sources :

Example 11 with DateString

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

the class RexToExpressionTest method test_date.

@Test
public void test_date() {
    RexLiteral literal = literal(new DateString("2021-09-17"), new DateString("2021-09-18"), SqlTypeName.DATE);
    Range<?> converted = convert(literal);
    assertThat(converted).isEqualToComparingFieldByField(range(LocalDate.of(2021, 9, 17), LocalDate.of(2021, 9, 18)));
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) DateString(org.apache.calcite.util.DateString) Test(org.junit.Test)

Example 12 with DateString

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

the class RexProgramTest method testSimplifyCastLiteral3.

@Test
public void testSimplifyCastLiteral3() {
    // Default TimeZone is "America/Los_Angeles" (DummyDataContext)
    final RexLiteral literalDate = rexBuilder.makeDateLiteral(new DateString("2011-07-20"));
    final RexLiteral literalTime = rexBuilder.makeTimeLiteral(new TimeString("12:34:56"), 0);
    final RexLiteral literalTimestamp = rexBuilder.makeTimestampLiteral(new TimestampString("2011-07-20 12:34:56"), 0);
    final RexLiteral literalTimeLTZ = rexBuilder.makeTimeWithLocalTimeZoneLiteral(new TimeString(1, 23, 45), 0);
    final RexLiteral timeLTZChar1 = rexBuilder.makeLiteral("12:34:45 America/Los_Angeles");
    final RexLiteral timeLTZChar2 = rexBuilder.makeLiteral("12:34:45 UTC");
    final RexLiteral timeLTZChar3 = rexBuilder.makeLiteral("12:34:45 GMT+01");
    final RexLiteral timestampLTZChar1 = rexBuilder.makeLiteral("2011-07-20 12:34:56 Asia/Tokyo");
    final RexLiteral timestampLTZChar2 = rexBuilder.makeLiteral("2011-07-20 12:34:56 GMT+01");
    final RexLiteral timestampLTZChar3 = rexBuilder.makeLiteral("2011-07-20 12:34:56 UTC");
    final RexLiteral literalTimestampLTZ = rexBuilder.makeTimestampWithLocalTimeZoneLiteral(new TimestampString(2011, 7, 20, 8, 23, 45), 0);
    final RelDataType dateType = typeFactory.createSqlType(SqlTypeName.DATE);
    final RelDataType timeType = typeFactory.createSqlType(SqlTypeName.TIME);
    final RelDataType timestampType = typeFactory.createSqlType(SqlTypeName.TIMESTAMP);
    final RelDataType timeLTZType = typeFactory.createSqlType(SqlTypeName.TIME_WITH_LOCAL_TIME_ZONE);
    final RelDataType timestampLTZType = typeFactory.createSqlType(SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE);
    final RelDataType varCharType = typeFactory.createSqlType(SqlTypeName.VARCHAR, 40);
    checkSimplify(cast(timeLTZChar1, timeLTZType), "20:34:45");
    checkSimplify(cast(timeLTZChar2, timeLTZType), "12:34:45");
    checkSimplify(cast(timeLTZChar3, timeLTZType), "11:34:45");
    checkSimplify(cast(literalTimeLTZ, timeLTZType), "01:23:45");
    checkSimplify(cast(timestampLTZChar1, timestampLTZType), "2011-07-20 03:34:56");
    checkSimplify(cast(timestampLTZChar2, timestampLTZType), "2011-07-20 11:34:56");
    checkSimplify(cast(timestampLTZChar3, timestampLTZType), "2011-07-20 12:34:56");
    checkSimplify(cast(literalTimestampLTZ, timestampLTZType), "2011-07-20 08:23:45");
    checkSimplify(cast(literalDate, timestampLTZType), "2011-07-20 07:00:00");
    checkSimplify(cast(literalTime, timestampLTZType), "2011-07-20 19:34:56");
    checkSimplify(cast(literalTimestamp, timestampLTZType), "2011-07-20 19:34:56");
    checkSimplify(cast(literalTimestamp, dateType), "2011-07-20");
    checkSimplify(cast(literalTimestampLTZ, dateType), "2011-07-20");
    checkSimplify(cast(literalTimestampLTZ, timeType), "01:23:45");
    checkSimplify(cast(literalTimestampLTZ, timestampType), "2011-07-20 01:23:45");
    checkSimplify(cast(literalTimeLTZ, timeType), "17:23:45");
    checkSimplify(cast(literalTime, timeLTZType), "20:34:56");
    checkSimplify(cast(literalTimestampLTZ, timeLTZType), "08:23:45");
    checkSimplify(cast(literalTimeLTZ, varCharType), "'17:23:45 America/Los_Angeles'");
    checkSimplify(cast(literalTimestampLTZ, varCharType), "'2011-07-20 01:23:45 America/Los_Angeles'");
    checkSimplify(cast(literalTimeLTZ, timestampType), "2011-07-19 18:23:45");
    checkSimplify(cast(literalTimeLTZ, timestampLTZType), "2011-07-20 01:23:45");
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) TimeString(org.apache.calcite.util.TimeString) DateString(org.apache.calcite.util.DateString) RelDataType(org.apache.calcite.rel.type.RelDataType) TimestampString(org.apache.calcite.util.TimestampString) Test(org.junit.Test)

Example 13 with DateString

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.DateString 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 14 with DateString

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

the class RexBuilderTest method testDateStringDayError.

/**
 * Tests {@link DateString} day range.
 */
@Test
public void testDateStringDayError() {
    try {
        final DateString dateString = new DateString(1969, 7, 41);
        fail("expected exception, got " + dateString);
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("Day out of range: [41]"));
    }
    try {
        final DateString dateString = new DateString("1234-01-32");
        fail("expected exception, got " + dateString);
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("Day out of range: [32]"));
    }
    // We don't worry about the number of days in a month. 30 is in range.
    final DateString dateString = new DateString("1234-02-30");
    assertThat(dateString, notNullValue());
}
Also used : DateString(org.apache.calcite.util.DateString) Test(org.junit.Test)

Example 15 with DateString

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

the class RexBuilderTest method testDateStringMonthError.

/**
 * Tests {@link DateString} month range.
 */
@Test
public void testDateStringMonthError() {
    try {
        final DateString dateString = new DateString(1969, 27, 21);
        fail("expected exception, got " + dateString);
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("Month out of range: [27]"));
    }
    try {
        final DateString dateString = new DateString("1234-13-02");
        fail("expected exception, got " + dateString);
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("Month out of range: [13]"));
    }
}
Also used : DateString(org.apache.calcite.util.DateString) Test(org.junit.Test)

Aggregations

DateString (org.apache.calcite.util.DateString)18 TimestampString (org.apache.calcite.util.TimestampString)11 TimeString (org.apache.calcite.util.TimeString)9 Test (org.junit.Test)9 RelDataType (org.apache.calcite.rel.type.RelDataType)7 BigDecimal (java.math.BigDecimal)6 RexNode (org.apache.calcite.rex.RexNode)5 Calendar (java.util.Calendar)4 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 List (java.util.List)2 Nullable (javax.annotation.Nullable)2 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)2 RexBuilder (org.apache.calcite.rex.RexBuilder)2 SqlIntervalQualifier (org.apache.calcite.sql.SqlIntervalQualifier)2 SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)2 CompositeList (org.apache.calcite.util.CompositeList)2 TimestampWithTimeZoneString (org.apache.calcite.util.TimestampWithTimeZoneString)2