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;
}
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)));
}
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());
}
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;
}
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());
}
Aggregations