Search in sources :

Example 11 with Parameter

use of io.trino.sql.tree.Parameter in project trino by trinodb.

the class CreateMaterializedViewTask method execute.

@Override
public ListenableFuture<Void> execute(CreateMaterializedView statement, QueryStateMachine stateMachine, List<Expression> parameters, WarningCollector warningCollector) {
    Session session = stateMachine.getSession();
    QualifiedObjectName name = createQualifiedObjectName(session, statement, statement.getName());
    Map<NodeRef<Parameter>, Expression> parameterLookup = parameterExtractor(statement, parameters);
    String sql = getFormattedSql(statement.getQuery(), sqlParser);
    Analysis analysis = analyzerFactory.createAnalyzer(session, parameters, parameterLookup, 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());
    CatalogName catalogName = getRequiredCatalogHandle(plannerContext.getMetadata(), session, statement, name.getCatalogName());
    Map<String, Object> properties = materializedViewPropertyManager.getProperties(catalogName, statement.getProperties(), session, plannerContext, accessControl, parameterLookup, true);
    MaterializedViewDefinition definition = new MaterializedViewDefinition(sql, session.getCatalog(), session.getSchema(), columns, statement.getComment(), session.getIdentity(), Optional.empty(), properties);
    accessControl.checkCanCreateMaterializedView(session.toSecurityContext(), name, properties);
    plannerContext.getMetadata().createMaterializedView(session, name, definition, statement.isReplace(), statement.isNotExists());
    stateMachine.setOutput(analysis.getTarget());
    stateMachine.setReferencedTables(analysis.getReferencedTables());
    return immediateVoidFuture();
}
Also used : ViewColumn(io.trino.metadata.ViewColumn) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) AnalyzerFactory(io.trino.sql.analyzer.AnalyzerFactory) ParameterUtils.parameterExtractor(io.trino.sql.ParameterUtils.parameterExtractor) Inject(javax.inject.Inject) CatalogName(io.trino.connector.CatalogName) MetadataUtil.createQualifiedObjectName(io.trino.metadata.MetadataUtil.createQualifiedObjectName) NodeRef(io.trino.sql.tree.NodeRef) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) SqlParser(io.trino.sql.parser.SqlParser) Futures.immediateVoidFuture(com.google.common.util.concurrent.Futures.immediateVoidFuture) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) MetadataUtil.getRequiredCatalogHandle(io.trino.metadata.MetadataUtil.getRequiredCatalogHandle) CreateMaterializedView(io.trino.sql.tree.CreateMaterializedView) List(java.util.List) FeaturesConfig(io.trino.FeaturesConfig) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) AccessControl(io.trino.security.AccessControl) Parameter(io.trino.sql.tree.Parameter) SqlFormatterUtil.getFormattedSql(io.trino.sql.SqlFormatterUtil.getFormattedSql) WarningCollector(io.trino.execution.warnings.WarningCollector) MaterializedViewDefinition(io.trino.metadata.MaterializedViewDefinition) MaterializedViewPropertyManager(io.trino.metadata.MaterializedViewPropertyManager) Optional(java.util.Optional) Expression(io.trino.sql.tree.Expression) Session(io.trino.Session) PlannerContext(io.trino.sql.PlannerContext) Analysis(io.trino.sql.analyzer.Analysis) ViewColumn(io.trino.metadata.ViewColumn) MetadataUtil.createQualifiedObjectName(io.trino.metadata.MetadataUtil.createQualifiedObjectName) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) NodeRef(io.trino.sql.tree.NodeRef) Expression(io.trino.sql.tree.Expression) MaterializedViewDefinition(io.trino.metadata.MaterializedViewDefinition) Analysis(io.trino.sql.analyzer.Analysis) CatalogName(io.trino.connector.CatalogName) Session(io.trino.Session)

Example 12 with Parameter

use of io.trino.sql.tree.Parameter in project trino by trinodb.

the class TestSqlParser method testPrepareWithParameters.

@Test
public void testPrepareWithParameters() {
    assertStatement("PREPARE myquery FROM SELECT ?, ? FROM foo", new Prepare(identifier("myquery"), simpleQuery(selectList(new Parameter(0), new Parameter(1)), table(QualifiedName.of("foo")))));
    assertStatement("PREPARE myquery FROM SELECT * FROM foo LIMIT ?", new Prepare(identifier("myquery"), simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("foo")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new Limit(new Parameter(0))))));
    assertStatement("PREPARE myquery FROM SELECT ?, ? FROM foo LIMIT ?", new Prepare(identifier("myquery"), simpleQuery(selectList(new Parameter(0), new Parameter(1)), table(QualifiedName.of("foo")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new Limit(new Parameter(2))))));
    assertStatement("PREPARE myquery FROM SELECT ? FROM foo FETCH FIRST ? ROWS ONLY", new Prepare(identifier("myquery"), simpleQuery(selectList(new Parameter(0)), table(QualifiedName.of("foo")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new FetchFirst(new Parameter(1))))));
    assertStatement("PREPARE myquery FROM SELECT ?, ? FROM foo FETCH NEXT ? ROWS WITH TIES", new Prepare(identifier("myquery"), simpleQuery(selectList(new Parameter(0), new Parameter(1)), table(QualifiedName.of("foo")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new FetchFirst(new Parameter(2), true)))));
    assertStatement("PREPARE myquery FROM SELECT ?, ? FROM foo OFFSET ? ROWS", new Prepare(identifier("myquery"), simpleQuery(selectList(new Parameter(0), new Parameter(1)), table(QualifiedName.of("foo")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new Offset(new Parameter(2))), Optional.empty())));
    assertStatement("PREPARE myquery FROM SELECT ? FROM foo OFFSET ? ROWS LIMIT ?", new Prepare(identifier("myquery"), simpleQuery(selectList(new Parameter(0)), table(QualifiedName.of("foo")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new Offset(new Parameter(1))), Optional.of(new Limit(new Parameter(2))))));
    assertStatement("PREPARE myquery FROM SELECT ? FROM foo OFFSET ? ROWS FETCH FIRST ? ROWS WITH TIES", new Prepare(identifier("myquery"), simpleQuery(selectList(new Parameter(0)), table(QualifiedName.of("foo")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new Offset(new Parameter(1))), Optional.of(new FetchFirst(new Parameter(2), true)))));
}
Also used : Prepare(io.trino.sql.tree.Prepare) Parameter(io.trino.sql.tree.Parameter) AllColumns(io.trino.sql.tree.AllColumns) Limit(io.trino.sql.tree.Limit) FetchFirst(io.trino.sql.tree.FetchFirst) Offset(io.trino.sql.tree.Offset) Test(org.junit.jupiter.api.Test)

Aggregations

Parameter (io.trino.sql.tree.Parameter)12 Expression (io.trino.sql.tree.Expression)6 NodeRef (io.trino.sql.tree.NodeRef)5 Session (io.trino.Session)4 CatalogName (io.trino.connector.CatalogName)4 AccessControl (io.trino.security.AccessControl)4 PlannerContext (io.trino.sql.PlannerContext)4 List (java.util.List)4 Map (java.util.Map)4 Objects.requireNonNull (java.util.Objects.requireNonNull)4 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)3 Futures.immediateVoidFuture (com.google.common.util.concurrent.Futures.immediateVoidFuture)3 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)3 WarningCollector (io.trino.execution.warnings.WarningCollector)3 MetadataUtil.createQualifiedObjectName (io.trino.metadata.MetadataUtil.createQualifiedObjectName)3 QualifiedObjectName (io.trino.metadata.QualifiedObjectName)3 TrinoException (io.trino.spi.TrinoException)3 ParameterUtils.parameterExtractor (io.trino.sql.ParameterUtils.parameterExtractor)3 ParsingOptions (io.trino.sql.parser.ParsingOptions)3 NodeLocation (io.trino.sql.tree.NodeLocation)3