1 package com.mapd.parser.server.test;
3 import static org.junit.Assert.assertEquals;
5 import static java.util.Arrays.asList;
7 import com.google.common.collect.ImmutableList;
10 import org.apache.calcite.avatica.util.Casing;
11 import org.apache.calcite.linq4j.tree.Expression;
14 import org.apache.calcite.rel.type.RelProtoDataType;
15 import org.apache.calcite.schema.Function;
16 import org.apache.calcite.schema.Schema;
17 import org.apache.calcite.schema.SchemaPlus;
18 import org.apache.calcite.schema.SchemaVersion;
19 import org.apache.calcite.schema.Table;
20 import org.apache.calcite.sql.fun.SqlStdOperatorTable;
21 import org.apache.calcite.sql.parser.SqlParser;
22 import org.apache.calcite.tools.FrameworkConfig;
23 import org.apache.calcite.tools.Frameworks;
24 import org.apache.calcite.tools.Planner;
25 import org.junit.Test;
27 import java.util.Arrays;
28 import java.util.Collection;
29 import java.util.Collections;
30 import java.util.HashSet;
31 import java.util.List;
43 return Collections.emptySet();
47 public RelProtoDataType
getType(String arg0) {
53 return Collections.emptySet();
58 return Collections.emptySet();
63 return Collections.emptySet();
73 return Collections.emptySet();
96 final SchemaPlus rootSchema = Frameworks.createRootSchema(
true);
97 final FrameworkConfig config =
98 Frameworks.newConfigBuilder()
99 .defaultSchema(rootSchema.add(
"heavydb", heavydb))
102 .parserConfig(SqlParser.configBuilder()
103 .setUnquotedCasing(Casing.UNCHANGED)
104 .setCaseSensitive(
false)
111 return ImmutableList.copyOf(asList(vals));
115 return ImmutableList.of();
118 public static Set<ImmutableList<String>>
asSet(ImmutableList<String> val) {
119 Set<ImmutableList<String>> ret =
new HashSet<ImmutableList<String>>();
124 public static Set<ImmutableList<String>>
asSet(ImmutableList<String>... vals) {
125 Set<ImmutableList<String>> ret =
new HashSet<ImmutableList<String>>();
126 ret.addAll(Arrays.asList(vals));
130 public static Set<ImmutableList<String>>
asSet() {
131 Set<ImmutableList<String>> ret =
new HashSet<ImmutableList<String>>();
135 public void testSelect(String sql, Set<ImmutableList<String>> expectedSelects)
140 assertEquals(
"selects", expectedSelects, capturer.
selects);
147 Set<ImmutableList<String>> expectedUpdates,
148 Set<ImmutableList<String>> expectedSelects) throws Exception {
152 assertEquals(
"selects", expectedSelects, capturer.
selects);
154 assertEquals(
"updates", expectedUpdates, capturer.
updates);
159 Set<ImmutableList<String>> expectedInserts,
160 Set<ImmutableList<String>> expectedSelects) throws Exception {
164 assertEquals(
"selects", expectedSelects, capturer.
selects);
165 assertEquals(
"inserts", expectedInserts, capturer.
inserts);
171 Set<ImmutableList<String>> expectedDeletes,
172 Set<ImmutableList<String>> expectedSelects) throws Exception {
176 assertEquals(
"selects", expectedSelects, capturer.
selects);
179 assertEquals(
"deletes", expectedDeletes, capturer.
deletes);
184 String sql =
"SELECT * FROM sales";
187 sql =
"SELECT * FROM sales AS s";
190 sql =
"SELECT * FROM sales AS s, reports AS r WHERE s.id = r.id";
193 sql =
"SELECT * FROM sales AS s left outer join reports AS r on s.id = r.id";
196 sql =
"SELECT *, (SELECT sum(val) FROM marketing m WHERE m.id=a.id) FROM sales AS s left outer join reports AS r on s.id = r.id";
202 sql =
"SELECT * FROM sales UNION SELECT * FROM reports UNION SELECT * FROM marketing";
208 sql =
"SELECT COUNT(*) AS n, str FROM query_rewrite_test WHERE str IN ('str2', 'str99') GROUP BY str HAVING n > 0 ORDER BY n DESC";
211 sql =
"SELECT str, SUM(y) as total_y FROM test GROUP BY str ORDER BY total_y DESC, str LIMIT 1";
214 sql =
"SELECT str FROM (SELECT str, SUM(y) as total_y FROM test GROUP BY str ORDER BY total_y DESC, str LIMIT 1)";
217 sql =
"SELECT deptno, dname FROM (SELECT * from dept) AS view_name LIMIT 10";
220 sql =
"WITH d1 AS (SELECT deptno, dname FROM dept LIMIT 10) SELECT ename, dname FROM emp, d1 WHERE emp.deptno = d1.deptno ORDER BY ename ASC LIMIT 10";
226 String sql =
"SELECT * FROM mapd.sales";
229 sql =
"SELECT * FROM mapd.sales AS s";
232 sql =
"SELECT * FROM mapd.sales AS s, mapd.reports AS r WHERE s.id = r.id";
236 sql =
"SELECT * FROM mapd.sales AS s left outer join mapd.reports AS r on s.id = r.id";
240 sql =
"SELECT *, (SELECT sum(val) FROM mapd.marketing m WHERE m.id=a.id) FROM mapd.sales AS s left outer join mapd.reports AS r on s.id = r.id";
246 sql =
"SELECT * FROM mapd.sales UNION SELECT * FROM mapd.reports UNION SELECT * FROM mapd.marketing";
252 sql =
"SELECT COUNT(*) AS n, str FROM mapd.query_rewrite_test WHERE str IN ('str2', 'str99') GROUP BY str HAVING n > 0 ORDER BY n DESC";
255 sql =
"SELECT str, SUM(y) as total_y FROM mapd.test GROUP BY str ORDER BY total_y DESC, str LIMIT 1";
258 sql =
"SELECT str FROM (SELECT str, SUM(y) as total_y FROM mapd.test GROUP BY str ORDER BY total_y DESC, str LIMIT 1)";
261 sql =
"SELECT deptno, dname FROM (SELECT * from mapd.dept) AS view_name LIMIT 10";
264 sql =
"WITH d1 AS (SELECT deptno, dname FROM mapd.dept LIMIT 10) SELECT ename, dname FROM mapd.emp, d1 WHERE emp.deptno = d1.deptno ORDER BY ename ASC LIMIT 10";
271 String sql =
"INSERT INTO sales VALUES(10)";
274 sql =
"INSERT INTO sales(id, target) VALUES(10, 21321)";
277 sql =
"INSERT INTO sales(id, target) VALUES(10, (SELECT max(r.val) FROM reports AS r))";
280 sql =
"INSERT INTO sales(id, target) VALUES((SELECT m.id FROM marketing m), (SELECT max(r.val) FROM reports AS r))";
288 String sql =
"INSERT INTO mapd.sales VALUES(10)";
291 sql =
"INSERT INTO mapd.sales(id, target) VALUES(10, 21321)";
294 sql =
"INSERT INTO mapd.sales(id, target) VALUES(10, (SELECT max(r.val) FROM mapd.reports AS r))";
299 sql =
"INSERT INTO mapd.sales(id, target) VALUES((SELECT m.id FROM mapd.marketing m), (SELECT max(r.val) FROM mapd.reports AS r))";
308 String sql =
"UPDATE sales SET id=10";
311 sql =
"UPDATE sales SET id=10 WHERE id=1";
314 sql =
"UPDATE sales SET id=(SELECT max(r.val) FROM reports AS r)";
317 sql =
"UPDATE sales SET id=(SELECT max(r.val) FROM reports AS r) WHERE id=(SELECT max(m.val) FROM marketing AS m)";
322 sql =
"UPDATE shardkey SET y=99 WHERE x=(SELECT max(id) from v2 LIMIT 1)";
328 String sql =
"UPDATE mapd.sales SET id=10";
331 sql =
"UPDATE mapd.sales SET id=10 WHERE id=1";
334 sql =
"UPDATE mapd.sales SET id=(SELECT max(r.val) FROM mapd.reports AS r)";
339 sql =
"UPDATE mapd.sales SET id=(SELECT max(r.val) FROM mapd.reports AS r) WHERE id=(SELECT max(m.val) FROM mapd.marketing AS m)";
345 sql =
"UPDATE mapd.shardkey SET y=99 WHERE x=(SELECT max(id) from mapd.v2 LIMIT 1)";
353 String sql =
"DELETE FROM sales";
356 sql =
"DELETE FROM sales WHERE id=1";
359 sql =
"DELETE FROM sales WHERE id=(SELECT max(r.val) FROM reports AS r)";
362 sql =
"DELETE FROM sales WHERE id=(SELECT max(r.val) FROM reports AS r) AND id=(SELECT max(m.val) FROM marketing AS m)";
370 String sql =
"DELETE FROM mapd.sales";
373 sql =
"DELETE FROM mapd.sales WHERE id=1";
376 sql =
"DELETE FROM mapd.sales WHERE id=(SELECT max(r.val) FROM mapd.reports AS r)";
381 sql =
"DELETE FROM mapd.sales WHERE id=(SELECT max(r.val) FROM mapd.reports AS r) AND id=(SELECT max(m.val) FROM mapd.marketing AS m)";
static Set< ImmutableList< String > > asSet(ImmutableList< String > val)
static ImmutableList< String > asImmutableList()
Expression getExpression(SchemaPlus parentSchema, String name)
Set< ImmutableList< String > > deletes
static Set< ImmutableList< String > > asSet(ImmutableList< String >...vals)
Schema getSubSchema(String name)
void testInsert(String sql, Set< ImmutableList< String >> expectedInserts, Set< ImmutableList< String >> expectedSelects)
Set< String > getTableNames()
void testDelete(String sql, Set< ImmutableList< String >> expectedDeletes, Set< ImmutableList< String >> expectedSelects)
Set< ImmutableList< String > > updates
Collection< Function > getFunctions(String name)
static ImmutableList< String > asImmutableList(String...vals)
Set< ImmutableList< String > > inserts
Set< String > getTypeNames()
void testUpdate(String sql, Set< ImmutableList< String >> expectedUpdates, Set< ImmutableList< String >> expectedSelects)
Set< String > getFunctionNames()
Table getTable(String name)
Set< ImmutableList< String > > selects
void testSelect(String sql, Set< ImmutableList< String >> expectedSelects)
RelProtoDataType getType(String arg0)
Set< String > getSubSchemaNames()
void testSelectsWithSchema()
Schema snapshot(SchemaVersion version)
void testUpdatesWithSchema()
static Set< ImmutableList< String > > asSet()
void testInsertsWithSchema()
void testDeletesWithSchema()