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));
}
}
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();
}
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);
}
}
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);
}
Aggregations