All files server/adapters/sqlite-adapter.ts

82.35% Statements 98/119
63.93% Branches 78/122
93.1% Functions 27/29
82.3% Lines 93/113

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                        445x               8x                         4x 4x                 5x               7x 7x                         3x 3x         3x                 12x     4x     4x     4x                                                                 318x     17x       22x 22x         81x       31x     142x     13x 13x       6x 6x       6x 6x                         97x   43x   2x   4x   2x   27x   6x     2x     6x     5x                     12x       4x     4x   4x                       205x                   38x   38x     38x   38x             38x                 38x               16x                                     26x   26x   16x 16x                     33161x 90x   33071x 4x   33067x 13x     33062x 11785x   21277x               12x     8x 4x     4x             345x               45299x                       2x                                   2x                   1x                   3x                             11x       11x   11x         11x 11x 4x 4x   4x       4x         4x       11x 11x 11x 11x   11x 11x     11x   4x       3x                       2x   2x          
/**
 * SQLite Database Adapter
 * Implements SQLite-specific SQL generation for time dimensions, string matching, and type casting
 * Supports local SQLite with better-sqlite3 driver
 */
 
import { sql, type SQL, type AnyColumn } from 'drizzle-orm'
import type { TimeGranularity } from '../types'
import { BaseDatabaseAdapter, type DatabaseCapabilities, type WindowFunctionType, type WindowFunctionConfig } from './base-adapter'
 
export class SQLiteAdapter extends BaseDatabaseAdapter {
  getEngineType(): 'sqlite' {
    return 'sqlite'
  }
 
  /**
   * SQLite does not support LATERAL joins
   * Flow queries require LATERAL for efficient execution and are not supported on SQLite
   */
  supportsLateralJoins(): boolean {
    return false
  }
 
  // ============================================
  // Funnel Analysis Methods
  // ============================================
 
  /**
   * Build SQLite INTERVAL from ISO 8601 duration
   * SQLite doesn't have native interval types, so we convert to seconds
   * for arithmetic operations on Unix timestamps
   */
  buildIntervalFromISO(duration: string): SQL {
    const totalSeconds = this.durationToSeconds(duration)
    return sql`${totalSeconds}`
  }
 
  /**
   * Build SQLite time difference in seconds
   * SQLite timestamps are stored as Unix seconds, so simple subtraction works
   * Returns (end - start) as seconds
   */
  buildTimeDifferenceSeconds(end: SQL, start: SQL): SQL {
    return sql`(${end} - ${start})`
  }
 
  /**
   * Build SQLite timestamp + interval expression
   * Since SQLite stores timestamps as Unix seconds, just add the seconds
   */
  buildDateAddInterval(timestamp: SQL, duration: string): SQL {
    const totalSeconds = this.durationToSeconds(duration)
    return sql`(${timestamp} + ${totalSeconds})`
  }
 
  /**
   * Build SQLite conditional aggregation using CASE WHEN
   * SQLite doesn't support FILTER clause, so we use CASE WHEN pattern
   * Example: AVG(CASE WHEN step_1_time IS NOT NULL THEN time_diff END)
   */
  buildConditionalAggregation(
    aggFn: 'count' | 'avg' | 'min' | 'max' | 'sum',
    expr: SQL | null,
    condition: SQL
  ): SQL {
    const fnName = aggFn.toUpperCase()
    Iif (aggFn === 'count' && !expr) {
      // COUNT(*) with condition -> COUNT(CASE WHEN condition THEN 1 END)
      return sql`COUNT(CASE WHEN ${condition} THEN 1 END)`
    }
    // AVG/MIN/MAX/SUM -> AGG(CASE WHEN condition THEN expr END)
    return sql`${sql.raw(fnName)}(CASE WHEN ${condition} THEN ${expr} END)`
  }
 
  /**
   * Build SQLite date difference in periods
   * SQLite uses Julian day calculations for date arithmetic
   * Note: SQLite timestamps are stored as Unix seconds
   */
  buildDateDiffPeriods(startDate: SQL, endDate: SQL, unit: 'day' | 'week' | 'month'): SQL {
    switch (unit) {
      case 'day':
        // Calculate day difference using Julian day
        return sql`CAST((julianday(datetime(${endDate}, 'unixepoch')) - julianday(datetime(${startDate}, 'unixepoch'))) AS INTEGER)`
      case 'week':
        // Calculate week difference
        return sql`CAST((julianday(datetime(${endDate}, 'unixepoch')) - julianday(datetime(${startDate}, 'unixepoch'))) / 7 AS INTEGER)`
      case 'month':
        // Calculate month difference using string manipulation
        return sql`((CAST(strftime('%Y', datetime(${endDate}, 'unixepoch')) AS INTEGER) - CAST(strftime('%Y', datetime(${startDate}, 'unixepoch')) AS INTEGER)) * 12 + (CAST(strftime('%m', datetime(${endDate}, 'unixepoch')) AS INTEGER) - CAST(strftime('%m', datetime(${startDate}, 'unixepoch')) AS INTEGER)))`
      default:
        throw new Error(`Unsupported date diff unit for SQLite: ${unit}`)
    }
  }
 
  /**
   * Build SQLite period series using recursive CTE
   * SQLite 3.8.3+ supports recursive CTEs for generating sequences
   */
  buildPeriodSeriesSubquery(maxPeriod: number): SQL {
    return sql`(
      WITH RECURSIVE periods(period_number) AS (
        SELECT 0
        UNION ALL
        SELECT period_number + 1 FROM periods WHERE period_number < ${maxPeriod}
      )
      SELECT period_number FROM periods
    ) p`
  }
 
  /**
   * Build SQLite time dimension using date/datetime functions with modifiers
   * For integer timestamp columns (milliseconds), first convert to datetime
   * SQLite doesn't have DATE_TRUNC like PostgreSQL, so we use strftime and date modifiers
   * Returns datetime strings for consistency with other databases
   */
  buildTimeDimension(granularity: TimeGranularity, fieldExpr: AnyColumn | SQL): SQL {
    // For SQLite with Drizzle's { mode: 'timestamp' }, timestamps are stored as Unix seconds
    // The datetime() function with 'unixepoch' expects seconds, so no conversion needed
    // For SQLite, we need to apply modifiers directly in the datetime conversion
    // to avoid nested datetime() calls which don't work properly
 
    switch (granularity) {
      case 'year':
        // Start of year: 2023-01-01 00:00:00
        return sql`datetime(${fieldExpr}, 'unixepoch', 'start of year')`
      case 'quarter': {
        // Calculate quarter start date using SQLite's date arithmetic
        // First convert to datetime, then calculate quarter
        const dateForQuarter = sql`datetime(${fieldExpr}, 'unixepoch')`
        return sql`datetime(${dateForQuarter}, 'start of year',
          '+' || (((CAST(strftime('%m', ${dateForQuarter}) AS INTEGER) - 1) / 3) * 3) || ' months')`
      }
      case 'month':
        // Start of month: 2023-05-01 00:00:00
        return sql`datetime(${fieldExpr}, 'unixepoch', 'start of month')`
      case 'week':
        // Start of week (Monday): Use SQLite's weekday modifier
        // weekday 1 = Monday, so go to Monday then back 6 days to get start of week
        return sql`date(datetime(${fieldExpr}, 'unixepoch'), 'weekday 1', '-6 days')`
      case 'day':
        // Start of day: 2023-05-15 00:00:00
        return sql`datetime(${fieldExpr}, 'unixepoch', 'start of day')`
      case 'hour': {
        // Truncate to hour: 2023-05-15 14:00:00
        const dateForHour = sql`datetime(${fieldExpr}, 'unixepoch')`
        return sql`datetime(strftime('%Y-%m-%d %H:00:00', ${dateForHour}))`
      }
      case 'minute': {
        // Truncate to minute: 2023-05-15 14:30:00
        const dateForMinute = sql`datetime(${fieldExpr}, 'unixepoch')`
        return sql`datetime(strftime('%Y-%m-%d %H:%M:00', ${dateForMinute}))`
      }
      case 'second': {
        // Already at second precision: 2023-05-15 14:30:25
        const dateForSecond = sql`datetime(${fieldExpr}, 'unixepoch')`
        return sql`datetime(strftime('%Y-%m-%d %H:%M:%S', ${dateForSecond}))`
      }
      default:
        // Fallback to converting the timestamp to datetime without truncation
        return sql`datetime(${fieldExpr}, 'unixepoch')`
    }
  }
 
  /**
   * Build SQLite string matching conditions using LOWER() + LIKE for case-insensitive matching
   * SQLite LIKE is case-insensitive by default, but LOWER() ensures consistency
   */
  buildStringCondition(fieldExpr: AnyColumn | SQL, operator: 'contains' | 'notContains' | 'startsWith' | 'endsWith' | 'like' | 'notLike' | 'ilike' | 'regex' | 'notRegex', value: string): SQL {
    switch (operator) {
      case 'contains':
        return sql`LOWER(${fieldExpr}) LIKE ${`%${value.toLowerCase()}%`}`
      case 'notContains':
        return sql`LOWER(${fieldExpr}) NOT LIKE ${`%${value.toLowerCase()}%`}`
      case 'startsWith':
        return sql`LOWER(${fieldExpr}) LIKE ${`${value.toLowerCase()}%`}`
      case 'endsWith':
        return sql`LOWER(${fieldExpr}) LIKE ${`%${value.toLowerCase()}`}`
      case 'like':
        return sql`${fieldExpr} LIKE ${value}`
      case 'notLike':
        return sql`${fieldExpr} NOT LIKE ${value}`
      case 'ilike':
        // SQLite doesn't have ILIKE, use LOWER() + LIKE for case-insensitive
        return sql`LOWER(${fieldExpr}) LIKE ${value.toLowerCase()}`
      case 'regex':
        // SQLite regex requires loading extension, use GLOB as fallback
        return sql`${fieldExpr} GLOB ${value}`
      case 'notRegex':
        // SQLite regex requires loading extension, use NOT GLOB as fallback
        return sql`${fieldExpr} NOT GLOB ${value}`
      default:
        throw new Error(`Unsupported string operator: ${operator}`)
    }
  }
 
  /**
   * Build SQLite type casting using CAST() function
   * SQLite has dynamic typing but supports CAST for consistency
   */
  castToType(fieldExpr: AnyColumn | SQL, targetType: 'timestamp' | 'decimal' | 'integer'): SQL {
    switch (targetType) {
      case 'timestamp':
        // For integer timestamp columns, convert to datetime
        // Assumes millisecond Unix timestamps
        return sql`datetime(${fieldExpr} / 1000, 'unixepoch')`
      case 'decimal':
        // Cast to REAL for decimal numbers
        return sql`CAST(${fieldExpr} AS REAL)`
      case 'integer':
        return sql`CAST(${fieldExpr} AS INTEGER)`
      default:
        throw new Error(`Unsupported cast type: ${targetType}`)
    }
  }
 
 
  /**
   * Build SQLite AVG aggregation with IFNULL for NULL handling
   * SQLite AVG returns NULL for empty sets, using IFNULL for consistency
   */
  buildAvg(fieldExpr: AnyColumn | SQL): SQL {
    return sql`IFNULL(AVG(${fieldExpr}), 0)`
  }
 
 
  /**
   * Build SQLite CASE WHEN conditional expression
   */
  buildCaseWhen(conditions: Array<{ when: SQL; then: any }>, elseValue?: any): SQL {
    // Check if 'then' values are SQL objects (they have queryChunks property)
    // If so, we need to treat them as SQL expressions, not bound parameters
    const cases = conditions.map(c => {
      // Check if it's a SQL object by checking for SQL-like properties
      const isSqlObject = c.then && typeof c.then === 'object' && 
                         (c.then.queryChunks || c.then._ || c.then.sql);
      
      if (isSqlObject) {
        // It's a SQL expression, embed it directly without parameterization
        return sql`WHEN ${c.when} THEN ${sql.raw('(') }${c.then}${sql.raw(')')}`
      } else E{
        // It's a regular value, parameterize it
        return sql`WHEN ${c.when} THEN ${c.then}`
      }
    }).reduce((acc, curr) => sql`${acc} ${curr}`)
    
    Iif (elseValue !== undefined) {
      const isElseSqlObject = elseValue && typeof elseValue === 'object' && 
                              (elseValue.queryChunks || elseValue._ || elseValue.sql);
      if (isElseSqlObject) {
        return sql`CASE ${cases} ELSE ${sql.raw('(')}${elseValue}${sql.raw(')')} END`
      } else {
        return sql`CASE ${cases} ELSE ${elseValue} END`
      }
    }
    return sql`CASE ${cases} END`
  }
 
  /**
   * Build SQLite boolean literal
   * SQLite uses 1/0 for true/false
   */
  buildBooleanLiteral(value: boolean): SQL {
    return value ? sql`1` : sql`0`
  }
 
  /**
   * Preprocess calculated measure templates for SQLite-specific handling
   *
   * SQLite performs integer division by default (5/2 = 2 instead of 2.5).
   * This method wraps division operands with CAST to REAL to ensure float division.
   *
   * Pattern matched: {measure1} / {measure2} or {measure1} / NULLIF({measure2}, 0)
   * Transforms to: CAST({measure1} AS REAL) / ...
   *
   * @param calculatedSql - Template string with {member} references
   * @returns Preprocessed template with CAST for division operations
   */
  preprocessCalculatedTemplate(calculatedSql: string): string {
    // Match division patterns: {anything} / {anything} or {anything} / NULLIF(...)
    // We need to cast the numerator to REAL to ensure float division
    // Pattern: captures the opening brace and content before division operator
    const divisionPattern = /(\{[^}]+\})\s*\/\s*/g
 
    return calculatedSql.replace(divisionPattern, (_match, numerator) => {
      // Replace {measure} with CAST({measure} AS REAL)
      const castNumerator = numerator.replace(/\{([^}]+)\}/, 'CAST({$1} AS REAL)')
      return `${castNumerator} / `
    })
  }
 
 
  /**
   * Convert filter values to SQLite-compatible types
   * SQLite doesn't support boolean types - convert boolean to integer (1/0)
   * Convert Date objects to milliseconds for integer timestamp columns
   */
  convertFilterValue(value: any): any {
    if (typeof value === 'boolean') {
      return value ? 1 : 0
    }
    if (value instanceof Date) {
      return value.getTime()
    }
    if (Array.isArray(value)) {
      return value.map(v => this.convertFilterValue(v))
    }
    // If it's already a number (likely already converted timestamp), return as-is
    if (typeof value === 'number') {
      return value
    }    
    return value
  }
 
  /**
   * Prepare date value for SQLite integer timestamp storage
   * Convert Date objects to milliseconds (Unix timestamp * 1000)
   */
  prepareDateValue(date: Date): any {
    if (!(date instanceof Date)) {
      // prepareDateValue called with non-Date value
      // Try to handle it gracefully
      if (typeof date === 'number') return date
      Eif (typeof date === 'string') return new Date(date).getTime()
      throw new Error(`prepareDateValue expects a Date object, got ${typeof date}`)
    }
    return date.getTime()
  }
 
  /**
   * SQLite stores timestamps as integers (milliseconds)
   */
  isTimestampInteger(): boolean {
    return true
  }
 
  /**
   * Convert SQLite time dimension results back to Date objects
   * SQLite time dimensions return datetime strings, but clients expect Date objects
   */
  convertTimeDimensionResult(value: any): any {
    return value
  }
 
  // ============================================
  // Statistical & Window Function Methods
  // ============================================
 
  /**
   * SQLite has limited statistical support (no native STDDEV/VARIANCE/PERCENTILE)
   * but supports window functions since SQLite 3.25
   */
  getCapabilities(): DatabaseCapabilities {
    return {
      supportsStddev: false,      // Requires extension
      supportsVariance: false,    // Requires extension
      supportsPercentile: false,  // Requires extension
      supportsWindowFunctions: true, // SQLite 3.25+
      supportsFrameClause: true,
      supportsLateralJoins: false, // SQLite does not support LATERAL
      supportsPercentileSubqueries: false // No percentile support anyway
    }
  }
 
  /**
   * SQLite does not have native STDDEV
   * Returns null for graceful degradation
   */
  buildStddev(_fieldExpr: AnyColumn | SQL, _useSample = false): SQL | null {
    // SQLite doesn't have native STDDEV functions
    // Return null to trigger graceful degradation
    return null
  }
 
  /**
   * SQLite does not have native VARIANCE
   * Returns null for graceful degradation
   */
  buildVariance(_fieldExpr: AnyColumn | SQL, _useSample = false): SQL | null {
    // SQLite doesn't have native VARIANCE functions
    // Return null to trigger graceful degradation
    return null
  }
 
  /**
   * SQLite does not have native PERCENTILE
   * Returns null for graceful degradation
   */
  buildPercentile(_fieldExpr: AnyColumn | SQL, _percentile: number): SQL | null {
    // SQLite doesn't have native PERCENTILE functions
    // Return null to trigger graceful degradation
    return null
  }
 
  /**
   * Build SQLite window function expression
   * SQLite 3.25+ supports window functions
   */
  buildWindowFunction(
    type: WindowFunctionType,
    fieldExpr: AnyColumn | SQL | null,
    partitionBy?: (AnyColumn | SQL)[],
    orderBy?: Array<{ field: AnyColumn | SQL; direction: 'asc' | 'desc' }>,
    config?: WindowFunctionConfig
  ): SQL {
    // Build OVER clause components
    const partitionClause = partitionBy && partitionBy.length > 0
      ? sql`PARTITION BY ${sql.join(partitionBy, sql`, `)}`
      : sql``
 
    const orderClause = orderBy && orderBy.length > 0
      ? sql`ORDER BY ${sql.join(orderBy.map(o =>
          o.direction === 'desc' ? sql`${o.field} DESC` : sql`${o.field} ASC`
        ), sql`, `)}`
      : sql``
 
    // Build frame clause if specified
    let frameClause = sql``
    if (config?.frame) {
      const { type: frameType, start, end } = config.frame
      const frameTypeStr = frameType.toUpperCase()
 
      const startStr = start === 'unbounded' ? 'UNBOUNDED PRECEDING'
        : typeof start === 'number' ? `${start} PRECEDING`
        : 'CURRENT ROW'
 
      const endStr = end === 'unbounded' ? 'UNBOUNDED FOLLOWING'
        : end === 'current' ? 'CURRENT ROW'
        : typeof end === 'number' ? `${end} FOLLOWING`
        : 'CURRENT ROW'
 
      frameClause = sql`${sql.raw(frameTypeStr)} BETWEEN ${sql.raw(startStr)} AND ${sql.raw(endStr)}`
    }
 
    // Combine OVER clause
    const overParts: SQL[] = []
    Iif (partitionBy && partitionBy.length > 0) overParts.push(partitionClause)
    Eif (orderBy && orderBy.length > 0) overParts.push(orderClause)
    if (config?.frame) overParts.push(frameClause)
 
    const overContent = overParts.length > 0 ? sql.join(overParts, sql` `) : sql``
    const over = sql`OVER (${overContent})`
 
    // Build the window function based on type
    switch (type) {
      case 'lag':
        return sql`LAG(${fieldExpr}, ${config?.offset ?? 1}${config?.defaultValue !== undefined ? sql`, ${config.defaultValue}` : sql``}) ${over}`
      case 'lead':
        return sql`LEAD(${fieldExpr}, ${config?.offset ?? 1}${config?.defaultValue !== undefined ? sql`, ${config.defaultValue}` : sql``}) ${over}`
      case 'rank':
        return sql`RANK() ${over}`
      case 'denseRank':
        return sql`DENSE_RANK() ${over}`
      case 'rowNumber':
        return sql`ROW_NUMBER() ${over}`
      case 'ntile':
        return sql`NTILE(${config?.nTile ?? 4}) ${over}`
      case 'firstValue':
        return sql`FIRST_VALUE(${fieldExpr}) ${over}`
      case 'lastValue':
        return sql`LAST_VALUE(${fieldExpr}) ${over}`
      case 'movingAvg':
        return sql`AVG(${fieldExpr}) ${over}`
      case 'movingSum':
        return sql`SUM(${fieldExpr}) ${over}`
      default:
        throw new Error(`Unsupported window function: ${type}`)
    }
  }
}