Search in sources :

Example 21 with ScalarType

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);
    }
}
Also used : Array(java.sql.Array) Random(java.util.Random) Connection(java.sql.Connection) ScalarType(org.apache.calcite.avatica.ColumnMetaData.ScalarType) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 22 with ScalarType

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);
        });
    }
}
Also used : ArrayType(org.apache.calcite.avatica.ColumnMetaData.ArrayType) ArrayAccessor(org.apache.calcite.avatica.util.AbstractCursor.ArrayAccessor) Arrays(java.util.Arrays) ListIteratorCursor(org.apache.calcite.avatica.util.ListIteratorCursor) Connection(java.sql.Connection) Time(java.sql.Time) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) Random(java.util.Random) Rep(org.apache.calcite.avatica.ColumnMetaData.Rep) Accessor(org.apache.calcite.avatica.util.Cursor.Accessor) Array(java.sql.Array) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) Calendar(java.util.Calendar) Unsafe(org.apache.calcite.avatica.util.Unsafe) ResultSet(java.sql.ResultSet) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) Assert.fail(org.junit.Assert.fail) Parameterized(org.junit.runners.Parameterized) AfterClass(org.junit.AfterClass) Iterator(java.util.Iterator) SqlType(org.apache.calcite.avatica.SqlType) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData) Assert.assertNotNull(org.junit.Assert.assertNotNull) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Timestamp(java.sql.Timestamp) HttpServer(org.apache.calcite.avatica.server.HttpServer) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) PreparedStatement(java.sql.PreparedStatement) Date(java.sql.Date) ScalarType(org.apache.calcite.avatica.ColumnMetaData.ScalarType) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Statement(java.sql.Statement) Serialization(org.apache.calcite.avatica.remote.Driver.Serialization) ArrayImpl(org.apache.calcite.avatica.util.ArrayImpl) AvaticaUtils(org.apache.calcite.avatica.AvaticaUtils) Collections(java.util.Collections) AvaticaType(org.apache.calcite.avatica.ColumnMetaData.AvaticaType) DriverManager(java.sql.DriverManager) Assert.assertEquals(org.junit.Assert.assertEquals) Types(java.sql.Types) Connection(java.sql.Connection) ScalarType(org.apache.calcite.avatica.ColumnMetaData.ScalarType) ArrayList(java.util.ArrayList) Array(java.sql.Array) Random(java.util.Random) Test(org.junit.Test)

Example 23 with ScalarType

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);
    }
}
Also used : Array(java.sql.Array) Random(java.util.Random) Connection(java.sql.Connection) ScalarType(org.apache.calcite.avatica.ColumnMetaData.ScalarType) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 24 with ScalarType

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);
}
Also used : Array(java.sql.Array) ArrayFactoryImpl(org.apache.calcite.avatica.util.ArrayFactoryImpl) ArrayImpl(org.apache.calcite.avatica.util.ArrayImpl) ScalarType(org.apache.calcite.avatica.ColumnMetaData.ScalarType) List(java.util.List) Common(org.apache.calcite.avatica.proto.Common) Test(org.junit.Test)

Example 25 with ScalarType

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;
    }
}
Also used : Meta(org.apache.calcite.avatica.Meta) ScalarType(org.apache.calcite.avatica.ColumnMetaData.ScalarType) QueryState(org.apache.calcite.avatica.QueryState) AvaticaResultSet(org.apache.calcite.avatica.AvaticaResultSet) AvaticaResultSetMetaData(org.apache.calcite.avatica.AvaticaResultSetMetaData) ArrayList(java.util.ArrayList) List(java.util.List) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData)

Aggregations

ScalarType (org.apache.calcite.avatica.ColumnMetaData.ScalarType)25 Test (org.junit.Test)23 Array (java.sql.Array)19 ArrayList (java.util.ArrayList)18 Connection (java.sql.Connection)16 List (java.util.List)9 Random (java.util.Random)9 ColumnMetaData (org.apache.calcite.avatica.ColumnMetaData)8 Accessor (org.apache.calcite.avatica.util.Cursor.Accessor)5 Calendar (java.util.Calendar)4 ResultSet (java.sql.ResultSet)3 Time (java.sql.Time)3 Date (java.sql.Date)2 PreparedStatement (java.sql.PreparedStatement)2 Statement (java.sql.Statement)2 Timestamp (java.sql.Timestamp)2 ArrayType (org.apache.calcite.avatica.ColumnMetaData.ArrayType)2 Meta (org.apache.calcite.avatica.Meta)2 ArrayImpl (org.apache.calcite.avatica.util.ArrayImpl)2 IOException (java.io.IOException)1