@@ -2272,6 +2272,230 @@ Y_UNIT_TEST_SUITE(KqpQuery) {
2272
2272
UNIT_ASSERT_C (result.IsSuccess (), result.GetIssues ().ToString ());
2273
2273
}
2274
2274
}
2275
+
2276
+ Y_UNIT_TEST_QUAD (CreateAsSelectTypes, NotNull, IsOlap) {
2277
+ NKikimrConfig::TAppConfig appConfig;
2278
+ appConfig.MutableTableServiceConfig ()->SetEnableOlapSink (true );
2279
+ appConfig.MutableTableServiceConfig ()->SetEnableOltpSink (true );
2280
+ appConfig.MutableTableServiceConfig ()->SetEnableCreateTableAs (true );
2281
+ appConfig.MutableTableServiceConfig ()->SetEnablePerStatementQueryExecution (true );
2282
+ auto settings = TKikimrSettings ()
2283
+ .SetAppConfig (appConfig)
2284
+ .SetWithSampleTables (false )
2285
+ .SetEnableTempTables (true );
2286
+ TKikimrRunner kikimr (settings);
2287
+ auto client = kikimr.GetQueryClient ();
2288
+
2289
+ {
2290
+ const TString createSource = std::format (R"(
2291
+ CREATE TABLE `/Root/Source` (
2292
+ Key Int8 NOT NULL,
2293
+ CInt8 Int8 {0},
2294
+ CUint8 Uint8 {0},
2295
+ CInt16 Int16 {0},
2296
+ CUint16 Uint16 {0},
2297
+ CInt32 Int32 {0},
2298
+ CUint32 Uint32 {0},
2299
+ CInt64 Int64 {0},
2300
+ CUint64 Uint64 {0},
2301
+ CFloat Float {0},
2302
+ CDouble Double {0},
2303
+ CDate Date {0},
2304
+ CDatetime Datetime {0},
2305
+ CTimestamp Timestamp {0},
2306
+ CDate32 Date32 {0},
2307
+ CDatetime64 Datetime64 {0},
2308
+ CTimestamp64 Timestamp64 {0},
2309
+ CString String {0},
2310
+ CUtf8 Utf8 {0},
2311
+ CYson Yson {0},
2312
+ CJson Json {0},
2313
+ CJsonDocument JsonDocument {0},
2314
+ {1}
2315
+ PRIMARY KEY (Key)
2316
+ );
2317
+ )" ,
2318
+ NotNull ? " NOT NULL" : " " ,
2319
+ IsOlap ? " " : std::format (R"(
2320
+ CBool Bool {0},
2321
+ CInterval Interval {0},
2322
+ CInterval64 Interval64 {0},
2323
+ CUuid Uuid {0},
2324
+ CDyNumber DyNumber {0},)" ,
2325
+ NotNull ? " NOT NULL" : " " ));
2326
+
2327
+ auto result = client.ExecuteQuery (createSource, NYdb::NQuery::TTxControl::NoTx ()).ExtractValueSync ();
2328
+ UNIT_ASSERT_C (result.GetStatus () == NYdb::EStatus::SUCCESS, result.GetIssues ().ToString ());
2329
+ }
2330
+
2331
+ {
2332
+ auto prepareResult = client.ExecuteQuery (std::format (R"(
2333
+ UPSERT INTO `/Root/Source` (
2334
+ Key
2335
+ , CInt8
2336
+ , CUint8
2337
+ , CInt16
2338
+ , CUint16
2339
+ , CInt32
2340
+ , CUint32
2341
+ , CInt64
2342
+ , CUint64
2343
+ , CFloat
2344
+ , CDouble
2345
+ , CDate
2346
+ , CDatetime
2347
+ , CTimestamp
2348
+ , CDate32
2349
+ , CDatetime64
2350
+ , CTimestamp64
2351
+ , CString
2352
+ , CUtf8
2353
+ , CYson
2354
+ , CJson
2355
+ , CJsonDocument
2356
+ {0}
2357
+ )
2358
+ VALUES (
2359
+ 0
2360
+ , 42
2361
+ , 42
2362
+ , 42
2363
+ , 42
2364
+ , 42
2365
+ , 42
2366
+ , 42
2367
+ , 42
2368
+ , CAST(42.0 AS Float)
2369
+ , 42.0
2370
+ , Date("2025-01-01")
2371
+ , Datetime("2025-01-01T00:00:00Z")
2372
+ , Timestamp("2025-01-01T00:00:00Z")
2373
+ , Date("2025-01-01")
2374
+ , Datetime("2025-01-01T00:00:00Z")
2375
+ , Timestamp("2025-01-01T00:00:00Z")
2376
+ , String("test")
2377
+ , Utf8("test")
2378
+ , Yson("<a=1>[3;%false]")
2379
+ , Json(@@{{"a":1,"b":null}}@@)
2380
+ , JsonDocument('{{"a":1,"b":null}}')
2381
+ {1}
2382
+ );
2383
+ )" ,
2384
+ IsOlap ? " " : " , CBool, CInterval, CInterval64, CUuid, CDyNumber" ,
2385
+ IsOlap ? " " : " , False, Interval(\" P1DT2H3M4.567890S\" ), Interval(\" P1DT2H3M4.567890S\" ), Uuid(\" f9d5cc3f-f1dc-4d9c-b97e-766e57ca4ccb\" ), DyNumber(\" 42\" )" ),
2386
+ NYdb::NQuery::TTxControl::BeginTx ().CommitTx ()).ExtractValueSync ();
2387
+ UNIT_ASSERT_C (prepareResult.IsSuccess (), prepareResult.GetIssues ().ToString ());
2388
+ }
2389
+
2390
+ {
2391
+ auto prepareResult = client.ExecuteQuery (std::format (R"(
2392
+ CREATE TABLE `/Root/Destination` (
2393
+ PRIMARY KEY (Key)
2394
+ )
2395
+ WITH (STORE = {0})
2396
+ AS SELECT *
2397
+ FROM `/Root/Source`;
2398
+ )" , IsOlap ? " COLUMN" : " ROW" ), NYdb::NQuery::TTxControl::NoTx ()).ExtractValueSync ();
2399
+ UNIT_ASSERT_C (prepareResult.IsSuccess (), prepareResult.GetIssues ().ToString ());
2400
+ }
2401
+
2402
+ {
2403
+ auto db = kikimr.GetTableClient ();
2404
+ auto session = db.CreateSession ().GetValueSync ().GetSession ();
2405
+ auto desc = session.DescribeTable (" /Root/Destination" ).ExtractValueSync ();
2406
+ UNIT_ASSERT_C (desc.IsSuccess (), desc.GetIssues ().ToString ());
2407
+
2408
+ auto columns = desc.GetTableDescription ().GetTableColumns ();
2409
+ for (const auto & column : columns) {
2410
+ if (column.Name == " Key" ) {
2411
+ continue ;
2412
+ }
2413
+
2414
+ UNIT_ASSERT (!column.NotNull );
2415
+
2416
+ static THashMap<TString, TString> nameToType = {
2417
+ {" CBool" , " Bool?" },
2418
+ {" CInt8" , " Int8?" },
2419
+ {" CUint8" , " Uint8?" },
2420
+ {" CInt16" , " Int16?" },
2421
+ {" CUint16" , " Uint16?" },
2422
+ {" CInt32" , " Int32?" },
2423
+ {" CUint32" , " Uint32?" },
2424
+ {" CInt64" , " Int64?" },
2425
+ {" CUint64" , " Uint64?" },
2426
+ {" CFloat" , " Float?" },
2427
+ {" CDouble" , " Double?" },
2428
+ {" CDate" , " Date?" },
2429
+ {" CDatetime" , " Datetime?" },
2430
+ {" CTimestamp" , " Timestamp?" },
2431
+ {" CInterval" , " Interval?" },
2432
+ {" CDate32" , " Date32?" },
2433
+ {" CDatetime64" , " Datetime64?" },
2434
+ {" CTimestamp64" , " Timestamp64?" },
2435
+ {" CInterval64" , " Interval64?" },
2436
+ {" CString" , " String?" },
2437
+ {" CUtf8" , " Utf8?" },
2438
+ {" CYson" , " Yson?" },
2439
+ {" CJson" , " Json?" },
2440
+ {" CUuid" , " Uuid?" },
2441
+ {" CJsonDocument" , " JsonDocument?" },
2442
+ {" CDyNumber" , " DyNumber?" },
2443
+ };
2444
+
2445
+ UNIT_ASSERT_VALUES_EQUAL_C (nameToType.at (column.Name ), column.Type .ToString (), column.Name );
2446
+ }
2447
+ }
2448
+ }
2449
+
2450
+ Y_UNIT_TEST_TWIN (CreateAsSelectBadTypes, IsOlap) {
2451
+ NKikimrConfig::TAppConfig appConfig;
2452
+ appConfig.MutableTableServiceConfig ()->SetEnableOlapSink (true );
2453
+ appConfig.MutableTableServiceConfig ()->SetEnableOltpSink (true );
2454
+ appConfig.MutableTableServiceConfig ()->SetEnableCreateTableAs (true );
2455
+ appConfig.MutableTableServiceConfig ()->SetEnablePerStatementQueryExecution (true );
2456
+ auto settings = TKikimrSettings ()
2457
+ .SetAppConfig (appConfig)
2458
+ .SetWithSampleTables (false )
2459
+ .SetEnableTempTables (true );
2460
+ TKikimrRunner kikimr (settings);
2461
+ auto client = kikimr.GetQueryClient ();
2462
+
2463
+ {
2464
+ auto result = client.ExecuteQuery (std::format (R"(
2465
+ CREATE TABLE `/Root/Destination` (
2466
+ PRIMARY KEY (Key)
2467
+ )
2468
+ WITH (STORE = {0})
2469
+ AS SELECT 1 AS Key, AsList(1, 2, 3, 4, 5) AS Value;
2470
+ )" , IsOlap ? " COLUMN" : " ROW" ), NYdb::NQuery::TTxControl::NoTx ()).ExtractValueSync ();
2471
+ UNIT_ASSERT_C (!result.IsSuccess (), result.GetIssues ().ToString ());
2472
+ UNIT_ASSERT_STRING_CONTAINS_C (result.GetIssues ().ToString (), " Invalid type for column: Value." , result.GetIssues ().ToString ());
2473
+ }
2474
+
2475
+ {
2476
+ auto result = client.ExecuteQuery (std::format (R"(
2477
+ CREATE TABLE `/Root/Destination` (
2478
+ PRIMARY KEY (Key)
2479
+ )
2480
+ WITH (STORE = {0})
2481
+ AS SELECT 1 AS Key, NULL AS Value;
2482
+ )" , IsOlap ? " COLUMN" : " ROW" ), NYdb::NQuery::TTxControl::NoTx ()).ExtractValueSync ();
2483
+ UNIT_ASSERT_C (!result.IsSuccess (), result.GetIssues ().ToString ());
2484
+ UNIT_ASSERT_STRING_CONTAINS_C (result.GetIssues ().ToString (), " Invalid type for column: Value." , result.GetIssues ().ToString ());
2485
+ }
2486
+
2487
+ {
2488
+ auto result = client.ExecuteQuery (std::format (R"(
2489
+ CREATE TABLE `/Root/Destination` (
2490
+ PRIMARY KEY (Key)
2491
+ )
2492
+ WITH (STORE = {0})
2493
+ AS SELECT 1 AS Key, [] AS Value;
2494
+ )" , IsOlap ? " COLUMN" : " ROW" ), NYdb::NQuery::TTxControl::NoTx ()).ExtractValueSync ();
2495
+ UNIT_ASSERT_C (!result.IsSuccess (), result.GetIssues ().ToString ());
2496
+ UNIT_ASSERT_STRING_CONTAINS_C (result.GetIssues ().ToString (), " Invalid type for column: Value." , result.GetIssues ().ToString ());
2497
+ }
2498
+ }
2275
2499
}
2276
2500
2277
2501
} // namespace NKqp
0 commit comments