Skip to content

Commit

Permalink
RN-702: Removed support for where clause in mergeRows transform
Browse files Browse the repository at this point in the history
- It was never used in practice, and made the code more complex
  • Loading branch information
rohan-bes committed Oct 27, 2022
1 parent 69c86d8 commit 51cb551
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,31 +121,6 @@ describe('parser', () => {
});

describe('in transforms', () => {
it('mergeRows supports parser lookups on where', () => {
const transform = buildTransform([
{
transform: 'mergeRows',
using: {
organisationUnit: 'exclude',
period: 'exclude',
'*': 'sum',
},
where: "=eq($organisationUnit, 'TO')",
},
]);
expect(transform(TransformTable.fromRows(PARSABLE_ANALYTICS))).toStrictEqual(
TransformTable.fromRows(
[
{ BCD1: 11 },
{ period: '20200101', organisationUnit: 'PG', BCD1: 7 },
{ period: '20200102', organisationUnit: 'PG', BCD1: 8 },
{ period: '20200103', organisationUnit: 'PG', BCD1: 2 },
],
['period', 'organisationUnit', 'BCD1'],
),
);
});

it('excludeRows supports parser lookups on where', () => {
const transform = buildTransform([
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import { yup } from '@tupaia/utils';
import { yupTsUtils } from '@tupaia/tsutils';

import { Context } from '../../../context';
import { TransformParser } from '../../parser';
import { mergeStrategies } from './mergeStrategies';
import { buildWhere } from '../where';
import { FieldValue, Row } from '../../../types';
import { buildCreateGroupKey } from './createGroupKey';
import { buildGetMergeStrategy } from './getMergeStrategy';
Expand All @@ -19,7 +16,6 @@ import { TransformTable } from '../../table';
type MergeRowsParams = {
createGroupKey: (row: Row) => string;
getMergeStrategy: (field: string) => keyof typeof mergeStrategies;
where: (parser: TransformParser) => boolean;
};

const optionalMergeStrategyNameValidator = yup
Expand Down Expand Up @@ -65,23 +61,15 @@ type Group = {
[columnName: string]: FieldValue[];
};

const groupRows = (table: TransformTable, params: MergeRowsParams, context: Context) => {
const groupRows = (table: TransformTable, params: MergeRowsParams) => {
const groupsByKey: Record<string, Group> = {};
const parser = new TransformParser(table, context);
const ungroupedRows: Row[] = []; // Rows that don't match the 'where' clause are left ungrouped

table.getRows().forEach((row: Row) => {
if (!params.where(parser)) {
ungroupedRows.push(row);
parser.next();
return;
}
const groupKey = params.createGroupKey(row);
addRowToGroup(groupsByKey, groupKey, row); // mutates groupsByKey
parser.next();
});

return { groups: Object.values(groupsByKey), ungroupedRows };
return Object.values(groupsByKey);
};

const addRowToGroup = (groupsByKey: Record<string, Group>, groupKey: string, row: Row) => {
Expand Down Expand Up @@ -115,11 +103,10 @@ const mergeGroups = (groups: Group[], params: MergeRowsParams) => {
});
};

const mergeRows = (table: TransformTable, params: MergeRowsParams, context: Context) => {
const { groups, ungroupedRows } = groupRows(table, params, context);
const mergeRows = (table: TransformTable, params: MergeRowsParams) => {
const groups = groupRows(table, params);
const mergedRows = mergeGroups(groups, params);
const newRowData = mergedRows.concat(ungroupedRows);
return new TransformTable(table.getColumns(), newRowData);
return new TransformTable(table.getColumns(), mergedRows);
};

const buildParams = (params: unknown): MergeRowsParams => {
Expand All @@ -130,11 +117,10 @@ const buildParams = (params: unknown): MergeRowsParams => {
return {
createGroupKey: buildCreateGroupKey(groupBy),
getMergeStrategy: buildGetMergeStrategy(groupBy, using),
where: buildWhere(params),
};
};

export const buildMergeRows = (params: unknown, context: Context) => {
export const buildMergeRows = (params: unknown) => {
const builtParams = buildParams(params);
return (table: TransformTable) => mergeRows(table, builtParams, context);
return (table: TransformTable) => mergeRows(table, builtParams);
};

0 comments on commit 51cb551

Please sign in to comment.