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 | 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 16x 16x 2x 2x 12x 12x 12x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 1x 1x 1x 1x 3x 2x 1x 1x 2x 3x 1x 1x | /**
* MySQL Database Adapter
* Implements MySQL-specific SQL generation for time dimensions, string matching, and type casting
* Provides MySQL equivalents to PostgreSQL functions
*/
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 MySQLAdapter extends BaseDatabaseAdapter {
getEngineType(): 'mysql' | 'singlestore' {
return 'mysql'
}
/**
* MySQL supports LATERAL joins since version 8.0.14
*/
supportsLateralJoins(): boolean {
return true
}
// ============================================
// Funnel Analysis Methods
// ============================================
/**
* Build MySQL INTERVAL from ISO 8601 duration
* MySQL uses DATE_ADD with INTERVAL syntax but intervals must be added separately
* For simplicity, we convert to seconds for consistent handling
*/
buildIntervalFromISO(duration: string): SQL {
const parsed = this.parseISODuration(duration)
const parts: string[] = []
// MySQL allows multiple interval additions but for simplicity convert to a single unit
// We'll use the most significant unit
Iif (parsed.years) parts.push(`${parsed.years} YEAR`)
Iif (parsed.months) parts.push(`${parsed.months} MONTH`)
Eif (parsed.days) parts.push(`${parsed.days} DAY`)
Iif (parsed.hours) parts.push(`${parsed.hours} HOUR`)
Iif (parsed.minutes) parts.push(`${parsed.minutes} MINUTE`)
Iif (parsed.seconds) parts.push(`${parsed.seconds} SECOND`)
// For MySQL, return the interval as seconds for consistent arithmetic
const totalSeconds = this.durationToSeconds(duration)
return sql`${totalSeconds}`
}
/**
* Build MySQL time difference in seconds using TIMESTAMPDIFF
* Returns (end - start) as seconds
*/
buildTimeDifferenceSeconds(end: SQL, start: SQL): SQL {
return sql`TIMESTAMPDIFF(SECOND, ${start}, ${end})`
}
/**
* Build MySQL timestamp + interval expression
* Uses DATE_ADD function
*/
buildDateAddInterval(timestamp: SQL, duration: string): SQL {
const parsed = this.parseISODuration(duration)
// MySQL DATE_ADD supports multiple interval additions
// Build a chain of DATE_ADD calls for each component
let result: SQL = timestamp
Iif (parsed.years) result = sql`DATE_ADD(${result}, INTERVAL ${parsed.years} YEAR)`
Iif (parsed.months) result = sql`DATE_ADD(${result}, INTERVAL ${parsed.months} MONTH)`
Eif (parsed.days) result = sql`DATE_ADD(${result}, INTERVAL ${parsed.days} DAY)`
Iif (parsed.hours) result = sql`DATE_ADD(${result}, INTERVAL ${parsed.hours} HOUR)`
Iif (parsed.minutes) result = sql`DATE_ADD(${result}, INTERVAL ${parsed.minutes} MINUTE)`
Iif (parsed.seconds) result = sql`DATE_ADD(${result}, INTERVAL ${parsed.seconds} SECOND)`
return result
}
/**
* Build MySQL conditional aggregation using CASE WHEN
* MySQL 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()
if (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 MySQL date difference in periods using TIMESTAMPDIFF
* For retention analysis period calculations
*/
buildDateDiffPeriods(startDate: SQL, endDate: SQL, unit: 'day' | 'week' | 'month'): SQL {
// MySQL TIMESTAMPDIFF returns the number of intervals between two dates
const mysqlUnit = unit.toUpperCase()
return sql`TIMESTAMPDIFF(${sql.raw(mysqlUnit)}, ${startDate}, ${endDate})`
}
/**
* Build MySQL period series using recursive CTE
* MySQL 8.0+ 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 MySQL time dimension using DATE_FORMAT function
* MySQL equivalent to PostgreSQL's DATE_TRUNC
*/
buildTimeDimension(granularity: TimeGranularity, fieldExpr: AnyColumn | SQL): SQL {
// MySQL uses DATE_FORMAT with specific format strings for truncation
const formatMap: Record<TimeGranularity, string> = {
year: '%Y-01-01 00:00:00',
quarter: '%Y-%q-01 00:00:00', // %q gives quarter (1,2,3,4), but we need to map this properly
month: '%Y-%m-01 00:00:00',
week: '%Y-%u-01 00:00:00', // %u gives week of year
day: '%Y-%m-%d 00:00:00',
hour: '%Y-%m-%d %H:00:00',
minute: '%Y-%m-%d %H:%i:00',
second: '%Y-%m-%d %H:%i:%s'
}
// Special handling for quarter and week since MySQL doesn't have direct equivalents to PostgreSQL
switch (granularity) {
case 'quarter':
// Calculate quarter start date using QUARTER() function
return sql`DATE_ADD(MAKEDATE(YEAR(${fieldExpr}), 1), INTERVAL (QUARTER(${fieldExpr}) - 1) * 3 MONTH)`
case 'week':
// Get start of week (Monday) using MySQL's week functions
return sql`DATE_SUB(${fieldExpr}, INTERVAL WEEKDAY(${fieldExpr}) DAY)`
default: {
const format = formatMap[granularity]
Iif (!format) {
// Fallback to original expression if granularity not recognized
return fieldExpr as SQL
}
return sql`STR_TO_DATE(DATE_FORMAT(${fieldExpr}, ${format}), '%Y-%m-%d %H:%i:%s')`
}
}
}
/**
* Build MySQL string matching conditions using LIKE
* MySQL LIKE is case-insensitive by default (depending on collation)
* For guaranteed case-insensitive matching, we use LOWER() functions
*/
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':
// MySQL doesn't have ILIKE, use LOWER() + LIKE for case-insensitive
return sql`LOWER(${fieldExpr}) LIKE ${value.toLowerCase()}`
case 'regex':
return sql`${fieldExpr} REGEXP ${value}`
case 'notRegex':
return sql`${fieldExpr} NOT REGEXP ${value}`
default:
throw new Error(`Unsupported string operator: ${operator}`)
}
}
/**
* Build MySQL type casting using CAST() function
* MySQL equivalent to PostgreSQL's :: casting syntax
*/
castToType(fieldExpr: AnyColumn | SQL, targetType: 'timestamp' | 'decimal' | 'integer'): SQL {
switch (targetType) {
case 'timestamp':
return sql`CAST(${fieldExpr} AS DATETIME)`
case 'decimal':
return sql`CAST(${fieldExpr} AS DECIMAL(10,2))`
case 'integer':
return sql`CAST(${fieldExpr} AS SIGNED INTEGER)`
default:
throw new Error(`Unsupported cast type: ${targetType}`)
}
}
/**
* Build MySQL AVG aggregation with IFNULL for NULL handling
* MySQL AVG returns NULL for empty sets, using IFNULL for consistency
*/
buildAvg(fieldExpr: AnyColumn | SQL): SQL {
return sql`IFNULL(AVG(${fieldExpr}), 0)`
}
/**
* Build MySQL CASE WHEN conditional expression
*/
buildCaseWhen(conditions: Array<{ when: SQL; then: any }>, elseValue?: any): SQL {
const cases = conditions.map(c => sql`WHEN ${c.when} THEN ${c.then}`).reduce((acc, curr) => sql`${acc} ${curr}`)
if (elseValue !== undefined) {
return sql`CASE ${cases} ELSE ${elseValue} END`
}
return sql`CASE ${cases} END`
}
/**
* Build MySQL boolean literal
* MySQL uses TRUE/FALSE keywords (equivalent to 1/0)
*/
buildBooleanLiteral(value: boolean): SQL {
return value ? sql`TRUE` : sql`FALSE`
}
/**
* Convert filter values - MySQL uses native types
* No conversion needed for MySQL
*/
convertFilterValue(value: any): any {
return value
}
/**
* Prepare date value for MySQL
* MySQL accepts Date objects directly
*/
prepareDateValue(date: Date): any {
return date
}
/**
* MySQL stores timestamps as native timestamp types
*/
isTimestampInteger(): boolean {
return false
}
/**
* MySQL time dimensions already return proper values
* No conversion needed
*/
convertTimeDimensionResult(value: any): any {
return value
}
// ============================================
// Statistical & Window Function Methods
// ============================================
/**
* MySQL 8.0+ has support for statistical and window functions
* but not PERCENTILE_CONT
*/
getCapabilities(): DatabaseCapabilities {
return {
supportsStddev: true,
supportsVariance: true,
supportsPercentile: false, // MySQL doesn't have PERCENTILE_CONT
supportsWindowFunctions: true,
supportsFrameClause: true,
supportsLateralJoins: true, // MySQL 8.0.14+
supportsPercentileSubqueries: false // No percentile support anyway
}
}
/**
* Build MySQL STDDEV aggregation
* Uses STDDEV_POP for population, STDDEV_SAMP for sample
*/
buildStddev(fieldExpr: AnyColumn | SQL, useSample = false): SQL {
const fn = useSample ? 'STDDEV_SAMP' : 'STDDEV_POP'
return sql`IFNULL(${sql.raw(fn)}(${fieldExpr}), 0)`
}
/**
* Build MySQL VARIANCE aggregation
* Uses VAR_POP for population, VAR_SAMP for sample
*/
buildVariance(fieldExpr: AnyColumn | SQL, useSample = false): SQL {
const fn = useSample ? 'VAR_SAMP' : 'VAR_POP'
return sql`IFNULL(${sql.raw(fn)}(${fieldExpr}), 0)`
}
/**
* MySQL does not support PERCENTILE_CONT
* Returns null for graceful degradation
*/
buildPercentile(_fieldExpr: AnyColumn | SQL, _percentile: number): SQL | null {
// MySQL doesn't have native PERCENTILE_CONT
// Return null to trigger graceful degradation
return null
}
/**
* Build MySQL window function expression
* MySQL 8.0+ has full window function support
*/
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[] = []
if (partitionBy && partitionBy.length > 0) overParts.push(partitionClause)
if (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}`)
}
}
} |