All files server/query-planner.ts

93.99% Statements 360/383
80.23% Branches 203/253
93.61% Functions 44/47
93.69% Lines 342/365

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 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202                                                                                                546x           1670x 1670x 166x 166x   1670x             3428x     3428x 3424x 4529x 4529x         3428x 1355x 1535x 1535x         3428x 508x 536x 536x         3428x 1452x 1513x       3428x               3497x 934x 934x 1984x   934x       2563x 2563x 2563x 2563x                 691x   691x 403x     288x 328x     288x                 548x 108x 108x 220x   108x       440x 440x 440x 440x     184x 60x                               3340x 3340x   3340x         3340x 3340x   3340x         3340x 2541x                   799x     799x   799x                                     3340x 2541x       799x         799x 799x                                   799x 799x 799x       799x 799x 799x 1534x 1534x                   799x 799x 1230x   711x 711x 635x         1762x   799x 963x                   963x 1127x       963x           963x         963x 987x 8x     979x 979x         979x   8x     8x                                 971x             971x   971x               979x       799x                           799x   799x         799x 979x 979x 308x       671x 1374x         979x     979x   979x 20x       651x               651x             651x 651x             651x   12x       651x   651x               651x           651x                                     799x                                         651x     651x 651x 473x 581x 581x 533x       651x 96x 112x 112x 20x           651x 651x 643x 643x     643x     429x             429x               651x                   651x 651x   651x 827x 827x       827x   827x 827x       827x     827x 8x 8x 16x 8x           651x             8x 8x 8x   8x 16x   16x     16x       8x                   1726x 8x       1718x 2770x 2770x 1326x       392x                               651x 651x     272x 272x     272x 52x 52x 20x 20x 20x             272x 348x   200x 200x     184x 668x 668x     176x     176x 176x   176x 172x           172x                 272x             372x   516x 100x 100x 100x       416x 416x 416x 416x                               256x   256x   384x 60x 60x 48x   60x         324x 36x       36x   36x   20x 20x 20x             36x       288x 288x 288x 216x         256x               40x 72x 4x     4x   68x           68x 68x 68x 16x       24x             176x   176x   44x 44x 44x   4x               44x                         88x 88x     88x 4x                                           84x 84x     84x                                 84x 72x   36x 36x           36x 36x 36x                 36x 36x   36x 36x 36x     36x 36x 72x 72x   36x       36x   36x 20x   16x       36x 8x           36x             84x                       883x 48x               835x 835x 883x   883x 741x     835x 835x 1834x 1834x 1834x 1834x   1834x                 835x 1362x   609x 609x 1362x 64x     609x 633x 437x                       872x   398x 648x 390x 648x 8x   390x                 8x 8x                                       36x 36x     36x 36x 28x 28x         36x 8x                 28x 28x   28x         28x                 28x                 28x     28x                                     36x     36x       36x 36x 36x 16x     20x 20x         20x 40x         20x     20x   20x         20x           20x 20x           20x           20x 20x 20x                             36x    
/**
 * Query Planner for Unified Query Architecture
 * Handles query planning, cube analysis, and join resolution
 * All SQL building has been moved to QueryBuilder
 */
 
import type {
  Cube,
  QueryContext,
  QueryPlan,
  CubeJoin,
  SemanticQuery,
  QueryAnalysis,
  PrimaryCubeAnalysis,
  PrimaryCubeCandidate,
  JoinPathAnalysis,
  JoinPathStep,
  PreAggregationAnalysis,
  PropagatingFilter,
  Filter
} from './types'
 
import {
  resolveCubeReference,
  getJoinType,
  expandBelongsToManyJoin
} from './cube-utils'
 
import { JoinPathResolver } from './join-path-resolver'
import { MeasureBuilder } from './builders/measure-builder'
 
 
/**
 * Pre-aggregation plan for handling hasMany relationships
 */
// interface PreAggregationPlan {
//   cube: Cube
//   alias: string
//   joinKeys: Array<{
//     sourceColumn: string
//     targetColumn: string
//   }>
//   needsPreAggregation: boolean
//   measures: string[]
// }
 
export class QueryPlanner {
  // Cache resolver per cubes map to avoid repeated instantiation
  private resolverCache: WeakMap<Map<string, Cube>, JoinPathResolver> = new WeakMap()
 
  /**
   * Get or create a JoinPathResolver for the given cubes map
   */
  private getResolver(cubes: Map<string, Cube>): JoinPathResolver {
    let resolver = this.resolverCache.get(cubes)
    if (!resolver) {
      resolver = new JoinPathResolver(cubes)
      this.resolverCache.set(cubes, resolver)
    }
    return resolver
  }
 
  /**
   * Analyze a semantic query to determine which cubes are involved
   */
  analyzeCubeUsage(query: SemanticQuery): Set<string> {
    const cubesUsed = new Set<string>()
    
    // Extract cube names from measures
    if (query.measures) {
      for (const measure of query.measures) {
        const [cubeName] = measure.split('.')
        cubesUsed.add(cubeName)
      }
    }
    
    // Extract cube names from dimensions
    if (query.dimensions) {
      for (const dimension of query.dimensions) {
        const [cubeName] = dimension.split('.')
        cubesUsed.add(cubeName)
      }
    }
    
    // Extract cube names from time dimensions
    if (query.timeDimensions) {
      for (const timeDim of query.timeDimensions) {
        const [cubeName] = timeDim.dimension.split('.')
        cubesUsed.add(cubeName)
      }
    }
    
    // Extract cube names from filters
    if (query.filters) {
      for (const filter of query.filters) {
        this.extractCubeNamesFromFilter(filter, cubesUsed)
      }
    }
    
    return cubesUsed
  }
 
  /**
   * Recursively extract cube names from filters (handles logical filters)
   */
  private extractCubeNamesFromFilter(filter: any, cubesUsed: Set<string>): void {
    // Handle logical filters (AND/OR) - Server format: { and: [...] } or { or: [...] }
    if ('and' in filter || 'or' in filter) {
      const logicalFilters = filter.and || filter.or || []
      for (const subFilter of logicalFilters) {
        this.extractCubeNamesFromFilter(subFilter, cubesUsed)
      }
      return
    }
 
    // Handle simple filter condition
    Eif ('member' in filter) {
      const [cubeName] = filter.member.split('.')
      Eif (cubeName) {
        cubesUsed.add(cubeName)
      }
    }
  }
 
  /**
   * Extract measures referenced in filters (for CTE inclusion)
   */
  private extractMeasuresFromFilters(query: SemanticQuery, cube: Cube): string[] {
    const measures: string[] = []
 
    if (!query.filters) {
      return measures
    }
 
    for (const filter of query.filters) {
      this.extractMeasuresFromFilter(filter, cube, measures)
    }
 
    return measures
  }
 
  /**
   * Recursively extract measures from filters for a specific cube
   * Only includes filter members that are actually measures (not dimensions)
   */
  private extractMeasuresFromFilter(filter: any, targetCube: Cube, measures: string[]): void {
    // Handle logical filters (AND/OR)
    if ('and' in filter || 'or' in filter) {
      const logicalFilters = filter.and || filter.or || []
      for (const subFilter of logicalFilters) {
        this.extractMeasuresFromFilter(subFilter, targetCube, measures)
      }
      return
    }
 
    // Handle simple filter condition
    Eif ('member' in filter) {
      const memberName = filter.member
      const [cubeName, fieldName] = memberName.split('.')
      if (cubeName === targetCube.name) {
        // Only include if this is actually a measure, not a dimension
        // Dimension filters should not trigger CTE creation
        if (targetCube.measures && targetCube.measures[fieldName]) {
          measures.push(memberName)
        }
        // Dimension filters are intentionally excluded - they don't cause fan-out
        // and shouldn't trigger pre-aggregation CTEs
      }
    }
  }
 
  /**
   * Create a unified query plan that works for both single and multi-cube queries
   */
  createQueryPlan(
    cubes: Map<string, Cube>,
    query: SemanticQuery,
    ctx: QueryContext
  ): QueryPlan {
    const cubesUsed = this.analyzeCubeUsage(query)
    const cubeNames = Array.from(cubesUsed)
    
    Iif (cubeNames.length === 0) {
      throw new Error('No cubes found in query')
    }
    
    // Choose primary cube
    const primaryCubeName = this.choosePrimaryCube(cubeNames, query, cubes)
    const primaryCube = cubes.get(primaryCubeName)
    
    Iif (!primaryCube) {
      throw new Error(`Primary cube '${primaryCubeName}' not found`)
    }
    
    // For single cube queries, return simple plan with empty join array
    if (cubeNames.length === 1) {
      return {
        primaryCube,
        joinCubes: [], // Empty for single cube
        selections: {}, // Will be built by QueryBuilder
        whereConditions: [], // Will be built by QueryBuilder
        groupByFields: [] // Will be built by QueryBuilder
      }
    }
    
    // For multi-cube queries, build join plan
    const joinCubes = this.buildJoinPlan(cubes, primaryCube, cubeNames, ctx, query)
    
    // Detect hasMany relationships and plan pre-aggregation CTEs
    const preAggregationCTEs = this.planPreAggregationCTEs(cubes, primaryCube, joinCubes, query)
    
    return {
      primaryCube,
      joinCubes,
      selections: {}, // Will be built by QueryBuilder
      whereConditions: [], // Will be built by QueryBuilder
      groupByFields: [], // Will be built by QueryBuilder
      preAggregationCTEs
    }
  }
 
  /**
   * Choose the primary cube based on query analysis
   * Uses a consistent strategy to avoid measure order dependencies
   *
   * Delegates to analyzePrimaryCubeSelection() for the actual logic,
   * ensuring a single source of truth for primary cube selection.
   */
  choosePrimaryCube(cubeNames: string[], query: SemanticQuery, cubes?: Map<string, Cube>): string {
    // For single cube, return immediately
    if (cubeNames.length === 1) {
      return cubeNames[0]
    }
 
    // Without cubes map, fall back to alphabetical
    Iif (!cubes) {
      return [...cubeNames].sort()[0]
    }
 
    // Use the detailed analysis method and extract just the selected cube
    const analysis = this.analyzePrimaryCubeSelection(cubeNames, query, cubes)
    return analysis.selectedCube
  }
 
  /**
   * Build join plan for multi-cube query
   * Supports both direct joins and transitive joins through intermediate cubes
   *
   * Uses query-aware path selection to prefer joining through cubes that have
   * measures in the query (e.g., joining Teams through EmployeeTeams when
   * EmployeeTeams.count is a measure)
   */
  private buildJoinPlan(
    cubes: Map<string, Cube>,
    primaryCube: Cube,
    cubeNames: string[],
    ctx: QueryContext,
    query: SemanticQuery
  ): QueryPlan['joinCubes'] {
    const resolver = this.getResolver(cubes)
    const joinCubes: QueryPlan['joinCubes'] = []
    const processedCubes = new Set([primaryCube.name])
 
    // Identify cubes that have measures in the query - these should be preferred
    // in join path selection to ensure semantically correct joins
    const cubesWithMeasures = new Set<string>()
    Eif (query.measures) {
      for (const measure of query.measures) {
        const [cubeName] = measure.split('.')
        cubesWithMeasures.add(cubeName)
      }
    }
 
    // Pre-identify cubes that will become CTEs (hasMany relationships with measures)
    // IMPORTANT: We must NOT give "already processed" preference to CTE'd cubes because
    // when a cube becomes a CTE, its original table columns are replaced by CTE columns.
    // If we route join paths through a CTE'd cube, the join conditions will reference
    // the original table (e.g., productivity.departmentId) instead of the CTE alias,
    // causing "missing FROM-clause entry" errors.
    const willBeCTEd = new Set<string>()
    for (const cubeName of cubesWithMeasures) {
      if (cubeName === primaryCube.name) continue
      // Check if this cube has hasMany relationship from primary
      const hasManyJoin = this.findHasManyJoinDef(primaryCube, cubeName)
      if (hasManyJoin) {
        willBeCTEd.add(cubeName)
      }
    }
 
    // Find cubes to join (all except primary)
    const cubesToJoin = cubeNames.filter(name => name !== primaryCube.name)
 
    for (const cubeName of cubesToJoin) {
      Iif (processedCubes.has(cubeName)) {
        continue // Already processed
      }
 
      // When finding join paths, filter out CTE'd cubes from the "already processed" set.
      // This prevents the path scorer from preferring longer paths through CTE'd cubes
      // when shorter direct paths exist.
      // Example: Without this fix, Employees → Productivity → Departments would be chosen
      // over Employees → Departments because Productivity is "already processed", even though
      // Productivity is a CTE and its join conditions won't work.
      const effectiveProcessed = new Set(
        [...processedCubes].filter(c => !willBeCTEd.has(c))
      )
 
      // Use preferring method to route through cubes with measures when possible
      const joinPath = resolver.findPathPreferring(
        primaryCube.name,
        cubeName,
        cubesWithMeasures,
        effectiveProcessed
      )
      Iif (!joinPath || joinPath.length === 0) {
        throw new Error(`No join path found from '${primaryCube.name}' to '${cubeName}'`)
      }
 
      // Add all cubes in the join path
      for (const { toCube, joinDef } of joinPath) {
        if (processedCubes.has(toCube)) {
          continue // Skip if already processed
        }
 
        const cube = cubes.get(toCube)
        Iif (!cube) {
          throw new Error(`Cube '${toCube}' not found`)
        }
 
        // Check if this is a belongsToMany relationship
        if (joinDef.relationship === 'belongsToMany' && joinDef.through) {
          // Expand the belongsToMany join into junction table joins
          const expanded = expandBelongsToManyJoin(joinDef, ctx.securityContext)
 
          // Add the join with junction table information
          joinCubes.push({
            cube,
            alias: `${toCube.toLowerCase()}_cube`,
            joinType: expanded.junctionJoins[1].joinType, // Use the target join type
            joinCondition: expanded.junctionJoins[1].condition, // Target join condition
            junctionTable: {
              table: joinDef.through.table,
              alias: `junction_${toCube.toLowerCase()}`,
              joinType: expanded.junctionJoins[0].joinType,
              joinCondition: expanded.junctionJoins[0].condition,
              securitySql: joinDef.through.securitySql
            }
          })
        } else {
          // Regular join (belongsTo, hasOne, hasMany)
          // Build join condition using new array-based format
          // For regular table joins, we don't use artificial aliases - use actual table references
          const joinCondition = resolver.buildJoinCondition(
            joinDef as CubeJoin,
            null, // No source alias needed - use the actual column
            null // No target alias needed - use the actual column
          )
 
          // Derive join type from relationship
          const joinType = getJoinType(joinDef.relationship, joinDef.sqlJoinType) as 'inner' | 'left' | 'right' | 'full'
 
          joinCubes.push({
            cube,
            alias: `${toCube.toLowerCase()}_cube`,
            joinType,
            joinCondition
          })
        }
 
        processedCubes.add(toCube)
      }
    }
 
    return joinCubes
  }
 
  /**
   * Plan pre-aggregation CTEs for hasMany relationships to prevent fan-out
   * Note: belongsToMany relationships handle fan-out differently through their junction table structure
   * and don't require CTEs - the two-hop join with the junction table provides natural grouping
   */
  private planPreAggregationCTEs(
    _cubes: Map<string, Cube>,
    primaryCube: Cube,
    joinCubes: QueryPlan['joinCubes'],
    query: SemanticQuery
  ): QueryPlan['preAggregationCTEs'] {
    const preAggCTEs: QueryPlan['preAggregationCTEs'] = []
 
    Iif (!query.measures || query.measures.length === 0) {
      return preAggCTEs // No measures, no fan-out risk
    }
 
    // Check each join cube for hasMany relationships
    for (const joinCube of joinCubes) {
      const hasManyJoinDef = this.findHasManyJoinDef(primaryCube, joinCube.cube.name)
      if (!hasManyJoinDef) {
        continue // Not a hasMany relationship
      }
 
      // Check if we have measures from this hasMany cube (from SELECT clause)
      const measuresFromSelect = query.measures ? query.measures.filter(m =>
        m.startsWith(joinCube.cube.name + '.')
      ) : []
 
      // Also check for measures referenced in filters (for HAVING clause)
      // Only actual measures are included - dimension filters don't trigger CTE creation
      const measuresFromFilters = this.extractMeasuresFromFilters(query, joinCube.cube)
 
      // Combine and deduplicate measures from both SELECT and filters
      const allMeasuresFromThisCube = [...new Set([...measuresFromSelect, ...measuresFromFilters])]
 
      if (allMeasuresFromThisCube.length === 0) {
        continue // No measures from this cube, no fan-out risk
      }
 
      // Extract join keys from the new array-based format
      const joinKeys = hasManyJoinDef.on.map(joinOn => ({
        sourceColumn: joinOn.source.name,
        targetColumn: joinOn.target.name,
        sourceColumnObj: joinOn.source,
        targetColumnObj: joinOn.target
      }))
 
      // Find propagating filters from related cubes that should apply to this CTE
      const propagatingFilters = this.findPropagatingFilters(query, joinCube.cube, _cubes)
 
      // Categorize measures for post-aggregation window function handling
      // Window functions now operate on aggregated data, so we need to:
      // 1. Collect regular aggregate measures
      // 2. Collect base measures required by post-aggregation window functions
      // 3. Window functions themselves are applied in the outer query, not in CTEs
      const cubeMap = new Map([[joinCube.cube.name, joinCube.cube]])
      const { aggregateMeasures, requiredBaseMeasures } = MeasureBuilder.categorizeForPostAggregation(
        allMeasuresFromThisCube,
        cubeMap
      )
 
      // Combine aggregate measures with base measures required by window functions
      // This ensures the CTE contains all data needed for window function computation
      const allAggregateMeasures = [...new Set([
        ...aggregateMeasures,
        ...Array.from(requiredBaseMeasures).filter(m => m.startsWith(joinCube.cube.name + '.'))
      ])]
 
      // Create aggregate CTE if we have any aggregate measures (including window base measures)
      Eif (allAggregateMeasures.length > 0) {
        // Expand calculated measures to include their dependencies
        const expandedAggregateMeasures = this.expandCalculatedMeasureDependencies(
          joinCube.cube,
          allAggregateMeasures
        )
 
        // Detect downstream cubes that need join keys in the CTE
        // Example: If query has Teams.name dimension and EmployeeTeams.count measure,
        // the EmployeeTeams CTE needs to include team_id so Teams can be joined through it
        const downstreamJoinKeys = this.findDownstreamJoinKeys(
          joinCube.cube,
          query,
          _cubes
        )
 
        preAggCTEs!.push({
          cube: joinCube.cube,
          alias: joinCube.alias,
          cteAlias: `${joinCube.cube.name.toLowerCase()}_agg`,
          joinKeys,
          measures: expandedAggregateMeasures,
          propagatingFilters: propagatingFilters.length > 0 ? propagatingFilters : undefined,
          downstreamJoinKeys: downstreamJoinKeys.length > 0 ? downstreamJoinKeys : undefined,
          cteType: 'aggregate'
        })
      }
 
      // NOTE: Window CTEs are no longer created here.
      // Post-aggregation window functions are applied in the outer query (executor.ts)
      // after the data has been aggregated. This follows the analytics pattern:
      // 1. Aggregate data in CTE (GROUP BY dimensions)
      // 2. Apply window functions to aggregated results in outer SELECT
    }
 
    return preAggCTEs
  }
 
  /**
   * Find downstream cubes that need join keys included in the CTE.
   *
   * When a query has dimensions from a cube (e.g., Teams.name) and measures from
   * a junction cube (e.g., EmployeeTeams.count), the junction CTE needs to include
   * the join key to the dimension cube (team_id) so the dimension cube can be
   * joined through the CTE instead of via an alternative path.
   *
   * @param cteCube The cube being converted to a CTE (e.g., EmployeeTeams)
   * @param query The semantic query with dimensions and measures
   * @param allCubes Map of all registered cubes
   * @returns Array of downstream join key info for cubes needing join through this CTE
   */
  private findDownstreamJoinKeys(
    cteCube: Cube,
    query: SemanticQuery,
    _allCubes: Map<string, Cube>
  ): Array<{ targetCubeName: string; joinKeys: Array<{ sourceColumn: string; targetColumn: string; sourceColumnObj?: any; targetColumnObj?: any }> }> {
    const downstreamJoinKeys: Array<{ targetCubeName: string; joinKeys: Array<{ sourceColumn: string; targetColumn: string; sourceColumnObj?: any; targetColumnObj?: any }> }> = []
 
    // Get cubes that have dimensions in the query (excluding the CTE cube itself)
    const dimensionCubeNames = new Set<string>()
    if (query.dimensions) {
      for (const dim of query.dimensions) {
        const [cubeName] = dim.split('.')
        if (cubeName !== cteCube.name) {
          dimensionCubeNames.add(cubeName)
        }
      }
    }
    if (query.timeDimensions) {
      for (const timeDim of query.timeDimensions) {
        const [cubeName] = timeDim.dimension.split('.')
        if (cubeName !== cteCube.name) {
          dimensionCubeNames.add(cubeName)
        }
      }
    }
 
    // For each dimension cube, check if it's directly joinable from the CTE cube
    Eif (cteCube.joins) {
      for (const [, joinDef] of Object.entries(cteCube.joins)) {
        const targetCube = resolveCubeReference(joinDef.targetCube)
        const targetCubeName = targetCube.name
 
        // Check if this target cube has dimensions in the query
        if (dimensionCubeNames.has(targetCubeName)) {
          // This cube's dimensions are in the query and it's joinable from the CTE cube
          // Include the join keys so the dimension cube can be joined through the CTE
          const joinKeys = joinDef.on.map(joinOn => ({
            sourceColumn: joinOn.source.name,
            targetColumn: joinOn.target.name,
            sourceColumnObj: joinOn.source,
            targetColumnObj: joinOn.target
          }))
 
          downstreamJoinKeys.push({
            targetCubeName,
            joinKeys
          })
        }
      }
    }
 
    return downstreamJoinKeys
  }
 
  /**
   * Expand calculated measures to include their dependencies
   */
  private expandCalculatedMeasureDependencies(
    cube: Cube,
    measures: string[]
  ): string[] {
    const expandedSet = new Set<string>()
    const toProcess = [...measures]
 
    while (toProcess.length > 0) {
      const measureName = toProcess.pop()!
      Iif (expandedSet.has(measureName)) {
        continue
      }
 
      expandedSet.add(measureName)
 
      const [, fieldName] = measureName.split('.')
      Iif (!cube.measures || !cube.measures[fieldName]) {
        continue
      }
 
      const measure = cube.measures[fieldName]
 
      // If it's a calculated measure, extract its dependencies
      if (measure.type === 'calculated' && measure.calculatedSql) {
        const deps = this.extractDependenciesFromTemplate(measure.calculatedSql, cube.name)
        for (const dep of deps) {
          if (!expandedSet.has(dep)) {
            toProcess.push(dep)
          }
        }
      }
    }
 
    return Array.from(expandedSet)
  }
 
  /**
   * Extract measure references from calculatedSql template
   */
  private extractDependenciesFromTemplate(calculatedSql: string, cubeName: string): string[] {
    const regex = /\{([^}]+)\}/g
    const matches = calculatedSql.matchAll(regex)
    const deps: string[] = []
 
    for (const match of matches) {
      const ref = match[1].trim()
      // Handle both {measure} and {Cube.measure} formats
      Iif (ref.includes('.')) {
        deps.push(ref)
      } else {
        deps.push(`${cubeName}.${ref}`)
      }
    }
 
    return deps
  }
 
  /**
   * Find hasMany join definition from primary cube to target cube
   */
  private findHasManyJoinDef(
    primaryCube: Cube,
    targetCubeName: string
  ): CubeJoin | null {
    if (!primaryCube.joins) {
      return null
    }
 
    // Look through all joins from primary cube
    for (const [, joinDef] of Object.entries(primaryCube.joins)) {
      const resolvedTargetCube = resolveCubeReference(joinDef.targetCube)
      if (resolvedTargetCube.name === targetCubeName && joinDef.relationship === 'hasMany') {
        return joinDef as CubeJoin
      }
    }
 
    return null
  }
 
  /**
   * Find filters that need to propagate from related cubes to a CTE cube.
   * When cube A has filters and a hasMany relationship to cube B (the CTE cube),
   * A's filters should propagate into B's CTE via a subquery.
   *
   * Example: Employees.createdAt filter should propagate to Productivity CTE
   * via: employee_id IN (SELECT id FROM employees WHERE created_at >= $date)
   */
  private findPropagatingFilters(
    query: SemanticQuery,
    cteCube: Cube,
    allCubes: Map<string, Cube>
  ): PropagatingFilter[] {
    const result: PropagatingFilter[] = []
    if (!query.filters) return result
 
    // Extract all cube names referenced in filters
    const filterCubeNames = new Set<string>()
    this.extractFilterCubeNamesToSet(query.filters, filterCubeNames)
 
    // Also check time dimension filters which may have date ranges
    if (query.timeDimensions) {
      for (const timeDim of query.timeDimensions) {
        if (timeDim.dateRange) {
          const [cubeName] = timeDim.dimension.split('.')
          Eif (cubeName) {
            filterCubeNames.add(cubeName)
          }
        }
      }
    }
 
    // For each filter cube, check if it has a hasMany relationship TO the CTE cube
    for (const filterCubeName of filterCubeNames) {
      if (filterCubeName === cteCube.name) continue // Same cube, handled elsewhere
 
      const filterCube = allCubes.get(filterCubeName)
      if (!filterCube?.joins) continue
 
      // Check if filterCube has hasMany -> cteCube
      for (const [, joinDef] of Object.entries(filterCube.joins)) {
        const targetCube = resolveCubeReference(joinDef.targetCube)
        if (targetCube.name === cteCube.name && joinDef.relationship === 'hasMany') {
          // Found: filterCube hasMany -> cteCube
          // Extract the filters for this cube
          const filtersForCube = this.extractFiltersForCube(query.filters, filterCubeName)
 
          // Also add time dimension date ranges as filters
          const timeFilters = this.extractTimeDimensionFiltersForCube(query, filterCubeName)
          const allFilters = [...filtersForCube, ...timeFilters]
 
          if (allFilters.length > 0 && joinDef.on.length > 0) {
            result.push({
              sourceCube: filterCube,
              filters: allFilters,
              // Map all join keys for composite key support
              // source = filterCube PK (e.g., employees.id)
              // target = cteCube FK (e.g., productivity.employeeId)
              joinConditions: joinDef.on.map(key => ({
                source: key.source,
                target: key.target
              }))
            })
          }
        }
      }
    }
    return result
  }
 
  /**
   * Extract cube names from filters into a Set (helper for findPropagatingFilters)
   */
  private extractFilterCubeNamesToSet(filters: Filter[], cubesSet: Set<string>): void {
    for (const filter of filters) {
      // Handle logical filters (AND/OR)
      if ('and' in filter || 'or' in filter) {
        const logicalFilters = (filter as any).and || (filter as any).or || []
        this.extractFilterCubeNamesToSet(logicalFilters, cubesSet)
        continue
      }
 
      // Handle simple filter condition
      Eif ('member' in filter) {
        const [cubeName] = (filter as any).member.split('.')
        Eif (cubeName) {
          cubesSet.add(cubeName)
        }
      }
    }
  }
 
  /**
   * Extract filters for a specific cube from the filter array
   *
   * Logic for preserving filter semantics:
   * - AND: Safe to extract only matching branches (AND of fewer conditions is more permissive)
   * - OR: Must include ALL branches or skip entirely (partial OR changes semantics)
   *       If any branch belongs to another cube, skip the entire OR to be safe
   *       since we can't evaluate the other cube's conditions
   */
  private extractFiltersForCube(filters: Filter[], targetCubeName: string): Filter[] {
    const result: Filter[] = []
 
    for (const filter of filters) {
      // Handle AND filters - safe to extract only matching branches
      if ('and' in filter) {
        const subFilters = this.extractFiltersForCube((filter as any).and || [], targetCubeName)
        if (subFilters.length > 0) {
          result.push({ and: subFilters })
        }
        continue
      }
 
      // Handle OR filters - must check if ALL branches belong to target cube
      // If any branch belongs to another cube, skip the entire OR
      if ('or' in filter) {
        const orFilters = (filter as any).or || []
 
        // Check if all simple filters in this OR belong to target cube
        // If any belong to other cubes, skip this OR entirely
        const allBelongToTarget = this.allFiltersFromCube(orFilters, targetCubeName)
 
        if (allBelongToTarget) {
          // All branches belong to target cube, safe to include
          const subFilters = this.extractFiltersForCube(orFilters, targetCubeName)
          Eif (subFilters.length > 0) {
            result.push({ or: subFilters })
          }
        }
        // If not all belong to target, skip this OR filter entirely
        // This is the safe choice for CTE propagation - we can't evaluate
        // conditions from other cubes, so we shouldn't filter rows based on
        // partial OR conditions
        continue
      }
 
      // Handle simple filter condition
      Eif ('member' in filter) {
        const [cubeName] = (filter as any).member.split('.')
        if (cubeName === targetCubeName) {
          result.push(filter)
        }
      }
    }
 
    return result
  }
 
  /**
   * Check if all simple filters in a filter array belong to the specified cube
   * Recursively checks nested AND/OR filters
   */
  private allFiltersFromCube(filters: Filter[], targetCubeName: string): boolean {
    for (const filter of filters) {
      if ('and' in filter) {
        Iif (!this.allFiltersFromCube((filter as any).and || [], targetCubeName)) {
          return false
        }
        continue
      }
      Iif ('or' in filter) {
        if (!this.allFiltersFromCube((filter as any).or || [], targetCubeName)) {
          return false
        }
        continue
      }
      Eif ('member' in filter) {
        const [cubeName] = (filter as any).member.split('.')
        if (cubeName !== targetCubeName) {
          return false
        }
      }
    }
    return true
  }
 
  /**
   * Extract time dimension date range filters as regular filters for a specific cube
   */
  private extractTimeDimensionFiltersForCube(query: SemanticQuery, targetCubeName: string): Filter[] {
    const result: Filter[] = []
 
    if (!query.timeDimensions) return result
 
    for (const timeDim of query.timeDimensions) {
      const [cubeName] = timeDim.dimension.split('.')
      if (cubeName === targetCubeName && timeDim.dateRange) {
        // Convert time dimension dateRange to an inDateRange filter
        result.push({
          member: timeDim.dimension,
          operator: 'inDateRange',
          values: Array.isArray(timeDim.dateRange) ? timeDim.dateRange : [timeDim.dateRange]
        })
      }
    }
 
    return result
  }
 
  /**
   * Analyze query planning decisions without building the full query
   * Returns detailed metadata about how the query plan would be constructed
   * Used for debugging and transparency in the playground UI
   */
  analyzeQueryPlan(
    cubes: Map<string, Cube>,
    query: SemanticQuery,
    _ctx: QueryContext
  ): QueryAnalysis {
    const cubesUsed = this.analyzeCubeUsage(query)
    const cubeNames = Array.from(cubesUsed)
 
    // Handle empty query
    if (cubeNames.length === 0) {
      return {
        timestamp: new Date().toISOString(),
        cubeCount: 0,
        cubesInvolved: [],
        primaryCube: {
          selectedCube: '',
          reason: 'single_cube',
          explanation: 'No cubes found in query'
        },
        joinPaths: [],
        preAggregations: [],
        querySummary: {
          queryType: 'single_cube',
          joinCount: 0,
          cteCount: 0,
          hasPreAggregation: false
        },
        warnings: ['No cubes found in query - add measures or dimensions']
      }
    }
 
    // Analyze primary cube selection
    const primaryCubeAnalysis = this.analyzePrimaryCubeSelection(cubeNames, query, cubes)
    const primaryCubeName = primaryCubeAnalysis.selectedCube
 
    // Build analysis object
    const analysis: QueryAnalysis = {
      timestamp: new Date().toISOString(),
      cubeCount: cubeNames.length,
      cubesInvolved: cubeNames.sort(),
      primaryCube: primaryCubeAnalysis,
      joinPaths: [],
      preAggregations: [],
      querySummary: {
        queryType: 'single_cube',
        joinCount: 0,
        cteCount: 0,
        hasPreAggregation: false
      },
      warnings: []
    }
 
    // If multi-cube, analyze join paths
    if (cubeNames.length > 1) {
      const cubesToJoin = cubeNames.filter(name => name !== primaryCubeName)
 
      for (const targetCube of cubesToJoin) {
        analysis.joinPaths.push(
          this.analyzeJoinPath(cubes, primaryCubeName, targetCube)
        )
      }
 
      // Analyze pre-aggregation requirements
      const primaryCube = cubes.get(primaryCubeName)
      Eif (primaryCube) {
        analysis.preAggregations = this.analyzePreAggregations(
          cubes,
          primaryCube,
          cubesToJoin,
          query
        )
      }
 
      // Update summary
      const successfulPaths = analysis.joinPaths.filter(p => p.pathFound)
      const failedPaths = analysis.joinPaths.filter(p => !p.pathFound)
 
      analysis.querySummary.joinCount = successfulPaths.length
      analysis.querySummary.cteCount = analysis.preAggregations.length
      analysis.querySummary.hasPreAggregation = analysis.preAggregations.length > 0
 
      // Detect post-aggregation window functions in the query
      const allCubes = new Map<string, Cube>()
      for (const name of cubeNames) {
        const cube = cubes.get(name)
        Eif (cube) allCubes.set(name, cube)
      }
      const hasPostAggWindows = MeasureBuilder.hasPostAggregationWindows(
        query.measures || [],
        allCubes
      )
      analysis.querySummary.hasWindowFunctions = hasPostAggWindows
 
      if (analysis.preAggregations.length > 0) {
        analysis.querySummary.queryType = 'multi_cube_cte'
      } else {
        analysis.querySummary.queryType = 'multi_cube_join'
      }
 
      // Add warnings for failed paths
      for (const failedPath of failedPaths) {
        analysis.warnings!.push(
          `No join path found to cube '${failedPath.targetCube}'. Check that joins are defined correctly.`
        )
      }
 
      // Add info about post-aggregation window functions
      Iif (hasPostAggWindows) {
        analysis.warnings!.push(
          `Query contains post-aggregation window functions which will be applied to aggregated results.`
        )
      }
    }
 
    return analysis
  }
 
  /**
   * Analyze why a particular cube was chosen as primary
   */
  private analyzePrimaryCubeSelection(
    cubeNames: string[],
    query: SemanticQuery,
    cubes: Map<string, Cube>
  ): PrimaryCubeAnalysis {
    // Single cube case
    if (cubeNames.length === 1) {
      return {
        selectedCube: cubeNames[0],
        reason: 'single_cube',
        explanation: 'Only one cube is used in this query'
      }
    }
 
    // Build candidates list
    const candidates: PrimaryCubeCandidate[] = []
    const dimensionCubes = (query.dimensions || []).map(d => d.split('.')[0])
    const cubeDimensionCount = new Map<string, number>()
 
    for (const cube of dimensionCubes) {
      cubeDimensionCount.set(cube, (cubeDimensionCount.get(cube) || 0) + 1)
    }
 
    const resolver = this.getResolver(cubes)
    for (const cubeName of cubeNames) {
      const cube = cubes.get(cubeName)
      const dimensionCount = cubeDimensionCount.get(cubeName) || 0
      const joinCount = cube?.joins ? Object.keys(cube.joins).length : 0
      const canReachAll = resolver.canReachAll(cubeName, cubeNames)
 
      candidates.push({
        cubeName,
        dimensionCount,
        joinCount,
        canReachAll
      })
    }
 
    // Tier 1: Check for dimension-based selection
    if (query.dimensions && query.dimensions.length > 0) {
      const maxDimensions = Math.max(...candidates.map(c => c.dimensionCount))
 
      Eif (maxDimensions > 0) {
        const primaryCandidates = candidates
          .filter(c => c.dimensionCount === maxDimensions)
          .sort((a, b) => a.cubeName.localeCompare(b.cubeName))
 
        // Check if candidate can reach all other cubes
        for (const candidate of primaryCandidates) {
          if (candidate.canReachAll) {
            return {
              selectedCube: candidate.cubeName,
              reason: 'most_dimensions',
              explanation: `Selected because it has ${candidate.dimensionCount} dimension${candidate.dimensionCount !== 1 ? 's' : ''} in the query (defines the analytical grain)`,
              candidates
            }
          }
        }
      }
    }
 
    // Tier 2: Connectivity-based selection
    const reachableCandidates = candidates.filter(c => c.canReachAll)
 
    if (reachableCandidates.length > 0) {
      const maxConnectivity = Math.max(...reachableCandidates.map(c => c.joinCount))
      const mostConnected = reachableCandidates
        .filter(c => c.joinCount === maxConnectivity)
        .sort((a, b) => a.cubeName.localeCompare(b.cubeName))[0]
 
      return {
        selectedCube: mostConnected.cubeName,
        reason: 'most_connected',
        explanation: `Selected because it has ${mostConnected.joinCount} join relationship${mostConnected.joinCount !== 1 ? 's' : ''} and can reach all other cubes`,
        candidates
      }
    }
 
    // Tier 3: Alphabetical fallback
    const fallback = [...cubeNames].sort()[0]
    return {
      selectedCube: fallback,
      reason: 'alphabetical_fallback',
      explanation: 'Selected alphabetically as fallback (no cube could reach all others)',
      candidates
    }
  }
 
  /**
   * Analyze the join path between two cubes with detailed step information
   *
   * Uses JoinPathResolver.findPath() for the actual path finding,
   * then converts the result to human-readable analysis format.
   */
  private analyzeJoinPath(
    cubes: Map<string, Cube>,
    fromCube: string,
    toCube: string
  ): JoinPathAnalysis {
    // Use the resolver for BFS path finding (cached, optimized)
    const resolver = this.getResolver(cubes)
    const internalPath = resolver.findPath(fromCube, toCube)
 
    // Build visited cubes list from path
    const visitedCubes = [fromCube]
    if (internalPath) {
      for (const step of internalPath) {
        visitedCubes.push(step.toCube)
      }
    }
 
    // No path found
    if (!internalPath || internalPath.length === 0) {
      return {
        targetCube: toCube,
        pathFound: false,
        error: `No join path found from '${fromCube}' to '${toCube}'. Ensure the target cube has a relationship defined (belongsTo, hasOne, hasMany, or belongsToMany).`,
        visitedCubes
      }
    }
 
    // Convert internal path to analysis format
    const pathSteps: JoinPathStep[] = internalPath.map(step => {
      const joinType = getJoinType(step.joinDef.relationship, step.joinDef.sqlJoinType) as 'inner' | 'left' | 'right' | 'full'
 
      const joinColumns = step.joinDef.on.map(joinOn => ({
        sourceColumn: joinOn.source.name,
        targetColumn: joinOn.target.name
      }))
 
      const result: JoinPathStep = {
        fromCube: step.fromCube,
        toCube: step.toCube,
        relationship: step.joinDef.relationship,
        joinType,
        joinColumns
      }
 
      // Add junction table info for belongsToMany
      Iif (step.joinDef.relationship === 'belongsToMany' && step.joinDef.through) {
        const through = step.joinDef.through
        result.junctionTable = {
          tableName: (through.table as any)[Symbol.for('drizzle:Name')] || 'junction_table',
          sourceColumns: through.sourceKey.map(k => k.target.name),
          targetColumns: through.targetKey.map(k => k.source.name)
        }
      }
 
      return result
    })
 
    return {
      targetCube: toCube,
      pathFound: true,
      path: pathSteps,
      pathLength: pathSteps.length,
      visitedCubes
    }
  }
 
  /**
   * Analyze pre-aggregation requirements for hasMany relationships
   * This mirrors the logic in planPreAggregationCTEs to ensure analysis matches execution
   */
  private analyzePreAggregations(
    cubes: Map<string, Cube>,
    primaryCube: Cube,
    cubesToJoin: string[],
    query: SemanticQuery
  ): PreAggregationAnalysis[] {
    const preAggregations: PreAggregationAnalysis[] = []
 
    // No measures in query means no fan-out risk, no CTEs needed
    Iif (!query.measures || query.measures.length === 0) {
      return preAggregations
    }
 
    for (const targetCubeName of cubesToJoin) {
      const hasManyJoinDef = this.findHasManyJoinDef(primaryCube, targetCubeName)
      if (!hasManyJoinDef) {
        continue
      }
 
      const targetCube = cubes.get(targetCubeName)
      Iif (!targetCube) {
        continue
      }
 
      // Check if we have measures from this cube (from SELECT clause)
      const measuresFromSelect = query.measures.filter(m =>
        m.startsWith(targetCubeName + '.')
      )
 
      // Also check for measures referenced in filters (for HAVING clause)
      // Only actual measures are included - dimension filters don't trigger CTE creation
      const measuresFromFilters = this.extractMeasuresFromFilters(query, targetCube)
 
      // Combine and deduplicate measures from both SELECT and filters
      const allMeasuresFromThisCube = [...new Set([...measuresFromSelect, ...measuresFromFilters])]
 
      Iif (allMeasuresFromThisCube.length === 0) {
        continue
      }
 
      // Extract join keys
      const joinKeys = hasManyJoinDef.on.map(joinOn => ({
        sourceColumn: joinOn.source.name,
        targetColumn: joinOn.target.name
      }))
 
      // Categorize measures for post-aggregation window function handling
      const cubeMap = new Map([[targetCubeName, targetCube]])
      const { aggregateMeasures, postAggWindowMeasures, requiredBaseMeasures } = MeasureBuilder.categorizeForPostAggregation(
        allMeasuresFromThisCube,
        cubeMap
      )
 
      // Combine aggregate measures with base measures required by window functions
      const allAggregateMeasures = [...new Set([
        ...aggregateMeasures,
        ...Array.from(requiredBaseMeasures).filter(m => m.startsWith(targetCubeName + '.'))
      ])]
 
      // Create analysis for aggregate CTE if we have any aggregate measures
      Eif (allAggregateMeasures.length > 0) {
        const hasWindowDeps = postAggWindowMeasures.length > 0
        preAggregations.push({
          cubeName: targetCubeName,
          cteAlias: `${targetCubeName.toLowerCase()}_agg`,
          reason: hasWindowDeps
            ? `hasMany relationship from ${primaryCube.name} - requires pre-aggregation; includes base measures for post-aggregation window functions`
            : `hasMany relationship from ${primaryCube.name} - requires pre-aggregation to prevent row duplication (fan-out)`,
          measures: allAggregateMeasures,
          joinKeys,
          cteType: 'aggregate'
        })
      }
 
      // Note: Window CTEs are no longer created - post-aggregation windows are applied in outer query
    }
 
    return preAggregations
  }
}