|
| 1 | +// |
| 2 | +// Copyright (c) 2016-2024 Deephaven Data Labs and Patent Pending |
| 3 | +// |
| 4 | +package io.deephaven.server.flightsql; |
| 5 | + |
| 6 | +import io.deephaven.server.DeephavenServerTestBase; |
| 7 | +import org.apache.arrow.adbc.core.AdbcConnection; |
| 8 | +import org.apache.arrow.adbc.core.AdbcDatabase; |
| 9 | +import org.apache.arrow.adbc.core.AdbcDriver; |
| 10 | +import org.apache.arrow.adbc.core.AdbcException; |
| 11 | +import org.apache.arrow.adbc.core.AdbcStatement; |
| 12 | +import org.apache.arrow.adbc.driver.flightsql.FlightSqlConnectionProperties; |
| 13 | +import org.apache.arrow.adbc.driver.flightsql.FlightSqlDriverFactory; |
| 14 | +import org.apache.arrow.memory.BufferAllocator; |
| 15 | +import org.apache.arrow.memory.RootAllocator; |
| 16 | +import org.apache.arrow.vector.IntVector; |
| 17 | +import org.apache.arrow.vector.VectorSchemaRoot; |
| 18 | +import org.apache.arrow.vector.ipc.ArrowReader; |
| 19 | +import org.apache.arrow.vector.types.Types; |
| 20 | +import org.apache.arrow.vector.types.pojo.Field; |
| 21 | +import org.apache.arrow.vector.types.pojo.FieldType; |
| 22 | +import org.apache.arrow.vector.types.pojo.Schema; |
| 23 | +import org.junit.jupiter.api.AfterEach; |
| 24 | +import org.junit.jupiter.api.BeforeEach; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | + |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
| 32 | + |
| 33 | +public abstract class FlightSqlAdbcTestBase extends DeephavenServerTestBase { |
| 34 | + |
| 35 | + private static final Map<String, String> DEEPHAVEN_INT = Map.of( |
| 36 | + "deephaven:isSortable", "true", |
| 37 | + "deephaven:isRowStyle", "false", |
| 38 | + "deephaven:isPartitioning", "false", |
| 39 | + "deephaven:type", "int", |
| 40 | + "deephaven:isNumberFormat", "false", |
| 41 | + "deephaven:isStyle", "false", |
| 42 | + "deephaven:isDateFormat", "false"); |
| 43 | + |
| 44 | + BufferAllocator allocator; |
| 45 | + AdbcDatabase database; |
| 46 | + AdbcConnection connection; |
| 47 | + |
| 48 | + @BeforeEach |
| 49 | + void setUp() throws AdbcException { |
| 50 | + final Map<String, Object> options = new HashMap<>(); |
| 51 | + AdbcDriver.PARAM_URI.set(options, String.format("grpc://localhost:%d", localPort)); |
| 52 | + FlightSqlConnectionProperties.WITH_COOKIE_MIDDLEWARE.set(options, true); |
| 53 | + options.put(FlightSqlConnectionProperties.RPC_CALL_HEADER_PREFIX + "Authorization", "Anonymous"); |
| 54 | + options.put(FlightSqlConnectionProperties.RPC_CALL_HEADER_PREFIX + "x-deephaven-auth-cookie-request", "true"); |
| 55 | + allocator = new RootAllocator(); |
| 56 | + database = new FlightSqlDriverFactory().getDriver(allocator).open(options); |
| 57 | + connection = database.connect(); |
| 58 | + } |
| 59 | + |
| 60 | + @AfterEach |
| 61 | + void tearDown() throws Exception { |
| 62 | + connection.close(); |
| 63 | + database.close(); |
| 64 | + allocator.close(); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void executeSchema() throws Exception { |
| 69 | + final Schema expectedSchema = new Schema(List |
| 70 | + .of(new Field("Foo", new FieldType(true, Types.MinorType.INT.getType(), null, DEEPHAVEN_INT), null))); |
| 71 | + try (final AdbcStatement statement = connection.createStatement()) { |
| 72 | + statement.setSqlQuery("SELECT 42 as Foo"); |
| 73 | + assertThat(statement.executeSchema()).isEqualTo(expectedSchema); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void executeQuery() throws Exception { |
| 79 | + final Schema expectedSchema = new Schema(List |
| 80 | + .of(new Field("Foo", new FieldType(true, Types.MinorType.INT.getType(), null, DEEPHAVEN_INT), null))); |
| 81 | + try (final AdbcStatement statement = connection.createStatement()) { |
| 82 | + statement.setSqlQuery("SELECT 42 as Foo"); |
| 83 | + try (final AdbcStatement.QueryResult result = statement.executeQuery()) { |
| 84 | + final ArrowReader reader = result.getReader(); |
| 85 | + assertThat(reader.loadNextBatch()).isTrue(); |
| 86 | + final VectorSchemaRoot root = reader.getVectorSchemaRoot(); |
| 87 | + assertThat(root.getSchema()).isEqualTo(expectedSchema); |
| 88 | + final IntVector vector = (IntVector) root.getVector(0); |
| 89 | + assertThat(vector.isNull(0)).isFalse(); |
| 90 | + assertThat(vector.get(0)).isEqualTo(42); |
| 91 | + assertThat(reader.loadNextBatch()).isFalse(); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void preparedExecuteQuery() throws Exception { |
| 98 | + final Schema expectedSchema = new Schema(List |
| 99 | + .of(new Field("Foo", new FieldType(true, Types.MinorType.INT.getType(), null, DEEPHAVEN_INT), null))); |
| 100 | + try (final AdbcStatement statement = connection.createStatement()) { |
| 101 | + statement.setSqlQuery("SELECT 42 as Foo"); |
| 102 | + statement.prepare(); |
| 103 | + try (final AdbcStatement.QueryResult result = statement.executeQuery()) { |
| 104 | + final ArrowReader reader = result.getReader(); |
| 105 | + assertThat(reader.loadNextBatch()).isTrue(); |
| 106 | + final VectorSchemaRoot root = reader.getVectorSchemaRoot(); |
| 107 | + assertThat(root.getSchema()).isEqualTo(expectedSchema); |
| 108 | + final IntVector vector = (IntVector) root.getVector(0); |
| 109 | + assertThat(vector.isNull(0)).isFalse(); |
| 110 | + assertThat(vector.get(0)).isEqualTo(42); |
| 111 | + assertThat(reader.loadNextBatch()).isFalse(); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments