use of io.trino.metadata.Metadata in project trino by trinodb.
the class SimplifyFilterPredicate method simplifyFilterExpression.
private Optional<Expression> simplifyFilterExpression(Expression expression) {
if (expression instanceof IfExpression) {
IfExpression ifExpression = (IfExpression) expression;
Expression condition = ifExpression.getCondition();
Expression trueValue = ifExpression.getTrueValue();
Optional<Expression> falseValue = ifExpression.getFalseValue();
if (trueValue.equals(TRUE_LITERAL) && (falseValue.isEmpty() || isNotTrue(falseValue.get()))) {
return Optional.of(condition);
}
if (isNotTrue(trueValue) && falseValue.isPresent() && falseValue.get().equals(TRUE_LITERAL)) {
return Optional.of(isFalseOrNullPredicate(condition));
}
if (falseValue.isPresent() && falseValue.get().equals(trueValue) && isDeterministic(trueValue, metadata)) {
return Optional.of(trueValue);
}
if (isNotTrue(trueValue) && (falseValue.isEmpty() || isNotTrue(falseValue.get()))) {
return Optional.of(FALSE_LITERAL);
}
if (condition.equals(TRUE_LITERAL)) {
return Optional.of(trueValue);
}
if (isNotTrue(condition)) {
return Optional.of(falseValue.orElse(FALSE_LITERAL));
}
return Optional.empty();
}
if (expression instanceof NullIfExpression) {
NullIfExpression nullIfExpression = (NullIfExpression) expression;
return Optional.of(LogicalExpression.and(nullIfExpression.getFirst(), isFalseOrNullPredicate(nullIfExpression.getSecond())));
}
if (expression instanceof SearchedCaseExpression) {
SearchedCaseExpression caseExpression = (SearchedCaseExpression) expression;
Optional<Expression> defaultValue = caseExpression.getDefaultValue();
List<Expression> operands = caseExpression.getWhenClauses().stream().map(WhenClause::getOperand).collect(toImmutableList());
List<Expression> results = caseExpression.getWhenClauses().stream().map(WhenClause::getResult).collect(toImmutableList());
long trueResultsCount = results.stream().filter(result -> result.equals(TRUE_LITERAL)).count();
long notTrueResultsCount = results.stream().filter(SimplifyFilterPredicate::isNotTrue).count();
// all results true
if (trueResultsCount == results.size() && defaultValue.isPresent() && defaultValue.get().equals(TRUE_LITERAL)) {
return Optional.of(TRUE_LITERAL);
}
// all results not true
if (notTrueResultsCount == results.size() && (defaultValue.isEmpty() || isNotTrue(defaultValue.get()))) {
return Optional.of(FALSE_LITERAL);
}
// one result true, and remaining results not true
if (trueResultsCount == 1 && notTrueResultsCount == results.size() - 1 && (defaultValue.isEmpty() || isNotTrue(defaultValue.get()))) {
ImmutableList.Builder<Expression> builder = ImmutableList.builder();
for (WhenClause whenClause : caseExpression.getWhenClauses()) {
Expression operand = whenClause.getOperand();
Expression result = whenClause.getResult();
if (isNotTrue(result)) {
builder.add(isFalseOrNullPredicate(operand));
} else {
builder.add(operand);
return Optional.of(combineConjuncts(metadata, builder.build()));
}
}
}
// all results not true, and default true
if (notTrueResultsCount == results.size() && defaultValue.isPresent() && defaultValue.get().equals(TRUE_LITERAL)) {
ImmutableList.Builder<Expression> builder = ImmutableList.builder();
operands.stream().forEach(operand -> builder.add(isFalseOrNullPredicate(operand)));
return Optional.of(combineConjuncts(metadata, builder.build()));
}
// skip clauses with not true conditions
List<WhenClause> whenClauses = new ArrayList<>();
for (WhenClause whenClause : caseExpression.getWhenClauses()) {
Expression operand = whenClause.getOperand();
if (operand.equals(TRUE_LITERAL)) {
if (whenClauses.isEmpty()) {
return Optional.of(whenClause.getResult());
}
return Optional.of(new SearchedCaseExpression(whenClauses, Optional.of(whenClause.getResult())));
}
if (!isNotTrue(operand)) {
whenClauses.add(whenClause);
}
}
if (whenClauses.isEmpty()) {
return Optional.of(defaultValue.orElse(FALSE_LITERAL));
}
if (whenClauses.size() < caseExpression.getWhenClauses().size()) {
return Optional.of(new SearchedCaseExpression(whenClauses, defaultValue));
}
return Optional.empty();
}
if (expression instanceof SimpleCaseExpression) {
SimpleCaseExpression caseExpression = (SimpleCaseExpression) expression;
Optional<Expression> defaultValue = caseExpression.getDefaultValue();
if (caseExpression.getOperand() instanceof NullLiteral) {
return Optional.of(defaultValue.orElse(FALSE_LITERAL));
}
List<Expression> results = caseExpression.getWhenClauses().stream().map(WhenClause::getResult).collect(toImmutableList());
if (results.stream().allMatch(result -> result.equals(TRUE_LITERAL)) && defaultValue.isPresent() && defaultValue.get().equals(TRUE_LITERAL)) {
return Optional.of(TRUE_LITERAL);
}
if (results.stream().allMatch(SimplifyFilterPredicate::isNotTrue) && (defaultValue.isEmpty() || isNotTrue(defaultValue.get()))) {
return Optional.of(FALSE_LITERAL);
}
return Optional.empty();
}
return Optional.empty();
}
use of io.trino.metadata.Metadata in project trino by trinodb.
the class PushPredicateIntoTableScan method splitExpression.
private static SplitExpression splitExpression(PlannerContext plannerContext, Expression predicate) {
Metadata metadata = plannerContext.getMetadata();
List<Expression> dynamicFilters = new ArrayList<>();
List<Expression> deterministicPredicates = new ArrayList<>();
List<Expression> nonDeterministicPredicate = new ArrayList<>();
for (Expression conjunct : extractConjuncts(predicate)) {
if (isDynamicFilter(conjunct)) {
// dynamic filters have no meaning for connectors, so don't pass them
dynamicFilters.add(conjunct);
} else {
if (isDeterministic(conjunct, metadata)) {
deterministicPredicates.add(conjunct);
} else {
// don't include non-deterministic predicates
nonDeterministicPredicate.add(conjunct);
}
}
}
return new SplitExpression(combineConjuncts(metadata, dynamicFilters), combineConjuncts(metadata, deterministicPredicates), combineConjuncts(metadata, nonDeterministicPredicate));
}
use of io.trino.metadata.Metadata in project trino by trinodb.
the class BaseHiveConnectorTest method getTableMetadata.
private TableMetadata getTableMetadata(String catalog, String schema, String tableName) {
Session session = getSession();
Metadata metadata = getDistributedQueryRunner().getCoordinator().getMetadata();
return transaction(getQueryRunner().getTransactionManager(), getQueryRunner().getAccessControl()).readOnly().execute(session, transactionSession -> {
Optional<TableHandle> tableHandle = metadata.getTableHandle(transactionSession, new QualifiedObjectName(catalog, schema, tableName));
assertTrue(tableHandle.isPresent());
return metadata.getTableMetadata(transactionSession, tableHandle.get());
});
}
use of io.trino.metadata.Metadata in project trino by trinodb.
the class TestSetRoleTask method setUp.
@BeforeClass
public void setUp() {
queryRunner = LocalQueryRunner.create(TEST_SESSION);
MockConnectorFactory mockConnectorFactory = MockConnectorFactory.builder().withListRoleGrants((connectorSession, roles, grantees, limit) -> ImmutableSet.of(new RoleGrant(new TrinoPrincipal(USER, USER_NAME), ROLE_NAME, false))).build();
queryRunner.createCatalog(CATALOG_NAME, mockConnectorFactory, ImmutableMap.of());
MockConnectorFactory systemConnectorFactory = MockConnectorFactory.builder().withName("system_role_connector").build();
queryRunner.createCatalog(SYSTEM_ROLE_CATALOG_NAME, systemConnectorFactory, ImmutableMap.of());
transactionManager = queryRunner.getTransactionManager();
accessControl = queryRunner.getAccessControl();
metadata = queryRunner.getMetadata();
parser = queryRunner.getSqlParser();
executor = newCachedThreadPool(daemonThreadsNamed("test-set-role-task-executor-%s"));
}
use of io.trino.metadata.Metadata in project trino by trinodb.
the class TestQueryStateMachine method createQueryStateMachineWithTicker.
private QueryStateMachine createQueryStateMachineWithTicker(Ticker ticker) {
Metadata metadata = createTestMetadataManager();
TransactionManager transactionManager = createTestTransactionManager();
AccessControlManager accessControl = new AccessControlManager(transactionManager, emptyEventListenerManager(), new AccessControlConfig(), DefaultSystemAccessControl.NAME);
accessControl.setSystemAccessControls(List.of(AllowAllSystemAccessControl.INSTANCE));
QueryStateMachine stateMachine = QueryStateMachine.beginWithTicker(Optional.empty(), QUERY, Optional.empty(), TEST_SESSION, LOCATION, new ResourceGroupId("test"), false, transactionManager, accessControl, executor, ticker, metadata, WarningCollector.NOOP, QUERY_TYPE);
stateMachine.setInputs(INPUTS);
stateMachine.setOutput(OUTPUT);
stateMachine.setColumns(OUTPUT_FIELD_NAMES, OUTPUT_FIELD_TYPES);
stateMachine.setUpdateType(UPDATE_TYPE);
for (Entry<String, String> entry : SET_SESSION_PROPERTIES.entrySet()) {
stateMachine.addSetSessionProperties(entry.getKey(), entry.getValue());
}
RESET_SESSION_PROPERTIES.forEach(stateMachine::addResetSessionProperties);
return stateMachine;
}
Aggregations