Search in sources :

Example 1 with MemoryGroupByMeta

use of org.pentaho.di.trans.steps.memgroupby.MemoryGroupByMeta in project pdi-dataservice-server-plugin by pentaho.

the class SqlTransGenerator method generateUniqueStep.

private StepMeta generateUniqueStep(RowMetaInterface rowMeta) {
    SQLFields fields = sql.getSelectFields();
    MemoryGroupByMeta meta = new MemoryGroupByMeta();
    meta.allocate(fields.getFields().size(), 0);
    for (int i = 0; i < fields.getFields().size(); i++) {
        SQLField field = fields.getFields().get(i);
        if (!Utils.isEmpty(field.getAlias()) && rowMeta.searchValueMeta(field.getAlias()) != null) {
            meta.getGroupField()[i] = field.getAlias();
        } else {
            meta.getGroupField()[i] = field.getField();
        }
    }
    StepMeta stepMeta = new StepMeta("DISTINCT", meta);
    stepMeta.setLocation(xLocation, 50);
    xLocation += 100;
    stepMeta.setDraw(true);
    return stepMeta;
}
Also used : MemoryGroupByMeta(org.pentaho.di.trans.steps.memgroupby.MemoryGroupByMeta) SQLFields(org.pentaho.di.core.sql.SQLFields) SQLField(org.pentaho.di.core.sql.SQLField) StepMeta(org.pentaho.di.trans.step.StepMeta)

Example 2 with MemoryGroupByMeta

use of org.pentaho.di.trans.steps.memgroupby.MemoryGroupByMeta in project pdi-dataservice-server-plugin by pentaho.

the class SqlTransGenerator method generateGroupByStep.

private StepMeta generateGroupByStep(List<SQLField> aggFields, List<SQLField> groupFields, RowMetaInterface inputFields) throws KettleException {
    MemoryGroupByMeta meta = new MemoryGroupByMeta();
    meta.allocate(groupFields.size(), aggFields.size());
    // See if we need to always return a row or not (0 rows counted scenario)
    // 
    boolean returnRow = false;
    // 
    for (int i = 0; i < groupFields.size(); i++) {
        SQLField field = groupFields.get(i);
        meta.getGroupField()[i] = field.getField();
    }
    // 
    for (int i = 0; i < aggFields.size(); i++) {
        SQLField field = aggFields.get(i);
        ValueMetaInterface valueMeta = field.getValueMeta();
        meta.getAggregateField()[i] = Const.NVL(field.getAlias(), field.getField());
        String subjectField;
        if (field.getValueData() == null) {
            // 
            if (valueMeta == null) {
                // 
                if (inputFields.size() == 0) {
                    throw new KettleException("No field fields found to aggregate on.");
                }
                subjectField = inputFields.getValueMeta(0).getName();
            } else {
                subjectField = valueMeta.getName();
            }
        } else {
            // A constant field to aggregate.
            // 
            subjectField = "Constant_" + field.getFieldIndex() + "_" + field.getField();
        }
        meta.getSubjectField()[i] = subjectField;
        int agg = 0;
        switch(field.getAggregation()) {
            case SUM:
                agg = MemoryGroupByMeta.TYPE_GROUP_SUM;
                break;
            case MIN:
                agg = MemoryGroupByMeta.TYPE_GROUP_MIN;
                break;
            case MAX:
                agg = MemoryGroupByMeta.TYPE_GROUP_MAX;
                break;
            case COUNT:
                if (field.isCountStar()) {
                    agg = MemoryGroupByMeta.TYPE_GROUP_COUNT_ANY;
                } else if (field.isCountDistinct()) {
                    agg = MemoryGroupByMeta.TYPE_GROUP_COUNT_DISTINCT;
                } else {
                    // Count a particular field
                    agg = MemoryGroupByMeta.TYPE_GROUP_COUNT_ALL;
                }
                returnRow = true;
                break;
            case AVG:
                agg = MemoryGroupByMeta.TYPE_GROUP_AVERAGE;
                break;
            default:
                throw new KettleException("Unhandled aggregation method [" + field.getAggregation() + "]");
        }
        meta.getAggregateType()[i] = agg;
    }
    meta.setAlwaysGivingBackOneRow(returnRow);
    StepMeta stepMeta = new StepMeta("Group by", meta);
    stepMeta.setLocation(xLocation, 50);
    xLocation += 100;
    stepMeta.setDraw(true);
    return stepMeta;
}
Also used : MemoryGroupByMeta(org.pentaho.di.trans.steps.memgroupby.MemoryGroupByMeta) KettleException(org.pentaho.di.core.exception.KettleException) SQLField(org.pentaho.di.core.sql.SQLField) StepMeta(org.pentaho.di.trans.step.StepMeta) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface)

Aggregations

SQLField (org.pentaho.di.core.sql.SQLField)2 StepMeta (org.pentaho.di.trans.step.StepMeta)2 MemoryGroupByMeta (org.pentaho.di.trans.steps.memgroupby.MemoryGroupByMeta)2 KettleException (org.pentaho.di.core.exception.KettleException)1 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)1 SQLFields (org.pentaho.di.core.sql.SQLFields)1