From a9f907e4b9f4e29eaf0137389177c6ce62b4bbae Mon Sep 17 00:00:00 2001 From: wjgogogo <906626481@qq.com> Date: Wed, 20 Dec 2023 19:51:46 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=BB=B4=E5=BA=A6?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E4=B8=AD=E5=8C=85=E5=90=AB=20[&]=20=E5=88=86?= =?UTF-8?q?=E9=9A=94=E7=AC=A6=E5=AF=BC=E8=87=B4=E7=9A=84=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E4=B8=8D=E6=AD=A3=E7=A1=AE=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/s2-core/src/data-set/interface.ts | 4 +++- packages/s2-core/src/utils/dataset/pivot-data-set.ts | 10 +++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/s2-core/src/data-set/interface.ts b/packages/s2-core/src/data-set/interface.ts index 7d8c8ab1c3..5cab0f105f 100644 --- a/packages/s2-core/src/data-set/interface.ts +++ b/packages/s2-core/src/data-set/interface.ts @@ -1,5 +1,5 @@ import type { QueryDataType } from '../common/constant/query'; -import type { Data, SortParam } from '../common/interface'; +import type { SortParam } from '../common/interface'; import type { Node } from '../facet/layout/node'; import type { BaseDataSet } from './base-data-set'; @@ -11,6 +11,8 @@ export type Query = Record; export type PivotMetaValue = { // 当前维度结合父级维度生成的完整 id 信息 id: string; + // 当前维度结合父级维度生成的完整 dimensions 信息,主要是预防 field 数据本身出现 [&] 导致维度信息识别不正确 + dimensions: string[]; // 当前维度 value: string; level: number; diff --git a/packages/s2-core/src/utils/dataset/pivot-data-set.ts b/packages/s2-core/src/utils/dataset/pivot-data-set.ts index 077eb3a9aa..8e73baa460 100644 --- a/packages/s2-core/src/utils/dataset/pivot-data-set.ts +++ b/packages/s2-core/src/utils/dataset/pivot-data-set.ts @@ -214,11 +214,10 @@ export function getDataPath(params: DataPathParams) { const value = dimensionValues[i]; if (isFirstCreate && currentMeta && !currentMeta?.has(value)) { - const id = dimensionValues + const currentDimensions = dimensionValues .slice(0, i + 1) - .map((it) => String(it)) - .join(ID_SEPARATOR); - + .map((it) => String(it)); + const id = currentDimensions.join(ID_SEPARATOR); const isTotal = value === TOTAL_VALUE; let level; @@ -232,6 +231,7 @@ export function getDataPath(params: DataPathParams) { currentMeta.set(value, { id, + dimensions: currentDimensions, value, level, children: new Map(), @@ -536,7 +536,7 @@ export function flattenDimensionValues(params: { sortedDimensionValues, }); - return metaValues.map((v) => v.id.split(ID_SEPARATOR)); + return metaValues.map((v) => v.dimensions); } export function getFlattenDimensionValues( From 1b4e5711ac7cde80df6594236266b1f8a89c433a Mon Sep 17 00:00:00 2001 From: wjgogogo <906626481@qq.com> Date: Wed, 20 Dec 2023 20:18:11 +0800 Subject: [PATCH 2/2] =?UTF-8?q?test:=20=E5=8D=95=E6=B5=8B=E8=A1=A5?= =?UTF-8?q?=E5=85=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../unit/utils/dataset/pivot-data-set-spec.ts | 145 +++++++++++++----- 1 file changed, 107 insertions(+), 38 deletions(-) diff --git a/packages/s2-core/__tests__/unit/utils/dataset/pivot-data-set-spec.ts b/packages/s2-core/__tests__/unit/utils/dataset/pivot-data-set-spec.ts index 89384dca37..418a6753cf 100644 --- a/packages/s2-core/__tests__/unit/utils/dataset/pivot-data-set-spec.ts +++ b/packages/s2-core/__tests__/unit/utils/dataset/pivot-data-set-spec.ts @@ -18,7 +18,7 @@ describe('pivot-data-set utils test', () => { city: [ '$$total$$[&]$$total$$', '四川省[&]$$total$$', - '四川省[&]成都市', + '四川省[&]成[&]都[&]市', '四川省[&]绵阳市', '浙江省[&]$$total$$', '浙江省[&]杭州市', @@ -27,9 +27,9 @@ describe('pivot-data-set utils test', () => { type: [ '$$total$$[&]$$total$$[&]$$total$$', '四川省[&]$$total$$[&]$$total$$', - '四川省[&]成都市[&]$$total$$', - '四川省[&]成都市[&]家具', - '四川省[&]成都市[&]办公用品', + '四川省[&]成[&]都[&]市[&]$$total$$', + '四川省[&]成[&]都[&]市[&]家具', + '四川省[&]成[&]都[&]市[&]办公用品', '四川省[&]绵阳市[&]$$total$$', '四川省[&]绵阳市[&]家具', '四川省[&]绵阳市[&]办公用品', @@ -45,13 +45,13 @@ describe('pivot-data-set utils test', () => { subType: [ '$$total$$[&]$$total$$[&]$$total$$[&]$$total$$', '四川省[&]$$total$$[&]$$total$$[&]$$total$$', - '四川省[&]成都市[&]$$total$$[&]$$total$$', - '四川省[&]成都市[&]家具[&]$$total$$', - '四川省[&]成都市[&]家具[&]桌子', - '四川省[&]成都市[&]家具[&]沙发', - '四川省[&]成都市[&]办公用品[&]$$total$$', - '四川省[&]成都市[&]办公用品[&]笔', - '四川省[&]成都市[&]办公用品[&]纸张', + '四川省[&]成[&]都[&]市[&]$$total$$[&]$$total$$', + '四川省[&]成[&]都[&]市[&]家具[&]$$total$$', + '四川省[&]成[&]都[&]市[&]家具[&]桌子', + '四川省[&]成[&]都[&]市[&]家具[&]沙发', + '四川省[&]成[&]都[&]市[&]办公用品[&]$$total$$', + '四川省[&]成[&]都[&]市[&]办公用品[&]笔', + '四川省[&]成[&]都[&]市[&]办公用品[&]纸张', '四川省[&]绵阳市[&]$$total$$[&]$$total$$', '四川省[&]绵阳市[&]家具[&]$$total$$', '四川省[&]绵阳市[&]家具[&]桌子', @@ -84,26 +84,36 @@ describe('pivot-data-set utils test', () => { childField: 'city', level: 1, id: '四川省', + dimensions: ['四川省'], children: new Map([ [ - '成都市', + '成[&]都[&]市', { childField: 'type', level: 1, - id: '四川省[&]成都市', + id: '四川省[&]成[&]都[&]市', + dimensions: ['四川省', '成[&]都[&]市'], children: new Map([ [ '家具', { childField: 'subType', level: 1, - id: '四川省[&]成都市[&]家具', + id: '四川省[&]成[&]都[&]市[&]家具', + dimensions: ['四川省', '成[&]都[&]市', '家具'], children: new Map([ [ '桌子', { childFiled: null, - id: '四川省[&]成都市[&]家具[&]桌子', + id: '四川省[&]成[&]都[&]市[&]家具[&]桌子', + dimensions: [ + '四川省', + '成[&]都[&]市', + '家具', + '桌子', + ], + level: 1, children: new Map(), }, @@ -113,7 +123,13 @@ describe('pivot-data-set utils test', () => { { childFiled: null, level: 2, - id: '四川省[&]成都市[&]家具[&]沙发', + id: '四川省[&]成[&]都[&]市[&]家具[&]沙发', + dimensions: [ + '四川省', + '成[&]都[&]市', + '家具', + '沙发', + ], children: new Map(), }, ], @@ -125,14 +141,21 @@ describe('pivot-data-set utils test', () => { { childField: 'subType', level: 2, - id: '四川省[&]成都市[&]办公用品', + id: '四川省[&]成[&]都[&]市[&]办公用品', + dimensions: ['四川省', '成[&]都[&]市', '办公用品'], children: new Map([ [ '笔', { childFiled: null, level: 1, - id: '四川省[&]成都市[&]办公用品[&]笔', + id: '四川省[&]成[&]都[&]市[&]办公用品[&]笔', + dimensions: [ + '四川省', + '成[&]都[&]市', + '办公用品', + '笔', + ], children: new Map(), }, ], @@ -141,7 +164,13 @@ describe('pivot-data-set utils test', () => { { childFiled: null, level: 2, - id: '四川省[&]成都市[&]办公用品[&]纸张', + id: '四川省[&]成[&]都[&]市[&]办公用品[&]纸张', + dimensions: [ + '四川省', + '成[&]都[&]市', + '办公用品', + '纸张', + ], children: new Map(), }, ], @@ -157,6 +186,7 @@ describe('pivot-data-set utils test', () => { childField: 'type', level: 2, id: '四川省[&]绵阳市', + dimensions: ['四川省', '绵阳市'], children: new Map([ [ '家具', @@ -164,6 +194,7 @@ describe('pivot-data-set utils test', () => { childField: 'subType', level: 1, id: '四川省[&]绵阳市[&]家具', + dimensions: ['四川省', '绵阳市', '家具'], children: new Map([ [ '桌子', @@ -171,7 +202,7 @@ describe('pivot-data-set utils test', () => { childFiled: null, level: 1, id: '四川省[&]绵阳市[&]家具[&]桌子', - + dimensions: ['四川省', '绵阳市', '家具', '桌子'], children: new Map(), }, ], @@ -181,6 +212,7 @@ describe('pivot-data-set utils test', () => { childFiled: null, level: 2, id: '四川省[&]绵阳市[&]家具[&]沙发', + dimensions: ['四川省', '绵阳市', '家具', '沙发'], children: new Map(), }, ], @@ -193,14 +225,15 @@ describe('pivot-data-set utils test', () => { childField: 'subType', level: 2, id: '四川省[&]绵阳市[&]办公用品', + dimensions: ['四川省', '绵阳市', '办公用品'], children: new Map([ [ '笔', { childFiled: null, level: 1, - id: '四川省[&]绵阳市[&]办公用品[&]笔', + dimensions: ['四川省', '绵阳市', '办公用品', '笔'], children: new Map(), }, ], @@ -210,6 +243,12 @@ describe('pivot-data-set utils test', () => { childFiled: null, level: 2, id: '四川省[&]绵阳市[&]办公用品[&]纸张', + dimensions: [ + '四川省', + '绵阳市', + '办公用品', + '纸张', + ], children: new Map(), }, ], @@ -228,6 +267,7 @@ describe('pivot-data-set utils test', () => { childField: 'city', level: 2, id: '浙江省', + dimensions: ['浙江省'], children: new Map([ [ '杭州市', @@ -235,6 +275,7 @@ describe('pivot-data-set utils test', () => { childField: 'type', level: 1, id: '浙江省[&]杭州市', + dimensions: ['浙江省', '杭州市'], children: new Map([ [ '家具', @@ -242,6 +283,7 @@ describe('pivot-data-set utils test', () => { childField: 'subType', level: 1, id: '浙江省[&]杭州市[&]家具', + dimensions: ['浙江省', '杭州市', '家具'], children: new Map([ [ '桌子', @@ -249,6 +291,7 @@ describe('pivot-data-set utils test', () => { childFiled: null, level: 1, id: '浙江省[&]杭州市[&]家具[&]桌子', + dimensions: ['浙江省', '杭州市', '家具', '桌子'], children: new Map(), }, ], @@ -257,7 +300,8 @@ describe('pivot-data-set utils test', () => { { childFiled: null, level: 2, - id: '浙江省[&]杭州市[&]家具[沙发]桌子', + id: '浙江省[&]杭州市[&]家具[&]沙发', + dimensions: ['浙江省', '杭州市', '家具', '沙发'], children: new Map(), }, ], @@ -270,6 +314,7 @@ describe('pivot-data-set utils test', () => { childField: 'subType', level: 2, id: '浙江省[&]杭州市[&]办公用品', + dimensions: ['浙江省', '杭州市'], children: new Map([ [ '笔', @@ -277,6 +322,7 @@ describe('pivot-data-set utils test', () => { childFiled: null, level: 1, id: '浙江省[&]杭州市[&]办公用品[&]笔', + dimensions: ['浙江省', '杭州市', '办公用品', '笔'], children: new Map(), }, ], @@ -286,6 +332,12 @@ describe('pivot-data-set utils test', () => { childFiled: null, level: 2, id: '浙江省[&]杭州市[&]办公用品[&]纸张', + dimensions: [ + '浙江省', + '杭州市', + '办公用品', + '纸张', + ], children: new Map(), }, ], @@ -301,6 +353,7 @@ describe('pivot-data-set utils test', () => { childField: 'type', level: 2, id: '浙江省[&]舟山市', + dimensions: ['浙江省', '舟山市'], children: new Map([ [ '家具', @@ -308,6 +361,7 @@ describe('pivot-data-set utils test', () => { childField: 'subType', level: 1, id: '浙江省[&]舟山市[&]家具', + dimensions: ['浙江省', '舟山市', '家具'], children: new Map([ [ '桌子', @@ -315,6 +369,7 @@ describe('pivot-data-set utils test', () => { childFiled: null, level: 1, id: '浙江省[&]舟山市[&]家具[&]桌子', + dimensions: ['浙江省', '舟山市', '家具', '桌子'], children: new Map(), }, ], @@ -324,6 +379,7 @@ describe('pivot-data-set utils test', () => { childFiled: null, level: 2, id: '浙江省[&]舟山市[&]家具[&]沙发', + dimensions: ['浙江省', '舟山市', '家具', '沙发'], children: new Map(), }, ], @@ -336,6 +392,7 @@ describe('pivot-data-set utils test', () => { childField: 'subType', level: 2, id: '浙江省[&]舟山市[&]办公用品', + dimensions: ['浙江省', '舟山市', '办公用品'], children: new Map([ [ '笔', @@ -343,6 +400,7 @@ describe('pivot-data-set utils test', () => { childFiled: null, level: 1, id: '浙江省[&]舟山市[&]办公用品[&]笔', + dimensions: ['浙江省', '舟山市', '办公用品', '笔'], children: new Map(), }, ], @@ -352,6 +410,12 @@ describe('pivot-data-set utils test', () => { childFiled: null, level: 2, id: '浙江省[&]舟山市[&]办公用品[&]纸张', + dimensions: [ + '浙江省', + '舟山市', + '办公用品', + '纸张', + ], children: new Map(), }, ], @@ -382,7 +446,7 @@ describe('pivot-data-set utils test', () => { ).toBeFalse(); expect( - existDimensionTotalGroup(['四川省', '成都市', '办公用品', '纸张']), + existDimensionTotalGroup(['四川省', '成[&]都[&]市', '办公用品', '纸张']), ).toBeFalse(); }); @@ -406,18 +470,18 @@ describe('pivot-data-set utils test', () => { fields, pivotMeta, sortedDimensionValues, - dimensionValues: ['四川省', '成都市', '办公用品', '纸张'], + dimensionValues: ['四川省', '成[&]都[&]市', '办公用品', '纸张'], }), - ).toEqual([['四川省', '成都市', '办公用品', '纸张']]); + ).toEqual([['四川省', '成[&]都[&]市', '办公用品', '纸张']]); expect( flattenDimensionValues({ fields, pivotMeta, sortedDimensionValues, - dimensionValues: ['四川省', '成都市', MULTI_VALUE, MULTI_VALUE], + dimensionValues: ['四川省', '成[&]都[&]市', MULTI_VALUE, MULTI_VALUE], }), - ).toEqual([['四川省', '成都市', MULTI_VALUE, MULTI_VALUE]]); + ).toEqual([['四川省', '成[&]都[&]市', MULTI_VALUE, MULTI_VALUE]]); }); test(`should return flatten dimension values if exist total group`, () => { @@ -426,13 +490,13 @@ describe('pivot-data-set utils test', () => { fields, pivotMeta, sortedDimensionValues, - dimensionValues: [MULTI_VALUE, '成都市', MULTI_VALUE, '纸张'], + dimensionValues: [MULTI_VALUE, '成[&]都[&]市', MULTI_VALUE, '纸张'], }), ).toMatchInlineSnapshot(` Array [ Array [ "四川省", - "成都市", + "成[&]都[&]市", "办公用品", "纸张", ], @@ -444,31 +508,36 @@ describe('pivot-data-set utils test', () => { fields, pivotMeta, sortedDimensionValues, - dimensionValues: [MULTI_VALUE, '成都市', MULTI_VALUE, MULTI_VALUE], + dimensionValues: [ + MULTI_VALUE, + '成[&]都[&]市', + MULTI_VALUE, + MULTI_VALUE, + ], }), ).toMatchInlineSnapshot(` Array [ Array [ "四川省", - "成都市", + "成[&]都[&]市", "家具", "桌子", ], Array [ "四川省", - "成都市", + "成[&]都[&]市", "家具", "沙发", ], Array [ "四川省", - "成都市", + "成[&]都[&]市", "办公用品", "笔", ], Array [ "四川省", - "成都市", + "成[&]都[&]市", "办公用品", "纸张", ], @@ -486,13 +555,13 @@ describe('pivot-data-set utils test', () => { Array [ Array [ "四川省", - "成都市", + "成[&]都[&]市", "办公用品", "笔", ], Array [ "四川省", - "成都市", + "成[&]都[&]市", "办公用品", "纸张", ], @@ -546,13 +615,13 @@ describe('pivot-data-set utils test', () => { Array [ Array [ "四川省", - "成都市", + "成[&]都[&]市", "办公用品", "笔", ], Array [ "四川省", - "成都市", + "成[&]都[&]市", "办公用品", "纸张", ],