OmniSciDB  c07336695a
FromTableReorderingTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019 OmniSci, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "TestHelpers.h"
18 
19 #include "../Import/Importer.h"
20 #include "../Parser/parser.h"
21 #include "../QueryEngine/ArrowResultSet.h"
22 #include "../QueryEngine/Descriptors/RelAlgExecutionDescriptor.h"
23 #include "../QueryEngine/Execute.h"
24 #include "../QueryEngine/FromTableReordering.h"
25 #include "../QueryRunner/QueryRunner.h"
26 #include "../Shared/Compressor.h"
27 #include "../Shared/ConfigResolve.h"
28 #include "../Shared/TimeGM.h"
29 #include "../Shared/scope.h"
30 #include "../SqliteConnector/SqliteConnector.h"
31 #include "DistributedLoader.h"
32 
33 #include <gtest/gtest.h>
34 
35 TEST(Ordering, Basic) {
36  // Basic test of inner join ordering. Equal table sizes.
37  {
38  auto a1 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kINT, true}, 0, 0, 0);
39  auto a2 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kINT, true}, 1, 1, 1);
40  auto op = std::make_shared<Analyzer::BinOper>(kINT, kGT, kONE, a1, a2);
41 
43  JoinQualsPerNestingLevel nesting_levels;
44  nesting_levels.push_back(jc);
45 
46  size_t number_of_join_tables{2};
47  std::vector<InputTableInfo> viti(number_of_join_tables);
48 
49  auto input_permutation = get_node_input_permutation(nesting_levels, viti, nullptr);
50  decltype(input_permutation) expected_input_permutation{0, 1};
51  ASSERT_EQ(expected_input_permutation, input_permutation);
52  }
53 
54  // Basic test of inner join ordering. Descending table sizes.
55  {
56  auto a1 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kINT, true}, 0, 0, 0);
57  auto a2 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kINT, true}, 1, 1, 1);
58  auto op = std::make_shared<Analyzer::BinOper>(kINT, kGT, kONE, a1, a2);
59 
61  JoinQualsPerNestingLevel nesting_levels;
62  nesting_levels.push_back(jc);
63 
64  size_t number_of_join_tables{2};
65  std::vector<InputTableInfo> viti(number_of_join_tables);
66  viti[0].info.setPhysicalNumTuples(2);
67  viti[1].info.setPhysicalNumTuples(1);
68 
69  auto input_permutation = get_node_input_permutation(nesting_levels, viti, nullptr);
70  decltype(input_permutation) expected_input_permutation{0, 1};
71  ASSERT_EQ(expected_input_permutation, input_permutation);
72  }
73 
74  // Basic test of inner join ordering. Ascending table sizes.
75  {
76  auto a1 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kINT, true}, 0, 0, 0);
77  auto a2 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kINT, true}, 1, 1, 1);
78  auto op = std::make_shared<Analyzer::BinOper>(kINT, kGT, kONE, a1, a2);
79 
81  JoinQualsPerNestingLevel nesting_levels;
82  nesting_levels.push_back(jc);
83 
84  size_t number_of_join_tables{2};
85  std::vector<InputTableInfo> viti(number_of_join_tables);
86  viti[0].info.setPhysicalNumTuples(1);
87  viti[1].info.setPhysicalNumTuples(2);
88 
89  auto input_permutation = get_node_input_permutation(nesting_levels, viti, nullptr);
90  decltype(input_permutation) expected_input_permutation{1, 0};
91  ASSERT_EQ(expected_input_permutation, input_permutation);
92  }
93 
94  // Basic test of left join ordering. Equal table sizes.
95  {
96  auto a1 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kINT, true}, 0, 0, 0);
97  auto a2 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kINT, true}, 1, 1, 1);
98  auto op = std::make_shared<Analyzer::BinOper>(kINT, kGT, kONE, a1, a2);
99 
100  JoinCondition jc{{op}, JoinType::LEFT};
101  JoinQualsPerNestingLevel nesting_levels;
102  nesting_levels.push_back(jc);
103 
104  size_t number_of_join_tables{2};
105  std::vector<InputTableInfo> viti(number_of_join_tables);
106 
107  auto input_permutation = get_node_input_permutation(nesting_levels, viti, nullptr);
108  decltype(input_permutation) expected_input_permutation{0, 1};
109  ASSERT_EQ(expected_input_permutation, input_permutation);
110  }
111 
112  // Basic test of left join ordering. Descending table sizes.
113  {
114  auto a1 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kINT, true}, 0, 0, 0);
115  auto a2 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kINT, true}, 1, 1, 1);
116  auto op = std::make_shared<Analyzer::BinOper>(kINT, kGT, kONE, a1, a2);
117 
118  JoinCondition jc{{op}, JoinType::LEFT};
119  JoinQualsPerNestingLevel nesting_levels;
120  nesting_levels.push_back(jc);
121 
122  size_t number_of_join_tables{2};
123  std::vector<InputTableInfo> viti(number_of_join_tables);
124  viti[0].info.setPhysicalNumTuples(2);
125  viti[1].info.setPhysicalNumTuples(1);
126 
127  auto input_permutation = get_node_input_permutation(nesting_levels, viti, nullptr);
128  decltype(input_permutation) expected_input_permutation{0, 1};
129  ASSERT_EQ(expected_input_permutation, input_permutation);
130  }
131 
132  // Basic test of left join ordering. Ascending table sizes.
133  {
134  auto a1 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kINT, true}, 0, 0, 0);
135  auto a2 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kINT, true}, 1, 1, 1);
136  auto op = std::make_shared<Analyzer::BinOper>(kINT, kGT, kONE, a1, a2);
137 
138  JoinCondition jc{{op}, JoinType::LEFT};
139  JoinQualsPerNestingLevel nesting_levels;
140  nesting_levels.push_back(jc);
141 
142  size_t number_of_join_tables{2};
143  std::vector<InputTableInfo> viti(number_of_join_tables);
144  viti[0].info.setPhysicalNumTuples(1);
145  viti[1].info.setPhysicalNumTuples(2);
146 
147  auto input_permutation = get_node_input_permutation(nesting_levels, viti, nullptr);
148  decltype(input_permutation) expected_input_permutation{0, 1};
149  ASSERT_EQ(expected_input_permutation, input_permutation);
150  }
151 
152  // Basic test of inner geojoin ordering. Equal table sizes.
153  {
154  auto a1 =
155  std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kMULTIPOLYGON, true}, 1, 1, 1);
156  auto a2 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kPOINT, true}, 0, 0, 0);
157  std::vector<std::shared_ptr<Analyzer::Expr>> geoargs = {a1, a2};
158  auto op = std::make_shared<Analyzer::FunctionOper>(
159  SQLTypeInfo{kBOOLEAN, true}, "ST_Contains_MultiPolygon_Point", geoargs);
160 
161  JoinCondition jc{{op}, JoinType::INNER};
162  JoinQualsPerNestingLevel nesting_levels;
163  nesting_levels.push_back(jc);
164 
165  size_t number_of_join_tables{2};
166  std::vector<InputTableInfo> viti(number_of_join_tables);
167 
168  auto input_permutation = get_node_input_permutation(nesting_levels, viti, nullptr);
169  decltype(input_permutation) expected_input_permutation{0, 1};
170  ASSERT_EQ(expected_input_permutation, input_permutation);
171  }
172 
173  // Basic test of inner geojoin ordering. Descending table sizes.
174  {
175  auto a1 =
176  std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kMULTIPOLYGON, true}, 1, 1, 1);
177  auto a2 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kPOINT, true}, 0, 0, 0);
178  std::vector<std::shared_ptr<Analyzer::Expr>> geoargs = {a1, a2};
179  auto op = std::make_shared<Analyzer::FunctionOper>(
180  SQLTypeInfo{kBOOLEAN, true}, "ST_Contains_MultiPolygon_Point", geoargs);
181 
182  JoinCondition jc{{op}, JoinType::INNER};
183  JoinQualsPerNestingLevel nesting_levels;
184  nesting_levels.push_back(jc);
185 
186  size_t number_of_join_tables{2};
187  std::vector<InputTableInfo> viti(number_of_join_tables);
188  viti[0].info.setPhysicalNumTuples(2);
189  viti[1].info.setPhysicalNumTuples(1);
190 
191  auto input_permutation = get_node_input_permutation(nesting_levels, viti, nullptr);
192  decltype(input_permutation) expected_input_permutation{0, 1};
193  ASSERT_EQ(expected_input_permutation, input_permutation);
194  }
195 
196  // Basic test of inner geojoin ordering. Ascending table sizes.
197  {
198  auto a1 =
199  std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kMULTIPOLYGON, true}, 1, 1, 1);
200  auto a2 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kPOINT, true}, 0, 0, 0);
201  std::vector<std::shared_ptr<Analyzer::Expr>> geoargs = {a1, a2};
202  auto op = std::make_shared<Analyzer::FunctionOper>(
203  SQLTypeInfo{kBOOLEAN, true}, "ST_Contains_MultiPolygon_Point", geoargs);
204 
205  JoinCondition jc{{op}, JoinType::INNER};
206  JoinQualsPerNestingLevel nesting_levels;
207  nesting_levels.push_back(jc);
208 
209  size_t number_of_join_tables{2};
210  std::vector<InputTableInfo> viti(number_of_join_tables);
211  viti[0].info.setPhysicalNumTuples(1);
212  viti[1].info.setPhysicalNumTuples(2);
213 
214  auto input_permutation = get_node_input_permutation(nesting_levels, viti, nullptr);
215  decltype(input_permutation) expected_input_permutation{0, 1};
216  ASSERT_EQ(expected_input_permutation, input_permutation);
217  }
218 }
219 
220 TEST(Ordering, Triple) {
221  // Triple test of inner join ordering. Equal table sizes.
222  {
223  auto a1 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kINT, true}, 0, 0, 0);
224  auto a2 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kINT, true}, 1, 1, 1);
225  auto a3 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kINT, true}, 2, 2, 2);
226  auto op1 = std::make_shared<Analyzer::BinOper>(kINT, kEQ, kONE, a1, a2);
227  auto op2 = std::make_shared<Analyzer::BinOper>(kINT, kEQ, kONE, a2, a3);
228 
229  JoinCondition jc1{{op1}, JoinType::INNER};
230  JoinCondition jc2{{op2}, JoinType::INNER};
231  JoinQualsPerNestingLevel nesting_levels;
232  nesting_levels.push_back(jc1);
233  nesting_levels.push_back(jc2);
234 
235  size_t number_of_join_tables{3};
236  std::vector<InputTableInfo> viti(number_of_join_tables);
237 
238  auto input_permutation = get_node_input_permutation(nesting_levels, viti, nullptr);
239  decltype(input_permutation) expected_input_permutation{0, 1, 2};
240  ASSERT_EQ(expected_input_permutation, input_permutation);
241  }
242 
243  // Triple test of inner join ordering. Descending table sizes.
244  {
245  auto a1 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kINT, true}, 0, 0, 0);
246  auto a2 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kINT, true}, 1, 1, 1);
247  auto a3 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kINT, true}, 2, 2, 2);
248  auto op1 = std::make_shared<Analyzer::BinOper>(kINT, kEQ, kONE, a1, a2);
249  auto op2 = std::make_shared<Analyzer::BinOper>(kINT, kEQ, kONE, a2, a3);
250 
251  JoinCondition jc1{{op1}, JoinType::INNER};
252  JoinCondition jc2{{op2}, JoinType::INNER};
253  JoinQualsPerNestingLevel nesting_levels;
254  nesting_levels.push_back(jc1);
255  nesting_levels.push_back(jc2);
256 
257  size_t number_of_join_tables{3};
258  std::vector<InputTableInfo> viti(number_of_join_tables);
259  viti[0].info.setPhysicalNumTuples(3);
260  viti[1].info.setPhysicalNumTuples(2);
261  viti[2].info.setPhysicalNumTuples(1);
262 
263  auto input_permutation = get_node_input_permutation(nesting_levels, viti, nullptr);
264  decltype(input_permutation) expected_input_permutation{0, 1, 2};
265  ASSERT_EQ(expected_input_permutation, input_permutation);
266  }
267 
268  // Triple test of inner join ordering. Ascending table sizes.
269  {
270  auto a1 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kINT, true}, 0, 0, 0);
271  auto a2 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kINT, true}, 1, 1, 1);
272  auto a3 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kINT, true}, 2, 2, 2);
273  auto op1 = std::make_shared<Analyzer::BinOper>(kINT, kEQ, kONE, a1, a2);
274  auto op2 = std::make_shared<Analyzer::BinOper>(kINT, kEQ, kONE, a2, a3);
275 
276  JoinCondition jc1{{op1}, JoinType::INNER};
277  JoinCondition jc2{{op2}, JoinType::INNER};
278  JoinQualsPerNestingLevel nesting_levels;
279  nesting_levels.push_back(jc1);
280  nesting_levels.push_back(jc2);
281 
282  size_t number_of_join_tables{3};
283  std::vector<InputTableInfo> viti(number_of_join_tables);
284  viti[0].info.setPhysicalNumTuples(1);
285  viti[1].info.setPhysicalNumTuples(2);
286  viti[2].info.setPhysicalNumTuples(3);
287 
288  auto input_permutation = get_node_input_permutation(nesting_levels, viti, nullptr);
289  decltype(input_permutation) expected_input_permutation{2, 1, 0};
290  ASSERT_EQ(expected_input_permutation, input_permutation);
291  }
292 
293  // Triple test of left join ordering. Equal table sizes.
294  {
295  auto a1 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kINT, true}, 0, 0, 0);
296  auto a2 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kINT, true}, 1, 1, 1);
297  auto a3 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kINT, true}, 2, 2, 2);
298  auto op1 = std::make_shared<Analyzer::BinOper>(kINT, kEQ, kONE, a1, a2);
299  auto op2 = std::make_shared<Analyzer::BinOper>(kINT, kEQ, kONE, a2, a3);
300 
301  JoinCondition jc1{{op1}, JoinType::LEFT};
302  JoinCondition jc2{{op2}, JoinType::LEFT};
303  JoinQualsPerNestingLevel nesting_levels;
304  nesting_levels.push_back(jc1);
305  nesting_levels.push_back(jc2);
306 
307  size_t number_of_join_tables{3};
308  std::vector<InputTableInfo> viti(number_of_join_tables);
309 
310  auto input_permutation = get_node_input_permutation(nesting_levels, viti, nullptr);
311  decltype(input_permutation) expected_input_permutation{0, 1, 2};
312  ASSERT_EQ(expected_input_permutation, input_permutation);
313  }
314 
315  // Triple test of left join ordering. Descending table sizes.
316  {
317  auto a1 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kINT, true}, 0, 0, 0);
318  auto a2 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kINT, true}, 1, 1, 1);
319  auto a3 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kINT, true}, 2, 2, 2);
320  auto op1 = std::make_shared<Analyzer::BinOper>(kINT, kEQ, kONE, a1, a2);
321  auto op2 = std::make_shared<Analyzer::BinOper>(kINT, kEQ, kONE, a2, a3);
322 
323  JoinCondition jc1{{op1}, JoinType::LEFT};
324  JoinCondition jc2{{op2}, JoinType::LEFT};
325  JoinQualsPerNestingLevel nesting_levels;
326  nesting_levels.push_back(jc1);
327  nesting_levels.push_back(jc2);
328 
329  size_t number_of_join_tables{3};
330  std::vector<InputTableInfo> viti(number_of_join_tables);
331  viti[0].info.setPhysicalNumTuples(3);
332  viti[1].info.setPhysicalNumTuples(2);
333  viti[2].info.setPhysicalNumTuples(1);
334 
335  auto input_permutation = get_node_input_permutation(nesting_levels, viti, nullptr);
336  decltype(input_permutation) expected_input_permutation{0, 1, 2};
337  ASSERT_EQ(expected_input_permutation, input_permutation);
338  }
339 
340  // Triple test of left join ordering. Ascending table sizes.
341  {
342  auto a1 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kINT, true}, 0, 0, 0);
343  auto a2 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kINT, true}, 1, 1, 1);
344  auto a3 = std::make_shared<Analyzer::ColumnVar>(SQLTypeInfo{kINT, true}, 2, 2, 2);
345  auto op1 = std::make_shared<Analyzer::BinOper>(kINT, kEQ, kONE, a1, a2);
346  auto op2 = std::make_shared<Analyzer::BinOper>(kINT, kEQ, kONE, a2, a3);
347 
348  JoinCondition jc1{{op1}, JoinType::LEFT};
349  JoinCondition jc2{{op2}, JoinType::LEFT};
350  JoinQualsPerNestingLevel nesting_levels;
351  nesting_levels.push_back(jc1);
352  nesting_levels.push_back(jc2);
353 
354  size_t number_of_join_tables{3};
355  std::vector<InputTableInfo> viti(number_of_join_tables);
356  viti[0].info.setPhysicalNumTuples(1);
357  viti[1].info.setPhysicalNumTuples(2);
358  viti[2].info.setPhysicalNumTuples(3);
359 
360  auto input_permutation = get_node_input_permutation(nesting_levels, viti, nullptr);
361  decltype(input_permutation) expected_input_permutation{0, 1, 2};
362  ASSERT_EQ(expected_input_permutation, input_permutation);
363  }
364 }
365 
366 int main(int argc, char** argv) {
368  testing::InitGoogleTest(&argc, argv);
369 
370  int err{0};
371  try {
372  err = RUN_ALL_TESTS();
373  } catch (const std::exception& e) {
374  LOG(ERROR) << e.what();
375  }
376  return err;
377 }
int main(int argc, char **argv)
#define LOG(tag)
Definition: Logger.h:182
Definition: sqldefs.h:30
std::vector< JoinCondition > JoinQualsPerNestingLevel
std::vector< node_t > get_node_input_permutation(const JoinQualsPerNestingLevel &left_deep_join_quals, const std::vector< InputTableInfo > &table_infos, const Executor *executor)
Definition: sqldefs.h:34
Definition: sqldefs.h:69
TEST(Ordering, Basic)
void init_logger_stderr_only(int argc, char const *const *argv)
Definition: TestHelpers.h:194
Definition: sqltypes.h:47