Skip to content

Commit

Permalink
fix: preserve order of sorted data (#870)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasilev-alex committed Jul 20, 2020
1 parent 300b9dc commit 861db10
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 6 deletions.
29 changes: 23 additions & 6 deletions packages/cubejs-client-core/src/ResultSet.js
Expand Up @@ -26,6 +26,24 @@ const TIME_SERIES = {
const DateRegex = /^\d\d\d\d-\d\d-\d\d$/;
const LocalDateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z?$/;

const groupByToPairs = (keyFn) => {
const acc = new Map();

return (data) => {
data.forEach((row) => {
const key = keyFn(row);

if (!acc.has(key)) {
acc.set(key, []);
}

acc.get(key).push(row);
});

return Array.from(acc.entries());
};
};

class ResultSet {
constructor(loadResponse, options = {}) {
this.loadResponse = loadResponse;
Expand Down Expand Up @@ -233,7 +251,7 @@ class ResultSet {

pivot(pivotConfig) {
pivotConfig = this.normalizePivotConfig(pivotConfig);
let groupByXAxis = groupBy(({ xValues }) => this.axisValuesString(xValues));
let groupByXAxis = groupByToPairs(({ xValues }) => this.axisValuesString(xValues));

// eslint-disable-next-line no-unused-vars
let measureValue = (row, measure, xValues) => row[measure];
Expand All @@ -255,8 +273,8 @@ class ResultSet {
({ xValues }) => moment(xValues[0]).format(moment.HTML5_FMT.DATETIME_LOCAL_MS),
rows
);
return series.map(d => ({ [d]: byXValues[d] || [{ xValues: [d], row: {} }] }))
.reduce((a, b) => Object.assign(a, b), {});
return toPairs(series.map(d => ({ [d]: byXValues[d] || [{ xValues: [d], row: {} }] }))
.reduce((a, b) => Object.assign(a, b), {}));
};

// eslint-disable-next-line no-unused-vars
Expand All @@ -267,8 +285,7 @@ class ResultSet {
const xGrouped = pipe(
map(row => this.axisValues(pivotConfig.x)(row).map(xValues => ({ xValues, row }))),
unnest,
groupByXAxis,
toPairs
groupByXAxis
)(this.timeDimensionBackwardCompatibleData());

const allYValues = pipe(
Expand All @@ -284,7 +301,7 @@ class ResultSet {
)(xGrouped);

// eslint-disable-next-line no-unused-vars
return xGrouped.map(([xValuesString, rows]) => {
return xGrouped.map(([, rows]) => {
const { xValues } = rows[0];
const yGrouped = pipe(
map(({ row }) => this.axisValues(pivotConfig.y)(row).map(yValues => ({ yValues, row }))),
Expand Down
52 changes: 52 additions & 0 deletions packages/cubejs-client-core/src/tests/ResultSet.test.js
Expand Up @@ -912,5 +912,57 @@ describe('ResultSet', () => {
{ 'Orders.createdAt': '2020-01-10T13:50:05.000' }
]);
});

test('order is preserved', () => {
const resultSet = new ResultSet({
query: {
measures: ['User.total'],
dimensions: ['User.visits'],
filters: [],
timezone: 'UTC'
},
data: [
{
'User.total': 1,
'User.visits': 1
},
{
'User.total': 15,
'User.visits': 0.9
},
{
'User.total': 20,
'User.visits': 0.7
},
{
'User.total': 10,
'User.visits': 0
},
],
annotation: {
measures: {
'User.total': {}
},
dimensions: {
'User.visits': {
title: 'User Visits',
shortTitle: 'Visits',
type: 'number'
}
},
segments: {},
timeDimensions: {}
}
});

expect(resultSet.pivot()).toEqual(
[
{ xValues: [1], yValuesArray: [[['User.total'], 1]] },
{ xValues: [0.9], yValuesArray: [[['User.total'], 15]] },
{ xValues: [0.7], yValuesArray: [[['User.total'], 20]] },
{ xValues: [0], yValuesArray: [[['User.total'], 10]] },
]
);
});
});
});

0 comments on commit 861db10

Please sign in to comment.