/** * Executes a simple nested loop join. The innerLoopDs will be copied in an in-memory dataset. * */ public static InMemoryDataSet nestedLoopJoin(DataSet innerLoopDs, DataSet outerLoopDs, Iterable<FilterItem> filtersIterable) { List<FilterItem> filters = new ArrayList<>(); for (FilterItem fi : filtersIterable) { filters.add(fi); } List<Row> innerRows = innerLoopDs.toRows(); List<SelectItem> allItems = new ArrayList<>(outerLoopDs.getSelectItems()); allItems.addAll(innerLoopDs.getSelectItems()); Set<FilterItem> applicableFilters = applicableFilters(filters, allItems); DataSetHeader jointHeader = new CachingDataSetHeader(allItems); List<Row> resultRows = new ArrayList<>(); for (Row outerRow : outerLoopDs) { for (Row innerRow : innerRows) { Object[] joinedRowObjects = new Object[outerRow.getValues().length + innerRow.getValues().length]; System.arraycopy(outerRow.getValues(), 0, joinedRowObjects, 0, outerRow.getValues().length); System.arraycopy(innerRow.getValues(), 0, joinedRowObjects, outerRow.getValues().length, innerRow.getValues().length); Row joinedRow = new DefaultRow(jointHeader, joinedRowObjects); if (applicableFilters.isEmpty() || applicableFilters.stream().allMatch(fi -> fi.accept(joinedRow))) { resultRows.add(joinedRow); } } } return new InMemoryDataSet(jointHeader, resultRows); }
/** * Executes a simple nested loop join. The innerLoopDs will be copied in an in-memory dataset. * */ public static InMemoryDataSet nestedLoopJoin(DataSet innerLoopDs, DataSet outerLoopDs, Iterable<FilterItem> filtersIterable) { List<FilterItem> filters = new ArrayList<>(); for (FilterItem fi : filtersIterable) { filters.add(fi); } List<Row> innerRows = innerLoopDs.toRows(); List<SelectItem> allItems = new ArrayList<>(outerLoopDs.getSelectItems()); allItems.addAll(innerLoopDs.getSelectItems()); Set<FilterItem> applicableFilters = applicableFilters(filters, allItems); DataSetHeader jointHeader = new CachingDataSetHeader(allItems); List<Row> resultRows = new ArrayList<>(); for (Row outerRow : outerLoopDs) { for (Row innerRow : innerRows) { Object[] joinedRowObjects = new Object[outerRow.getValues().length + innerRow.getValues().length]; System.arraycopy(outerRow.getValues(), 0, joinedRowObjects, 0, outerRow.getValues().length); System.arraycopy(innerRow.getValues(), 0, joinedRowObjects, outerRow.getValues().length, innerRow.getValues().length); Row joinedRow = new DefaultRow(jointHeader, joinedRowObjects); if (applicableFilters.isEmpty() || applicableFilters.stream().allMatch(fi -> fi.accept(joinedRow))) { resultRows.add(joinedRow); } } } return new InMemoryDataSet(jointHeader, resultRows); }