forked from apache/paimon-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_handler_tests.rs
More file actions
531 lines (457 loc) · 15.3 KB
/
sql_handler_tests.rs
File metadata and controls
531 lines (457 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//! SQL handler integration tests for paimon-datafusion.
use std::sync::Arc;
use datafusion::catalog::CatalogProvider;
use datafusion::prelude::SessionContext;
use paimon::catalog::Identifier;
use paimon::spec::{ArrayType, BlobType, DataType, IntType, MapType, VarCharType};
use paimon::{Catalog, CatalogOptions, FileSystemCatalog, Options};
use paimon_datafusion::{PaimonCatalogProvider, PaimonRelationPlanner, PaimonSqlHandler};
use tempfile::TempDir;
fn create_test_env() -> (TempDir, Arc<FileSystemCatalog>) {
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let warehouse = format!("file://{}", temp_dir.path().display());
let mut options = Options::new();
options.set(CatalogOptions::WAREHOUSE, warehouse);
let catalog = FileSystemCatalog::new(options).expect("Failed to create catalog");
(temp_dir, Arc::new(catalog))
}
fn create_handler(catalog: Arc<FileSystemCatalog>) -> PaimonSqlHandler {
let ctx = SessionContext::new();
ctx.register_catalog(
"paimon",
Arc::new(PaimonCatalogProvider::new(catalog.clone())),
);
ctx.register_relation_planner(Arc::new(PaimonRelationPlanner::new()))
.expect("Failed to register relation planner");
PaimonSqlHandler::new(ctx, catalog, "paimon")
}
// ======================= CREATE / DROP SCHEMA =======================
#[tokio::test]
async fn test_create_schema() {
let (_tmp, catalog) = create_test_env();
let handler = create_handler(catalog.clone());
handler
.sql("CREATE SCHEMA paimon.test_db")
.await
.expect("CREATE SCHEMA should succeed");
let databases = catalog.list_databases().await.unwrap();
assert!(
databases.contains(&"test_db".to_string()),
"Database test_db should exist after CREATE SCHEMA"
);
}
#[tokio::test]
async fn test_drop_schema() {
let (_tmp, catalog) = create_test_env();
let handler = create_handler(catalog.clone());
catalog
.create_database("drop_me", false, Default::default())
.await
.unwrap();
handler
.sql("DROP SCHEMA paimon.drop_me CASCADE")
.await
.expect("DROP SCHEMA should succeed");
let databases = catalog.list_databases().await.unwrap();
assert!(
!databases.contains(&"drop_me".to_string()),
"Database drop_me should not exist after DROP SCHEMA"
);
}
#[tokio::test]
async fn test_schema_names_via_catalog_provider() {
let (_tmp, catalog) = create_test_env();
let provider = PaimonCatalogProvider::new(catalog.clone());
catalog
.create_database("db_a", false, Default::default())
.await
.unwrap();
catalog
.create_database("db_b", false, Default::default())
.await
.unwrap();
let names = provider.schema_names();
assert!(names.contains(&"db_a".to_string()));
assert!(names.contains(&"db_b".to_string()));
}
// ======================= CREATE TABLE =======================
#[tokio::test]
async fn test_create_table() {
let (_tmp, catalog) = create_test_env();
let handler = create_handler(catalog.clone());
catalog
.create_database("mydb", false, Default::default())
.await
.unwrap();
handler
.sql(
"CREATE TABLE paimon.mydb.users (
id INT NOT NULL,
name STRING,
age INT,
PRIMARY KEY (id)
)",
)
.await
.expect("CREATE TABLE should succeed");
let tables = catalog.list_tables("mydb").await.unwrap();
assert!(
tables.contains(&"users".to_string()),
"Table users should exist after CREATE TABLE"
);
// Verify schema
let table = catalog
.get_table(&Identifier::new("mydb", "users"))
.await
.unwrap();
let schema = table.schema();
assert_eq!(schema.fields().len(), 3);
assert_eq!(schema.primary_keys(), &["id"]);
}
#[tokio::test]
async fn test_create_table_with_blob_type() {
let (_tmp, catalog) = create_test_env();
let handler = create_handler(catalog.clone());
catalog
.create_database("mydb", false, Default::default())
.await
.unwrap();
handler
.sql(
"CREATE TABLE paimon.mydb.assets (
id INT NOT NULL,
payload BLOB,
PRIMARY KEY (id)
)",
)
.await
.expect("CREATE TABLE with BLOB should succeed");
let table = catalog
.get_table(&Identifier::new("mydb", "assets"))
.await
.unwrap();
let schema = table.schema();
assert_eq!(schema.fields().len(), 2);
assert_eq!(schema.primary_keys(), &["id"]);
assert_eq!(
*schema.fields()[1].data_type(),
DataType::Blob(BlobType::new())
);
}
#[tokio::test]
async fn test_create_table_with_partition() {
let (_tmp, catalog) = create_test_env();
let handler = create_handler(catalog.clone());
catalog
.create_database("mydb", false, Default::default())
.await
.unwrap();
handler
.sql(
"CREATE TABLE paimon.mydb.events (
id INT NOT NULL,
name STRING,
dt STRING,
PRIMARY KEY (id, dt)
) PARTITIONED BY (dt STRING)
WITH ('bucket' = '2')",
)
.await
.expect("CREATE TABLE with partition should succeed");
let table = catalog
.get_table(&Identifier::new("mydb", "events"))
.await
.unwrap();
let schema = table.schema();
assert_eq!(schema.partition_keys(), &["dt"]);
assert_eq!(schema.primary_keys(), &["id", "dt"]);
assert_eq!(
schema.options().get("bucket"),
Some(&"2".to_string()),
"Table option 'bucket' should be preserved"
);
}
#[tokio::test]
async fn test_create_table_if_not_exists() {
let (_tmp, catalog) = create_test_env();
let handler = create_handler(catalog.clone());
catalog
.create_database("mydb", false, Default::default())
.await
.unwrap();
let sql = "CREATE TABLE IF NOT EXISTS paimon.mydb.t1 (
id INT NOT NULL
)";
// First create should succeed
handler.sql(sql).await.expect("First CREATE should succeed");
// Second create with IF NOT EXISTS should also succeed
handler
.sql(sql)
.await
.expect("Second CREATE with IF NOT EXISTS should succeed");
}
#[tokio::test]
async fn test_create_external_table_rejected() {
let (_tmp, catalog) = create_test_env();
let handler = create_handler(catalog.clone());
catalog
.create_database("mydb", false, Default::default())
.await
.unwrap();
let result = handler
.sql(
"CREATE EXTERNAL TABLE paimon.mydb.bad (
id INT NOT NULL
) STORED AS PARQUET
LOCATION '/some/path'",
)
.await;
assert!(result.is_err(), "CREATE EXTERNAL TABLE should be rejected");
let err_msg = result.unwrap_err().to_string();
assert!(
err_msg.contains("CREATE EXTERNAL TABLE is not supported"),
"Error should mention CREATE EXTERNAL TABLE is not supported, got: {err_msg}"
);
}
// ======================= CREATE TABLE with complex types =======================
#[tokio::test]
async fn test_create_table_with_array_and_map() {
let (_tmp, catalog) = create_test_env();
let handler = create_handler(catalog.clone());
catalog
.create_database("mydb", false, Default::default())
.await
.unwrap();
handler
.sql(
"CREATE TABLE paimon.mydb.complex_types (
id INT NOT NULL,
tags ARRAY<STRING>,
props MAP(STRING, INT),
PRIMARY KEY (id)
)",
)
.await
.expect("CREATE TABLE with ARRAY and MAP should succeed");
let table = catalog
.get_table(&Identifier::new("mydb", "complex_types"))
.await
.unwrap();
let schema = table.schema();
assert_eq!(schema.fields().len(), 3);
assert_eq!(schema.primary_keys(), &["id"]);
// Verify ARRAY<STRING> column
let tags_field = &schema.fields()[1];
assert_eq!(tags_field.name(), "tags");
assert_eq!(
*tags_field.data_type(),
DataType::Array(ArrayType::new(
DataType::VarChar(VarCharType::string_type())
))
);
// Verify MAP(STRING, INT) column
let props_field = &schema.fields()[2];
assert_eq!(props_field.name(), "props");
assert_eq!(
*props_field.data_type(),
DataType::Map(MapType::new(
DataType::VarChar(VarCharType::string_type())
.copy_with_nullable(false)
.unwrap(),
DataType::Int(IntType::new()),
))
);
}
#[tokio::test]
async fn test_create_table_with_row_type() {
let (_tmp, catalog) = create_test_env();
let handler = create_handler(catalog.clone());
catalog
.create_database("mydb", false, Default::default())
.await
.unwrap();
handler
.sql(
"CREATE TABLE paimon.mydb.row_table (
id INT NOT NULL,
address STRUCT<city STRING, zip INT>,
PRIMARY KEY (id)
)",
)
.await
.expect("CREATE TABLE with STRUCT should succeed");
let table = catalog
.get_table(&Identifier::new("mydb", "row_table"))
.await
.unwrap();
let schema = table.schema();
assert_eq!(schema.fields().len(), 2);
// Verify STRUCT<city STRING, zip INT> column
let address_field = &schema.fields()[1];
assert_eq!(address_field.name(), "address");
if let DataType::Row(row) = address_field.data_type() {
assert_eq!(row.fields().len(), 2);
assert_eq!(row.fields()[0].name(), "city");
assert!(matches!(row.fields()[0].data_type(), DataType::VarChar(_)));
assert_eq!(row.fields()[1].name(), "zip");
assert!(matches!(row.fields()[1].data_type(), DataType::Int(_)));
} else {
panic!("expected Row type for address column");
}
}
// ======================= DROP TABLE =======================
#[tokio::test]
async fn test_drop_table() {
let (_tmp, catalog) = create_test_env();
let handler = create_handler(catalog.clone());
catalog
.create_database("mydb", false, Default::default())
.await
.unwrap();
// Create a table first
let schema = paimon::spec::Schema::builder()
.column(
"id",
paimon::spec::DataType::Int(paimon::spec::IntType::new()),
)
.build()
.unwrap();
catalog
.create_table(&Identifier::new("mydb", "to_drop"), schema, false)
.await
.unwrap();
assert!(catalog
.list_tables("mydb")
.await
.unwrap()
.contains(&"to_drop".to_string()));
handler
.sql("DROP TABLE paimon.mydb.to_drop")
.await
.expect("DROP TABLE should succeed");
assert!(
!catalog
.list_tables("mydb")
.await
.unwrap()
.contains(&"to_drop".to_string()),
"Table should not exist after DROP TABLE"
);
}
// ======================= ALTER TABLE =======================
#[tokio::test]
async fn test_alter_table_add_column() {
let (_tmp, catalog) = create_test_env();
let handler = create_handler(catalog.clone());
catalog
.create_database("mydb", false, Default::default())
.await
.unwrap();
let schema = paimon::spec::Schema::builder()
.column(
"id",
paimon::spec::DataType::Int(paimon::spec::IntType::new()),
)
.column(
"name",
paimon::spec::DataType::VarChar(paimon::spec::VarCharType::string_type()),
)
.build()
.unwrap();
catalog
.create_table(&Identifier::new("mydb", "alter_test"), schema, false)
.await
.unwrap();
// ALTER TABLE is not yet implemented in FileSystemCatalog, so we expect an error
let result = handler
.sql("ALTER TABLE paimon.mydb.alter_test ADD COLUMN age INT")
.await;
// FileSystemCatalog returns Unsupported for alter_table, which is expected
assert!(
result.is_err(),
"ALTER TABLE should fail because FileSystemCatalog does not implement alter_table yet"
);
let err_msg = result.unwrap_err().to_string();
assert!(
err_msg.contains("not yet implemented") || err_msg.contains("Unsupported"),
"Error should indicate alter_table is not implemented, got: {err_msg}"
);
}
#[tokio::test]
async fn test_alter_table_rename() {
let (_tmp, catalog) = create_test_env();
let handler = create_handler(catalog.clone());
catalog
.create_database("mydb", false, Default::default())
.await
.unwrap();
let schema = paimon::spec::Schema::builder()
.column(
"id",
paimon::spec::DataType::Int(paimon::spec::IntType::new()),
)
.build()
.unwrap();
catalog
.create_table(&Identifier::new("mydb", "old_name"), schema, false)
.await
.unwrap();
handler
.sql("ALTER TABLE mydb.old_name RENAME TO new_name")
.await
.expect("ALTER TABLE RENAME should succeed");
let tables = catalog.list_tables("mydb").await.unwrap();
assert!(
!tables.contains(&"old_name".to_string()),
"old_name should not exist after rename"
);
assert!(
tables.contains(&"new_name".to_string()),
"new_name should exist after rename"
);
}
#[tokio::test]
async fn test_ddl_handler_delegates_select() {
let (_tmp, catalog) = create_test_env();
let handler = create_handler(catalog.clone());
catalog
.create_database("mydb", false, Default::default())
.await
.unwrap();
let schema = paimon::spec::Schema::builder()
.column(
"id",
paimon::spec::DataType::Int(paimon::spec::IntType::new()),
)
.build()
.unwrap();
catalog
.create_table(&Identifier::new("mydb", "t1"), schema, false)
.await
.unwrap();
// SELECT should be delegated to DataFusion
let df = handler
.sql("SELECT * FROM paimon.mydb.t1")
.await
.expect("SELECT should be delegated to DataFusion");
let batches = df.collect().await.expect("SELECT should execute");
// Empty table, but should succeed
let total_rows: usize = batches.iter().map(|b| b.num_rows()).sum();
assert_eq!(total_rows, 0, "Empty table should return 0 rows");
}