Esempio n. 1
0
  public ObjectExecution(
      Select query,
      @SuppressWarnings("unused") RuntimeMetadata metadata,
      ObjectExecutionFactory factory,
      ObjectConnection connection,
      ExecutionContext executionContext)
      throws TranslatorException {
    this.factory = factory;
    this.query = query;
    this.connection = connection;
    this.executionContext = executionContext;

    projects = new ArrayList<CompiledScript>(query.getDerivedColumns().size());
    for (DerivedColumn dc : query.getDerivedColumns()) {
      ColumnReference cr = (ColumnReference) dc.getExpression();
      String name = null;
      if (cr.getMetadataObject() != null) {
        Column c = cr.getMetadataObject();
        name = getNameInSource(c);
      } else {
        name = cr.getName();
      }
      if (name.equalsIgnoreCase("this")) { // $NON-NLS-1$
        projects.add(null);
      } else {
        try {
          projects.add(scriptEngine.compile(OBJECT_NAME + "." + name)); // $NON-NLS-1$
        } catch (ScriptException e) {
          throw new TranslatorException(e);
        }
      }
    }
  }
Esempio n. 2
0
 @Override
 public void visit(DerivedColumn obj) {
   if (obj.getExpression() instanceof Function) {
     visitNode(obj.getExpression());
   } else {
     ColumnReference column = (ColumnReference) obj.getExpression();
     this.selectedColumns.add(column.getMetadataObject().getName());
   }
 }
  @Override
  public void visit(DerivedColumn obj) {
    this.currentAlias = buildAlias(obj.getAlias());
    visitNode(obj.getExpression());

    Column column = (Column) this.onGoingExpression.pop();

    String CF = column.getProperty(AccumuloMetadataProcessor.CF, false);
    String CQ = column.getProperty(AccumuloMetadataProcessor.CQ, false);
    if (CQ != null) {
      this.keybasedColumnMap.put(CF + "/" + CQ, column); // $NON-NLS-1$
    } else {
      this.keybasedColumnMap.put(CF, column);
    }

    // no expressions in select are allowed.
    this.selectColumns.add(column);
  }
  @Override
  public List<?> translateCommand(Command command, ExecutionContext context) {
    if (command instanceof Insert) {
      try {
        handleInsertSequences((Insert) command);
      } catch (TranslatorException e) {
        throw new RuntimeException(e);
      }
    }

    if (!(command instanceof QueryExpression)) {
      return null;
    }
    QueryExpression queryCommand = (QueryExpression) command;
    if (queryCommand.getLimit() == null) {
      return null;
    }
    Limit limit = queryCommand.getLimit();
    queryCommand.setLimit(null);

    if (command instanceof Select) {
      Select select = (Select) command;

      TableReference tr = select.getFrom().get(0);
      if (tr instanceof NamedTable && isDual((NamedTable) tr)) {
        if (limit.getRowOffset() > 0 || limit.getRowLimit() == 0) {
          // no data
          select.setWhere(
              new Comparison(
                  new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER),
                  new Literal(0, TypeFacility.RUNTIME_TYPES.INTEGER),
                  Operator.EQ));
          return null;
        }
        return null; // dual does not allow a limit
      }
    }

    List<Object> parts = new ArrayList<Object>();
    parts.add("SELECT "); // $NON-NLS-1$
    /*
     * if all of the columns are aliased, assume that names matter - it actually only seems to matter for
     * the first query of a set op when there is a order by.  Rather than adding logic to traverse up,
     * we just use the projected names
     */
    boolean allAliased = true;
    for (DerivedColumn selectSymbol : queryCommand.getProjectedQuery().getDerivedColumns()) {
      if (selectSymbol.getAlias() == null) {
        allAliased = false;
        break;
      }
    }
    if (allAliased) {
      String[] columnNames = queryCommand.getColumnNames();
      for (int i = 0; i < columnNames.length; i++) {
        if (i > 0) {
          parts.add(", "); // $NON-NLS-1$
        }
        parts.add(columnNames[i]);
      }
    } else {
      parts.add("*"); // $NON-NLS-1$
    }
    if (limit.getRowOffset() > 0) {
      parts.add(" FROM (SELECT VIEW_FOR_LIMIT.*, ROWNUM ROWNUM_ FROM ("); // $NON-NLS-1$
    } else {
      parts.add(" FROM ("); // $NON-NLS-1$
    }
    parts.add(queryCommand);
    if (limit.getRowOffset() > 0) {
      parts.add(") VIEW_FOR_LIMIT WHERE ROWNUM <= "); // $NON-NLS-1$
      parts.add(limit.getRowLimit() + limit.getRowOffset());
      parts.add(") WHERE ROWNUM_ > "); // $NON-NLS-1$
      parts.add(limit.getRowOffset());
    } else {
      parts.add(") WHERE ROWNUM <= "); // $NON-NLS-1$
      parts.add(limit.getRowLimit());
    }
    return parts;
  }