Search in sources :

Example 11 with TimestampString

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

the class ExpressionConverterTest method testTimestampLiteral.

@Test
public void testTimestampLiteral() {
    RexNode rex = converter.visit(valueLiteral(LocalDateTime.parse("2012-12-12T12:12:12.12345"), DataTypes.TIMESTAMP(3).notNull()));
    assertThat(((RexLiteral) rex).getValueAs(TimestampString.class), equalTo(new TimestampString("2012-12-12 12:12:12.123")));
    assertThat(rex.getType().getSqlTypeName(), equalTo(SqlTypeName.TIMESTAMP));
    assertThat(rex.getType().getPrecision(), equalTo(3));
}
Also used : TimestampString(org.apache.calcite.util.TimestampString) RexNode(org.apache.calcite.rex.RexNode) Test(org.junit.Test)

Example 12 with TimestampString

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

the class TimestampStringUtils method toLocalDateTime.

/**
 * Convert a calcite's {@link TimestampString} to a {@link LocalDateTime}.
 */
public static LocalDateTime toLocalDateTime(TimestampString timestampString) {
    final String v = timestampString.toString();
    final int year = Integer.parseInt(v.substring(0, 4));
    final int month = Integer.parseInt(v.substring(5, 7));
    final int day = Integer.parseInt(v.substring(8, 10));
    final int h = Integer.parseInt(v.substring(11, 13));
    final int m = Integer.parseInt(v.substring(14, 16));
    final int s = Integer.parseInt(v.substring(17, 19));
    final int nano = getNanosInSecond(v);
    return LocalDateTime.of(year, month, day, h, m, s, nano);
}
Also used : TimestampString(org.apache.calcite.util.TimestampString)

Example 13 with TimestampString

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

the class RexNodeConverter method convert.

protected RexNode convert(ExprNodeConstantDesc literal) throws CalciteSemanticException {
    final RelDataTypeFactory dtFactory = rexBuilder.getTypeFactory();
    final PrimitiveTypeInfo hiveType = (PrimitiveTypeInfo) literal.getTypeInfo();
    final RelDataType calciteDataType = TypeConverter.convert(hiveType, dtFactory);
    PrimitiveCategory hiveTypeCategory = hiveType.getPrimitiveCategory();
    ConstantObjectInspector coi = literal.getWritableObjectInspector();
    Object value = ObjectInspectorUtils.copyToStandardJavaObject(coi.getWritableConstantValue(), coi);
    RexNode calciteLiteral = null;
    // If value is null, the type should also be VOID.
    if (value == null) {
        hiveTypeCategory = PrimitiveCategory.VOID;
    }
    // TODO: Verify if we need to use ConstantObjectInspector to unwrap data
    switch(hiveTypeCategory) {
        case BOOLEAN:
            calciteLiteral = rexBuilder.makeLiteral(((Boolean) value).booleanValue());
            break;
        case BYTE:
            calciteLiteral = rexBuilder.makeExactLiteral(new BigDecimal((Byte) value), calciteDataType);
            break;
        case SHORT:
            calciteLiteral = rexBuilder.makeExactLiteral(new BigDecimal((Short) value), calciteDataType);
            break;
        case INT:
            calciteLiteral = rexBuilder.makeExactLiteral(new BigDecimal((Integer) value));
            break;
        case LONG:
            calciteLiteral = rexBuilder.makeBigintLiteral(new BigDecimal((Long) value));
            break;
        case DECIMAL:
            if (value instanceof HiveDecimal) {
                value = ((HiveDecimal) value).bigDecimalValue();
            } else if (value instanceof Decimal128) {
                value = ((Decimal128) value).toBigDecimal();
            }
            if (value == null) {
                // literals.
                throw new CalciteSemanticException("Expression " + literal.getExprString() + " is not a valid decimal", UnsupportedFeature.Invalid_decimal);
            // TODO: return createNullLiteral(literal);
            }
            calciteLiteral = rexBuilder.makeExactLiteral((BigDecimal) value, calciteDataType);
            break;
        case FLOAT:
            calciteLiteral = rexBuilder.makeApproxLiteral(new BigDecimal(Float.toString((Float) value)), calciteDataType);
            break;
        case DOUBLE:
            // TODO: The best solution is to support NaN in expression reduction.
            if (Double.isNaN((Double) value)) {
                throw new CalciteSemanticException("NaN", UnsupportedFeature.Invalid_decimal);
            }
            calciteLiteral = rexBuilder.makeApproxLiteral(new BigDecimal(Double.toString((Double) value)), calciteDataType);
            break;
        case CHAR:
            if (value instanceof HiveChar) {
                value = ((HiveChar) value).getValue();
            }
            final int lengthChar = TypeInfoUtils.getCharacterLengthForType(hiveType);
            RelDataType charType = rexBuilder.getTypeFactory().createTypeWithCharsetAndCollation(rexBuilder.getTypeFactory().createSqlType(SqlTypeName.CHAR, lengthChar), Charset.forName(ConversionUtil.NATIVE_UTF16_CHARSET_NAME), SqlCollation.IMPLICIT);
            calciteLiteral = rexBuilder.makeLiteral(RexNodeExprFactory.makeHiveUnicodeString((String) value), charType, false);
            break;
        case VARCHAR:
            if (value instanceof HiveVarchar) {
                value = ((HiveVarchar) value).getValue();
            }
            final int lengthVarchar = TypeInfoUtils.getCharacterLengthForType(hiveType);
            RelDataType varcharType = rexBuilder.getTypeFactory().createTypeWithCharsetAndCollation(rexBuilder.getTypeFactory().createSqlType(SqlTypeName.VARCHAR, lengthVarchar), Charset.forName(ConversionUtil.NATIVE_UTF16_CHARSET_NAME), SqlCollation.IMPLICIT);
            calciteLiteral = rexBuilder.makeLiteral(RexNodeExprFactory.makeHiveUnicodeString((String) value), varcharType, true);
            break;
        case STRING:
            RelDataType stringType = rexBuilder.getTypeFactory().createTypeWithCharsetAndCollation(rexBuilder.getTypeFactory().createSqlType(SqlTypeName.VARCHAR, Integer.MAX_VALUE), Charset.forName(ConversionUtil.NATIVE_UTF16_CHARSET_NAME), SqlCollation.IMPLICIT);
            calciteLiteral = rexBuilder.makeLiteral(RexNodeExprFactory.makeHiveUnicodeString((String) value), stringType, true);
            break;
        case DATE:
            final Date date = (Date) value;
            calciteLiteral = rexBuilder.makeDateLiteral(DateString.fromDaysSinceEpoch(date.toEpochDay()));
            break;
        case TIMESTAMP:
            final TimestampString tsString;
            if (value instanceof Calendar) {
                tsString = TimestampString.fromCalendarFields((Calendar) value);
            } else {
                final Timestamp ts = (Timestamp) value;
                tsString = TimestampString.fromMillisSinceEpoch(ts.toEpochMilli()).withNanos(ts.getNanos());
            }
            // Must call makeLiteral, not makeTimestampLiteral
            // to have the RexBuilder.roundTime logic kick in
            calciteLiteral = rexBuilder.makeLiteral(tsString, rexBuilder.getTypeFactory().createSqlType(SqlTypeName.TIMESTAMP, rexBuilder.getTypeFactory().getTypeSystem().getDefaultPrecision(SqlTypeName.TIMESTAMP)), false);
            break;
        case TIMESTAMPLOCALTZ:
            final TimestampString tsLocalTZString;
            Instant i = ((TimestampTZ) value).getZonedDateTime().toInstant();
            tsLocalTZString = TimestampString.fromMillisSinceEpoch(i.toEpochMilli()).withNanos(i.getNano());
            calciteLiteral = rexBuilder.makeTimestampWithLocalTimeZoneLiteral(tsLocalTZString, rexBuilder.getTypeFactory().getTypeSystem().getDefaultPrecision(SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE));
            break;
        case INTERVAL_YEAR_MONTH:
            // Calcite year-month literal value is months as BigDecimal
            BigDecimal totalMonths = BigDecimal.valueOf(((HiveIntervalYearMonth) value).getTotalMonths());
            calciteLiteral = rexBuilder.makeIntervalLiteral(totalMonths, new SqlIntervalQualifier(TimeUnit.YEAR, TimeUnit.MONTH, new SqlParserPos(1, 1)));
            break;
        case INTERVAL_DAY_TIME:
            // Calcite day-time interval is millis value as BigDecimal
            // Seconds converted to millis
            BigDecimal secsValueBd = BigDecimal.valueOf(((HiveIntervalDayTime) value).getTotalSeconds() * 1000);
            // Nanos converted to millis
            BigDecimal nanosValueBd = BigDecimal.valueOf(((HiveIntervalDayTime) value).getNanos(), 6);
            calciteLiteral = rexBuilder.makeIntervalLiteral(secsValueBd.add(nanosValueBd), new SqlIntervalQualifier(TimeUnit.MILLISECOND, null, new SqlParserPos(1, 1)));
            break;
        case VOID:
            calciteLiteral = rexBuilder.makeLiteral(null, calciteDataType, true);
            break;
        case BINARY:
        case UNKNOWN:
        default:
            throw new RuntimeException("Unsupported Literal");
    }
    return calciteLiteral;
}
Also used : HiveChar(org.apache.hadoop.hive.common.type.HiveChar) RelDataType(org.apache.calcite.rel.type.RelDataType) GenericUDFTimestamp(org.apache.hadoop.hive.ql.udf.generic.GenericUDFTimestamp) Timestamp(org.apache.hadoop.hive.common.type.Timestamp) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) HiveDecimal(org.apache.hadoop.hive.common.type.HiveDecimal) CalciteSemanticException(org.apache.hadoop.hive.ql.optimizer.calcite.CalciteSemanticException) PrimitiveCategory(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory) HiveIntervalDayTime(org.apache.hadoop.hive.common.type.HiveIntervalDayTime) SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) SqlIntervalQualifier(org.apache.calcite.sql.SqlIntervalQualifier) Calendar(java.util.Calendar) Instant(java.time.Instant) Decimal128(org.apache.hadoop.hive.common.type.Decimal128) HiveVarchar(org.apache.hadoop.hive.common.type.HiveVarchar) BigDecimal(java.math.BigDecimal) Date(org.apache.hadoop.hive.common.type.Date) HiveFloorDate(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveFloorDate) HiveExtractDate(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveExtractDate) GenericUDFToDate(org.apache.hadoop.hive.ql.udf.generic.GenericUDFToDate) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) TimestampString(org.apache.calcite.util.TimestampString) RexNode(org.apache.calcite.rex.RexNode)

Example 14 with TimestampString

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

the class RexNodeExprFactory method createTimestampLocalTimeZoneConstantExpr.

/**
 * {@inheritDoc}
 */
@Override
protected RexLiteral createTimestampLocalTimeZoneConstantExpr(String value, ZoneId zoneId) {
    TimestampTZ t = TimestampTZUtil.parse(value);
    final TimestampString tsLocalTZString;
    if (value == null) {
        tsLocalTZString = null;
    } else {
        Instant i = t.getZonedDateTime().toInstant();
        tsLocalTZString = TimestampString.fromMillisSinceEpoch(i.toEpochMilli()).withNanos(i.getNano());
    }
    return rexBuilder.makeTimestampWithLocalTimeZoneLiteral(tsLocalTZString, rexBuilder.getTypeFactory().getTypeSystem().getDefaultPrecision(SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE));
}
Also used : TimestampTZ(org.apache.hadoop.hive.common.type.TimestampTZ) Instant(java.time.Instant) TimestampString(org.apache.calcite.util.TimestampString)

Example 15 with TimestampString

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

the class RexToExpressionTest method test_timestamp.

@Test
public void test_timestamp() {
    RexLiteral literal = literal(new TimestampString("2021-09-17 12:23:34"), new TimestampString("2021-09-17 12:23:35"), SqlTypeName.TIMESTAMP);
    Range<?> converted = convert(literal);
    assertThat(converted).isEqualToComparingFieldByField(range(LocalDateTime.of(2021, 9, 17, 12, 23, 34), LocalDateTime.of(2021, 9, 17, 12, 23, 35)));
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) TimestampString(org.apache.calcite.util.TimestampString) Test(org.junit.Test)

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