Search in sources :

Example 11 with Statement

use of com.facebook.presto.sql.tree.Statement in project presto by prestodb.

the class TestLimitQueryDeterminismAnalyzer method assertAnalyzerQuery.

private static void assertAnalyzerQuery(MockPrestoAction prestoAction, String expectedQuery) {
    Statement expectedStatement = sqlParser.createStatement(expectedQuery, PARSING_OPTIONS);
    Statement actualStatement = prestoAction.getLastStatement();
    assertEquals(formatSql(actualStatement, Optional.empty()), formatSql(expectedStatement, Optional.empty()));
}
Also used : Statement(com.facebook.presto.sql.tree.Statement)

Example 12 with Statement

use of com.facebook.presto.sql.tree.Statement in project presto by prestodb.

the class TestSqlFormatter method assetQuery.

private void assetQuery(String query) {
    SqlParser parser = new SqlParser();
    Statement statement = parser.createStatement(query, new ParsingOptions());
    String formattedQuery = getFormattedSql(statement, parser, Optional.empty());
    assertEquals(formattedQuery, query);
}
Also used : ParsingOptions(com.facebook.presto.sql.parser.ParsingOptions) Statement(com.facebook.presto.sql.tree.Statement) SqlParser(com.facebook.presto.sql.parser.SqlParser)

Example 13 with Statement

use of com.facebook.presto.sql.tree.Statement in project presto by prestodb.

the class TestPrepareTask method testPrepareInvalidStatement.

@Test
public void testPrepareInvalidStatement() {
    Statement statement = new Execute(identifier("foo"), emptyList());
    String sqlString = "PREPARE my_query FROM EXECUTE foo";
    try {
        executePrepare("my_query", statement, sqlString, TEST_SESSION);
        fail("expected exception");
    } catch (PrestoException e) {
        assertEquals(e.getErrorCode(), NOT_SUPPORTED.toErrorCode());
        assertEquals(e.getMessage(), "Invalid statement type for prepared statement: EXECUTE");
    }
}
Also used : Execute(com.facebook.presto.sql.tree.Execute) Statement(com.facebook.presto.sql.tree.Statement) PrestoException(com.facebook.presto.spi.PrestoException) Test(org.testng.annotations.Test)

Example 14 with Statement

use of com.facebook.presto.sql.tree.Statement in project presto by prestodb.

the class CreateViewTask method execute.

@Override
public ListenableFuture<?> execute(CreateView statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, Session session, List<Expression> parameters, WarningCollector warningCollector) {
    QualifiedObjectName name = createQualifiedObjectName(session, statement, statement.getName());
    accessControl.checkCanCreateView(session.getRequiredTransactionId(), session.getIdentity(), session.getAccessControlContext(), name);
    String sql = getFormattedSql(statement.getQuery(), sqlParser, Optional.of(parameters));
    Analysis analysis = analyzeStatement(statement, session, metadata, accessControl, parameters, warningCollector);
    List<ViewColumn> columns = analysis.getOutputDescriptor(statement.getQuery()).getVisibleFields().stream().map(field -> new ViewColumn(field.getName().get(), field.getType())).collect(toImmutableList());
    List<ColumnMetadata> columnMetadata = columns.stream().map(column -> new ColumnMetadata(column.getName(), column.getType())).collect(toImmutableList());
    ConnectorTableMetadata viewMetadata = new ConnectorTableMetadata(toSchemaTableName(name), columnMetadata);
    // use DEFINER security by default
    Optional<String> owner = Optional.of(session.getUser());
    if (statement.getSecurity().orElse(null) == INVOKER) {
        owner = Optional.empty();
    }
    String data = codec.toJson(new ViewDefinition(sql, session.getCatalog(), session.getSchema(), columns, owner, !owner.isPresent()));
    metadata.createView(session, name.getCatalogName(), viewMetadata, data, statement.isReplace());
    return immediateFuture(null);
}
Also used : WarningCollector(com.facebook.presto.spi.WarningCollector) JsonCodec(com.facebook.airlift.json.JsonCodec) Analyzer(com.facebook.presto.sql.analyzer.Analyzer) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) INVOKER(com.facebook.presto.sql.tree.CreateView.Security.INVOKER) ViewDefinition(com.facebook.presto.metadata.ViewDefinition) Inject(javax.inject.Inject) Objects.requireNonNull(java.util.Objects.requireNonNull) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) TransactionManager(com.facebook.presto.transaction.TransactionManager) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata) Futures.immediateFuture(com.google.common.util.concurrent.Futures.immediateFuture) MetadataUtil.toSchemaTableName(com.facebook.presto.metadata.MetadataUtil.toSchemaTableName) Session(com.facebook.presto.Session) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) SqlParser(com.facebook.presto.sql.parser.SqlParser) FeaturesConfig(com.facebook.presto.sql.analyzer.FeaturesConfig) SqlFormatterUtil.getFormattedSql(com.facebook.presto.sql.SqlFormatterUtil.getFormattedSql) List(java.util.List) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) Expression(com.facebook.presto.sql.tree.Expression) MetadataUtil.createQualifiedObjectName(com.facebook.presto.metadata.MetadataUtil.createQualifiedObjectName) ViewColumn(com.facebook.presto.metadata.ViewDefinition.ViewColumn) Analysis(com.facebook.presto.sql.analyzer.Analysis) Optional(java.util.Optional) Metadata(com.facebook.presto.metadata.Metadata) AccessControl(com.facebook.presto.security.AccessControl) CreateView(com.facebook.presto.sql.tree.CreateView) Statement(com.facebook.presto.sql.tree.Statement) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) Analysis(com.facebook.presto.sql.analyzer.Analysis) ViewColumn(com.facebook.presto.metadata.ViewDefinition.ViewColumn) ViewDefinition(com.facebook.presto.metadata.ViewDefinition) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) MetadataUtil.createQualifiedObjectName(com.facebook.presto.metadata.MetadataUtil.createQualifiedObjectName) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata)

Example 15 with Statement

use of com.facebook.presto.sql.tree.Statement in project presto by prestodb.

the class Analyzer method analyzeSemantic.

public Analysis analyzeSemantic(Statement statement, boolean isDescribe) {
    Statement rewrittenStatement = StatementRewrite.rewrite(session, metadata, sqlParser, queryExplainer, statement, parameters, accessControl, warningCollector);
    Analysis analysis = new Analysis(rewrittenStatement, parameters, isDescribe);
    StatementAnalyzer analyzer = new StatementAnalyzer(analysis, metadata, sqlParser, accessControl, session, warningCollector);
    analyzer.analyze(rewrittenStatement, Optional.empty());
    analyzeForUtilizedColumns(analysis, analysis.getStatement());
    return analysis;
}
Also used : Statement(com.facebook.presto.sql.tree.Statement)

Aggregations

Statement (com.facebook.presto.sql.tree.Statement)34 SqlQueryManager.unwrapExecuteStatement (com.facebook.presto.execution.SqlQueryManager.unwrapExecuteStatement)7 Test (org.testng.annotations.Test)7 Session (com.facebook.presto.Session)6 PrestoException (com.facebook.presto.spi.PrestoException)6 Execute (com.facebook.presto.sql.tree.Execute)5 Expression (com.facebook.presto.sql.tree.Expression)5 Analysis (com.facebook.presto.sql.analyzer.Analysis)4 Analyzer (com.facebook.presto.sql.analyzer.Analyzer)4 ParsingException (com.facebook.presto.sql.parser.ParsingException)4 Explain (com.facebook.presto.sql.tree.Explain)4 FeaturesConfig (com.facebook.presto.sql.analyzer.FeaturesConfig)3 QueryExplainer (com.facebook.presto.sql.analyzer.QueryExplainer)3 CreateView (com.facebook.presto.sql.tree.CreateView)3 TransactionManager (com.facebook.presto.transaction.TransactionManager)3 ImmutableList (com.google.common.collect.ImmutableList)3 QueryPreparer (com.facebook.presto.execution.QueryPreparer)2 SqlQueryExecutionFactory (com.facebook.presto.execution.SqlQueryExecution.SqlQueryExecutionFactory)2 QueryStats (com.facebook.presto.jdbc.QueryStats)2 QueryType (com.facebook.presto.spi.resourceGroups.QueryType)2