Esempio n. 1
0
 private SortOrder initOrder(Session session) {
   int size = groupConcatOrderList.size();
   int[] index = new int[size];
   int[] sortType = new int[size];
   for (int i = 0; i < size; i++) {
     SelectOrderBy o = groupConcatOrderList.get(i);
     index[i] = i + 1;
     int order = o.descending ? SortOrder.DESCENDING : SortOrder.ASCENDING;
     sortType[i] = order;
   }
   return new SortOrder(session.getDatabase(), index, sortType, null);
 }
Esempio n. 2
0
  @Override
  public void updateAggregate(Session session) {
    // TODO aggregates: check nested MIN(MAX(ID)) and so on
    // if (on != null) {
    // on.updateAggregate();
    // }
    HashMap<Expression, Object> group = select.getCurrentGroup();
    if (group == null) {
      // this is a different level (the enclosing query)
      return;
    }

    int groupRowId = select.getCurrentGroupRowId();
    if (lastGroupRowId == groupRowId) {
      // already visited
      return;
    }
    lastGroupRowId = groupRowId;

    AggregateData data = (AggregateData) group.get(this);
    if (data == null) {
      data = AggregateData.create(type);
      group.put(this, data);
    }
    Value v = on == null ? null : on.getValue(session);
    if (type == GROUP_CONCAT) {
      if (v != ValueNull.INSTANCE) {
        v = v.convertTo(Value.STRING);
        if (groupConcatOrderList != null) {
          int size = groupConcatOrderList.size();
          Value[] array = new Value[1 + size];
          array[0] = v;
          for (int i = 0; i < size; i++) {
            SelectOrderBy o = groupConcatOrderList.get(i);
            array[i + 1] = o.expression.getValue(session);
          }
          v = ValueArray.get(array);
        }
      }
    }
    data.add(session.getDatabase(), dataType, distinct, v);
  }
Esempio n. 3
0
 @Override
 public Value getValue(Session session) {
   if (select.isQuickAggregateQuery()) {
     switch (type) {
       case COUNT:
       case COUNT_ALL:
         Table table = select.getTopTableFilter().getTable();
         return ValueLong.get(table.getRowCount(session));
       case MIN:
       case MAX:
         boolean first = type == MIN;
         Index index = getColumnIndex();
         int sortType = index.getIndexColumns()[0].sortType;
         if ((sortType & SortOrder.DESCENDING) != 0) {
           first = !first;
         }
         Cursor cursor = index.findFirstOrLast(session, first);
         SearchRow row = cursor.getSearchRow();
         Value v;
         if (row == null) {
           v = ValueNull.INSTANCE;
         } else {
           v = row.getValue(index.getColumns()[0].getColumnId());
         }
         return v;
       default:
         DbException.throwInternalError("type=" + type);
     }
   }
   HashMap<Expression, Object> group = select.getCurrentGroup();
   if (group == null) {
     throw DbException.get(ErrorCode.INVALID_USE_OF_AGGREGATE_FUNCTION_1, getSQL());
   }
   AggregateData data = (AggregateData) group.get(this);
   if (data == null) {
     data = AggregateData.create(type);
   }
   Value v = data.getValue(session.getDatabase(), dataType, distinct);
   if (type == GROUP_CONCAT) {
     ArrayList<Value> list = ((AggregateDataGroupConcat) data).getList();
     if (list == null || list.size() == 0) {
       return ValueNull.INSTANCE;
     }
     if (groupConcatOrderList != null) {
       final SortOrder sortOrder = groupConcatSort;
       Collections.sort(
           list,
           new Comparator<Value>() {
             @Override
             public int compare(Value v1, Value v2) {
               Value[] a1 = ((ValueArray) v1).getList();
               Value[] a2 = ((ValueArray) v2).getList();
               return sortOrder.compare(a1, a2);
             }
           });
     }
     StatementBuilder buff = new StatementBuilder();
     String sep =
         groupConcatSeparator == null ? "," : groupConcatSeparator.getValue(session).getString();
     for (Value val : list) {
       String s;
       if (val.getType() == Value.ARRAY) {
         s = ((ValueArray) val).getList()[0].getString();
       } else {
         s = val.getString();
       }
       if (s == null) {
         continue;
       }
       if (sep != null) {
         buff.appendExceptFirst(sep);
       }
       buff.append(s);
     }
     v = ValueString.get(buff.toString());
   }
   return v;
 }