Search in sources :

Example 1 with TimestampString

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

the class SqlParserUtil method parseTimestampLiteral.

public static SqlTimestampLiteral parseTimestampLiteral(String s, SqlParserPos pos) {
    final String dateStr = parseString(s);
    final DateTimeUtils.PrecisionTime pt = DateTimeUtils.parsePrecisionDateTimeLiteral(dateStr, Format.PER_THREAD.get().timestamp, DateTimeUtils.UTC_ZONE, -1);
    if (pt == null) {
        throw SqlUtil.newContextException(pos, RESOURCE.illegalLiteral("TIMESTAMP", s, RESOURCE.badFormat(DateTimeUtils.TIMESTAMP_FORMAT_STRING).str()));
    }
    final TimestampString ts = TimestampString.fromCalendarFields(pt.getCalendar()).withFraction(pt.getFraction());
    return SqlLiteral.createTimestamp(ts, pt.getPrecision(), pos);
}
Also used : DateTimeUtils(org.apache.calcite.avatica.util.DateTimeUtils) TimeString(org.apache.calcite.util.TimeString) DateString(org.apache.calcite.util.DateString) TimestampString(org.apache.calcite.util.TimestampString) TimestampString(org.apache.calcite.util.TimestampString)

Example 2 with TimestampString

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

the class RexImplicationCheckerTest method testSimplifyFloor.

/**
 * Test case for simplifier of ceil and floor.
 */
@Test
public void testSimplifyFloor() {
    final ImmutableList<TimeUnitRange> timeUnitRanges = ImmutableList.of(TimeUnitRange.WEEK, TimeUnitRange.YEAR, TimeUnitRange.QUARTER, TimeUnitRange.MONTH, TimeUnitRange.DAY, TimeUnitRange.HOUR, TimeUnitRange.MINUTE, TimeUnitRange.SECOND, TimeUnitRange.MILLISECOND, TimeUnitRange.MICROSECOND);
    final Fixture f = new Fixture();
    final RexUtil.ExprSimplifier defaultSimplifier = new RexUtil.ExprSimplifier(f.simplify, true);
    final RexNode literalTs = f.timestampLiteral(new TimestampString("2010-10-10 00:00:00"));
    // Exclude WEEK as it is only used for the negative tests
    for (int i = 1; i < timeUnitRanges.size(); i++) {
        final RexNode innerFloorCall = f.rexBuilder.makeCall(SqlStdOperatorTable.CEIL, literalTs, f.rexBuilder.makeFlag(timeUnitRanges.get(i)));
        for (int j = 1; j <= i; j++) {
            final RexNode outerFloorCall = f.rexBuilder.makeCall(SqlStdOperatorTable.FLOOR, innerFloorCall, f.rexBuilder.makeFlag(timeUnitRanges.get(j)));
            final RexCall simplifiedExpr = (RexCall) defaultSimplifier.apply(outerFloorCall);
            assertThat(simplifiedExpr.getKind(), is(SqlKind.FLOOR));
            assertThat(((RexLiteral) simplifiedExpr.getOperands().get(1)).getValue().toString(), is(timeUnitRanges.get(j).toString()));
            assertThat(simplifiedExpr.getOperands().get(0).toString(), is(literalTs.toString()));
        }
    }
    // Negative test
    for (int i = timeUnitRanges.size() - 1; i >= 0; i--) {
        final RexNode innerFloorCall = f.rexBuilder.makeCall(SqlStdOperatorTable.FLOOR, literalTs, f.rexBuilder.makeFlag(timeUnitRanges.get(i)));
        for (int j = timeUnitRanges.size() - 1; j > i; j--) {
            final RexNode outerFloorCall = f.rexBuilder.makeCall(SqlStdOperatorTable.CEIL, innerFloorCall, f.rexBuilder.makeFlag(timeUnitRanges.get(j)));
            final RexCall simplifiedExpr = (RexCall) defaultSimplifier.apply(outerFloorCall);
            assertThat(simplifiedExpr.toString(), is(outerFloorCall.toString()));
        }
    }
}
Also used : RexCall(org.apache.calcite.rex.RexCall) RexLiteral(org.apache.calcite.rex.RexLiteral) TimestampString(org.apache.calcite.util.TimestampString) TimeUnitRange(org.apache.calcite.avatica.util.TimeUnitRange) RexUtil(org.apache.calcite.rex.RexUtil) RexNode(org.apache.calcite.rex.RexNode) Test(org.junit.Test)

Example 3 with TimestampString

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

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

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

the class DruidJsonFilter method toDruidLiteral.

/**
 * @param rexNode    rexNode to translate to Druid literal equivalante
 * @param rowType    rowType associated to rexNode
 * @param druidQuery druid Query
 *
 * @return non null string or null if it can not translate to valid Druid equivalent
 */
@Nullable
private static String toDruidLiteral(RexNode rexNode, RelDataType rowType, DruidQuery druidQuery) {
    final SimpleDateFormat dateFormatter = new SimpleDateFormat(TimeExtractionFunction.ISO_TIME_FORMAT, Locale.ROOT);
    final String timeZone = druidQuery.getConnectionConfig().timeZone();
    if (timeZone != null) {
        dateFormatter.setTimeZone(TimeZone.getTimeZone(timeZone));
    }
    final String val;
    final RexLiteral rhsLiteral = (RexLiteral) rexNode;
    if (SqlTypeName.NUMERIC_TYPES.contains(rhsLiteral.getTypeName())) {
        val = String.valueOf(RexLiteral.value(rhsLiteral));
    } else if (SqlTypeName.CHAR_TYPES.contains(rhsLiteral.getTypeName())) {
        val = String.valueOf(RexLiteral.stringValue(rhsLiteral));
    } else if (SqlTypeName.TIMESTAMP == rhsLiteral.getTypeName() || SqlTypeName.DATE == rhsLiteral.getTypeName() || SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE == rhsLiteral.getTypeName()) {
        TimestampString timestampString = DruidDateTimeUtils.literalValue(rexNode, TimeZone.getTimeZone(timeZone));
        if (timestampString == null) {
            throw new AssertionError("Cannot translate Literal" + rexNode + " of type " + rhsLiteral.getTypeName() + " to TimestampString");
        }
        // @TODO this is unnecessary we can send time as Long (eg millis since epoch) to druid
        val = dateFormatter.format(timestampString.getMillisSinceEpoch());
    } else {
        // Don't know how to filter on this kind of literal.
        val = null;
    }
    return val;
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) TimestampString(org.apache.calcite.util.TimestampString) TimestampString(org.apache.calcite.util.TimestampString) SimpleDateFormat(java.text.SimpleDateFormat) Nullable(javax.annotation.Nullable)

Aggregations

TimestampString (org.apache.calcite.util.TimestampString)22 DateString (org.apache.calcite.util.DateString)11 TimeString (org.apache.calcite.util.TimeString)10 RelDataType (org.apache.calcite.rel.type.RelDataType)8 Test (org.junit.Test)8 BigDecimal (java.math.BigDecimal)7 RexLiteral (org.apache.calcite.rex.RexLiteral)6 RexNode (org.apache.calcite.rex.RexNode)6 Calendar (java.util.Calendar)5 NlsString (org.apache.calcite.util.NlsString)5 Nullable (javax.annotation.Nullable)3 ByteString (org.apache.calcite.avatica.util.ByteString)3 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)3 SqlIntervalQualifier (org.apache.calcite.sql.SqlIntervalQualifier)3 ImmutableList (com.google.common.collect.ImmutableList)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Instant (java.time.Instant)2 List (java.util.List)2 DateTimeUtils (org.apache.calcite.avatica.util.DateTimeUtils)2 RexBuilder (org.apache.calcite.rex.RexBuilder)2