forked from heavyai/heavydb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMLTableFunctionsTest.cpp
797 lines (747 loc) · 33.3 KB
/
MLTableFunctionsTest.cpp
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
/*
* Copyright 2022 HEAVY.AI, Inc.
*
* Licensed 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.
*/
#include "../QueryEngine/Execute.h"
#include "../QueryRunner/QueryRunner.h"
#include "QueryEngine/TableFunctions/TableFunctionManager.h"
#include "TestHelpers.h"
#include <gtest/gtest.h>
#include <sstream>
#include <string>
#include <vector>
#ifndef BASE_PATH
#define BASE_PATH "./tmp"
#endif
using QR = QueryRunner::QueryRunner;
extern bool g_enable_table_functions;
using namespace TestHelpers;
namespace {
inline void run_ddl_statement(const std::string& stmt) {
QR::get()->runDDLStatement(stmt);
}
std::shared_ptr<ResultSet> run_multiple_agg(const std::string& query_str,
const ExecutorDeviceType device_type) {
return QR::get()->runSQL(query_str, device_type, false, false);
}
TargetValue run_simple_agg(const std::string& query_str,
const ExecutorDeviceType device_type,
const bool geo_return_geo_tv = true,
const bool allow_loop_joins = true) {
auto rows = QR::get()->runSQL(query_str, device_type, allow_loop_joins);
auto crt_row = rows->getNextRow(true, true);
CHECK_EQ(size_t(1), crt_row.size()) << query_str;
return crt_row[0];
}
} // namespace
bool skip_tests(const ExecutorDeviceType device_type) {
#ifdef HAVE_CUDA
return device_type == ExecutorDeviceType::GPU && !(QR::get()->gpusPresent());
#else
return device_type == ExecutorDeviceType::GPU;
#endif
}
#define SKIP_NO_GPU() \
if (skip_tests(dt)) { \
CHECK(dt == ExecutorDeviceType::GPU); \
LOG(WARNING) << "GPU not available, skipping GPU tests"; \
continue; \
}
const char* iris_schema = R"(
CREATE TABLE ml_iris(
id INT,
sepal_length_cm FLOAT,
sepal_width_cm FLOAT,
petal_length_cm FLOAT,
petal_width_cm FLOAT,
species TEXT);
)";
class SystemTFs : public ::testing::Test {
void SetUp() override {
ASSERT_NO_THROW(run_ddl_statement("DROP TABLE IF EXISTS ml_iris;"));
ASSERT_NO_THROW(run_ddl_statement(iris_schema));
const std::string import_file{"iris.csv"};
const auto load_str = std::string("COPY ml_iris FROM '") +
"../../Tests/Import/datafiles/ml/" + import_file +
"' WITH (header='true');";
ASSERT_NO_THROW(run_ddl_statement(load_str));
}
void TearDown() override { ASSERT_NO_THROW(run_ddl_statement("DROP TABLE ml_iris;")); }
};
std::string generate_cursor_query(const std::string& table_name,
const std::string& id_column,
const std::vector<std::string>& feature_columns,
const std::string& numeric_data_type) {
std::ostringstream oss;
oss << "CURSOR(SELECT ";
if (!id_column.empty()) {
oss << id_column << " AS id, ";
}
bool is_first_col = true;
for (const auto& feature_column : feature_columns) {
if (!is_first_col) {
oss << ", ";
} else {
is_first_col = false;
}
oss << "CAST(" << feature_column << " AS " << numeric_data_type << ")";
}
oss << " FROM " << table_name << ")";
return oss.str();
}
std::string generate_kmeans_query(const std::string& cursor_query,
const int32_t num_clusters,
const int32_t num_iterations,
const std::string& init_type = "",
const bool add_select_star = true) {
std::ostringstream oss;
if (add_select_star) {
oss << "SELECT * FROM ";
}
oss << "TABLE(KMEANS("
<< "data => " << cursor_query << ", num_clusters => " << num_clusters
<< ", num_iterations => " << num_iterations;
if (!init_type.empty()) {
oss << ", init_type => " << init_type;
}
if (add_select_star) {
oss << ")) ORDER BY id ASC";
}
return oss.str();
}
std::string generate_query(const std::string& algo_name,
const std::vector<std::pair<std::string, std::string>>& args,
const std::vector<std::string>& order_by_cols,
const bool make_args_named) {
std::ostringstream oss;
const bool project_data = !order_by_cols.empty();
if (project_data) {
oss << "SELECT * FROM ";
}
oss << "TABLE(" << algo_name << "(";
bool is_first_arg = true;
for (const auto& arg : args) {
if (!is_first_arg) {
oss << ", ";
}
is_first_arg = false;
if (make_args_named) {
oss << arg.first << " => ";
}
oss << arg.second;
}
oss << "))";
if (project_data) {
oss << " ORDER BY ";
is_first_arg = true;
for (const auto& order_by_col : order_by_cols) {
if (!is_first_arg) {
oss << ", ";
}
is_first_arg = false;
oss << order_by_col;
}
oss << " ASC NULLS LAST;";
}
return oss.str();
}
std::string generate_unsupervised_classifier_precision_query(
const std::string& algo_name,
const std::vector<std::pair<std::string, std::string>>& args,
const std::string& data_table_name,
const std::string& data_table_id_col,
const std::string& data_table_class_col,
const bool make_args_named = true) {
const auto classifier_query = generate_query(algo_name, args, {}, make_args_named);
std::ostringstream oss;
oss << "SELECT CAST(SUM(n) AS DOUBLE) / SUM(total_in_class) "
<< "FROM(SELECT class, cluster_id, n, total_in_class, perc_in_cluster "
<< "FROM(SELECT class, cluster_id, n, total_in_class, perc_in_cluster, "
<< "ROW_NUMBER() OVER (PARTITION BY class ORDER BY perc_in_cluster DESC) AS "
"cluster_rank "
<< "FROM(SELECT class, cluster_id, COUNT(*) as n, SUM(COUNT(*)) OVER (PARTITION BY "
"class) "
<< "AS total_in_class, CAST(COUNT(*) AS DOUBLE) / SUM(COUNT(*)) OVER (PARTITION BY "
"class) "
<< "AS perc_in_cluster "
<< "FROM(SELECT " << data_table_class_col << " AS class, cluster_id "
<< "FROM " << data_table_name << ", " << classifier_query << " classifier_query "
<< "WHERE " << data_table_name << "." << data_table_id_col
<< " = classifier_query.id) "
<< "GROUP BY class, cluster_id)) "
<< "WHERE cluster_rank = 1);";
return oss.str();
}
double calc_classification_accuracy(const std::string& data_table_name,
const std::string& classification_query) {
std::ostringstream oss;
return 0;
}
std::vector<std::string> get_supported_ml_frameworks() {
const std::string query =
"SELECT ml_framework FROM "
"TABLE(supported_ml_frameworks()) WHERE is_available = TRUE ORDER BY ml_framework "
"DESC;";
const auto rows = run_multiple_agg(query, ExecutorDeviceType::CPU);
std::vector<std::string> supported_ml_frameworks;
for (size_t row_idx = 0; row_idx < rows->rowCount(); ++row_idx) {
auto crt_row = rows->getNextRow(true, true);
supported_ml_frameworks.emplace_back(
std::string("'") +
boost::get<std::string>(TestHelpers::v<NullableString>(crt_row[0])) +
std::string("'"));
}
return supported_ml_frameworks;
}
TEST_F(SystemTFs, SupportedMLFrameworks) {
const std::vector<std::string> expected_ml_frameworks = {"onedal", "mlpack"};
std::vector<bool> expected_is_available;
std::vector<bool> expected_is_default;
bool found_ml_framework = false;
#ifdef HAVE_ONEDAL
expected_is_available.emplace_back(true);
expected_is_default.emplace_back(!found_ml_framework);
found_ml_framework = true;
#else
expected_is_available.emplace_back(false);
expected_is_default.emplace_back(false);
#endif
#ifdef HAVE_MLPACK
expected_is_available.emplace_back(true);
expected_is_default.emplace_back(!found_ml_framework);
found_ml_framework = true;
#else
expected_is_available.emplace_back(false);
expected_is_default.emplace_back(false);
#endif
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
const std::string query =
"SELECT ml_framework, is_available, is_default FROM "
"TABLE(supported_ml_frameworks()) ORDER BY ml_framework DESC;";
const auto rows = run_multiple_agg(query, dt);
const size_t num_rows = rows->rowCount();
EXPECT_EQ(num_rows, size_t(2));
EXPECT_EQ(rows->colCount(), size_t(3));
for (size_t row_idx = 0; row_idx < num_rows; ++row_idx) {
auto crt_row = rows->getNextRow(true, true);
const auto ml_framework =
boost::get<std::string>(TestHelpers::v<NullableString>(crt_row[0]));
EXPECT_EQ(ml_framework, expected_ml_frameworks[row_idx]);
EXPECT_EQ(TestHelpers::v<int64_t>(crt_row[1]),
expected_is_available[row_idx] ? 1 : 0);
EXPECT_EQ(TestHelpers::v<int64_t>(crt_row[2]),
expected_is_default[row_idx] ? 1 : 0);
}
}
}
TEST_F(SystemTFs, KMeansMissingArgs) {
const auto cursor_query = generate_cursor_query(
"ml_iris",
"id",
{"petal_length_cm", "petal_width_cm", "sepal_length_cm", "sepal_width_cm"},
"float");
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
// Missing args
{
for (bool make_args_named : {false, true}) {
EXPECT_ANY_THROW(run_multiple_agg(
generate_query("KMEANS",
{{"data", cursor_query}, {"num_clusters", "3"}},
{"id"},
make_args_named),
dt));
EXPECT_ANY_THROW(run_multiple_agg(
generate_query("KMEANS",
{{"data", cursor_query}, {"num_iterations", "10"}},
{"id"},
make_args_named),
dt));
EXPECT_ANY_THROW(
run_multiple_agg(generate_query("KMEANS",
{
{"data", cursor_query},
{"num_clusters", "3"},
{"init_type", "'DETERMINISTIC'"},
},
{"id"},
make_args_named),
dt));
EXPECT_ANY_THROW(run_multiple_agg(
generate_query("KMEANS",
{{"num_clusters", "3"}, {"num_iterations", "10"}},
{"id"},
make_args_named),
dt));
}
}
}
}
TEST_F(SystemTFs, KMeansInvalidType) {
for (auto numeric_data_type : {"int", "bigint"}) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
const auto cursor_query = generate_cursor_query(
"ml_iris",
"id",
{"petal_length_cm", "petal_width_cm", "sepal_length_cm", "sepal_width_cm"},
numeric_data_type);
EXPECT_ANY_THROW(run_multiple_agg(
generate_query(
"KMEANS",
{{"data", cursor_query}, {"num_clusters", "3"}, {"num_iterations", "10"}},
{"id"},
true /* make_args_named */),
dt));
}
}
}
TEST_F(SystemTFs, KMeansInvalidArgs) {
const auto numeric_data_type{"float"};
const auto cursor_query = generate_cursor_query(
"ml_iris",
"id",
{"petal_length_cm", "petal_width_cm", "sepal_length_cm", "sepal_width_cm"},
numeric_data_type);
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
// Should throw if num clusters <= 0
{
EXPECT_THROW(run_multiple_agg(generate_query("KMEANS",
{{"data", cursor_query},
{"num_clusters", "0"},
{"num_iterations", "10"}},
{"id"},
true /* make_args_named */),
dt),
UserTableFunctionError);
EXPECT_THROW(run_multiple_agg(generate_query("KMEANS",
{{"data", cursor_query},
{"num_clusters", "-1"},
{"num_iterations", "10"}},
{"id"},
true /* make_args_named */),
dt),
UserTableFunctionError);
// There are only 150 observations/rows in the iris dataset
// We should throw if we ask for more clusters than that (here 300)
EXPECT_THROW(run_multiple_agg(generate_query("KMEANS",
{{"data", cursor_query},
{"num_clusters", "300"},
{"num_iterations", "10"}},
{"id"},
true /* make_args_named */),
dt),
UserTableFunctionError);
}
// Should throw if num_iterations <= 0
{
EXPECT_THROW(run_multiple_agg(generate_query("KMEANS",
{{"data", cursor_query},
{"num_clusters", "3"},
{"num_iterations", "0"}},
{"id"},
true /* make_args_named */),
dt),
UserTableFunctionError);
EXPECT_THROW(run_multiple_agg(generate_query("KMEANS",
{{"data", cursor_query},
{"num_clusters", "3"},
{"num_iterations", "-1"}},
{"id"},
true /* make_args_named */),
dt),
UserTableFunctionError);
}
// Should throw if kmeans init type is invalid
{
EXPECT_THROW(run_multiple_agg(generate_query("KMEANS",
{{"data", cursor_query},
{"num_clusters", "3"},
{"num_iterations", "10"},
{"init_type", "'foo_bar'"}},
{"id"},
true /* make_args_named */),
dt),
UserTableFunctionError);
}
}
}
TEST_F(SystemTFs, KMeansNumClusters) {
for (auto numeric_data_type : {"float", "double"}) {
const auto cursor_query = generate_cursor_query(
"ml_iris",
"id",
{"petal_length_cm", "petal_width_cm", "sepal_length_cm", "sepal_width_cm"},
numeric_data_type);
const auto supported_ml_frameworks = get_supported_ml_frameworks();
for (auto& ml_framework : supported_ml_frameworks) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
const auto num_obs = TestHelpers::v<int64_t>(
run_simple_agg("SELECT COUNT(DISTINCT id) FROM ml_iris;", dt));
for (int32_t num_clusters = 1; num_clusters < 10; ++num_clusters) {
std::ostringstream oss;
oss << "SELECT COUNT(DISTINCT id), COUNT(DISTINCT cluster_id) FROM "
"TABLE(KMEANS("
<< "data => " << cursor_query << ", num_clusters => " << num_clusters
<< ", num_iterations => 10, init_type => 'DEFAULT', "
<< "preferred_ml_framework => " << ml_framework << "));";
const std::string query = oss.str();
const auto rows = run_multiple_agg(query, dt);
EXPECT_EQ(rows->rowCount(), size_t(1));
EXPECT_EQ(rows->colCount(), size_t(2));
auto crt_row = rows->getNextRow(true, true);
EXPECT_EQ(TestHelpers::v<int64_t>(crt_row[0]), num_obs);
EXPECT_EQ(TestHelpers::v<int64_t>(crt_row[1]), num_clusters);
}
}
}
}
}
TEST_F(SystemTFs, KMeansPrecision) {
const std::string data_table_name{"ml_iris"};
const std::string id_col{"id"};
const std::vector<std::string> feature_cols{
{"petal_length_cm", "petal_width_cm", "sepal_length_cm", "sepal_width_cm"}};
const std::string class_col{"species"};
const auto supported_ml_frameworks = get_supported_ml_frameworks();
for (auto& ml_framework : supported_ml_frameworks) {
for (auto numeric_data_type : {"FLOAT", "DOUBLE"}) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
const size_t num_classes = TestHelpers::v<int64_t>(run_simple_agg(
"SELECT COUNT(DISTINCT " + class_col + ") FROM " + data_table_name + ";",
dt));
const auto cursor_query = generate_cursor_query(
data_table_name, id_col, feature_cols, numeric_data_type);
const auto precision_query = generate_unsupervised_classifier_precision_query(
"KMEANS",
{{"data", cursor_query},
{"num_clusters", std::to_string(num_classes)},
{"num_iterations", "10"},
{"init_type", "'DEFAULT'"},
{"preferred_ml_framework", ml_framework}},
data_table_name,
id_col,
class_col,
true);
EXPECT_GE(TestHelpers::v<double>(run_simple_agg(precision_query, dt)), 0.8);
}
}
}
}
// Hitting issues with the dbscan preflight require checks
// causing a crash at launch time, so disabling until can troubleshoot
TEST_F(SystemTFs, DBSCANInvalidArgs) {
const std::string data_table_name{"ml_iris"};
const std::string id_col{"id"};
const std::vector<std::string> feature_cols{
{"petal_length_cm", "petal_width_cm", "sepal_length_cm", "sepal_width_cm"}};
const std::string class_col{"species"};
const auto numeric_data_type{"float"};
const auto cursor_query =
generate_cursor_query(data_table_name, id_col, feature_cols, numeric_data_type);
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
// Should throw if epsilon <= 0
EXPECT_THROW(run_multiple_agg(generate_query("DBSCAN",
{{"data", cursor_query},
{"epsilon", "-1.0"},
{"min_observations", "10"}},
{"id"},
true /* make_args_named */),
dt),
UserTableFunctionError);
EXPECT_THROW(run_multiple_agg(generate_query("DBSCAN",
{{"data", cursor_query},
{"epsilon", "0.0"},
{"min_observations", "10"}},
{"id"},
true /* make_args_named */),
dt),
UserTableFunctionError);
// Should throw if min_observations <= 0
EXPECT_THROW(run_multiple_agg(generate_query("DBSCAN",
{{"data", cursor_query},
{"epsilon", "0.5"},
{"min_observations", "-10"}},
{"id"},
true /* make_args_named */),
dt),
UserTableFunctionError);
EXPECT_THROW(run_multiple_agg(generate_query("DBSCAN",
{{"data", cursor_query},
{"epsilon", "0.5"},
{"min_observations", "0"}},
{"id"},
true /* make_args_named */),
dt),
UserTableFunctionError);
}
}
TEST_F(SystemTFs, DBSCAN) {
const std::string data_table_name{"ml_iris"};
const std::string id_col{"id"};
const std::vector<std::string> feature_cols{
{"petal_length_cm", "petal_width_cm", "sepal_length_cm", "sepal_width_cm"}};
const std::string class_col{"species"};
const auto supported_ml_frameworks = get_supported_ml_frameworks();
for (auto& ml_framework : supported_ml_frameworks) {
for (auto numeric_data_type : {"FLOAT", "DOUBLE"}) {
const auto cursor_query = generate_cursor_query(
"ml_iris",
"id",
{"petal_length_cm", "petal_width_cm", "sepal_length_cm", "sepal_width_cm"},
numeric_data_type);
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
const auto num_obs = TestHelpers::v<int64_t>(run_simple_agg(
"SELECT COUNT(DISTINCT id) FROM " + data_table_name + ";", dt));
const auto dbscan_partial_query =
generate_query("DBSCAN",
{{"data", cursor_query},
{"epsilon", "0.5"},
{"min_observations", "10"},
{"preferred_ml_framework", ml_framework}},
{},
true /* make_args_named */);
const auto query =
"SELECT COUNT(DISTINCT id), COUNT(DISTINCT cluster_id) "
" FROM " +
dbscan_partial_query + ";";
const auto rows = run_multiple_agg(query, dt);
EXPECT_EQ(rows->rowCount(), size_t(1));
EXPECT_EQ(rows->colCount(), size_t(2));
auto crt_row = rows->getNextRow(true, true);
EXPECT_EQ(TestHelpers::v<int64_t>(crt_row[0]), num_obs);
EXPECT_GT(TestHelpers::v<int64_t>(crt_row[1]), 1);
}
}
}
}
TEST_F(SystemTFs, LinearRegFit) {
const std::string data_table_name{"ml_iris"};
const std::string id_col{""}; // No id column allowed for Fit calls
const std::string label_column{"petal_length_cm"};
const auto supported_ml_frameworks = get_supported_ml_frameworks();
for (auto& ml_framework : supported_ml_frameworks) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
for (bool make_args_named : {false, true}) {
for (std::string numeric_data_type : {"FLOAT", "DOUBLE"}) {
{
// Synthetic data, also test nulls
const std::string cursor_query =
std::string(
"CURSOR(SELECT CAST(CASE WHEN MOD(generate_series, 10) = 0 THEN NULL "
"ELSE generate_series * 0.001 * 2.0 + 10.0 END AS ") +
numeric_data_type +
") AS y, "
"CAST(generate_series * 0.001 AS " +
numeric_data_type + ") AS x FROM TABLE(generate_series(0, 99999)))";
const auto query = generate_query(
"LINEAR_REG_FIT",
{{"data", cursor_query}, {"preferred_ml_framework", ml_framework}},
{"coef_idx"},
make_args_named);
const auto rows = run_multiple_agg(query, dt);
constexpr double allowed_epsilon{0.01};
const std::vector expected_coefs = {10.0, 2.0};
const int64_t num_rows = rows->rowCount();
EXPECT_EQ(num_rows, 2L);
EXPECT_EQ(rows->colCount(), size_t(2));
for (int64_t row_idx = 0; row_idx < num_rows; ++row_idx) {
auto crt_row = rows->getNextRow(true, true);
EXPECT_EQ(TestHelpers::v<int64_t>(crt_row[0]), row_idx);
const double actual_coef = (numeric_data_type == "FLOAT")
? TestHelpers::v<float>(crt_row[1])
: TestHelpers::v<double>(crt_row[1]);
EXPECT_GE(actual_coef, expected_coefs[row_idx] - allowed_epsilon);
EXPECT_LE(actual_coef, expected_coefs[row_idx] + allowed_epsilon);
}
}
{
// 1 independent variable
const auto cursor_query =
generate_cursor_query(data_table_name,
id_col,
{"petal_length_cm", "petal_width_cm"},
numeric_data_type);
const auto query = generate_query(
"LINEAR_REG_FIT",
{{"data", cursor_query}, {"preferred_ml_framework", ml_framework}},
{"coef_idx"},
make_args_named);
constexpr double allowed_epsilon{0.02};
const auto rows = run_multiple_agg(query, dt);
const std::vector expected_coefs = {1.09057, 2.22588};
const int64_t num_rows = rows->rowCount();
EXPECT_EQ(num_rows, 2L);
EXPECT_EQ(rows->colCount(), size_t(2));
for (int64_t row_idx = 0; row_idx < num_rows; ++row_idx) {
auto crt_row = rows->getNextRow(true, true);
EXPECT_EQ(TestHelpers::v<int64_t>(crt_row[0]), row_idx);
const double actual_coef = (numeric_data_type == "FLOAT")
? TestHelpers::v<float>(crt_row[1])
: TestHelpers::v<double>(crt_row[1]);
EXPECT_GE(actual_coef, expected_coefs[row_idx] - allowed_epsilon);
EXPECT_LE(actual_coef, expected_coefs[row_idx] + allowed_epsilon);
}
}
{
// 3 independent variables
const auto cursor_query = generate_cursor_query(data_table_name,
id_col,
{"petal_length_cm",
"petal_width_cm",
"sepal_length_cm",
"sepal_width_cm"},
numeric_data_type);
const auto query = generate_query(
"LINEAR_REG_FIT",
{{"data", cursor_query}, {"preferred_ml_framework", ml_framework}},
{"coef_idx"},
make_args_named);
const auto rows = run_multiple_agg(query, dt);
constexpr double allowed_epsilon{0.02};
const std::vector expected_coefs = {-0.252664, 1.44572, 0.730363, -0.651394};
const int64_t num_rows = rows->rowCount();
EXPECT_EQ(num_rows, 4L);
EXPECT_EQ(rows->colCount(), size_t(2));
for (int64_t row_idx = 0; row_idx < num_rows; ++row_idx) {
auto crt_row = rows->getNextRow(true, true);
EXPECT_EQ(TestHelpers::v<int64_t>(crt_row[0]), row_idx);
const double actual_coef = (numeric_data_type == "FLOAT")
? TestHelpers::v<float>(crt_row[1])
: TestHelpers::v<double>(crt_row[1]);
EXPECT_GE(actual_coef, expected_coefs[row_idx] - allowed_epsilon);
EXPECT_LE(actual_coef, expected_coefs[row_idx] + allowed_epsilon);
}
}
}
}
}
}
}
TEST_F(SystemTFs, LinearRegPredict) {
const auto supported_ml_frameworks = get_supported_ml_frameworks();
for (auto& ml_framework : supported_ml_frameworks) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
for (bool make_args_named : {false, true}) {
for (std::string numeric_data_type : {"FLOAT", "DOUBLE"}) {
{
// Synthetic data
const std::string data_table_name("TABLE(generate_series(0, 99999))");
const std::string id_col("generate_series");
const std::string feature_col("generate_series * 0.001");
const auto cursor_query = generate_cursor_query(
data_table_name, id_col, {feature_col}, numeric_data_type);
const std::string param_query =
std::string(
"CURSOR(SELECT * FROM TABLE(LINEAR_REG_FIT(data "
"=> CURSOR(SELECT CAST(CASE WHEN MOD(generate_series, 10) = 0 THEN "
"NULL ELSE "
"generate_series * 0.001 * 2.0 + 10.0 END AS ") +
numeric_data_type + ") AS y, CAST(generate_series * 0.001 AS " +
numeric_data_type +
") AS x "
"FROM TABLE(generate_series(0, 99999))))))";
const auto query = generate_query("LINEAR_REG_PREDICT",
{{"data", cursor_query},
{"params", param_query},
{"preferred_ml_framework", ml_framework}},
{"id"},
make_args_named);
const auto rows = run_multiple_agg(query, dt);
const int64_t num_rows = rows->rowCount();
EXPECT_EQ(num_rows, 100000L);
EXPECT_EQ(rows->colCount(), size_t(2));
constexpr double allowed_epsilon{0.01};
for (int64_t row_idx = 0; row_idx < num_rows; ++row_idx) {
auto crt_row = rows->getNextRow(true, true);
// id col
EXPECT_EQ(TestHelpers::v<int64_t>(crt_row[0]), row_idx);
const double predicted_val = (numeric_data_type == "FLOAT")
? TestHelpers::v<float>(crt_row[1])
: TestHelpers::v<double>(crt_row[1]);
const double expected_predicted_val = row_idx * 0.001 * 2.0 + 10.0;
EXPECT_GE(predicted_val, expected_predicted_val - allowed_epsilon);
EXPECT_LE(predicted_val, expected_predicted_val + allowed_epsilon);
}
}
}
}
}
}
}
TEST_F(SystemTFs, LinearRegFitPredict) {
const auto supported_ml_frameworks = get_supported_ml_frameworks();
for (auto& ml_framework : supported_ml_frameworks) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
for (bool make_args_named : {false, true}) {
for (std::string numeric_data_type : {"FLOAT", "DOUBLE"}) {
{
// Synthetic data
const std::string data_table_name("TABLE(generate_series(0, 99999))");
const std::string id_col("generate_series");
const std::vector<std::string> label_and_feature_cols = {
"generate_series * 0.001 * 2.0 + 10.0", "generate_series * 0.001"};
const auto cursor_query = generate_cursor_query(
data_table_name, id_col, label_and_feature_cols, numeric_data_type);
const auto query = generate_query(
"LINEAR_REG_FIT_PREDICT",
{{"data", cursor_query}, {"preferred_ml_framework", ml_framework}},
{"id"},
make_args_named);
const auto rows = run_multiple_agg(query, dt);
const int64_t num_rows = rows->rowCount();
EXPECT_EQ(num_rows, 100000L);
EXPECT_EQ(rows->colCount(), size_t(2));
constexpr double allowed_epsilon{0.01};
for (int64_t row_idx = 0; row_idx < num_rows; ++row_idx) {
auto crt_row = rows->getNextRow(true, true);
// id col
EXPECT_EQ(TestHelpers::v<int64_t>(crt_row[0]), row_idx);
const double predicted_val = (numeric_data_type == "FLOAT")
? TestHelpers::v<float>(crt_row[1])
: TestHelpers::v<double>(crt_row[1]);
const double expected_predicted_val = row_idx * 0.001 * 2.0 + 10.0;
EXPECT_GE(predicted_val, expected_predicted_val - allowed_epsilon);
EXPECT_LE(predicted_val, expected_predicted_val + allowed_epsilon);
}
}
}
}
}
}
}
int main(int argc, char** argv) {
TestHelpers::init_logger_stderr_only(argc, argv);
testing::InitGoogleTest(&argc, argv);
// Table function support must be enabled before initialized the query runner
// environment
g_enable_table_functions = true;
QR::init(BASE_PATH);
int err{0};
try {
err = RUN_ALL_TESTS();
} catch (const std::exception& e) {
LOG(ERROR) << e.what();
}
QR::reset();
return err;
}