Search in sources :

Example 21 with SqlParser

use of io.prestosql.sql.parser.SqlParser in project hetu-core by openlookeng.

the class Analyzer method analyze.

public Analysis analyze(Statement statement, boolean isDescribe) {
    Statement rewrittenStatement = StatementRewrite.rewrite(session, metadata, cubeManager, sqlParser, queryExplainer, statement, parameters, accessControl, warningCollector, heuristicIndexerManager);
    Analysis analysis = new Analysis(rewrittenStatement, parameters, isDescribe);
    analysis.setOriginalStatement(statement);
    StatementAnalyzer analyzer = new StatementAnalyzer(analysis, metadata, sqlParser, accessControl, session, warningCollector, heuristicIndexerManager, cubeManager);
    analyzer.analyze(rewrittenStatement, Optional.empty());
    // check column access permissions for each table
    analysis.getTableColumnReferences().forEach((accessControlInfo, tableColumnReferences) -> tableColumnReferences.forEach((tableName, columns) -> accessControlInfo.getAccessControl().checkCanSelectFromColumns(session.getRequiredTransactionId(), accessControlInfo.getIdentity(), tableName, columns)));
    return analysis;
}
Also used : Iterables(com.google.common.collect.Iterables) SqlParser(io.prestosql.sql.parser.SqlParser) NOT_SUPPORTED(io.prestosql.sql.analyzer.SemanticErrorCode.NOT_SUPPORTED) Statement(io.prestosql.sql.tree.Statement) WarningCollector(io.prestosql.execution.warnings.WarningCollector) ExpressionTreeUtils.extractWindowFunctions(io.prestosql.sql.analyzer.ExpressionTreeUtils.extractWindowFunctions) ImmutableList(com.google.common.collect.ImmutableList) FunctionCall(io.prestosql.sql.tree.FunctionCall) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Session(io.prestosql.Session) ExpressionTreeUtils.extractExpressions(io.prestosql.sql.analyzer.ExpressionTreeUtils.extractExpressions) StatementRewrite(io.prestosql.sql.rewrite.StatementRewrite) HeuristicIndexerManager(io.prestosql.heuristicindex.HeuristicIndexerManager) AccessControl(io.prestosql.security.AccessControl) GroupingOperation(io.prestosql.sql.tree.GroupingOperation) Metadata(io.prestosql.metadata.Metadata) NodeRef(io.prestosql.sql.tree.NodeRef) FunctionHandle(io.prestosql.spi.function.FunctionHandle) CubeManager(io.prestosql.cube.CubeManager) List(java.util.List) CANNOT_HAVE_AGGREGATIONS_WINDOWS_OR_GROUPING(io.prestosql.sql.analyzer.SemanticErrorCode.CANNOT_HAVE_AGGREGATIONS_WINDOWS_OR_GROUPING) ExpressionTreeUtils.extractExternalFunctions(io.prestosql.sql.analyzer.ExpressionTreeUtils.extractExternalFunctions) ExpressionTreeUtils.extractAggregateFunctions(io.prestosql.sql.analyzer.ExpressionTreeUtils.extractAggregateFunctions) Optional(java.util.Optional) FunctionAndTypeManager(io.prestosql.metadata.FunctionAndTypeManager) Expression(io.prestosql.sql.tree.Expression) Statement(io.prestosql.sql.tree.Statement)

Example 22 with SqlParser

use of io.prestosql.sql.parser.SqlParser in project hetu-core by openlookeng.

the class CubeOptimizer method rewriteFilterPredicate.

private Optional<RowExpression> rewriteFilterPredicate() {
    // rewrite the expression by removing source filter predicate as cube would not have those columns necessarily
    Expression queryPredicate = castToExpression(filterNode.getPredicate());
    if (cubeMetadata.getCubeFilter() == null || cubeMetadata.getCubeFilter().getSourceTablePredicate() == null) {
        // nothing more to do. just rewrite the symbol reference of the original predicate
        return Optional.of(castToRowExpression(rewriteSymbolReferenceToTargetMapping(queryPredicate)));
    }
    SqlParser sqlParser = new SqlParser();
    Expression cubeSourcePredicate = sqlParser.createExpression(cubeMetadata.getCubeFilter().getSourceTablePredicate(), new ParsingOptions());
    cubeSourcePredicate = rewriteIdentifiersWithSymbolReference(cubeSourcePredicate);
    Set<Symbol> sourceFilterPredicateColumns = SymbolsExtractor.extractUnique(cubeSourcePredicate);
    Expression modifiedPredicate = ExpressionUtils.filterConjuncts(queryPredicate, expr -> !sourceFilterPredicateColumns.containsAll(SymbolsExtractor.extractUnique(expr)));
    return Optional.ofNullable(modifiedPredicate.equals(BooleanLiteral.TRUE_LITERAL) ? null : castToRowExpression(rewriteSymbolReferenceToTargetMapping(modifiedPredicate)));
}
Also used : ConstantExpression(io.prestosql.spi.relation.ConstantExpression) CallExpression(io.prestosql.spi.relation.CallExpression) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) OriginalExpressionUtils.castToExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToExpression) ArithmeticBinaryExpression(io.prestosql.sql.tree.ArithmeticBinaryExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) RowExpression(io.prestosql.spi.relation.RowExpression) Expression(io.prestosql.sql.tree.Expression) ParsingOptions(io.prestosql.sql.parser.ParsingOptions) Symbol(io.prestosql.spi.plan.Symbol) SqlParser(io.prestosql.sql.parser.SqlParser)

Example 23 with SqlParser

use of io.prestosql.sql.parser.SqlParser in project hetu-core by openlookeng.

the class CubeOptimizer method filterPredicateMatches.

private boolean filterPredicateMatches(Expression queryPredicate, CubeMetadata cubeMetadata, Session session, TypeProvider types) {
    CubeFilter cubeFilter = cubeMetadata.getCubeFilter();
    if (cubeFilter == null) {
        // Cube was built for entire table
        return queryPredicate == null || doesCubeContainQueryPredicateColumns(queryPredicate, cubeMetadata);
    }
    if (queryPredicate == null) {
        // Query statement has no WHERE clause but CUBE was built for subset of original data
        return false;
    }
    SqlParser sqlParser = new SqlParser();
    Expression cubeSourceTablePredicate = cubeFilter.getSourceTablePredicate() == null ? null : sqlParser.createExpression(cubeFilter.getSourceTablePredicate(), new ParsingOptions());
    Pair<Expression, Expression> queryPredicateSplit = splitQueryPredicate(queryPredicate, cubeSourceTablePredicate);
    if (!arePredicatesEqual(queryPredicateSplit.getLeft(), cubeSourceTablePredicate, metadata, session, types)) {
        log.debug("Cube source table predicate %s not matching query predicate %s", cubeSourceTablePredicate, queryPredicate);
        return false;
    }
    // Check if columns in query predicate are all part of the Cube.
    if ((cubeFilter.getCubePredicate() != null && queryPredicateSplit.getRight() == null) || (queryPredicateSplit.getRight() != null && !doesCubeContainQueryPredicateColumns(queryPredicateSplit.getRight(), cubeMetadata))) {
        // Cube does not contain all columns in the remaining predicate
        return false;
    }
    if (cubeFilter.getCubePredicate() == null) {
        // Cube has no additional predicates to compare with. i.e. Cube can be used to optimize the query
        return true;
    }
    ExpressionDomainTranslator.ExtractionResult decomposedQueryPredicate = ExpressionDomainTranslator.fromPredicate(metadata, session, queryPredicateSplit.getRight(), types);
    if (!BooleanLiteral.TRUE_LITERAL.equals(decomposedQueryPredicate.getRemainingExpression())) {
        log.error("StarTree cube cannot support predicate %s", castToExpression(filterNode.getPredicate()));
        return false;
    }
    Expression cubePredicate = ExpressionUtils.rewriteIdentifiersToSymbolReferences(sqlParser.createExpression(cubeFilter.getCubePredicate(), new ParsingOptions()));
    ExpressionDomainTranslator.ExtractionResult decomposedCubePredicate = ExpressionDomainTranslator.fromPredicate(metadata, session, cubePredicate, types);
    if (!BooleanLiteral.TRUE_LITERAL.equals(decomposedCubePredicate.getRemainingExpression())) {
        // Extract disjunctions from the expression and evaluate separately
        return atLeastMatchesOne(ExpressionUtils.extractDisjuncts(cubePredicate), decomposedQueryPredicate.getTupleDomain(), session, types);
    }
    return decomposedCubePredicate.getTupleDomain().contains(decomposedQueryPredicate.getTupleDomain());
}
Also used : ConstantExpression(io.prestosql.spi.relation.ConstantExpression) CallExpression(io.prestosql.spi.relation.CallExpression) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) OriginalExpressionUtils.castToExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToExpression) ArithmeticBinaryExpression(io.prestosql.sql.tree.ArithmeticBinaryExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) RowExpression(io.prestosql.spi.relation.RowExpression) Expression(io.prestosql.sql.tree.Expression) ParsingOptions(io.prestosql.sql.parser.ParsingOptions) ExpressionDomainTranslator(io.prestosql.sql.planner.ExpressionDomainTranslator) CubeFilter(io.hetu.core.spi.cube.CubeFilter) SqlParser(io.prestosql.sql.parser.SqlParser)

Example 24 with SqlParser

use of io.prestosql.sql.parser.SqlParser in project hetu-core by openlookeng.

the class StarTreeAggregationRule method rewriteByRemovingSourceFilter.

private FilterNode rewriteByRemovingSourceFilter(PlanNode filterNode, CubeMetadata matchedCubeMetadata) {
    FilterNode rewritten = (FilterNode) filterNode;
    if (filterNode != null && matchedCubeMetadata.getCubeFilter() != null && matchedCubeMetadata.getCubeFilter().getSourceTablePredicate() != null) {
        // rewrite the expression by removing source filter predicate as cube would not have those columns necessarily
        Expression predicate = castToExpression(((FilterNode) filterNode).getPredicate());
        SqlParser sqlParser = new SqlParser();
        Set<Identifier> sourceFilterPredicateColumns = ExpressionUtils.getIdentifiers(sqlParser.createExpression(matchedCubeMetadata.getCubeFilter().getSourceTablePredicate(), new ParsingOptions()));
        predicate = ExpressionUtils.filterConjuncts(predicate, conjunct -> !sourceFilterPredicateColumns.containsAll(SymbolsExtractor.extractUnique(conjunct).stream().map(Symbol::getName).map(Identifier::new).collect(Collectors.toList())));
        rewritten = new FilterNode(filterNode.getId(), ((FilterNode) filterNode).getSource(), castToRowExpression(predicate));
    }
    return rewritten;
}
Also used : LongSupplier(java.util.function.LongSupplier) PrestoWarning(io.prestosql.spi.PrestoWarning) Patterns.aggregation(io.prestosql.sql.planner.plan.Patterns.aggregation) SystemSessionProperties(io.prestosql.SystemSessionProperties) TypeProvider(io.prestosql.sql.planner.TypeProvider) SqlParser(io.prestosql.sql.parser.SqlParser) AggregationNode(io.prestosql.spi.plan.AggregationNode) WarningCollector(io.prestosql.execution.warnings.WarningCollector) Capture.newCapture(io.prestosql.matching.Capture.newCapture) FilterNode(io.prestosql.spi.plan.FilterNode) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) OriginalExpressionUtils.castToExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToExpression) CubeFilter(io.hetu.core.spi.cube.CubeFilter) Identifier(io.prestosql.sql.tree.Identifier) CubeMetaStore(io.hetu.core.spi.cube.io.CubeMetaStore) SymbolsExtractor(io.prestosql.sql.planner.SymbolsExtractor) TableScanNode(io.prestosql.spi.plan.TableScanNode) Set(java.util.Set) PlanNode(io.prestosql.spi.plan.PlanNode) CubeStatement(io.hetu.core.spi.cube.CubeStatement) ProjectNode(io.prestosql.spi.plan.ProjectNode) Collectors(java.util.stream.Collectors) Metadata(io.prestosql.metadata.Metadata) PlanSymbolAllocator(io.prestosql.sql.planner.PlanSymbolAllocator) Captures(io.prestosql.matching.Captures) List(java.util.List) ExpressionUtils(io.prestosql.sql.ExpressionUtils) Capture(io.prestosql.matching.Capture) AggregationSignature(io.hetu.core.spi.cube.aggregator.AggregationSignature) Optional(java.util.Optional) STAR_TREE(io.prestosql.cube.CubeManager.STAR_TREE) Patterns.optionalSource(io.prestosql.sql.planner.plan.Patterns.optionalSource) Patterns.source(io.prestosql.sql.planner.plan.Patterns.source) TableMetadata(io.prestosql.metadata.TableMetadata) Logger(io.airlift.log.Logger) Pattern(io.prestosql.matching.Pattern) TableHandle(io.prestosql.spi.metadata.TableHandle) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ExpressionDomainTranslator(io.prestosql.sql.planner.ExpressionDomainTranslator) BooleanLiteral(io.prestosql.sql.tree.BooleanLiteral) Objects.requireNonNull(java.util.Objects.requireNonNull) Session(io.prestosql.Session) LinkedList(java.util.LinkedList) ParsingOptions(io.prestosql.sql.parser.ParsingOptions) Symbol(io.prestosql.spi.plan.Symbol) EXPIRED_CUBE(io.prestosql.spi.connector.StandardWarningCode.EXPIRED_CUBE) Rule(io.prestosql.sql.planner.iterative.Rule) TupleDomain(io.prestosql.spi.predicate.TupleDomain) CubeStatementGenerator(io.prestosql.cube.CubeStatementGenerator) SystemSessionProperties.isEnableStarTreeIndex(io.prestosql.SystemSessionProperties.isEnableStarTreeIndex) Patterns.anyPlan(io.prestosql.sql.planner.plan.Patterns.anyPlan) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) CubeManager(io.prestosql.cube.CubeManager) PlanNodeIdAllocator(io.prestosql.spi.plan.PlanNodeIdAllocator) FeaturesConfig(io.prestosql.sql.analyzer.FeaturesConfig) CubeMetadata(io.hetu.core.spi.cube.CubeMetadata) Patterns.tableScan(io.prestosql.sql.planner.plan.Patterns.tableScan) Comparator(java.util.Comparator) Expression(io.prestosql.sql.tree.Expression) Identifier(io.prestosql.sql.tree.Identifier) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) OriginalExpressionUtils.castToExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToExpression) Expression(io.prestosql.sql.tree.Expression) ParsingOptions(io.prestosql.sql.parser.ParsingOptions) Symbol(io.prestosql.spi.plan.Symbol) FilterNode(io.prestosql.spi.plan.FilterNode) SqlParser(io.prestosql.sql.parser.SqlParser)

Example 25 with SqlParser

use of io.prestosql.sql.parser.SqlParser in project hetu-core by openlookeng.

the class StarTreeAggregationRule method filterPredicateMatches.

private boolean filterPredicateMatches(FilterNode filterNode, CubeMetadata cubeMetadata, Session session, TypeProvider types) {
    CubeFilter cubeFilter = cubeMetadata.getCubeFilter();
    if (cubeFilter == null) {
        // Cube was built for entire table
        return filterNode == null || doesCubeContainQueryPredicateColumns(castToExpression(filterNode.getPredicate()), cubeMetadata);
    }
    if (filterNode == null) {
        // Query statement has no WHERE clause but CUBE was built for subset of original data
        return false;
    }
    SqlParser sqlParser = new SqlParser();
    Expression queryPredicate = castToExpression(filterNode.getPredicate());
    Expression sourceTablePredicate = cubeFilter.getSourceTablePredicate() == null ? null : sqlParser.createExpression(cubeFilter.getSourceTablePredicate(), new ParsingOptions());
    Pair<Expression, Expression> splitQueryPredicate = splitQueryPredicate(queryPredicate, sourceTablePredicate);
    if (!arePredicatesEqual(splitQueryPredicate.getLeft(), sourceTablePredicate, metadata, session, types)) {
        LOGGER.debug("Cube source table predicate %s not matching query predicate %s", sourceTablePredicate, queryPredicate);
        return false;
    }
    // Check if columns in query predicate are all part of the Cube.
    if ((cubeFilter.getCubePredicate() != null && splitQueryPredicate.getRight() == null) || (splitQueryPredicate.getRight() != null && !doesCubeContainQueryPredicateColumns(splitQueryPredicate.getRight(), cubeMetadata))) {
        // Cube does not contain all columns in the remaining predicate
        return false;
    }
    if (cubeFilter.getCubePredicate() == null) {
        // Cube has no additional predicates to compare with. i.e. Cube can be used to optimize the query
        return true;
    }
    Expression cubePredicate = ExpressionUtils.rewriteIdentifiersToSymbolReferences(sqlParser.createExpression(cubeFilter.getCubePredicate(), new ParsingOptions()));
    ExpressionDomainTranslator.ExtractionResult decomposedQueryPredicate = ExpressionDomainTranslator.fromPredicate(metadata, session, splitQueryPredicate.getRight(), types);
    if (!BooleanLiteral.TRUE_LITERAL.equals(decomposedQueryPredicate.getRemainingExpression())) {
        LOGGER.error("StarTree cube cannot support predicate %s", castToExpression(filterNode.getPredicate()));
        return false;
    }
    ExpressionDomainTranslator.ExtractionResult decomposedCubePredicate = ExpressionDomainTranslator.fromPredicate(metadata, session, cubePredicate, types);
    if (!BooleanLiteral.TRUE_LITERAL.equals(decomposedCubePredicate.getRemainingExpression())) {
        // Extract disjuncts from the Expression expression and evaluate separately
        return atLeastMatchesOne(ExpressionUtils.extractDisjuncts(cubePredicate), decomposedQueryPredicate.getTupleDomain(), session, types);
    }
    return decomposedCubePredicate.getTupleDomain().contains(decomposedQueryPredicate.getTupleDomain());
}
Also used : OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) OriginalExpressionUtils.castToExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToExpression) Expression(io.prestosql.sql.tree.Expression) ParsingOptions(io.prestosql.sql.parser.ParsingOptions) ExpressionDomainTranslator(io.prestosql.sql.planner.ExpressionDomainTranslator) CubeFilter(io.hetu.core.spi.cube.CubeFilter) SqlParser(io.prestosql.sql.parser.SqlParser)

Aggregations

SqlParser (io.prestosql.sql.parser.SqlParser)29 Expression (io.prestosql.sql.tree.Expression)11 ParsingOptions (io.prestosql.sql.parser.ParsingOptions)10 TypeAnalyzer (io.prestosql.sql.planner.TypeAnalyzer)6 Optional (java.util.Optional)6 BeforeClass (org.testng.annotations.BeforeClass)6 CubeFilter (io.hetu.core.spi.cube.CubeFilter)5 Metadata (io.prestosql.metadata.Metadata)5 Symbol (io.prestosql.spi.plan.Symbol)5 FeaturesConfig (io.prestosql.sql.analyzer.FeaturesConfig)5 OriginalExpressionUtils.castToRowExpression (io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression)5 FunctionCall (io.prestosql.sql.tree.FunctionCall)5 List (java.util.List)5 ImmutableList (com.google.common.collect.ImmutableList)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 Session (io.prestosql.Session)4 CubeManager (io.prestosql.cube.CubeManager)4 CallExpression (io.prestosql.spi.relation.CallExpression)4 VariableReferenceExpression (io.prestosql.spi.relation.VariableReferenceExpression)4 OriginalExpressionUtils.castToExpression (io.prestosql.sql.relational.OriginalExpressionUtils.castToExpression)4