Search in sources :

Example 1 with TimestampWithTimeZoneString

use of org.apache.calcite.util.TimestampWithTimeZoneString in project calcite by apache.

the class RexProgramTest method testCompareTimestampWithTimeZone.

@Test
public void testCompareTimestampWithTimeZone() {
    final TimestampWithTimeZoneString timestampLTZChar1 = new TimestampWithTimeZoneString("2011-07-20 10:34:56 America/Los_Angeles");
    final TimestampWithTimeZoneString timestampLTZChar2 = new TimestampWithTimeZoneString("2011-07-20 19:34:56 Europe/Rome");
    final TimestampWithTimeZoneString timestampLTZChar3 = new TimestampWithTimeZoneString("2011-07-20 01:34:56 Asia/Tokyo");
    final TimestampWithTimeZoneString timestampLTZChar4 = new TimestampWithTimeZoneString("2011-07-20 10:34:56 America/Los_Angeles");
    assertThat(timestampLTZChar1.equals(timestampLTZChar2), is(false));
    assertThat(timestampLTZChar1.equals(timestampLTZChar3), is(false));
    assertThat(timestampLTZChar1.equals(timestampLTZChar4), is(true));
}
Also used : TimestampWithTimeZoneString(org.apache.calcite.util.TimestampWithTimeZoneString) Test(org.junit.Test)

Example 2 with TimestampWithTimeZoneString

use of org.apache.calcite.util.TimestampWithTimeZoneString in project calcite by apache.

the class DruidDateTimeUtils method literalValue.

@Nullable
protected static TimestampString literalValue(RexNode node, TimeZone timeZone) {
    switch(node.getKind()) {
        case LITERAL:
            switch(((RexLiteral) node).getTypeName()) {
                case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
                    return ((RexLiteral) node).getValueAs(TimestampString.class);
                case TIMESTAMP:
                    // Cast timestamp to timestamp with local time zone
                    final TimestampString t = ((RexLiteral) node).getValueAs(TimestampString.class);
                    return new TimestampWithTimeZoneString(t.toString() + " " + timeZone.getID()).withTimeZone(DateTimeUtils.UTC_ZONE).getLocalTimestampString();
                case DATE:
                    // Cast date to timestamp with local time zone
                    final DateString d = ((RexLiteral) node).getValueAs(DateString.class);
                    return new TimestampWithTimeZoneString(TimestampString.fromMillisSinceEpoch(d.getMillisSinceEpoch()).toString() + " " + timeZone.getID()).withTimeZone(DateTimeUtils.UTC_ZONE).getLocalTimestampString();
            }
            break;
        case CAST:
            // We can handle that case by traversing the dummy CAST.
            assert node instanceof RexCall;
            final RexCall call = (RexCall) node;
            final RexNode operand = call.getOperands().get(0);
            final RelDataType callType = call.getType();
            final RelDataType operandType = operand.getType();
            if (operand.getKind() == SqlKind.LITERAL && callType.getSqlTypeName() == operandType.getSqlTypeName() && (callType.getSqlTypeName() == SqlTypeName.DATE || callType.getSqlTypeName() == SqlTypeName.TIMESTAMP || callType.getSqlTypeName() == SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE) && callType.isNullable() && !operandType.isNullable()) {
                return literalValue(operand, timeZone);
            }
    }
    return null;
}
Also used : RexCall(org.apache.calcite.rex.RexCall) RexLiteral(org.apache.calcite.rex.RexLiteral) DateString(org.apache.calcite.util.DateString) TimestampWithTimeZoneString(org.apache.calcite.util.TimestampWithTimeZoneString) RelDataType(org.apache.calcite.rel.type.RelDataType) TimestampString(org.apache.calcite.util.TimestampString) RexNode(org.apache.calcite.rex.RexNode) Nullable(javax.annotation.Nullable)

Example 3 with TimestampWithTimeZoneString

use of org.apache.calcite.util.TimestampWithTimeZoneString in project calcite by apache.

the class RexBuilderTest method testTimestampWithLocalTimeZoneLiteral.

/**
 * Tests
 * {@link RexBuilder#makeTimestampWithLocalTimeZoneLiteral(TimestampString, int)}.
 */
@Test
public void testTimestampWithLocalTimeZoneLiteral() {
    final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
    final RelDataType timestampType = typeFactory.createSqlType(SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE);
    final RelDataType timestampType3 = typeFactory.createSqlType(SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE, 3);
    final RelDataType timestampType9 = typeFactory.createSqlType(SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE, 9);
    final RelDataType timestampType18 = typeFactory.createSqlType(SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE, 18);
    final RexBuilder builder = new RexBuilder(typeFactory);
    // The new way
    final TimestampWithTimeZoneString ts = new TimestampWithTimeZoneString(1969, 7, 21, 2, 56, 15, TimeZone.getTimeZone("PST").getID());
    checkTimestampWithLocalTimeZone(builder.makeLiteral(ts.getLocalTimestampString(), timestampType, false));
    // Now with milliseconds
    final TimestampWithTimeZoneString ts2 = ts.withMillis(56);
    assertThat(ts2.toString(), is("1969-07-21 02:56:15.056 PST"));
    final RexNode literal2 = builder.makeLiteral(ts2.getLocalTimestampString(), timestampType3, false);
    assertThat(((RexLiteral) literal2).getValue().toString(), is("1969-07-21 02:56:15.056"));
    // Now with nanoseconds
    final TimestampWithTimeZoneString ts3 = ts.withNanos(56);
    final RexNode literal3 = builder.makeLiteral(ts3.getLocalTimestampString(), timestampType9, false);
    assertThat(((RexLiteral) literal3).getValueAs(TimestampString.class).toString(), is("1969-07-21 02:56:15"));
    final TimestampWithTimeZoneString ts3b = ts.withNanos(2345678);
    final RexNode literal3b = builder.makeLiteral(ts3b.getLocalTimestampString(), timestampType9, false);
    assertThat(((RexLiteral) literal3b).getValueAs(TimestampString.class).toString(), is("1969-07-21 02:56:15.002"));
    // Now with a very long fraction
    final TimestampWithTimeZoneString ts4 = ts.withFraction("102030405060708090102");
    final RexNode literal4 = builder.makeLiteral(ts4.getLocalTimestampString(), timestampType18, false);
    assertThat(((RexLiteral) literal4).getValueAs(TimestampString.class).toString(), is("1969-07-21 02:56:15.102"));
    // toString
    assertThat(ts2.round(1).toString(), is("1969-07-21 02:56:15 PST"));
    assertThat(ts2.round(2).toString(), is("1969-07-21 02:56:15.05 PST"));
    assertThat(ts2.round(3).toString(), is("1969-07-21 02:56:15.056 PST"));
    assertThat(ts2.round(4).toString(), is("1969-07-21 02:56:15.056 PST"));
    assertThat(ts2.toString(6), is("1969-07-21 02:56:15.056000 PST"));
    assertThat(ts2.toString(1), is("1969-07-21 02:56:15.0 PST"));
    assertThat(ts2.toString(0), is("1969-07-21 02:56:15 PST"));
    assertThat(ts2.round(0).toString(), is("1969-07-21 02:56:15 PST"));
    assertThat(ts2.round(0).toString(0), is("1969-07-21 02:56:15 PST"));
    assertThat(ts2.round(0).toString(1), is("1969-07-21 02:56:15.0 PST"));
    assertThat(ts2.round(0).toString(2), is("1969-07-21 02:56:15.00 PST"));
}
Also used : SqlTypeFactoryImpl(org.apache.calcite.sql.type.SqlTypeFactoryImpl) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) TimestampWithTimeZoneString(org.apache.calcite.util.TimestampWithTimeZoneString) RelDataType(org.apache.calcite.rel.type.RelDataType) Test(org.junit.Test)

Aggregations

TimestampWithTimeZoneString (org.apache.calcite.util.TimestampWithTimeZoneString)3 RelDataType (org.apache.calcite.rel.type.RelDataType)2 Test (org.junit.Test)2 Nullable (javax.annotation.Nullable)1 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)1 RexCall (org.apache.calcite.rex.RexCall)1 RexLiteral (org.apache.calcite.rex.RexLiteral)1 RexNode (org.apache.calcite.rex.RexNode)1 SqlTypeFactoryImpl (org.apache.calcite.sql.type.SqlTypeFactoryImpl)1 DateString (org.apache.calcite.util.DateString)1 TimestampString (org.apache.calcite.util.TimestampString)1