( nodes: QueryNode[], aliases: Record<string, string> )
| 89 | * Collects selected columns with their expressions and ordering |
| 90 | */ |
| 91 | export function collectSelectedColumns( |
| 92 | nodes: QueryNode[], |
| 93 | aliases: Record<string, string> |
| 94 | ): { columns: SelectedColumn[]; hasAggregation: boolean; nonAggregatedCols: string[] } { |
| 95 | const selectedColsWithOrder: SelectedColumn[] = []; |
| 96 | const nonAggregatedCols: string[] = []; |
| 97 | let hasAggregation = false; |
| 98 | |
| 99 | nodes.forEach((node) => { |
| 100 | const data = node.data; |
| 101 | const alias = aliases[node.id]; |
| 102 | |
| 103 | if (data.selectedColumns) { |
| 104 | Object.entries(data.selectedColumns).forEach(([col, isChecked]) => { |
| 105 | if (isChecked) { |
| 106 | const agg = data.columnAggregations?.[col]; |
| 107 | const colAlias = data.columnAliases?.[col]; |
| 108 | let colExpr = `${alias}.${col}`; |
| 109 | let order = 999; |
| 110 | |
| 111 | if (agg?.function) { |
| 112 | hasAggregation = true; |
| 113 | if (agg.function === 'COUNT_DISTINCT') { |
| 114 | colExpr = `COUNT(DISTINCT ${alias}.${col})`; |
| 115 | } else { |
| 116 | colExpr = `${agg.function}(${alias}.${col})`; |
| 117 | } |
| 118 | |
| 119 | if (agg?.alias) { |
| 120 | colExpr += ` AS ${agg.alias}`; |
| 121 | } |
| 122 | |
| 123 | if (agg?.order !== undefined) { |
| 124 | order = agg.order; |
| 125 | } |
| 126 | } else { |
| 127 | nonAggregatedCols.push(`${alias}.${col}`); |
| 128 | |
| 129 | if (colAlias?.alias) { |
| 130 | colExpr += ` AS ${colAlias.alias}`; |
| 131 | } |
| 132 | |
| 133 | if (colAlias?.order !== undefined) { |
| 134 | order = colAlias.order; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | selectedColsWithOrder.push({ expr: colExpr, order, colName: col }); |
| 139 | } |
| 140 | }); |
| 141 | } |
| 142 | }); |
| 143 | |
| 144 | return { columns: selectedColsWithOrder, hasAggregation, nonAggregatedCols }; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Sorts columns by their order property |
no outgoing calls
no test coverage detected