public static Identifier identifier(String name) { return new Identifier(name); }
public static Identifier quotedIdentifier(String name) { return new Identifier(name, true); }
public static Expression from(QualifiedName name) { Expression result = null; for (String part : name.getParts()) { if (result == null) { result = new Identifier(part); } else { result = new DereferenceExpression(result, new Identifier(part)); } } return result; }
private static Identifier input(String symbol) { return new Identifier(symbol); } }
private static Identifier name(String name) { return new Identifier(name); }
@Override public Node visitQuotedIdentifier(SqlBaseParser.QuotedIdentifierContext context) { String token = context.getText(); String identifier = token.substring(1, token.length() - 1) .replace("\"\"", "\""); return new Identifier(getLocation(context), identifier, true); }
@Override public Node visitUnquotedIdentifier(SqlBaseParser.UnquotedIdentifierContext context) { return new Identifier(getLocation(context), context.getText(), false); }
@Override public Expression rewriteLambdaExpression(LambdaExpression node, Void context, ExpressionTreeRewriter<Void> treeRewriter) { checkState(analysis.getCoercion(node) == null, "cannot coerce a lambda expression"); ImmutableList.Builder<LambdaArgumentDeclaration> newArguments = ImmutableList.builder(); for (LambdaArgumentDeclaration argument : node.getArguments()) { Symbol symbol = lambdaDeclarationToSymbolMap.get(NodeRef.of(argument)); newArguments.add(new LambdaArgumentDeclaration(new Identifier(symbol.getName()))); } Expression rewrittenBody = treeRewriter.rewrite(node.getBody(), null); return new LambdaExpression(newArguments.build(), rewrittenBody); }
@Test(expectedExceptions = PrestoException.class, expectedExceptionsMessageRegExp = "Catalog does not exist: .*") public void testSetPathInvalidCatalog() { PathSpecification invalidPathSpecification = new PathSpecification(Optional.empty(), ImmutableList.of( new PathElement(Optional.of(new Identifier("invalidCatalog")), new Identifier("thisDoesNotMatter")))); QueryStateMachine stateMachine = createQueryStateMachine("SET PATH invalidCatalog.thisDoesNotMatter"); executeSetPathTask(invalidPathSpecification, stateMachine); }
@Test public void testSqlPathCreation() { ImmutableList.Builder<SqlPathElement> correctValues = ImmutableList.builder(); correctValues.add(new SqlPathElement( Optional.of(new Identifier("normal")), new Identifier("schema"))); correctValues.add(new SqlPathElement( Optional.of(new Identifier("who.uses.periods")), new Identifier("in.schema.names"))); correctValues.add(new SqlPathElement( Optional.of(new Identifier("same,deal")), new Identifier("with,commas"))); correctValues.add(new SqlPathElement( Optional.of(new Identifier("aterrible")), new Identifier("thing!@#$%^&*()"))); List<SqlPathElement> expected = correctValues.build(); SqlPath path = new SqlPath(Optional.of("normal.schema," + "\"who.uses.periods\".\"in.schema.names\"," + "\"same,deal\".\"with,commas\"," + "aterrible.\"thing!@#$%^&*()\"")); assertEquals(path.getParsedPath(), expected); assertEquals(path.toString(), Joiner.on(", ").join(expected)); } }
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()); }
@Test public void testSetPath() { PathSpecification pathSpecification = new PathSpecification(Optional.empty(), ImmutableList.of( new PathElement(Optional.empty(), new Identifier("foo")))); QueryStateMachine stateMachine = createQueryStateMachine("SET PATH foo"); executeSetPathTask(pathSpecification, stateMachine); assertEquals(stateMachine.getSetPath(), "foo"); }
@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 testAggregationFilter() { assertStatement("SELECT SUM(x) FILTER (WHERE x > 4)", new Query( Optional.empty(), new QuerySpecification( selectList( new FunctionCall( QualifiedName.of("SUM"), Optional.empty(), Optional.of(new ComparisonExpression( ComparisonExpression.Operator.GREATER_THAN, new Identifier("x"), new LongLiteral("4"))), Optional.empty(), false, ImmutableList.of(new Identifier("x")))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty())); }
@Test public void testLambda() { assertExpression("() -> x", new LambdaExpression( ImmutableList.of(), new Identifier("x"))); assertExpression("x -> sin(x)", new LambdaExpression( ImmutableList.of(new LambdaArgumentDeclaration(identifier("x"))), new FunctionCall(QualifiedName.of("sin"), ImmutableList.of(new Identifier("x"))))); assertExpression("(x, y) -> mod(x, y)", new LambdaExpression( ImmutableList.of(new LambdaArgumentDeclaration(identifier("x")), new LambdaArgumentDeclaration(identifier("y"))), new FunctionCall( QualifiedName.of("mod"), ImmutableList.of(new Identifier("x"), new Identifier("y"))))); }
@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))))); }
@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 testCreateSchema() { assertStatement("CREATE SCHEMA test", new CreateSchema(QualifiedName.of("test"), false, ImmutableList.of())); assertStatement("CREATE SCHEMA IF NOT EXISTS test", new CreateSchema(QualifiedName.of("test"), true, ImmutableList.of())); assertStatement("CREATE SCHEMA test WITH (a = 'apple', b = 123)", new CreateSchema( QualifiedName.of("test"), false, ImmutableList.of( new Property(new Identifier("a"), new StringLiteral("apple")), new Property(new Identifier("b"), new LongLiteral("123"))))); assertStatement("CREATE SCHEMA \"some name that contains space\"", new CreateSchema(QualifiedName.of("some name that contains space"), false, ImmutableList.of())); }
@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)); }
private Set<String> executeDeallocate(String statementName, String sqlString, Session session) { TransactionManager transactionManager = createTestTransactionManager(); AccessControl accessControl = new AccessControlManager(transactionManager); QueryStateMachine stateMachine = QueryStateMachine.begin( sqlString, session, URI.create("fake://uri"), new ResourceGroupId("test"), false, transactionManager, accessControl, executor, metadata, WarningCollector.NOOP); Deallocate deallocate = new Deallocate(new Identifier(statementName)); new DeallocateTask().execute(deallocate, transactionManager, metadata, new AllowAllAccessControl(), stateMachine, emptyList()); return stateMachine.getDeallocatedPreparedStatements(); } }