use of org.apache.calcite.avatica.ColumnMetaData.ScalarType in project calcite-avatica by apache.
the class ArrayTypeTest method varbinaryArrays.
@Test
public void varbinaryArrays() throws Exception {
try (Connection conn = DriverManager.getConnection(url)) {
ScalarType component = ColumnMetaData.scalar(Types.VARBINARY, "VARBINARY", Rep.BYTE_STRING);
// [ Array(binary, binary, binary), Array(binary, binary, binary), ...]
List<Array> arrays = new ArrayList<>();
// Construct the data
for (int i = 0; i < 5; i++) {
List<byte[]> elements = new ArrayList<>();
for (int j = 0; j < 5; j++) {
elements.add((i + "_" + j).getBytes(UTF_8));
}
arrays.add(createArray("VARBINARY", component, elements));
}
writeAndReadArrays(conn, "binary_arrays", "VARBINARY", component, arrays, BYTE_ARRAY_ARRAY_VALIDATOR);
}
}
use of org.apache.calcite.avatica.ColumnMetaData.ScalarType in project calcite-avatica by apache.
the class ArrayTypeTest method simpleArrayTest.
@Test
public void simpleArrayTest() throws Exception {
try (Connection conn = DriverManager.getConnection(url)) {
ScalarType varcharComponent = ColumnMetaData.scalar(Types.VARCHAR, "VARCHAR", Rep.STRING);
List<Array> varcharArrays = new ArrayList<>();
for (int i = 0; i < 5; i++) {
List<String> value = Collections.singletonList(Integer.toString(i));
varcharArrays.add(createArray("VARCHAR", varcharComponent, value));
}
writeAndReadArrays(conn, "varchar_arrays", "VARCHAR(30)", varcharComponent, varcharArrays, PRIMITIVE_LIST_VALIDATOR);
}
}
use of org.apache.calcite.avatica.ColumnMetaData.ScalarType in project calcite-avatica by apache.
the class ArrayTypeTest method timestampArrays.
@Test
public void timestampArrays() throws Exception {
try (Connection conn = DriverManager.getConnection(url)) {
final long now = System.currentTimeMillis();
ScalarType component = ColumnMetaData.scalar(Types.TIMESTAMP, "TIMESTAMP", Rep.JAVA_SQL_TIMESTAMP);
List<Array> arrays = new ArrayList<>();
// Construct the data
for (int i = 0; i < 5; i++) {
List<Timestamp> elements = new ArrayList<>();
for (int j = 0; j < 5; j++) {
elements.add(new Timestamp(now + i + j));
}
arrays.add(createArray("TIMESTAMP", component, elements));
}
writeAndReadArrays(conn, "timestamp_arrays", "TIMESTAMP", component, arrays, new Validator<Array>() {
@Override
public void validate(Array expected, Array actual) throws SQLException {
Object[] expectedTimestamps = (Object[]) expected.getArray();
Object[] actualTimestamps = (Object[]) actual.getArray();
assertEquals(expectedTimestamps.length, actualTimestamps.length);
final Calendar cal = Unsafe.localCalendar();
for (int i = 0; i < expectedTimestamps.length; i++) {
cal.setTime((Timestamp) expectedTimestamps[i]);
int expectedDayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
int expectedMonth = cal.get(Calendar.MONTH);
int expectedYear = cal.get(Calendar.YEAR);
int expectedHour = cal.get(Calendar.HOUR_OF_DAY);
int expectedMinute = cal.get(Calendar.MINUTE);
int expectedSecond = cal.get(Calendar.SECOND);
int expectedMillisecond = cal.get(Calendar.MILLISECOND);
cal.setTime((Timestamp) actualTimestamps[i]);
assertEquals(expectedDayOfMonth, cal.get(Calendar.DAY_OF_MONTH));
assertEquals(expectedMonth, cal.get(Calendar.MONTH));
assertEquals(expectedYear, cal.get(Calendar.YEAR));
assertEquals(expectedHour, cal.get(Calendar.HOUR_OF_DAY));
assertEquals(expectedMinute, cal.get(Calendar.MINUTE));
assertEquals(expectedSecond, cal.get(Calendar.SECOND));
assertEquals(expectedMillisecond, cal.get(Calendar.MILLISECOND));
}
}
});
// Ensure an array with a null element can be written/read
Array arrayWithNull = createArray("TIMESTAMP", component, Arrays.asList((Timestamp) null));
writeAndReadArrays(conn, "timestamp_array_with_null", "TIMESTAMP", component, Collections.singletonList(arrayWithNull), new Validator<Array>() {
@Override
public void validate(Array expected, Array actual) throws Exception {
Object[] expectedArray = (Object[]) expected.getArray();
Object[] actualArray = (Object[]) actual.getArray();
assertEquals(1, expectedArray.length);
assertEquals(expectedArray.length, actualArray.length);
assertEquals(expectedArray[0], actualArray[0]);
}
});
}
}
use of org.apache.calcite.avatica.ColumnMetaData.ScalarType in project calcite-avatica by apache.
the class ArrayTypeTest method arraysOfByteArrays.
@Test
public void arraysOfByteArrays() throws Exception {
final Random r = new Random();
try (Connection conn = DriverManager.getConnection(url)) {
ScalarType component = ColumnMetaData.scalar(Types.TINYINT, "TINYINT", Rep.BYTE);
// [ Array([b, b, b]), Array([b, b, b]), ... ]
List<Array> arrays = new ArrayList<>();
// Construct the data
for (int i = 0; i < 5; i++) {
List<Byte> elements = new ArrayList<>();
for (int j = 0; j < 5; j++) {
byte value = (byte) r.nextInt(Byte.MAX_VALUE);
// 50% of the time, negate the value
if (0 == r.nextInt(2)) {
value *= -1;
}
elements.add(Byte.valueOf(value));
}
arrays.add(createArray("TINYINT", component, elements));
}
// Verify read/write
writeAndReadArrays(conn, "byte_arrays", "TINYINT", component, arrays, BYTE_ARRAY_VALIDATOR);
}
}
use of org.apache.calcite.avatica.ColumnMetaData.ScalarType in project calcite-avatica by apache.
the class ArrayTypeTest method bigintArrays.
@Test
public void bigintArrays() throws Exception {
final Random r = new Random();
try (Connection conn = DriverManager.getConnection(url)) {
ScalarType component = ColumnMetaData.scalar(Types.BIGINT, "BIGINT", Rep.LONG);
List<Array> arrays = new ArrayList<>();
// Construct the data
for (int i = 0; i < 3; i++) {
List<Long> elements = new ArrayList<>();
for (int j = 0; j < 7; j++) {
long element = r.nextLong();
if (r.nextBoolean()) {
element *= -1;
}
elements.add(element);
}
arrays.add(createArray("BIGINT", component, elements));
}
writeAndReadArrays(conn, "long_arrays", "BIGINT", component, arrays, PRIMITIVE_LIST_VALIDATOR);
}
}
Aggregations