public static Query simpleQuery(Select select) { return query(new QuerySpecification( select, Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty())); }
public static Query simpleQuery(Select select, Relation from, Optional<Expression> where, Optional<GroupBy> groupBy, Optional<Expression> having, Optional<OrderBy> orderBy, Optional<String> limit) { return query(new QuerySpecification( select, Optional.of(from), where, groupBy, having, orderBy, limit)); }
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()); }
private static QuerySpecification createSelect123() { return new QuerySpecification( selectList(new LongLiteral("123")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()); }
if (innerQuery instanceof QuerySpecification) { QuerySpecification querySpecification = (QuerySpecification) innerQuery; innerQuery = new QuerySpecification( querySpecification.getSelect(), querySpecification.getFrom(),
@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()); }
getLocation(context), Optional.empty(), new QuerySpecification( getLocation(context), query.getSelect(),
@Test public void testAtTimeZone() { assertStatement("SELECT timestamp '2012-10-31 01:00 UTC' AT TIME ZONE 'America/Los_Angeles'", new Query( Optional.empty(), new QuerySpecification( selectList( new AtTimeZone(new TimestampLiteral("2012-10-31 01:00 UTC"), new StringLiteral("America/Los_Angeles"))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty())); }
new Query( Optional.empty(), new QuerySpecification( selectList(new FunctionCall(QualifiedName.of("substr"), Lists.newArrayList(new StringLiteral(givenString), new LongLiteral("2")))), Optional.empty(), new Query( Optional.empty(), new QuerySpecification( selectList(new FunctionCall(QualifiedName.of("substr"), Lists.newArrayList(new StringLiteral(givenString), new LongLiteral("2"), new LongLiteral("3")))), Optional.empty(),
new Query( Optional.empty(), new QuerySpecification( selectList(new FunctionCall(QualifiedName.of("substring"), Lists.newArrayList(new StringLiteral(givenString), new LongLiteral("2")))), Optional.empty(), new Query( Optional.empty(), new QuerySpecification( selectList(new FunctionCall(QualifiedName.of("substring"), Lists.newArrayList(new StringLiteral(givenString), new LongLiteral("2"), new LongLiteral("3")))), 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 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()));
new Query( Optional.empty(), new QuerySpecification( selectList(new AllColumns()), Optional.of(new Table(QualifiedName.of("table1"))), new Query( Optional.empty(), new QuerySpecification( selectList(new AllColumns()), Optional.of(new Table(QualifiedName.of("table1"))), new Query( Optional.empty(), new QuerySpecification( selectList(new AllColumns()), Optional.of(new Table(QualifiedName.of("table1"))), new Query( Optional.empty(), new QuerySpecification( selectList(new AllColumns()), Optional.of(new Table(QualifiedName.of("table1"))), new Query( Optional.empty(), new QuerySpecification( selectList( DereferenceExpression.from(QualifiedName.of("a")),
new Query( Optional.empty(), new QuerySpecification( selectList( new SubscriptExpression(new DereferenceExpression(new Identifier("col1"), identifier("f1")), new LongLiteral("0")), new Query( Optional.empty(), new QuerySpecification( selectList( new DereferenceExpression(new Cast(new Row(Lists.newArrayList(new LongLiteral("11"), new LongLiteral("12"))), "ROW(COL0 INTEGER,COL1 INTEGER)"), identifier("col0"))),
public static Query simpleQuery(Select select, Relation from, Optional<Expression> where, Optional<GroupBy> groupBy, Optional<Expression> having, Optional<OrderBy> orderBy, Optional<String> limit) { return query(new QuerySpecification( select, Optional.of(from), where, groupBy, having, orderBy, limit)); }
public static Query simpleQuery(Select select) { return query(new QuerySpecification( select, Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty())); }
public static Query simpleQuery(Select select, Relation from, Optional<Expression> where, Optional<GroupBy> groupBy, Optional<Expression> having, List<SortItem> ordering, Optional<String> limit) { return query(new QuerySpecification( select, Optional.of(from), where, groupBy, having, ordering, limit)); }
private static QuerySpecification createSelect123() { return new QuerySpecification( selectList(new LongLiteral("123")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableList.of(), Optional.empty() ); }
private static QuerySpecification createSelect123() { return new QuerySpecification( selectList(new LongLiteral("123")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()); }
@Test public void testAtTimeZone() { assertStatement("SELECT timestamp '2012-10-31 01:00 UTC' AT TIME ZONE 'America/Los_Angeles'", new Query( Optional.empty(), new QuerySpecification( selectList( new AtTimeZone(new TimestampLiteral("2012-10-31 01:00 UTC"), new StringLiteral("America/Los_Angeles"))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty())); }