|
| 1 | +#!/usr/bin/env python |
| 2 | +import trino |
| 3 | +import argparse |
| 4 | +import sys |
| 5 | +import re |
| 6 | + |
| 7 | +if not sys.warnoptions: |
| 8 | + import warnings |
| 9 | +warnings.simplefilter("ignore") |
| 10 | + |
| 11 | + |
| 12 | +def get_connection(username, password, namespace): |
| 13 | + host = ( |
| 14 | + "trino-coordinator-default-0.trino-coordinator-default." |
| 15 | + + namespace |
| 16 | + + ".svc.cluster.local" |
| 17 | + ) |
| 18 | + # If you want to debug this locally use |
| 19 | + # kubectl -n kuttl-test-XXX port-forward svc/trino-coordinator-default 8443 |
| 20 | + # host = '127.0.0.1' |
| 21 | + |
| 22 | + conn = trino.dbapi.connect( |
| 23 | + host=host, |
| 24 | + port=8443, |
| 25 | + user=username, |
| 26 | + http_scheme="https", |
| 27 | + auth=trino.auth.BasicAuthentication(username, password), |
| 28 | + session_properties={"query_max_execution_time": "60s"}, |
| 29 | + ) |
| 30 | + conn._http_session.verify = False |
| 31 | + return conn |
| 32 | + |
| 33 | + |
| 34 | +def run_query(connection, query): |
| 35 | + print(f"[DEBUG] Executing query {query}") |
| 36 | + cursor = connection.cursor() |
| 37 | + cursor.execute(query) |
| 38 | + return cursor.fetchall() |
| 39 | + |
| 40 | + |
| 41 | +if __name__ == "__main__": |
| 42 | + # Construct an argument parser |
| 43 | + all_args = argparse.ArgumentParser() |
| 44 | + # Add arguments to the parser |
| 45 | + all_args.add_argument( |
| 46 | + "-n", "--namespace", required=True, help="Namespace the test is running in" |
| 47 | + ) |
| 48 | + |
| 49 | + args = vars(all_args.parse_args()) |
| 50 | + namespace = args["namespace"] |
| 51 | + |
| 52 | + print("Starting S3 tests...") |
| 53 | + connection = get_connection("admin", "admin", namespace) |
| 54 | + |
| 55 | + trino_version = run_query( |
| 56 | + connection, |
| 57 | + "select node_version from system.runtime.nodes where coordinator = true and state = 'active'", |
| 58 | + )[0][0] |
| 59 | + print(f'[INFO] Testing against Trino version "{trino_version}"') |
| 60 | + |
| 61 | + # Strip SDP release suffix from the version string |
| 62 | + trino_product_version = re.split(r"-stackable", trino_version, maxsplit=1)[0] |
| 63 | + |
| 64 | + assert len(trino_product_version) >= 3 |
| 65 | + assert trino_product_version.isnumeric() |
| 66 | + assert trino_version == run_query(connection, "select version()")[0][0] |
| 67 | + |
| 68 | + run_query( |
| 69 | + connection, |
| 70 | + "CREATE SCHEMA IF NOT EXISTS hive.minio WITH (location = 's3a://trino/')", |
| 71 | + ) |
| 72 | + |
| 73 | + run_query(connection, "DROP TABLE IF EXISTS hive.minio.taxi_data") |
| 74 | + run_query(connection, "DROP TABLE IF EXISTS hive.minio.taxi_data_copy") |
| 75 | + run_query(connection, "DROP TABLE IF EXISTS hive.minio.taxi_data_transformed") |
| 76 | + run_query(connection, "DROP TABLE IF EXISTS hive.hdfs.taxi_data_copy") |
| 77 | + run_query(connection, "DROP TABLE IF EXISTS iceberg.minio.taxi_data_copy_iceberg") |
| 78 | + |
| 79 | + run_query( |
| 80 | + connection, |
| 81 | + """ |
| 82 | +CREATE TABLE IF NOT EXISTS hive.minio.taxi_data ( |
| 83 | + vendor_id VARCHAR, |
| 84 | + tpep_pickup_datetime VARCHAR, |
| 85 | + tpep_dropoff_datetime VARCHAR, |
| 86 | + passenger_count VARCHAR, |
| 87 | + trip_distance VARCHAR, |
| 88 | + ratecode_id VARCHAR |
| 89 | +) WITH ( |
| 90 | + external_location = 's3a://trino/taxi-data/', |
| 91 | + format = 'csv', |
| 92 | + skip_header_line_count = 1 |
| 93 | +) |
| 94 | + """, |
| 95 | + ) |
| 96 | + assert ( |
| 97 | + run_query(connection, "SELECT COUNT(*) FROM hive.minio.taxi_data")[0][0] == 5000 |
| 98 | + ) |
| 99 | + rows_written = run_query( |
| 100 | + connection, |
| 101 | + "CREATE TABLE IF NOT EXISTS hive.minio.taxi_data_copy AS SELECT * FROM hive.minio.taxi_data", |
| 102 | + )[0][0] |
| 103 | + assert rows_written == 5000 or rows_written == 0 |
| 104 | + assert ( |
| 105 | + run_query(connection, "SELECT COUNT(*) FROM hive.minio.taxi_data_copy")[0][0] |
| 106 | + == 5000 |
| 107 | + ) |
| 108 | + |
| 109 | + rows_written = run_query( |
| 110 | + connection, |
| 111 | + """ |
| 112 | +CREATE TABLE IF NOT EXISTS hive.minio.taxi_data_transformed AS |
| 113 | +SELECT |
| 114 | + CAST(vendor_id as BIGINT) as vendor_id, |
| 115 | + tpep_pickup_datetime, |
| 116 | + tpep_dropoff_datetime, |
| 117 | + CAST(passenger_count as BIGINT) as passenger_count, |
| 118 | + CAST(trip_distance as DOUBLE) as trip_distance, |
| 119 | + CAST(ratecode_id as BIGINT) as ratecode_id |
| 120 | +FROM hive.minio.taxi_data |
| 121 | +""", |
| 122 | + )[0][0] |
| 123 | + assert rows_written == 5000 or rows_written == 0 |
| 124 | + assert ( |
| 125 | + run_query(connection, "SELECT COUNT(*) FROM hive.minio.taxi_data_transformed")[ |
| 126 | + 0 |
| 127 | + ][0] |
| 128 | + == 5000 |
| 129 | + ) |
| 130 | + |
| 131 | + print("[INFO] Testing HDFS") |
| 132 | + |
| 133 | + run_query( |
| 134 | + connection, |
| 135 | + "CREATE SCHEMA IF NOT EXISTS hive.hdfs WITH (location = 'hdfs://hdfs/trino/')", |
| 136 | + ) |
| 137 | + rows_written = run_query( |
| 138 | + connection, |
| 139 | + "CREATE TABLE IF NOT EXISTS hive.hdfs.taxi_data_copy AS SELECT * FROM hive.minio.taxi_data", |
| 140 | + )[0][0] |
| 141 | + assert rows_written == 5000 or rows_written == 0 |
| 142 | + assert ( |
| 143 | + run_query(connection, "SELECT COUNT(*) FROM hive.hdfs.taxi_data_copy")[0][0] |
| 144 | + == 5000 |
| 145 | + ) |
| 146 | + |
| 147 | + print("[INFO] Testing Iceberg") |
| 148 | + run_query( |
| 149 | + connection, "DROP TABLE IF EXISTS iceberg.minio.taxi_data_copy_iceberg" |
| 150 | + ) # Clean up table to don't fail an second run |
| 151 | + assert ( |
| 152 | + run_query( |
| 153 | + connection, |
| 154 | + """ |
| 155 | +CREATE TABLE IF NOT EXISTS iceberg.minio.taxi_data_copy_iceberg |
| 156 | +WITH (partitioning = ARRAY['vendor_id', 'passenger_count'], format = 'parquet') |
| 157 | +AS SELECT * FROM hive.minio.taxi_data |
| 158 | +""", |
| 159 | + )[0][0] |
| 160 | + == 5000 |
| 161 | + ) |
| 162 | + # Check current count |
| 163 | + assert ( |
| 164 | + run_query( |
| 165 | + connection, "SELECT COUNT(*) FROM iceberg.minio.taxi_data_copy_iceberg" |
| 166 | + )[0][0] |
| 167 | + == 5000 |
| 168 | + ) |
| 169 | + assert ( |
| 170 | + run_query( |
| 171 | + connection, |
| 172 | + 'SELECT COUNT(*) FROM iceberg.minio."taxi_data_copy_iceberg$snapshots"', |
| 173 | + )[0][0] |
| 174 | + == 1 |
| 175 | + ) |
| 176 | + assert ( |
| 177 | + run_query( |
| 178 | + connection, |
| 179 | + 'SELECT COUNT(*) FROM iceberg.minio."taxi_data_copy_iceberg$partitions"', |
| 180 | + )[0][0] |
| 181 | + == 12 |
| 182 | + ) |
| 183 | + assert ( |
| 184 | + run_query( |
| 185 | + connection, |
| 186 | + 'SELECT COUNT(*) FROM iceberg.minio."taxi_data_copy_iceberg$files"', |
| 187 | + )[0][0] |
| 188 | + == 12 |
| 189 | + ) |
| 190 | + |
| 191 | + assert ( |
| 192 | + run_query( |
| 193 | + connection, |
| 194 | + "INSERT INTO iceberg.minio.taxi_data_copy_iceberg SELECT * FROM hive.minio.taxi_data", |
| 195 | + )[0][0] |
| 196 | + == 5000 |
| 197 | + ) |
| 198 | + |
| 199 | + # Check current count |
| 200 | + assert ( |
| 201 | + run_query( |
| 202 | + connection, "SELECT COUNT(*) FROM iceberg.minio.taxi_data_copy_iceberg" |
| 203 | + )[0][0] |
| 204 | + == 10000 |
| 205 | + ) |
| 206 | + assert ( |
| 207 | + run_query( |
| 208 | + connection, |
| 209 | + 'SELECT COUNT(*) FROM iceberg.minio."taxi_data_copy_iceberg$snapshots"', |
| 210 | + )[0][0] |
| 211 | + == 2 |
| 212 | + ) |
| 213 | + assert ( |
| 214 | + run_query( |
| 215 | + connection, |
| 216 | + 'SELECT COUNT(*) FROM iceberg.minio."taxi_data_copy_iceberg$partitions"', |
| 217 | + )[0][0] |
| 218 | + == 12 |
| 219 | + ) |
| 220 | + assert ( |
| 221 | + run_query( |
| 222 | + connection, |
| 223 | + 'SELECT COUNT(*) FROM iceberg.minio."taxi_data_copy_iceberg$files"', |
| 224 | + )[0][0] |
| 225 | + == 24 |
| 226 | + ) |
| 227 | + |
| 228 | + if trino_version == "377": |
| 229 | + # io.trino.spi.TrinoException: This connector [iceberg] does not support versioned tables |
| 230 | + print( |
| 231 | + "[INFO] Skipping the Iceberg tests reading versioned tables for trino version 377 as it does not support versioned tables" |
| 232 | + ) |
| 233 | + else: |
| 234 | + # Check count for first snapshot |
| 235 | + first_snapshot = run_query( |
| 236 | + connection, |
| 237 | + 'select snapshot_id from iceberg.minio."taxi_data_copy_iceberg$snapshots" order by committed_at limit 1', |
| 238 | + )[0][0] |
| 239 | + assert ( |
| 240 | + run_query( |
| 241 | + connection, |
| 242 | + f"SELECT COUNT(*) FROM iceberg.minio.taxi_data_copy_iceberg FOR VERSION AS OF {first_snapshot}", |
| 243 | + )[0][0] |
| 244 | + == 5000 |
| 245 | + ) |
| 246 | + |
| 247 | + # Compact files |
| 248 | + run_query( |
| 249 | + connection, "ALTER TABLE iceberg.minio.taxi_data_copy_iceberg EXECUTE optimize" |
| 250 | + ) |
| 251 | + |
| 252 | + # Check current count |
| 253 | + assert ( |
| 254 | + run_query( |
| 255 | + connection, "SELECT COUNT(*) FROM iceberg.minio.taxi_data_copy_iceberg" |
| 256 | + )[0][0] |
| 257 | + == 10000 |
| 258 | + ) |
| 259 | + assert ( |
| 260 | + run_query( |
| 261 | + connection, |
| 262 | + 'SELECT COUNT(*) FROM iceberg.minio."taxi_data_copy_iceberg$snapshots"', |
| 263 | + )[0][0] |
| 264 | + == 3 |
| 265 | + ) |
| 266 | + assert ( |
| 267 | + run_query( |
| 268 | + connection, |
| 269 | + 'SELECT COUNT(*) FROM iceberg.minio."taxi_data_copy_iceberg$partitions"', |
| 270 | + )[0][0] |
| 271 | + == 12 |
| 272 | + ) |
| 273 | + assert ( |
| 274 | + run_query( |
| 275 | + connection, |
| 276 | + 'SELECT COUNT(*) FROM iceberg.minio."taxi_data_copy_iceberg$files"', |
| 277 | + )[0][0] |
| 278 | + == 12 |
| 279 | + ) # Compaction yeah :) |
| 280 | + |
| 281 | + # Test could be improved by also testing update and deletes |
| 282 | + |
| 283 | + # Test postgres connection |
| 284 | + run_query(connection, "SHOW SCHEMAS IN postgresgeneric") |
| 285 | + run_query(connection, "CREATE SCHEMA IF NOT EXISTS postgresgeneric.tpch") |
| 286 | + run_query( |
| 287 | + connection, |
| 288 | + "CREATE TABLE IF NOT EXISTS postgresgeneric.tpch.nation AS SELECT * FROM tpch.tiny.nation", |
| 289 | + ) |
| 290 | + assert ( |
| 291 | + run_query(connection, "SELECT COUNT(*) FROM postgresgeneric.tpch.nation")[0][0] |
| 292 | + == 25 |
| 293 | + ) |
| 294 | + |
| 295 | + print("[SUCCESS] All tests in check-s3.py succeeded!") |
0 commit comments