Search in sources :

Example 6 with LongValue

use of org.knime.core.data.LongValue in project knime-core by knime.

the class LinearInterpolationStatisticTB method consumeRow.

/**
 * {@inheritDoc}
 */
@Override
protected void consumeRow(final DataRow dataRow) {
    DataCell cell = dataRow.getCell(getColumnIndex());
    if (cell.isMissing()) {
        incMissing();
    } else {
        for (int i = 0; i < getNumMissing(); i++) {
            DataCell res;
            if (getPrevious().isMissing()) {
                res = cell;
            } else {
                if (m_isDateColumn) {
                    DateAndTimeValue val = (DateAndTimeValue) cell;
                    DateAndTimeValue prevVal = (DateAndTimeValue) getPrevious();
                    boolean hasDate = val.hasDate() | prevVal.hasDate();
                    boolean hasTime = val.hasTime() | prevVal.hasTime();
                    boolean hasMilis = val.hasMillis() | prevVal.hasMillis();
                    long prev = prevVal.getUTCTimeInMillis();
                    long next = val.getUTCTimeInMillis();
                    long lin = Math.round(prev + 1.0 * (i + 1) / (1.0 * (getNumMissing() + 1)) * (next - prev));
                    res = new DateAndTimeCell(lin, hasDate, hasTime, hasMilis);
                } else {
                    DoubleValue val = (DoubleValue) cell;
                    double prev = ((DoubleValue) getPrevious()).getDoubleValue();
                    double next = val.getDoubleValue();
                    double lin = prev + 1.0 * (i + 1) / (1.0 * (getNumMissing() + 1)) * (next - prev);
                    if (getPrevious() instanceof IntValue) {
                        // get an int, create an int
                        res = new IntCell((int) Math.round(lin));
                    } else if (getPrevious() instanceof LongValue) {
                        // get an long, create an long
                        res = new LongCell(Math.round(lin));
                    } else {
                        res = new DoubleCell(lin);
                    }
                }
            }
            addMapping(res);
        }
        resetMissing(cell);
    }
}
Also used : DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) DoubleCell(org.knime.core.data.def.DoubleCell) IntCell(org.knime.core.data.def.IntCell) LongCell(org.knime.core.data.def.LongCell) DoubleValue(org.knime.core.data.DoubleValue) LongValue(org.knime.core.data.LongValue) DataCell(org.knime.core.data.DataCell) DateAndTimeCell(org.knime.core.data.date.DateAndTimeCell) IntValue(org.knime.core.data.IntValue)

Example 7 with LongValue

use of org.knime.core.data.LongValue in project knime-core by knime.

the class AverageInterpolationStatisticTB method consumeRow.

/**
 * {@inheritDoc}
 */
@Override
protected void consumeRow(final DataRow dataRow) {
    DataCell cell = dataRow.getCell(getColumnIndex());
    if (cell.isMissing()) {
        incMissing();
    } else {
        for (int i = 0; i < getNumMissing(); i++) {
            DataCell res;
            if (getPrevious().isMissing()) {
                res = cell;
            } else {
                if (m_isDateColumn) {
                    DateAndTimeValue val = (DateAndTimeValue) cell;
                    DateAndTimeValue prevVal = (DateAndTimeValue) getPrevious();
                    boolean hasDate = val.hasDate() | prevVal.hasDate();
                    boolean hasTime = val.hasTime() | prevVal.hasTime();
                    boolean hasMilis = val.hasMillis() | prevVal.hasMillis();
                    long prev = prevVal.getUTCTimeInMillis();
                    long next = val.getUTCTimeInMillis();
                    long lin = Math.round((prev + next) / 2);
                    res = new DateAndTimeCell(lin, hasDate, hasTime, hasMilis);
                } else {
                    DoubleValue val = (DoubleValue) cell;
                    double prev = ((DoubleValue) getPrevious()).getDoubleValue();
                    double next = val.getDoubleValue();
                    double lin = (prev + next) / 2;
                    if (getPrevious() instanceof IntValue) {
                        // get an int, create an int
                        res = new IntCell((int) Math.round(lin));
                    } else if (getPrevious() instanceof LongValue) {
                        // get an long, create an long
                        res = new LongCell(Math.round(lin));
                    } else {
                        res = new DoubleCell(lin);
                    }
                }
            }
            addMapping(res);
        }
        resetMissing(cell);
    }
}
Also used : DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) DoubleCell(org.knime.core.data.def.DoubleCell) IntCell(org.knime.core.data.def.IntCell) LongCell(org.knime.core.data.def.LongCell) DoubleValue(org.knime.core.data.DoubleValue) LongValue(org.knime.core.data.LongValue) DataCell(org.knime.core.data.DataCell) DateAndTimeCell(org.knime.core.data.date.DateAndTimeCell) IntValue(org.knime.core.data.IntValue)

Example 8 with LongValue

use of org.knime.core.data.LongValue in project knime-core by knime.

the class LinearInterpolationStatisticMB method consumeRow.

/**
 * {@inheritDoc}
 */
@Override
protected void consumeRow(final DataRow dataRow) {
    DataCell cell = dataRow.getCell(m_colIdx);
    if (cell.isMissing()) {
        m_numMissing++;
    } else {
        for (int i = 0; i < m_numMissing; i++) {
            DataCell res;
            if (m_previous.isMissing()) {
                res = cell;
            } else {
                if (m_isDateColumn) {
                    DateAndTimeValue val = (DateAndTimeValue) cell;
                    DateAndTimeValue prevVal = (DateAndTimeValue) m_previous;
                    boolean hasDate = val.hasDate() | prevVal.hasDate();
                    boolean hasTime = val.hasTime() | prevVal.hasTime();
                    boolean hasMilis = val.hasMillis() | prevVal.hasMillis();
                    long prev = prevVal.getUTCTimeInMillis();
                    long next = val.getUTCTimeInMillis();
                    long lin = Math.round(prev + 1.0 * (i + 1) / (1.0 * (m_numMissing + 1)) * (next - prev));
                    res = new DateAndTimeCell(lin, hasDate, hasTime, hasMilis);
                } else {
                    DoubleValue val = (DoubleValue) cell;
                    double prev = ((DoubleValue) m_previous).getDoubleValue();
                    double next = val.getDoubleValue();
                    double lin = prev + 1.0 * (i + 1) / (1.0 * (m_numMissing + 1)) * (next - prev);
                    if (m_previous instanceof IntValue) {
                        // get an int, create an int
                        res = new IntCell((int) Math.round(lin));
                    } else if (m_previous instanceof LongValue) {
                        // get an long, create an long
                        res = new LongCell(Math.round(lin));
                    } else {
                        res = new DoubleCell(lin);
                    }
                }
            }
            m_values.add(res);
        }
        m_numMissing = 0;
        m_previous = cell;
    }
}
Also used : DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) DoubleCell(org.knime.core.data.def.DoubleCell) IntCell(org.knime.core.data.def.IntCell) LongCell(org.knime.core.data.def.LongCell) DoubleValue(org.knime.core.data.DoubleValue) LongValue(org.knime.core.data.LongValue) DataCell(org.knime.core.data.DataCell) DateAndTimeCell(org.knime.core.data.date.DateAndTimeCell) IntValue(org.knime.core.data.IntValue)

Example 9 with LongValue

use of org.knime.core.data.LongValue in project knime-core by knime.

the class BinningUtil method calculateIntegerMaxNoOfBins.

/**
 * Calculates the maximum number of bins for the given column spec if it
 * is an integer column or returns the given number of bins.
 * @param noOfBins the current number of bins
 * @param xColSpec to calculate the range for
 * @return the maximum number of bins
 */
public static int calculateIntegerMaxNoOfBins(final int noOfBins, final DataColumnSpec xColSpec) {
    int result = noOfBins;
    if (xColSpec != null && xColSpec.getType().isCompatible(LongValue.class)) {
        final DataColumnDomain domain = xColSpec.getDomain();
        if (domain != null) {
            final LongValue lowerBound = (LongValue) domain.getLowerBound();
            final LongValue upperBound = (LongValue) domain.getUpperBound();
            double range = (double) upperBound.getLongValue() - (double) lowerBound.getLongValue() + 1.0;
            if (range > Integer.MAX_VALUE) {
                range = Integer.MAX_VALUE;
            }
            if (range <= 0) {
                // we need at least one bin
                range = 1;
            }
            if (result > range) {
                result = (int) range;
            }
        }
    }
    return result;
}
Also used : DataColumnDomain(org.knime.core.data.DataColumnDomain) LongValue(org.knime.core.data.LongValue)

Example 10 with LongValue

use of org.knime.core.data.LongValue in project knime-core by knime.

the class DatabaseHelper method fillStatement.

/**
 * Set given column value into SQL statement.
 * @param stmt statement used
 * @param dbIdx database index to update/write
 * @param cspec column spec to check type
 * @param cell the data cell to write into the statement
 * @param tz the {@link TimeZone} to use
 * @param columnTypes
 * @throws SQLException if the value can't be set
 */
protected void fillStatement(final PreparedStatement stmt, final int dbIdx, final DataColumnSpec cspec, final DataCell cell, final TimeZone tz, final Map<Integer, Integer> columnTypes) throws SQLException {
    if (cspec.getType().isCompatible(BooleanValue.class)) {
        if (cell.isMissing()) {
            stmt.setNull(dbIdx, Types.BOOLEAN);
        } else {
            boolean bool = ((BooleanValue) cell).getBooleanValue();
            stmt.setBoolean(dbIdx, bool);
        }
    } else if (cspec.getType().isCompatible(IntValue.class)) {
        if (cell.isMissing()) {
            stmt.setNull(dbIdx, Types.INTEGER);
        } else {
            int integer = ((IntValue) cell).getIntValue();
            stmt.setInt(dbIdx, integer);
        }
    } else if (cspec.getType().isCompatible(LongValue.class)) {
        if (cell.isMissing()) {
            stmt.setNull(dbIdx, Types.BIGINT);
        } else {
            long dbl = ((LongValue) cell).getLongValue();
            stmt.setLong(dbIdx, dbl);
        }
    } else if (cspec.getType().isCompatible(DoubleValue.class)) {
        if (cell.isMissing()) {
            stmt.setNull(dbIdx, Types.DOUBLE);
        } else {
            double dbl = ((DoubleValue) cell).getDoubleValue();
            if (Double.isNaN(dbl)) {
                stmt.setNull(dbIdx, Types.DOUBLE);
            } else {
                stmt.setDouble(dbIdx, dbl);
            }
        }
    } else if (cspec.getType().isCompatible(DateAndTimeValue.class)) {
        if (cell.isMissing()) {
            stmt.setNull(dbIdx, Types.DATE);
        } else {
            final DateAndTimeValue dateCell = (DateAndTimeValue) cell;
            final long corrDate = dateCell.getUTCTimeInMillis() - tz.getOffset(dateCell.getUTCTimeInMillis());
            if (!dateCell.hasTime() && !dateCell.hasMillis()) {
                java.sql.Date date = new java.sql.Date(corrDate);
                stmt.setDate(dbIdx, date);
            } else if (!dateCell.hasDate()) {
                java.sql.Time time = new java.sql.Time(corrDate);
                stmt.setTime(dbIdx, time);
            } else {
                java.sql.Timestamp timestamp = new java.sql.Timestamp(corrDate);
                stmt.setTimestamp(dbIdx, timestamp);
            }
        }
    } else if (cspec.getType().isCompatible(BinaryObjectDataValue.class)) {
        if (cell.isMissing()) {
            stmt.setNull(dbIdx, Types.BLOB);
        } else {
            try {
                BinaryObjectDataValue value = (BinaryObjectDataValue) cell;
                InputStream is = value.openInputStream();
                if (is == null) {
                    stmt.setNull(dbIdx, Types.BLOB);
                } else {
                    try {
                        // to be compatible with JDBC 3.0, the length of the stream is restricted to max integer,
                        // which are ~2GB; with JDBC 4.0 longs are supported and the respective method can be called
                        stmt.setBinaryStream(dbIdx, is, (int) value.length());
                    } catch (SQLException ex) {
                        // if no supported (i.e. SQLite) set byte array
                        byte[] bytes = IOUtils.toByteArray(is);
                        stmt.setBytes(dbIdx, bytes);
                    }
                }
            } catch (IOException ioe) {
                stmt.setNull(dbIdx, Types.BLOB);
            }
        }
    } else if (cspec.getType().isCompatible(CollectionDataValue.class)) {
        fillArray(stmt, dbIdx, cell, tz);
    } else if ((columnTypes == null) || cspec.getType().isCompatible(StringValue.class)) {
        if (cell.isMissing()) {
            stmt.setNull(dbIdx, Types.VARCHAR);
        } else {
            stmt.setString(dbIdx, cell.toString());
        }
    } else {
        Integer sqlType = columnTypes.get(dbIdx);
        if (sqlType == null) {
            sqlType = Types.VARCHAR;
        }
        if (cell.isMissing()) {
            stmt.setNull(dbIdx, sqlType);
        } else {
            stmt.setObject(dbIdx, cell.toString(), sqlType);
        }
    }
}
Also used : SQLException(java.sql.SQLException) DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) InputStream(java.io.InputStream) IOException(java.io.IOException) DoubleValue(org.knime.core.data.DoubleValue) BinaryObjectDataValue(org.knime.core.data.blob.BinaryObjectDataValue) BooleanValue(org.knime.core.data.BooleanValue) LongValue(org.knime.core.data.LongValue) IntValue(org.knime.core.data.IntValue)

Aggregations

LongValue (org.knime.core.data.LongValue)13 DoubleValue (org.knime.core.data.DoubleValue)11 IntValue (org.knime.core.data.IntValue)10 DateAndTimeValue (org.knime.core.data.date.DateAndTimeValue)10 DataCell (org.knime.core.data.DataCell)7 IntCell (org.knime.core.data.def.IntCell)7 LongCell (org.knime.core.data.def.LongCell)7 DateAndTimeCell (org.knime.core.data.date.DateAndTimeCell)6 DoubleCell (org.knime.core.data.def.DoubleCell)6 SQLException (java.sql.SQLException)4 BooleanValue (org.knime.core.data.BooleanValue)4 BinaryObjectDataValue (org.knime.core.data.blob.BinaryObjectDataValue)4 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 ResultSetMetaData (java.sql.ResultSetMetaData)2 Statement (java.sql.Statement)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2