@@ -347,7 +347,6 @@ export const replaceBlankSpace = (sql: string, fieldNames: string[]) => {
347
347
const matchedFieldName = fieldNames . find ( field => matchColumnName ( column , field ) ) ;
348
348
return matchedFieldName ?? column ;
349
349
} ) ;
350
- console . log ( columnsInSql , validColumnNames ) ;
351
350
352
351
const finalSql = columnsInSql . reduce ( ( prev , _cur , index ) => {
353
352
const originColumnName = columnsInSql [ index ] ;
@@ -360,3 +359,62 @@ export const replaceBlankSpace = (sql: string, fieldNames: string[]) => {
360
359
} , sql ) ;
361
360
return finalSql ;
362
361
} ;
362
+
363
+ /**
364
+ * sometimes skylark2 pro will return a sql statement with some measure fields not being aggregated
365
+ * this will make an empty field in dataset
366
+ * so we need to aggregate these fields.
367
+ *
368
+ */
369
+ export const sumAllMeasureFields = (
370
+ sql : string ,
371
+ fieldInfo : SimpleFieldInfo [ ] ,
372
+ columnReplaceMap : Map < string , string > ,
373
+ sqlReplaceMap : Map < string , string >
374
+ ) => {
375
+ const measureFieldsInSql = fieldInfo
376
+ . filter ( field => field . role === ROLE . MEASURE )
377
+ . map ( field => {
378
+ const { fieldName } = field ;
379
+ const replacedName1 = replaceString ( fieldName , columnReplaceMap ) ;
380
+ const replacedName2 = replaceString ( replacedName1 , sqlReplaceMap ) ;
381
+
382
+ return replacedName2 ;
383
+ } ) ;
384
+
385
+ const ast : any = alasql . parse ( sql ) ;
386
+ const nonAggregatedColumns : string [ ] = ast . statements [ 0 ] . columns
387
+ . filter ( ( column : any ) => ! column . aggregatorid )
388
+ . map ( ( column : any ) => column . columnid ) ;
389
+ const groupByColumns : string [ ] = ast . statements [ 0 ] . group . map ( ( column : any ) => column . columnid ) ;
390
+
391
+ //aggregate columns that is not in group by statement
392
+ const needAggregateColumns = nonAggregatedColumns
393
+ //filter all the measure fields
394
+ . filter ( column => measureFieldsInSql . includes ( column ) )
395
+ //filter measure fields that is not in groupby
396
+ . filter ( column => ! groupByColumns . includes ( column ) ) ;
397
+
398
+ const patchedFields = needAggregateColumns . map ( column => `SUM(\`${ column } \`) as ${ column } ` ) ;
399
+
400
+ const finalSql = needAggregateColumns . reduce ( ( prev , cur , index ) => {
401
+ const regexStr = `\`?${ cur } \`?` ;
402
+ const regex = new RegExp ( regexStr , 'g' ) ;
403
+ return prev . replace ( regex , patchedFields [ index ] ) ;
404
+ } , sql ) ;
405
+
406
+ return finalSql ;
407
+ } ;
408
+
409
+ /**
410
+ * convert group by columns to string
411
+ */
412
+ export const convertGroupByToString = ( sql : string , dataset : DataItem [ ] ) => {
413
+ const ast : any = alasql . parse ( sql ) ;
414
+ const groupByColumns : string [ ] = ast . statements [ 0 ] . group . map ( ( column : any ) => column . columnid ) ;
415
+ dataset . forEach ( item => {
416
+ groupByColumns . forEach ( column => {
417
+ item [ column ] = item [ column ] . toString ( ) ;
418
+ } ) ;
419
+ } ) ;
420
+ } ;
0 commit comments