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);
}
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);
}
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);
}
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;
}
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"));
}
Aggregations