use of org.knime.core.node.port.PortObjectSpec in project knime-core by knime.
the class DatabaseLoopingNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
final BufferedDataTable inputTable = (BufferedDataTable) inData[0];
final long rowCount = inputTable.size();
final String column = m_columnModel.getStringValue();
final DataTableSpec spec = inputTable.getDataTableSpec();
final int colIdx = spec.findColumnIndex(column);
if (colIdx < 0) {
throw new InvalidSettingsException("Column " + column + " not found in input table.");
}
final Set<DataCell> values = new HashSet<>();
BufferedDataContainer buf = null;
final String oQuery = getQuery();
final Collection<DataCell> curSet = new LinkedHashSet<>();
final DBReader load = loadConnectionSettings(inData[getNrInPorts() - 1]);
try {
final int noValues = m_noValues.getIntValue();
MutableInteger rowCnt = new MutableInteger(0);
for (Iterator<DataRow> it = inputTable.iterator(); it.hasNext(); ) {
exec.checkCanceled();
DataCell cell = it.next().getCell(colIdx);
if (values.contains(cell) && !it.hasNext() && curSet.isEmpty()) {
continue;
}
values.add(cell);
curSet.add(cell);
if (curSet.size() == noValues || !it.hasNext()) {
StringBuilder queryValues = new StringBuilder();
for (DataCell v : curSet) {
if (queryValues.length() > 0) {
queryValues.append("','");
}
queryValues.append(v.toString());
}
String newQuery = parseQuery(oQuery.replaceAll(IN_PLACE_HOLDER, queryValues.toString()));
load.updateQuery(newQuery);
exec.setProgress(values.size() * (double) noValues / rowCount, "Selecting all values \"" + queryValues + "\"...");
final BufferedDataTable table = getResultTable(exec, inData, load);
if (buf == null) {
DataTableSpec resSpec = table.getDataTableSpec();
buf = exec.createDataContainer(createSpec(resSpec, spec.getColumnSpec(column)));
}
if (m_aggByRow.getBooleanValue()) {
aggregate(table, rowCnt, buf, CollectionCellFactory.createListCell(curSet));
} else {
notAggregate(table, rowCnt, buf, CollectionCellFactory.createListCell(curSet));
}
curSet.clear();
}
}
if (buf == null) {
// create empty dummy container with spec generated during #configure
final PortObjectSpec[] inSpec;
if ((inData.length > 1) && (inData[1] instanceof DatabaseConnectionPortObject)) {
DatabaseConnectionPortObject dbPort = (DatabaseConnectionPortObject) inData[1];
inSpec = new PortObjectSpec[] { inputTable.getSpec(), dbPort.getSpec() };
} else {
inSpec = new PortObjectSpec[] { inputTable.getSpec() };
}
final String newQuery = createDummyValueQuery(spec, colIdx, oQuery);
setQuery(newQuery);
final DataTableSpec resultSpec = getResultSpec(inSpec);
final DataTableSpec outSpec = createSpec(resultSpec, spec.getColumnSpec(column));
buf = exec.createDataContainer(outSpec);
}
buf.close();
} catch (CanceledExecutionException cee) {
throw cee;
} catch (Exception e) {
setLastSpec(null);
throw e;
} finally {
// reset query to original
setQuery(oQuery);
}
final BufferedDataTable resultTable = buf.getTable();
setLastSpec(resultTable.getDataTableSpec());
return new BufferedDataTable[] { resultTable };
}
use of org.knime.core.node.port.PortObjectSpec in project knime-core by knime.
the class DatabaseLoopingNodeModel method configure.
/**
* {@inheritDoc}
*/
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
final DataTableSpec lastSpec = getLastSpec();
if (lastSpec != null) {
return new DataTableSpec[] { lastSpec };
}
final DataTableSpec tableSpec = (DataTableSpec) inSpecs[0];
String column = m_columnModel.getStringValue();
if (column == null) {
throw new InvalidSettingsException("No column selected.");
}
final int colIdx = tableSpec.findColumnIndex(column);
if (colIdx < 0) {
throw new InvalidSettingsException("Column '" + column + "' not found in input data.");
}
if ((inSpecs.length > 1) && (inSpecs[1] instanceof DatabaseConnectionPortObjectSpec)) {
DatabaseConnectionSettings connSettings = ((DatabaseConnectionPortObjectSpec) inSpecs[1]).getConnectionSettings(getCredentialsProvider());
m_settings.setValidateQuery(connSettings.getRetrieveMetadataInConfigure());
} else {
m_settings.setValidateQuery(true);
}
if (!m_settings.getValidateQuery()) {
setLastSpec(null);
return new DataTableSpec[] { null };
}
final String oQuery = getQuery();
PortObjectSpec[] spec = null;
try {
final String newQuery;
newQuery = createDummyValueQuery(tableSpec, colIdx, oQuery);
setQuery(newQuery);
spec = new DataTableSpec[] { getResultSpec(inSpecs) };
} catch (InvalidSettingsException e) {
setLastSpec(null);
throw e;
} catch (SQLException ex) {
setLastSpec(null);
Throwable cause = ExceptionUtils.getRootCause(ex);
if (cause == null) {
cause = ex;
}
throw new InvalidSettingsException("Could not determine table spec from database query: " + cause.getMessage(), ex);
} finally {
setQuery(oQuery);
}
if (spec[0] == null) {
return spec;
} else {
final DataTableSpec resultSpec = createSpec((DataTableSpec) spec[0], tableSpec.getColumnSpec(column));
setLastSpec(resultSpec);
return new DataTableSpec[] { resultSpec };
}
}
use of org.knime.core.node.port.PortObjectSpec in project knime-core by knime.
the class DBNumericBinnerNodeModel method configure.
/**
* {@inheritDoc}
*/
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
final DatabasePortObjectSpec inDatabasePortObjectSpec = (DatabasePortObjectSpec) inSpecs[0];
DatabaseQueryConnectionSettings connectionSettings = inDatabasePortObjectSpec.getConnectionSettings(getCredentialsProvider());
boolean suppCase = connectionSettings.getUtility().supportsCase();
if (!suppCase) {
if (m_columnToBins.size() > 1) {
throw new InvalidSettingsException("Database does not support \"CASE\". Please choose only one column.");
}
}
if (m_columnToBins.isEmpty()) {
setWarningMessage("No columns selected for binning");
} else {
checkDuplicateBinNames();
}
DataTableSpec outDataTableSpec = DBAutoBinner.createNewDataTableSpec(inDatabasePortObjectSpec.getDataTableSpec(), m_columnToAppended);
PMMLPortObject outPMMLPortObject = createPMMLPortObject(inDatabasePortObjectSpec.getDataTableSpec(), outDataTableSpec);
DBBinnerMaps binnerMaps = DBAutoBinner.intoBinnerMaps(outPMMLPortObject, inDatabasePortObjectSpec.getDataTableSpec());
DatabasePortObjectSpec outDatabasePortObjectSpec = null;
try {
outDatabasePortObjectSpec = createDatabasePortObjectSpec(connectionSettings, inDatabasePortObjectSpec, binnerMaps);
} catch (InvalidKeyException | BadPaddingException | IllegalBlockSizeException | SQLException | IOException e) {
// TODO Auto-generated catch block
}
return new PortObjectSpec[] { outDatabasePortObjectSpec, outPMMLPortObject.getSpec() };
}
use of org.knime.core.node.port.PortObjectSpec in project knime-core by knime.
the class DBColumnRenameNodeModel method configure.
/**
* {@inheritDoc}
*/
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
final DatabasePortObjectSpec dbSpec = (DatabasePortObjectSpec) inSpecs[0];
final DatabasePortObjectSpec outSpec = createDBOutSpec(dbSpec);
return new PortObjectSpec[] { outSpec };
}
use of org.knime.core.node.port.PortObjectSpec in project knime-core by knime.
the class DBGroupByNodeModel2 method configure.
/**
* {@inheritDoc}
*/
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
final DatabasePortObjectSpec dbSpec = (DatabasePortObjectSpec) inSpecs[0];
final DataTableSpec tableSpec = dbSpec.getDataTableSpec();
final DatabaseQueryConnectionSettings connection = dbSpec.getConnectionSettings(null);
final String dbIdentifier = connection.getDatabaseIdentifier();
final List<DBColumnAggregationFunctionRow> columnFunctions = DBColumnAggregationFunctionRow.loadFunctions(m_settings, DBGroupByNodeModel2.CFG_AGGREGATION_FUNCTIONS, dbIdentifier, tableSpec);
final ArrayList<DBColumnAggregationFunctionRow> invalidColAggrs = new ArrayList<>(1);
final Set<String> usedColNames = new HashSet<>(tableSpec.getNumColumns());
usedColNames.addAll(m_groupByCols.getIncludeList());
m_aggregationFunction2Use.clear();
for (DBColumnAggregationFunctionRow row : columnFunctions) {
final DataColumnSpec columnSpec = row.getColumnSpec();
final DataColumnSpec inputSpec = tableSpec.getColumnSpec(columnSpec.getName());
final AggregationFunction function = row.getFunction();
if (inputSpec == null || !inputSpec.getType().equals(columnSpec.getType())) {
invalidColAggrs.add(row);
continue;
}
if (function instanceof InvalidAggregationFunction) {
throw new InvalidSettingsException(((InvalidAggregationFunction) function).getErrorMessage());
}
if (function.hasOptionalSettings()) {
try {
function.configure(tableSpec);
} catch (InvalidSettingsException e) {
throw new InvalidSettingsException("Exception in aggregation function " + function.getLabel() + " of column " + row.getColumnSpec().getName() + ": " + e.getMessage());
}
}
usedColNames.add(row.getColumnSpec().getName());
m_aggregationFunction2Use.add(row);
}
final List<DBPatternAggregationFunctionRow> patternFunctions = DBPatternAggregationFunctionRow.loadFunctions(m_settings, CFG_PATTERN_AGGREGATION_FUNCTIONS, dbIdentifier, tableSpec);
if (tableSpec.getNumColumns() > usedColNames.size() && !patternFunctions.isEmpty()) {
for (final DataColumnSpec spec : tableSpec) {
if (!usedColNames.contains(spec.getName())) {
for (final DBPatternAggregationFunctionRow patternFunction : patternFunctions) {
final Pattern pattern = patternFunction.getRegexPattern();
final DBAggregationFunction function = patternFunction.getFunction();
if (pattern != null && pattern.matcher(spec.getName()).matches() && function.isCompatible(spec.getType())) {
final DBColumnAggregationFunctionRow row = new DBColumnAggregationFunctionRow(spec, patternFunction.getFunction());
m_aggregationFunction2Use.add(row);
usedColNames.add(spec.getName());
}
}
}
}
}
final List<DBDataTypeAggregationFunctionRow> typeFunctions = DBDataTypeAggregationFunctionRow.loadFunctions(m_settings, CFG_TYPE_AGGREGATION_FUNCTIONS, dbIdentifier, tableSpec);
// check if some columns are left
if (tableSpec.getNumColumns() > usedColNames.size() && !typeFunctions.isEmpty()) {
for (final DataColumnSpec spec : tableSpec) {
if (!usedColNames.contains(spec.getName())) {
final DataType dataType = spec.getType();
for (final DBDataTypeAggregationFunctionRow typeAggregator : typeFunctions) {
if (typeAggregator.isCompatibleType(dataType)) {
final DBColumnAggregationFunctionRow row = new DBColumnAggregationFunctionRow(spec, typeAggregator.getFunction());
m_aggregationFunction2Use.add(row);
usedColNames.add(spec.getName());
}
}
}
}
}
if (m_groupByCols.getIncludeList().isEmpty() && m_aggregationFunction2Use.isEmpty() && !m_addCountStar.getBooleanValue()) {
throw new InvalidSettingsException("Please select at least one group or aggregation function or the " + "COUNT(*) option.");
}
if (!invalidColAggrs.isEmpty()) {
setWarningMessage(invalidColAggrs.size() + " aggregation functions ignored due to incompatible columns.");
}
return new PortObjectSpec[] { createDbOutSpec(dbSpec, true) };
}
Aggregations