use of org.knime.core.data.LongValue 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();
}
use of org.knime.core.data.LongValue 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();
}
use of org.knime.core.data.LongValue in project knime-core by knime.
the class RuleEngineNodeModel method getRulesOutcome.
/**
* @param outType
* @param row
* @param r
* @param isDisallowlongOutputForCompatibility TODO
* @param variableProvider TODO
* @return
* @noreference This method is not intended to be referenced by clients.
*/
public static final DataCell getRulesOutcome(final DataType outType, final DataRow row, final List<Rule> rules, final boolean isDisallowLongOutputForCompatibility, final VariableProvider variableProvider) {
for (Rule r : rules) {
if (r.getCondition().matches(row, variableProvider).getOutcome() == MatchState.matchedAndStop) {
Outcome outcome2 = r.getOutcome();
// r.getSideEffect().perform(row, this);
DataCell cell = (DataCell) outcome2.getComputedResult(row, variableProvider);
// ... don't want Booleans (also implementing Long), for instance)
if (cell instanceof LongCell && isDisallowLongOutputForCompatibility) {
long l = ((LongValue) cell).getLongValue();
if (l > Integer.MAX_VALUE) {
throw new RuntimeException("Values larger than " + Integer.MAX_VALUE + " not supported in old instances of the node -- recreate the node " + "(node was created using an KNIME version < 3.2");
}
cell = new IntCell((int) l);
}
if (outType.equals(StringCell.TYPE) && !cell.isMissing() && !cell.getType().equals(StringCell.TYPE)) {
return new StringCell(cell.toString());
} else {
return cell;
}
}
}
return DataType.getMissingCell();
}
use of org.knime.core.data.LongValue 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();
}
});
}
use of org.knime.core.data.LongValue in project knime-core by knime.
the class BinningUtil method binNominal.
/**
* @param colSpec the {@link DataColumnSpec} of the column to bin
* @param noOfBins the number of bins
* @return <code>true</code> if the bins should be nominal
*/
public static boolean binNominal(final DataColumnSpec colSpec, final int noOfBins) {
final DataType dataType = colSpec.getType();
if (!dataType.isCompatible(DoubleValue.class)) {
// it's not numerical
return true;
}
if (dataType.isCompatible(LongValue.class)) {
// it's an integer...
final DataColumnDomain domain = colSpec.getDomain();
final long lowerBound = ((LongValue) domain.getLowerBound()).getLongValue();
final long upperBound = ((LongValue) domain.getUpperBound()).getLongValue();
if (upperBound - lowerBound <= noOfBins) {
// an own bin
return true;
}
}
return false;
}
Aggregations