Skip to content

Commit

Permalink
add more complex tests and nested type test (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasmine-ge authored Aug 13, 2024
1 parent 79a39e8 commit 6210ba7
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.timeplus.jdbc.type;

import com.timeplus.jdbc.AbstractITest;
import com.timeplus.misc.BytesHelper;
import org.junit.jupiter.api.Test;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Arrays;
import java.sql.Array;


import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class NestedDataTypeTest extends AbstractITest implements BytesHelper {

// test for nested type, array of array, array of tuple, tuple of array, tuple of tuple, nullable type involved
@Test
public void testNestedType() throws Exception {
withStatement(statement -> {
statement.execute("DROP STREAM IF EXISTS nested_test");
statement.execute(
"CREATE STREAM IF NOT EXISTS nested_test (value uint32, nested_value nested(name string, age int32)) Engine=Memory()");

Integer rowCnt = 300;
try (PreparedStatement pstmt = statement.getConnection().prepareStatement(
"INSERT INTO nested_test (value, nested_value.name, nested_value.age) values(?, ?, ?);")) {
for (int i = 0; i < rowCnt; i++) {
Array innerArray1 = statement.getConnection().createArrayOf("string", new Object[]{"1", "2", "3"});
Array innerArray2 = statement.getConnection().createArrayOf("int32", new Object[]{4, 5, 6});
pstmt.setInt(1, 1);
pstmt.setObject(2, innerArray1);
pstmt.setObject(3, innerArray2);

pstmt.addBatch();
}
pstmt.executeBatch();
}

ResultSet rs = statement.executeQuery("SELECT * FROM nested_test;");
int size = 0;
while (rs.next()) {
size++;
Integer value = rs.getInt(1);
assertEquals(value, 1);

Array array = rs.getArray(2);
Object[] innerArrayValue1 = (Object[]) array.getArray();
assertEquals(innerArrayValue1.length, 3);
assertEquals(innerArrayValue1[0], "1");
assertEquals(innerArrayValue1[1], "2");
assertEquals(innerArrayValue1[2], "3");

Array array2 = rs.getArray(3);
Object[] innerArrayValue2 = (Object[]) array2.getArray();
assertEquals(innerArrayValue2.length, 3);
Integer[] value2 = Arrays.stream(innerArrayValue2).map(Integer.class::cast).toArray(Integer[]::new);
assertTrue(Arrays.equals(value2, new Integer[]{4, 5, 6}));

}
assertEquals(size, rowCnt);
statement.execute("DROP STREAM IF EXISTS nested_test");
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ public void testNestedArrayMapType() throws Exception {
statement.execute("DROP STREAM IF EXISTS nested_test");
statement.execute(
"CREATE STREAM IF NOT EXISTS nested_test (value array(map(int32, low_cardinality(nullable(string)))), "
+"value2 map(string, array(low_cardinality(nullable(int))))) Engine=Memory()");
+"value2 map(string, array(low_cardinality(nullable(int)))), value3 map(string, map(string, low_cardinality(nullable(string))))) Engine=Memory()");

Integer rowCnt = 300;
try (PreparedStatement pstmt = statement.getConnection().prepareStatement(
"INSERT INTO nested_test (value, value2) values(?, ?);")) {
"INSERT INTO nested_test (value, value2, value3) values(?, ?, ?);")) {
for (int i = 0; i < rowCnt; i++) {
// array of map
Map<Integer, String> map1 = new HashMap<>();
Expand All @@ -143,6 +143,14 @@ public void testNestedArrayMapType() throws Exception {
map3.put("array", array);
pstmt.setObject(2, map3);

// map (string, map(string, int))
Map<String, Object> map4 = new HashMap<>();
map4.put("test", null);
map4.put("test2", "test");
Map<String, Object> map5 = new HashMap<>();
map5.put("test", map4);
pstmt.setObject(3, map5);

pstmt.addBatch();
}
pstmt.executeBatch();
Expand Down Expand Up @@ -173,6 +181,12 @@ public void testNestedArrayMapType() throws Exception {
assertEquals(arrayValue3[1], 2);
assertEquals(arrayValue3[2], null);

// map (string, map(string, int))
Map<String, Object> map4 = (Map<String, Object>) rs.getObject(3);
Map<String, Object> map5 = (Map<String, Object>) map4.get("test");
assertEquals(map5.get("test"), null);
assertEquals(map5.get("test2"), "test");

}
assertEquals(size, rowCnt);
statement.execute("DROP STREAM IF EXISTS nested_test");
Expand All @@ -185,11 +199,11 @@ public void testNestedTupleMapType() throws Exception {
statement.execute("DROP STREAM IF EXISTS nested_test");
statement.execute(
"CREATE STREAM IF NOT EXISTS nested_test (value tuple(map(int32, low_cardinality(nullable(string))), low_cardinality(nullable(string))), "
+"value2 map(string, tuple(low_cardinality(nullable(int))))) Engine=Memory()");
+"value2 map(string, tuple(low_cardinality(nullable(int)))), value3 tuple(tuple(low_cardinality(nullable(int))))) Engine=Memory()");

Integer rowCnt = 300;
try (PreparedStatement pstmt = statement.getConnection().prepareStatement(
"INSERT INTO nested_test (value, value2) values(?, ?);")) {
"INSERT INTO nested_test (value, value2, value3) values(?, ?, ?);")) {
for (int i = 0; i < rowCnt; i++) {
// tuple (map, string)
Map<Integer, String> map1 = new HashMap<>();
Expand All @@ -206,6 +220,13 @@ public void testNestedTupleMapType() throws Exception {
map2.put("tuple", tuple2);
pstmt.setObject(2, map2);

// tuple (tuple)
Object[] tupleValue3 = new Object[]{1};
TimeplusStruct tuple3 = new TimeplusStruct("tuple", tupleValue3);
Object[] tupleValue4 = new Object[]{tuple3};
TimeplusStruct tuple4 = new TimeplusStruct("tuple", tupleValue4);
pstmt.setObject(3, tuple4);

pstmt.addBatch();
}
pstmt.executeBatch();
Expand Down Expand Up @@ -233,6 +254,15 @@ public void testNestedTupleMapType() throws Exception {
assertEquals(tupleValue2.length, 1);
assertEquals(tupleValue2[0], 3);

// tuple (tuple)
Object obj3 = rs.getObject(3);
TimeplusStruct tuple3 = (TimeplusStruct) obj3;
Object[] tupleValue3 = (Object[]) tuple3.getAttributes();
TimeplusStruct tuple4 = (TimeplusStruct) tupleValue3[0];
Object[] tupleValue4 = (Object[]) tuple4.getAttributes();
assertEquals(tupleValue4.length, 1);
assertEquals(tupleValue4[0], 1);

}
assertEquals(size, rowCnt);
statement.execute("DROP STREAM IF EXISTS nested_test");
Expand Down

0 comments on commit 6210ba7

Please sign in to comment.