Search in sources :

Example 21 with SqlParser

use of com.facebook.presto.sql.parser.SqlParser in project presto by prestodb.

the class TestCreateFunctionTask method testCreateTemporaryFunctionWithSameNameFails.

@Test(expectedExceptions = PrestoException.class, expectedExceptionsMessageRegExp = "Session function .* has already been defined")
public void testCreateTemporaryFunctionWithSameNameFails() {
    SqlParser parser = new SqlParser();
    String sqlString = "CREATE TEMPORARY FUNCTION foo() RETURNS int RETURN 1";
    CreateFunction statement = (CreateFunction) parser.createStatement(sqlString, ParsingOptions.builder().build());
    TransactionManager transactionManager = createTestTransactionManager();
    QueryStateMachine stateMachine = createQueryStateMachine(sqlString, TEST_SESSION, false, transactionManager, executorService, metadataManager);
    WarningCollector warningCollector = stateMachine.getWarningCollector();
    CreateFunctionTask createFunctionTask = new CreateFunctionTask(parser);
    createFunctionTask.execute(statement, transactionManager, metadataManager, new AllowAllAccessControl(), TEST_SESSION, emptyList(), warningCollector);
    createFunctionTask.execute(statement, transactionManager, metadataManager, new AllowAllAccessControl(), TEST_SESSION, emptyList(), warningCollector);
}
Also used : TaskTestUtils.createQueryStateMachine(com.facebook.presto.execution.TaskTestUtils.createQueryStateMachine) InMemoryTransactionManager.createTestTransactionManager(com.facebook.presto.transaction.InMemoryTransactionManager.createTestTransactionManager) TransactionManager(com.facebook.presto.transaction.TransactionManager) AllowAllAccessControl(com.facebook.presto.security.AllowAllAccessControl) CreateFunction(com.facebook.presto.sql.tree.CreateFunction) SqlParser(com.facebook.presto.sql.parser.SqlParser) WarningCollector(com.facebook.presto.spi.WarningCollector) Test(org.testng.annotations.Test)

Example 22 with SqlParser

use of com.facebook.presto.sql.parser.SqlParser in project presto by prestodb.

the class TestCreateMaterializedViewTask method testCreateMaterializedViewNotExistsTrue.

@Test
public void testCreateMaterializedViewNotExistsTrue() {
    SqlParser parser = new SqlParser();
    String sql = String.format("CREATE MATERIALIZED VIEW IF NOT EXISTS %s AS SELECT 2021 AS col_0 FROM %s", MATERIALIZED_VIEW_A, TABLE_A);
    CreateMaterializedView statement = (CreateMaterializedView) parser.createStatement(sql, ParsingOptions.builder().build());
    QueryStateMachine stateMachine = QueryStateMachine.begin(sql, Optional.empty(), testSession, URI.create("fake://uri"), new ResourceGroupId("test"), Optional.empty(), false, transactionManager, accessControl, executorService, metadata, WarningCollector.NOOP);
    WarningCollector warningCollector = stateMachine.getWarningCollector();
    CreateMaterializedViewTask createMaterializedViewTask = new CreateMaterializedViewTask(parser);
    getFutureValue(createMaterializedViewTask.execute(statement, transactionManager, metadata, accessControl, testSession, emptyList(), warningCollector));
    assertEquals(metadata.getCreateMaterializedViewCallCount(), 1);
}
Also used : CreateMaterializedView(com.facebook.presto.sql.tree.CreateMaterializedView) ResourceGroupId(com.facebook.presto.spi.resourceGroups.ResourceGroupId) SqlParser(com.facebook.presto.sql.parser.SqlParser) WarningCollector(com.facebook.presto.spi.WarningCollector) Test(org.testng.annotations.Test)

Example 23 with SqlParser

use of com.facebook.presto.sql.parser.SqlParser in project presto by prestodb.

the class TestCreateMaterializedViewTask method testCreateMaterializedViewExistsFalse.

@Test
public void testCreateMaterializedViewExistsFalse() {
    SqlParser parser = new SqlParser();
    String sql = String.format("CREATE MATERIALIZED VIEW %s AS SELECT 2021 AS col_0 FROM %s", MATERIALIZED_VIEW_B, TABLE_A);
    CreateMaterializedView statement = (CreateMaterializedView) parser.createStatement(sql, ParsingOptions.builder().build());
    QueryStateMachine stateMachine = QueryStateMachine.begin(sql, Optional.empty(), testSession, URI.create("fake://uri"), new ResourceGroupId("test"), Optional.empty(), false, transactionManager, accessControl, executorService, metadata, WarningCollector.NOOP);
    WarningCollector warningCollector = stateMachine.getWarningCollector();
    try {
        getFutureValue(new CreateMaterializedViewTask(parser).execute(statement, transactionManager, metadata, accessControl, testSession, emptyList(), warningCollector));
        fail("expected exception");
    } catch (RuntimeException e) {
        // Expected
        assertTrue(e instanceof PrestoException);
        PrestoException prestoException = (PrestoException) e;
        assertEquals(prestoException.getErrorCode(), ALREADY_EXISTS.toErrorCode());
    }
    assertEquals(metadata.getCreateMaterializedViewCallCount(), 0);
}
Also used : CreateMaterializedView(com.facebook.presto.sql.tree.CreateMaterializedView) ResourceGroupId(com.facebook.presto.spi.resourceGroups.ResourceGroupId) SqlParser(com.facebook.presto.sql.parser.SqlParser) PrestoException(com.facebook.presto.spi.PrestoException) WarningCollector(com.facebook.presto.spi.WarningCollector) Test(org.testng.annotations.Test)

Example 24 with SqlParser

use of com.facebook.presto.sql.parser.SqlParser in project presto by prestodb.

the class HttpRequestSessionFactory method parsePreparedStatementsHeaders.

private static Map<String, String> parsePreparedStatementsHeaders(HttpServletRequest servletRequest) {
    Map<String, String> preparedStatements = new HashMap<>();
    for (String header : splitSessionHeader(servletRequest.getHeaders(PRESTO_PREPARED_STATEMENT))) {
        List<String> nameValue = Splitter.on('=').limit(2).trimResults().splitToList(header);
        assertRequest(nameValue.size() == 2, "Invalid %s header", PRESTO_PREPARED_STATEMENT);
        String statementName;
        String sqlString;
        try {
            statementName = urlDecode(nameValue.get(0));
            sqlString = urlDecode(nameValue.get(1));
        } catch (IllegalArgumentException e) {
            throw badRequest(format("Invalid %s header: %s", PRESTO_PREPARED_STATEMENT, e.getMessage()));
        }
        // Validate statement
        SqlParser sqlParser = new SqlParser();
        try {
            sqlParser.createStatement(sqlString);
        } catch (ParsingException e) {
            throw badRequest(format("Invalid %s header: %s", PRESTO_PREPARED_STATEMENT, e.getMessage()));
        }
        preparedStatements.put(statementName, sqlString);
    }
    return preparedStatements;
}
Also used : HashMap(java.util.HashMap) ParsingException(com.facebook.presto.sql.parser.ParsingException) SqlParser(com.facebook.presto.sql.parser.SqlParser)

Example 25 with SqlParser

use of com.facebook.presto.sql.parser.SqlParser in project presto by prestodb.

the class TestShadowing method testCreateTableAsSelectDifferentCatalog.

@Test
public void testCreateTableAsSelectDifferentCatalog() throws Exception {
    handle.execute("CREATE TABLE \"my_test_table2\" (column1 BIGINT, column2 DOUBLE)");
    SqlParser parser = new SqlParser();
    Query query = new Query(CATALOG, SCHEMA, ImmutableList.of(), "CREATE TABLE public.my_test_table2 AS SELECT 1 column1, 2.0 column2", ImmutableList.of(), null, null, ImmutableMap.of());
    QueryRewriter rewriter = new QueryRewriter(parser, URL, QualifiedName.of("other_catalog", "other_schema", "tmp_"), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), 1, new Duration(10, SECONDS));
    Query rewrittenQuery = rewriter.shadowQuery(query);
    assertEquals(rewrittenQuery.getPreQueries().size(), 1);
    CreateTableAsSelect createTableAs = (CreateTableAsSelect) parser.createStatement(rewrittenQuery.getPreQueries().get(0));
    assertEquals(createTableAs.getName().getParts().size(), 3);
    assertEquals(createTableAs.getName().getPrefix().get(), QualifiedName.of("other_catalog", "other_schema"));
    assertTrue(createTableAs.getName().getSuffix().startsWith("tmp_"));
    assertFalse(createTableAs.getName().getSuffix().contains("my_test_table"));
}
Also used : CreateTableAsSelect(com.facebook.presto.sql.tree.CreateTableAsSelect) SqlParser(com.facebook.presto.sql.parser.SqlParser) Duration(io.airlift.units.Duration) Test(org.testng.annotations.Test)

Aggregations

SqlParser (com.facebook.presto.sql.parser.SqlParser)38 Expression (com.facebook.presto.sql.tree.Expression)11 Test (org.testng.annotations.Test)11 Metadata (com.facebook.presto.metadata.Metadata)10 Optional (java.util.Optional)10 WarningCollector (com.facebook.presto.spi.WarningCollector)8 RowExpression (com.facebook.presto.spi.relation.RowExpression)8 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)8 TransactionManager (com.facebook.presto.transaction.TransactionManager)8 Session (com.facebook.presto.Session)7 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)7 List (java.util.List)7 Type (com.facebook.presto.common.type.Type)6 ImmutableList (com.google.common.collect.ImmutableList)6 AllowAllAccessControl (com.facebook.presto.security.AllowAllAccessControl)5 PlanVariableAllocator (com.facebook.presto.sql.planner.PlanVariableAllocator)5 Objects.requireNonNull (java.util.Objects.requireNonNull)5 TaskTestUtils.createQueryStateMachine (com.facebook.presto.execution.TaskTestUtils.createQueryStateMachine)4 PlanNode (com.facebook.presto.spi.plan.PlanNode)4 FeaturesConfig (com.facebook.presto.sql.analyzer.FeaturesConfig)4