All files server/query-builder.ts

97.72% Statements 172/176
94.96% Branches 151/159
92.59% Functions 25/27
98.25% Lines 169/172

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599                                                                                  710x 710x 710x 710x                         4011x                         3368x     3368x     3368x 1303x 1487x 1487x 1487x 1479x 1479x   1479x           3368x 3344x             3344x 4405x 4405x 4353x 4353x           3368x 516x 544x 544x 544x 544x 544x           544x           3368x 20x     3368x                                                       8x                           108x                       128x                       748x                             4191x     4191x     4191x     4191x 1952x       2081x 1507x 1507x   1507x     112x 112x 112x 112x 1395x   12x       1957x 1957x 1458x           4191x 604x 648x 648x 648x     344x 80x 80x 36x         308x 308x 308x 308x 292x 292x       16x     16x 16x 16x             4191x                         3320x     3320x     3320x 1448x 1513x 1513x 108x         3320x                                   7758x 2044x   2044x 1210x 2476x 2476x 1210x     834x 834x 1812x 1812x 834x         5714x 5714x 5714x 5714x     5466x 5466x 5466x 7758x     5466x         2731x 404x 404x 108x             2623x           2623x 2502x 2502x 2502x 2359x     264x       2731x             2735x     172x 2563x       108x 108x                   2455x                           2387x                     372x                         3332x             3340x     3340x     540x       3340x 399x   431x 8x       423x 431x         3332x 512x     512x 28x     512x 540x   428x         3332x               3192x     3192x     3192x 3180x       3192x 1163x 1351x 1351x 1351x 1351x 1351x   522x           3192x                 3312x 3312x 4x     3312x   3312x 317x 8x   309x     3304x 75x 8x   67x     3296x                           2015x                              
/**
 * Shared Query Builder
 * Contains all SQL building logic that was previously duplicated between executor and multi-cube-builder
 * Single source of truth for all SQL generation
 */
 
import {
  sql,
  and,
  or,
  count,
  asc,
  desc,
  SQL,
  type AnyColumn
} from 'drizzle-orm'
 
import type {
  SemanticQuery,
  FilterOperator,
  Filter,
  FilterCondition,
  LogicalFilter,
  Cube,
  QueryContext,
  QueryPlan
} from './types'
 
import { resolveSqlExpression } from './cube-utils'
import type { DatabaseAdapter } from './adapters/base-adapter'
import { getFilterKey, getTimeDimensionFilterKey } from './filter-cache'
import { DateTimeBuilder, FilterBuilder, GroupByBuilder, MeasureBuilder } from './builders'
import type { ResolvedMeasures } from './template-substitution'
 
export class QueryBuilder {
  private dateTimeBuilder: DateTimeBuilder
  private filterBuilder: FilterBuilder
  private groupByBuilder: GroupByBuilder
  private measureBuilder: MeasureBuilder
 
  constructor(databaseAdapter: DatabaseAdapter) {
    this.dateTimeBuilder = new DateTimeBuilder(databaseAdapter)
    this.filterBuilder = new FilterBuilder(databaseAdapter, this.dateTimeBuilder)
    this.groupByBuilder = new GroupByBuilder(this.dateTimeBuilder)
    this.measureBuilder = new MeasureBuilder(databaseAdapter)
  }
 
  /**
   * Build resolvedMeasures map for a set of measures
   * Delegates to MeasureBuilder
   */
  buildResolvedMeasures(
    measureNames: string[],
    cubeMap: Map<string, Cube>,
    context: QueryContext,
    customMeasureBuilder?: (measureName: string, measure: any, cube: Cube) => SQL
  ): ResolvedMeasures {
    return this.measureBuilder.buildResolvedMeasures(measureNames, cubeMap, context, customMeasureBuilder)
  }
 
  /**
   * Build dynamic selections for measures, dimensions, and time dimensions
   * Works for both single and multi-cube queries
   * Handles calculated measures with dependency resolution
   */
  buildSelections(
    cubes: Map<string, Cube> | Cube,
    query: SemanticQuery,
    context: QueryContext
  ): Record<string, SQL | AnyColumn> {
    const selections: Record<string, SQL | AnyColumn> = {}
 
    // Convert single cube to map for consistent handling
    const cubeMap = cubes instanceof Map ? cubes : new Map([[cubes.name, cubes]])
 
    // Add dimensions
    if (query.dimensions) {
      for (const dimensionName of query.dimensions) {
        const [cubeName, fieldName] = dimensionName.split('.')
        const cube = cubeMap.get(cubeName)
        if (cube && cube.dimensions && cube.dimensions[fieldName]) {
          const dimension = cube.dimensions[fieldName]
          const sqlExpr = resolveSqlExpression(dimension.sql, context)
          // Use explicit alias for dimension expressions so they can be referenced in ORDER BY
          selections[dimensionName] = sql`${sqlExpr}`.as(dimensionName) as unknown as SQL
        }
      }
    }
 
    // Add measures with aggregations using the centralized helper
    if (query.measures) {
      const resolvedMeasures = this.buildResolvedMeasures(
        query.measures,
        cubeMap,
        context
      )
 
      // Add user-requested measures to selections
      for (const measureName of query.measures) {
        const measureBuilder = resolvedMeasures.get(measureName)
        if (measureBuilder) {
          const measureExpr = measureBuilder()
          selections[measureName] = sql`${measureExpr}`.as(measureName) as unknown as SQL
        }
      }
    }
 
    // Add time dimensions with granularity
    if (query.timeDimensions) {
      for (const timeDim of query.timeDimensions) {
        const [cubeName, fieldName] = timeDim.dimension.split('.')
        const cube = cubeMap.get(cubeName)
        Eif (cube && cube.dimensions && cube.dimensions[fieldName]) {
          const dimension = cube.dimensions[fieldName]
          const timeExpr = this.buildTimeDimensionExpression(
            dimension.sql,
            timeDim.granularity,
            context
          )
          // Use explicit alias for time dimension expressions so they can be referenced in ORDER BY
          selections[timeDim.dimension] = sql`${timeExpr}`.as(timeDim.dimension) as unknown as SQL
        }
      }
    }
 
    // Default to COUNT(*) if no selections
    if (Object.keys(selections).length === 0) {
      selections.count = count()
    }
 
    return selections
  }
 
  /**
   * Build calculated measure expression by substituting {member} references
   * Delegates to MeasureBuilder
   */
  public buildCalculatedMeasure(
    measure: any,
    cube: Cube,
    allCubes: Map<string, Cube>,
    resolvedMeasures: ResolvedMeasures,
    context: QueryContext
  ): SQL {
    return this.measureBuilder.buildCalculatedMeasure(measure, cube, allCubes, resolvedMeasures, context)
  }
 
  /**
   * Build resolved measures map for a calculated measure from CTE columns
   * Delegates to MeasureBuilder
   */
  public buildCTECalculatedMeasure(
    measure: any,
    cube: Cube,
    cteInfo: { cteAlias: string; measures: string[]; cube: Cube },
    allCubes: Map<string, Cube>,
    context: QueryContext
  ): SQL {
    return this.measureBuilder.buildCTECalculatedMeasure(measure, cube, cteInfo, allCubes, context)
  }
 
  /**
   * Build measure expression for HAVING clause, handling CTE references correctly
   * Delegates to MeasureBuilder
   */
  private buildHavingMeasureExpression(
    cubeName: string,
    fieldKey: string,
    measure: any,
    context: QueryContext,
    queryPlan?: QueryPlan
  ): SQL {
    return this.measureBuilder.buildHavingMeasureExpression(cubeName, fieldKey, measure, context, queryPlan)
  }
 
  /**
   * Build measure expression with aggregation and filters
   * Delegates to MeasureBuilder
   */
  buildMeasureExpression(
    measure: any,
    context: QueryContext,
    cube?: Cube
  ): SQL {
    return this.measureBuilder.buildMeasureExpression(measure, context, cube)
  }
 
  /**
   * Build time dimension expression with granularity using database adapter
   * Delegates to DateTimeBuilder
   */
  buildTimeDimensionExpression(
    dimensionSql: any,
    granularity: string | undefined,
    context: QueryContext
  ): SQL {
    return this.dateTimeBuilder.buildTimeDimensionExpression(dimensionSql, granularity, context)
  }
 
  /**
   * Build WHERE conditions from semantic query filters (dimensions only)
   * Works for both single and multi-cube queries
   * @param preBuiltFilters - Optional map of cube name to pre-built filter SQL for parameter deduplication
   */
  buildWhereConditions(
    cubes: Map<string, Cube> | Cube,
    query: SemanticQuery,
    context: QueryContext,
    queryPlan?: QueryPlan,
    preBuiltFilters?: Map<string, SQL[]>
  ): SQL[] {
    const conditions: SQL[] = []
 
    // Convert single cube to map for consistent handling
    const cubeMap = cubes instanceof Map ? cubes : new Map([[cubes.name, cubes]])
 
    // Track which cubes have had their pre-built filters added
    const cubesWithPreBuiltFiltersAdded = new Set<string>()
 
    // Process regular filters (dimensions only for WHERE clause)
    if (query.filters && query.filters.length > 0) {
      for (const filter of query.filters) {
        // Check if this filter's cube has pre-built filters we should use instead
        // IMPORTANT: Only use pre-built filters if the filter's cube is in our current cubeMap
        // This prevents using Employees filters in a Productivity CTE context
        if (preBuiltFilters && 'member' in filter) {
          const [cubeName] = (filter.member as string).split('.')
          const cubeIsInContext = cubeMap.has(cubeName)
 
          if (cubeIsInContext && preBuiltFilters.has(cubeName) && !cubesWithPreBuiltFiltersAdded.has(cubeName)) {
            // Use the pre-built filter SQL instead of building fresh
            // This deduplicates parameters between CTE subquery and main query
            const preBuilt = preBuiltFilters.get(cubeName)!
            conditions.push(...preBuilt)
            cubesWithPreBuiltFiltersAdded.add(cubeName)
            continue
          } else if (cubesWithPreBuiltFiltersAdded.has(cubeName)) {
            // Skip - already added pre-built filters for this cube
            continue
          }
        }
 
        const condition = this.processFilter(filter, cubeMap, context, 'where', queryPlan)
        if (condition) {
          conditions.push(condition)
        }
      }
    }    
 
    // Process time dimension date range filters
    if (query.timeDimensions) {
      for (const timeDim of query.timeDimensions) {
        const [cubeName, fieldName] = timeDim.dimension.split('.')
        const cube = cubeMap.get(cubeName)
        if (cube && cube.dimensions[fieldName] && timeDim.dateRange) {
          // Check if this cube is in a pre-aggregation CTE - if so, skip this filter in WHERE clause
          // The time dimension filter will be applied within the CTE itself during pre-aggregation
          if (queryPlan?.preAggregationCTEs) {
            const isInCTE = queryPlan.preAggregationCTEs.some((cte: any) => cte.cube.name === cubeName)
            if (isInCTE) {
              continue // Skip this filter - it's handled in the CTE
            }
          }
 
          // Try to use cached time dimension filter for parameter deduplication
          Eif (context.filterCache) {
            const key = getTimeDimensionFilterKey(timeDim.dimension, timeDim.dateRange)
            const cached = context.filterCache.get(key)
            if (cached) {
              conditions.push(cached)
              continue
            }
          }
 
          const dimension = cube.dimensions[fieldName]
          // Use the raw field expression for date filtering (not the truncated version)
          // This ensures we filter on the actual timestamp values before aggregation
          const fieldExpr = resolveSqlExpression(dimension.sql, context)
          const dateCondition = this.buildDateRangeCondition(fieldExpr, timeDim.dateRange)
          Iif (dateCondition) {
            conditions.push(dateCondition)
          }
        }
      }
    }    
    
    return conditions
  }
 
  /**
   * Build HAVING conditions from semantic query filters (measures only)
   * Works for both single and multi-cube queries
   */
  buildHavingConditions(
    cubes: Map<string, Cube> | Cube, 
    query: SemanticQuery, 
    context: QueryContext,
    queryPlan?: QueryPlan
  ): SQL[] {
    const conditions: SQL[] = []
    
    // Convert single cube to map for consistent handling
    const cubeMap = cubes instanceof Map ? cubes : new Map([[cubes.name, cubes]])
    
    // Process regular filters (measures only for HAVING clause)
    if (query.filters && query.filters.length > 0) {
      for (const filter of query.filters) {
        const condition = this.processFilter(filter, cubeMap, context, 'having', queryPlan)
        if (condition) {
          conditions.push(condition)
        }
      }
    }    
    
    return conditions
  }
 
  /**
   * Process a single filter (basic or logical)
   * @param filterType - 'where' for dimension filters, 'having' for measure filters
   */
  private processFilter(
    filter: Filter,
    cubes: Map<string, Cube>,
    context: QueryContext,
    filterType: 'where' | 'having',
    queryPlan?: QueryPlan
  ): SQL | null {
    // Handle logical filters (AND/OR)
    // NOTE: We do NOT cache logical filters because they can contain mixed cube references.
    // When some cubes are in CTEs, the cached version would reference wrong table contexts.
    // Individual simple filters within logical filters are still cached for deduplication.
    if ('and' in filter || 'or' in filter) {
      const logicalFilter = filter as LogicalFilter
 
      if (logicalFilter.and) {
        const conditions = logicalFilter.and
          .map(f => this.processFilter(f, cubes, context, filterType, queryPlan))
          .filter((condition): condition is SQL => condition !== null)
        return conditions.length > 0 ? and(...conditions) as SQL : null
      }
 
      Eif (logicalFilter.or) {
        const conditions = logicalFilter.or
          .map(f => this.processFilter(f, cubes, context, filterType, queryPlan))
          .filter((condition): condition is SQL => condition !== null)
        return conditions.length > 0 ? or(...conditions) as SQL : null
      }
    }
 
    // Handle simple filter condition
    const filterCondition = filter as FilterCondition
    const [cubeName, fieldKey] = filterCondition.member.split('.')
    const cube = cubes.get(cubeName)
    if (!cube) return null
 
    // Find the field in dimensions or measures
    const dimension = cube.dimensions[fieldKey]
    const measure = cube.measures[fieldKey]
    const field = dimension || measure
    Iif (!field) return null
 
    // Apply filter based on type and what we're looking for
    if (filterType === 'where' && dimension) {
      // Check if this cube is in a pre-aggregation CTE - if so, skip this filter in WHERE clause
      // The filter will be applied within the CTE itself
      // IMPORTANT: This check MUST come before the cache check, because cached filters
      // would reference the wrong table context (CTE table vs main query table)
      if (queryPlan?.preAggregationCTEs) {
        const isInCTE = queryPlan.preAggregationCTEs.some((cte: any) => cte.cube.name === cubeName)
        if (isInCTE) {
          return null // Skip this filter - it's handled in the CTE
        }
      }
 
      // For array operators, we need the raw column (not isolated SQL)
      // because Drizzle's array functions need column type metadata for proper encoding
      // These CANNOT be cached because the raw column reference is required
      const isArrayOperator = ['arrayContains', 'arrayOverlaps', 'arrayContained'].includes(filterCondition.operator)
 
      // Try to use cached filter SQL for parameter deduplication
      // This avoids creating duplicate parameters for the same filter values
      // NOTE: Skip cache for array operators (they require raw column)
      // NOTE: We only use cache for non-CTE cubes (checked above)
      if (!isArrayOperator && context.filterCache) {
        const key = getFilterKey(filter)
        const cached = context.filterCache.get(key)
        if (cached) {
          return cached
        }
      }
      const fieldExpr = isArrayOperator
        ? (typeof dimension.sql === 'function' ? dimension.sql(context) : dimension.sql)
        : resolveSqlExpression(dimension.sql, context)
 
      return this.buildFilterCondition(
        fieldExpr,
        filterCondition.operator,
        filterCondition.values,
        field,
        filterCondition.dateRange
      )
    } else if (filterType === 'where' && measure) {
      // NEVER apply measure filters in WHERE clause - they should only be in HAVING
      // This prevents incorrect behavior where measure filters are applied before aggregation
      return null
    } else if (filterType === 'having' && measure) {
      // HAVING clause: use aggregated measure expression
      // Note: HAVING filters are NOT cached because they use aggregated expressions
      // which may be different depending on the query context (CTE vs main query)
      const measureExpr = this.buildHavingMeasureExpression(cubeName, fieldKey, measure, context, queryPlan)
      return this.buildFilterCondition(
        measureExpr,
        filterCondition.operator,
        filterCondition.values,
        field,
        filterCondition.dateRange
      )
    }
 
    // Skip if this filter doesn't match the type we're processing
    return null
  }
 
  /**
   * Build filter condition using Drizzle operators
   * Delegates to FilterBuilder
   */
  private buildFilterCondition(
    fieldExpr: AnyColumn | SQL,
    operator: FilterOperator,
    values: any[],
    field?: any,
    dateRange?: string | string[]
  ): SQL | null {
    return this.filterBuilder.buildFilterCondition(fieldExpr, operator, values, field, dateRange)
  }
 
  /**
   * Build date range condition for time dimensions
   * Delegates to DateTimeBuilder
   */
  buildDateRangeCondition(
    fieldExpr: AnyColumn | SQL,
    dateRange: string | string[]
  ): SQL | null {
    return this.dateTimeBuilder.buildDateRangeCondition(fieldExpr, dateRange)
  }
 
  /**
   * Build GROUP BY fields from dimensions and time dimensions
   * Delegates to GroupByBuilder
   */
  buildGroupByFields(
    cubes: Map<string, Cube> | Cube,
    query: SemanticQuery,
    context: QueryContext,
    queryPlan?: any
  ): (SQL | AnyColumn)[] {
    return this.groupByBuilder.buildGroupByFields(cubes, query, context, queryPlan)
  }
 
  /**
   * Build ORDER BY clause with automatic time dimension sorting
   */
  buildOrderBy(query: SemanticQuery, selectedFields?: string[]): SQL[] {
    const orderClauses: SQL[] = []
    
    // Get all selected fields (measures + dimensions + timeDimensions)
    const allSelectedFields = selectedFields || [
      ...(query.measures || []),
      ...(query.dimensions || []),
      ...(query.timeDimensions?.map(td => td.dimension) || [])
    ]
    
    // First, add explicit ordering from query.order
    if (query.order && Object.keys(query.order).length > 0) {
      for (const [field, direction] of Object.entries(query.order)) {
        // Validate that the field exists in the selected fields
        if (!allSelectedFields.includes(field)) {
          throw new Error(`Cannot order by '${field}': field is not selected in the query`)
        }
        
        // Use Drizzle's built-in asc/desc functions for proper ordering
        const orderClause = direction === 'desc' ? desc(sql.identifier(field)) : asc(sql.identifier(field))
        orderClauses.push(orderClause)
      }
    }
    
    // Then, automatically add time dimension sorting for any time dimensions not explicitly ordered
    if (query.timeDimensions && query.timeDimensions.length > 0) {
      const explicitlyOrderedFields = new Set(Object.keys(query.order || {}))
      
      // Sort time dimensions by their dimension name to ensure consistent ordering
      const sortedTimeDimensions = [...query.timeDimensions].sort((a, b) => 
        a.dimension.localeCompare(b.dimension)
      )
      
      for (const timeDim of sortedTimeDimensions) {
        if (!explicitlyOrderedFields.has(timeDim.dimension)) {
          // Automatically sort time dimensions in ascending order (earliest to latest)
          orderClauses.push(asc(sql.identifier(timeDim.dimension)))
        }
      }
    }
    
    return orderClauses
  }
 
  /**
   * Collect numeric field names (measures + numeric dimensions) for type conversion
   * Works for both single and multi-cube queries
   */
  collectNumericFields(cubes: Map<string, Cube> | Cube, query: SemanticQuery): string[] {
    const numericFields: string[] = []
    
    // Convert single cube to map for consistent handling
    const cubeMap = cubes instanceof Map ? cubes : new Map([[cubes.name, cubes]])
    
    // Add all measure fields (they are always numeric)
    if (query.measures) {
      numericFields.push(...query.measures)
    }
    
    // Add numeric dimension fields
    if (query.dimensions) {
      for (const dimensionName of query.dimensions) {
        const [cubeName, fieldName] = dimensionName.split('.')
        const cube = cubeMap.get(cubeName)
        Eif (cube) {
          const dimension = cube.dimensions[fieldName]
          if (dimension && dimension.type === 'number') {
            // Use the full name (with prefix) as it appears in the result
            numericFields.push(dimensionName)
          }
        }
      }
    }
    
    return numericFields
  }
 
  /**
   * Apply LIMIT and OFFSET to a query with validation
   * If offset is provided without limit, add a reasonable default limit
   */
  applyLimitAndOffset<T>(query: T, semanticQuery: SemanticQuery): T {
    // If offset is provided without limit, add a reasonable default limit
    let effectiveLimit = semanticQuery.limit
    if (semanticQuery.offset !== undefined && semanticQuery.offset > 0 && effectiveLimit === undefined) {
      effectiveLimit = 50 // Default limit when offset is used
    }
    
    let result = query
    
    if (effectiveLimit !== undefined) {
      if (effectiveLimit < 0) {
        throw new Error('Limit must be non-negative')
      }
      result = (result as any).limit(effectiveLimit)
    }
    
    if (semanticQuery.offset !== undefined) {
      if (semanticQuery.offset < 0) {
        throw new Error('Offset must be non-negative')
      }
      result = (result as any).offset(semanticQuery.offset)
    }
 
    return result
  }
 
  /**
   * Public wrapper for buildFilterCondition - used by executor for cache preloading
   * This allows pre-building filter SQL before query construction
   */
  buildFilterConditionPublic(
    fieldExpr: AnyColumn | SQL,
    operator: FilterOperator,
    values: any[],
    field?: any,
    dateRange?: string | string[]
  ): SQL | null {
    return this.buildFilterCondition(fieldExpr, operator, values, field, dateRange)
  }
 
  /**
   * Build a logical filter (AND/OR) - used by executor for cache preloading
   * This handles nested filter structures and builds combined SQL
   * Delegates to FilterBuilder
   */
  buildLogicalFilter(
    filter: Filter,
    cubes: Map<string, Cube>,
    context: QueryContext
  ): SQL | null {
    return this.filterBuilder.buildLogicalFilter(filter, cubes, context)
  }
}