Search in sources :

Example 86 with Session

use of com.facebook.presto.Session in project presto by prestodb.

the class AbstractTestQueries method testExecuteUsingWithSubquery.

@Test
public void testExecuteUsingWithSubquery() {
    String query = "SELECT ? in (SELECT orderkey FROM orders)";
    Session session = Session.builder(getSession()).addPreparedStatement("my_query", query).build();
    assertQuery(session, "EXECUTE my_query USING 10", "SELECT 10 in (SELECT orderkey FROM orders)");
}
Also used : Session(com.facebook.presto.Session) Test(org.testng.annotations.Test)

Example 87 with Session

use of com.facebook.presto.Session in project presto by prestodb.

the class TransactionBuilder method execute.

public <T> T execute(Session session, Function<Session, T> callback) {
    requireNonNull(session, "session is null");
    requireNonNull(callback, "callback is null");
    boolean managedTransaction = !session.getTransactionId().isPresent();
    Session transactionSession;
    if (managedTransaction) {
        TransactionId transactionId = transactionManager.beginTransaction(isolationLevel, readOnly, singleStatement);
        transactionSession = session.beginTransactionId(transactionId, transactionManager, accessControl);
    } else {
        // Check if we can merge with the existing transaction
        TransactionInfo transactionInfo = transactionManager.getTransactionInfo(session.getTransactionId().get());
        checkState(transactionInfo.getIsolationLevel().meetsRequirementOf(isolationLevel), "Cannot provide %s isolation with existing transaction isolation: %s", isolationLevel, transactionInfo.getIsolationLevel());
        checkState(!transactionInfo.isReadOnly() || readOnly, "Cannot provide read-write semantics with existing read-only transaction");
        checkState(!transactionInfo.isAutoCommitContext() && !singleStatement, "Cannot combine auto commit transactions");
        transactionSession = session;
    }
    boolean success = false;
    try {
        T result = callback.apply(transactionSession);
        success = true;
        return result;
    } finally {
        if (managedTransaction) {
            if (success) {
                getFutureValue(transactionManager.asyncCommit(transactionSession.getTransactionId().get()));
            } else {
                transactionManager.asyncAbort(transactionSession.getTransactionId().get());
            }
        }
    }
}
Also used : Session(com.facebook.presto.Session)

Example 88 with Session

use of com.facebook.presto.Session in project presto by prestodb.

the class AggregationMatcher method detailMatches.

@Override
public MatchResult detailMatches(PlanNode node, Session session, Metadata metadata, SymbolAliases symbolAliases) {
    checkState(shapeMatches(node), "Plan testing framework error: shapeMatches returned false in detailMatches in %s", this.getClass().getName());
    AggregationNode aggregationNode = (AggregationNode) node;
    if (groupId.isPresent() != aggregationNode.getGroupIdSymbol().isPresent()) {
        return NO_MATCH;
    }
    if (groupingSets.size() != aggregationNode.getGroupingSets().size()) {
        return NO_MATCH;
    }
    List<Symbol> aggregationsWithMask = aggregationNode.getAggregations().entrySet().stream().filter(entry -> entry.getValue().isDistinct()).map(entry -> entry.getKey()).collect(Collectors.toList());
    if (aggregationsWithMask.size() != masks.keySet().size()) {
        return NO_MATCH;
    }
    for (Symbol symbol : aggregationsWithMask) {
        if (!masks.keySet().contains(symbol)) {
            return NO_MATCH;
        }
    }
    for (int i = 0; i < groupingSets.size(); i++) {
        if (!matches(groupingSets.get(i), aggregationNode.getGroupingSets().get(i), symbolAliases)) {
            return NO_MATCH;
        }
    }
    return match();
}
Also used : Session(com.facebook.presto.Session) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) Preconditions.checkState(com.google.common.base.Preconditions.checkState) List(java.util.List) Symbol(com.facebook.presto.sql.planner.Symbol) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) Map(java.util.Map) MatchResult.match(com.facebook.presto.sql.planner.assertions.MatchResult.match) Optional(java.util.Optional) NO_MATCH(com.facebook.presto.sql.planner.assertions.MatchResult.NO_MATCH) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) AggregationNode(com.facebook.presto.sql.planner.plan.AggregationNode) Metadata(com.facebook.presto.metadata.Metadata) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) Symbol(com.facebook.presto.sql.planner.Symbol) AggregationNode(com.facebook.presto.sql.planner.plan.AggregationNode)

Example 89 with Session

use of com.facebook.presto.Session in project presto by prestodb.

the class JoinMatcher method detailMatches.

@Override
public MatchResult detailMatches(PlanNode node, Session session, Metadata metadata, SymbolAliases symbolAliases) {
    checkState(shapeMatches(node), "Plan testing framework error: shapeMatches returned false in detailMatches in %s", this.getClass().getName());
    JoinNode joinNode = (JoinNode) node;
    if (joinNode.getCriteria().size() != equiCriteria.size()) {
        return NO_MATCH;
    }
    if (filter.isPresent()) {
        if (!joinNode.getFilter().isPresent()) {
            return NO_MATCH;
        }
        if (!new ExpressionVerifier(symbolAliases).process(joinNode.getFilter().get(), filter.get())) {
            return NO_MATCH;
        }
    } else {
        if (joinNode.getFilter().isPresent()) {
            return NO_MATCH;
        }
    }
    /*
         * Have to use order-independent comparison; there are no guarantees what order
         * the equi criteria will have after planning and optimizing.
         */
    Set<JoinNode.EquiJoinClause> actual = ImmutableSet.copyOf(joinNode.getCriteria());
    Set<JoinNode.EquiJoinClause> expected = equiCriteria.stream().map(maker -> maker.getExpectedValue(symbolAliases)).collect(toImmutableSet());
    return new MatchResult(expected.equals(actual));
}
Also used : JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) ImmutableSet(com.google.common.collect.ImmutableSet) Session(com.facebook.presto.Session) Set(java.util.Set) Preconditions.checkState(com.google.common.base.Preconditions.checkState) List(java.util.List) Expression(com.facebook.presto.sql.tree.Expression) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) Objects.requireNonNull(java.util.Objects.requireNonNull) Optional(java.util.Optional) NO_MATCH(com.facebook.presto.sql.planner.assertions.MatchResult.NO_MATCH) ImmutableCollectors.toImmutableSet(com.facebook.presto.util.ImmutableCollectors.toImmutableSet) Metadata(com.facebook.presto.metadata.Metadata) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode)

Example 90 with Session

use of com.facebook.presto.Session in project presto by prestodb.

the class MemoryLocalQueryRunner method createMemoryLocalQueryRunner.

private static LocalQueryRunner createMemoryLocalQueryRunner() {
    Session.SessionBuilder sessionBuilder = testSessionBuilder().setCatalog("memory").setSchema("default");
    Session session = sessionBuilder.build();
    LocalQueryRunner localQueryRunner = LocalQueryRunner.queryRunnerWithInitialTransaction(session);
    // add tpch
    localQueryRunner.createCatalog("tpch", new TpchConnectorFactory(1), ImmutableMap.<String, String>of());
    localQueryRunner.createCatalog("memory", new MemoryConnectorFactory(), ImmutableMap.<String, String>of());
    return localQueryRunner;
}
Also used : TpchConnectorFactory(com.facebook.presto.tpch.TpchConnectorFactory) MemoryConnectorFactory(com.facebook.presto.plugin.memory.MemoryConnectorFactory) LocalQueryRunner(com.facebook.presto.testing.LocalQueryRunner) Session(com.facebook.presto.Session)

Aggregations

Session (com.facebook.presto.Session)121 Test (org.testng.annotations.Test)61 QueryId (com.facebook.presto.spi.QueryId)21 MaterializedResult (com.facebook.presto.testing.MaterializedResult)20 SemanticException (com.facebook.presto.sql.analyzer.SemanticException)19 PrestoException (com.facebook.presto.spi.PrestoException)17 TransactionManager (com.facebook.presto.transaction.TransactionManager)16 AccessControl (com.facebook.presto.security.AccessControl)15 AllowAllAccessControl (com.facebook.presto.security.AllowAllAccessControl)14 TransactionManager.createTestTransactionManager (com.facebook.presto.transaction.TransactionManager.createTestTransactionManager)14 QualifiedObjectName (com.facebook.presto.metadata.QualifiedObjectName)13 AccessControlManager (com.facebook.presto.security.AccessControlManager)13 ConnectorId (com.facebook.presto.connector.ConnectorId)12 LocalQueryRunner (com.facebook.presto.testing.LocalQueryRunner)12 Type (com.facebook.presto.spi.type.Type)11 List (java.util.List)11 TableHandle (com.facebook.presto.metadata.TableHandle)10 TpchConnectorFactory (com.facebook.presto.tpch.TpchConnectorFactory)10 Optional (java.util.Optional)10 ColumnHandle (com.facebook.presto.spi.ColumnHandle)9