Search in sources :

Example 6 with ColumnNamePolicy

use of org.knime.base.node.preproc.groupby.ColumnNamePolicy in project knime-core by knime.

the class DBPivotNodeDialog method validateSettings.

private void validateSettings(final NodeSettingsWO settings) throws InvalidSettingsException {
    // check if the dialog contains invalid group columns
    final Set<String> invalidInclCols = m_groupCol.getInvalidIncludeColumns();
    if (invalidInclCols != null && !invalidInclCols.isEmpty()) {
        throw new InvalidSettingsException(invalidInclCols.size() + " invalid group columns found.");
    }
    final Set<String> invalidInclPivotCols = m_pivotCol.getInvalidIncludeColumns();
    if (invalidInclPivotCols != null && !invalidInclPivotCols.isEmpty()) {
        throw new InvalidSettingsException(invalidInclPivotCols.size() + " invalid pivot columns found.");
    }
    final ColumnNamePolicy columnNamePolicy = ColumnNamePolicy.getPolicy4Label(m_columnNamePolicy.getStringValue());
    if (columnNamePolicy == null) {
        throw new InvalidSettingsException("Invalid column name policy");
    }
    m_aggregationPanel.validate();
}
Also used : ColumnNamePolicy(org.knime.base.node.preproc.groupby.ColumnNamePolicy) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) SettingsModelFilterString(org.knime.core.node.defaultnodesettings.SettingsModelFilterString) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString)

Example 7 with ColumnNamePolicy

use of org.knime.base.node.preproc.groupby.ColumnNamePolicy in project knime-core by knime.

the class DBPivotNodeModel method createQuery.

private String createQuery(final DatabaseQueryConnectionSettings connectionSettings, final DataTableSpec dataTableSpec, final ExecutionMonitor exec) throws SQLException, CanceledExecutionException {
    final StatementManipulator manipulator = connectionSettings.getUtility().getStatementManipulator();
    final String query = connectionSettings.getQuery();
    exec.setMessage("Getting pivot values.");
    ExecutionMonitor subExec = exec.createSubProgress(0.7);
    final List<String> pivotColumns = m_pivotCols.getIncludeList();
    final Map<DataColumnSpec, Set<Object>> pivotElements = connectionSettings.execute(getCredentialsProvider(), conn -> {
        int counter = 1;
        final Map<DataColumnSpec, Set<Object>> pivotMap = new LinkedHashMap<>();
        for (String pivotColumn : pivotColumns) {
            subExec.setProgress(counter / (double) pivotColumns.size(), "Fetching unique values for column " + pivotColumn + ". There are " + (pivotColumns.size() - counter) + " columns left.");
            DataColumnSpec columnSpec = dataTableSpec.getColumnSpec(pivotColumn);
            final String valueQuery = "SELECT DISTINCT " + manipulator.quoteIdentifier(pivotColumn) + " FROM (" + query + ") T";
            try (ResultSet valueSet = conn.createStatement().executeQuery(valueQuery)) {
                exec.checkCanceled();
                final Set<Object> vals = new HashSet<>();
                while (valueSet.next()) {
                    final Object dbVal = valueSet.getObject(1);
                    if (!valueSet.wasNull()) {
                        vals.add(dbVal);
                    }
                }
                pivotMap.put(columnSpec, vals);
                counter++;
            }
        }
        return pivotMap;
    });
    exec.setProgress(0.8, "Getting aggregation methods and columns.");
    List<String> groupByColumns = m_groupByCols.getIncludeList();
    final List<Pair<String, DBAggregationFunction>> aggValues = new LinkedList<>();
    for (int i = 0; i < m_aggregationFunction2Use.size(); i++) {
        exec.checkCanceled();
        final DBColumnAggregationFunctionRow aggregationFunction = m_aggregationFunction2Use.get(i);
        String colName = aggregationFunction.getColumnSpec().getName();
        DBAggregationFunction function = aggregationFunction.getFunction();
        aggValues.add(new Pair<>(colName, function));
    }
    final ColumnNamePolicy pivotColPoliciy = ColumnNamePolicy.getPolicy4Label(m_columnNamePolicy.getStringValue());
    PivotColumnNameGenerator pivotColName = new PivotColumnNameGenerator() {

        @Override
        public String createColumnName(final String columnName, final DBAggregationFunction function, final List<Object> pivotValues) {
            String vals = "";
            Iterator<Object> iterator = pivotValues.iterator();
            while (iterator.hasNext()) {
                vals = vals + iterator.next() + "_";
            }
            vals = vals.substring(0, vals.length() - 1);
            String method = function.getColumnName();
            switch(pivotColPoliciy) {
                case KEEP_ORIGINAL_NAME:
                    return vals + "+" + columnName;
                case AGGREGATION_METHOD_COLUMN_NAME:
                    return vals + "+" + method + "(" + columnName + ")";
                case COLUMN_NAME_AGGREGATION_METHOD:
                    return vals + "+" + columnName + " (" + method + ")";
                default:
                    throw new IllegalStateException("Unhandled column naming policy: " + pivotColPoliciy);
            }
        }
    };
    exec.setProgress(0.9, "Creating query.");
    exec.checkCanceled();
    return manipulator.getPivotStatement(query, groupByColumns, pivotElements, aggValues, pivotColName);
}
Also used : HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) Set(java.util.Set) SettingsModelFilterString(org.knime.core.node.defaultnodesettings.SettingsModelFilterString) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) StatementManipulator(org.knime.core.node.port.database.StatementManipulator) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap) ColumnNamePolicy(org.knime.base.node.preproc.groupby.ColumnNamePolicy) DataColumnSpec(org.knime.core.data.DataColumnSpec) DBColumnAggregationFunctionRow(org.knime.base.node.io.database.groupby.dialog.column.DBColumnAggregationFunctionRow) PivotColumnNameGenerator(org.knime.core.node.port.database.pivoting.PivotColumnNameGenerator) ResultSet(java.sql.ResultSet) DBAggregationFunction(org.knime.core.node.port.database.aggregation.DBAggregationFunction) DatabasePortObject(org.knime.core.node.port.database.DatabasePortObject) PortObject(org.knime.core.node.port.PortObject) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) HashSet(java.util.HashSet) Pair(org.knime.core.util.Pair)

Example 8 with ColumnNamePolicy

use of org.knime.base.node.preproc.groupby.ColumnNamePolicy in project knime-core by knime.

the class MovingAggregationNodeModel method validateSettings.

/**
 * {@inheritDoc}
 */
@Override
protected void validateSettings(final NodeSettingsRO settings) throws InvalidSettingsException {
    m_winLength.validateSettings(settings);
    m_windowType.validateSettings(settings);
    m_cumulativeComputing.validateSettings(settings);
    m_handleMissings.validateSettings(settings);
    m_removeRetainedCols.validateSettings(settings);
    m_maxUniqueVals.validateSettings(settings);
    m_valueDelimiter.validateSettings(settings);
    final List<ColumnAggregator> aggregators = ColumnAggregator.loadColumnAggregators(settings);
    final List<DataTypeAggregator> typeAggregators = new LinkedList<>();
    final List<PatternAggregator> regexAggregators = new LinkedList<>();
    try {
        regexAggregators.addAll(PatternAggregator.loadAggregators(settings, CFG_PATTERN_AGGREGATORS));
        typeAggregators.addAll(DataTypeAggregator.loadAggregators(settings, CFG_DATA_TYPE_AGGREGATORS));
    } catch (InvalidSettingsException e) {
    // introduced in 2.11
    }
    if (aggregators.isEmpty() && regexAggregators.isEmpty() && typeAggregators.isEmpty()) {
        throw new IllegalArgumentException("Please select at least one aggregation option");
    }
    final String policyLabel = ((SettingsModelString) m_columnNamePolicy.createCloneWithValidatedValue(settings)).getStringValue();
    final ColumnNamePolicy namePolicy = ColumnNamePolicy.getPolicy4Label(policyLabel);
    try {
        GroupByNodeModel.checkDuplicateAggregators(namePolicy, aggregators);
    } catch (IllegalArgumentException e) {
        throw new InvalidSettingsException(e.getMessage());
    }
    final boolean removeAggrCols = ((SettingsModelBoolean) m_removeAggregationCols.createCloneWithValidatedValue(settings)).getBooleanValue();
    if (ColumnNamePolicy.KEEP_ORIGINAL_NAME.equals(namePolicy) && !removeAggrCols) {
        throw new InvalidSettingsException("'" + ColumnNamePolicy.KEEP_ORIGINAL_NAME.getLabel() + "' option only valid if aggregation columns are filtered");
    }
}
Also used : PatternAggregator(org.knime.base.data.aggregation.dialogutil.pattern.PatternAggregator) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DataTypeAggregator(org.knime.base.data.aggregation.dialogutil.type.DataTypeAggregator) LinkedList(java.util.LinkedList) SettingsModelBoolean(org.knime.core.node.defaultnodesettings.SettingsModelBoolean) ColumnNamePolicy(org.knime.base.node.preproc.groupby.ColumnNamePolicy) ColumnAggregator(org.knime.base.data.aggregation.ColumnAggregator) InvalidSettingsException(org.knime.core.node.InvalidSettingsException)

Aggregations

ColumnNamePolicy (org.knime.base.node.preproc.groupby.ColumnNamePolicy)8 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)6 SettingsModelFilterString (org.knime.core.node.defaultnodesettings.SettingsModelFilterString)5 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)4 DBColumnAggregationFunctionRow (org.knime.base.node.io.database.groupby.dialog.column.DBColumnAggregationFunctionRow)3 DataColumnSpec (org.knime.core.data.DataColumnSpec)3 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 ColumnAggregator (org.knime.base.data.aggregation.ColumnAggregator)2 GlobalSettings (org.knime.base.data.aggregation.GlobalSettings)2 DBAggregationFunction (org.knime.core.node.port.database.aggregation.DBAggregationFunction)2 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Set (java.util.Set)1 OperatorColumnSettings (org.knime.base.data.aggregation.OperatorColumnSettings)1 PatternAggregator (org.knime.base.data.aggregation.dialogutil.pattern.PatternAggregator)1 DataTypeAggregator (org.knime.base.data.aggregation.dialogutil.type.DataTypeAggregator)1