private static Relation from(String catalog, SchemaTableName table) { return table(QualifiedName.of(catalog, table.getSchemaName(), table.getTableName())); }
@Test public void testPrepare() { Query query = simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("foo"))); String sqlString = "PREPARE my_query FROM SELECT * FROM foo"; Map<String, String> statements = executePrepare("my_query", query, sqlString, TEST_SESSION); assertEquals(statements, ImmutableMap.of("my_query", "SELECT *\nFROM\n foo\n")); }
@Test public void testSelectStatement() { PreparedQuery preparedQuery = QUERY_PREPARER.prepareQuery(TEST_SESSION, "SELECT * FROM foo"); assertEquals(preparedQuery.getStatement(), simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("foo")))); }
@Test public void testDoubleInQuery() { assertStatement("SELECT 123.456E7 FROM DUAL", simpleQuery( selectList(new DoubleLiteral("123.456E7")), table(QualifiedName.of("DUAL")))); }
@Test public void testDelete() { assertStatement("DELETE FROM t", new Delete(table(QualifiedName.of("t")), Optional.empty())); assertStatement("DELETE FROM \"awesome table\"", new Delete(table(QualifiedName.of("awesome table")), Optional.empty())); assertStatement("DELETE FROM t WHERE a = b", new Delete(table(QualifiedName.of("t")), Optional.of( new ComparisonExpression(ComparisonExpression.Operator.EQUAL, new Identifier("a"), new Identifier("b"))))); }
@Test public void testPrepareNameExists() { Session session = testSessionBuilder() .addPreparedStatement("my_query", "SELECT bar, baz from foo") .build(); Query query = simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("foo"))); String sqlString = "PREPARE my_query FROM SELECT * FROM foo"; Map<String, String> statements = executePrepare("my_query", query, sqlString, session); assertEquals(statements, ImmutableMap.of("my_query", "SELECT *\nFROM\n foo\n")); }
@Test public void testExplainAnalyzeVerbose() { assertStatement("EXPLAIN ANALYZE VERBOSE SELECT * FROM t", new Explain(simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t"))), true, true, ImmutableList.of())); }
@Test public void testExplainAnalyze() { assertStatement("EXPLAIN ANALYZE SELECT * FROM t", new Explain(simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t"))), true, false, ImmutableList.of())); }
@Test public void testExplainVerbose() { assertStatement("EXPLAIN VERBOSE SELECT * FROM t", new Explain(simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t"))), false, true, ImmutableList.of())); }
@Test public void testInsertInto() { QualifiedName table = QualifiedName.of("a"); Query query = simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t"))); assertStatement("INSERT INTO a SELECT * FROM t", new Insert(table, Optional.empty(), query)); assertStatement("INSERT INTO a (c1, c2) SELECT * FROM t", new Insert(table, Optional.of(ImmutableList.of(identifier("c1"), identifier("c2"))), query)); }
@Test public void testExecuteStatement() { Session session = testSessionBuilder() .addPreparedStatement("my_query", "SELECT * FROM foo") .build(); PreparedQuery preparedQuery = QUERY_PREPARER.prepareQuery(session, "EXECUTE my_query"); assertEquals(preparedQuery.getStatement(), simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("foo")))); }
@Test public void testExplainVerboseTypeLogical() { assertStatement("EXPLAIN VERBOSE (type LOGICAL) SELECT * FROM t", new Explain(simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t"))), false, true, ImmutableList.of(new ExplainType(ExplainType.Type.LOGICAL)))); }
@Test public void testExplainAnalyzeTypeDistributed() { assertStatement("EXPLAIN ANALYZE (type DISTRIBUTED) SELECT * FROM t", new Explain(simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t"))), true, false, ImmutableList.of(new ExplainType(ExplainType.Type.DISTRIBUTED)))); }
@Test public void testExplainAnalyzeVerboseTypeDistributed() { assertStatement("EXPLAIN ANALYZE VERBOSE (type DISTRIBUTED) SELECT * FROM t", new Explain(simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t"))), true, true, ImmutableList.of(new ExplainType(ExplainType.Type.DISTRIBUTED)))); }
@Test public void testPrepare() { assertStatement("PREPARE myquery FROM select * from foo", new Prepare(identifier("myquery"), simpleQuery( selectList(new AllColumns()), table(QualifiedName.of("foo"))))); }
@Test public void testPrepareWithParameters() { assertStatement("PREPARE myquery FROM SELECT ?, ? FROM foo", new Prepare(identifier("myquery"), simpleQuery( selectList(new Parameter(0), new Parameter(1)), table(QualifiedName.of("foo"))))); }
@Test public void testWith() { assertStatement("WITH a (t, u) AS (SELECT * FROM x), b AS (SELECT * FROM y) TABLE z", new Query(Optional.of(new With(false, ImmutableList.of( new WithQuery(identifier("a"), simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("x"))), Optional.of(ImmutableList.of(identifier("t"), identifier("u")))), new WithQuery(identifier("b"), simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("y"))), Optional.empty())))), new Table(QualifiedName.of("z")), Optional.empty(), Optional.empty())); assertStatement("WITH RECURSIVE a AS (SELECT * FROM x) TABLE y", new Query(Optional.of(new With(true, ImmutableList.of( new WithQuery(identifier("a"), simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("x"))), Optional.empty())))), new Table(QualifiedName.of("y")), Optional.empty(), Optional.empty())); }
@Test public void testExplain() { assertStatement("EXPLAIN SELECT * FROM t", new Explain(simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t"))), false, false, ImmutableList.of())); assertStatement("EXPLAIN (TYPE LOGICAL) SELECT * FROM t", new Explain( simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t"))), false, false, ImmutableList.of(new ExplainType(ExplainType.Type.LOGICAL)))); assertStatement("EXPLAIN (TYPE LOGICAL, FORMAT TEXT) SELECT * FROM t", new Explain( simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t"))), false, false, ImmutableList.of( new ExplainType(ExplainType.Type.LOGICAL), new ExplainFormat(ExplainFormat.Type.TEXT)))); }
@Test public void testCreateView() { Query query = simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t"))); assertStatement("CREATE VIEW a AS SELECT * FROM t", new CreateView(QualifiedName.of("a"), query, false)); assertStatement("CREATE OR REPLACE VIEW a AS SELECT * FROM t", new CreateView(QualifiedName.of("a"), query, true)); assertStatement("CREATE VIEW bar.foo AS SELECT * FROM t", new CreateView(QualifiedName.of("bar", "foo"), query, false)); assertStatement("CREATE VIEW \"awesome view\" AS SELECT * FROM t", new CreateView(QualifiedName.of("awesome view"), query, false)); assertStatement("CREATE VIEW \"awesome schema\".\"awesome view\" AS SELECT * FROM t", new CreateView(QualifiedName.of("awesome schema", "awesome view"), query, false)); }
@Test public void testQuantifiedComparison() { assertExpression("col1 < ANY (SELECT col2 FROM table1)", new QuantifiedComparisonExpression( LESS_THAN, QuantifiedComparisonExpression.Quantifier.ANY, identifier("col1"), new SubqueryExpression(simpleQuery(selectList(new SingleColumn(identifier("col2"))), table(QualifiedName.of("table1")))))); assertExpression("col1 = ALL (VALUES ROW(1), ROW(2))", new QuantifiedComparisonExpression( ComparisonExpression.Operator.EQUAL, QuantifiedComparisonExpression.Quantifier.ALL, identifier("col1"), new SubqueryExpression(query(values(row(new LongLiteral("1")), row(new LongLiteral("2"))))))); assertExpression("col1 >= SOME (SELECT 10)", new QuantifiedComparisonExpression( ComparisonExpression.Operator.GREATER_THAN_OR_EQUAL, QuantifiedComparisonExpression.Quantifier.SOME, identifier("col1"), new SubqueryExpression(simpleQuery(selectList(new LongLiteral("10")))))); }