All files / server/executors duckdb-executor.ts

100% Statements 98/98
85.85% Branches 85/99
100% Functions 13/13
100% Lines 86/86

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                            662x   662x 650x 650x 635x 34722x   1x     15x 15x         15x           12x 1x     11x 11x     6x 9x     1x     5x 5x         5x               20x   20x 18x 18x     2x 1x 1x 1x 1x       1x   1x               34731x   34727x 34727x     179275x 103066x   76209x     34727x               103091x     102978x     63972x     28x   6x 6x 6x 4x         2x       22x   22x 14x       8x 6x         2x       20x                         8x     8x 1x         7x   7x 7x   7x 6x 4x 3x   2x         7x 7x 7x 6x     6x         6x 6x             7x               9x 2x     7x 1x     6x   7x   6x                         5x 1x     6x               1x 1x                       235x    
/**
 * DuckDB database executor
 * Works with drizzle-duckdb drivers
 */
 
import type { SQL } from 'drizzle-orm'
import { sql } from 'drizzle-orm'
import type { DrizzleDatabase, ExplainOptions, ExplainResult, IndexInfo } from '../types'
import { BaseDatabaseExecutor } from './base-executor'
import { parseDuckDBExplain } from '../explain/duckdb-parser'
 
export class DuckDBExecutor extends BaseDatabaseExecutor {
  async execute<T = any[]>(query: SQL | any, numericFields?: string[]): Promise<T> {
    // Handle Drizzle query objects directly
    Eif (query && typeof query === 'object') {
      // Check for various execution methods that Drizzle queries might have
      if (typeof query.execute === 'function') {
        try {
          const result = await query.execute()
          if (Array.isArray(result)) {
            return result.map(row => this.convertNumericFields(row, numericFields)) as T
          }
          return result as T
        } catch (err) {
          // Extract SQL for better error logging
          const sqlInfo = this.extractSqlFromQuery(query)
          console.error('[DuckDB] Query execution failed:', {
            error: err instanceof Error ? err.message : String(err),
            sql: sqlInfo.sql,
            params: sqlInfo.params,
          })
          throw err
        }
      }
    }
 
    // Handle raw SQL objects
    if (!this.db.execute) {
      throw new Error('DuckDB database instance must have an execute method')
    }
 
    try {
      const result = await this.db.execute(query)
 
      // Convert numeric strings to numbers for DuckDB results
      if (Array.isArray(result)) {
        return result.map(row => this.convertNumericFields(row, numericFields)) as T
      }
 
      return result as T
    } catch (err) {
      // Extract SQL for better error logging
      const sqlInfo = this.extractSqlFromQuery(query)
      console.error('[DuckDB] Query execution failed:', {
        error: err instanceof Error ? err.message : String(err),
        sql: sqlInfo.sql,
        params: sqlInfo.params,
      })
      throw err
    }
  }
 
  /**
   * Extract SQL string and params from a query object for error logging
   */
  private extractSqlFromQuery(query: any): { sql: string; params: unknown[] } {
    try {
      // Drizzle SQL objects have toSQL() method
      if (query && typeof query.toSQL === 'function') {
        const { sql: sqlStr, params } = query.toSQL()
        return { sql: sqlStr, params }
      }
      // Try getSQL for older Drizzle versions
      if (query && typeof query.getSQL === 'function') {
        const sqlObj = query.getSQL()
        Eif (sqlObj && typeof sqlObj.toSQL === 'function') {
          const { sql: sqlStr, params } = sqlObj.toSQL()
          return { sql: sqlStr, params }
        }
      }
      // Fallback: stringify the query
      return { sql: String(query), params: [] }
    } catch {
      return { sql: '[unable to extract SQL]', params: [] }
    }
  }
 
  /**
   * Convert numeric string fields to numbers (only for measure fields)
   */
  private convertNumericFields(row: any, numericFields?: string[]): any {
    if (!row || typeof row !== 'object') return row
 
    const converted: any = {}
    for (const [key, value] of Object.entries(row)) {
      // Only convert measure fields to numbers
      // Dimensions and time dimensions should keep their original types
      if (numericFields && numericFields.includes(key)) {
        converted[key] = this.coerceToNumber(value)
      } else {
        converted[key] = value
      }
    }
    return converted
  }
 
  /**
   * Coerce a value to a number if it represents a numeric type
   */
  private coerceToNumber(value: any): any {
    // Handle null/undefined - preserve null values for aggregations
    if (value == null) return value
 
    // Already a number
    if (typeof value === 'number') return value
 
    // Handle BigInt values from COUNT operations
    if (typeof value === 'bigint') return Number(value)
 
    // Handle DuckDB-specific types
    if (value && typeof value === 'object') {
      // Check if it has a numeric toString() method
      Eif (typeof value.toString === 'function') {
        const stringValue = value.toString()
        if (/^-?\d+(\.\d+)?$/.test(stringValue)) {
          return stringValue.includes('.') ? parseFloat(stringValue) : parseInt(stringValue, 10)
        }
      }
 
      // If it's an object but doesn't look numeric, return as-is
      return value
    }
 
    // Handle string representations of numbers
    Eif (typeof value === 'string') {
      // Check for exact numeric strings
      if (/^-?\d+(\.\d+)?$/.test(value)) {
        return value.includes('.') ? parseFloat(value) : parseInt(value, 10)
      }
 
      // Check for other numeric formats (scientific notation, etc.)
      if (!isNaN(parseFloat(value)) && isFinite(parseFloat(value))) {
        return parseFloat(value)
      }
    }
 
    // Return as-is for non-numeric values
    return value
  }
 
  getEngineType(): 'duckdb' {
    return 'duckdb'
  }
 
  /**
   * Execute EXPLAIN on a SQL query to get the execution plan
   * DuckDB supports EXPLAIN and EXPLAIN ANALYZE
   */
  async explainQuery(
    sqlString: string,
    params: unknown[],
    options?: ExplainOptions
  ): Promise<ExplainResult> {
    // Build EXPLAIN command
    const explainPrefix = options?.analyze ? 'EXPLAIN ANALYZE' : 'EXPLAIN'
 
    // Execute EXPLAIN with parameters
    if (!this.db.execute) {
      throw new Error('DuckDB database instance must have an execute method')
    }
 
    // For DuckDB, we need to replace parameters with values
    // DuckDB uses $1, $2 style placeholders like PostgreSQL
    const result = await this.db.execute(
      sql`${sql.raw(explainPrefix)} ${sql.raw(sqlString.replace(/\$(\d+)/g, (_, n) => {
        const paramIndex = parseInt(n, 10) - 1
        const value = params[paramIndex]
        // Escape and quote the value appropriately
        if (value === null) return 'NULL'
        if (typeof value === 'number') return String(value)
        if (typeof value === 'boolean') return value ? 'TRUE' : 'FALSE'
        if (value instanceof Date) return `'${value.toISOString()}'`
        // String: escape single quotes
        return `'${String(value).replace(/'/g, "''")}'`
      }))}`
    )
 
    // DuckDB returns EXPLAIN output as rows
    const rawLines: string[] = []
    Eif (Array.isArray(result)) {
      for (const row of result) {
        Eif (row && typeof row === 'object') {
          // DuckDB might return different column names for EXPLAIN output
          const planLine =
            (row as Record<string, unknown>)['explain_value'] ||
            (row as Record<string, unknown>)['QUERY PLAN'] ||
            (row as Record<string, unknown>)['query_plan'] ||
            (row as Record<string, unknown>)['Plan'] ||
            Object.values(row as Record<string, unknown>)[0]
          Eif (typeof planLine === 'string') {
            rawLines.push(planLine)
          }
        }
      }
    }
 
    // Parse the output using the DuckDB parser
    return parseDuckDBExplain(rawLines, { sql: sqlString, params })
  }
 
  /**
   * Get existing indexes for the specified tables
   * DuckDB uses duckdb_indexes() table function
   */
  async getTableIndexes(tableNames: string[]): Promise<IndexInfo[]> {
    if (!tableNames || tableNames.length === 0) {
      return []
    }
 
    if (!this.db.execute) {
      throw new Error('DuckDB database instance must have an execute method')
    }
 
    try {
      // Build table list for SQL IN clause
      const tableList = tableNames.map(t => `'${t.toLowerCase()}'`).join(',')
 
      const result = await this.db.execute(sql`
        SELECT
          table_name,
          index_name,
          LISTAGG(column_name, ',') WITHIN GROUP (ORDER BY index_oid) as columns,
          is_unique,
          is_primary
        FROM duckdb_indexes()
        WHERE LOWER(table_name) IN (${sql.raw(tableList)})
        GROUP BY table_name, index_name, is_unique, is_primary
        ORDER BY table_name, index_name
      `)
 
      if (!Array.isArray(result)) {
        return []
      }
 
      return result.map((row: any) => ({
        table_name: row.table_name,
        index_name: row.index_name,
        columns: typeof row.columns === 'string' ? row.columns.split(',') : [],
        is_unique: Boolean(row.is_unique),
        is_primary: Boolean(row.is_primary)
      }))
    } catch (err) {
      console.warn('Failed to get table indexes:', err)
      return []
    }
  }
}
 
/**
 * Factory function for creating DuckDB executors
 */
export function createDuckDBExecutor(
  db: DrizzleDatabase,
  schema?: any
): DuckDBExecutor {
  return new DuckDBExecutor(db, schema, 'duckdb')
}