Search in sources :

Example 6 with TimestampString

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

the class RexBuilder method makeLiteral.

/**
 * Internal method to create a call to a literal. Code outside this package
 * should call one of the type-specific methods such as
 * {@link #makeDateLiteral(DateString)}, {@link #makeLiteral(boolean)},
 * {@link #makeLiteral(String)}.
 *
 * @param o        Value of literal, must be appropriate for the type
 * @param type     Type of literal
 * @param typeName SQL type of literal
 * @return Literal
 */
protected RexLiteral makeLiteral(Comparable o, RelDataType type, SqlTypeName typeName) {
    // All literals except NULL have NOT NULL types.
    type = typeFactory.createTypeWithNullability(type, o == null);
    int p;
    switch(typeName) {
        case CHAR:
            // from the type if necessary.
            assert o instanceof NlsString;
            NlsString nlsString = (NlsString) o;
            if ((nlsString.getCollation() == null) || (nlsString.getCharset() == null)) {
                assert type.getSqlTypeName() == SqlTypeName.CHAR;
                assert type.getCharset().name() != null;
                assert type.getCollation() != null;
                o = new NlsString(nlsString.getValue(), type.getCharset().name(), type.getCollation());
            }
            break;
        case TIME:
            assert o instanceof TimeString;
            p = type.getPrecision();
            if (p == RelDataType.PRECISION_NOT_SPECIFIED) {
                p = 0;
            }
            o = ((TimeString) o).round(p);
            break;
        case TIME_WITH_LOCAL_TIME_ZONE:
            assert o instanceof TimeString;
            p = type.getPrecision();
            if (p == RelDataType.PRECISION_NOT_SPECIFIED) {
                p = 0;
            }
            o = ((TimeString) o).round(p);
            break;
        case TIMESTAMP:
            assert o instanceof TimestampString;
            p = type.getPrecision();
            if (p == RelDataType.PRECISION_NOT_SPECIFIED) {
                p = 0;
            }
            o = ((TimestampString) o).round(p);
            break;
        case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
            assert o instanceof TimestampString;
            p = type.getPrecision();
            if (p == RelDataType.PRECISION_NOT_SPECIFIED) {
                p = 0;
            }
            o = ((TimestampString) o).round(p);
    }
    return new RexLiteral(o, type, typeName);
}
Also used : TimeString(org.apache.calcite.util.TimeString) NlsString(org.apache.calcite.util.NlsString) TimestampString(org.apache.calcite.util.TimestampString)

Example 7 with TimestampString

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

the class DruidDateRangeRulesTest method testFilterWithCast.

/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-1738">[CALCITE-1738]
 * Push CAST of literals to Druid</a>.
 */
@Test
public void testFilterWithCast() {
    final Fixture2 f = new Fixture2();
    final Calendar c = Util.calendar();
    c.clear();
    c.set(2010, Calendar.JANUARY, 1);
    final TimestampString from = TimestampString.fromCalendarFields(c);
    c.clear();
    c.set(2011, Calendar.JANUARY, 1);
    final TimestampString to = TimestampString.fromCalendarFields(c);
    // d >= 2010-01-01 AND d < 2011-01-01
    checkDateRangeNoSimplify(f, f.and(f.ge(f.d, f.cast(f.timestampDataType, f.timestampLiteral(from))), f.lt(f.d, f.cast(f.timestampDataType, f.timestampLiteral(to)))), is("[2010-01-01T00:00:00.000Z/2011-01-01T00:00:00.000Z]"));
}
Also used : Calendar(java.util.Calendar) TimestampString(org.apache.calcite.util.TimestampString) Test(org.junit.Test)

Example 8 with TimestampString

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

the class HiveParserRexNodeConverter method convertConstant.

public static RexNode convertConstant(ExprNodeConstantDesc literal, RelOptCluster cluster) throws SemanticException {
    RexBuilder rexBuilder = cluster.getRexBuilder();
    RelDataTypeFactory dtFactory = rexBuilder.getTypeFactory();
    PrimitiveTypeInfo hiveType = (PrimitiveTypeInfo) literal.getTypeInfo();
    RelDataType calciteDataType = HiveParserTypeConverter.convert(hiveType, dtFactory);
    PrimitiveObjectInspector.PrimitiveCategory hiveTypeCategory = hiveType.getPrimitiveCategory();
    ConstantObjectInspector coi = literal.getWritableObjectInspector();
    Object value = ObjectInspectorUtils.copyToStandardJavaObject(coi.getWritableConstantValue(), coi);
    RexNode calciteLiteral;
    HiveShim hiveShim = HiveParserUtils.getSessionHiveShim();
    // If value is null, the type should also be VOID.
    if (value == null) {
        hiveTypeCategory = PrimitiveObjectInspector.PrimitiveCategory.VOID;
    }
    // TODO: Verify if we need to use ConstantObjectInspector to unwrap data
    switch(hiveTypeCategory) {
        case BOOLEAN:
            calciteLiteral = rexBuilder.makeLiteral((Boolean) value);
            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;
        // TODO: is Decimal an exact numeric or approximate numeric?
        case DECIMAL:
            if (value instanceof HiveDecimal) {
                value = ((HiveDecimal) value).bigDecimalValue();
            } else if (value instanceof Decimal128) {
                value = ((Decimal128) value).toBigDecimal();
            }
            if (value == null) {
                // For now, we will not run CBO in the presence of invalid decimal literals.
                throw new SemanticException("Expression " + literal.getExprString() + " is not a valid decimal");
            // TODO: return createNullLiteral(literal);
            }
            BigDecimal bd = (BigDecimal) value;
            BigInteger unscaled = bd.unscaledValue();
            if (unscaled.compareTo(MIN_LONG_BI) >= 0 && unscaled.compareTo(MAX_LONG_BI) <= 0) {
                calciteLiteral = rexBuilder.makeExactLiteral(bd);
            } else {
                // CBO doesn't support unlimited precision decimals. In practice, this
                // will work...
                // An alternative would be to throw CboSemanticException and fall back
                // to no CBO.
                RelDataType relType = cluster.getTypeFactory().createSqlType(SqlTypeName.DECIMAL, unscaled.toString().length(), bd.scale());
                calciteLiteral = rexBuilder.makeExactLiteral(bd, relType);
            }
            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 SemanticException("NaN");
            }
            calciteLiteral = rexBuilder.makeApproxLiteral(new BigDecimal(Double.toString((Double) value)), calciteDataType);
            break;
        case CHAR:
            if (value instanceof HiveChar) {
                value = ((HiveChar) value).getValue();
            }
            calciteLiteral = rexBuilder.makeCharLiteral(asUnicodeString((String) value));
            break;
        case VARCHAR:
            if (value instanceof HiveVarchar) {
                value = ((HiveVarchar) value).getValue();
            }
            calciteLiteral = rexBuilder.makeCharLiteral(asUnicodeString((String) value));
            break;
        case STRING:
            Object constantDescVal = literal.getValue();
            constantDescVal = constantDescVal instanceof NlsString ? constantDescVal : asUnicodeString((String) value);
            // calcite treat string literal as char type, we should treat it as string just like
            // hive
            RelDataType type = HiveParserTypeConverter.convert(hiveType, dtFactory);
            // if we get here, the value is not null
            type = dtFactory.createTypeWithNullability(type, false);
            calciteLiteral = rexBuilder.makeLiteral(constantDescVal, type, true);
            break;
        case DATE:
            LocalDate localDate = HiveParserUtils.getSessionHiveShim().toFlinkDate(value);
            DateString dateString = new DateString(localDate.getYear(), localDate.getMonthValue(), localDate.getDayOfMonth());
            calciteLiteral = rexBuilder.makeDateLiteral(dateString);
            break;
        case TIMESTAMP:
            TimestampString timestampString;
            if (value instanceof Calendar) {
                timestampString = TimestampString.fromCalendarFields((Calendar) value);
            } else {
                LocalDateTime localDateTime = HiveParserUtils.getSessionHiveShim().toFlinkTimestamp(value);
                timestampString = new TimestampString(localDateTime.getYear(), localDateTime.getMonthValue(), localDateTime.getDayOfMonth(), localDateTime.getHour(), localDateTime.getMinute(), localDateTime.getSecond());
                timestampString = timestampString.withNanos(localDateTime.getNano());
            }
            // hive always treats timestamp with precision 9
            calciteLiteral = rexBuilder.makeTimestampLiteral(timestampString, 9);
            break;
        case VOID:
            calciteLiteral = cluster.getRexBuilder().makeLiteral(null, dtFactory.createSqlType(SqlTypeName.NULL), true);
            break;
        case BINARY:
        case UNKNOWN:
        default:
            if (hiveShim.isIntervalYearMonthType(hiveTypeCategory)) {
                // Calcite year-month literal value is months as BigDecimal
                BigDecimal totalMonths = BigDecimal.valueOf(((HiveParserIntervalYearMonth) value).getTotalMonths());
                calciteLiteral = rexBuilder.makeIntervalLiteral(totalMonths, new SqlIntervalQualifier(TimeUnit.YEAR, TimeUnit.MONTH, new SqlParserPos(1, 1)));
            } else if (hiveShim.isIntervalDayTimeType(hiveTypeCategory)) {
                // Calcite day-time interval is millis value as BigDecimal
                // Seconds converted to millis
                BigDecimal secsValueBd = BigDecimal.valueOf(((HiveParserIntervalDayTime) value).getTotalSeconds() * 1000);
                // Nanos converted to millis
                BigDecimal nanosValueBd = BigDecimal.valueOf(((HiveParserIntervalDayTime) value).getNanos(), 6);
                calciteLiteral = rexBuilder.makeIntervalLiteral(secsValueBd.add(nanosValueBd), new SqlIntervalQualifier(TimeUnit.MILLISECOND, null, new SqlParserPos(1, 1)));
            } else {
                throw new RuntimeException("UnSupported Literal type " + hiveTypeCategory);
            }
    }
    return calciteLiteral;
}
Also used : LocalDateTime(java.time.LocalDateTime) HiveChar(org.apache.hadoop.hive.common.type.HiveChar) RelDataType(org.apache.calcite.rel.type.RelDataType) LocalDate(java.time.LocalDate) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) HiveDecimal(org.apache.hadoop.hive.common.type.HiveDecimal) RexBuilder(org.apache.calcite.rex.RexBuilder) SemanticException(org.apache.hadoop.hive.ql.parse.SemanticException) SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) SqlIntervalQualifier(org.apache.calcite.sql.SqlIntervalQualifier) Calendar(java.util.Calendar) Decimal128(org.apache.hadoop.hive.common.type.Decimal128) HiveVarchar(org.apache.hadoop.hive.common.type.HiveVarchar) BigDecimal(java.math.BigDecimal) DateString(org.apache.calcite.util.DateString) BigInteger(java.math.BigInteger) NlsString(org.apache.calcite.util.NlsString) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) TimestampString(org.apache.calcite.util.TimestampString) HiveShim(org.apache.flink.table.catalog.hive.client.HiveShim) HiveParserIntervalDayTime(org.apache.flink.table.planner.delegation.hive.copy.HiveParserIntervalDayTime) RexNode(org.apache.calcite.rex.RexNode)

Example 9 with TimestampString

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

the class RexNodeJsonSerdeTest method testRexNodeSerde.

// --------------------------------------------------------------------------------------------
// Test data
// --------------------------------------------------------------------------------------------
@SuppressWarnings("UnstableApiUsage")
private static Stream<RexNode> testRexNodeSerde() {
    final RexBuilder rexBuilder = new RexBuilder(FACTORY);
    final RelDataType inputType = FACTORY.createStructType(StructKind.PEEK_FIELDS_NO_EXPAND, Arrays.asList(FACTORY.createSqlType(SqlTypeName.INTEGER), FACTORY.createSqlType(SqlTypeName.BIGINT), FACTORY.createStructType(StructKind.PEEK_FIELDS_NO_EXPAND, Arrays.asList(FACTORY.createSqlType(SqlTypeName.VARCHAR), FACTORY.createSqlType(SqlTypeName.VARCHAR)), Arrays.asList("n1", "n2"))), Arrays.asList("f1", "f2", "f3"));
    return Stream.of(rexBuilder.makeNullLiteral(FACTORY.createSqlType(SqlTypeName.VARCHAR)), rexBuilder.makeLiteral(true), rexBuilder.makeExactLiteral(new BigDecimal(Byte.MAX_VALUE), FACTORY.createSqlType(SqlTypeName.TINYINT)), rexBuilder.makeExactLiteral(new BigDecimal(Short.MAX_VALUE), FACTORY.createSqlType(SqlTypeName.SMALLINT)), rexBuilder.makeExactLiteral(new BigDecimal(Integer.MAX_VALUE), FACTORY.createSqlType(SqlTypeName.INTEGER)), rexBuilder.makeExactLiteral(new BigDecimal(Long.MAX_VALUE), FACTORY.createSqlType(SqlTypeName.BIGINT)), rexBuilder.makeExactLiteral(BigDecimal.valueOf(Double.MAX_VALUE), FACTORY.createSqlType(SqlTypeName.DOUBLE)), rexBuilder.makeApproxLiteral(BigDecimal.valueOf(Float.MAX_VALUE), FACTORY.createSqlType(SqlTypeName.FLOAT)), rexBuilder.makeExactLiteral(new BigDecimal("23.1234567890123456789012345678")), rexBuilder.makeIntervalLiteral(BigDecimal.valueOf(100), new SqlIntervalQualifier(TimeUnit.YEAR, 4, TimeUnit.YEAR, RelDataType.PRECISION_NOT_SPECIFIED, SqlParserPos.ZERO)), rexBuilder.makeIntervalLiteral(BigDecimal.valueOf(3), new SqlIntervalQualifier(TimeUnit.YEAR, 2, TimeUnit.MONTH, RelDataType.PRECISION_NOT_SPECIFIED, SqlParserPos.ZERO)), rexBuilder.makeIntervalLiteral(BigDecimal.valueOf(3), new SqlIntervalQualifier(TimeUnit.DAY, 2, TimeUnit.SECOND, 6, SqlParserPos.ZERO)), rexBuilder.makeIntervalLiteral(BigDecimal.valueOf(3), new SqlIntervalQualifier(TimeUnit.SECOND, 2, TimeUnit.SECOND, 6, SqlParserPos.ZERO)), rexBuilder.makeDateLiteral(DateString.fromDaysSinceEpoch(10)), rexBuilder.makeDateLiteral(new DateString("2000-12-12")), rexBuilder.makeTimeLiteral(TimeString.fromMillisOfDay(1234), 3), rexBuilder.makeTimeLiteral(TimeString.fromMillisOfDay(123456), 6), rexBuilder.makeTimeLiteral(new TimeString("01:01:01.000000001"), 9), rexBuilder.makeTimestampLiteral(TimestampString.fromMillisSinceEpoch(1234), 3), rexBuilder.makeTimestampLiteral(TimestampString.fromMillisSinceEpoch(123456789), 9), rexBuilder.makeTimestampLiteral(new TimestampString("0001-01-01 01:01:01.000000001"), 9), rexBuilder.makeTimestampLiteral(new TimestampString("2000-12-12 12:30:57.1234"), 4), rexBuilder.makeBinaryLiteral(ByteString.EMPTY), rexBuilder.makeBinaryLiteral(ByteString.ofBase64("SGVsbG8gV29ybGQh")), rexBuilder.makeLiteral(""), rexBuilder.makeLiteral("abc"), rexBuilder.makeFlag(SqlTrimFunction.Flag.BOTH), rexBuilder.makeFlag(TimeUnitRange.DAY), rexBuilder.makeSearchArgumentLiteral(Sarg.of(false, ImmutableRangeSet.of(Range.closed(BigDecimal.valueOf(1), BigDecimal.valueOf(10)))), FACTORY.createSqlType(SqlTypeName.INTEGER)), rexBuilder.makeSearchArgumentLiteral(Sarg.of(false, ImmutableRangeSet.of(Range.range(BigDecimal.valueOf(1), BoundType.OPEN, BigDecimal.valueOf(10), BoundType.CLOSED))), FACTORY.createSqlType(SqlTypeName.INTEGER)), rexBuilder.makeSearchArgumentLiteral(Sarg.of(false, TreeRangeSet.create(Arrays.asList(Range.closed(BigDecimal.valueOf(1), BigDecimal.valueOf(1)), Range.closed(BigDecimal.valueOf(3), BigDecimal.valueOf(3)), Range.closed(BigDecimal.valueOf(6), BigDecimal.valueOf(6))))), FACTORY.createSqlType(SqlTypeName.INTEGER)), rexBuilder.makeInputRef(FACTORY.createSqlType(SqlTypeName.BIGINT), 0), rexBuilder.makeCorrel(inputType, new CorrelationId("$cor1")), rexBuilder.makeFieldAccess(rexBuilder.makeCorrel(inputType, new CorrelationId("$cor2")), "f2", true), // cast($1 as smallint)
    rexBuilder.makeCast(FACTORY.createSqlType(SqlTypeName.SMALLINT), rexBuilder.makeInputRef(FACTORY.createSqlType(SqlTypeName.INTEGER), 1)), // $1 in (1, 3, 5)
    rexBuilder.makeIn(rexBuilder.makeInputRef(FACTORY.createSqlType(SqlTypeName.INTEGER), 1), Arrays.asList(rexBuilder.makeExactLiteral(new BigDecimal(1)), rexBuilder.makeExactLiteral(new BigDecimal(3)), rexBuilder.makeExactLiteral(new BigDecimal(5)))), // null or $1 is null
    rexBuilder.makeCall(SqlStdOperatorTable.OR, rexBuilder.makeNullLiteral(FACTORY.createSqlType(SqlTypeName.INTEGER)), rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL, rexBuilder.makeInputRef(FACTORY.createSqlType(SqlTypeName.INTEGER), 1))), // $1 >= 10
    rexBuilder.makeCall(SqlStdOperatorTable.GREATER_THAN_OR_EQUAL, rexBuilder.makeInputRef(FACTORY.createSqlType(SqlTypeName.INTEGER), 1), rexBuilder.makeExactLiteral(new BigDecimal(10))), // hash_code($1)
    rexBuilder.makeCall(FlinkSqlOperatorTable.HASH_CODE, rexBuilder.makeInputRef(FACTORY.createSqlType(SqlTypeName.INTEGER), 1)), rexBuilder.makePatternFieldRef("test", FACTORY.createSqlType(SqlTypeName.INTEGER), 0));
}
Also used : TimeString(org.apache.calcite.util.TimeString) SqlIntervalQualifier(org.apache.calcite.sql.SqlIntervalQualifier) DateString(org.apache.calcite.util.DateString) RexBuilder(org.apache.calcite.rex.RexBuilder) RelDataType(org.apache.calcite.rel.type.RelDataType) TimestampString(org.apache.calcite.util.TimestampString) CorrelationId(org.apache.calcite.rel.core.CorrelationId) BigDecimal(java.math.BigDecimal)

Example 10 with TimestampString

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

the class AbstractSqlCallContext method getLiteralValueAs.

/**
 * Bridges to {@link ValueLiteralExpression#getValueAs(Class)}.
 */
@SuppressWarnings("unchecked")
protected static <T> T getLiteralValueAs(LiteralValueAccessor accessor, Class<T> clazz) {
    Preconditions.checkArgument(!clazz.isPrimitive());
    Object convertedValue = null;
    if (clazz == Duration.class) {
        final long longVal = accessor.getValueAs(Long.class);
        convertedValue = Duration.ofMillis(longVal);
    } else if (clazz == Period.class) {
        final long longVal = accessor.getValueAs(Long.class);
        if (longVal <= Integer.MAX_VALUE && longVal >= Integer.MIN_VALUE) {
            convertedValue = Period.ofMonths((int) longVal);
        }
    } else if (clazz == java.time.LocalDate.class) {
        final DateString dateString = accessor.getValueAs(DateString.class);
        convertedValue = java.time.LocalDate.parse(dateString.toString());
    } else if (clazz == java.time.LocalTime.class) {
        final TimeString timeString = accessor.getValueAs(TimeString.class);
        convertedValue = java.time.LocalTime.parse(timeString.toString());
    } else if (clazz == java.time.LocalDateTime.class) {
        final TimestampString timestampString = accessor.getValueAs(TimestampString.class);
        convertedValue = java.time.LocalDateTime.parse(timestampString.toString().replace(' ', 'T'));
    } else if (clazz == java.time.Instant.class) {
        // timestamp string is in UTC, convert back to an instant
        final TimestampString timestampString = accessor.getValueAs(TimestampString.class);
        convertedValue = java.time.LocalDateTime.parse(timestampString.toString().replace(' ', 'T')).atOffset(ZoneOffset.UTC).toInstant();
    }
    if (convertedValue != null) {
        return (T) convertedValue;
    }
    return accessor.getValueAs(clazz);
}
Also used : TimeString(org.apache.calcite.util.TimeString) DateString(org.apache.calcite.util.DateString) Period(java.time.Period) TimestampString(org.apache.calcite.util.TimestampString)

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