Search in sources :

Example 1 with IntValue

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

the class TSAverageHandler method getMean.

private DataCell getMean() {
    if (m_previous instanceof IntValue) {
        // get an int, create an int
        double mean = (((DoubleValue) m_previous).getDoubleValue() + ((DoubleValue) m_next).getDoubleValue()) * 0.5;
        return new IntCell((int) Math.round(mean));
    }
    if (m_previous instanceof LongValue) {
        // get an int, create an int
        double mean = (((DoubleValue) m_previous).getDoubleValue() + ((DoubleValue) m_next).getDoubleValue()) * 0.5;
        return new LongCell(Math.round(mean));
    }
    if (m_previous instanceof DoubleValue) {
        // get an double, create an double
        double mean = (((DoubleValue) m_previous).getDoubleValue() + ((DoubleValue) m_next).getDoubleValue()) * 0.5;
        return new DoubleCell(mean);
    }
    if (m_previous instanceof DateAndTimeValue) {
        // get an int, create an int
        DateAndTimeValue dataCell1 = (DateAndTimeValue) m_previous;
        DateAndTimeValue dataCell2 = ((DateAndTimeValue) m_next);
        boolean hasDate = dataCell1.hasDate() | dataCell2.hasDate();
        boolean hasTime = dataCell1.hasTime() | dataCell2.hasTime();
        boolean hasMilis = dataCell1.hasMillis() | dataCell2.hasMillis();
        double d = dataCell1.getUTCTimeInMillis() + dataCell2.getUTCTimeInMillis();
        d *= 0.5;
        return new DateAndTimeCell((long) d, hasDate, hasTime, hasMilis);
    }
    return DataType.getMissingCell();
}
Also used : LongCell(org.knime.core.data.def.LongCell) DoubleValue(org.knime.core.data.DoubleValue) DoubleCell(org.knime.core.data.def.DoubleCell) DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) LongValue(org.knime.core.data.LongValue) DateAndTimeCell(org.knime.core.data.date.DateAndTimeCell) IntValue(org.knime.core.data.IntValue) IntCell(org.knime.core.data.def.IntCell)

Example 2 with IntValue

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

the class TSLinearHandler method getLinearInterpolation.

private DataCell getLinearInterpolation(final double stepnumber, final double stepcount) {
    if (m_previous instanceof DoubleValue || m_previous instanceof IntValue || m_previous instanceof LongValue) {
        double prev = ((DoubleValue) m_previous).getDoubleValue();
        double next = ((DoubleValue) m_next).getDoubleValue();
        double lin = prev + 1.0 * stepnumber / (1.0 * stepcount) * (next - prev);
        if (m_previous instanceof IntValue) {
            // get an int, create an int
            return new IntCell((int) Math.round(lin));
        }
        if (m_previous instanceof LongValue) {
            // get an long, create an long
            return new LongCell(Math.round(lin));
        }
        return new DoubleCell(lin);
    }
    if (m_previous instanceof DateAndTimeValue) {
        // get an int, create an int
        DateAndTimeValue dataCell1 = (DateAndTimeValue) m_previous;
        DateAndTimeValue dataCell2 = ((DateAndTimeValue) m_next);
        boolean hasDate = dataCell1.hasDate() | dataCell2.hasDate();
        boolean hasTime = dataCell1.hasTime() | dataCell2.hasTime();
        boolean hasMilis = dataCell1.hasMillis() | dataCell2.hasMillis();
        double prev = dataCell1.getUTCTimeInMillis();
        double next = dataCell2.getUTCTimeInMillis();
        double d = prev + stepnumber / stepcount * (next - prev);
        return new DateAndTimeCell((long) d, hasDate, hasTime, hasMilis);
    }
    return DataType.getMissingCell();
}
Also used : LongCell(org.knime.core.data.def.LongCell) DoubleValue(org.knime.core.data.DoubleValue) DoubleCell(org.knime.core.data.def.DoubleCell) DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) LongValue(org.knime.core.data.LongValue) DateAndTimeCell(org.knime.core.data.date.DateAndTimeCell) IntValue(org.knime.core.data.IntValue) IntCell(org.knime.core.data.def.IntCell)

Example 3 with IntValue

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

the class TwoSampleTTestNodeView method renderDescriptiveStatistics.

/**
 * Create HTML for the descriptive statistics.
 */
private String renderDescriptiveStatistics() {
    StringBuilder buffer = NodeViewUtil.createHtmlHeader();
    buffer.append("<body>\n");
    buffer.append("<h3>Group Statistics</h3>");
    NodeViewUtil.renderDataTable(getNodeModel().getDescritiveStatistics(), TwoSampleTTestStatistics.TEST_COLUMN, Arrays.asList(new String[] { TwoSampleTTestStatistics.TEST_COLUMN, TwoSampleTTestStatistics.IGNORED_COUNT_GROUP_COL }), new HashMap<String, String>(), buffer);
    BufferedDataTable descStats = getNodeModel().getDescritiveStatistics();
    if (descStats.getRowCount() > 0) {
        CloseableRowIterator iter = descStats.iteratorFailProve();
        int ignoredIndex = descStats.getSpec().findColumnIndex(TwoSampleTTestStatistics.IGNORED_COUNT_GROUP_COL);
        DataCell ignoredCell = iter.next().getCell(ignoredIndex);
        if (!ignoredCell.isMissing()) {
            int ignoredCount = ((IntValue) ignoredCell).getIntValue();
            if (ignoredCount > 0) {
                buffer.append("<p>");
                buffer.append(ignoredCount);
                buffer.append(ignoredCount > 1 ? " rows have been ignored. Their " : " row has been ignored. Its ");
                buffer.append("value in the grouping column is neither \"");
                buffer.append(getNodeModel().getSettings().getGroupOne());
                buffer.append("\" nor \"");
                buffer.append(getNodeModel().getSettings().getGroupTwo());
                buffer.append("\".");
                buffer.append("</p>");
            }
        }
        iter.close();
    }
    buffer.append("</body>\n");
    buffer.append("</html>\n");
    return buffer.toString();
}
Also used : BufferedDataTable(org.knime.core.node.BufferedDataTable) CloseableRowIterator(org.knime.core.data.container.CloseableRowIterator) DataCell(org.knime.core.data.DataCell) IntValue(org.knime.core.data.IntValue)

Example 4 with IntValue

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

the class CategoryToNumberNodeDialogPane method loadSettingsFrom.

/**
 * {@inheritDoc}
 */
@Override
protected void loadSettingsFrom(final NodeSettingsRO settings, final PortObjectSpec[] specs) throws NotConfigurableException {
    m_settings.loadSettingsForDialog(settings);
    String[] included = null != m_settings.getIncludedColumns() ? m_settings.getIncludedColumns() : new String[0];
    m_includedColumns.update((DataTableSpec) specs[0], false, included);
    if (m_includedColumns.getExcludedColumnSet().size() + m_includedColumns.getIncludedColumnSet().size() == 0) {
        throw new NotConfigurableException("No column in " + "the input compatible to \"StringValue\".");
    }
    m_includedColumns.setKeepAllSelected(m_settings.getIncludeAll());
    m_appendColums.setSelected(m_settings.getAppendColumns());
    m_columnSuffix.setText(m_settings.getColumnSuffix());
    m_columnSuffix.setEnabled(m_appendColums.isSelected());
    m_startIndex.setValue(m_settings.getStartIndex());
    m_increment.setValue(m_settings.getIncrement());
    m_maxCategories.setValue(m_settings.getMaxCategories());
    if (!m_settings.getDefaultValue().isMissing()) {
        IntValue value = (IntValue) m_settings.getDefaultValue();
        m_defaultValue.setText(Integer.toString(value.getIntValue()));
    } else {
        m_defaultValue.setText("");
    }
    if (!m_settings.getMapMissingTo().isMissing()) {
        IntValue value = (IntValue) m_settings.getMapMissingTo();
        m_mapMissingTo.setText(Integer.toString(value.getIntValue()));
    } else {
        m_mapMissingTo.setText("");
    }
}
Also used : NotConfigurableException(org.knime.core.node.NotConfigurableException) IntValue(org.knime.core.data.IntValue)

Example 5 with IntValue

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

the class DBWriterImpl method writeData.

/**
 * {@inheritDoc}
 * @deprecated
 */
@Deprecated
@Override
public String writeData(final String table, final RowInput input, final long rowCount, final boolean appendData, final ExecutionMonitor exec, final Map<String, String> sqlTypes, final CredentialsProvider cp, final int batchSize, final boolean insertNullForMissingCols, final boolean failOnError) throws Exception {
    final DatabaseConnectionSettings conSettings = getDatabaseConnectionSettings();
    // final Connection conn = conSettings.createConnection(cp);
    return conSettings.execute(cp, conn -> {
        exec.setMessage("Waiting for free database connection...");
        final StringBuilder columnNamesForInsertStatement = new StringBuilder("(");
        // synchronized (conSettings.syncConnection(conn)) {
        exec.setMessage("Start writing rows in database...");
        DataTableSpec spec = input.getDataTableSpec();
        // mapping from spec columns to database columns
        final int[] mapping;
        // append data to existing table
        if (appendData) {
            if (conSettings.getUtility().tableExists(conn, table)) {
                String query = conSettings.getUtility().getStatementManipulator().forMetadataOnly("SELECT * FROM " + table);
                try (ResultSet rs = conn.createStatement().executeQuery(query)) {
                    ResultSetMetaData rsmd = rs.getMetaData();
                    final Map<String, Integer> columnNames = new LinkedHashMap<String, Integer>();
                    for (int i = 0; i < spec.getNumColumns(); i++) {
                        String colName = replaceColumnName(spec.getColumnSpec(i).getName());
                        columnNames.put(colName.toLowerCase(), i);
                    }
                    // sanity check to lock if all input columns are in db
                    ArrayList<String> columnNotInSpec = new ArrayList<String>(columnNames.keySet());
                    for (int i = 0; i < rsmd.getColumnCount(); i++) {
                        String dbColName = replaceColumnName(rsmd.getColumnName(i + 1));
                        if (columnNames.containsKey(dbColName.toLowerCase())) {
                            columnNotInSpec.remove(dbColName.toLowerCase());
                            columnNamesForInsertStatement.append(dbColName).append(',');
                        } else if (insertNullForMissingCols) {
                            // append the column name of a missing column only if the insert null for missing
                            // column option is enabled
                            columnNamesForInsertStatement.append(dbColName).append(',');
                        }
                    }
                    if (rsmd.getColumnCount() > 0) {
                        columnNamesForInsertStatement.deleteCharAt(columnNamesForInsertStatement.length() - 1);
                    }
                    columnNamesForInsertStatement.append(')');
                    if (columnNotInSpec.size() > 0) {
                        throw new RuntimeException("No. of columns in input" + " table > in database; not existing columns: " + columnNotInSpec.toString());
                    }
                    mapping = new int[rsmd.getColumnCount()];
                    for (int i = 0; i < mapping.length; i++) {
                        String name = replaceColumnName(rsmd.getColumnName(i + 1)).toLowerCase();
                        if (!columnNames.containsKey(name)) {
                            mapping[i] = -1;
                            continue;
                        }
                        mapping[i] = columnNames.get(name);
                        DataColumnSpec cspec = spec.getColumnSpec(mapping[i]);
                        int type = rsmd.getColumnType(i + 1);
                        switch(type) {
                            // check all boolean compatible types
                            case Types.BIT:
                            case Types.BOOLEAN:
                                // types must be compatible to BooleanValue
                                if (!cspec.getType().isCompatible(BooleanValue.class)) {
                                    throw new RuntimeException("Column \"" + name + "\" of type \"" + cspec.getType() + "\" from input does not match type " + "\"" + rsmd.getColumnTypeName(i + 1) + "\" in database at position " + i);
                                }
                                break;
                            // check all int compatible types
                            case Types.TINYINT:
                            case Types.SMALLINT:
                            case Types.INTEGER:
                                // types must be compatible to IntValue
                                if (!cspec.getType().isCompatible(IntValue.class)) {
                                    throw new RuntimeException("Column \"" + name + "\" of type \"" + cspec.getType() + "\" from input does not match type " + "\"" + rsmd.getColumnTypeName(i + 1) + "\" in database at position " + i);
                                }
                                break;
                            case Types.BIGINT:
                                // types must also be compatible to LongValue
                                if (!cspec.getType().isCompatible(LongValue.class)) {
                                    throw new RuntimeException("Column \"" + name + "\" of type \"" + cspec.getType() + "\" from input does not match type " + "\"" + rsmd.getColumnTypeName(i + 1) + "\" in database at position " + i);
                                }
                                break;
                            // check all double compatible types
                            case Types.FLOAT:
                            case Types.DOUBLE:
                            case Types.NUMERIC:
                            case Types.DECIMAL:
                            case Types.REAL:
                                // types must also be compatible to DoubleValue
                                if (!cspec.getType().isCompatible(DoubleValue.class)) {
                                    throw new RuntimeException("Column \"" + name + "\" of type \"" + cspec.getType() + "\" from input does not match type " + "\"" + rsmd.getColumnTypeName(i + 1) + "\" in database at position " + i);
                                }
                                break;
                            // check for date-and-time compatible types
                            case Types.DATE:
                            case Types.TIME:
                            case Types.TIMESTAMP:
                                // types must also be compatible to DataValue
                                if (!cspec.getType().isCompatible(DateAndTimeValue.class)) {
                                    throw new RuntimeException("Column \"" + name + "\" of type \"" + cspec.getType() + "\" from input does not match type " + "\"" + rsmd.getColumnTypeName(i + 1) + "\" in database at position " + i);
                                }
                                break;
                            // check for blob compatible types
                            case Types.BLOB:
                            case Types.BINARY:
                            case Types.LONGVARBINARY:
                                // types must also be compatible to DataValue
                                if (!cspec.getType().isCompatible(BinaryObjectDataValue.class)) {
                                    throw new RuntimeException("Column \"" + name + "\" of type \"" + cspec.getType() + "\" from input does not match type " + "\"" + rsmd.getColumnTypeName(i + 1) + "\" in database at position " + i);
                                }
                                break;
                        }
                    }
                }
            } else {
                LOGGER.info("Table \"" + table + "\" does not exist in database, " + "will create new table.");
                // and create new table
                final String query = "CREATE TABLE " + table + " " + createTableStmt(spec, sqlTypes, columnNamesForInsertStatement);
                LOGGER.debug("Executing SQL statement as execute: " + query);
                try (Statement statement = conn.createStatement()) {
                    statement.execute(query);
                }
                if (!conn.getAutoCommit()) {
                    conn.commit();
                }
                mapping = new int[spec.getNumColumns()];
                for (int k = 0; k < mapping.length; k++) {
                    mapping[k] = k;
                }
            }
        } else {
            LOGGER.debug("Append not enabled. Table " + table + " will be dropped if exists.");
            mapping = new int[spec.getNumColumns()];
            for (int k = 0; k < mapping.length; k++) {
                mapping[k] = k;
            }
            Statement statement = null;
            try {
                statement = conn.createStatement();
                // remove existing table (if any)
                final String query = "DROP TABLE " + table;
                LOGGER.debug("Executing SQL statement as execute: " + query);
                statement.execute(query);
            } catch (Throwable t) {
                if (statement == null) {
                    throw new SQLException("Could not create SQL statement," + " reason: " + t.getMessage(), t);
                }
                LOGGER.info("Exception droping table \"" + table + "\": " + t.getMessage() + ". Will create new table.");
            } finally {
                if (!conn.getAutoCommit()) {
                    conn.commit();
                }
            }
            // and create new table
            final String query = "CREATE TABLE " + table + " " + createTableStmt(spec, sqlTypes, columnNamesForInsertStatement);
            LOGGER.debug("Executing SQL statement as execute: " + query);
            statement.execute(query);
            statement.close();
            if (!conn.getAutoCommit()) {
                conn.commit();
            }
        }
        // this is a (temporary) workaround for bug #5802: if there is a DataValue column in the input table
        // we need to use the SQL type for creating the insert statements.
        Map<Integer, Integer> columnTypes = null;
        for (DataColumnSpec cs : spec) {
            if (cs.getType().getPreferredValueClass() == DataValue.class) {
                columnTypes = getColumnTypes(conn, table);
                break;
            }
        }
        final String insertStamtement = createInsertStatment(table, columnNamesForInsertStatement.toString(), mapping, insertNullForMissingCols);
        // problems writing more than 13 columns. the prepare statement
        // ensures that we can set the columns directly row-by-row, the
        // database will handle the commit
        long cnt = 1;
        long errorCnt = 0;
        long allErrors = 0;
        // count number of rows added to current batch
        int curBatchSize = 0;
        LOGGER.debug("Executing SQL statement as prepareStatement: " + insertStamtement);
        final PreparedStatement stmt = conn.prepareStatement(insertStamtement);
        // remember auto-commit flag
        final boolean autoCommit = conn.getAutoCommit();
        DatabaseConnectionSettings.setAutoCommit(conn, false);
        try {
            final TimeZone timezone = conSettings.getTimeZone();
            // get the first row
            DataRow row;
            DataRow nextRow = input.poll();
            // iterate over all incoming data rows
            while (nextRow != null) {
                row = nextRow;
                cnt++;
                exec.checkCanceled();
                if (rowCount > 0) {
                    exec.setProgress(1.0 * cnt / rowCount, "Row " + "#" + cnt);
                } else {
                    exec.setProgress("Writing Row#" + cnt);
                }
                int dbIdx = 1;
                for (int i = 0; i < mapping.length; i++) {
                    if (mapping[i] < 0) {
                        if (insertNullForMissingCols) {
                            // insert only null if the insert null for missing col option is enabled
                            stmt.setNull(dbIdx++, Types.NULL);
                        }
                    } else {
                        final DataColumnSpec cspec = spec.getColumnSpec(mapping[i]);
                        final DataCell cell = row.getCell(mapping[i]);
                        fillStatement(stmt, dbIdx++, cspec, cell, timezone, columnTypes);
                    }
                }
                // if batch mode
                if (batchSize > 1) {
                    // a new row will be added
                    stmt.addBatch();
                }
                // get one more input row to check if 'row' is the last one
                nextRow = input.poll();
                curBatchSize++;
                // if batch size equals number of row in batch or input table at end
                if ((curBatchSize == batchSize) || nextRow == null) {
                    curBatchSize = 0;
                    try {
                        // write batch
                        if (batchSize > 1) {
                            stmt.executeBatch();
                        } else {
                            // or write single row
                            stmt.execute();
                        }
                    } catch (Throwable t) {
                        final String errorMsg;
                        if (batchSize > 1) {
                            errorMsg = "Error while adding rows #" + (cnt - batchSize) + " - #" + cnt + ", reason: " + t.getMessage();
                        } else {
                            errorMsg = "Error while adding row #" + cnt + " (" + row.getKey() + "), reason: " + t.getMessage();
                        }
                        // introduced in KNIME 3.3.2
                        if (failOnError) {
                            try {
                                // rollback all changes
                                conn.rollback();
                                LOGGER.debug("Rollback complete transaction with auto commit=" + autoCommit);
                            } catch (Throwable ex) {
                                LOGGER.info("Failed rollback after db exception with auto commit=" + autoCommit + ". Rollback error: " + ex.getMessage(), ex);
                            }
                            throw new Exception(errorMsg, t);
                        }
                        // anyway.
                        if (!conn.getAutoCommit()) {
                            conn.commit();
                        }
                        allErrors++;
                        if (errorCnt > -1) {
                            exec.setMessage(errorMsg);
                            if (errorCnt++ < 10) {
                                LOGGER.warn(errorMsg);
                            } else {
                                errorCnt = -1;
                                LOGGER.warn(errorMsg + " - more errors...", t);
                            }
                        }
                    } finally {
                        // clear batch if in batch mode
                        if (batchSize > 1) {
                            stmt.clearBatch();
                        }
                    }
                }
            }
            if (!conn.getAutoCommit()) {
                conn.commit();
            }
            if (allErrors == 0) {
                return null;
            } else {
                return "Errors \"" + allErrors + "\" writing " + (cnt - 1) + " rows.";
            }
        } finally {
            DatabaseConnectionSettings.setAutoCommit(conn, autoCommit);
            stmt.close();
        }
    });
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) SQLException(java.sql.SQLException) DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) ArrayList(java.util.ArrayList) DataRow(org.knime.core.data.DataRow) LinkedHashMap(java.util.LinkedHashMap) ResultSetMetaData(java.sql.ResultSetMetaData) DataColumnSpec(org.knime.core.data.DataColumnSpec) BinaryObjectDataValue(org.knime.core.data.blob.BinaryObjectDataValue) BooleanValue(org.knime.core.data.BooleanValue) ResultSet(java.sql.ResultSet) IntValue(org.knime.core.data.IntValue) DatabaseConnectionSettings(org.knime.core.node.port.database.DatabaseConnectionSettings) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException) TimeZone(java.util.TimeZone) DoubleValue(org.knime.core.data.DoubleValue) LongValue(org.knime.core.data.LongValue) DataCell(org.knime.core.data.DataCell)

Aggregations

IntValue (org.knime.core.data.IntValue)19 DoubleValue (org.knime.core.data.DoubleValue)14 DataCell (org.knime.core.data.DataCell)12 LongValue (org.knime.core.data.LongValue)10 DateAndTimeValue (org.knime.core.data.date.DateAndTimeValue)10 DataRow (org.knime.core.data.DataRow)6 DateAndTimeCell (org.knime.core.data.date.DateAndTimeCell)6 DoubleCell (org.knime.core.data.def.DoubleCell)6 IntCell (org.knime.core.data.def.IntCell)6 LongCell (org.knime.core.data.def.LongCell)6 SQLException (java.sql.SQLException)4 BooleanValue (org.knime.core.data.BooleanValue)4 DataTableSpec (org.knime.core.data.DataTableSpec)4 BinaryObjectDataValue (org.knime.core.data.blob.BinaryObjectDataValue)4 BufferedDataTable (org.knime.core.node.BufferedDataTable)4 IOException (java.io.IOException)3 DataColumnSpec (org.knime.core.data.DataColumnSpec)3 StringValue (org.knime.core.data.StringValue)3 InputStream (java.io.InputStream)2 PreparedStatement (java.sql.PreparedStatement)2