Search in sources :

Example 96 with HazelcastJsonValue

use of com.hazelcast.core.HazelcastJsonValue in project hazelcast by hazelcast.

the class CastFunctionIntegrationTest method testVarchar_char.

@Test
public void testVarchar_char() {
    putAndCheckValue(new ExpressionValue.CharacterVal(), sql("field1", VARCHAR), VARCHAR, null);
    putAndCheckValue('b', sql("this", VARCHAR), VARCHAR, "b");
    putAndCheckFailure('b', sql("this", BOOLEAN), DATA_EXCEPTION, "Cannot parse VARCHAR value to BOOLEAN");
    putAndCheckFailure('b', sql("this", TINYINT), DATA_EXCEPTION, "Cannot parse VARCHAR value to TINYINT");
    putAndCheckFailure('b', sql("this", SMALLINT), DATA_EXCEPTION, "Cannot parse VARCHAR value to SMALLINT");
    putAndCheckFailure('b', sql("this", INTEGER), DATA_EXCEPTION, "Cannot parse VARCHAR value to INTEGER");
    putAndCheckFailure('b', sql("this", BIGINT), DATA_EXCEPTION, "Cannot parse VARCHAR value to BIGINT");
    putAndCheckFailure('b', sql("this", DECIMAL), DATA_EXCEPTION, "Cannot parse VARCHAR value to DECIMAL");
    putAndCheckFailure('b', sql("this", REAL), DATA_EXCEPTION, "Cannot parse VARCHAR value to REAL");
    putAndCheckFailure('b', sql("this", DOUBLE), DATA_EXCEPTION, "Cannot parse VARCHAR value to DOUBLE");
    putAndCheckFailure('b', sql("this", DATE), DATA_EXCEPTION, "Cannot parse VARCHAR value to DATE");
    putAndCheckFailure('b', sql("this", TIME), DATA_EXCEPTION, "Cannot parse VARCHAR value to TIME");
    putAndCheckFailure('b', sql("this", TIMESTAMP), DATA_EXCEPTION, "Cannot parse VARCHAR value to TIMESTAMP");
    putAndCheckFailure('b', sql("this", TIMESTAMP_WITH_TIME_ZONE), DATA_EXCEPTION, "Cannot parse VARCHAR value to TIMESTAMP WITH TIME ZONE");
    putAndCheckValue('b', sql("this", OBJECT), OBJECT, "b");
    putAndCheckValue('b', sql("this", JSON), JSON, new HazelcastJsonValue("b"));
}
Also used : ExpressionValue(com.hazelcast.jet.sql.impl.support.expressions.ExpressionValue) HazelcastJsonValue(com.hazelcast.core.HazelcastJsonValue) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 97 with HazelcastJsonValue

use of com.hazelcast.core.HazelcastJsonValue in project hazelcast by hazelcast.

the class JsonValueFunctionIntegrationTest method initMultiTypeObject.

private void initMultiTypeObject() {
    final MultiTypeObject value = new MultiTypeObject();
    final String serializedValue;
    try {
        serializedValue = SERIALIZER.writeValueAsString(value);
    } catch (JsonProcessingException e) {
        throw new HazelcastException(e);
    }
    final IMap<Long, HazelcastJsonValue> test = instance().getMap("test");
    test.put(1L, new HazelcastJsonValue(serializedValue));
}
Also used : HazelcastException(com.hazelcast.core.HazelcastException) HazelcastJsonValue(com.hazelcast.core.HazelcastJsonValue) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 98 with HazelcastJsonValue

use of com.hazelcast.core.HazelcastJsonValue in project hazelcast by hazelcast.

the class JsonQueryFunction method eval.

@Override
public HazelcastJsonValue 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");
    }
    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());
    }
    return wrap(execute(json, jsonPath));
}
Also used : JsonPathCompilerException(org.jsfr.json.exception.JsonPathCompilerException) HazelcastJsonValue(com.hazelcast.core.HazelcastJsonValue) JsonPath(org.jsfr.json.path.JsonPath)

Example 99 with HazelcastJsonValue

use of com.hazelcast.core.HazelcastJsonValue in project hazelcast by hazelcast.

the class SampleMetadataResolver method resolve.

@Nullable
@SuppressWarnings("checkstyle:returncount")
static Metadata resolve(InternalSerializationService ss, Object target, boolean key) {
    try {
        if (target instanceof Data) {
            Data data = (Data) target;
            if (data.isPortable()) {
                ClassDefinition classDefinition = ss.getPortableContext().lookupClassDefinition(data);
                return resolvePortable(classDefinition, key);
            } else if (data.isCompact()) {
                return resolveCompact(ss.extractSchemaFromData(data), key);
            } else if (data.isJson()) {
                return null;
            } else {
                return resolveJava(ss.toObject(data).getClass(), key);
            }
        } else if (target instanceof VersionedPortable) {
            VersionedPortable portable = (VersionedPortable) target;
            ClassDefinition classDefinition = ss.getPortableContext().lookupClassDefinition(portable.getFactoryId(), portable.getClassId(), portable.getClassVersion());
            return resolvePortable(classDefinition, key);
        } else if (target instanceof Portable) {
            Portable portable = (Portable) target;
            ClassDefinition classDefinition = ss.getPortableContext().lookupClassDefinition(portable.getFactoryId(), portable.getClassId(), 0);
            return resolvePortable(classDefinition, key);
        } else if (target instanceof PortableGenericRecord) {
            return resolvePortable(((PortableGenericRecord) target).getClassDefinition(), key);
        } else if (target instanceof CompactGenericRecord) {
            return resolveCompact(((CompactGenericRecord) target).getSchema(), key);
        } else if (ss.isCompactSerializable(target)) {
            Schema schema = ss.extractSchemaFromObject(target);
            return resolveCompact(schema, key);
        } else if (target instanceof HazelcastJsonValue) {
            return null;
        } else {
            return resolveJava(target.getClass(), key);
        }
    } catch (Exception e) {
        return null;
    }
}
Also used : VersionedPortable(com.hazelcast.nio.serialization.VersionedPortable) Portable(com.hazelcast.nio.serialization.Portable) PortableGenericRecord(com.hazelcast.internal.serialization.impl.portable.PortableGenericRecord) Schema(com.hazelcast.internal.serialization.impl.compact.Schema) HazelcastJsonValue(com.hazelcast.core.HazelcastJsonValue) CompactGenericRecord(com.hazelcast.internal.serialization.impl.compact.CompactGenericRecord) Data(com.hazelcast.internal.serialization.Data) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) VersionedPortable(com.hazelcast.nio.serialization.VersionedPortable) Nullable(javax.annotation.Nullable)

Aggregations

HazelcastJsonValue (com.hazelcast.core.HazelcastJsonValue)99 Test (org.junit.Test)80 QuickTest (com.hazelcast.test.annotation.QuickTest)74 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)67 JsonObject (com.hazelcast.internal.json.JsonObject)25 JsonValue (com.hazelcast.internal.json.JsonValue)19 NavigableJsonInputAdapter (com.hazelcast.internal.serialization.impl.NavigableJsonInputAdapter)12 JsonArray (com.hazelcast.internal.json.JsonArray)9 IMap (com.hazelcast.map.IMap)9 Map (java.util.Map)7 HazelcastInstance (com.hazelcast.core.HazelcastInstance)6 Data (com.hazelcast.internal.serialization.Data)5 JsonSchemaNode (com.hazelcast.json.internal.JsonSchemaNode)3 ConnectionResponse (com.hazelcast.internal.ascii.HTTPCommunicator.ConnectionResponse)2 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)2 DefaultSerializationServiceBuilder (com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder)2 CompactGenericRecord (com.hazelcast.internal.serialization.impl.compact.CompactGenericRecord)2 PortableGenericRecord (com.hazelcast.internal.serialization.impl.portable.PortableGenericRecord)2 StringUtil.bytesToString (com.hazelcast.internal.util.StringUtil.bytesToString)2 ExpressionValue (com.hazelcast.jet.sql.impl.support.expressions.ExpressionValue)2