public static Table table(QualifiedName name) { return new Table(name); }
@Override public Node visitShowStats(SqlBaseParser.ShowStatsContext context) { return new ShowStats(Optional.of(getLocation(context)), new Table(getQualifiedName(context.qualifiedName()))); }
@Override public Node visitTable(SqlBaseParser.TableContext context) { return new Table(getLocation(context), getQualifiedName(context.qualifiedName())); }
private String checksumSql(List<Column> columns, QualifiedName table) throws QueryRewriteException { if (columns.isEmpty()) { throw new QueryRewriteException("Table " + table + " has no columns"); } ImmutableList.Builder<SelectItem> selectItems = ImmutableList.builder(); for (Column column : columns) { Expression expression = new Identifier(column.getName()); if (column.isApproximateType()) { expression = new FunctionCall(QualifiedName.of("round"), ImmutableList.of(expression, new LongLiteral(Integer.toString(doublePrecision)))); } selectItems.add(new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(expression)))); } Select select = new Select(false, selectItems.build()); return formatSql(new QuerySpecification(select, Optional.of(new Table(table)), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty()); }
@Override public Node visitTableName(SqlBaseParser.TableNameContext context) { return new Table(getLocation(context), getQualifiedName(context.qualifiedName())); }
@Override public Node visitDelete(SqlBaseParser.DeleteContext context) { return new Delete( getLocation(context), new Table(getLocation(context), getQualifiedName(context.qualifiedName())), visitIfPresent(context.booleanExpression(), Expression.class)); }
@Test public void testShowStats() { final String[] tableNames = {"t", "s.t", "c.s.t"}; for (String fullName : tableNames) { QualifiedName qualifiedName = QualifiedName.of(Arrays.asList(fullName.split("\\."))); assertStatement(format("SHOW STATS FOR %s", qualifiedName), new ShowStats(new Table(qualifiedName))); } }
private ShowStats createShowStats(QualifiedName name, List<SelectItem> selects, Optional<Expression> where) { return new ShowStats( new TableSubquery(simpleQuery(new Select(false, selects), new Table(name), where, Optional.empty()))); }
new Join( Join.Type.CROSS, new Table(QualifiedName.of("a")), new Table(QualifiedName.of("b")), Optional.empty()), new Table(QualifiedName.of("c")), Optional.of(new JoinOn(BooleanLiteral.TRUE_LITERAL))))); assertStatement("SELECT * FROM a CROSS JOIN b NATURAL JOIN c CROSS JOIN d NATURAL JOIN e", new Join( Join.Type.CROSS, new Table(QualifiedName.of("a")), new Table(QualifiedName.of("b")), Optional.empty()), new Table(QualifiedName.of("c")), Optional.of(new NaturalJoin())), new Table(QualifiedName.of("d")), Optional.empty()), new Table(QualifiedName.of("e")), Optional.of(new NaturalJoin()))));
@Test public void testSelectWithOrderBy() { assertStatement("SELECT * FROM table1 ORDER BY a", new Query( Optional.empty(), new QuerySpecification( selectList(new AllColumns()), Optional.of(new Table(QualifiedName.of("table1"))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new OrderBy(ImmutableList.of(new SortItem( new Identifier("a"), ASCENDING, UNDEFINED)))), Optional.empty()), Optional.empty(), Optional.empty())); }
@Test public void testCreateTableAsSelect() throws Exception { handle.execute("CREATE TABLE \"my_test_table\" (column1 BIGINT, column2 DOUBLE)"); SqlParser parser = new SqlParser(); Query query = new Query(CATALOG, SCHEMA, ImmutableList.of(), "CREATE TABLE my_test_table AS SELECT 1 column1, CAST('2.0' AS DOUBLE) column2 LIMIT 1", ImmutableList.of(), null, null, ImmutableMap.of()); QueryRewriter rewriter = new QueryRewriter(parser, URL, QualifiedName.of("tmp_"), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), 1, new Duration(10, SECONDS)); Query rewrittenQuery = rewriter.shadowQuery(query); assertEquals(rewrittenQuery.getPreQueries().size(), 1); assertEquals(rewrittenQuery.getPostQueries().size(), 1); CreateTableAsSelect createTableAs = (CreateTableAsSelect) parser.createStatement(rewrittenQuery.getPreQueries().get(0)); assertEquals(createTableAs.getName().getParts().size(), 1); assertTrue(createTableAs.getName().getSuffix().startsWith("tmp_")); assertFalse(createTableAs.getName().getSuffix().contains("my_test_table")); assertEquals(statementToQueryType(parser, rewrittenQuery.getQuery()), READ); Table table = new Table(createTableAs.getName()); SingleColumn column1 = new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new Identifier("COLUMN1")))); SingleColumn column2 = new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new FunctionCall(QualifiedName.of("round"), ImmutableList.of(new Identifier("COLUMN2"), new LongLiteral("1")))))); Select select = new Select(false, ImmutableList.of(column1, column2)); QuerySpecification querySpecification = new QuerySpecification(select, Optional.of(table), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()); assertEquals(parser.createStatement(rewrittenQuery.getQuery()), new com.facebook.presto.sql.tree.Query(Optional.empty(), querySpecification, Optional.empty(), Optional.empty())); assertEquals(parser.createStatement(rewrittenQuery.getPostQueries().get(0)), new DropTable(createTableAs.getName(), true)); }
query(new Values(ImmutableList.of(new LongLiteral("1")))), Optional.of(ImmutableList.of(identifier("x"))))))), new Table(QualifiedName.of("t")), Optional.empty(), Optional.empty());
@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())); }
new Join( Join.Type.IMPLICIT, new Table(QualifiedName.of("t")), new AliasedRelation(lateralRelation, identifier("a"), ImmutableList.of(identifier("x"))), Optional.empty()))); new Join( Join.Type.CROSS, new Table(QualifiedName.of("t")), lateralRelation, Optional.empty()))); new Join( Join.Type.FULL, new Table(QualifiedName.of("t")), lateralRelation, Optional.of(new JoinOn(BooleanLiteral.TRUE_LITERAL)))));
assertEquals(insert.getColumns(), Optional.of(ImmutableList.of(identifier("b"), identifier("a"), identifier("c")))); Table table = new Table(createTable.getName()); SingleColumn columnA = new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new Identifier("A")))); SingleColumn columnB = new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new FunctionCall(QualifiedName.of("round"), ImmutableList.of(new Identifier("B"), new LongLiteral("1"))))));
@Test public void testUnnest() { assertStatement("SELECT * FROM t CROSS JOIN UNNEST(a)", simpleQuery( selectList(new AllColumns()), new Join( Join.Type.CROSS, new Table(QualifiedName.of("t")), new Unnest(ImmutableList.of(new Identifier("a")), false), Optional.empty()))); assertStatement("SELECT * FROM t CROSS JOIN UNNEST(a, b) WITH ORDINALITY", simpleQuery( selectList(new AllColumns()), new Join( Join.Type.CROSS, new Table(QualifiedName.of("t")), new Unnest(ImmutableList.of(new Identifier("a"), new Identifier("b")), true), Optional.empty()))); assertStatement("SELECT * FROM t FULL JOIN UNNEST(a) AS tmp (c) ON true", simpleQuery( selectList(new AllColumns()), new Join( Join.Type.FULL, new Table(QualifiedName.of("t")), new AliasedRelation(new Unnest(ImmutableList.of(new Identifier("a")), false), new Identifier("tmp"), ImmutableList.of(new Identifier("c"))), Optional.of(new JoinOn(BooleanLiteral.TRUE_LITERAL))))); }
new QuerySpecification( selectList(new AllColumns()), Optional.of(new Table(QualifiedName.of("table1"))), Optional.empty(), Optional.of(new GroupBy(false, ImmutableList.of(new SimpleGroupBy(ImmutableList.of(new Identifier("a")))))), new QuerySpecification( selectList(new AllColumns()), Optional.of(new Table(QualifiedName.of("table1"))), Optional.empty(), Optional.of(new GroupBy(false, ImmutableList.of( new QuerySpecification( selectList(new AllColumns()), Optional.of(new Table(QualifiedName.of("table1"))), Optional.empty(), Optional.of(new GroupBy(false, ImmutableList.of(new SimpleGroupBy(ImmutableList.of())))), new QuerySpecification( selectList(new AllColumns()), Optional.of(new Table(QualifiedName.of("table1"))), Optional.empty(), Optional.of(new GroupBy(false, ImmutableList.of(new GroupingSets(ImmutableList.of(ImmutableList.of(new Identifier("a"))))))), Optional.empty(), ImmutableList.of(QualifiedName.of("a"), QualifiedName.of("b")))), Optional.of(new Table(QualifiedName.of("table1"))), Optional.empty(), Optional.of(new GroupBy(false, ImmutableList.of(new GroupingSets(ImmutableList.of(ImmutableList.of(new Identifier("a")), ImmutableList.of(new Identifier("b"))))))),
new DereferenceExpression( new DereferenceExpression(new DereferenceExpression(new Identifier("col3"), identifier("f1")), identifier("f2")), identifier("f3"))), Optional.of(new Table(QualifiedName.of("table1"))), Optional.empty(), Optional.empty(), new DereferenceExpression(new DereferenceExpression(new SubscriptExpression(new Identifier("col3"), new LongLiteral("2")), identifier("f2")), identifier("f3")), new SubscriptExpression(new Identifier("col4"), new LongLiteral("4"))), Optional.of(new Table(QualifiedName.of("table1"))), Optional.empty(), Optional.empty(),
@Test public void testShowStats() { final String[] tableNames = {"t", "s.t", "c.s.t"}; for (String fullName : tableNames) { QualifiedName qualifiedName = QualifiedName.of(Arrays.asList(fullName.split("\\."))); assertStatement(format("SHOW STATS FOR %s", qualifiedName), new ShowStats(new Table(qualifiedName))); } }