1616// under the License.
1717
1818suite(" test_flight_utf8_validation" , " arrow_flight_sql" ) {
19- sql " DROP TABLE IF EXISTS flight_utf8_input"
20- sql """ CREATE TABLE flight_utf8_input (id INT, encoded STRING)
19+ // Reuse Flight credentials and avoid the TLS-dependent early return in connect().
20+ def flight = context. getArrowFlightSqlConnection()
21+ def input = " ${ context.dbName} .flight_utf8_input"
22+ def expectInvalidUtf8 = { String query ->
23+ try {
24+ arrow_flight_sql(query)
25+ assertTrue (false , " Expected an invalid UTF-8 Flight result error" )
26+ } catch (Exception error) {
27+ assertTrue (error. toString(). contains(" Invalid UTF8" ), error. toString())
28+ }
29+ }
30+ arrow_flight_sql " DROP TABLE IF EXISTS ${ input} "
31+ arrow_flight_sql """ CREATE TABLE ${ input} (id INT, encoded STRING)
2132 DUPLICATE KEY(id) DISTRIBUTED BY HASH(id) BUCKETS 1
2233 PROPERTIES ("replication_num" = "1")"""
23- sql """ INSERT INTO flight_utf8_input VALUES
24- (1, '616263'), (2, 'E4B8ADF09F9880'), (3, ''), (4, NULL),
25- (5, '84'), (6, 'C0AF'), (7, 'EDA080'), (8, 'F4908080'),
26- (9, 'C2'), (10, 'A2')"""
27-
28- // TestAction uses the current JDBC connection, so explicitly bind it to Flight.
29- def flightUrl = context. getArrowFlightSqlConnection(). getMetaData(). getURL()
30- connect(context. config. jdbcUser, context. config. jdbcPassword, flightUrl) {
31- def input = " ${ context.dbName} .flight_utf8_input"
34+ try {
35+ arrow_flight_sql """ INSERT INTO ${ input} VALUES
36+ (1, '616263'), (2, 'E4B8ADF09F9880'), (3, ''), (4, NULL),
37+ (5, '84'), (6, 'C0AF'), (7, 'EDA080'), (8, 'F4908080'),
38+ (9, 'C2'), (10, 'A2')"""
3239 for (def id : [5 , 6 , 7 , 8 ]) {
33- test {
34- sql " SELECT unhex(encoded) AS payload FROM ${ input} WHERE id = ${ id} "
35- exception " Invalid UTF8"
36- }
37- }
38- test {
39- sql " SELECT unhex('84') AS payload"
40- exception " Invalid UTF8"
40+ expectInvalidUtf8(" SELECT unhex(encoded) AS payload FROM ${ input} WHERE id = ${ id} " )
4141 }
42+ // Constant folding can replace invalid bytes with valid text before Flight conversion.
43+ // Keep a table scan and skip folding so the constant is evaluated and validated on the BE.
44+ expectInvalidUtf8(""" SELECT /*+ SET_VAR(debug_skip_fold_constant=true) */
45+ unhex('84') AS payload FROM ${ input} WHERE id = 1""" )
4246 // These bytes are valid only when concatenated; each row must be valid independently.
43- test {
44- sql " SELECT unhex(encoded) AS payload FROM ${ input} WHERE id IN (9, 10) ORDER BY id"
45- exception " Invalid UTF8"
46- }
47+ expectInvalidUtf8(" SELECT unhex(encoded) AS payload FROM ${ input} WHERE id IN (9, 10) ORDER BY id" )
4748 for (def expression : [" array(unhex(encoded))" ,
4849 " named_struct('child', unhex(encoded))" ,
4950 " map('key', unhex(encoded))" ,
5051 " map(unhex(encoded), 'value')" ]) {
51- test {
52- sql " SELECT ${ expression} AS nested FROM ${ input} WHERE id = 5"
53- exception " Invalid UTF8"
54- }
52+ expectInvalidUtf8(" SELECT ${ expression} AS nested FROM ${ input} WHERE id = 5" )
5553 }
5654 // Reaching an invalid value after valid rows must fail rather than publish malformed text.
57- test {
58- sql " SELECT unhex(encoded) AS payload FROM ${ input} ORDER BY id"
59- exception " Invalid UTF8"
60- }
61- test {
62- sql " SELECT unhex(encoded) FROM ${ input} WHERE id <= 4 ORDER BY id"
63- result [[' abc' ], [' 中😀' ], [' ' ], [null ]]
64- }
55+ expectInvalidUtf8(" SELECT unhex(encoded) AS payload FROM ${ input} ORDER BY id" )
56+ assertEquals ([[' abc' ], [' 中😀' ], [' ' ], [null ]],
57+ arrow_flight_sql(" SELECT unhex(encoded) FROM ${ input} WHERE id <= 4 ORDER BY id" ))
6558 // Binary payloads may contain arbitrary bytes and must keep their binary Arrow type.
66- context . getConnection() . createStatement(). withCloseable { statement ->
59+ flight . createStatement(). withCloseable { statement ->
6760 statement. executeQuery(" SELECT to_binary(encoded) AS payload FROM ${ input} WHERE id = 5" )
6861 .withCloseable { rows ->
6962 assertTrue (rows. next())
@@ -72,5 +65,7 @@ suite("test_flight_utf8_validation", "arrow_flight_sql") {
7265 assertFalse (rows. next())
7366 }
7467 }
68+ } finally {
69+ arrow_flight_sql " DROP TABLE IF EXISTS ${ input} "
7570 }
7671}
0 commit comments