Search in sources :

Example 1 with SerializableSymbol

use of org.apache.flink.table.planner.typeutils.SymbolUtil.SerializableSymbol in project flink by apache.

the class RexNodeJsonSerializer method serializeLiteralValue.

private static void serializeLiteralValue(Comparable<?> value, SqlTypeName literalTypeName, SqlTypeName elementTypeName, JsonGenerator gen, SerializerProvider serializerProvider) throws IOException {
    if (value == null) {
        gen.writeNullField(FIELD_NAME_VALUE);
        return;
    }
    switch(literalTypeName) {
        case BOOLEAN:
            gen.writeBooleanField(FIELD_NAME_VALUE, (Boolean) value);
            break;
        case TINYINT:
            gen.writeNumberField(FIELD_NAME_VALUE, ((BigDecimal) value).byteValue());
            break;
        case SMALLINT:
            gen.writeNumberField(FIELD_NAME_VALUE, ((BigDecimal) value).shortValue());
            break;
        case INTEGER:
            gen.writeNumberField(FIELD_NAME_VALUE, ((BigDecimal) value).intValue());
            break;
        case BIGINT:
            gen.writeNumberField(FIELD_NAME_VALUE, ((BigDecimal) value).longValue());
            break;
        case FLOAT:
            gen.writeNumberField(FIELD_NAME_VALUE, ((BigDecimal) value).floatValue());
            break;
        case DOUBLE:
            gen.writeNumberField(FIELD_NAME_VALUE, ((BigDecimal) value).doubleValue());
            break;
        case DECIMAL:
        case INTERVAL_YEAR:
        case INTERVAL_YEAR_MONTH:
        case INTERVAL_MONTH:
        case INTERVAL_DAY:
        case INTERVAL_DAY_HOUR:
        case INTERVAL_DAY_MINUTE:
        case INTERVAL_DAY_SECOND:
        case INTERVAL_HOUR:
        case INTERVAL_HOUR_MINUTE:
        case INTERVAL_HOUR_SECOND:
        case INTERVAL_MINUTE:
        case INTERVAL_MINUTE_SECOND:
        case INTERVAL_SECOND:
        case DATE:
        case TIME:
        case TIMESTAMP:
        case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
            gen.writeStringField(FIELD_NAME_VALUE, value.toString());
            break;
        case BINARY:
        case VARBINARY:
            gen.writeStringField(FIELD_NAME_VALUE, ((ByteString) value).toBase64String());
            break;
        case CHAR:
        case VARCHAR:
            gen.writeStringField(FIELD_NAME_VALUE, ((NlsString) value).getValue());
            break;
        case SYMBOL:
            final SerializableSymbol symbol = calciteToSerializable((Enum<?>) value);
            gen.writeStringField(FIELD_NAME_SYMBOL, symbol.getKind());
            gen.writeStringField(FIELD_NAME_VALUE, symbol.getValue());
            break;
        case SARG:
            serializeSargValue((Sarg<?>) value, elementTypeName, gen, serializerProvider);
            break;
        default:
            throw new TableException(String.format("Literal type '%s' is not supported for serializing value '%s'.", literalTypeName, value));
    }
}
Also used : SerializableSymbol(org.apache.flink.table.planner.typeutils.SymbolUtil.SerializableSymbol) TableException(org.apache.flink.table.api.TableException)

Example 2 with SerializableSymbol

use of org.apache.flink.table.planner.typeutils.SymbolUtil.SerializableSymbol in project flink by apache.

the class RexNodeJsonSerializer method serializeSargValue.

@SuppressWarnings("UnstableApiUsage")
private static void serializeSargValue(Sarg<?> value, SqlTypeName sqlTypeName, JsonGenerator gen, SerializerProvider serializerProvider) throws IOException {
    gen.writeFieldName(FIELD_NAME_SARG);
    gen.writeStartObject();
    gen.writeFieldName(FIELD_NAME_RANGES);
    gen.writeStartArray();
    for (Range<?> range : value.rangeSet.asRanges()) {
        gen.writeStartObject();
        if (range.hasLowerBound()) {
            gen.writeFieldName(FIELD_NAME_BOUND_LOWER);
            gen.writeStartObject();
            serializeLiteralValue(range.lowerEndpoint(), sqlTypeName, sqlTypeName, gen, serializerProvider);
            final SerializableSymbol symbol = calciteToSerializable(range.lowerBoundType());
            gen.writeStringField(FIELD_NAME_BOUND_TYPE, symbol.getValue());
            gen.writeEndObject();
        }
        if (range.hasUpperBound()) {
            gen.writeFieldName(FIELD_NAME_BOUND_UPPER);
            gen.writeStartObject();
            serializeLiteralValue(range.upperEndpoint(), sqlTypeName, sqlTypeName, gen, serializerProvider);
            final SerializableSymbol symbol = calciteToSerializable(range.upperBoundType());
            gen.writeStringField(FIELD_NAME_BOUND_TYPE, symbol.getValue());
            gen.writeEndObject();
        }
        gen.writeEndObject();
    }
    gen.writeEndArray();
    gen.writeBooleanField(FIELD_NAME_CONTAINS_NULL, value.containsNull);
    gen.writeEndObject();
}
Also used : SerializableSymbol(org.apache.flink.table.planner.typeutils.SymbolUtil.SerializableSymbol)

Example 3 with SerializableSymbol

use of org.apache.flink.table.planner.typeutils.SymbolUtil.SerializableSymbol in project flink by apache.

the class RexNodeJsonDeserializer method deserializeLiteralValue.

@Nullable
private static Object deserializeLiteralValue(JsonNode literalNode, SqlTypeName sqlTypeName, SerdeContext serdeContext) {
    final JsonNode valueNode = literalNode.required(FIELD_NAME_VALUE);
    if (valueNode.isNull()) {
        return null;
    }
    switch(sqlTypeName) {
        case BOOLEAN:
            return valueNode.booleanValue();
        case TINYINT:
        case SMALLINT:
        case INTEGER:
        case BIGINT:
        case FLOAT:
        case DOUBLE:
        case DECIMAL:
        case INTERVAL_YEAR:
        case INTERVAL_YEAR_MONTH:
        case INTERVAL_MONTH:
        case INTERVAL_DAY:
        case INTERVAL_DAY_HOUR:
        case INTERVAL_DAY_MINUTE:
        case INTERVAL_DAY_SECOND:
        case INTERVAL_HOUR:
        case INTERVAL_HOUR_MINUTE:
        case INTERVAL_HOUR_SECOND:
        case INTERVAL_MINUTE:
        case INTERVAL_MINUTE_SECOND:
        case INTERVAL_SECOND:
            return new BigDecimal(valueNode.asText());
        case DATE:
            return new DateString(valueNode.asText());
        case TIME:
            return new TimeString(valueNode.asText());
        case TIMESTAMP:
        case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
            return new TimestampString(valueNode.asText());
        case BINARY:
        case VARBINARY:
            return ByteString.ofBase64(valueNode.asText());
        case CHAR:
        case VARCHAR:
            return serdeContext.getRexBuilder().makeLiteral(valueNode.asText()).getValue();
        case SYMBOL:
            final JsonNode symbolNode = literalNode.required(FIELD_NAME_SYMBOL);
            final SerializableSymbol symbol = SerializableSymbol.of(symbolNode.asText(), valueNode.asText());
            return serializableToCalcite(symbol);
        default:
            throw new TableException("Unknown literal: " + valueNode);
    }
}
Also used : SerializableSymbol(org.apache.flink.table.planner.typeutils.SymbolUtil.SerializableSymbol) TableException(org.apache.flink.table.api.TableException) TimeString(org.apache.calcite.util.TimeString) DateString(org.apache.calcite.util.DateString) JsonNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode) TimestampString(org.apache.calcite.util.TimestampString) BigDecimal(java.math.BigDecimal) Nullable(javax.annotation.Nullable)

Example 4 with SerializableSymbol

use of org.apache.flink.table.planner.typeutils.SymbolUtil.SerializableSymbol in project flink by apache.

the class SymbolUtilTest method testCalciteToSerializable.

@Test
public void testCalciteToSerializable() {
    final SerializableSymbol trimString = SerializableSymbol.of("TRIM", "LEADING");
    assertThat(calciteToSerializable(SqlTrimFunction.Flag.LEADING)).isEqualTo(trimString);
    assertThat(SymbolUtil.serializableToCalcite(SqlTrimFunction.Flag.class, "LEADING")).isEqualTo(SqlTrimFunction.Flag.LEADING);
    final SerializableSymbol emptyOrErrorString = SerializableSymbol.of("JSON_QUERY_ON_EMPTY_OR_ERROR", "EMPTY_OBJECT");
    assertThat(calciteToSerializable(SqlJsonQueryEmptyOrErrorBehavior.EMPTY_OBJECT)).isEqualTo(emptyOrErrorString);
    assertThat(serializableToCalcite(emptyOrErrorString)).isEqualTo(SqlJsonQueryEmptyOrErrorBehavior.EMPTY_OBJECT);
}
Also used : SerializableSymbol(org.apache.flink.table.planner.typeutils.SymbolUtil.SerializableSymbol) SqlTrimFunction(org.apache.calcite.sql.fun.SqlTrimFunction) Test(org.junit.Test)

Aggregations

SerializableSymbol (org.apache.flink.table.planner.typeutils.SymbolUtil.SerializableSymbol)4 TableException (org.apache.flink.table.api.TableException)2 BigDecimal (java.math.BigDecimal)1 Nullable (javax.annotation.Nullable)1 SqlTrimFunction (org.apache.calcite.sql.fun.SqlTrimFunction)1 DateString (org.apache.calcite.util.DateString)1 TimeString (org.apache.calcite.util.TimeString)1 TimestampString (org.apache.calcite.util.TimestampString)1 JsonNode (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode)1 Test (org.junit.Test)1