Search in sources :

Example 41 with ValueMetaNumber

use of org.pentaho.di.core.row.value.ValueMetaNumber in project pentaho-kettle by pentaho.

the class Formula2IT method testIsText_EmptyStringIsNotNull.

@Test
public void testIsText_EmptyStringIsNotNull() throws KettleException {
    FieldAccessorUtl.ensureEmptyStringIsNotNull(true);
    final String fIsText = "ISTEXT([" + KEY_ARG + "])";
    assertBooleanFormula(fIsText, buildRowMeta(new ValueMetaString(KEY_ARG)), new Object[] { "1" }, true);
    assertBooleanFormula(fIsText, buildRowMeta(new ValueMetaString(KEY_ARG)), new Object[] { "a" }, true);
    assertBooleanFormula(fIsText, buildRowMeta(new ValueMetaString(KEY_ARG)), new Object[] { null }, false);
    assertBooleanFormula(fIsText, buildRowMeta(new ValueMetaString(KEY_ARG)), new Object[] { "" }, true);
    assertBooleanFormula(fIsText, buildRowMeta(new ValueMetaString(KEY_ARG)), new Object[] { "   " }, true);
    assertBooleanFormula(fIsText, buildRowMeta(new ValueMetaNumber(KEY_ARG)), new Object[] { 1 }, false);
    assertBooleanFormula(fIsText, buildRowMeta(new ValueMetaNumber(KEY_ARG)), new Object[] { 0 }, false);
    assertBooleanFormula(fIsText, buildRowMeta(new ValueMetaNumber(KEY_ARG)), new Object[] { -1.0 }, false);
    assertBooleanFormula(fIsText, buildRowMeta(new ValueMetaNumber(KEY_ARG)), new Object[] { null }, false);
}
Also used : ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) ValueMetaNumber(org.pentaho.di.core.row.value.ValueMetaNumber) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) Test(org.junit.Test)

Example 42 with ValueMetaNumber

use of org.pentaho.di.core.row.value.ValueMetaNumber in project pentaho-kettle by pentaho.

the class UnivariateStatsMeta method getValueMetas.

/**
 * Returns an array of ValueMetaInterface that contains the meta data for each value computed by the supplied
 * UnivariateStatsMetaFunction
 *
 * @param fn
 *          the <code>UnivariateStatsMetaFunction</code> to construct meta data for
 * @param origin
 *          the origin
 * @return an array of meta data
 */
private ValueMetaInterface[] getValueMetas(UnivariateStatsMetaFunction fn, String origin) {
    ValueMetaInterface[] v = new ValueMetaInterface[fn.numberOfMetricsRequested()];
    int index = 0;
    if (fn.getCalcN()) {
        v[index] = new ValueMetaNumber(fn.getSourceFieldName() + "(N)");
        v[index].setOrigin(origin);
        index++;
    }
    if (fn.getCalcMean()) {
        v[index] = new ValueMetaNumber(fn.getSourceFieldName() + "(mean)");
        v[index].setOrigin(origin);
        index++;
    }
    if (fn.getCalcStdDev()) {
        v[index] = new ValueMetaNumber(fn.getSourceFieldName() + "(stdDev)");
        v[index].setOrigin(origin);
        index++;
    }
    if (fn.getCalcMin()) {
        v[index] = new ValueMetaNumber(fn.getSourceFieldName() + "(min)");
        v[index].setOrigin(origin);
        index++;
    }
    if (fn.getCalcMax()) {
        v[index] = new ValueMetaNumber(fn.getSourceFieldName() + "(max)");
        v[index].setOrigin(origin);
        index++;
    }
    if (fn.getCalcMedian()) {
        v[index] = new ValueMetaNumber(fn.getSourceFieldName() + "(median)");
        v[index].setOrigin(origin);
        index++;
    }
    if (fn.getCalcPercentile() >= 0) {
        double percent = fn.getCalcPercentile();
        // NumberFormat pF = NumberFormat.getPercentInstance();
        NumberFormat pF = NumberFormat.getInstance();
        pF.setMaximumFractionDigits(2);
        String res = pF.format(percent * 100);
        v[index] = new ValueMetaNumber(fn.getSourceFieldName() + "(" + res + "th percentile)");
        v[index].setOrigin(origin);
        index++;
    }
    return v;
}
Also used : ValueMetaNumber(org.pentaho.di.core.row.value.ValueMetaNumber) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) NumberFormat(java.text.NumberFormat)

Example 43 with ValueMetaNumber

use of org.pentaho.di.core.row.value.ValueMetaNumber in project pdi-dataservice-server-plugin by pentaho.

the class SqlTransGeneratorTest method testTypeHandlingWithAvg.

@Test
public void testTypeHandlingWithAvg() throws KettleException {
    SQL sql = new SQL("SELECT avg( intVal ), avg( numericVal ), avg( bigNumber)  FROM table");
    RowMetaInterface rowMeta = new RowMeta();
    rowMeta.addValueMeta(new ValueMetaInteger("intVal"));
    rowMeta.addValueMeta(new ValueMetaNumber("numericVal"));
    rowMeta.addValueMeta(new ValueMetaBigNumber("bigNumber"));
    sql.parse(rowMeta);
    SqlTransGenerator generator = new SqlTransGenerator(sql, 0);
    SelectValuesMeta meta = (SelectValuesMeta) getStepByName(generator.generateTransMeta(), "Set Conversion");
    assertThat(meta.getMeta().length, is(3));
    // Integer should be converted to TYPE_NUMBER
    assertThat(meta.getMeta()[0].getType(), is(TYPE_NUMBER));
    // Other types should be left alone ( SelectMetadataChange.getType remains TYPE_NONE )
    assertThat(meta.getMeta()[1].getType(), is(TYPE_NONE));
    assertThat(meta.getMeta()[2].getType(), is(TYPE_NONE));
}
Also used : SelectValuesMeta(org.pentaho.di.trans.steps.selectvalues.SelectValuesMeta) RowMeta(org.pentaho.di.core.row.RowMeta) ValueMetaNumber(org.pentaho.di.core.row.value.ValueMetaNumber) ValueMetaBigNumber(org.pentaho.di.core.row.value.ValueMetaBigNumber) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) ValueMetaInteger(org.pentaho.di.core.row.value.ValueMetaInteger) SQL(org.pentaho.di.core.sql.SQL) Test(org.junit.Test)

Example 44 with ValueMetaNumber

use of org.pentaho.di.core.row.value.ValueMetaNumber in project pentaho-cassandra-plugin by pentaho.

the class DriverCQLRowHandlerTest method testExpandCollection.

@Test
public void testExpandCollection() throws Exception {
    List<Object[]> rowList = new ArrayList<Object[]>();
    ArrayList<Long> numList = new ArrayList<Long>();
    numList.add(1L);
    numList.add(2L);
    numList.add(3L);
    rowList.add(new Object[] { 1L, numList });
    rowList.add(new Object[] { 2L, new ArrayList<Long>() });
    Iterator<Object[]> it = rowList.iterator();
    DriverKeyspace keyspace = mock(DriverKeyspace.class);
    Session session = mock(Session.class);
    ResultSet rs = mock(ResultSet.class);
    when(session.execute(anyString())).thenReturn(rs);
    when(rs.isExhausted()).then(invoc -> {
        return !it.hasNext();
    });
    when(rs.one()).then(invocation -> {
        Object[] rowArr = it.next();
        Row row = mock(Row.class);
        when(row.getObject(anyInt())).then(invoc -> {
            return rowArr[(int) invoc.getArguments()[0]];
        });
        when(row.getLong(0)).thenReturn((long) rowArr[0]);
        return row;
    });
    mockColumnDefinitions(rs, DataType.bigint(), DataType.list(DataType.bigint()));
    DriverCQLRowHandler rowHandler = new DriverCQLRowHandler(keyspace, session, true);
    RowMetaInterface rowMeta = new RowMeta();
    rowMeta.addValueMeta(new ValueMetaInteger("id"));
    rowMeta.addValueMeta(new ValueMetaNumber("nums"));
    rowHandler.newRowQuery(mock(StepInterface.class), "tab", "select * from tab", null, null, mock(LogChannelInterface.class));
    List<Object[]> resultRows = getNextOutputRows(rowHandler, rowMeta);
    assertEquals(4, resultRows.size());
    assertEquals(1L, resultRows.get(0)[1]);
    assertEquals(2L, resultRows.get(3)[0]);
    assertNull(resultRows.get(3)[1]);
}
Also used : RowMeta(org.pentaho.di.core.row.RowMeta) ArrayList(java.util.ArrayList) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) StepInterface(org.pentaho.di.trans.step.StepInterface) ValueMetaNumber(org.pentaho.di.core.row.value.ValueMetaNumber) ResultSet(com.datastax.driver.core.ResultSet) ValueMetaInteger(org.pentaho.di.core.row.value.ValueMetaInteger) Row(com.datastax.driver.core.Row) LogChannelInterface(org.pentaho.di.core.logging.LogChannelInterface) Session(com.datastax.driver.core.Session) Test(org.junit.Test)

Example 45 with ValueMetaNumber

use of org.pentaho.di.core.row.value.ValueMetaNumber in project pentaho-kettle by pentaho.

the class XBase method getFields.

@SuppressWarnings("fallthrough")
public RowMetaInterface getFields() throws KettleException {
    String debug = "get fields from XBase file";
    RowMetaInterface row = new RowMeta();
    try {
        // Fetch all field information
        // 
        debug = "allocate data types";
        datatype = new byte[reader.getFieldCount()];
        for (int i = 0; i < reader.getFieldCount(); i++) {
            if (log.isDebug()) {
                debug = "get field #" + i;
            }
            DBFField field = reader.getField(i);
            ValueMetaInterface value = null;
            datatype[i] = field.getDataType();
            switch(datatype[i]) {
                case // Memo
                DBFField.FIELD_TYPE_M:
                    debug = "memo field";
                    if ((log != null) && log.isDebug()) {
                        log.logDebug("Field #" + i + " is a memo-field! (" + field.getName() + ")");
                    }
                case // Character
                DBFField.FIELD_TYPE_C:
                    // case DBFField.FIELD_TYPE_P: // Picture
                    debug = "character field";
                    value = new ValueMetaString(field.getName());
                    value.setLength(field.getFieldLength());
                    break;
                // Integer
                case FIELD_TYPE_I:
                // Numeric
                case DBFField.FIELD_TYPE_N:
                case // Float
                DBFField.FIELD_TYPE_F:
                    debug = "Number field";
                    value = new ValueMetaNumber(field.getName());
                    value.setLength(field.getFieldLength(), field.getDecimalCount());
                    break;
                case // Logical
                DBFField.FIELD_TYPE_L:
                    debug = "Logical field";
                    value = new ValueMetaBoolean(field.getName());
                    value.setLength(-1, -1);
                    break;
                case // Date
                DBFField.FIELD_TYPE_D:
                    debug = "Date field";
                    value = new ValueMetaDate(field.getName());
                    value.setLength(-1, -1);
                    break;
                default:
                    if ((log != null) && (log.isDebug())) {
                        log.logDebug("Unknown Datatype" + datatype[i]);
                    }
            }
            if (value != null) {
                row.addValueMeta(value);
            }
        }
    } catch (Exception e) {
        throw new KettleException("Error reading DBF metadata (in part " + debug + ")", e);
    }
    return row;
}
Also used : ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) KettleException(org.pentaho.di.core.exception.KettleException) RowMeta(org.pentaho.di.core.row.RowMeta) ValueMetaNumber(org.pentaho.di.core.row.value.ValueMetaNumber) DBFField(com.linuxense.javadbf.DBFField) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) ValueMetaBoolean(org.pentaho.di.core.row.value.ValueMetaBoolean) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) ValueMetaDate(org.pentaho.di.core.row.value.ValueMetaDate) DBFException(com.linuxense.javadbf.DBFException) KettleException(org.pentaho.di.core.exception.KettleException) IOException(java.io.IOException) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface)

Aggregations

ValueMetaNumber (org.pentaho.di.core.row.value.ValueMetaNumber)95 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)75 ValueMetaInteger (org.pentaho.di.core.row.value.ValueMetaInteger)52 Test (org.junit.Test)51 RowMeta (org.pentaho.di.core.row.RowMeta)39 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)34 ValueMetaDate (org.pentaho.di.core.row.value.ValueMetaDate)34 ValueMetaBoolean (org.pentaho.di.core.row.value.ValueMetaBoolean)32 ValueMetaBigNumber (org.pentaho.di.core.row.value.ValueMetaBigNumber)30 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)25 ValueMetaBinary (org.pentaho.di.core.row.value.ValueMetaBinary)20 ValueMetaTimestamp (org.pentaho.di.core.row.value.ValueMetaTimestamp)16 ValueMetaInternetAddress (org.pentaho.di.core.row.value.ValueMetaInternetAddress)15 KettleException (org.pentaho.di.core.exception.KettleException)12 RowMetaAndData (org.pentaho.di.core.RowMetaAndData)11 KettleStepException (org.pentaho.di.core.exception.KettleStepException)7 ArrayList (java.util.ArrayList)6 Date (java.util.Date)6 RowSet (org.pentaho.di.core.RowSet)6 RowAdapter (org.pentaho.di.trans.step.RowAdapter)6