Search in sources :

Example 11 with SqlParser

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

the class TestNullabilityAnalyzer method assertNullability.

private void assertNullability(String expression, boolean mayReturnNullForNotNullInput) {
    Expression rawExpression = rewriteIdentifiersToSymbolReferences(new SqlParser().createExpression(expression, new ParsingOptions()));
    Expression desugaredExpression = new TestingDesugarExpressions(TYPES.allVariables()).rewrite(rawExpression);
    RowExpression rowExpression = TRANSLATOR.translate(desugaredExpression, TYPES);
    assertEquals(analyzer.mayReturnNullOnNonNullInput(rowExpression), mayReturnNullForNotNullInput);
}
Also used : RowExpression(com.facebook.presto.spi.relation.RowExpression) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Expression(com.facebook.presto.sql.tree.Expression) ParsingOptions(com.facebook.presto.sql.parser.ParsingOptions) SqlParser(com.facebook.presto.sql.parser.SqlParser) RowExpression(com.facebook.presto.spi.relation.RowExpression)

Example 12 with SqlParser

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

the class HttpRequestSessionContext method parsePreparedStatementsHeaders.

private static Map<String, String> parsePreparedStatementsHeaders(HttpServletRequest servletRequest, SqlParserOptions sqlParserOptions) {
    ImmutableMap.Builder<String, String> preparedStatements = ImmutableMap.builder();
    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(sqlParserOptions);
        try {
            sqlParser.createStatement(sqlString, new ParsingOptions(AS_DOUBLE));
        } catch (ParsingException e) {
            throw badRequest(format("Invalid %s header: %s", PRESTO_PREPARED_STATEMENT, e.getMessage()));
        }
        preparedStatements.put(statementName, sqlString);
    }
    return preparedStatements.build();
}
Also used : ParsingOptions(com.facebook.presto.sql.parser.ParsingOptions) ParsingException(com.facebook.presto.sql.parser.ParsingException) SqlParser(com.facebook.presto.sql.parser.SqlParser) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap)

Example 13 with SqlParser

use of com.facebook.presto.sql.parser.SqlParser 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 14 with SqlParser

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

the class CreateMaterializedViewTask method execute.

@Override
public ListenableFuture<?> execute(CreateMaterializedView statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, Session session, List<Expression> parameters, WarningCollector warningCollector) {
    QualifiedObjectName viewName = createQualifiedObjectName(session, statement, statement.getName());
    Optional<TableHandle> viewHandle = metadata.getTableHandle(session, viewName);
    if (viewHandle.isPresent()) {
        if (!statement.isNotExists()) {
            throw new SemanticException(MATERIALIZED_VIEW_ALREADY_EXISTS, statement, "Materialized view '%s' already exists", viewName);
        }
        return immediateFuture(null);
    }
    accessControl.checkCanCreateTable(session.getRequiredTransactionId(), session.getIdentity(), session.getAccessControlContext(), viewName);
    accessControl.checkCanCreateView(session.getRequiredTransactionId(), session.getIdentity(), session.getAccessControlContext(), viewName);
    Analyzer analyzer = new Analyzer(session, metadata, sqlParser, accessControl, Optional.empty(), parameters, warningCollector);
    Analysis analysis = analyzer.analyze(statement);
    ConnectorId connectorId = metadata.getCatalogHandle(session, viewName.getCatalogName()).orElseThrow(() -> new PrestoException(NOT_FOUND, "Catalog does not exist: " + viewName.getCatalogName()));
    List<ColumnMetadata> columnMetadata = analysis.getOutputDescriptor(statement.getQuery()).getVisibleFields().stream().map(field -> new ColumnMetadata(field.getName().get(), field.getType())).collect(toImmutableList());
    Map<String, Expression> sqlProperties = mapFromProperties(statement.getProperties());
    Map<String, Object> properties = metadata.getTablePropertyManager().getProperties(connectorId, viewName.getCatalogName(), sqlProperties, session, metadata, parameters);
    ConnectorTableMetadata viewMetadata = new ConnectorTableMetadata(toSchemaTableName(viewName), columnMetadata, properties, statement.getComment());
    String sql = getFormattedSql(statement.getQuery(), sqlParser, Optional.of(parameters));
    List<SchemaTableName> baseTables = analysis.getTableNodes().stream().map(table -> {
        QualifiedObjectName tableName = createQualifiedObjectName(session, table, table.getName());
        if (!viewName.getCatalogName().equals(tableName.getCatalogName())) {
            throw new SemanticException(NOT_SUPPORTED, statement, "Materialized view %s created from a base table in a different catalog %s is not supported.", viewName, tableName);
        }
        return toSchemaTableName(tableName);
    }).distinct().collect(toImmutableList());
    MaterializedViewColumnMappingExtractor extractor = new MaterializedViewColumnMappingExtractor(analysis, session);
    ConnectorMaterializedViewDefinition viewDefinition = new ConnectorMaterializedViewDefinition(sql, viewName.getSchemaName(), viewName.getObjectName(), baseTables, Optional.of(session.getUser()), extractor.getMaterializedViewColumnMappings(), extractor.getMaterializedViewDirectColumnMappings(), extractor.getBaseTablesOnOuterJoinSide(), Optional.empty());
    try {
        metadata.createMaterializedView(session, viewName.getCatalogName(), viewMetadata, viewDefinition, statement.isNotExists());
    } catch (PrestoException e) {
        // connectors are not required to handle the ignoreExisting flag
        if (!e.getErrorCode().equals(ALREADY_EXISTS.toErrorCode()) || !statement.isNotExists()) {
            throw e;
        }
    }
    return immediateFuture(null);
}
Also used : WarningCollector(com.facebook.presto.spi.WarningCollector) Analyzer(com.facebook.presto.sql.analyzer.Analyzer) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) NOT_FOUND(com.facebook.presto.spi.StandardErrorCode.NOT_FOUND) PrestoException(com.facebook.presto.spi.PrestoException) SemanticException(com.facebook.presto.sql.analyzer.SemanticException) Inject(javax.inject.Inject) SchemaTableName(com.facebook.presto.spi.SchemaTableName) ALREADY_EXISTS(com.facebook.presto.spi.StandardErrorCode.ALREADY_EXISTS) NodeUtils.mapFromProperties(com.facebook.presto.sql.NodeUtils.mapFromProperties) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) ConnectorMaterializedViewDefinition(com.facebook.presto.spi.ConnectorMaterializedViewDefinition) TableHandle(com.facebook.presto.spi.TableHandle) 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) MaterializedViewColumnMappingExtractor(com.facebook.presto.sql.analyzer.MaterializedViewColumnMappingExtractor) NOT_SUPPORTED(com.facebook.presto.sql.analyzer.SemanticErrorCode.NOT_SUPPORTED) SqlParser(com.facebook.presto.sql.parser.SqlParser) CreateMaterializedView(com.facebook.presto.sql.tree.CreateMaterializedView) 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) MATERIALIZED_VIEW_ALREADY_EXISTS(com.facebook.presto.sql.analyzer.SemanticErrorCode.MATERIALIZED_VIEW_ALREADY_EXISTS) Analysis(com.facebook.presto.sql.analyzer.Analysis) Optional(java.util.Optional) ConnectorId(com.facebook.presto.spi.ConnectorId) Metadata(com.facebook.presto.metadata.Metadata) AccessControl(com.facebook.presto.security.AccessControl) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) ConnectorMaterializedViewDefinition(com.facebook.presto.spi.ConnectorMaterializedViewDefinition) PrestoException(com.facebook.presto.spi.PrestoException) Analyzer(com.facebook.presto.sql.analyzer.Analyzer) SchemaTableName(com.facebook.presto.spi.SchemaTableName) MetadataUtil.toSchemaTableName(com.facebook.presto.metadata.MetadataUtil.toSchemaTableName) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) MetadataUtil.createQualifiedObjectName(com.facebook.presto.metadata.MetadataUtil.createQualifiedObjectName) MaterializedViewColumnMappingExtractor(com.facebook.presto.sql.analyzer.MaterializedViewColumnMappingExtractor) Expression(com.facebook.presto.sql.tree.Expression) Analysis(com.facebook.presto.sql.analyzer.Analysis) TableHandle(com.facebook.presto.spi.TableHandle) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata) SemanticException(com.facebook.presto.sql.analyzer.SemanticException) ConnectorId(com.facebook.presto.spi.ConnectorId)

Example 15 with SqlParser

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

the class AlterFunctionTask method execute.

@Override
public ListenableFuture<?> execute(AlterFunction statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, Session session, List<Expression> parameters, WarningCollector warningCollector) {
    Analyzer analyzer = new Analyzer(session, metadata, sqlParser, accessControl, Optional.empty(), parameters, warningCollector);
    analyzer.analyze(statement);
    QualifiedObjectName functionName = qualifyObjectName(statement.getFunctionName());
    AlterRoutineCharacteristics alterRoutineCharacteristics = new AlterRoutineCharacteristics(statement.getCharacteristics().getNullCallClause().map(com.facebook.presto.sql.tree.RoutineCharacteristics.NullCallClause::name).map(NullCallClause::valueOf));
    metadata.getFunctionAndTypeManager().alterFunction(functionName, statement.getParameterTypes().map(types -> types.stream().map(TypeSignature::parseTypeSignature).collect(toImmutableList())), alterRoutineCharacteristics);
    return immediateFuture(null);
}
Also used : WarningCollector(com.facebook.presto.spi.WarningCollector) Futures.immediateFuture(com.google.common.util.concurrent.Futures.immediateFuture) Analyzer(com.facebook.presto.sql.analyzer.Analyzer) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Session(com.facebook.presto.Session) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) NullCallClause(com.facebook.presto.spi.function.RoutineCharacteristics.NullCallClause) TypeSignature(com.facebook.presto.common.type.TypeSignature) SqlParser(com.facebook.presto.sql.parser.SqlParser) Inject(javax.inject.Inject) List(java.util.List) Expression(com.facebook.presto.sql.tree.Expression) AlterFunction(com.facebook.presto.sql.tree.AlterFunction) Objects.requireNonNull(java.util.Objects.requireNonNull) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) Optional(java.util.Optional) AlterRoutineCharacteristics(com.facebook.presto.spi.function.AlterRoutineCharacteristics) TransactionManager(com.facebook.presto.transaction.TransactionManager) FunctionAndTypeManager.qualifyObjectName(com.facebook.presto.metadata.FunctionAndTypeManager.qualifyObjectName) Metadata(com.facebook.presto.metadata.Metadata) AccessControl(com.facebook.presto.security.AccessControl) AlterRoutineCharacteristics(com.facebook.presto.spi.function.AlterRoutineCharacteristics) NullCallClause(com.facebook.presto.spi.function.RoutineCharacteristics.NullCallClause) Analyzer(com.facebook.presto.sql.analyzer.Analyzer) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName)

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