Search in sources :

Example 11 with SqlParser

use of io.trino.sql.parser.SqlParser in project trino by trinodb.

the class CreateViewTask method execute.

@Override
public ListenableFuture<Void> execute(CreateView statement, QueryStateMachine stateMachine, List<Expression> parameters, WarningCollector warningCollector) {
    Session session = stateMachine.getSession();
    QualifiedObjectName name = createQualifiedObjectName(session, statement, statement.getName());
    accessControl.checkCanCreateView(session.toSecurityContext(), name);
    if (metadata.isMaterializedView(session, name)) {
        throw semanticException(TABLE_ALREADY_EXISTS, statement, "Materialized view already exists: '%s'", name);
    }
    if (metadata.isView(session, name)) {
        if (!statement.isReplace()) {
            throw semanticException(TABLE_ALREADY_EXISTS, statement, "View already exists: '%s'", name);
        }
    } else if (metadata.getTableHandle(session, name).isPresent()) {
        throw semanticException(TABLE_ALREADY_EXISTS, statement, "Table already exists: '%s'", name);
    }
    String sql = getFormattedSql(statement.getQuery(), sqlParser);
    Analysis analysis = analyzerFactory.createAnalyzer(session, parameters, parameterExtractor(statement, parameters), stateMachine.getWarningCollector()).analyze(statement);
    List<ViewColumn> columns = analysis.getOutputDescriptor(statement.getQuery()).getVisibleFields().stream().map(field -> new ViewColumn(field.getName().get(), field.getType().getTypeId())).collect(toImmutableList());
    // use DEFINER security by default
    Optional<Identity> owner = Optional.of(session.getIdentity());
    if (statement.getSecurity().orElse(null) == INVOKER) {
        owner = Optional.empty();
    }
    ViewDefinition definition = new ViewDefinition(sql, session.getCatalog(), session.getSchema(), columns, statement.getComment(), owner);
    metadata.createView(session, name, definition, statement.isReplace());
    stateMachine.setOutput(analysis.getTarget());
    stateMachine.setReferencedTables(analysis.getReferencedTables());
    return immediateVoidFuture();
}
Also used : CreateView(io.trino.sql.tree.CreateView) ViewColumn(io.trino.metadata.ViewColumn) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) INVOKER(io.trino.sql.tree.CreateView.Security.INVOKER) AnalyzerFactory(io.trino.sql.analyzer.AnalyzerFactory) ParameterUtils.parameterExtractor(io.trino.sql.ParameterUtils.parameterExtractor) Inject(javax.inject.Inject) TABLE_ALREADY_EXISTS(io.trino.spi.StandardErrorCode.TABLE_ALREADY_EXISTS) MetadataUtil.createQualifiedObjectName(io.trino.metadata.MetadataUtil.createQualifiedObjectName) Identity(io.trino.spi.security.Identity) Objects.requireNonNull(java.util.Objects.requireNonNull) SqlParser(io.trino.sql.parser.SqlParser) SemanticExceptions.semanticException(io.trino.sql.analyzer.SemanticExceptions.semanticException) Futures.immediateVoidFuture(com.google.common.util.concurrent.Futures.immediateVoidFuture) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ViewDefinition(io.trino.metadata.ViewDefinition) List(java.util.List) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) AccessControl(io.trino.security.AccessControl) SqlFormatterUtil.getFormattedSql(io.trino.sql.SqlFormatterUtil.getFormattedSql) WarningCollector(io.trino.execution.warnings.WarningCollector) Metadata(io.trino.metadata.Metadata) Optional(java.util.Optional) Expression(io.trino.sql.tree.Expression) Session(io.trino.Session) Analysis(io.trino.sql.analyzer.Analysis) Analysis(io.trino.sql.analyzer.Analysis) ViewColumn(io.trino.metadata.ViewColumn) ViewDefinition(io.trino.metadata.ViewDefinition) Identity(io.trino.spi.security.Identity) MetadataUtil.createQualifiedObjectName(io.trino.metadata.MetadataUtil.createQualifiedObjectName) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) Session(io.trino.Session)

Example 12 with SqlParser

use of io.trino.sql.parser.SqlParser in project trino by trinodb.

the class VerifyCommand method run.

@Override
public void run() {
    if (configFilename != null) {
        // Read
        System.setProperty("config", configFilename);
    }
    ImmutableList.Builder<Module> builder = ImmutableList.<Module>builder().add(new PrestoVerifierModule()).addAll(getAdditionalModules());
    Bootstrap app = new Bootstrap(builder.build());
    Injector injector;
    try {
        injector = app.initialize();
    } catch (Exception e) {
        throwIfUnchecked(e);
        throw new RuntimeException(e);
    }
    try {
        VerifierConfig config = injector.getInstance(VerifierConfig.class);
        injector.injectMembers(this);
        Set<String> supportedEventClients = injector.getInstance(Key.get(new TypeLiteral<Set<String>>() {
        }, SupportedEventClients.class));
        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 = Jdbi.create(getQueryDatabase(injector)).installPlugin(new SqlObjectPlugin()).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(), 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(), 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
        int numFailedQueries = new Verifier(System.out, config, eventClients).run(queries);
        System.exit((numFailedQueries > 0) ? 1 : 0);
    } catch (InterruptedException | MalformedURLException e) {
        throw new RuntimeException(e);
    } finally {
        injector.getInstance(LifeCycleManager.class).stop();
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) SqlObjectPlugin(org.jdbi.v3.sqlobject.SqlObjectPlugin) 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(io.trino.sql.parser.SqlParser) UncheckedIOException(java.io.UncheckedIOException) QueryRewriteException(io.trino.verifier.QueryRewriter.QueryRewriteException) SQLException(java.sql.SQLException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) EventClient(io.airlift.event.client.EventClient) LifeCycleManager(io.airlift.bootstrap.LifeCycleManager) Module(com.google.inject.Module)

Example 13 with SqlParser

use of io.trino.sql.parser.SqlParser in project trino by trinodb.

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), PARSING_OPTIONS);
    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"))))));
    assertEquals(parser.createStatement(rewrittenQuery.getQuery(), PARSING_OPTIONS), simpleQuery(selectList(column1, column2), table));
    assertEquals(parser.createStatement(rewrittenQuery.getPostQueries().get(0), PARSING_OPTIONS), new DropTable(createTableAs.getName(), true));
}
Also used : DropTable(io.trino.sql.tree.DropTable) CreateTable(io.trino.sql.tree.CreateTable) Table(io.trino.sql.tree.Table) Identifier(io.trino.sql.tree.Identifier) QueryUtil.simpleQuery(io.trino.sql.QueryUtil.simpleQuery) LongLiteral(io.trino.sql.tree.LongLiteral) CreateTableAsSelect(io.trino.sql.tree.CreateTableAsSelect) SqlParser(io.trino.sql.parser.SqlParser) Duration(io.airlift.units.Duration) SingleColumn(io.trino.sql.tree.SingleColumn) FunctionCall(io.trino.sql.tree.FunctionCall) DropTable(io.trino.sql.tree.DropTable) Test(org.testng.annotations.Test)

Example 14 with SqlParser

use of io.trino.sql.parser.SqlParser in project trino by trinodb.

the class TestShadowing method testInsert.

@Test
public void testInsert() throws Exception {
    handle.execute("CREATE TABLE \"test_insert_table\" (a BIGINT, b DOUBLE, c VARCHAR)");
    SqlParser parser = new SqlParser();
    Query query = new Query(CATALOG, SCHEMA, ImmutableList.of(), "INSERT INTO test_insert_table (b, a, c) values (1.1, 1, 'a'), (2.0, 2, 'b'), (3.1, 3, 'c')", 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(), 2);
    CreateTable createTable = (CreateTable) parser.createStatement(rewrittenQuery.getPreQueries().get(0), PARSING_OPTIONS);
    assertEquals(createTable.getName().getParts().size(), 3);
    assertEquals(createTable.getName().getPrefix().get(), QualifiedName.of("other_catalog", "other_schema"));
    assertTrue(createTable.getName().getSuffix().startsWith("tmp_"));
    assertFalse(createTable.getName().getSuffix().contains("test_insert_table"));
    Insert insert = (Insert) parser.createStatement(rewrittenQuery.getPreQueries().get(1), PARSING_OPTIONS);
    assertEquals(insert.getTarget(), createTable.getName());
    assertEquals(insert.getColumns(), Optional.of(ImmutableList.of(identifier("b"), identifier("a"), identifier("c"))));
    Table table = new Table(createTable.getName());
    SingleColumn columnA = new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new Identifier("A"))));
    SingleColumn columnB = new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new FunctionCall(QualifiedName.of("round"), ImmutableList.of(new Identifier("B"), new LongLiteral("1"))))));
    SingleColumn columnC = new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new Identifier("C"))));
    assertEquals(parser.createStatement(rewrittenQuery.getQuery(), PARSING_OPTIONS), simpleQuery(selectList(columnA, columnB, columnC), table));
    assertEquals(rewrittenQuery.getPostQueries().size(), 1);
    assertEquals(parser.createStatement(rewrittenQuery.getPostQueries().get(0), PARSING_OPTIONS), new DropTable(createTable.getName(), true));
}
Also used : DropTable(io.trino.sql.tree.DropTable) CreateTable(io.trino.sql.tree.CreateTable) Table(io.trino.sql.tree.Table) Identifier(io.trino.sql.tree.Identifier) QueryUtil.simpleQuery(io.trino.sql.QueryUtil.simpleQuery) LongLiteral(io.trino.sql.tree.LongLiteral) SqlParser(io.trino.sql.parser.SqlParser) CreateTable(io.trino.sql.tree.CreateTable) Duration(io.airlift.units.Duration) SingleColumn(io.trino.sql.tree.SingleColumn) FunctionCall(io.trino.sql.tree.FunctionCall) Insert(io.trino.sql.tree.Insert) DropTable(io.trino.sql.tree.DropTable) Test(org.testng.annotations.Test)

Aggregations

SqlParser (io.trino.sql.parser.SqlParser)14 Test (org.testng.annotations.Test)7 Expression (io.trino.sql.tree.Expression)6 JsonCodecFactory (io.airlift.json.JsonCodecFactory)4 ObjectMapperProvider (io.airlift.json.ObjectMapperProvider)4 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)3 Duration (io.airlift.units.Duration)3 QualifiedObjectName (io.trino.metadata.QualifiedObjectName)3 QueryUtil.simpleQuery (io.trino.sql.QueryUtil.simpleQuery)3 AnalyzerFactory (io.trino.sql.analyzer.AnalyzerFactory)3 Symbol (io.trino.sql.planner.Symbol)3 ExpressionAndValuePointers (io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers)3 IrLabel (io.trino.sql.planner.rowpattern.ir.IrLabel)3 ArithmeticUnaryExpression (io.trino.sql.tree.ArithmeticUnaryExpression)3 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)3 IfExpression (io.trino.sql.tree.IfExpression)3 NullLiteral (io.trino.sql.tree.NullLiteral)3 Futures.immediateVoidFuture (com.google.common.util.concurrent.Futures.immediateVoidFuture)2 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)2 Session (io.trino.Session)2