Skip to content

Commit d117502

Browse files
committed
[test](flight-sql) Cover schema discovery through Flight RPC
1 parent e9d908b commit d117502

1 file changed

Lines changed: 139 additions & 0 deletions

File tree

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
import org.apache.arrow.driver.jdbc.shaded.org.apache.arrow.flight.CallOption
19+
import org.apache.arrow.driver.jdbc.shaded.org.apache.arrow.flight.CallOptions
20+
import org.apache.arrow.driver.jdbc.shaded.org.apache.arrow.flight.CloseSessionRequest
21+
import org.apache.arrow.driver.jdbc.shaded.org.apache.arrow.flight.FlightClient
22+
import org.apache.arrow.driver.jdbc.shaded.org.apache.arrow.flight.FlightRuntimeException
23+
import org.apache.arrow.driver.jdbc.shaded.org.apache.arrow.flight.FlightStatusCode
24+
import org.apache.arrow.driver.jdbc.shaded.org.apache.arrow.flight.Location
25+
import org.apache.arrow.driver.jdbc.shaded.org.apache.arrow.flight.sql.FlightSqlClient
26+
import org.apache.arrow.driver.jdbc.shaded.org.apache.arrow.memory.RootAllocator
27+
import org.apache.arrow.driver.jdbc.shaded.org.apache.arrow.vector.types.TimeUnit
28+
import org.apache.arrow.driver.jdbc.shaded.org.apache.arrow.vector.types.pojo.ArrowType
29+
30+
suite("test_schema_only", "arrow_flight_sql") {
31+
def config = context.config.otherConfigs
32+
def location = Location.forGrpcInsecure(config.get("extArrowFlightSqlHost"),
33+
Integer.parseInt(config.get("extArrowFlightSqlPort")))
34+
new RootAllocator().withCloseable { allocator ->
35+
FlightClient.builder(allocator, location).build().withCloseable { flight ->
36+
def token = flight.authenticateBasicToken(config.get("extArrowFlightSqlUser"),
37+
config.get("extArrowFlightSqlPassword")).get()
38+
CallOption[] options = [token, CallOptions.timeout(30, java.util.concurrent.TimeUnit.SECONDS)]
39+
def client = new FlightSqlClient(flight)
40+
String showTimeout = "SHOW VARIABLES LIKE 'query_timeout'"
41+
def readTimeout = {
42+
def info = client.execute(showTimeout, options)
43+
assertEquals(1, info.getEndpoints().size())
44+
def values = []
45+
flight.getStream(info.getEndpoints().get(0).getTicket(), options).withCloseable { stream ->
46+
while (stream.next()) {
47+
def root = stream.getRoot()
48+
for (int i = 0; i < root.getRowCount(); ++i) {
49+
values.add(root.getVector("Value").getObject(i).toString())
50+
}
51+
}
52+
}
53+
assertEquals(1, values.size())
54+
return values[0]
55+
}
56+
def prepareSchema = { String query ->
57+
def prepared = client.prepare(query, options)
58+
try {
59+
// ADBC ExecuteSchema consumes this schema before executing or fetching FlightInfo.
60+
def schema = prepared.getResultSetSchema()
61+
assertTrue(schema.getFields()*.getName() != ["ResultMeta"])
62+
assertEquals(schema, prepared.fetchSchema(options).getSchema())
63+
return schema
64+
} finally {
65+
prepared.close(options)
66+
}
67+
}
68+
try {
69+
def cases = [
70+
["SELECT CAST(1 AS BIGINT) AS id, CAST(NULL AS VARCHAR(20)) AS name",
71+
["id", "name"], [new ArrowType.Int(64, true), new ArrowType.Utf8()]],
72+
["""SELECT CAST('2025-01-02 03:04:05.123456' AS DATETIME(6)) AS event_time,
73+
ARRAY('first', 'second') AS tags,
74+
MAP('key', CAST(1 AS BIGINT)) AS properties,
75+
NAMED_STRUCT('amount', CAST(1 AS DECIMAL(18,4))) AS record""",
76+
["event_time", "tags", "properties", "record"],
77+
[new ArrowType.Timestamp(TimeUnit.MICROSECOND, null), new ArrowType.List(),
78+
new ArrowType.Map(false), new ArrowType.Struct()]],
79+
["SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME FROM information_schema.tables WHERE 1=0",
80+
["TABLE_CATALOG", "TABLE_SCHEMA", "TABLE_NAME"],
81+
[new ArrowType.Utf8(), new ArrowType.Utf8(), new ArrowType.Utf8()]],
82+
[showTimeout, ["Variable_name", "Value", "Default_Value", "Changed"],
83+
[new ArrowType.Utf8(), new ArrowType.Utf8(), new ArrowType.Utf8(), new ArrowType.Utf8()]]
84+
]
85+
cases.eachWithIndex { entry, index ->
86+
def schema = prepareSchema(entry[0])
87+
assertEquals(entry[1], schema.getFields()*.getName())
88+
assertEquals(entry[2], schema.getFields()*.getType())
89+
assertEquals(schema, client.getExecuteSchema(entry[0], options).getSchema())
90+
if (index == 0) {
91+
assertTrue(!schema.getFields()[0].isNullable())
92+
assertTrue(schema.getFields()[1].isNullable())
93+
} else if (index == 1) {
94+
assertEquals(new ArrowType.Utf8(), schema.getFields()[1].getChildren()[0].getType())
95+
def entries = schema.getFields()[2].getChildren()[0]
96+
assertTrue(!entries.isNullable())
97+
assertTrue(!entries.getChildren()[0].isNullable())
98+
assertEquals(new ArrowType.Int(64, true), entries.getChildren()[1].getType())
99+
assertEquals(new ArrowType.Decimal(18, 4, 128),
100+
schema.getFields()[3].getChildren()[0].getType())
101+
}
102+
}
103+
104+
// Resolving command metadata must not execute SET or leak query-scoped SET_VAR hints.
105+
def originalTimeout = readTimeout()
106+
def changedTimeout = originalTimeout == "17" ? "19" : "17"
107+
def setQuery = "SET query_timeout=${changedTimeout}".toString()
108+
assertEquals(["StatusResult"], prepareSchema(setQuery).getFields()*.getName())
109+
client.getExecuteSchema(setQuery, options)
110+
assertEquals(originalTimeout, readTimeout())
111+
def hintedQuery = "SELECT /*+ SET_VAR(query_timeout=${changedTimeout}) */ 1 AS id".toString()
112+
prepareSchema(hintedQuery)
113+
client.getExecuteSchema(hintedQuery, options)
114+
assertEquals(originalTimeout, readTimeout())
115+
116+
["SELEC 1", "SELECT definitely_missing_schema_column"].each { query ->
117+
[true, false].each { preparedRoute ->
118+
FlightRuntimeException failure = null
119+
try {
120+
if (preparedRoute) {
121+
prepareSchema(query)
122+
} else {
123+
client.getExecuteSchema(query, options)
124+
}
125+
} catch (FlightRuntimeException e) {
126+
failure = e
127+
}
128+
assertTrue(failure != null, "Schema discovery accepted invalid SQL: ${query}")
129+
assertEquals(FlightStatusCode.INVALID_ARGUMENT, failure.status().code())
130+
assertEquals(["id", "name"], prepareSchema(cases[0][0]).getFields()*.getName())
131+
assertEquals(originalTimeout, readTimeout())
132+
}
133+
}
134+
} finally {
135+
client.closeSession(new CloseSessionRequest(), options)
136+
}
137+
}
138+
}
139+
}

0 commit comments

Comments
 (0)