@quillsql/react 2.12.21 → 2.12.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/dist/cjs/Chart.d.ts.map +1 -1
  2. package/dist/cjs/Chart.js +43 -18
  3. package/dist/cjs/ChartEditor.js +1 -1
  4. package/dist/cjs/Dashboard.d.ts +10 -2
  5. package/dist/cjs/Dashboard.d.ts.map +1 -1
  6. package/dist/cjs/Dashboard.js +16 -9
  7. package/dist/cjs/Table.js +2 -2
  8. package/dist/cjs/components/Chart/BarChart.d.ts.map +1 -1
  9. package/dist/cjs/components/Chart/BarChart.js +4 -2
  10. package/dist/cjs/components/Chart/ChartTooltip.d.ts.map +1 -1
  11. package/dist/cjs/components/Chart/ChartTooltip.js +1 -1
  12. package/dist/cjs/components/Chart/LineChart.d.ts.map +1 -1
  13. package/dist/cjs/components/Dashboard/DataLoader.d.ts.map +1 -1
  14. package/dist/cjs/components/Dashboard/DataLoader.js +13 -20
  15. package/dist/cjs/components/QuillTable.d.ts.map +1 -1
  16. package/dist/cjs/components/QuillTable.js +2 -2
  17. package/dist/cjs/components/ReportBuilder/convert.d.ts +2 -2
  18. package/dist/cjs/components/UiComponents.d.ts +6 -1
  19. package/dist/cjs/components/UiComponents.d.ts.map +1 -1
  20. package/dist/cjs/components/UiComponents.js +40 -2
  21. package/dist/cjs/internals/ReportBuilder/PivotList.js +5 -4
  22. package/dist/cjs/internals/ReportBuilder/PivotModal.d.ts +11 -0
  23. package/dist/cjs/internals/ReportBuilder/PivotModal.d.ts.map +1 -1
  24. package/dist/cjs/internals/ReportBuilder/PivotModal.js +8 -1
  25. package/dist/cjs/utils/dashboard.d.ts +1 -1
  26. package/dist/cjs/utils/dashboard.d.ts.map +1 -1
  27. package/dist/cjs/utils/dashboard.js +112 -4
  28. package/dist/cjs/utils/errorProcessing.d.ts.map +1 -1
  29. package/dist/cjs/utils/errorProcessing.js +0 -3
  30. package/dist/cjs/utils/merge.js +1 -1
  31. package/dist/cjs/utils/pivotProcessing.d.ts.map +1 -1
  32. package/dist/cjs/utils/pivotProcessing.js +0 -42
  33. package/dist/cjs/utils/queryConstructor.d.ts +2 -0
  34. package/dist/cjs/utils/queryConstructor.d.ts.map +1 -1
  35. package/dist/cjs/utils/queryConstructor.js +124 -1
  36. package/dist/esm/Chart.d.ts.map +1 -1
  37. package/dist/esm/Chart.js +43 -18
  38. package/dist/esm/ChartEditor.js +1 -1
  39. package/dist/esm/Dashboard.d.ts +10 -2
  40. package/dist/esm/Dashboard.d.ts.map +1 -1
  41. package/dist/esm/Dashboard.js +17 -10
  42. package/dist/esm/Table.js +2 -2
  43. package/dist/esm/components/Chart/BarChart.d.ts.map +1 -1
  44. package/dist/esm/components/Chart/BarChart.js +4 -2
  45. package/dist/esm/components/Chart/ChartTooltip.d.ts.map +1 -1
  46. package/dist/esm/components/Chart/ChartTooltip.js +1 -1
  47. package/dist/esm/components/Chart/LineChart.d.ts.map +1 -1
  48. package/dist/esm/components/Dashboard/DataLoader.d.ts.map +1 -1
  49. package/dist/esm/components/Dashboard/DataLoader.js +13 -20
  50. package/dist/esm/components/QuillTable.d.ts.map +1 -1
  51. package/dist/esm/components/QuillTable.js +2 -2
  52. package/dist/esm/components/ReportBuilder/convert.d.ts +2 -2
  53. package/dist/esm/components/UiComponents.d.ts +6 -1
  54. package/dist/esm/components/UiComponents.d.ts.map +1 -1
  55. package/dist/esm/components/UiComponents.js +15 -1
  56. package/dist/esm/internals/ReportBuilder/PivotList.js +5 -4
  57. package/dist/esm/internals/ReportBuilder/PivotModal.d.ts +11 -0
  58. package/dist/esm/internals/ReportBuilder/PivotModal.d.ts.map +1 -1
  59. package/dist/esm/internals/ReportBuilder/PivotModal.js +7 -2
  60. package/dist/esm/utils/dashboard.d.ts +1 -1
  61. package/dist/esm/utils/dashboard.d.ts.map +1 -1
  62. package/dist/esm/utils/dashboard.js +113 -5
  63. package/dist/esm/utils/errorProcessing.d.ts.map +1 -1
  64. package/dist/esm/utils/errorProcessing.js +0 -3
  65. package/dist/esm/utils/merge.js +1 -1
  66. package/dist/esm/utils/pivotProcessing.d.ts.map +1 -1
  67. package/dist/esm/utils/pivotProcessing.js +0 -42
  68. package/dist/esm/utils/queryConstructor.d.ts +2 -0
  69. package/dist/esm/utils/queryConstructor.d.ts.map +1 -1
  70. package/dist/esm/utils/queryConstructor.js +122 -0
  71. package/package.json +1 -1
@@ -1,6 +1,9 @@
1
1
  import { valueFormatter } from './valueFormatter';
2
- import { generatePivotTable, generatePivotTableYAxis, } from '../internals/ReportBuilder/PivotModal';
2
+ import { generatePivotTable, generatePivotTableYAxis, getDateRange, getDateString, } from '../internals/ReportBuilder/PivotModal';
3
3
  import { snakeAndCamelCaseToTitleCase } from './textProcessing';
4
+ import { generatePivotQuery } from './queryConstructor';
5
+ import { getData } from './dataFetcher';
6
+ import { differenceInDays } from 'date-fns';
4
7
  const defaultDashboardItem = {
5
8
  id: '',
6
9
  name: '',
@@ -28,7 +31,7 @@ const defaultDashboardItem = {
28
31
  * interface to build amazing dashboards on top of.
29
32
  * @param item
30
33
  */
31
- export function cleanDashboardItem(item, dashboardFilters) {
34
+ export async function cleanDashboardItem(item, dashboardFilters, client, dateBucket) {
32
35
  if (!item)
33
36
  return defaultDashboardItem;
34
37
  if (!item.rows)
@@ -37,7 +40,7 @@ export function cleanDashboardItem(item, dashboardFilters) {
37
40
  id: item._id,
38
41
  name: item.name,
39
42
  };
40
- const pivotTable = getPivotTable(item, dashboardFilters);
43
+ const pivotTable = await getPivotTable(item, dashboardFilters, client, dateBucket);
41
44
  const { primaryAggregation, comparisonAggregation, aggregationPercentChange, } = getAggragations(pivotTable, item);
42
45
  return {
43
46
  id: item._id,
@@ -105,12 +108,117 @@ function formatAggragation(item, value) {
105
108
  /**
106
109
  * Extract and transform the data from itemInfo.
107
110
  */
108
- function getPivotTable(report, dashboardFilters) {
111
+ async function getPivotTable(report, dashboardFilters, client, dateBucketInitial) {
109
112
  if (!report)
110
113
  return undefined;
111
- const dateFilter = Object.values(dashboardFilters ?? {}).find((filter) => filter.filterType == 'date_range');
114
+ let dateFilter = Object.values(dashboardFilters ?? {}).find((filter) => filter.filterType === 'date_range' || filter.opterator === 'BETWEEN');
115
+ if (dateFilter?.opterator === 'BETWEEN') {
116
+ dateFilter.startDate = dateFilter.value[0];
117
+ dateFilter.endDate = dateFilter.value[1];
118
+ }
112
119
  const pivot = report?.pivot;
113
120
  const data = report || {};
121
+ if (client && client.databaseType.toLowerCase() === 'postgresql') {
122
+ try {
123
+ let dateRange = undefined;
124
+ let comparisonInterval = undefined;
125
+ let dateBucket = dateBucketInitial;
126
+ if (!dateBucket && pivot.rowFieldType !== 'string' && pivot.rowField) {
127
+ let filterDateRange = undefined;
128
+ if (dateFilter) {
129
+ filterDateRange = {
130
+ start: dateFilter.startDate,
131
+ end: dateFilter.endDate,
132
+ };
133
+ }
134
+ dateRange = getDateRange(filterDateRange, pivot.rowField, data.rows);
135
+ const difference = differenceInDays(dateRange.end, dateRange.start);
136
+ if (difference < 14) {
137
+ dateBucket = 'day';
138
+ }
139
+ else if (difference < 60) {
140
+ dateBucket = 'week';
141
+ }
142
+ else if (difference < 365 * 3) {
143
+ dateBucket = 'month';
144
+ }
145
+ else {
146
+ dateBucket = 'year';
147
+ }
148
+ }
149
+ if (dateFilter && dateFilter && dateFilter.comparisonRange) {
150
+ const comparisonRange = differenceInDays(dateFilter?.comparisonRange.endDate, dateFilter?.comparisonRange.startDate);
151
+ if (!isNaN(comparisonRange)) {
152
+ if (dateBucket === 'month') {
153
+ comparisonInterval = comparisonRange / 30 + ' month';
154
+ }
155
+ else if (dateBucket === 'year') {
156
+ comparisonInterval = comparisonRange / 365 + ' year';
157
+ }
158
+ else {
159
+ comparisonInterval = comparisonRange + ' day';
160
+ }
161
+ }
162
+ }
163
+ const sqlQuery = generatePivotQuery(pivot, report.itemQuery, report.rows, dateBucket, comparisonInterval);
164
+ if (sqlQuery && report.rows.length > 0) {
165
+ const hostedBody = {
166
+ metadata: {
167
+ preQueries: [sqlQuery],
168
+ task: 'query',
169
+ orgId: client.customerId || '*',
170
+ clientId: client.publicKey,
171
+ databaseType: client?.databaseType,
172
+ getCustomFields: false,
173
+ runQueryConfig: {
174
+ overridePost: true,
175
+ convertDatatypes: true,
176
+ },
177
+ },
178
+ };
179
+ const cloudBody = { ...hostedBody };
180
+ const resp = await getData(client, 'query', 'same-origin', hostedBody, cloudBody);
181
+ // With our current design we have to remove the second row field but leave the first for comparison purposes.
182
+ const rows = resp.queryResults[0].rows;
183
+ const columns = resp.queryResults[0].fields
184
+ .map((field) => ({
185
+ field: field.name,
186
+ label: field.name.replace('comparison_', 'comparison '),
187
+ }))
188
+ .filter((field, index) => field.field !== 'comparison_' + pivot.rowField || index === 0);
189
+ if (pivot.rowFieldType !== 'string') {
190
+ rows.forEach((row) => {
191
+ row[pivot.rowField] = getDateString(row[pivot.rowField], undefined, dateBucket);
192
+ });
193
+ // add a row for each date in the range that doesn't have a value
194
+ if (pivot.rowFieldType !== 'string') {
195
+ const dateSet = new Set(rows.map((row) => row[pivot.rowField]));
196
+ // create a loop that will go through each formatted date and add a row if it doesn't exist going to the current date
197
+ for (let date = dateFilter.startDate; date <= dateFilter.endDate; date = new Date(date.getTime() + 24 * 60 * 60 * 1000)) {
198
+ const formattedDate = getDateString(date.toDateString(), undefined, dateBucket);
199
+ if (!dateSet.has(formattedDate)) {
200
+ const newRow = {};
201
+ newRow[pivot.rowField] = formattedDate;
202
+ rows.push(newRow);
203
+ dateSet.add(formattedDate);
204
+ }
205
+ // order the rows by the date field
206
+ rows.sort((a, b) => {
207
+ return new Date(a[pivot.rowField]) < new Date(b[pivot.rowField])
208
+ ? -1
209
+ : 1;
210
+ });
211
+ }
212
+ }
213
+ }
214
+ return {
215
+ rows: rows,
216
+ columns: columns,
217
+ };
218
+ }
219
+ }
220
+ catch (e) { }
221
+ }
114
222
  return pivot && data.rows
115
223
  ? generatePivotTable(pivot, JSON.parse(JSON.stringify(data.rows)), // deep copy
116
224
  dateFilter?.startDate && dateFilter?.endDate
@@ -1 +1 @@
1
- {"version":3,"file":"errorProcessing.d.ts","sourceRoot":"","sources":["../../../src/utils/errorProcessing.ts"],"names":[],"mappings":"AAAA,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,GAAG,QAO/C"}
1
+ {"version":3,"file":"errorProcessing.d.ts","sourceRoot":"","sources":["../../../src/utils/errorProcessing.ts"],"names":[],"mappings":"AAAA,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,GAAG,QAI/C"}
@@ -2,7 +2,4 @@ export function processFilterErrorList(resp) {
2
2
  if (!resp || !resp.filterErrorList || !Array.isArray(resp.filterErrorList)) {
3
3
  return;
4
4
  }
5
- resp.filterErrorList.forEach((filterError) => {
6
- console.error(filterError);
7
- });
8
5
  }
@@ -1,5 +1,5 @@
1
1
  export function mergeComparisonRange(resp) {
2
- if (resp.chartType === "table")
2
+ if (resp.chartType === 'table')
3
3
  return;
4
4
  mergeForwards(resp);
5
5
  // mergeBackwards(resp); // uncomment to switch back and forth
@@ -1 +1 @@
1
- {"version":3,"file":"pivotProcessing.d.ts","sourceRoot":"","sources":["../../../src/utils/pivotProcessing.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,EAAE,MAAM,uCAAuC,CAAC;AAC9D,OAAO,EAAU,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAG3D,wBAAgB,UAAU,CACxB,KAAK,EAAE,KAAK,EACZ,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,cAAc,EAAE,GACxB,MAAM,CAsCR;AAmED,wBAAgB,YAAY,CAAC,KAAK,EAAE,KAAK,WAgBxC;AAED,wBAAgB,4BAA4B,CAC1C,OAAO,EAAE,cAAc,EAAE,EACzB,YAAY,EAAE;IAAE,CAAC,KAAK,EAAE,MAAM,GAAG,GAAG,CAAA;CAAE;;;;EAsCvC;AAED,wBAAgB,UAAU,CACxB,KAAK,EAAE,KAAK,EACZ,eAAe,EAAE;IACf,SAAS,EAAE,GAAG,CAAC;IACf,YAAY,EAAE,GAAG,CAAC;IAClB,WAAW,EAAE,GAAG,CAAC;CAClB,GACA,KAAK,CAqCP"}
1
+ {"version":3,"file":"pivotProcessing.d.ts","sourceRoot":"","sources":["../../../src/utils/pivotProcessing.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,EAAE,MAAM,uCAAuC,CAAC;AAC9D,OAAO,EAAU,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAG3D,wBAAgB,UAAU,CACxB,KAAK,EAAE,KAAK,EACZ,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,cAAc,EAAE,GACxB,MAAM,CAsCR;AAkBD,wBAAgB,YAAY,CAAC,KAAK,EAAE,KAAK,WAgBxC;AAED,wBAAgB,4BAA4B,CAC1C,OAAO,EAAE,cAAc,EAAE,EACzB,YAAY,EAAE;IAAE,CAAC,KAAK,EAAE,MAAM,GAAG,GAAG,CAAA;CAAE;;;;EAsCvC;AAED,wBAAgB,UAAU,CACxB,KAAK,EAAE,KAAK,EACZ,eAAe,EAAE;IACf,SAAS,EAAE,GAAG,CAAC;IACf,YAAY,EAAE,GAAG,CAAC;IAClB,WAAW,EAAE,GAAG,CAAC;CAClB,GACA,KAAK,CAqCP"}
@@ -30,48 +30,6 @@ export function pivotToSql(pivot, query, columns) {
30
30
  }
31
31
  return query;
32
32
  }
33
- function createPivotSqlQuery(rowField, columnField, valueField, aggregationType, query) {
34
- // const sqlAggregationType = convertToSqlAggregationType(aggregationType);
35
- return `with query_table as (${query}),
36
- distinct_column_fields as (
37
- select distinct
38
- ${columnField}
39
- from
40
- query_table
41
- ),
42
- case_when_query as (
43
- select
44
- string_agg(
45
- concat(
46
- '${aggregationType}(CASE WHEN ${columnField} = ''',
47
- replace(${columnField}, '''', ''''''),
48
- ''' THEN ${valueField} ELSE 0 END) AS ',
49
- concat(concat('"', replace(${columnField}, ' ', '_')), '"')
50
- ),
51
- ',
52
- '
53
- ) as sql_part
54
- from
55
- distinct_column_fields
56
- ),
57
- joined_queries as (
58
- select
59
- concat(
60
- 'WITH query_table as (${query}) SELECT ${rowField}, ',
61
- sql_part,
62
- '
63
- FROM query_table
64
- GROUP BY ${rowField}
65
- ORDER BY ${rowField}'
66
- ) as query
67
- from
68
- case_when_query
69
- )
70
- select
71
- query
72
- from
73
- joined_queries;`;
74
- }
75
33
  function generateTwoDPivotQuery(pivot, query, columns) {
76
34
  const caseWhens = columns.map((column) => {
77
35
  return `${pivot.aggregationType}(
@@ -1,2 +1,4 @@
1
+ import { Pivot } from '../internals/ReportBuilder/PivotModal';
1
2
  export declare function generateDistinctQuery(stringFields: string[], query: string): string;
3
+ export declare function generatePivotQuery(pivot: Pivot, itemQueries: string[], rows: any[], dateBucket?: string, comparisonInterval?: string): string | undefined;
2
4
  //# sourceMappingURL=queryConstructor.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"queryConstructor.d.ts","sourceRoot":"","sources":["../../../src/utils/queryConstructor.ts"],"names":[],"mappings":"AAAA,wBAAgB,qBAAqB,CAAC,YAAY,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,MAAM,UAQ1E"}
1
+ {"version":3,"file":"queryConstructor.d.ts","sourceRoot":"","sources":["../../../src/utils/queryConstructor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,uCAAuC,CAAC;AAe9D,wBAAgB,qBAAqB,CAAC,YAAY,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,MAAM,UAQ1E;AAED,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,KAAK,EACZ,WAAW,EAAE,MAAM,EAAE,EACrB,IAAI,EAAE,GAAG,EAAE,EACX,UAAU,CAAC,EAAE,MAAM,EACnB,kBAAkB,CAAC,EAAE,MAAM,GAC1B,MAAM,GAAG,SAAS,CAiBpB"}
@@ -1,3 +1,16 @@
1
+ import { isValidPivot } from './pivotProcessing';
2
+ function processAggType(aggType) {
3
+ if (aggType === 'count')
4
+ return 'SUM';
5
+ return aggType?.toLowerCase() === 'average' ? 'AVG' : aggType?.toLowerCase();
6
+ }
7
+ function processValueField(aggType, valueField) {
8
+ if (aggType === 'min' || aggType === 'max')
9
+ return `"${valueField}" ELSE null`;
10
+ if (aggType === 'count')
11
+ return `1 ELSE 0`;
12
+ return valueField ? `"${valueField}" ELSE 0` : `1 ELSE 0`;
13
+ }
1
14
  export function generateDistinctQuery(stringFields, query) {
2
15
  const distinctQueries = stringFields.map((field) => {
3
16
  return `SELECT '${field}' AS field, ARRAY_AGG(DISTINCT ${field}) AS string_values FROM querytable`;
@@ -5,3 +18,112 @@ export function generateDistinctQuery(stringFields, query) {
5
18
  const distinctQuery = distinctQueries.join(' UNION ALL ');
6
19
  return `WITH querytable AS (${query.replace(';', '')}) ` + distinctQuery;
7
20
  }
21
+ export function generatePivotQuery(pivot, itemQueries, rows, dateBucket, comparisonInterval) {
22
+ if (!isValidPivot(pivot)) {
23
+ return undefined;
24
+ }
25
+ if (!pivot.rowField) {
26
+ return undefined;
27
+ }
28
+ if (pivot.columnField) {
29
+ return create2DPivotQuery(pivot, itemQueries, rows, dateBucket, comparisonInterval);
30
+ }
31
+ return create1DPivotQuery(pivot, itemQueries, dateBucket, comparisonInterval);
32
+ }
33
+ function create2DPivotQuery(pivot, itemQueries, rows, dateBucket, comparisonInterval) {
34
+ if (!pivot || !pivot.columnField) {
35
+ return undefined;
36
+ }
37
+ // @ts-ignore
38
+ const columnValues = rows.map((row) => row[pivot.columnField]);
39
+ const distinctColumnValues = Array.from(new Set(columnValues)).filter((value) => value !== undefined && value !== null);
40
+ if (distinctColumnValues.length === 0) {
41
+ return undefined;
42
+ }
43
+ if (pivot.rowFieldType === 'string') {
44
+ return create2DStringPivotQuery(pivot, itemQueries, distinctColumnValues);
45
+ }
46
+ return create2DDatePivotQuery(pivot, itemQueries, distinctColumnValues, dateBucket, comparisonInterval);
47
+ }
48
+ function create2DStringPivotQuery(pivot, itemQueries, distinctColumnValues) {
49
+ const caseWhens = distinctColumnValues.map((column) => {
50
+ return `${processAggType(pivot.aggregationType)}(CASE WHEN "${pivot.columnField}" = '${column.replaceAll("'", "''")}' THEN ${processValueField(pivot.aggregationType, pivot.valueField)} END) AS "${column}"`;
51
+ });
52
+ const caseWhensCompare = distinctColumnValues.map((column) => {
53
+ return `${processAggType(pivot.aggregationType)}(CASE WHEN "${pivot.columnField}" = '${column.replaceAll("'", "''")}' THEN ${processValueField(pivot.aggregationType, pivot.valueField)} END) AS "comparison_${column}"`;
54
+ });
55
+ const compareCTE = itemQueries[1]
56
+ ? `, quill_ct as (${itemQueries[1]?.replaceAll(';', '')})`
57
+ : '';
58
+ const compareCaseWhenCTE = itemQueries[1]
59
+ ? `, quill_ct_cw as (SELECT "${pivot.rowField}" as "comparison_${pivot.rowField}", ${caseWhensCompare.join(', ')} FROM quill_ct GROUP BY ${pivot.rowField})`
60
+ : '';
61
+ const joinQuery = itemQueries[1]
62
+ ? `JOIN quill_ct_cw ct ON ct."comparison_${pivot.rowField}" = qt."${pivot.rowField}"`
63
+ : '';
64
+ return `WITH quill_qt AS (${itemQueries[0]?.replaceAll(';', '')}),
65
+ quill_qt_cw AS (SELECT ${pivot.rowField}, ${caseWhens.join(', ')} FROM quill_qt GROUP BY ${pivot.rowField})
66
+ ${compareCTE} ${compareCaseWhenCTE}
67
+ SELECT * FROM quill_qt_cw qt
68
+ ${joinQuery}`;
69
+ }
70
+ function create2DDatePivotQuery(pivot, itemQueries, distinctColumnValues, dateBucket = 'month', comparisonInterval) {
71
+ const caseWhens = distinctColumnValues.map((column) => {
72
+ return `${processAggType(pivot.aggregationType)}(CASE WHEN qt."${pivot.columnField}" = '${column.replaceAll("'", "''")}' THEN ${processValueField(pivot.aggregationType, pivot.valueField)} END) AS "${column}"`;
73
+ });
74
+ const caseWhensCompare = distinctColumnValues.map((column) => {
75
+ return `${processAggType(pivot.aggregationType)}(CASE WHEN "${pivot.columnField}" = '${column.replaceAll("'", "''")}' THEN ${processValueField(pivot.aggregationType, pivot.valueField)} END) AS "comparison_${column}"`;
76
+ });
77
+ const compareCTE = itemQueries[1] && comparisonInterval
78
+ ? `, quill_ct as (${itemQueries[1]?.replaceAll(';', '')})`
79
+ : '';
80
+ const compareCaseWhenCTE = itemQueries[1] && comparisonInterval
81
+ ? `, quill_ct_cw as (SELECT date_trunc('${dateBucket}', ${pivot.rowField}) as comparison_${pivot.rowField}, ${caseWhensCompare.join(', ')} FROM quill_ct GROUP BY date_trunc('${dateBucket}', ${pivot.rowField}))`
82
+ : '';
83
+ const joinQuery = itemQueries[1] && comparisonInterval
84
+ ? `LEFT JOIN quill_ct_cw ct ON qt."${pivot.rowField}" = date_trunc('${dateBucket}', (ct."comparison_${pivot.rowField}" + INTERVAL '${comparisonInterval} day'))`
85
+ : '';
86
+ return `WITH quill_query_table AS (${itemQueries[0]?.replaceAll(';', '')}),
87
+ quill_qt_agg as (SELECT date_trunc('${dateBucket}', ${pivot.rowField}) as ${pivot.rowField}, ${caseWhens.join(', ')} FROM quill_query_table qt GROUP BY date_trunc('${dateBucket}', ${pivot.rowField}))
88
+ ${compareCTE} ${compareCaseWhenCTE}
89
+ SELECT * FROM quill_qt_agg qt
90
+ ${joinQuery} ORDER BY qt."${pivot.rowField}"`;
91
+ }
92
+ function create1DPivotQuery(pivot, itemQueries, dateBucket = 'month', comparisonInterval) {
93
+ if (pivot.rowFieldType === 'string') {
94
+ return create1DStringPivotQuery(pivot, itemQueries);
95
+ }
96
+ return create1DDatePivotQuery(pivot, itemQueries, dateBucket, comparisonInterval);
97
+ }
98
+ function create1DStringPivotQuery(pivot, itemQueries) {
99
+ const compareCTE = itemQueries[1]
100
+ ? `, quill_ct as (${itemQueries[1]?.replaceAll(';', '')})`
101
+ : '';
102
+ const compareCaseWhenCTE = itemQueries[1]
103
+ ? `, quill_ct_cw as (SELECT ${pivot.rowField} as "comparison_${pivot.rowField}", ${processAggType(pivot.aggregationType)}(${pivot.valueField}) as comparison_${pivot.valueField} FROM quill_ct GROUP BY ${pivot.rowField})`
104
+ : '';
105
+ const joinQuery = itemQueries[1]
106
+ ? `JOIN quill_ct_cw ct ON ct."comparison_${pivot.rowField}" = qt."${pivot.rowField}"`
107
+ : '';
108
+ return `WITH quill_qt AS (${itemQueries[0]?.replaceAll(';', '')}),
109
+ quill_qt_cw AS (SELECT ${pivot.rowField}, ${processAggType(pivot.aggregationType)}(${pivot.valueField}) as ${pivot.valueField} FROM quill_qt GROUP BY ${pivot.rowField})
110
+ ${compareCTE} ${compareCaseWhenCTE}
111
+ SELECT * FROM quill_qt_cw qt
112
+ ${joinQuery}`;
113
+ }
114
+ function create1DDatePivotQuery(pivot, itemQueries, dateBucket = 'month', comparisonInterval) {
115
+ const compareCTE = itemQueries[1] && comparisonInterval
116
+ ? `, quill_ct as (${itemQueries[1]?.replaceAll(';', '')})`
117
+ : '';
118
+ const compareCaseWhenCTE = itemQueries[1] && comparisonInterval
119
+ ? `, quill_ct_cw as (SELECT date_trunc('${dateBucket}', ${pivot.rowField}) as comparison_${pivot.rowField}, ${processAggType(pivot.aggregationType)}(${pivot.valueField}) as comparison_${pivot.valueField} FROM quill_ct GROUP BY date_trunc('${dateBucket}', ${pivot.rowField}))`
120
+ : '';
121
+ const joinQuery = itemQueries[1] && comparisonInterval
122
+ ? `LEFT JOIN quill_ct_cw ct ON qt."${pivot.rowField}" = date_trunc('${dateBucket}', (ct."comparison_${pivot.rowField}" + INTERVAL '${comparisonInterval}'))`
123
+ : '';
124
+ return `WITH quill_qt AS (${itemQueries[0]?.replaceAll(';', '')}),
125
+ quill_qt_agg AS (SELECT date_trunc('${dateBucket}', ${pivot.rowField}) as ${pivot.rowField}, ${processAggType(pivot.aggregationType)}(${pivot.valueField}) as ${pivot.valueField} FROM quill_qt GROUP BY date_trunc('${dateBucket}', ${pivot.rowField}))
126
+ ${compareCTE} ${compareCaseWhenCTE}
127
+ SELECT * FROM quill_qt_agg qt
128
+ ${joinQuery} ORDER BY qt."${pivot.rowField}"`;
129
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quillsql/react",
3
- "version": "2.12.21",
3
+ "version": "2.12.23",
4
4
  "exports": {
5
5
  ".": {
6
6
  "import": "./dist/esm/index.js",