Search in sources :

Example 1 with CalculatorMeta

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

the class SqlTransGenerator method generateIifStep.

/**
 * This method generates a 4 steps for every IIF clause... TODO: replace with one step...
 *
 * @param iifField
 * @param lastStep
 * @param transMeta
 * @return steps
 */
private StepMeta generateIifStep(SQLField iifField, TransMeta transMeta, StepMeta lastStep) {
    IifFunction iif = iifField.getIif();
    // The Filter condition...
    // 
    FilterRowsMeta filterMeta = new FilterRowsMeta();
    filterMeta.setCondition(iifField.getIif().getSqlCondition().getCondition());
    StepMeta filterStep = new StepMeta(iifField.getExpression(), filterMeta);
    filterStep.setLocation(xLocation, 50);
    xLocation += 100;
    filterStep.setDraw(true);
    lastStep = addToTrans(filterStep, transMeta, lastStep);
    // The True and false steps...
    // 
    StepMetaInterface trueMetaInterface;
    ValueMetaInterface valueMeta = iif.getTrueValue().getValueMeta();
    if (iif.isTrueField()) {
        CalculatorMeta trueMeta = new CalculatorMeta();
        trueMetaInterface = trueMeta;
        trueMeta.allocate(1);
        CalculatorMetaFunction function = new CalculatorMetaFunction();
        function.setFieldName(Const.NVL(iifField.getAlias(), iifField.getField()));
        function.setCalcType(CalculatorMetaFunction.CALC_COPY_OF_FIELD);
        function.setValueType(valueMeta.getType());
        function.setValueLength(valueMeta.getLength());
        function.setValuePrecision(valueMeta.getPrecision());
        function.setFieldA(iif.getTrueValueString());
        function.setConversionMask(valueMeta.getConversionMask());
        // CHECKSTYLE:Indentation:OFF
        trueMeta.getCalculation()[0] = function;
    } else {
        ConstantMeta trueMeta = new ConstantMeta();
        trueMetaInterface = trueMeta;
        trueMeta.allocate(1);
        // CHECKSTYLE:Indentation:OFF
        trueMeta.getFieldName()[0] = Const.NVL(iifField.getAlias(), iifField.getField());
        trueMeta.getFieldType()[0] = iif.getTrueValue().getValueMeta().getTypeDesc();
        trueMeta.getValue()[0] = iif.getTrueValue().toString();
        trueMeta.getFieldFormat()[0] = valueMeta.getConversionMask();
    }
    StepMeta trueStep = new StepMeta("TRUE: " + iifField.getExpression(), trueMetaInterface);
    trueStep.setLocation(xLocation, 50);
    trueStep.setDraw(true);
    lastStep = addToTrans(trueStep, transMeta, filterStep);
    StepMetaInterface falseMetaInterface;
    valueMeta = iif.getFalseValue().getValueMeta();
    if (iif.isFalseField()) {
        CalculatorMeta falseMeta = new CalculatorMeta();
        falseMetaInterface = falseMeta;
        falseMeta.allocate(1);
        CalculatorMetaFunction function = new CalculatorMetaFunction();
        function.setFieldName(Const.NVL(iifField.getAlias(), iifField.getField()));
        function.setCalcType(CalculatorMetaFunction.CALC_COPY_OF_FIELD);
        function.setValueType(valueMeta.getType());
        function.setValueLength(valueMeta.getLength());
        function.setValuePrecision(valueMeta.getPrecision());
        function.setFieldA(iif.getFalseValueString());
        function.setConversionMask(valueMeta.getConversionMask());
        falseMeta.getCalculation()[0] = function;
    } else {
        ConstantMeta falseMeta = new ConstantMeta();
        falseMetaInterface = falseMeta;
        falseMeta.allocate(1);
        falseMeta.getFieldName()[0] = Const.NVL(iifField.getAlias(), iifField.getField());
        falseMeta.getFieldType()[0] = iif.getFalseValue().getValueMeta().getTypeDesc();
        falseMeta.getFieldFormat()[0] = valueMeta.getConversionMask();
        falseMeta.getValue()[0] = iif.getFalseValue().toString();
    }
    StepMeta falseStep = new StepMeta("FALSE: " + iifField.getExpression(), falseMetaInterface);
    falseStep.setLocation(xLocation, 150);
    xLocation += 100;
    falseStep.setDraw(true);
    lastStep = addToTrans(falseStep, transMeta, filterStep);
    // specify true/false targets
    List<StreamInterface> targetStreams = filterMeta.getStepIOMeta().getTargetStreams();
    targetStreams.get(0).setSubject(trueStep.getName());
    targetStreams.get(1).setSubject(falseStep.getName());
    filterMeta.searchInfoAndTargetSteps(transMeta.getSteps());
    DummyTransMeta dummyMeta = new DummyTransMeta();
    StepMeta dummyStep = new StepMeta("Collect: " + iifField.getExpression(), dummyMeta);
    dummyStep.setLocation(xLocation, 50);
    xLocation += 100;
    dummyStep.setDraw(true);
    lastStep = addToTrans(dummyStep, transMeta, trueStep);
    transMeta.addTransHop(new TransHopMeta(falseStep, dummyStep));
    return lastStep;
}
Also used : FilterRowsMeta(org.pentaho.di.trans.steps.filterrows.FilterRowsMeta) StepMetaInterface(org.pentaho.di.trans.step.StepMetaInterface) IifFunction(org.pentaho.di.core.sql.IifFunction) CalculatorMeta(org.pentaho.di.trans.steps.calculator.CalculatorMeta) TransHopMeta(org.pentaho.di.trans.TransHopMeta) ConstantMeta(org.pentaho.di.trans.steps.constant.ConstantMeta) StepMeta(org.pentaho.di.trans.step.StepMeta) CalculatorMetaFunction(org.pentaho.di.trans.steps.calculator.CalculatorMetaFunction) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) StreamInterface(org.pentaho.di.trans.step.errorhandling.StreamInterface) DummyTransMeta(org.pentaho.di.trans.steps.dummytrans.DummyTransMeta)

Example 2 with CalculatorMeta

use of org.pentaho.di.trans.steps.calculator.CalculatorMeta in project data-access by pentaho.

the class StagingTransformGenerator method createCalcStep.

protected StepMeta createCalcStep(TransMeta transMeta, String stepName, ColumnInfo[] columns) {
    CalculatorMeta meta = new CalculatorMeta();
    List<CalculatorMetaFunction> funcs = new ArrayList<CalculatorMetaFunction>();
    for (ColumnInfo column : columns) {
        if (column != null && !column.isIgnore() && column.getDataType() == DataType.DATE) {
            // see if we need to break out the date fields
            int dateBreakOut = column.getDateFieldBreakout();
            if ((dateBreakOut & ColumnInfo.DATE_LEVEL_YEAR) > 0) {
                CalculatorMetaFunction func = createDateCalc(CalculatorMetaFunction.CALC_YEAR_OF_DATE, column.getTitle() + " (year)", column.getId(), 4);
                funcs.add(func);
            }
            if ((dateBreakOut & ColumnInfo.DATE_LEVEL_QUARTER) > 0) {
                CalculatorMetaFunction func = createDateCalc(CalculatorMetaFunction.CALC_QUARTER_OF_DATE, column.getTitle() + " (qtr)", column.getId(), 4);
                funcs.add(func);
            }
            if ((dateBreakOut & ColumnInfo.DATE_LEVEL_MONTH) > 0) {
                CalculatorMetaFunction func = createDateCalc(CalculatorMetaFunction.CALC_MONTH_OF_DATE, column.getTitle() + " (month)", column.getId(), 4);
                funcs.add(func);
            }
            if ((dateBreakOut & ColumnInfo.DATE_LEVEL_WEEK) > 0) {
                CalculatorMetaFunction func = createDateCalc(CalculatorMetaFunction.CALC_WEEK_OF_YEAR, column.getTitle() + " (week)", column.getId(), 4);
                funcs.add(func);
            }
            if ((dateBreakOut & ColumnInfo.DATE_LEVEL_DAY) > 0) {
                CalculatorMetaFunction func = createDateCalc(CalculatorMetaFunction.CALC_DAY_OF_MONTH, column.getTitle() + " (day)", column.getId(), 4);
                funcs.add(func);
            }
            if ((dateBreakOut & ColumnInfo.DATE_LEVEL_DAYOFWEEK) > 0) {
                CalculatorMetaFunction func = createDateCalc(CalculatorMetaFunction.CALC_DAY_OF_WEEK, column.getTitle() + " (day of week)", column.getId(), 4);
                funcs.add(func);
            }
        }
    }
    if (funcs.size() == 0) {
        return null;
    }
    meta.setCalculation(funcs.toArray(new CalculatorMetaFunction[funcs.size()]));
    StepMeta stepMeta = new StepMeta(stepName, stepName, meta);
    transMeta.addStep(stepMeta);
    return stepMeta;
}
Also used : ArrayList(java.util.ArrayList) ColumnInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.ColumnInfo) CalculatorMeta(org.pentaho.di.trans.steps.calculator.CalculatorMeta) CalculatorMetaFunction(org.pentaho.di.trans.steps.calculator.CalculatorMetaFunction) StepMeta(org.pentaho.di.trans.step.StepMeta)

Example 3 with CalculatorMeta

use of org.pentaho.di.trans.steps.calculator.CalculatorMeta in project pentaho-metaverse by pentaho.

the class MetaverseValidationIT method testCalculatorStepNode.

@Test
public void testCalculatorStepNode() throws Exception {
    CalculatorStepNode node = root.getCalculatorStepNode();
    Set<String> usedFields = new HashSet<String>();
    CalculatorMeta calculatorMeta = (CalculatorMeta) getStepMeta(node);
    for (CalculatorMetaFunction calculatorMetaFunction : calculatorMeta.getCalculation()) {
        String fieldName = calculatorMetaFunction.getFieldA();
        if (!StringUtils.isEmpty(fieldName)) {
            usedFields.add(fieldName);
        }
        fieldName = calculatorMetaFunction.getFieldB();
        if (!StringUtils.isEmpty(fieldName)) {
            usedFields.add(fieldName);
        }
        fieldName = calculatorMetaFunction.getFieldC();
        if (!StringUtils.isEmpty(fieldName)) {
            usedFields.add(fieldName);
        }
    }
    int expectedUsedFieldCount = usedFields.size();
    // Make sure we have the right number of links used, created and deleted.
    List<String> nodeUses = new ArrayList<String>();
    for (StreamFieldNode sfn : node.getStreamFieldNodesUses()) {
        nodeUses.add(sfn.getName());
    }
    StreamFieldNode area = null;
    StreamFieldNode kelvin = null;
    StreamFieldNode celsius = null;
    List<String> nodeOutputs = new ArrayList<String>();
    for (StreamFieldNode sfn : node.getOutputStreamFields()) {
        nodeOutputs.add(sfn.getName());
        Operations ops = MetaverseUtil.convertOperationsStringToMap(sfn.getOperations());
        if (sfn.getName().equals("area")) {
            area = sfn;
            assertNotNull(ops.get(ChangeType.DATA));
            assertEquals(Operation.CALC_CATEGORY, ops.get(ChangeType.DATA).get(0).getCategory());
        } else if (sfn.getName().equals("celsius")) {
            celsius = sfn;
            assertNotNull(ops);
            assertNotNull(ops.get(ChangeType.DATA));
            assertEquals(Operation.CALC_CATEGORY, ops.get(ChangeType.DATA).get(0).getCategory());
        } else if (sfn.getName().equals("kelvin")) {
            kelvin = sfn;
            assertNotNull(ops);
            assertNotNull(ops.get(ChangeType.DATA));
            assertEquals(Operation.CALC_CATEGORY, ops.get(ChangeType.DATA).get(0).getCategory());
        }
    }
    assertEquals(2, getIterableSize(area.getFieldNodesThatDeriveMe()));
    String[] fieldsThatDerive = new String[2];
    fieldsThatDerive = new String[2];
    int i = 0;
    for (StreamFieldNode sfn : celsius.getFieldNodesThatDeriveMe()) {
        fieldsThatDerive[i++] = sfn.getName();
    }
    assertTrue(Arrays.asList(fieldsThatDerive).contains("tempCelsius"));
    assertTrue(Arrays.asList(fieldsThatDerive).contains("tempRatio"));
    fieldsThatDerive = new String[2];
    i = 0;
    for (StreamFieldNode sfn : kelvin.getFieldNodesThatDeriveMe()) {
        fieldsThatDerive[i++] = sfn.getName();
    }
    assertTrue(Arrays.asList(fieldsThatDerive).contains("tempKelvin"));
    assertTrue(Arrays.asList(fieldsThatDerive).contains("tempRatio"));
    assertEquals(expectedUsedFieldCount, nodeUses.size());
    assertEquals(getExpectedOutputFieldCount(calculatorMeta), nodeOutputs.size());
}
Also used : CalculatorStepNode(org.pentaho.metaverse.frames.CalculatorStepNode) ArrayList(java.util.ArrayList) CalculatorMeta(org.pentaho.di.trans.steps.calculator.CalculatorMeta) StreamFieldNode(org.pentaho.metaverse.frames.StreamFieldNode) CalculatorMetaFunction(org.pentaho.di.trans.steps.calculator.CalculatorMetaFunction) Operations(org.pentaho.metaverse.api.model.Operations) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

CalculatorMeta (org.pentaho.di.trans.steps.calculator.CalculatorMeta)3 CalculatorMetaFunction (org.pentaho.di.trans.steps.calculator.CalculatorMetaFunction)3 ArrayList (java.util.ArrayList)2 StepMeta (org.pentaho.di.trans.step.StepMeta)2 HashSet (java.util.HashSet)1 Test (org.junit.Test)1 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)1 IifFunction (org.pentaho.di.core.sql.IifFunction)1 TransHopMeta (org.pentaho.di.trans.TransHopMeta)1 StepMetaInterface (org.pentaho.di.trans.step.StepMetaInterface)1 StreamInterface (org.pentaho.di.trans.step.errorhandling.StreamInterface)1 ConstantMeta (org.pentaho.di.trans.steps.constant.ConstantMeta)1 DummyTransMeta (org.pentaho.di.trans.steps.dummytrans.DummyTransMeta)1 FilterRowsMeta (org.pentaho.di.trans.steps.filterrows.FilterRowsMeta)1 Operations (org.pentaho.metaverse.api.model.Operations)1 CalculatorStepNode (org.pentaho.metaverse.frames.CalculatorStepNode)1 StreamFieldNode (org.pentaho.metaverse.frames.StreamFieldNode)1 ColumnInfo (org.pentaho.platform.dataaccess.datasource.wizard.models.ColumnInfo)1