use of org.knime.core.util.UniqueNameGenerator in project knime-core by knime.
the class DBReaderImpl method loopTable.
/**
* @since 3.2
*/
@SuppressWarnings("resource")
@Override
public BufferedDataTableRowOutput loopTable(final ExecutionContext exec, final CredentialsProvider cp, final RowInput data, final long rowCount, final boolean failIfException, final boolean appendInputColumns, final boolean includeEmptyResults, final boolean retainAllColumns, final String... columns) throws Exception {
if (m_blobFactory == null) {
m_blobFactory = new BinaryObjectCellFactory();
}
final DatabaseQueryConnectionSettings dbConn = getQueryConnection();
return getQueryConnection().execute(cp, conn -> {
/* Get the selected timezone */
final TimeZone timezone = dbConn.getTimeZone();
/* Get the input table spec */
final DataTableSpec inSpec = data.getDataTableSpec();
/* Create PreparedStatement */
final String query = dbConn.getQuery();
LOGGER.debug("Executing SQL preparedStatement as execute: " + query);
/* Initialize the error table */
final UniqueNameGenerator errorGenerator = new UniqueNameGenerator(inSpec);
final DataColumnSpec errorColSpec = errorGenerator.newColumn(DEF_ERROR_COL_NAME, StringCell.TYPE);
final DataTableSpec errorSpec = new DataTableSpec(inSpec, new DataTableSpec(errorColSpec));
m_errorContainer = exec.createDataContainer(errorSpec);
DataTableSpec dbSpec = new DataTableSpec();
BufferedDataTableRowOutput output = null;
exec.setMessage("Start reading rows from database...");
try (final PreparedStatement stmt = conn.prepareStatement(query)) {
long inDataCounter = 1;
long rowIdCounter = 0;
DataRow row;
while ((row = data.poll()) != null) {
exec.checkCanceled();
if (rowCount > 0) {
exec.setProgress(1.0 * inDataCounter / rowCount, "Row " + "#" + inDataCounter + " of " + rowCount);
} else {
exec.setProgress("Writing Row " + "#" + inDataCounter);
}
final DataCell[] inCells = new DataCell[columns.length];
for (int i = 0; i < columns.length; i++) {
final int dbIdx = i + 1;
final int colIdx = inSpec.findColumnIndex(columns[i]);
final DataColumnSpec colSpec = inSpec.getColumnSpec(colIdx);
inCells[i] = row.getCell(colIdx);
fillStatement(stmt, dbIdx, colSpec, inCells[i], timezone, null);
}
try (final ResultSet result = stmt.executeQuery()) {
/* In the first iteration, create the out DataTableSpec and BufferedDataTableRowOutput */
if (output == null) {
dbSpec = createTableSpec(result.getMetaData());
if (appendInputColumns) {
// Create out DataTableSpec for input table
final DataTableSpec newInSpec;
if (retainAllColumns) {
newInSpec = inSpec;
} else {
final DataColumnSpec[] inColSpecs = new DataColumnSpec[columns.length];
for (int i = 0; i < inColSpecs.length; i++) {
inColSpecs[i] = inSpec.getColumnSpec(columns[i]);
}
newInSpec = new DataTableSpec(inColSpecs);
}
// Create DataTableSpec for database columns, rename if necessary
final UniqueNameGenerator generator = new UniqueNameGenerator(newInSpec);
final DataColumnSpec[] dbColSpecs = new DataColumnSpec[dbSpec.getNumColumns()];
for (int i = 0; i < dbColSpecs.length; i++) {
final DataColumnSpec colSpec = dbSpec.getColumnSpec(i);
dbColSpecs[i] = generator.newColumn(colSpec.getName(), colSpec.getType());
}
dbSpec = new DataTableSpec(dbColSpecs);
m_spec = new DataTableSpec(newInSpec, dbSpec);
} else {
m_spec = dbSpec;
}
output = new BufferedDataTableRowOutput(exec.createDataContainer(m_spec));
}
/* Iterate over the result of the database query and put it into the output table*/
final RowIterator dbRowIterator = createDBRowIterator(dbSpec, dbConn, m_blobFactory, false, result, rowIdCounter);
boolean hasDbRow = false;
while (dbRowIterator.hasNext()) {
hasDbRow = true;
final DataRow dbRow = dbRowIterator.next();
if (appendInputColumns) {
final DataRow inRow;
if (retainAllColumns) {
inRow = new DefaultRow(dbRow.getKey(), row);
} else {
inRow = new DefaultRow(dbRow.getKey(), inCells);
}
final JoinedRow joinedRow = new JoinedRow(inRow, dbRow);
output.push(joinedRow);
} else {
output.push(dbRow);
}
rowIdCounter++;
}
/* Append columns using MissingCell if no result is returned */
if (!hasDbRow && appendInputColumns && includeEmptyResults) {
final DataCell[] cells = new DataCell[dbSpec.getNumColumns()];
Arrays.fill(cells, DataType.getMissingCell());
final RowKey rowKey = RowKey.createRowKey(rowIdCounter);
final DataRow emptyDbRows = new DefaultRow(rowKey, cells);
final DataRow inRow;
if (retainAllColumns) {
inRow = new DefaultRow(rowKey, row);
} else {
inRow = new DefaultRow(rowKey, inCells);
}
final JoinedRow joinedRow = new JoinedRow(inRow, emptyDbRows);
output.push(joinedRow);
rowIdCounter++;
}
inDataCounter++;
} catch (SQLException ex) {
LOGGER.debug("SQLException: " + ex.getMessage());
if (!failIfException) {
if (output == null) {
throw new SQLException(ex);
}
final AppendedColumnRow appendedRow = new AppendedColumnRow(row, new StringCell(ex.getMessage()));
m_errorContainer.addRowToTable(appendedRow);
} else {
throw new SQLException(ex);
}
}
}
} finally {
data.close();
if (output == null) {
output = new BufferedDataTableRowOutput(exec.createDataContainer(inSpec));
}
output.close();
if (m_errorContainer != null) {
m_errorContainer.close();
}
}
return output;
});
}
use of org.knime.core.util.UniqueNameGenerator in project knime-core by knime.
the class ExtractMissingValueCauseNodeModel method createCellFactory.
/**
* @param inSpec Current input spec
* @return The cell factory for the output cells.
*/
private AppendErrorMessageCellFactory createCellFactory(final DataTableSpec inSpec) {
String[] includeList = m_colSelect.applyTo(inSpec).getIncludes();
String suffix = m_suffix.getStringValue();
int[] includeIndexes = Arrays.stream(includeList).mapToInt(s -> inSpec.findColumnIndex(s)).toArray();
UniqueNameGenerator nameGen = new UniqueNameGenerator(inSpec);
DataColumnSpec[] newCols = Arrays.stream(includeList).map(s -> nameGen.newColumn(s + suffix, StringCell.TYPE)).toArray(DataColumnSpec[]::new);
return new AppendErrorMessageCellFactory(includeIndexes, newCols);
}
use of org.knime.core.util.UniqueNameGenerator in project knime-core by knime.
the class TreeEnsembleClassificationPredictorCellFactory method createFactory.
/**
* Creates a TreeEnsembleClassificationPredictorCellFactory from the provided <b>predictor</b>
* @param predictor
* @return an instance of TreeEnsembleClassificationPredictorCellFactory configured according to the settings of the provided
* <b>predictor<b>
* @throws InvalidSettingsException
*/
public static TreeEnsembleClassificationPredictorCellFactory createFactory(final TreeEnsemblePredictor predictor) throws InvalidSettingsException {
DataTableSpec testDataSpec = predictor.getDataSpec();
TreeEnsembleModelPortObjectSpec modelSpec = predictor.getModelSpec();
TreeEnsembleModelPortObject modelObject = predictor.getModelObject();
TreeEnsemblePredictorConfiguration configuration = predictor.getConfiguration();
UniqueNameGenerator nameGen = new UniqueNameGenerator(testDataSpec);
Map<String, DataCell> targetValueMap = modelSpec.getTargetColumnPossibleValueMap();
List<DataColumnSpec> newColsList = new ArrayList<DataColumnSpec>();
DataType targetColType = modelSpec.getTargetColumn().getType();
String targetColName = configuration.getPredictionColumnName();
DataColumnSpec targetCol = nameGen.newColumn(targetColName, targetColType);
newColsList.add(targetCol);
if (configuration.isAppendPredictionConfidence()) {
newColsList.add(nameGen.newColumn(targetCol.getName() + " (Confidence)", DoubleCell.TYPE));
}
if (configuration.isAppendClassConfidences()) {
// and this class is not called)
assert targetValueMap != null : "Target column has no possible values";
for (String v : targetValueMap.keySet()) {
newColsList.add(nameGen.newColumn(v, DoubleCell.TYPE));
}
}
if (configuration.isAppendModelCount()) {
newColsList.add(nameGen.newColumn("model count", IntCell.TYPE));
}
// assigned
assert modelObject == null || targetValueMap != null : "Target values must be known during execution";
DataColumnSpec[] newCols = newColsList.toArray(new DataColumnSpec[newColsList.size()]);
int[] learnColumnInRealDataIndices = modelSpec.calculateFilterIndices(testDataSpec);
return new TreeEnsembleClassificationPredictorCellFactory(predictor, targetValueMap, newCols, learnColumnInRealDataIndices);
}
use of org.knime.core.util.UniqueNameGenerator in project knime-core by knime.
the class TreeEnsembleRegressionPredictorCellFactory method createFactory.
/**
* Creates a TreeEnsembleRegressionPredictorCellFactory from the provided <b>predictor</b>
*
* @param predictor
* @return an instance of TreeEnsembleRegressionPredictorCellFactory configured according to the settings of the provided
* <b>predictor<b>
* @throws InvalidSettingsException
*/
public static TreeEnsembleRegressionPredictorCellFactory createFactory(final TreeEnsemblePredictor predictor) throws InvalidSettingsException {
DataTableSpec testDataSpec = predictor.getDataSpec();
TreeEnsembleModelPortObjectSpec modelSpec = predictor.getModelSpec();
// TreeEnsembleModelPortObject modelObject = predictor.getModelObject();
TreeEnsemblePredictorConfiguration configuration = predictor.getConfiguration();
UniqueNameGenerator nameGen = new UniqueNameGenerator(testDataSpec);
List<DataColumnSpec> newColsList = new ArrayList<DataColumnSpec>();
String targetColName = configuration.getPredictionColumnName();
DataColumnSpec targetCol = nameGen.newColumn(targetColName, DoubleCell.TYPE);
newColsList.add(targetCol);
if (configuration.isAppendPredictionConfidence()) {
newColsList.add(nameGen.newColumn(targetCol.getName() + " (Prediction Variance)", DoubleCell.TYPE));
}
if (configuration.isAppendModelCount()) {
newColsList.add(nameGen.newColumn("model count", IntCell.TYPE));
}
DataColumnSpec[] newCols = newColsList.toArray(new DataColumnSpec[newColsList.size()]);
int[] learnColumnInRealDataIndices = modelSpec.calculateFilterIndices(testDataSpec);
return new TreeEnsembleRegressionPredictorCellFactory(predictor, newCols, learnColumnInRealDataIndices);
}
use of org.knime.core.util.UniqueNameGenerator in project knime-core by knime.
the class RegressionTreePredictorCellFactory method createFactory.
/**
* @param predictor
* @return factory based on RegressionTreePredictor <b>predictor</b>
* @throws InvalidSettingsException
*/
public static RegressionTreePredictorCellFactory createFactory(final RegressionTreePredictor predictor) throws InvalidSettingsException {
DataTableSpec testDataSpec = predictor.getDataSpec();
RegressionTreeModelPortObjectSpec modelSpec = predictor.getModelSpec();
RegressionTreePredictorConfiguration configuration = predictor.getConfiguration();
UniqueNameGenerator nameGen = new UniqueNameGenerator(testDataSpec);
List<DataColumnSpec> newColsList = new ArrayList<DataColumnSpec>();
String targetColName = configuration.getPredictionColumnName();
DataColumnSpec targetCol = nameGen.newColumn(targetColName, DoubleCell.TYPE);
newColsList.add(targetCol);
DataColumnSpec[] newCols = newColsList.toArray(new DataColumnSpec[newColsList.size()]);
int[] learnColumnInRealDataIndices = modelSpec.calculateFilterIndices(testDataSpec);
return new RegressionTreePredictorCellFactory(predictor, newCols, learnColumnInRealDataIndices);
}
Aggregations