Search in sources :

Example 1 with JoinedRow

use of org.knime.core.data.def.JoinedRow in project knime-core by knime.

the class InMemoryIterator method next.

/**
 * {@inheritDoc}
 */
@Override
public DataRow next() {
    if (m_nextRow == null) {
        if (getNextMatch() == null) {
            throw new NoSuchElementException("No more rows");
        }
    }
    DataRow row;
    if (m_inverted) {
        row = new JoinedRow(m_nextRow[1], m_nextRow[0]);
    } else {
        row = new JoinedRow(m_nextRow[0], m_nextRow[1]);
    }
    m_nextRow = null;
    return row;
}
Also used : JoinedRow(org.knime.core.data.def.JoinedRow) DataRow(org.knime.core.data.DataRow) NoSuchElementException(java.util.NoSuchElementException)

Example 2 with JoinedRow

use of org.knime.core.data.def.JoinedRow in project knime-core by knime.

the class JoinedTableRowIterator method next.

/**
 * {@inheritDoc}
 */
@Override
public DataRow next() {
    if (m_leftIt.hasNext()) {
        final DataRow left = m_leftIt.next();
        final RowKey leftID = left.getKey();
        assert (!leftID.equals(m_lastRightID));
        DataRow right;
        RowKey rightID;
        boolean cont = true;
        do {
            right = null;
            rightID = null;
            if (!m_rightIt.hasNext() && initNewRightIterator()) {
                break;
            }
            right = nextRight();
            rightID = right.getKey();
            boolean madeWholeLoop = (m_lastRightID == null && !m_rightIt.hasNext()) || rightID.equals(m_lastRightID);
            cont = !madeWholeLoop && !rightID.equals(leftID);
            if (cont) {
                if (!m_hasSkippedRight) {
                    if (!m_table.isPrintedErrorOnSorting()) {
                        LOGGER.warn("Either both tables don't have all rows in " + "common or they are sorted differently.");
                        LOGGER.warn("(Iteration may have quadratic complexity " + "to ensure that all matching rows are " + "found.");
                        LOGGER.warn("I'll suppress further warnings.");
                        m_table.setPrintedErrorOnSorting(true);
                    }
                    m_hasSkippedRight = true;
                }
            }
        } while (cont);
        // no matching right row found
        if (!leftID.equals(rightID)) {
            right = getRightMissing(leftID);
        } else {
            m_lastRightID = rightID;
            assert (rightID.equals(leftID));
            m_rightSet.set(m_rightItCounter);
        }
        if (!m_leftIt.hasNext()) {
            // (according to the bit set m_rightSet)
            if (m_hasSkippedRight) {
                initNewRightIterator();
            }
            m_lastRightID = null;
            m_nextRightRow = findNextRightRow();
        }
        return new JoinedRow(left, right);
    } else {
        // in a perfect world, you don't come here...
        DataRow maybeNext = findNextRightRow();
        DataRow left = getLeftMissing(m_nextRightRow.getKey());
        DataRow merged = new JoinedRow(left, m_nextRightRow);
        m_nextRightRow = maybeNext;
        return merged;
    }
}
Also used : RowKey(org.knime.core.data.RowKey) JoinedRow(org.knime.core.data.def.JoinedRow) DataRow(org.knime.core.data.DataRow)

Example 3 with JoinedRow

use of org.knime.core.data.def.JoinedRow in project knime-core by knime.

the class JoinerNodeModel method processRemainingRightRows.

private void processRemainingRightRows(final DataContainer dc, final DataTable leftTable, final DataTable rightTable, final BitSet rightRows) throws CanceledExecutionException {
    if (m_rightIt.getIndex() >= rightRows.nextClearBit(0)) {
        m_rightIt = new CounterRowIterator(rightTable.iterator());
    }
    while (m_rightIt.hasNext()) {
        final DataRow rightRow = m_rightIt.next();
        m_exec.checkCanceled();
        if (!rightRows.get(m_rightIt.getIndex())) {
            dc.addRowToTable(new JoinedRow(new DefaultRow(rightRow.getKey(), JoinedTable.createMissingCells(leftTable.getDataTableSpec())), rightRow));
            rightRows.set(m_rightIt.getIndex());
            m_outputRows++;
            printProgress(rightRow.getKey());
        }
    }
}
Also used : JoinedRow(org.knime.core.data.def.JoinedRow) DefaultRow(org.knime.core.data.def.DefaultRow) DataRow(org.knime.core.data.DataRow)

Example 4 with JoinedRow

use of org.knime.core.data.def.JoinedRow in project knime-core by knime.

the class JoinerNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    BufferedDataContainer dc = exec.createDataContainer(JoinedTable.createSpec(inData[0].getDataTableSpec(), inData[1].getDataTableSpec(), m_method, m_suffix));
    DataTable leftTable = inData[0];
    DataTable rightTable = inData[1];
    // in the output
    if (JoinedTable.METHOD_FILTER.equals(m_method)) {
        DataTableSpec leftTableSpec = leftTable.getDataTableSpec();
        DataTableSpec rightTableSpec = rightTable.getDataTableSpec();
        LinkedHashSet<String> leftHash = new LinkedHashSet<String>();
        for (DataColumnSpec c : leftTableSpec) {
            leftHash.add(c.getName());
        }
        LinkedHashSet<String> rightHash = new LinkedHashSet<String>();
        for (DataColumnSpec c : rightTableSpec) {
            rightHash.add(c.getName());
        }
        rightHash.removeAll(leftHash);
        String[] survivors = rightHash.toArray(new String[rightHash.size()]);
        if (survivors.length < rightTableSpec.getNumColumns()) {
            rightTable = new FilterColumnTable(rightTable, survivors);
        }
    }
    final BitSet rightRows = new BitSet(inData[1].getRowCount());
    final LinkedHashMap<RowKey, SoftReference<Helper>> map = new LinkedHashMap<RowKey, SoftReference<Helper>>(1024);
    m_leftRows = 0;
    m_outputRows = 0;
    m_leftIt = null;
    m_rightIt = null;
    m_firstMapHelper = null;
    m_exec = exec;
    if (m_ignoreMissingRows) {
        m_max = Math.min(inData[0].getRowCount(), inData[1].getRowCount());
    } else {
        m_max = Math.max(inData[0].getRowCount(), inData[1].getRowCount());
    }
    while (true) {
        if (!readLeftChunk(leftTable, map)) {
            if (!m_ignoreMissingRows) {
                processRemainingRightRows(dc, leftTable, rightTable, rightRows);
            }
            break;
        }
        if ((m_rightIt == null) || (!m_rightIt.hasNext()) || (rightRows.nextClearBit(0) <= m_rightIt.getIndex())) {
            m_rightIt = new CounterRowIterator(rightTable.iterator());
        }
        while (m_rightIt.hasNext() && (map.size() > 0)) {
            m_exec.checkCanceled();
            DataRow rightRow = m_rightIt.next();
            SoftReference<Helper> sr = map.get(rightRow.getKey());
            if (sr != null) {
                Helper h = sr.get();
                if (h == null) {
                    map.remove(rightRow.getKey());
                } else {
                    h.m_rightRow = rightRow;
                    h.m_rightIndex = m_rightIt.getIndex();
                    if (h.m_leftIndex == m_leftRows) {
                        // m_firstMapHelper = h;
                        assert h.m_predecessor == null || !map.containsKey(h.m_predecessor.m_leftRow.getKey());
                        h.m_predecessor = null;
                        DataRow joinedRow = new JoinedRow(h.m_leftRow, h.m_rightRow);
                        dc.addRowToTable(joinedRow);
                        map.remove(rightRow.getKey());
                        rightRows.set(m_rightIt.getIndex());
                        m_leftRows++;
                        m_outputRows++;
                        printProgress(rightRow.getKey());
                    }
                }
            }
        }
        processRemainingLeftRowsInMap(dc, rightTable, map, rightRows);
        if (!m_ignoreMissingRows) {
            if (rightRows.cardinality() == inData[1].getRowCount()) {
                processRemainingLeftRowsInTable(dc, leftTable, rightTable);
            }
        } else {
            m_leftRows += map.size();
            map.clear();
            if (rightRows.cardinality() == inData[1].getRowCount()) {
                break;
            }
        }
    }
    m_leftIt = null;
    m_rightIt = null;
    m_exec = null;
    m_firstMapHelper = null;
    dc.close();
    return new BufferedDataTable[] { dc.getTable() };
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DataTable(org.knime.core.data.DataTable) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) RowKey(org.knime.core.data.RowKey) FilterColumnTable(org.knime.base.data.filter.column.FilterColumnTable) BitSet(java.util.BitSet) DataRow(org.knime.core.data.DataRow) LinkedHashMap(java.util.LinkedHashMap) DataColumnSpec(org.knime.core.data.DataColumnSpec) SoftReference(java.lang.ref.SoftReference) BufferedDataTable(org.knime.core.node.BufferedDataTable) JoinedRow(org.knime.core.data.def.JoinedRow)

Example 5 with JoinedRow

use of org.knime.core.data.def.JoinedRow in project knime-core by knime.

the class JoinerNodeModel method processRemainingLeftRowsInMap.

private void processRemainingLeftRowsInMap(final DataContainer dc, final DataTable rightTable, final Map<RowKey, SoftReference<Helper>> map, final BitSet rightRows) {
    // first read the remaining entries in the map
    for (SoftReference<Helper> sr : map.values()) {
        Helper h = sr.get();
        if (h == null) {
            break;
        }
        if (h.m_rightRow != null) {
            dc.addRowToTable(new JoinedRow(h.m_leftRow, h.m_rightRow));
            rightRows.set(h.m_rightIndex);
        } else if (!m_ignoreMissingRows) {
            dc.addRowToTable(new JoinedRow(h.m_leftRow, new DefaultRow(h.m_leftRow.getKey(), JoinedTable.createMissingCells(rightTable.getDataTableSpec()))));
        }
        m_outputRows++;
        printProgress(h.m_leftRow.getKey());
        m_leftRows++;
    }
    map.clear();
}
Also used : JoinedRow(org.knime.core.data.def.JoinedRow) DefaultRow(org.knime.core.data.def.DefaultRow)

Aggregations

JoinedRow (org.knime.core.data.def.JoinedRow)7 DataRow (org.knime.core.data.DataRow)6 DefaultRow (org.knime.core.data.def.DefaultRow)4 RowKey (org.knime.core.data.RowKey)3 DataColumnSpec (org.knime.core.data.DataColumnSpec)2 DataTableSpec (org.knime.core.data.DataTableSpec)2 SoftReference (java.lang.ref.SoftReference)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 BitSet (java.util.BitSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 NoSuchElementException (java.util.NoSuchElementException)1 TimeZone (java.util.TimeZone)1 FilterColumnTable (org.knime.base.data.filter.column.FilterColumnTable)1 DataCell (org.knime.core.data.DataCell)1 DataTable (org.knime.core.data.DataTable)1 RowIterator (org.knime.core.data.RowIterator)1 AppendedColumnRow (org.knime.core.data.append.AppendedColumnRow)1