Search in sources :

Example 1 with SqlParser

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

the class TestClientOptions method testUpdateSessionParameters.

@Test
public void testUpdateSessionParameters() throws Exception {
    ClientOptions options = new ClientOptions();
    ClientSession session = options.toClientSession();
    SqlParser sqlParser = new SqlParser();
    ImmutableMap<String, String> existingProperties = ImmutableMap.of("query_max_memory", "10GB", "distributed_join", "true");
    ImmutableMap<String, String> preparedStatements = ImmutableMap.of("my_query", "select * from foo");
    session = Console.processSessionParameterChange(sqlParser.createStatement("USE test_catalog.test_schema"), session, existingProperties, preparedStatements);
    assertEquals(session.getCatalog(), "test_catalog");
    assertEquals(session.getSchema(), "test_schema");
    assertEquals(session.getProperties().get("query_max_memory"), "10GB");
    assertEquals(session.getProperties().get("distributed_join"), "true");
    assertEquals(session.getPreparedStatements().get("my_query"), "select * from foo");
    session = Console.processSessionParameterChange(sqlParser.createStatement("USE test_schema_b"), session, existingProperties, preparedStatements);
    assertEquals(session.getCatalog(), "test_catalog");
    assertEquals(session.getSchema(), "test_schema_b");
    assertEquals(session.getProperties().get("query_max_memory"), "10GB");
    assertEquals(session.getProperties().get("distributed_join"), "true");
    assertEquals(session.getPreparedStatements().get("my_query"), "select * from foo");
    session = Console.processSessionParameterChange(sqlParser.createStatement("USE test_catalog_2.test_schema"), session, existingProperties, preparedStatements);
    assertEquals(session.getCatalog(), "test_catalog_2");
    assertEquals(session.getSchema(), "test_schema");
    assertEquals(session.getProperties().get("query_max_memory"), "10GB");
    assertEquals(session.getProperties().get("distributed_join"), "true");
    assertEquals(session.getPreparedStatements().get("my_query"), "select * from foo");
}
Also used : ClientSession(com.facebook.presto.client.ClientSession) SqlParser(com.facebook.presto.sql.parser.SqlParser) Test(org.testng.annotations.Test)

Example 2 with SqlParser

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

the class PrestoVerifier method run.

public int run(String[] args) throws Exception {
    if (args.length > 0) {
        System.setProperty("config", args[0]);
    }
    ImmutableList.Builder<Module> builder = ImmutableList.<Module>builder().add(new PrestoVerifierModule()).addAll(getAdditionalModules());
    Bootstrap app = new Bootstrap(builder.build());
    Injector injector = app.strictConfig().initialize();
    try {
        VerifierConfig config = injector.getInstance(VerifierConfig.class);
        injector.injectMembers(this);
        Set<String> supportedEventClients = injector.getInstance(Key.get(new TypeLiteral<Set<String>>() {
        }, Names.named(SUPPORTED_EVENT_CLIENTS)));
        for (String clientType : config.getEventClients()) {
            checkArgument(supportedEventClients.contains(clientType), "Unsupported event client: %s", clientType);
        }
        Set<EventClient> eventClients = injector.getInstance(Key.get(new TypeLiteral<Set<EventClient>>() {
        }));
        VerifierDao dao = new DBI(config.getQueryDatabase()).onDemand(VerifierDao.class);
        ImmutableList.Builder<QueryPair> queriesBuilder = ImmutableList.builder();
        for (String suite : config.getSuites()) {
            queriesBuilder.addAll(dao.getQueriesBySuite(suite, config.getMaxQueries()));
        }
        List<QueryPair> queries = queriesBuilder.build();
        queries = applyOverrides(config, queries);
        queries = filterQueryTypes(new SqlParser(getParserOptions()), config, queries);
        queries = filterQueries(queries);
        if (config.getShadowWrites()) {
            Sets.SetView<QueryType> allowedTypes = Sets.union(config.getTestQueryTypes(), config.getControlQueryTypes());
            checkArgument(!Sets.intersection(allowedTypes, ImmutableSet.of(CREATE, MODIFY)).isEmpty(), "CREATE or MODIFY queries must be allowed in test or control to use write shadowing");
            queries = rewriteQueries(new SqlParser(getParserOptions()), config, queries);
        }
        // Load jdbc drivers if needed
        if (config.getAdditionalJdbcDriverPath() != null) {
            List<URL> urlList = getUrls(config.getAdditionalJdbcDriverPath());
            URL[] urls = new URL[urlList.size()];
            urlList.toArray(urls);
            if (config.getTestJdbcDriverName() != null) {
                loadJdbcDriver(urls, config.getTestJdbcDriverName());
            }
            if (config.getControlJdbcDriverName() != null) {
                loadJdbcDriver(urls, config.getControlJdbcDriverName());
            }
        }
        // TODO: construct this with Guice
        Verifier verifier = new Verifier(System.out, config, eventClients);
        return verifier.run(queries);
    } finally {
        injector.getInstance(LifeCycleManager.class).stop();
    }
}
Also used : ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) DBI(org.skife.jdbi.v2.DBI) URL(java.net.URL) TypeLiteral(com.google.inject.TypeLiteral) Injector(com.google.inject.Injector) Sets(com.google.common.collect.Sets) Bootstrap(io.airlift.bootstrap.Bootstrap) SqlParser(com.facebook.presto.sql.parser.SqlParser) EventClient(io.airlift.event.client.EventClient) LifeCycleManager(io.airlift.bootstrap.LifeCycleManager) Module(com.google.inject.Module)

Example 3 with SqlParser

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

the class TestShadowing method testCreateTableAsSelect.

@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(PrestoVerifier.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));
}
Also used : Table(com.facebook.presto.sql.tree.Table) DropTable(com.facebook.presto.sql.tree.DropTable) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) SqlParser(com.facebook.presto.sql.parser.SqlParser) Duration(io.airlift.units.Duration) SingleColumn(com.facebook.presto.sql.tree.SingleColumn) DropTable(com.facebook.presto.sql.tree.DropTable) QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) Identifier(com.facebook.presto.sql.tree.Identifier) CreateTableAsSelect(com.facebook.presto.sql.tree.CreateTableAsSelect) CreateTableAsSelect(com.facebook.presto.sql.tree.CreateTableAsSelect) Select(com.facebook.presto.sql.tree.Select) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) Test(org.testng.annotations.Test)

Example 4 with SqlParser

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

the class TestRemoveUnsupportedDynamicFilters method removeUnsupportedDynamicFilters.

PlanNode removeUnsupportedDynamicFilters(PlanNode root) {
    return getQueryRunner().inTransaction(session -> {
        // metadata.getCatalogHandle() registers the catalog for the transaction
        session.getCatalog().ifPresent(catalog -> metadata.getCatalogHandle(session, catalog));
        PlanNode rewrittenPlan = new RemoveUnsupportedDynamicFilters(metadata.getFunctionAndTypeManager()).optimize(root, session, TypeProvider.empty(), new PlanVariableAllocator(), new PlanNodeIdAllocator(), WarningCollector.NOOP);
        new DynamicFiltersChecker().validate(rewrittenPlan, session, metadata, new SqlParser(), TypeProvider.empty(), WarningCollector.NOOP);
        return rewrittenPlan;
    });
}
Also used : PlanNode(com.facebook.presto.spi.plan.PlanNode) PlanNodeIdAllocator(com.facebook.presto.spi.plan.PlanNodeIdAllocator) SqlParser(com.facebook.presto.sql.parser.SqlParser) PlanVariableAllocator(com.facebook.presto.sql.planner.PlanVariableAllocator) DynamicFiltersChecker(com.facebook.presto.sql.planner.sanity.DynamicFiltersChecker) RemoveUnsupportedDynamicFilters(com.facebook.presto.sql.planner.iterative.rule.RemoveUnsupportedDynamicFilters)

Example 5 with SqlParser

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

the class TestStatisticsWriterNode method getJsonCodec.

private JsonCodec<StatisticsWriterNode> getJsonCodec() throws Exception {
    Module module = binder -> {
        SqlParser sqlParser = new SqlParser();
        FunctionAndTypeManager functionAndTypeManager = createTestFunctionAndTypeManager();
        binder.install(new JsonModule());
        binder.install(new HandleJsonModule());
        binder.bind(SqlParser.class).toInstance(sqlParser);
        binder.bind(TypeManager.class).toInstance(functionAndTypeManager);
        newSetBinder(binder, Type.class);
        jsonBinder(binder).addDeserializerBinding(Type.class).to(TypeDeserializer.class);
        jsonCodecBinder(binder).bindJsonCodec(StatisticsWriterNode.class);
    };
    Bootstrap app = new Bootstrap(ImmutableList.of(module));
    Injector injector = app.doNotInitializeLogging().quiet().initialize();
    HandleResolver handleResolver = injector.getInstance(HandleResolver.class);
    handleResolver.addConnectorName("test", new TestingHandleResolver());
    return injector.getInstance(new Key<JsonCodec<StatisticsWriterNode>>() {
    });
}
Also used : FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) PlanNodeId(com.facebook.presto.spi.plan.PlanNodeId) Module(com.google.inject.Module) JsonCodec(com.facebook.airlift.json.JsonCodec) Bootstrap(com.facebook.airlift.bootstrap.Bootstrap) ColumnStatisticMetadata(com.facebook.presto.spi.statistics.ColumnStatisticMetadata) Key(com.google.inject.Key) Assert.assertEquals(org.testng.Assert.assertEquals) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Test(org.testng.annotations.Test) FunctionAndTypeManager.createTestFunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager) JsonCodecBinder.jsonCodecBinder(com.facebook.airlift.json.JsonCodecBinder.jsonCodecBinder) ValuesNode(com.facebook.presto.spi.plan.ValuesNode) ImmutableList(com.google.common.collect.ImmutableList) TypeManager(com.facebook.presto.common.type.TypeManager) HandleJsonModule(com.facebook.presto.metadata.HandleJsonModule) Multibinder.newSetBinder(com.google.inject.multibindings.Multibinder.newSetBinder) TableHandle(com.facebook.presto.spi.TableHandle) Type(com.facebook.presto.common.type.Type) JsonModule(com.facebook.airlift.json.JsonModule) BIGINT(com.facebook.presto.common.type.BigintType.BIGINT) ROW_COUNT(com.facebook.presto.spi.statistics.TableStatisticType.ROW_COUNT) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) JsonBinder.jsonBinder(com.facebook.airlift.json.JsonBinder.jsonBinder) TestingTransactionHandle(com.facebook.presto.testing.TestingTransactionHandle) UUID(java.util.UUID) SqlParser(com.facebook.presto.sql.parser.SqlParser) Injector(com.google.inject.Injector) TestingHandleResolver(com.facebook.presto.testing.TestingHandleResolver) TypeDeserializer(com.facebook.presto.type.TypeDeserializer) ColumnStatisticType(com.facebook.presto.spi.statistics.ColumnStatisticType) PlanVariableAllocator(com.facebook.presto.sql.planner.PlanVariableAllocator) TestingTableHandle(com.facebook.presto.testing.TestingMetadata.TestingTableHandle) Optional(java.util.Optional) ConnectorId(com.facebook.presto.spi.ConnectorId) HandleResolver(com.facebook.presto.metadata.HandleResolver) TestingHandleResolver(com.facebook.presto.testing.TestingHandleResolver) HandleResolver(com.facebook.presto.metadata.HandleResolver) TestingHandleResolver(com.facebook.presto.testing.TestingHandleResolver) SqlParser(com.facebook.presto.sql.parser.SqlParser) HandleJsonModule(com.facebook.presto.metadata.HandleJsonModule) JsonModule(com.facebook.airlift.json.JsonModule) Type(com.facebook.presto.common.type.Type) ColumnStatisticType(com.facebook.presto.spi.statistics.ColumnStatisticType) JsonCodec(com.facebook.airlift.json.JsonCodec) FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) FunctionAndTypeManager.createTestFunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager) HandleJsonModule(com.facebook.presto.metadata.HandleJsonModule) Injector(com.google.inject.Injector) Bootstrap(com.facebook.airlift.bootstrap.Bootstrap) Module(com.google.inject.Module) HandleJsonModule(com.facebook.presto.metadata.HandleJsonModule) JsonModule(com.facebook.airlift.json.JsonModule) TypeDeserializer(com.facebook.presto.type.TypeDeserializer)

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