OmniSciDB  b28c0d5765
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TestServer.java
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, 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 package com.mapd.parser.server.test;
18 
19 import static org.junit.Assert.*;
20 
23 
24 import org.apache.thrift.TException;
25 import org.apache.thrift.protocol.TBinaryProtocol;
26 import org.apache.thrift.protocol.TProtocol;
27 import org.apache.thrift.transport.TSocket;
28 import org.apache.thrift.transport.TTransport;
29 import org.junit.AfterClass;
30 import org.junit.BeforeClass;
31 import org.junit.Ignore;
32 import org.junit.Test;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 
36 import java.util.ArrayList;
37 import java.util.Random;
38 import java.util.concurrent.ExecutorService;
39 import java.util.concurrent.Executors;
40 
41 import ai.heavy.thrift.calciteserver.CalciteServer;
42 import ai.heavy.thrift.calciteserver.TOptimizationOption;
43 import ai.heavy.thrift.calciteserver.TPlanResult;
44 import ai.heavy.thrift.calciteserver.TQueryParsingOption;
45 
46 public class TestServer {
47  private final static Logger HEAVYDBLOGGER = LoggerFactory.getLogger(TestServer.class);
48  private final static int TEST_THREAD_COUNT = 3;
49  private volatile int threadsRun = 0;
50  private volatile boolean threadHadFailure = false;
51  private volatile AssertionError ae;
52  private static CalciteServerWrapper csw = null;
53  private static SockTransportProperties client_skT = null;
54  private static SockTransportProperties server_skT = null;
55 
56  @BeforeClass
57  public static void startServer() {
58  csw = new CalciteServerWrapper(11000, 11001, "/data", null, client_skT, server_skT);
59  new Thread(csw).start();
60  }
61 
62  @AfterClass
63  public static void stopServer() {
64  csw.stopServer();
65  }
66 
67  @Ignore
68  public void testThreadedCall() {
69  final ExecutorService pool = Executors.newFixedThreadPool(TEST_THREAD_COUNT);
70 
71  Runnable r = new Runnable() {
72  @Override
73  public void run() {
74  try {
75  for (int i = 1; i <= 5; i++) {
77  }
78  } catch (AssertionError x) {
79  HEAVYDBLOGGER.error("error during Runnable");
80  threadHadFailure = true;
81  ae = x;
82  }
83  threadsRun++;
85  pool.shutdown();
86  }
87  }
88  };
89 
90  for (int i = 0; i < TEST_THREAD_COUNT; i++) {
91  pool.submit(r);
92  }
93  while (!pool.isShutdown()) {
94  // stay alive
95  }
96  if (threadHadFailure) {
97  throw ae;
98  }
99  }
100 
101  @Ignore
102  public void testSimpleCall() {
103  callCalciteCheck("Select ENAME from EMP",
104  "{\n"
105  + " \"rels\": [\n"
106  + " {\n"
107  + " \"id\": \"0\",\n"
108  + " \"relOp\": \"LogicalTableScan\",\n"
109  + " \"fieldNames\": [\n"
110  + " \"EMPNO\",\n"
111  + " \"ENAME\",\n"
112  + " \"JOB\",\n"
113  + " \"MGR\",\n"
114  + " \"HIREDATE\",\n"
115  + " \"SAL\",\n"
116  + " \"COMM\",\n"
117  + " \"DEPTNO\",\n"
118  + " \"SLACKER\",\n"
119  + " \"SLACKARR1\",\n"
120  + " \"SLACKARR2\"\n"
121  + " ],\n"
122  + " \"table\": [\n"
123  + " \"CATALOG\",\n"
124  + " \"SALES\",\n"
125  + " \"EMP\"\n"
126  + " ],\n"
127  + " \"inputs\": []\n"
128  + " },\n"
129  + " {\n"
130  + " \"id\": \"1\",\n"
131  + " \"relOp\": \"LogicalProject\",\n"
132  + " \"fields\": [\n"
133  + " \"ENAME\"\n"
134  + " ],\n"
135  + " \"exprs\": [\n"
136  + " {\n"
137  + " \"input\": 1\n"
138  + " }\n"
139  + " ]\n"
140  + " }\n"
141  + " ]\n"
142  + "}");
143  }
144 
145  @Ignore
146  public void testRandomCall() {
148  }
149 
150  private void randomCalciteCall() {
151  Random r = new Random();
152  int aliasID = r.nextInt(100000) + 1000000;
154  String.format(
155  "Select TABALIAS%d.ENAME AS COLALIAS%d from EMP TABALIAS%d LIMIT %d",
156  aliasID,
157  aliasID,
158  aliasID,
159  aliasID),
160  String.format("{\n"
161  + " \"rels\": [\n"
162  + " {\n"
163  + " \"id\": \"0\",\n"
164  + " \"relOp\": \"LogicalTableScan\",\n"
165  + " \"fieldNames\": [\n"
166  + " \"EMPNO\",\n"
167  + " \"ENAME\",\n"
168  + " \"JOB\",\n"
169  + " \"MGR\",\n"
170  + " \"HIREDATE\",\n"
171  + " \"SAL\",\n"
172  + " \"COMM\",\n"
173  + " \"DEPTNO\",\n"
174  + " \"SLACKER\",\n"
175  + " \"SLACKARR1\",\n"
176  + " \"SLACKARR2\"\n"
177  + " ],\n"
178  + " \"table\": [\n"
179  + " \"CATALOG\",\n"
180  + " \"SALES\",\n"
181  + " \"EMP\"\n"
182  + " ],\n"
183  + " \"inputs\": []\n"
184  + " },\n"
185  + " {\n"
186  + " \"id\": \"1\",\n"
187  + " \"relOp\": \"LogicalProject\",\n"
188  + " \"fields\": [\n"
189  + " \"COLALIAS%d\"\n"
190  + " ],\n"
191  + " \"exprs\": [\n"
192  + " {\n"
193  + " \"input\": 1\n"
194  + " }\n"
195  + " ]\n"
196  + " },\n"
197  + " {\n"
198  + " \"id\": \"2\",\n"
199  + " \"relOp\": \"LogicalSort\",\n"
200  + " \"collation\": [],\n"
201  + " \"fetch\": {\n"
202  + " \"literal\": %d,\n"
203  + " \"type\": \"DECIMAL\",\n"
204  + " \"scale\": 0,\n"
205  + " \"precision\": 7,\n"
206  + " \"type_scale\": 0,\n"
207  + " \"type_precision\": 10\n"
208  + " }\n"
209  + " },\n"
210  + " {\n"
211  + " \"id\": \"3\",\n"
212  + " \"relOp\": \"LogicalProject\",\n"
213  + " \"fields\": [\n"
214  + " \"COLALIAS%d\"\n"
215  + " ],\n"
216  + " \"exprs\": [\n"
217  + " {\n"
218  + " \"input\": 0\n"
219  + " }\n"
220  + " ]\n"
221  + " },\n"
222  + " {\n"
223  + " \"id\": \"4\",\n"
224  + " \"relOp\": \"LogicalSort\",\n"
225  + " \"collation\": [],\n"
226  + " \"fetch\": {\n"
227  + " \"literal\": %d,\n"
228  + " \"type\": \"DECIMAL\",\n"
229  + " \"scale\": 0,\n"
230  + " \"precision\": 7,\n"
231  + " \"type_scale\": 0,\n"
232  + " \"type_precision\": 10\n"
233  + " }\n"
234  + " }\n"
235  + " ]\n"
236  + "}",
237  aliasID,
238  aliasID,
239  aliasID,
240  aliasID));
241  }
242 
243  private void callCalciteCheck(String query, String result) {
244  try {
245  TTransport transport;
246  transport = new TSocket("localhost", 11000);
247  transport.open();
248  TProtocol protocol = new TBinaryProtocol(transport);
249  CalciteServer.Client client = new CalciteServer.Client(protocol);
250 
251  TQueryParsingOption queryParsingOption = new TQueryParsingOption();
252  queryParsingOption.legacy_syntax = false;
253  queryParsingOption.is_explain = false;
254  queryParsingOption.check_privileges = false;
255 
256  TOptimizationOption optimizationOption = new TOptimizationOption();
257  optimizationOption.is_view_optimize = false;
258  optimizationOption.enable_watchdog = false;
259  optimizationOption.filter_push_down_info = new ArrayList<>();
260 
261  TPlanResult algebra = client.process("user",
262  "passwd",
263  "SALES",
264  query,
265  queryParsingOption,
266  optimizationOption,
267  null);
268  transport.close();
269  try {
270  assertEquals(algebra.plan_result, result);
271  } catch (AssertionError s) {
272  HEAVYDBLOGGER.error("error during callCalciteCheck");
273  throw s;
274  }
275  } catch (TException x) {
276  fail(x.toString());
277  }
278  }
279 }
static CalciteServerWrapper csw
Definition: TestServer.java:52
void callCalciteCheck(String query, String result)
static SockTransportProperties client_skT
Definition: TestServer.java:53
static bool run
static SockTransportProperties server_skT
Definition: TestServer.java:54