* @param array $query * @return array */
(int $datasetId, array $query, bool $ownerOnly = false)
| 205 | * @return array<string,mixed> |
| 206 | */ |
| 207 | public function query(int $datasetId, array $query, bool $ownerOnly = false): array { |
| 208 | $resolved = $this->requireFlexible($datasetId, $ownerOnly); |
| 209 | $columns = $this->storageMapper->getColumns($datasetId); |
| 210 | $columnResolver = new FlexibleColumnResolver($columns); |
| 211 | $aggregate = (bool)($query['aggregate'] ?? false); |
| 212 | $projection = $this->resolveProjection($query, $columnResolver, $aggregate); |
| 213 | $filters = $this->resolveFilters($query['filters'] ?? [], $columnResolver); |
| 214 | $sort = $this->resolveSort($query['sort'] ?? [], $columnResolver, $projection); |
| 215 | $timeAggregation = $this->resolveTimeAggregation($query['timeAggregation'] ?? null, $columnResolver, $projection, $aggregate); |
| 216 | $topN = $this->resolveTopN($query['topN'] ?? null, $columnResolver, $projection, $aggregate); |
| 217 | $hasPostProcessing = $timeAggregation !== null || $topN !== null; |
| 218 | $limit = max(1, min(1000, (int)($query['limit'] ?? 100))); |
| 219 | $offset = max(0, (int)($query['offset'] ?? 0)); |
| 220 | $rows = $this->storageMapper->query( |
| 221 | $datasetId, |
| 222 | $projection, |
| 223 | $filters, |
| 224 | $hasPostProcessing ? [] : $sort, |
| 225 | $aggregate, |
| 226 | $hasPostProcessing ? 0 : $limit, |
| 227 | $hasPostProcessing ? 0 : $offset, |
| 228 | ); |
| 229 | |
| 230 | $data = []; |
| 231 | $recordIds = []; |
| 232 | foreach ($rows as $row) { |
| 233 | if (isset($row['record_id'])) { |
| 234 | $recordIds[] = (int)$row['record_id']; |
| 235 | } |
| 236 | $values = []; |
| 237 | foreach ($projection as $item) { |
| 238 | $column = $item['column']; |
| 239 | $values[] = $this->apiValue($column, $row[$column['ref']] ?? null, $aggregate, $item['aggregation']); |
| 240 | } |
| 241 | $data[] = $values; |
| 242 | } |
| 243 | if ($timeAggregation !== null) { |
| 244 | $data = $this->applyTimeAggregation($data, $projection, $timeAggregation); |
| 245 | } |
| 246 | if ($topN !== null) { |
| 247 | $data = $this->applyTopN($data, $projection, $topN); |
| 248 | } |
| 249 | if ($hasPostProcessing) { |
| 250 | $data = $this->sortRows($data, $projection, $sort); |
| 251 | $total = count($data); |
| 252 | $data = array_slice($data, $offset, $limit); |
| 253 | } else { |
| 254 | $total = count($data); |
| 255 | } |
| 256 | |
| 257 | $projectedColumns = array_map(static fn (array $item): array => $item['column'], $projection); |
| 258 | $dimensions = []; |
| 259 | $keyFigures = []; |
| 260 | foreach ($projectedColumns as $column) { |
| 261 | if ($column['role'] === 'dimension') { |
| 262 | $dimensions[$column['ref']] = $column['name']; |
| 263 | } else { |
| 264 | $keyFigures[] = $column['name']; |
no test coverage detected