Search in sources :

Example 11 with InternalGenericRecord

use of com.hazelcast.internal.serialization.impl.InternalGenericRecord in project hazelcast by hazelcast.

the class SqlCompactTest method test_allTypes.

@Test
public void test_allTypes() throws IOException {
    String from = randomName();
    TestAllTypesSqlConnector.create(sqlService, from);
    String to = randomName();
    sqlService.execute("CREATE MAPPING " + to + " (" + " id INT EXTERNAL NAME \"__key.id\" " + ", \"character\" VARCHAR " + ", string VARCHAR " + ", \"boolean\" BOOLEAN " + ", byte TINYINT " + ", short SMALLINT " + ", \"int\" INT " + ", long BIGINT " + ", \"float\" REAL " + ", \"double\" DOUBLE " + ", \"decimal\" DECIMAL " + ", \"time\" TIME " + ", \"date\" DATE " + ", \"timestamp\" TIMESTAMP " + ", timestampTz TIMESTAMP WITH TIME ZONE " + " ) TYPE " + IMapSqlConnector.TYPE_NAME + ' ' + "OPTIONS (" + '\'' + OPTION_KEY_FORMAT + "'='" + COMPACT_FORMAT + '\'' + ", '" + OPTION_KEY_COMPACT_TYPE_NAME + "'='" + PERSON_ID_TYPE_NAME + '\'' + ", '" + OPTION_VALUE_FORMAT + "'='" + COMPACT_FORMAT + '\'' + ", '" + OPTION_VALUE_COMPACT_TYPE_NAME + "'='" + ALL_TYPES_TYPE_NAME + '\'' + ")");
    sqlService.execute("SINK INTO " + to + " SELECT 13, 'a', string, \"boolean\", byte, short, \"int\", long, " + "\"float\", \"double\", \"decimal\", \"time\", \"date\", \"timestamp\", timestampTz " + " FROM " + from + " f");
    InternalGenericRecord valueRecord = serializationService.readAsInternalGenericRecord(randomEntryFrom(to).getValue());
    assertThat(valueRecord.getString("string")).isEqualTo("string");
    assertThat(valueRecord.getString("character")).isEqualTo("a");
    assertThat(valueRecord.getNullableBoolean("boolean")).isTrue();
    assertThat(valueRecord.getNullableInt8("byte")).isEqualTo((byte) 127);
    assertThat(valueRecord.getNullableInt16("short")).isEqualTo((short) 32767);
    assertThat(valueRecord.getNullableInt32("int")).isEqualTo(2147483647);
    assertThat(valueRecord.getNullableInt64("long")).isEqualTo(9223372036854775807L);
    assertThat(valueRecord.getNullableFloat32("float")).isEqualTo(1234567890.1F);
    assertThat(valueRecord.getNullableFloat64("double")).isEqualTo(123451234567890.1D);
    assertThat(valueRecord.getDecimal("decimal")).isEqualTo(new BigDecimal("9223372036854775.123"));
    assertThat(valueRecord.getTime("time")).isEqualTo(LocalTime.of(12, 23, 34));
    assertThat(valueRecord.getDate("date")).isEqualTo(LocalDate.of(2020, 4, 15));
    assertThat(valueRecord.getTimestamp("timestamp")).isEqualTo(LocalDateTime.of(2020, 4, 15, 12, 23, 34, 1_000_000));
    assertThat(valueRecord.getTimestampWithTimezone("timestampTz")).isEqualTo(OffsetDateTime.of(2020, 4, 15, 12, 23, 34, 200_000_000, UTC));
    assertRowsAnyOrder("SELECT * FROM " + to, singletonList(new Row(13, "a", "string", true, (byte) 127, (short) 32767, 2147483647, 9223372036854775807L, 1234567890.1F, 123451234567890.1D, new BigDecimal("9223372036854775.123"), LocalTime.of(12, 23, 34), LocalDate.of(2020, 4, 15), LocalDateTime.of(2020, 4, 15, 12, 23, 34, 1_000_000), OffsetDateTime.of(2020, 4, 15, 12, 23, 34, 200_000_000, UTC))));
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) InternalGenericRecord(com.hazelcast.internal.serialization.impl.InternalGenericRecord) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 12 with InternalGenericRecord

use of com.hazelcast.internal.serialization.impl.InternalGenericRecord in project hazelcast by hazelcast.

the class SqlCompactTest method test_fieldsMapping.

@Test
public void test_fieldsMapping() throws IOException {
    String name = randomName();
    sqlService.execute("CREATE MAPPING " + name + " (" + "key_id INT EXTERNAL NAME \"__key.id\"" + ", value_id INT EXTERNAL NAME \"this.id\"" + ") TYPE " + IMapSqlConnector.TYPE_NAME + ' ' + "OPTIONS (" + '\'' + OPTION_KEY_FORMAT + "'='" + COMPACT_FORMAT + '\'' + ", '" + OPTION_KEY_COMPACT_TYPE_NAME + "'='" + PERSON_ID_TYPE_NAME + '\'' + ", '" + OPTION_VALUE_FORMAT + "'='" + COMPACT_FORMAT + '\'' + ", '" + OPTION_VALUE_COMPACT_TYPE_NAME + "'='" + PERSON_TYPE_NAME + '\'' + ")");
    sqlService.execute("SINK INTO " + name + " (value_id, key_id) VALUES (2, 1)");
    Entry<Data, Data> entry = randomEntryFrom(name);
    InternalGenericRecord keyRecord = serializationService.readAsInternalGenericRecord(entry.getKey());
    assertThat(keyRecord.getNullableInt32("id")).isEqualTo(1);
    InternalGenericRecord valueRecord = serializationService.readAsInternalGenericRecord(entry.getValue());
    assertThat(valueRecord.getNullableInt32("id")).isEqualTo(2);
    assertRowsAnyOrder("SELECT key_id, value_id FROM " + name, singletonList(new Row(1, 2)));
}
Also used : Data(com.hazelcast.internal.serialization.Data) SqlRow(com.hazelcast.sql.SqlRow) InternalGenericRecord(com.hazelcast.internal.serialization.impl.InternalGenericRecord) Test(org.junit.Test)

Example 13 with InternalGenericRecord

use of com.hazelcast.internal.serialization.impl.InternalGenericRecord in project hazelcast by hazelcast.

the class PortableUpsertTargetTest method test_set.

@Test
public void test_set() throws IOException {
    ClassDefinition innerClassDefinition = new ClassDefinitionBuilder(4, 5, 6).build();
    ClassDefinition classDefinition = new ClassDefinitionBuilder(1, 2, 3).addPortableField("null", innerClassDefinition).addPortableField("object", innerClassDefinition).addStringField("string").addCharField("character").addBooleanField("boolean").addByteField("byte").addShortField("short").addIntField("int").addLongField("long").addFloatField("float").addDoubleField("double").addDecimalField("decimal").addTimeField("time").addDateField("date").addTimestampField("timestamp").addTimestampWithTimezoneField("timestampTz").build();
    UpsertTarget target = new PortableUpsertTarget(classDefinition);
    UpsertInjector nullFieldInjector = target.createInjector("null", QueryDataType.OBJECT);
    UpsertInjector objectFieldInjector = target.createInjector("object", QueryDataType.OBJECT);
    UpsertInjector stringFieldInjector = target.createInjector("string", QueryDataType.VARCHAR);
    UpsertInjector characterFieldInjector = target.createInjector("character", QueryDataType.VARCHAR_CHARACTER);
    UpsertInjector booleanFieldInjector = target.createInjector("boolean", QueryDataType.BOOLEAN);
    UpsertInjector byteFieldInjector = target.createInjector("byte", QueryDataType.TINYINT);
    UpsertInjector shortFieldInjector = target.createInjector("short", QueryDataType.SMALLINT);
    UpsertInjector intFieldInjector = target.createInjector("int", QueryDataType.INT);
    UpsertInjector longFieldInjector = target.createInjector("long", QueryDataType.BIGINT);
    UpsertInjector floatFieldInjector = target.createInjector("float", QueryDataType.REAL);
    UpsertInjector doubleFieldInjector = target.createInjector("double", QueryDataType.DOUBLE);
    UpsertInjector decimalFieldInjector = target.createInjector("decimal", QueryDataType.DECIMAL);
    UpsertInjector timeFieldInjector = target.createInjector("time", QueryDataType.TIME);
    UpsertInjector dateFieldInjector = target.createInjector("date", QueryDataType.DATE);
    UpsertInjector timestampFieldInjector = target.createInjector("timestamp", QueryDataType.TIMESTAMP);
    UpsertInjector timestampTzFieldInjector = target.createInjector("timestampTz", QueryDataType.TIMESTAMP_WITH_TZ_OFFSET_DATE_TIME);
    target.init();
    nullFieldInjector.set(null);
    objectFieldInjector.set(new PortableGenericRecordBuilder(innerClassDefinition).build());
    stringFieldInjector.set("1");
    characterFieldInjector.set('2');
    booleanFieldInjector.set(true);
    byteFieldInjector.set((byte) 3);
    shortFieldInjector.set((short) 4);
    intFieldInjector.set(5);
    longFieldInjector.set(6L);
    floatFieldInjector.set(7.1F);
    doubleFieldInjector.set(7.2D);
    decimalFieldInjector.set(new BigDecimal("8.1"));
    timeFieldInjector.set(LocalTime.of(12, 23, 34));
    dateFieldInjector.set(LocalDate.of(2021, 2, 9));
    timestampFieldInjector.set(LocalDateTime.of(2021, 2, 9, 12, 23, 34, 1_000_000));
    timestampTzFieldInjector.set(OffsetDateTime.of(2021, 2, 9, 12, 23, 34, 200_000_000, UTC));
    Object portable = target.conclude();
    InternalSerializationService ss = new DefaultSerializationServiceBuilder().build();
    InternalGenericRecord record = ss.readAsInternalGenericRecord(ss.toData(portable));
    assertThat(record.getGenericRecord("null")).isNull();
    assertThat(record.getGenericRecord("object")).isEqualTo(new PortableGenericRecordBuilder(innerClassDefinition).build());
    assertThat(record.getString("string")).isEqualTo("1");
    assertThat(record.getChar("character")).isEqualTo('2');
    assertThat(record.getBoolean("boolean")).isEqualTo(true);
    assertThat(record.getInt8("byte")).isEqualTo((byte) 3);
    assertThat(record.getInt16("short")).isEqualTo((short) 4);
    assertThat(record.getInt32("int")).isEqualTo(5);
    assertThat(record.getInt64("long")).isEqualTo(6L);
    assertThat(record.getFloat32("float")).isEqualTo(7.1F);
    assertThat(record.getFloat64("double")).isEqualTo(7.2D);
    assertThat(record.getDecimal("decimal")).isEqualTo(new BigDecimal("8.1"));
    assertThat(record.getTime("time")).isEqualTo(LocalTime.of(12, 23, 34));
    assertThat(record.getDate("date")).isEqualTo(LocalDate.of(2021, 2, 9));
    assertThat(record.getTimestamp("timestamp")).isEqualTo(LocalDateTime.of(2021, 2, 9, 12, 23, 34, 1_000_000));
    assertThat(record.getTimestampWithTimezone("timestampTz")).isEqualTo(OffsetDateTime.of(2021, 2, 9, 12, 23, 34, 200_000_000, UTC));
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) PortableGenericRecordBuilder(com.hazelcast.internal.serialization.impl.portable.PortableGenericRecordBuilder) BigDecimal(java.math.BigDecimal) InternalGenericRecord(com.hazelcast.internal.serialization.impl.InternalGenericRecord) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder) Test(org.junit.Test)

Example 14 with InternalGenericRecord

use of com.hazelcast.internal.serialization.impl.InternalGenericRecord in project hazelcast by hazelcast.

the class PortableUpsertTargetTest method when_injectNonExistingPropertyNullValue_then_succeeds.

@Test
public void when_injectNonExistingPropertyNullValue_then_succeeds() throws IOException {
    ClassDefinition classDefinition = new ClassDefinitionBuilder(1, 2, 3).build();
    UpsertTarget target = new PortableUpsertTarget(classDefinition);
    UpsertInjector injector = target.createInjector("field", QueryDataType.INT);
    target.init();
    injector.set(null);
    Object portable = target.conclude();
    InternalSerializationService ss = new DefaultSerializationServiceBuilder().build();
    InternalGenericRecord record = ss.readAsInternalGenericRecord(ss.toData(portable));
    assertThat(record).isNotNull();
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) InternalGenericRecord(com.hazelcast.internal.serialization.impl.InternalGenericRecord) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder) Test(org.junit.Test)

Example 15 with InternalGenericRecord

use of com.hazelcast.internal.serialization.impl.InternalGenericRecord in project hazelcast by hazelcast.

the class PortableUpsertTargetTest method when_doesNotInjectIntoPrimitive_then_insertsDefaultValue.

@Test
@Parameters(method = "primitiveClassDefinitions")
@SuppressWarnings("unused")
public void when_doesNotInjectIntoPrimitive_then_insertsDefaultValue(ClassDefinition classDefinition, QueryDataType type, Object defaultValue, Function<InternalGenericRecord, Object> valueExtractor) throws IOException {
    UpsertTarget target = new PortableUpsertTarget(classDefinition);
    target.init();
    Object portable = target.conclude();
    InternalSerializationService ss = new DefaultSerializationServiceBuilder().build();
    InternalGenericRecord record = ss.readAsInternalGenericRecord(ss.toData(portable));
    assertThat(valueExtractor.apply(record)).isEqualTo(defaultValue);
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) InternalGenericRecord(com.hazelcast.internal.serialization.impl.InternalGenericRecord) Parameters(junitparams.Parameters) Test(org.junit.Test)

Aggregations

InternalGenericRecord (com.hazelcast.internal.serialization.impl.InternalGenericRecord)18 Test (org.junit.Test)15 Data (com.hazelcast.internal.serialization.Data)10 SqlRow (com.hazelcast.sql.SqlRow)8 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)7 DefaultSerializationServiceBuilder (com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder)6 BigDecimal (java.math.BigDecimal)4 GenericRecordQueryReader (com.hazelcast.internal.serialization.impl.GenericRecordQueryReader)3 Parameters (junitparams.Parameters)3 CompactTestUtil.createCompactGenericRecord (com.hazelcast.internal.serialization.impl.compact.CompactTestUtil.createCompactGenericRecord)2 ClassDefinition (com.hazelcast.nio.serialization.ClassDefinition)2 ClassDefinitionBuilder (com.hazelcast.nio.serialization.ClassDefinitionBuilder)2 GenericRecord (com.hazelcast.nio.serialization.GenericRecord)2 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)2 QuickTest (com.hazelcast.test.annotation.QuickTest)2 CompactSerializationConfig (com.hazelcast.config.CompactSerializationConfig)1 SerializationConfig (com.hazelcast.config.SerializationConfig)1 CompactInternalGenericRecord (com.hazelcast.internal.serialization.impl.compact.CompactInternalGenericRecord)1 CompactTestUtil.createMainDTO (com.hazelcast.internal.serialization.impl.compact.CompactTestUtil.createMainDTO)1 PortableGenericRecord (com.hazelcast.internal.serialization.impl.portable.PortableGenericRecord)1