Ejemplo n.º 1
0
  private List<Object> executeProjectionWithAll(
      String projection, Type expectedType, Session session, ExpressionCompiler compiler) {
    requireNonNull(projection, "projection is null");

    Expression projectionExpression = createExpression(projection, metadata, SYMBOL_TYPES);

    List<Object> results = new ArrayList<>();

    //
    // If the projection does not need bound values, execute query using full engine
    if (!needsBoundValue(projectionExpression)) {
      MaterializedResult result = runner.execute("SELECT " + projection);
      assertType(result.getTypes(), expectedType);
      assertEquals(result.getTypes().size(), 1);
      assertEquals(result.getMaterializedRows().size(), 1);
      Object queryResult = Iterables.getOnlyElement(result.getMaterializedRows()).getField(0);
      results.add(queryResult);
    }

    // execute as standalone operator
    OperatorFactory operatorFactory =
        compileFilterProject(TRUE_LITERAL, projectionExpression, compiler);
    assertType(operatorFactory.getTypes(), expectedType);
    Object directOperatorValue = selectSingleValue(operatorFactory, session);
    results.add(directOperatorValue);

    // interpret
    Operator interpretedFilterProject =
        interpretedFilterProject(TRUE_LITERAL, projectionExpression, session);
    assertType(interpretedFilterProject.getTypes(), expectedType);
    Object interpretedValue = selectSingleValue(interpretedFilterProject);
    results.add(interpretedValue);

    // execute over normal operator
    SourceOperatorFactory scanProjectOperatorFactory =
        compileScanFilterProject(TRUE_LITERAL, projectionExpression, compiler);
    assertType(scanProjectOperatorFactory.getTypes(), expectedType);
    Object scanOperatorValue =
        selectSingleValue(scanProjectOperatorFactory, createNormalSplit(), session);
    results.add(scanOperatorValue);

    // execute over record set
    Object recordValue =
        selectSingleValue(scanProjectOperatorFactory, createRecordSetSplit(), session);
    results.add(recordValue);

    //
    // If the projection does not need bound values, execute query using full engine
    if (!needsBoundValue(projectionExpression)) {
      MaterializedResult result = runner.execute("SELECT " + projection);
      assertType(result.getTypes(), expectedType);
      assertEquals(result.getTypes().size(), 1);
      assertEquals(result.getMaterializedRows().size(), 1);
      Object queryResult = Iterables.getOnlyElement(result.getMaterializedRows()).getField(0);
      results.add(queryResult);
    }

    return results;
  }
Ejemplo n.º 2
0
  private static Page getAtMostOnePage(Operator operator, Page sourcePage) {
    // add our input page if needed
    if (operator.needsInput()) {
      operator.addInput(sourcePage);
    }

    // try to get the output page
    Page result = operator.getOutput();

    // tell operator to finish
    operator.finish();

    // try to get output until the operator is finished
    while (!operator.isFinished()) {
      // operator should never block
      assertTrue(operator.isBlocked().isDone());

      Page output = operator.getOutput();
      if (output != null) {
        assertNull(result);
        result = output;
      }
    }

    return result;
  }
Ejemplo n.º 3
0
  private Object selectSingleValue(Operator operator) {
    Page output = getAtMostOnePage(operator, SOURCE_PAGE);

    assertNotNull(output);
    assertEquals(output.getPositionCount(), 1);
    assertEquals(output.getChannelCount(), 1);
    Type type = operator.getTypes().get(0);

    Block block = output.getBlock(0);
    assertEquals(block.getPositionCount(), 1);

    return type.getObjectValue(session.toConnectorSession(), block, 0);
  }
Ejemplo n.º 4
0
  private static boolean executeFilter(Operator operator) {
    Page page = getAtMostOnePage(operator, SOURCE_PAGE);

    boolean value;
    if (page != null) {
      assertEquals(page.getPositionCount(), 1);
      assertEquals(page.getChannelCount(), 1);

      assertTrue(operator.getTypes().get(0).getBoolean(page.getBlock(0), 0));
      value = true;
    } else {
      value = false;
    }
    return value;
  }
Ejemplo n.º 5
0
  private void destroyIfNecessary() {
    checkLockHeld("Lock must be held to call destroyIfNecessary");

    if (!state.compareAndSet(State.NEED_DESTRUCTION, State.DESTROYED)) {
      return;
    }

    Throwable inFlightException = null;
    try {
      // call finish on every operator; if error occurs, just bail out; we will still call close
      for (Operator operator : operators) {
        operator.finish();
      }
    } catch (Throwable t) {
      // record in flight exception so we can add suppressed exceptions below
      inFlightException = t;
      throw t;
    } finally {
      // record the current interrupted status (and clear the flag); we'll reset it later
      boolean wasInterrupted = Thread.interrupted();

      // if we get an error while closing a driver, record it and we will throw it at the end
      try {
        for (Operator operator : operators) {
          if (operator instanceof AutoCloseable) {
            try {
              ((AutoCloseable) operator).close();
            } catch (InterruptedException t) {
              // don't record the stack
              wasInterrupted = true;
            } catch (Throwable t) {
              inFlightException =
                  addSuppressedException(
                      inFlightException,
                      t,
                      "Error closing operator %s for task %s",
                      operator.getOperatorContext().getOperatorId(),
                      driverContext.getTaskId());
            }
          }
        }
        driverContext.finished();
      } catch (Throwable t) {
        // this shouldn't happen but be safe
        inFlightException =
            addSuppressedException(
                inFlightException,
                t,
                "Error destroying driver for task %s",
                driverContext.getTaskId());
      } finally {
        // reset the interrupted flag
        if (wasInterrupted) {
          Thread.currentThread().interrupt();
        }
      }

      if (inFlightException != null) {
        // this will always be an Error or Runtime
        throw Throwables.propagate(inFlightException);
      }
    }
  }
Ejemplo n.º 6
0
  public ListenableFuture<?> process() {
    checkLockNotHeld("Can not process while holding the driver lock");

    try (DriverLockResult lockResult =
        tryLockAndProcessPendingStateChanges(100, TimeUnit.MILLISECONDS)) {
      try {
        if (!lockResult.wasAcquired()) {
          // this is unlikely to happen unless the driver is being
          // destroyed and in that case the caller should notice notice
          // this state change by calling isFinished
          return NOT_BLOCKED;
        }

        driverContext.start();

        if (!newSources.isEmpty()) {
          processNewSources();
        }

        for (int i = 0; i < operators.size() - 1 && !driverContext.isDone(); i++) {
          // check if current operator is blocked
          Operator current = operators.get(i);
          ListenableFuture<?> blocked = current.isBlocked();
          if (!blocked.isDone()) {
            current.getOperatorContext().recordBlocked(blocked);
            return blocked;
          }

          // check if next operator is blocked
          Operator next = operators.get(i + 1);
          blocked = next.isBlocked();
          if (!blocked.isDone()) {
            next.getOperatorContext().recordBlocked(blocked);
            return blocked;
          }

          // if current operator is finished...
          if (current.isFinished()) {
            // let next operator know there will be no more data
            next.getOperatorContext().startIntervalTimer();
            next.finish();
            next.getOperatorContext().recordFinish();
          } else {
            // if next operator needs input...
            if (next.needsInput()) {
              // get an output page from current operator
              current.getOperatorContext().startIntervalTimer();
              Page page = current.getOutput();
              current.getOperatorContext().recordGetOutput(page);

              // if we got an output page, add it to the next operator
              if (page != null) {
                next.getOperatorContext().startIntervalTimer();
                next.addInput(page);
                next.getOperatorContext().recordAddInput(page);
              }
            }
          }
        }
        return NOT_BLOCKED;
      } catch (Throwable t) {
        driverContext.failed(t);
        throw t;
      }
    }
  }