Search in sources :

Example 6 with HazelcastJsonValue

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

the class CastFunctionIntegrationTest method testParameter.

@Test
public void testParameter() {
    put(0);
    checkValue0(sql("?", VARCHAR), VARCHAR, "1", 1);
    checkValue0(sql("?", INTEGER), INTEGER, 1, "1");
    checkValue0(sql("?", JSON), JSON, new HazelcastJsonValue("[1]"), "[1]");
}
Also used : HazelcastJsonValue(com.hazelcast.core.HazelcastJsonValue) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 7 with HazelcastJsonValue

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

the class CastFunctionIntegrationTest method testVarchar_literal.

@Test
public void testVarchar_literal() {
    put(1);
    // VARCHAR -> VARCHAR
    checkValue0(sql(stringLiteral("foo"), VARCHAR), VARCHAR, "foo");
    checkValue0(sql(stringLiteral("true"), VARCHAR), VARCHAR, "true");
    checkValue0(sql(stringLiteral("false"), VARCHAR), VARCHAR, "false");
    checkValue0(sql(stringLiteral(LOCAL_DATE_VAL), VARCHAR), VARCHAR, LOCAL_DATE_VAL.toString());
    checkValue0(sql(stringLiteral(LOCAL_TIME_VAL), VARCHAR), VARCHAR, LOCAL_TIME_VAL.toString());
    checkValue0(sql(stringLiteral(LOCAL_DATE_TIME_VAL), VARCHAR), VARCHAR, LOCAL_DATE_TIME_VAL.toString());
    checkValue0(sql(stringLiteral(OFFSET_DATE_TIME_VAL), VARCHAR), VARCHAR, OFFSET_DATE_TIME_VAL.toString());
    // VARCHAR -> BOOLEAN
    checkFailure0(sql(stringLiteral("foo"), BOOLEAN), PARSING, "CAST function cannot convert literal 'foo' to type BOOLEAN: Cannot parse VARCHAR value to BOOLEAN");
    checkFailure0(sql(stringLiteral("null"), BOOLEAN), PARSING, "CAST function cannot convert literal 'null' to type BOOLEAN: Cannot parse VARCHAR value to BOOLEAN");
    checkValue0(sql(stringLiteral("true"), BOOLEAN), BOOLEAN, true);
    checkValue0(sql(stringLiteral("True"), BOOLEAN), BOOLEAN, true);
    checkValue0(sql(stringLiteral("false"), BOOLEAN), BOOLEAN, false);
    checkValue0(sql(stringLiteral("False"), BOOLEAN), BOOLEAN, false);
    // VARCHAR -> TINYINT
    checkValue0(sql(stringLiteral(1), TINYINT), TINYINT, (byte) 1);
    checkValue0(sql(stringLiteral(Byte.MAX_VALUE), TINYINT), TINYINT, Byte.MAX_VALUE);
    checkValue0(sql(stringLiteral(Byte.MIN_VALUE), TINYINT), TINYINT, Byte.MIN_VALUE);
    checkFailure0(sql(stringLiteral(Short.MAX_VALUE), TINYINT), PARSING, "CAST function cannot convert literal '32767' to type TINYINT: Cannot parse VARCHAR value to TINYINT");
    checkFailure0(sql(stringLiteral(Short.MIN_VALUE), TINYINT), PARSING, "CAST function cannot convert literal '-32768' to type TINYINT: Cannot parse VARCHAR value to TINYINT");
    checkFailure0(sql(stringLiteral("foo"), TINYINT), PARSING, "CAST function cannot convert literal 'foo' to type TINYINT: Cannot parse VARCHAR value to TINYINT");
    checkFailure0(sql(stringLiteral("true"), TINYINT), PARSING, "CAST function cannot convert literal 'true' to type TINYINT: Cannot parse VARCHAR value to TINYINT");
    checkFailure0(sql(stringLiteral("false"), TINYINT), PARSING, "CAST function cannot convert literal 'false' to type TINYINT: Cannot parse VARCHAR value to TINYINT");
    checkFailure0(sql(stringLiteral("1.1"), TINYINT), PARSING, "CAST function cannot convert literal '1.1' to type TINYINT: Cannot parse VARCHAR value to TINYINT");
    // VARCHAR -> SMALLINT
    checkValue0(sql(stringLiteral(1), SMALLINT), SMALLINT, (short) 1);
    checkValue0(sql(stringLiteral(Short.MAX_VALUE), SMALLINT), SMALLINT, Short.MAX_VALUE);
    checkValue0(sql(stringLiteral(Short.MIN_VALUE), SMALLINT), SMALLINT, Short.MIN_VALUE);
    checkFailure0(sql(stringLiteral(Integer.MAX_VALUE), SMALLINT), PARSING, "CAST function cannot convert literal '2147483647' to type SMALLINT: Cannot parse VARCHAR value to SMALLINT");
    checkFailure0(sql(stringLiteral(Integer.MIN_VALUE), SMALLINT), PARSING, "CAST function cannot convert literal '-2147483648' to type SMALLINT: Cannot parse VARCHAR value to SMALLINT");
    checkFailure0(sql(stringLiteral("foo"), SMALLINT), PARSING, "CAST function cannot convert literal 'foo' to type SMALLINT: Cannot parse VARCHAR value to SMALLINT");
    checkFailure0(sql(stringLiteral("true"), SMALLINT), PARSING, "CAST function cannot convert literal 'true' to type SMALLINT: Cannot parse VARCHAR value to SMALLINT");
    checkFailure0(sql(stringLiteral("false"), SMALLINT), PARSING, "CAST function cannot convert literal 'false' to type SMALLINT: Cannot parse VARCHAR value to SMALLINT");
    checkFailure0(sql(stringLiteral("1.1"), SMALLINT), PARSING, "CAST function cannot convert literal '1.1' to type SMALLINT: Cannot parse VARCHAR value to SMALLINT");
    // VARCHAR -> INTEGER
    checkValue0(sql(stringLiteral(1), INTEGER), INTEGER, 1);
    checkValue0(sql(stringLiteral(Integer.MAX_VALUE), INTEGER), INTEGER, Integer.MAX_VALUE);
    checkValue0(sql(stringLiteral(Integer.MIN_VALUE), INTEGER), INTEGER, Integer.MIN_VALUE);
    checkFailure0(sql(stringLiteral(Long.MAX_VALUE), INTEGER), PARSING, "CAST function cannot convert literal '9223372036854775807' to type INTEGER: Cannot parse VARCHAR value to INTEGER");
    checkFailure0(sql(stringLiteral(Long.MIN_VALUE), INTEGER), PARSING, "CAST function cannot convert literal '-9223372036854775808' to type INTEGER: Cannot parse VARCHAR value to INTEGER");
    checkFailure0(sql(stringLiteral("foo"), INTEGER), PARSING, "CAST function cannot convert literal 'foo' to type INTEGER: Cannot parse VARCHAR value to INTEGER");
    checkFailure0(sql(stringLiteral("true"), INTEGER), PARSING, "CAST function cannot convert literal 'true' to type INTEGER: Cannot parse VARCHAR value to INTEGER");
    checkFailure0(sql(stringLiteral("false"), INTEGER), PARSING, "CAST function cannot convert literal 'false' to type INTEGER: Cannot parse VARCHAR value to INTEGER");
    checkFailure0(sql(stringLiteral("1.1"), INTEGER), PARSING, "CAST function cannot convert literal '1.1' to type INTEGER: Cannot parse VARCHAR value to INTEGER");
    // VARCHAR -> BIGINT
    checkValue0(sql(stringLiteral(1), BIGINT), BIGINT, 1L);
    checkValue0(sql(stringLiteral(Long.MAX_VALUE), BIGINT), BIGINT, Long.MAX_VALUE);
    checkValue0(sql(stringLiteral(Long.MIN_VALUE), BIGINT), BIGINT, Long.MIN_VALUE);
    checkFailure0(sql(stringLiteral(Long.MAX_VALUE + "0"), BIGINT), PARSING, "CAST function cannot convert literal '92233720368547758070' to type BIGINT: Cannot parse VARCHAR value to BIGINT");
    checkFailure0(sql(stringLiteral(Long.MIN_VALUE + "0"), BIGINT), PARSING, "CAST function cannot convert literal '-92233720368547758080' to type BIGINT: Cannot parse VARCHAR value to BIGINT");
    checkFailure0(sql(stringLiteral("foo"), BIGINT), PARSING, "CAST function cannot convert literal 'foo' to type BIGINT: Cannot parse VARCHAR value to BIGINT");
    checkFailure0(sql(stringLiteral("true"), BIGINT), PARSING, "CAST function cannot convert literal 'true' to type BIGINT: Cannot parse VARCHAR value to BIGINT");
    checkFailure0(sql(stringLiteral("false"), BIGINT), PARSING, "CAST function cannot convert literal 'false' to type BIGINT: Cannot parse VARCHAR value to BIGINT");
    checkFailure0(sql(stringLiteral("1.1"), BIGINT), PARSING, "CAST function cannot convert literal '1.1' to type BIGINT: Cannot parse VARCHAR value to BIGINT");
    // VARCHAR -> DECIMAL
    checkValue0(sql(stringLiteral(1), DECIMAL), DECIMAL, decimal(1));
    checkValue0(sql(stringLiteral("1.1"), DECIMAL), DECIMAL, decimal("1.1"));
    checkValue0(sql(stringLiteral(Long.MAX_VALUE + "0"), DECIMAL), DECIMAL, decimal(Long.MAX_VALUE + "0"));
    checkValue0(sql(stringLiteral(Long.MIN_VALUE + "0"), DECIMAL), DECIMAL, decimal(Long.MIN_VALUE + "0"));
    checkFailure0(sql(stringLiteral("foo"), DECIMAL), PARSING, "CAST function cannot convert literal 'foo' to type DECIMAL: Cannot parse VARCHAR value to DECIMAL");
    checkFailure0(sql(stringLiteral("true"), DECIMAL), PARSING, "CAST function cannot convert literal 'true' to type DECIMAL: Cannot parse VARCHAR value to DECIMAL");
    checkFailure0(sql(stringLiteral("false"), DECIMAL), PARSING, "CAST function cannot convert literal 'false' to type DECIMAL: Cannot parse VARCHAR value to DECIMAL");
    // VARCHAR -> REAL
    checkValue0(sql(stringLiteral(1), REAL), REAL, 1f);
    checkValue0(sql(stringLiteral("1.1"), REAL), REAL, 1.1f);
    checkFailure0(sql(stringLiteral("foo"), REAL), PARSING, "CAST function cannot convert literal 'foo' to type REAL: Cannot parse VARCHAR value to REAL");
    checkFailure0(sql(stringLiteral("true"), REAL), PARSING, "CAST function cannot convert literal 'true' to type REAL: Cannot parse VARCHAR value to REAL");
    checkFailure0(sql(stringLiteral("false"), REAL), PARSING, "CAST function cannot convert literal 'false' to type REAL: Cannot parse VARCHAR value to REAL");
    // VARCHAR -> DOUBLE
    checkValue0(sql(stringLiteral(1), DOUBLE), DOUBLE, 1d);
    checkValue0(sql(stringLiteral("1.1"), DOUBLE), DOUBLE, 1.1d);
    checkFailure0(sql(stringLiteral("foo"), DOUBLE), PARSING, "CAST function cannot convert literal 'foo' to type DOUBLE: Cannot parse VARCHAR value to DOUBLE");
    checkFailure0(sql(stringLiteral("true"), DOUBLE), PARSING, "CAST function cannot convert literal 'true' to type DOUBLE: Cannot parse VARCHAR value to DOUBLE");
    checkFailure0(sql(stringLiteral("false"), DOUBLE), PARSING, "CAST function cannot convert literal 'false' to type DOUBLE: Cannot parse VARCHAR value to DOUBLE");
    // VARCHAR -> DATE
    checkValue0(sql(stringLiteral("2020-1-01"), DATE), DATE, LocalDate.parse("2020-01-01"));
    checkValue0(sql(stringLiteral("2020-9-1"), DATE), DATE, LocalDate.parse("2020-09-01"));
    checkValue0(sql(stringLiteral("2020-01-01"), DATE), DATE, LocalDate.parse("2020-01-01"));
    checkFailure0(sql(stringLiteral("foo"), DATE), PARSING, "CAST function cannot convert literal 'foo' to type DATE: Cannot parse VARCHAR value to DATE");
    // VARCHAR -> TIME
    checkValue0(sql(stringLiteral("00:00"), TIME), TIME, LocalTime.parse("00:00"));
    checkValue0(sql(stringLiteral("0:0"), TIME), TIME, LocalTime.parse("00:00"));
    checkValue0(sql(stringLiteral("9:0"), TIME), TIME, LocalTime.parse("09:00"));
    checkValue0(sql(stringLiteral("00:01"), TIME), TIME, LocalTime.parse("00:01"));
    checkValue0(sql(stringLiteral("00:1"), TIME), TIME, LocalTime.parse("00:01"));
    checkValue0(sql(stringLiteral("02:01"), TIME), TIME, LocalTime.parse("02:01"));
    checkValue0(sql(stringLiteral("00:00:00"), TIME), TIME, LocalTime.parse("00:00:00"));
    checkValue0(sql(stringLiteral("00:00:01"), TIME), TIME, LocalTime.parse("00:00:01"));
    checkValue0(sql(stringLiteral("00:02:01"), TIME), TIME, LocalTime.parse("00:02:01"));
    checkValue0(sql(stringLiteral("03:02:01"), TIME), TIME, LocalTime.parse("03:02:01"));
    checkFailure0(sql(stringLiteral("9"), TIME), PARSING, "CAST function cannot convert literal '9' to type TIME: Cannot parse VARCHAR value to TIME");
    checkFailure0(sql(stringLiteral("00:60"), TIME), PARSING, "CAST function cannot convert literal '00:60' to type TIME: Cannot parse VARCHAR value to TIME");
    checkFailure0(sql(stringLiteral("00:00:60"), TIME), PARSING, "CAST function cannot convert literal '00:00:60' to type TIME: Cannot parse VARCHAR value to TIME");
    checkFailure0(sql(stringLiteral("25:00"), TIME), PARSING, "CAST function cannot convert literal '25:00' to type TIME: Cannot parse VARCHAR value to TIME");
    checkFailure0(sql(stringLiteral("25:00:00"), TIME), PARSING, "CAST function cannot convert literal '25:00:00' to type TIME: Cannot parse VARCHAR value to TIME");
    checkFailure0(sql(stringLiteral("foo"), TIME), PARSING, "CAST function cannot convert literal 'foo' to type TIME: Cannot parse VARCHAR value to TIME");
    // VARCHAR -> TIMESTAMP
    checkValue0(sql(stringLiteral("2020-02-01T00:00:00"), TIMESTAMP), TIMESTAMP, LocalDateTime.parse("2020-02-01T00:00:00"));
    checkValue0(sql(stringLiteral("2020-02-01T00:00:01"), TIMESTAMP), TIMESTAMP, LocalDateTime.parse("2020-02-01T00:00:01"));
    checkValue0(sql(stringLiteral("2020-02-01T00:02:01"), TIMESTAMP), TIMESTAMP, LocalDateTime.parse("2020-02-01T00:02:01"));
    checkValue0(sql(stringLiteral("2020-02-01T03:02:01"), TIMESTAMP), TIMESTAMP, LocalDateTime.parse("2020-02-01T03:02:01"));
    checkFailure0(sql(stringLiteral("foo"), TIMESTAMP), PARSING, "CAST function cannot convert literal 'foo' to type TIMESTAMP: Cannot parse VARCHAR value to TIMESTAMP");
    // VARCHAR -> TIMESTAMP_WITH_TIME_ZONE
    checkValue0(sql(stringLiteral(OFFSET_DATE_TIME_VAL), TIMESTAMP_WITH_TIME_ZONE), TIMESTAMP_WITH_TIME_ZONE, OFFSET_DATE_TIME_VAL);
    checkFailure0(sql(stringLiteral("foo"), TIMESTAMP_WITH_TIME_ZONE), PARSING, "CAST function cannot convert literal 'foo' to type TIMESTAMP WITH TIME ZONE: Cannot parse VARCHAR value to TIMESTAMP WITH TIME ZONE");
    // VARCHAR -> OBJECT
    checkValue0(sql(stringLiteral("foo"), OBJECT), OBJECT, "foo");
    // VARCHAR -> JSON
    checkValue0(sql(stringLiteral("[1,2,3]"), JSON), JSON, new HazelcastJsonValue("[1,2,3]"));
}
Also used : HazelcastJsonValue(com.hazelcast.core.HazelcastJsonValue) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 8 with HazelcastJsonValue

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

the class CastFunctionIntegrationTest method testJson.

@Test
public void testJson() {
    final HazelcastJsonValue value = new HazelcastJsonValue("[1,2,3]");
    putAndCheckValue(value, sql("this", VARCHAR), VARCHAR, "[1,2,3]");
    putAndCheckValue(value, sql("this", OBJECT), OBJECT, value);
    putAndCheckFailure(value, sql("this", TINYINT), PARSING, castError(JSON, TINYINT));
    putAndCheckFailure(value, sql("this", SMALLINT), PARSING, castError(JSON, SMALLINT));
    putAndCheckFailure(value, sql("this", INTEGER), PARSING, castError(JSON, INTEGER));
    putAndCheckFailure(value, sql("this", BIGINT), PARSING, castError(JSON, BIGINT));
    putAndCheckFailure(value, sql("this", DECIMAL), PARSING, castError(JSON, DECIMAL));
    putAndCheckFailure(value, sql("this", REAL), PARSING, castError(JSON, REAL));
    putAndCheckFailure(value, sql("this", DOUBLE), PARSING, castError(JSON, DOUBLE));
    putAndCheckFailure(value, sql("this", DATE), PARSING, castError(JSON, DATE));
    putAndCheckFailure(value, sql("this", TIME), PARSING, castError(JSON, TIME));
    putAndCheckFailure(value, sql("this", TIMESTAMP), PARSING, castError(JSON, TIMESTAMP));
    putAndCheckFailure(value, sql("this", TIMESTAMP_WITH_TIME_ZONE), PARSING, castError(JSON, TIMESTAMP_WITH_TIME_ZONE));
}
Also used : HazelcastJsonValue(com.hazelcast.core.HazelcastJsonValue) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 9 with HazelcastJsonValue

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

the class Extractors method instantiateGetter.

private Getter instantiateGetter(Object targetObject, String attributeName, boolean failOnMissingReflectiveAttribute) {
    String attributeNameWithoutArguments = extractAttributeNameNameWithoutArguments(attributeName);
    ValueExtractor valueExtractor = extractors.get(attributeNameWithoutArguments);
    if (valueExtractor != null) {
        Object arguments = argumentsParser.parse(extractArgumentsFromAttributeName(attributeName));
        return new ExtractorGetter(ss, valueExtractor, arguments);
    } else if (targetObject instanceof Data) {
        return instantiateGetterForData((Data) targetObject);
    } else if (targetObject instanceof HazelcastJsonValue) {
        return JsonGetter.INSTANCE;
    } else if (targetObject instanceof PortableGenericRecord) {
        if (portableGetter == null) {
            // will be initialised a couple of times in the worst case
            portableGetter = new PortableGetter(ss);
        }
        return portableGetter;
    } else if (targetObject instanceof CompactGenericRecord) {
        if (compactGetter == null) {
            // will be initialised a couple of times in the worst case
            compactGetter = new CompactGetter(ss);
        }
        return compactGetter;
    } else {
        return ReflectionHelper.createGetter(targetObject, attributeName, failOnMissingReflectiveAttribute);
    }
}
Also used : PortableGenericRecord(com.hazelcast.internal.serialization.impl.portable.PortableGenericRecord) HazelcastJsonValue(com.hazelcast.core.HazelcastJsonValue) CompactGenericRecord(com.hazelcast.internal.serialization.impl.compact.CompactGenericRecord) Data(com.hazelcast.internal.serialization.Data) ValueExtractor(com.hazelcast.query.extractor.ValueExtractor)

Example 10 with HazelcastJsonValue

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

the class SqlExpandViewTest method when_viewIsExpandedWithJsonFunctions.

@Test
public void when_viewIsExpandedWithJsonFunctions() {
    createMapping("test", "bigint", "json");
    instance().getSql().execute("INSERT INTO test VALUES (1, '[1,2,3]')");
    instance().getSql().execute("INSERT INTO test VALUES (2, '[4,5,6]')");
    instance().getSql().execute("CREATE VIEW v1 AS SELECT JSON_VALUE(this, '$[1]' " + "RETURNING BIGINT NULL ON EMPTY NULL ON ERROR) FROM test");
    instance().getSql().execute("CREATE VIEW v2 AS SELECT JSON_QUERY(this, '$[1]' " + "WITH CONDITIONAL WRAPPER EMPTY OBJECT ON EMPTY EMPTY OBJECT ON ERROR) FROM test");
    assertRowsAnyOrder("SELECT * FROM v1", asList(new Row(2L), new Row(5L)));
    assertRowsAnyOrder("SELECT * FROM v2", asList(new Row(new HazelcastJsonValue("2")), new Row(new HazelcastJsonValue("5"))));
}
Also used : HazelcastJsonValue(com.hazelcast.core.HazelcastJsonValue) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

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