use of com.hazelcast.core.HazelcastJsonValue in project hazelcast by hazelcast.
the class JsonValueFunction method eval.
@Override
public T eval(final Row row, final ExpressionEvalContext context) {
// first evaluate the required parameter
final String path = (String) operands[1].eval(row, context);
if (path == null) {
throw QueryException.error("SQL/JSON path expression cannot be null");
}
// needed for further checks, can be a dynamic expression, therefore can not be inlined as part of function args
final Object defaultOnEmpty = operands[2].eval(row, context);
final Object defaultOnError = operands[3].eval(row, context);
final Object operand0 = operands[0].eval(row, context);
String json = operand0 instanceof HazelcastJsonValue ? operand0.toString() : (String) operand0;
if (json == null) {
json = "";
}
final JsonPath jsonPath;
try {
jsonPath = pathCache.asMap().computeIfAbsent(path, JsonPathUtil::compile);
} catch (JsonPathCompilerException e) {
// We deliberately don't use the cause here. The reason is that exceptions from ANTLR are not always
// serializable, they can contain references to parser context and other objects, which are not.
// That's why we also log the exception here.
LOGGER.fine("JSON_QUERY JsonPath compilation failed", e);
throw QueryException.error("Invalid SQL/JSON path expression: " + e.getMessage());
}
Collection<Object> resultColl;
try {
resultColl = JsonPathUtil.read(json, jsonPath);
} catch (Exception e) {
return onErrorResponse(e, defaultOnError);
}
if (resultColl.isEmpty()) {
return onEmptyResponse(defaultOnEmpty);
}
if (resultColl.size() > 1) {
throw QueryException.error("JSON_VALUE evaluated to multiple values");
}
Object onlyResult = resultColl.iterator().next();
if (JsonPathUtil.isArrayOrObject(onlyResult)) {
return onErrorResponse(QueryException.error("Result of JSON_VALUE cannot be array or object"), defaultOnError);
}
@SuppressWarnings("unchecked") T result = (T) convertResultType(onlyResult);
return result;
}
use of com.hazelcast.core.HazelcastJsonValue in project hazelcast by hazelcast.
the class JsonCreationUtil method serializeValue.
public static String serializeValue(final Object value) {
if (value == null) {
return "null";
}
if (value instanceof HazelcastJsonValue) {
return value.toString();
}
final Converter converter = Converters.getConverter(value.getClass());
final Object result;
switch(converter.getTypeFamily()) {
case TIME:
case DATE:
case TIMESTAMP:
case TIMESTAMP_WITH_TIME_ZONE:
result = converter.asVarchar(value);
break;
default:
result = value;
}
return SERIALIZER.toJson(result);
}
use of com.hazelcast.core.HazelcastJsonValue in project hazelcast by hazelcast.
the class HazelcastJsonUpsertTarget method conclude.
@Override
public Object conclude() {
JsonObject json = this.json;
this.json = null;
return new HazelcastJsonValue(json.toString());
}
use of com.hazelcast.core.HazelcastJsonValue in project hazelcast by hazelcast.
the class SampleMetadataResolverTest method test_json.
@Test
public void test_json() {
InternalSerializationService ss = new DefaultSerializationServiceBuilder().build();
Metadata metadata = SampleMetadataResolver.resolve(ss, new HazelcastJsonValue("{}"), key);
assertThat(metadata).isNull();
metadata = SampleMetadataResolver.resolve(ss, ss.toData(new HazelcastJsonValue("{}")), key);
assertThat(metadata).isNull();
}
use of com.hazelcast.core.HazelcastJsonValue in project hazelcast by hazelcast.
the class CastFunctionIntegrationTest method testVarchar_string.
@Test
public void testVarchar_string() {
// Character
putAndCheckValue('f', sql("this", VARCHAR), VARCHAR, "f");
// String
putAndCheckValue(new ExpressionValue.StringVal(), sql("field1", VARCHAR), VARCHAR, null);
putAndCheckValue("foo", sql("this", VARCHAR), VARCHAR, "foo");
// Boolean
putAndCheckValue(new ExpressionValue.StringVal(), sql("field1", BOOLEAN), BOOLEAN, null);
putAndCheckValue("true", sql("this", BOOLEAN), BOOLEAN, true);
putAndCheckValue("false", sql("this", BOOLEAN), BOOLEAN, false);
putAndCheckFailure("bad", sql("this", BOOLEAN), DATA_EXCEPTION, "Cannot parse VARCHAR value to BOOLEAN");
// Byte
putAndCheckValue(new ExpressionValue.StringVal(), sql("field1", TINYINT), TINYINT, null);
putAndCheckValue(string(0), sql("this", TINYINT), TINYINT, (byte) 0);
putAndCheckValue(string(Byte.MAX_VALUE), sql("this", TINYINT), TINYINT, Byte.MAX_VALUE);
putAndCheckValue(string(Byte.MIN_VALUE), sql("this", TINYINT), TINYINT, Byte.MIN_VALUE);
putAndCheckFailure(string(Short.MAX_VALUE), sql("this", TINYINT), DATA_EXCEPTION, "Cannot parse VARCHAR value to TINYINT");
putAndCheckFailure(string(Short.MIN_VALUE), sql("this", TINYINT), DATA_EXCEPTION, "Cannot parse VARCHAR value to TINYINT");
putAndCheckFailure("bad", sql("this", TINYINT), DATA_EXCEPTION, "Cannot parse VARCHAR value to TINYINT");
// Short
putAndCheckValue(new ExpressionValue.StringVal(), sql("field1", SMALLINT), SMALLINT, null);
putAndCheckValue(string(0), sql("this", SMALLINT), SMALLINT, (short) 0);
putAndCheckValue(string(Short.MAX_VALUE), sql("this", SMALLINT), SMALLINT, Short.MAX_VALUE);
putAndCheckValue(string(Short.MIN_VALUE), sql("this", SMALLINT), SMALLINT, Short.MIN_VALUE);
putAndCheckFailure(string(Integer.MAX_VALUE), sql("this", SMALLINT), DATA_EXCEPTION, "Cannot parse VARCHAR value to SMALLINT");
putAndCheckFailure(string(Integer.MIN_VALUE), sql("this", SMALLINT), DATA_EXCEPTION, "Cannot parse VARCHAR value to SMALLINT");
putAndCheckFailure("bad", sql("this", SMALLINT), DATA_EXCEPTION, "Cannot parse VARCHAR value to SMALLINT");
// Integer
putAndCheckValue(new ExpressionValue.StringVal(), sql("field1", INTEGER), INTEGER, null);
putAndCheckValue(string(0), sql("this", INTEGER), INTEGER, 0);
putAndCheckValue(string(Integer.MAX_VALUE), sql("this", INTEGER), INTEGER, Integer.MAX_VALUE);
putAndCheckValue(string(Integer.MIN_VALUE), sql("this", INTEGER), INTEGER, Integer.MIN_VALUE);
putAndCheckFailure(string(Long.MAX_VALUE), sql("this", INTEGER), DATA_EXCEPTION, "Cannot parse VARCHAR value to INTEGER");
putAndCheckFailure(string(Long.MIN_VALUE), sql("this", INTEGER), DATA_EXCEPTION, "Cannot parse VARCHAR value to INTEGER");
putAndCheckFailure("bad", sql("this", INTEGER), DATA_EXCEPTION, "Cannot parse VARCHAR value to INTEGER");
// Long
putAndCheckValue(new ExpressionValue.StringVal(), sql("field1", BIGINT), BIGINT, null);
putAndCheckValue(string(0), sql("this", BIGINT), BIGINT, 0L);
putAndCheckValue(string(Long.MAX_VALUE), sql("this", BIGINT), BIGINT, Long.MAX_VALUE);
putAndCheckValue(string(Long.MIN_VALUE), sql("this", BIGINT), BIGINT, Long.MIN_VALUE);
putAndCheckFailure(string(decimal(Long.MAX_VALUE).multiply(decimal(2))), sql("this", BIGINT), DATA_EXCEPTION, "Cannot parse VARCHAR value to BIGINT");
putAndCheckFailure(string(decimal(Long.MIN_VALUE).multiply(decimal(2))), sql("this", BIGINT), DATA_EXCEPTION, "Cannot parse VARCHAR value to BIGINT");
putAndCheckFailure("bad", sql("this", BIGINT), DATA_EXCEPTION, "Cannot parse VARCHAR value to BIGINT");
// BigInteger/BigDecimal
putAndCheckValue(new ExpressionValue.StringVal(), sql("field1", DECIMAL), DECIMAL, null);
putAndCheckValue(string(1), sql("this", DECIMAL), DECIMAL, BigDecimal.ONE);
putAndCheckValue(string("1.1"), sql("this", DECIMAL), DECIMAL, decimal("1.1"));
putAndCheckFailure("bad", sql("this", DECIMAL), DATA_EXCEPTION, "Cannot parse VARCHAR value to DECIMAL");
// Float
putAndCheckValue(new ExpressionValue.StringVal(), sql("field1", REAL), REAL, null);
putAndCheckValue(string("1.1"), sql("this", REAL), REAL, 1.1f);
putAndCheckFailure("bad", sql("this", REAL), DATA_EXCEPTION, "Cannot parse VARCHAR value to REAL");
// Double
putAndCheckValue(new ExpressionValue.StringVal(), sql("field1", DOUBLE), DOUBLE, null);
putAndCheckValue(string("1.1"), sql("this", DOUBLE), DOUBLE, 1.1d);
putAndCheckFailure("bad", sql("this", DOUBLE), DATA_EXCEPTION, "Cannot parse VARCHAR value to DOUBLE");
// LocalDate
putAndCheckValue(new ExpressionValue.StringVal(), sql("field1", DATE), DATE, null);
putAndCheckValue(LOCAL_DATE_VAL.toString(), sql("this", DATE), DATE, LOCAL_DATE_VAL);
// SQL standard also accepts DATE without leading zeroes to DATE.
putAndCheckValue(STANDARD_LOCAL_DATE_VAL, sql("this", DATE), DATE, LOCAL_DATE_VAL);
putAndCheckFailure("bad", sql("this", DATE), DATA_EXCEPTION, "Cannot parse VARCHAR value to DATE");
// LocalTime
putAndCheckValue(new ExpressionValue.StringVal(), sql("field1", TIME), TIME, null);
putAndCheckValue(LOCAL_TIME_VAL.toString(), sql("this", TIME), TIME, LOCAL_TIME_VAL);
putAndCheckValue(STANDARD_LOCAL_TIME_VAL, sql("this", TIME), TIME, LOCAL_TIME_VAL);
putAndCheckFailure("bad", sql("this", TIME), DATA_EXCEPTION, "Cannot parse VARCHAR value to TIME");
// LocalDateTime
putAndCheckValue(new ExpressionValue.StringVal(), sql("field1", TIMESTAMP), TIMESTAMP, null);
putAndCheckValue(LOCAL_DATE_TIME_VAL.toString(), sql("this", TIMESTAMP), TIMESTAMP, LOCAL_DATE_TIME_VAL);
putAndCheckValue(STANDARD_LOCAL_DATE_TIME_VAL, sql("this", TIMESTAMP), TIMESTAMP, LOCAL_DATE_TIME_VAL);
putAndCheckFailure("bad", sql("this", TIMESTAMP), DATA_EXCEPTION, "Cannot parse VARCHAR value to TIMESTAMP");
// OffsetDateTime
putAndCheckValue(new ExpressionValue.StringVal(), sql("field1", TIMESTAMP_WITH_TIME_ZONE), TIMESTAMP_WITH_TIME_ZONE, null);
putAndCheckValue(OFFSET_DATE_TIME_VAL.toString(), sql("this", TIMESTAMP_WITH_TIME_ZONE), TIMESTAMP_WITH_TIME_ZONE, OFFSET_DATE_TIME_VAL);
putAndCheckValue(STANDARD_LOCAL_OFFSET_TIME_VAL, sql("this", TIMESTAMP_WITH_TIME_ZONE), TIMESTAMP_WITH_TIME_ZONE, OFFSET_DATE_TIME_VAL);
putAndCheckFailure("bad", sql("this", TIMESTAMP_WITH_TIME_ZONE), DATA_EXCEPTION, "Cannot parse VARCHAR value to TIMESTAMP WITH TIME ZONE");
// Object
putAndCheckValue(new ExpressionValue.StringVal(), sql("field1", OBJECT), OBJECT, null);
putAndCheckValue("foo", sql("this", OBJECT), OBJECT, "foo");
// JSON
putAndCheckValue("[1,2,3]", sql("this", JSON), JSON, new HazelcastJsonValue("[1,2,3]"));
}
Aggregations