use of org.apache.calcite.avatica.ColumnMetaData.ScalarType in project calcite-avatica by apache.
the class ArrayTypeTest method longArrays.
@Test
public void longArrays() 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 < 5; i++) {
List<Long> elements = new ArrayList<>();
for (int j = 0; j < 5; j++) {
elements.add(r.nextLong());
}
arrays.add(createArray("BIGINT", component, elements));
}
// Verify read/write
writeAndReadArrays(conn, "long_arrays", "BIGINT", component, arrays, PRIMITIVE_LIST_VALIDATOR);
}
}
use of org.apache.calcite.avatica.ColumnMetaData.ScalarType in project calcite-avatica by apache.
the class ArrayTypeTest method realArrays.
@Test
public void realArrays() throws Exception {
final Random r = new Random();
try (Connection conn = DriverManager.getConnection(url)) {
ScalarType component = ColumnMetaData.scalar(Types.REAL, "REAL", Rep.FLOAT);
List<Array> arrays = new ArrayList<>();
// Construct the data
for (int i = 0; i < 3; i++) {
List<Float> elements = new ArrayList<>();
for (int j = 0; j < 7; j++) {
float element = r.nextFloat();
if (r.nextBoolean()) {
element *= -1;
}
elements.add(element);
}
arrays.add(createArray("REAL", component, elements));
}
writeAndReadArrays(conn, "real_arrays", "REAL", component, arrays, (expected, actual) -> {
// 'Real' maps to 'Float' following the JDBC specs, but hsqldb maps 'Double', 'Real'
// and 'Float' to Java 'double'
double[] expectedArray = Arrays.stream((Object[]) expected.getArray()).mapToDouble(x -> ((Float) x).doubleValue()).toArray();
double[] actualArray = Arrays.stream((Object[]) actual.getArray()).mapToDouble(x -> (double) x).toArray();
assertArrayEquals(expectedArray, actualArray, Double.MIN_VALUE);
});
}
}
use of org.apache.calcite.avatica.ColumnMetaData.ScalarType in project calcite-avatica by apache.
the class ArrayTypeTest method shortArraysWithNull.
@Test
public void shortArraysWithNull() throws Exception {
final Random r = new Random();
try (Connection conn = DriverManager.getConnection(url)) {
ScalarType component = ColumnMetaData.scalar(Types.SMALLINT, "SMALLINT", Rep.SHORT);
List<Array> arrays = new ArrayList<>();
// Construct the data
for (int i = 0; i < 5; i++) {
List<Short> elements = new ArrayList<>();
for (int j = 0; j < 4; j++) {
short value = (short) r.nextInt(Short.MAX_VALUE);
// 50% of the time, negate the value
if (0 == r.nextInt(2)) {
value *= -1;
}
elements.add(value);
}
elements.add(null);
arrays.add(createArray("SMALLINT", component, elements));
}
// Verify read/write
writeAndReadArrays(conn, "short_arrays", "SMALLINT", component, arrays, PRIMITIVE_LIST_VALIDATOR);
}
}
use of org.apache.calcite.avatica.ColumnMetaData.ScalarType in project calcite-avatica by apache.
the class TypedValueTest method testArrays.
@Test
public void testArrays() {
List<Object> serialObj = Arrays.<Object>asList(1, 2, 3, 4);
ArrayImpl.Factory factory = new ArrayFactoryImpl(Unsafe.localCalendar().getTimeZone());
ScalarType scalarType = ColumnMetaData.scalar(Types.INTEGER, "INTEGER", Rep.INTEGER);
Array a1 = factory.createArray(scalarType, serialObj);
TypedValue tv1 = TypedValue.ofJdbc(Rep.ARRAY, a1, Unsafe.localCalendar());
Object jdbcObj = tv1.toJdbc(Unsafe.localCalendar());
assertTrue("The JDBC object is an " + jdbcObj.getClass(), jdbcObj instanceof Array);
Object localObj = tv1.toLocal();
assertTrue("The local object is an " + localObj.getClass(), localObj instanceof List);
Common.TypedValue protoTv1 = tv1.toProto();
assertEquals(serialObj.size(), protoTv1.getArrayValueCount());
TypedValue tv1Copy = TypedValue.fromProto(protoTv1);
Object jdbcObjCopy = tv1Copy.toJdbc(Unsafe.localCalendar());
assertTrue("The JDBC object is an " + jdbcObjCopy.getClass(), jdbcObjCopy instanceof Array);
Object localObjCopy = tv1Copy.toLocal();
assertTrue("The local object is an " + localObjCopy.getClass(), localObjCopy instanceof List);
}
use of org.apache.calcite.avatica.ColumnMetaData.ScalarType in project calcite-avatica by apache.
the class ArrayFactoryImpl method create.
@Override
public ResultSet create(AvaticaType elementType, Iterable<Object> elements) throws SQLException {
// The ColumnMetaData for offset "1" in the ResultSet for an Array.
ScalarType arrayOffsetType = ColumnMetaData.scalar(Types.INTEGER, "INTEGER", Rep.PRIMITIVE_INT);
// Two columns (types) in the ResultSet we will create
List<ColumnMetaData> types = Arrays.asList(ColumnMetaData.dummy(arrayOffsetType, false), ColumnMetaData.dummy(elementType, true));
List<List<Object>> rows = createResultSetRowsForArrayData(elements);
// `(List<Object>) rows` is a compile error.
@SuppressWarnings({ "unchecked", "rawtypes" }) List<Object> untypedRows = (List<Object>) ((List) rows);
try (ListIteratorCursor cursor = new ListIteratorCursor(rows.iterator())) {
final String sql = "MOCKED";
QueryState state = new QueryState(sql);
Meta.Signature signature = new Meta.Signature(types, sql, Collections.<AvaticaParameter>emptyList(), Collections.<String, Object>emptyMap(), Meta.CursorFactory.LIST, Meta.StatementType.SELECT);
AvaticaResultSetMetaData resultSetMetaData = new AvaticaResultSetMetaData(null, sql, signature);
Meta.Frame frame = new Meta.Frame(0, true, untypedRows);
AvaticaResultSet resultSet = new AvaticaResultSet(null, state, signature, resultSetMetaData, timeZone, frame);
resultSet.execute2(cursor, types);
return resultSet;
}
}
Aggregations