Ejemplo n.º 1
0
 void saveBatch(boolean force) throws TeiidComponentException {
   Assertion.assertTrue(!this.isRemoved());
   if (batchBuffer == null
       || batchBuffer.isEmpty()
       || (!force && batchBuffer.size() < Math.max(1, batchSize / 32))) {
     return;
   }
   Long mbatch = manager.createManagedBatch(batchBuffer, null, false);
   this.batches.put(rowCount - batchBuffer.size() + 1, mbatch);
   batchBuffer = null;
 }
Ejemplo n.º 2
0
  public Set<Object> getCachedSet(Expression valueExpression)
      throws TeiidComponentException, TeiidProcessingException {
    Set<Object> result = null;
    if (cachedSets != null) {
      result = cachedSets.get(valueExpression);
    }
    if (result == null) {
      if (buffer.getRowCount() > buffer.getBatchSize()) {
        return null;
      }
      TupleSourceValueIterator ve = getValueIterator(valueExpression);
      int index = 0;
      Class<?> type = null;
      if (valueExpression instanceof Array) {
        type = ((Array) valueExpression).getComponentType();
      } else {
        if (valueExpression != null) {
          index = schema.indexOf(valueExpression);
          Assertion.assertTrue(index != -1);
        }
        type = ((Expression) schema.get(index)).getType();
      }

      if (!DataTypeManager.isHashable(type)) {
        result = new TreeSet<Object>(Constant.COMPARATOR);
      } else {
        result = new HashSet<Object>();
      }
      while (ve.hasNext()) {
        Object value = ve.next();
        if (value != null) {
          result.add(value);
        }
      }
      ve.close();
      if (cachedSets == null) {
        cachedSets = new HashMap<Expression, Set<Object>>();
      }
      cachedSets.put(valueExpression, result);
    }
    return result;
  }
Ejemplo n.º 3
0
  private RelationalNode convertPlan(PlanNode planNode)
      throws TeiidComponentException, TeiidProcessingException {

    // Convert current node in planTree
    RelationalNode convertedNode = convertNode(planNode);

    if (convertedNode == null) {
      Assertion.assertTrue(planNode.getChildCount() == 1);
      return convertPlan(planNode.getFirstChild());
    }

    RelationalNode nextParent = convertedNode;

    // convertedNode may be the head of 1 or more nodes   - go to end of chain
    while (nextParent.getChildren()[0] != null) {
      nextParent = nextParent.getChildren()[0];
    }

    // Call convertPlan recursively on children
    for (PlanNode childNode : planNode.getChildren()) {
      RelationalNode child = convertPlan(childNode);
      if (planNode.getType() == NodeConstants.Types.SET_OP
          && nextParent instanceof UnionAllNode
          && childNode.getProperty(Info.SET_OPERATION) == childNode.getProperty(Info.SET_OPERATION)
          && childNode.getType() == NodeConstants.Types.SET_OP
          && childNode.hasBooleanProperty(Info.USE_ALL)) {
        for (RelationalNode grandChild : child.getChildren()) {
          if (grandChild != null) {
            nextParent.addChild(grandChild);
          }
        }
      } else {
        nextParent.addChild(child);
      }
    }

    // Return root of tree for top node
    return convertedNode;
  }
Ejemplo n.º 4
0
  private TupleBatch nextBatchDirect()
      throws BlockedException, TeiidProcessingException, TeiidComponentException {

    boolean done = false;
    TupleBatch result = null;

    try {
      init();
      long currentTime = System.currentTimeMillis();
      Assertion.assertTrue(!processorClosed);

      // TODO: see if there is pending work before preempting

      while (currentTime < context.getTimeSliceEnd() || context.isNonBlocking()) {
        if (requestCanceled) {
          throw new TeiidProcessingException(
              QueryPlugin.Event.TEIID30160,
              QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30160, getProcessID()));
        }
        if (currentTime > context.getTimeoutEnd()) {
          throw new TeiidProcessingException(
              QueryPlugin.Event.TEIID30161, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30161));
        }
        result = processPlan.nextBatch();

        if (continuous) {
          result.setRowOffset(rowOffset);

          if (result.getTerminationFlag()) {
            result.setTermination(TupleBatch.ITERATION_TERMINATED);
            List<Object> terminationTuple =
                Arrays.asList(new Object[this.getOutputElements().size()]);
            result.getTuples().add(terminationTuple);
            this.context.getTupleSourceCache().close();
            this.processPlan.close();
            this.processPlan.reset();
            this.context.incrementReuseCount();
            this.open = false;
          }

          rowOffset = result.getEndRow() + 1;
        }

        if (result.getTermination() != TupleBatch.NOT_TERMINATED) {
          if (result.getTerminationFlag()) {
            done = true;
          }
          break;
        }

        if (result.getRowCount() > 0) {
          break;
        }
      }
    } catch (BlockedException e) {
      throw e;
    } catch (TeiidException e) {
      closeProcessing();
      if (e instanceof TeiidProcessingException) {
        throw (TeiidProcessingException) e;
      }
      if (e instanceof TeiidComponentException) {
        throw (TeiidComponentException) e;
      }
      throw new TeiidComponentException(QueryPlugin.Event.TEIID30162, e);
    }
    if (done) {
      closeProcessing();
    }
    if (result == null) {
      throw EXPIRED_TIME_SLICE;
    }
    return result;
  }
Ejemplo n.º 5
0
 private int getIndex(Expression valueExpression) {
   int index = schema.indexOf(valueExpression);
   Assertion.assertTrue(index != -1);
   return index;
 }
Ejemplo n.º 6
0
  /**
   * @param command
   * @param rsName
   * @param procAssignments
   * @param mode
   * @param usesLocalTemp - only matters in HOLD mode
   * @throws TeiidComponentException
   * @throws TeiidProcessingException
   */
  public void executePlan(
      ProcessorPlan command,
      String rsName,
      Map<ElementSymbol, ElementSymbol> procAssignments,
      CreateCursorResultSetInstruction.Mode mode,
      boolean usesLocalTemp)
      throws TeiidComponentException, TeiidProcessingException {

    CursorState state = (CursorState) this.cursorStates.getValue(rsName);
    if (state == null || rsName == null) {
      if (this.currentState != null && this.currentState.processor.getProcessorPlan() != command) {
        // sanity check for non-deterministic paths
        removeState(this.currentState);
        this.currentState = null;
      }
      if (this.currentState == null) {
        // this may not be the first time the plan is being run
        command.reset();

        CommandContext subContext = getContext().clone();
        subContext.setVariableContext(this.currentVarContext);
        state = new CursorState();
        state.usesLocalTemp = usesLocalTemp;
        state.processor = new QueryProcessor(command, subContext, this.bufferMgr, this);
        state.ts = new BatchIterator(state.processor);
        if (mode == Mode.HOLD
            && procAssignments != null
            && state.processor.getOutputElements().size() - procAssignments.size() > 0) {
          state.resultsBuffer =
              bufferMgr.createTupleBuffer(
                  state
                      .processor
                      .getOutputElements()
                      .subList(
                          0, state.processor.getOutputElements().size() - procAssignments.size()),
                  getContext().getConnectionId(),
                  TupleSourceType.PROCESSOR);
        } else if ((this.blockContext != null || this.programs.peek().isTrappingExceptions())
            && (mode == Mode.HOLD || rsName != null)) {
          state.resultsBuffer =
              bufferMgr.createTupleBuffer(
                  state.processor.getOutputElements(),
                  getContext().getConnectionId(),
                  TupleSourceType.PROCESSOR);
        }
        this.currentState = state;
      }
      // force execution to the first batch to ensure that the plan is executed in the context of
      // the procedure
      this.currentState.ts.hasNext();
      if (procAssignments != null) {
        // proc assignments force us to scroll through the entire results and save as we go
        while (this.currentState.ts.hasNext()) {
          if (this.currentState.currentRow != null && this.currentState.resultsBuffer != null) {
            this.currentState.resultsBuffer.addTuple(
                this.currentState.currentRow.subList(
                    0, this.currentState.resultsBuffer.getSchema().size()));
            this.currentState.currentRow = null;
          }
          this.currentState.currentRow = this.currentState.ts.nextTuple();
        }
        // process assignments
        Assertion.assertTrue(this.currentState.currentRow != null);
        for (Map.Entry<ElementSymbol, ElementSymbol> entry : procAssignments.entrySet()) {
          if (entry.getValue() == null
              || !metadata.elementSupports(
                  entry.getValue().getMetadataID(), SupportConstants.Element.UPDATE)) {
            continue;
          }
          int index = this.currentState.processor.getOutputElements().indexOf(entry.getKey());
          getCurrentVariableContext()
              .setValue(
                  entry.getValue(),
                  DataTypeManager.transformValue(
                      this.currentState.currentRow.get(index), entry.getValue().getType()));
        }
      } else if (this.currentState.resultsBuffer != null) {
        // result should be saved, typically to respect txn semantics
        while (this.currentState.ts.hasNext()) {
          List<?> tuple = this.currentState.ts.nextTuple();
          this.currentState.resultsBuffer.addTuple(tuple);
        }
        getCurrentVariableContext().setValue(ProcedurePlan.ROWCOUNT, 0);
      } else if (mode == Mode.UPDATE) {
        List<?> t = this.currentState.ts.nextTuple();
        if (this.currentState.ts.hasNext()) {
          throw new AssertionError(
              "Invalid update count result - more than 1 row returned"); //$NON-NLS-1$
        }
        removeState(this.currentState);
        this.currentState = null;
        int rowCount = 0;
        if (t != null) {
          rowCount = (Integer) t.get(0);
        }
        getCurrentVariableContext().setValue(ProcedurePlan.ROWCOUNT, rowCount);
        return;
      }
      if (rsName == null && mode == Mode.NOHOLD) {
        // unnamed without hold
        // process fully, but don't save
        // TODO: could set the rowcount in this case
        while (this.currentState.ts.hasNext()) {
          this.currentState.ts.nextTuple();
        }
        this.currentState = null;
        getCurrentVariableContext().setValue(ProcedurePlan.ROWCOUNT, 0);
        return;
      }
      if (this.currentState.resultsBuffer != null) {
        // close the results buffer and use a buffer backed tuplesource
        this.currentState.resultsBuffer.close();
        this.currentState.ts = this.currentState.resultsBuffer.createIndexedTupleSource(true);
      }
      CursorState old = (CursorState) this.cursorStates.setValue(rsName, this.currentState);
      if (old != null) {
        removeState(old);
      }
      this.currentState = null;
    }
  }