All files / datamodel/src/operator natural-join.js

100% Statements 1/1
100% Branches 0/0
100% Functions 1/1
100% Lines 1/1

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46                                                                                      1x    
import { crossProduct } from './cross-product';
import { naturalJoinFilter } from './natural-join-filter-function';
 
 
/**
 * {@link https://www.geeksforgeeks.org/extended-operators-in-relational-algebra | Natural join} is a special kind
 * of joining where filtering of rows are performed internally by resolving common fields are from both table and
 * the rows with common value are included.
 *
 * @example
 *  //@preamble_start
 *  Promise.all([loadData('/static/cars.json'), loadData('/static/cars-schema.json')]).then(function (params) {
 *      const data = params[0];
 *      const schema = params[1];
 *      const DataModel = muze.DataModel;
 *      const dm = new DataModel(data, schema);
 *  //@preamble_end
 *  // DataModel instance is created from https://www.charts.com/static/cars.json data,
 *  // https://www.charts.com/static/cars-schema.json schema and assigned to variable dm. DataModel is extracted from
 *  // muze namespace and assigned to the variable DataModel.
 *
 *  // Creates two small DataModel instance from the original DataModel instance, which will be joined. Used chained
 *  // operator for conciseness.
 *  const makerDM = dm.groupBy(['Origin', 'Maker']).project(['Origin', 'Maker']);
 *  const nameDM = dm.project(['Name','Miles_per_Gallon'])
 *
 *  const naturalJoin = DataModel.Operatros.naturalJoin;
 *  const outputDM = naturalJoin(makerDM, nameDM);
 *  //@preamble_start
 *  printDM(outputDM);
 *  });
 *  //@preamble_end
 *
 * @public
 * @namespace DataModel
 * @segment Operator
 *
 * @param {DataModel} leftDM Instance of DataModel
 * @param {DataModel} rightDM Instance of DataModel
 *
 * @return {DataModel} New DataModel instance with joined data
 */
export function naturalJoin (dataModel1, dataModel2) {
    return crossProduct(dataModel1, dataModel2, naturalJoinFilter(dataModel1, dataModel2), true);
}