Search in sources :

Example 1 with DateString

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

the class SqlParserUtil method parseDateLiteral.

public static SqlDateLiteral parseDateLiteral(String s, SqlParserPos pos) {
    final String dateStr = parseString(s);
    final Calendar cal = DateTimeUtils.parseDateFormat(dateStr, Format.PER_THREAD.get().date, DateTimeUtils.UTC_ZONE);
    if (cal == null) {
        throw SqlUtil.newContextException(pos, RESOURCE.illegalLiteral("DATE", s, RESOURCE.badFormat(DateTimeUtils.DATE_FORMAT_STRING).str()));
    }
    final DateString d = DateString.fromCalendarFields(cal);
    return SqlLiteral.createDate(d, pos);
}
Also used : Calendar(java.util.Calendar) DateString(org.apache.calcite.util.DateString) TimeString(org.apache.calcite.util.TimeString) DateString(org.apache.calcite.util.DateString) TimestampString(org.apache.calcite.util.TimestampString)

Example 2 with DateString

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

the class RexImplicationCheckerTest method testSimpleDate.

@Test
public void testSimpleDate() {
    final Fixture f = new Fixture();
    final DateString d = DateString.fromCalendarFields(Util.calendar());
    final RexNode node1 = f.ge(f.d, f.dateLiteral(d));
    final RexNode node2 = f.eq(f.d, f.dateLiteral(d));
    f.checkImplies(node2, node1);
    f.checkNotImplies(node1, node2);
    final DateString dBeforeEpoch1 = DateString.fromDaysSinceEpoch(-12345);
    final DateString dBeforeEpoch2 = DateString.fromDaysSinceEpoch(-123);
    final RexNode nodeBe1 = f.lt(f.d, f.dateLiteral(dBeforeEpoch1));
    final RexNode nodeBe2 = f.lt(f.d, f.dateLiteral(dBeforeEpoch2));
    f.checkImplies(nodeBe1, nodeBe2);
    f.checkNotImplies(nodeBe2, nodeBe1);
}
Also used : DateString(org.apache.calcite.util.DateString) RexNode(org.apache.calcite.rex.RexNode) Test(org.junit.Test)

Example 3 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 testSimplifyCastLiteral.

@Test
public void testSimplifyCastLiteral() {
    final List<RexLiteral> literals = new ArrayList<>();
    literals.add(rexBuilder.makeExactLiteral(BigDecimal.ONE, typeFactory.createSqlType(SqlTypeName.INTEGER)));
    literals.add(rexBuilder.makeExactLiteral(BigDecimal.valueOf(2), typeFactory.createSqlType(SqlTypeName.BIGINT)));
    literals.add(rexBuilder.makeExactLiteral(BigDecimal.valueOf(3), typeFactory.createSqlType(SqlTypeName.SMALLINT)));
    literals.add(rexBuilder.makeExactLiteral(BigDecimal.valueOf(4), typeFactory.createSqlType(SqlTypeName.TINYINT)));
    literals.add(rexBuilder.makeExactLiteral(new BigDecimal("1234"), typeFactory.createSqlType(SqlTypeName.DECIMAL, 4, 0)));
    literals.add(rexBuilder.makeExactLiteral(new BigDecimal("123.45"), typeFactory.createSqlType(SqlTypeName.DECIMAL, 5, 2)));
    literals.add(rexBuilder.makeApproxLiteral(new BigDecimal("3.1415"), typeFactory.createSqlType(SqlTypeName.REAL)));
    literals.add(rexBuilder.makeApproxLiteral(BigDecimal.valueOf(Math.E), typeFactory.createSqlType(SqlTypeName.FLOAT)));
    literals.add(rexBuilder.makeApproxLiteral(BigDecimal.valueOf(Math.PI), typeFactory.createSqlType(SqlTypeName.DOUBLE)));
    literals.add(rexBuilder.makeLiteral(true));
    literals.add(rexBuilder.makeLiteral(false));
    literals.add(rexBuilder.makeLiteral("hello world"));
    literals.add(rexBuilder.makeLiteral("1969-07-20 12:34:56"));
    literals.add(rexBuilder.makeLiteral("1969-07-20"));
    literals.add(rexBuilder.makeLiteral("12:34:45"));
    literals.add((RexLiteral) rexBuilder.makeLiteral(new ByteString(new byte[] { 1, 2, -34, 0, -128 }), typeFactory.createSqlType(SqlTypeName.BINARY, 5), false));
    literals.add(rexBuilder.makeDateLiteral(new DateString(1974, 8, 9)));
    literals.add(rexBuilder.makeTimeLiteral(new TimeString(1, 23, 45), 0));
    literals.add(rexBuilder.makeTimestampLiteral(new TimestampString(1974, 8, 9, 1, 23, 45), 0));
    final Multimap<SqlTypeName, RexLiteral> map = LinkedHashMultimap.create();
    for (RexLiteral literal : literals) {
        map.put(literal.getTypeName(), literal);
    }
    final List<RelDataType> types = new ArrayList<>();
    types.add(typeFactory.createSqlType(SqlTypeName.INTEGER));
    types.add(typeFactory.createSqlType(SqlTypeName.BIGINT));
    types.add(typeFactory.createSqlType(SqlTypeName.SMALLINT));
    types.add(typeFactory.createSqlType(SqlTypeName.TINYINT));
    types.add(typeFactory.createSqlType(SqlTypeName.REAL));
    types.add(typeFactory.createSqlType(SqlTypeName.FLOAT));
    types.add(typeFactory.createSqlType(SqlTypeName.DOUBLE));
    types.add(typeFactory.createSqlType(SqlTypeName.BOOLEAN));
    types.add(typeFactory.createSqlType(SqlTypeName.VARCHAR, 10));
    types.add(typeFactory.createSqlType(SqlTypeName.CHAR, 5));
    types.add(typeFactory.createSqlType(SqlTypeName.VARBINARY, 60));
    types.add(typeFactory.createSqlType(SqlTypeName.BINARY, 3));
    types.add(typeFactory.createSqlType(SqlTypeName.TIMESTAMP));
    types.add(typeFactory.createSqlType(SqlTypeName.TIME));
    types.add(typeFactory.createSqlType(SqlTypeName.DATE));
    for (RelDataType fromType : types) {
        for (RelDataType toType : types) {
            if (SqlTypeAssignmentRules.instance(false).canCastFrom(toType.getSqlTypeName(), fromType.getSqlTypeName())) {
                for (RexLiteral literal : map.get(fromType.getSqlTypeName())) {
                    final RexNode cast = rexBuilder.makeCast(toType, literal);
                    if (cast instanceof RexLiteral) {
                        assertThat(cast.getType(), is(toType));
                        // makeCast already simplified
                        continue;
                    }
                    final RexNode simplified = simplify.simplify(cast);
                    boolean expectedSimplify = literal.getTypeName() != toType.getSqlTypeName() || (literal.getTypeName() == SqlTypeName.CHAR && ((NlsString) literal.getValue()).getValue().length() > toType.getPrecision()) || (literal.getTypeName() == SqlTypeName.BINARY && ((ByteString) literal.getValue()).length() > toType.getPrecision());
                    boolean couldSimplify = !cast.equals(simplified);
                    final String reason = (expectedSimplify ? "expected to simplify, but could not: " : "simplified, but did not expect to: ") + cast + " --> " + simplified;
                    assertThat(reason, couldSimplify, is(expectedSimplify));
                }
            }
        }
    }
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) TimeString(org.apache.calcite.util.TimeString) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) ByteString(org.apache.calcite.avatica.util.ByteString) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) DateString(org.apache.calcite.util.DateString) TimestampWithTimeZoneString(org.apache.calcite.util.TimestampWithTimeZoneString) ByteString(org.apache.calcite.avatica.util.ByteString) TimestampString(org.apache.calcite.util.TimestampString) TimeString(org.apache.calcite.util.TimeString) NlsString(org.apache.calcite.util.NlsString) BigDecimal(java.math.BigDecimal) DateString(org.apache.calcite.util.DateString) NlsString(org.apache.calcite.util.NlsString) TimestampString(org.apache.calcite.util.TimestampString) RexNode(org.apache.calcite.rex.RexNode) Test(org.junit.Test)

Example 4 with DateString

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

/**
 * Tests {@link RexBuilder#makeDateLiteral(DateString)}.
 */
@Test
public void testDateLiteral() {
    final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
    RelDataType dateType = typeFactory.createSqlType(SqlTypeName.DATE);
    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);
    calendar.set(Calendar.MILLISECOND, 0);
    checkDate(builder.makeLiteral(calendar, dateType, false));
    // Old way #2: Provide in Integer
    checkDate(builder.makeLiteral(MOON_DAY, dateType, false));
    // The new way
    final DateString d = new DateString(1969, 7, 21);
    checkDate(builder.makeLiteral(d, dateType, false));
}
Also used : SqlTypeFactoryImpl(org.apache.calcite.sql.type.SqlTypeFactoryImpl) Calendar(java.util.Calendar) DateString(org.apache.calcite.util.DateString) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) 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