public static Select selectAll(List<SelectItem> items) { return new Select(false, items); }
public static Select selectList(SelectItem... items) { return new Select(false, ImmutableList.copyOf(items)); }
public static Select selectList(List<Expression> expressions) { ImmutableList.Builder<SelectItem> items = ImmutableList.builder(); for (Expression expression : expressions) { items.add(new SingleColumn(expression)); } return new Select(false, items.build()); }
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 visitQuerySpecification(SqlBaseParser.QuerySpecificationContext context) { Optional<Relation> from = Optional.empty(); List<SelectItem> selectItems = visit(context.selectItem(), SelectItem.class); List<Relation> relations = visit(context.relation(), Relation.class); if (!relations.isEmpty()) { // synthesize implicit join nodes Iterator<Relation> iterator = relations.iterator(); Relation relation = iterator.next(); while (iterator.hasNext()) { relation = new Join(getLocation(context), Join.Type.IMPLICIT, relation, iterator.next(), Optional.empty()); } from = Optional.of(relation); } return new QuerySpecification( getLocation(context), new Select(getLocation(context.SELECT()), isDistinct(context.setQuantifier()), selectItems), from, visitIfPresent(context.where, Expression.class), visitIfPresent(context.groupBy(), GroupBy.class), visitIfPresent(context.having, Expression.class), Optional.empty(), Optional.empty()); }
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()))); }
@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)); }
SingleColumn columnB = new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new FunctionCall(QualifiedName.of("round"), ImmutableList.of(new Identifier("B"), new LongLiteral("1")))))); SingleColumn columnC = new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new Identifier("C")))); Select select = new Select(false, ImmutableList.of(columnA, columnB, columnC)); 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()));
public static Select selectAll(List<SelectItem> items) { return new Select(false, items); }
public static Select selectAll(List<SelectItem> items) { return new Select(false, items); }
public static Select selectList(SelectItem... items) { return new Select(false, ImmutableList.copyOf(items)); }
public static Select selectList(SelectItem... items) { return new Select(false, ImmutableList.copyOf(items)); }
public static Select selectList(List<Expression> expressions) { ImmutableList.Builder<SelectItem> items = ImmutableList.builder(); for (Expression expression : expressions) { items.add(new SingleColumn(expression)); } return new Select(false, items.build()); }
public static Select selectList(Expression... expressions) { ImmutableList.Builder<SelectItem> items = ImmutableList.builder(); for (Expression expression : expressions) { items.add(new SingleColumn(expression)); } return new Select(false, items.build()); }
@Override public Node visitQuerySpecification(SqlBaseParser.QuerySpecificationContext context) { Optional<Relation> from = Optional.empty(); List<Relation> relations = visit(context.relation(), Relation.class); if (!relations.isEmpty()) { // synthesize implicit join nodes Iterator<Relation> iterator = relations.iterator(); Relation relation = iterator.next(); while (iterator.hasNext()) { relation = new Join(getLocation(context), Join.Type.IMPLICIT, relation, iterator.next(), Optional.<JoinCriteria>empty()); } from = Optional.of(relation); } return new QuerySpecification( getLocation(context), new Select(getLocation(context.SELECT()), isDistinct(context.setQuantifier()), visit(context.selectItem(), SelectItem.class)), from, visitIfPresent(context.where, Expression.class), visitIfPresent(context.groupBy(), GroupBy.class), visitIfPresent(context.having, Expression.class), ImmutableList.of(), Optional.<String>empty()); }
@Override public Node visitQuerySpecification(SqlBaseParser.QuerySpecificationContext context) { Optional<Relation> from = Optional.empty(); List<SelectItem> selectItems = visit(context.selectItem(), SelectItem.class); List<Relation> relations = visit(context.relation(), Relation.class); if (!relations.isEmpty()) { // synthesize implicit join nodes Iterator<Relation> iterator = relations.iterator(); Relation relation = iterator.next(); while (iterator.hasNext()) { relation = new Join(getLocation(context), Join.Type.IMPLICIT, relation, iterator.next(), Optional.empty()); } from = Optional.of(relation); } return new QuerySpecification( getLocation(context), new Select(getLocation(context.SELECT()), isDistinct(context.setQuantifier()), selectItems), from, visitIfPresent(context.where, Expression.class), visitIfPresent(context.groupBy(), GroupBy.class), visitIfPresent(context.having, Expression.class), Optional.empty(), Optional.empty()); }
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()))); }