|
| 1 | +import path from "path"; |
| 2 | +import {Command} from "commander"; |
| 3 | +import {z} from 'zod'; |
| 4 | +import {BenchmarkRunner, ROOT, runBenchmark, TableSpec} from "./@bench-common"; |
| 5 | + |
| 6 | +// Remember to port-forward the Spark HTTP server with |
| 7 | +// aws ssm start-session --target {host-id} --document-name AWS-StartPortForwardingSession --parameters "portNumber=9003,localPortNumber=9003" |
| 8 | + |
| 9 | +async function main() { |
| 10 | + const program = new Command(); |
| 11 | + |
| 12 | + program |
| 13 | + .option('--dataset <string>', 'Dataset to run queries on') |
| 14 | + .option('-i, --iterations <number>', 'Number of iterations', '3') |
| 15 | + .option('--query <number>', 'A specific query to run', undefined) |
| 16 | + .parse(process.argv); |
| 17 | + |
| 18 | + const options = program.opts(); |
| 19 | + |
| 20 | + const dataset: string = options.dataset |
| 21 | + const iterations = parseInt(options.iterations); |
| 22 | + const queries = options.query ? [parseInt(options.query)] : []; |
| 23 | + |
| 24 | + const runner = new SparkRunner({}); |
| 25 | + |
| 26 | + const datasetPath = path.join(ROOT, "benchmarks", "data", dataset); |
| 27 | + const outputPath = path.join(datasetPath, "remote-results.json") |
| 28 | + |
| 29 | + await runBenchmark(runner, { |
| 30 | + dataset, |
| 31 | + iterations, |
| 32 | + queries, |
| 33 | + outputPath, |
| 34 | + }); |
| 35 | +} |
| 36 | + |
| 37 | +const QueryResponse = z.object({ |
| 38 | + count: z.number() |
| 39 | +}) |
| 40 | +type QueryResponse = z.infer<typeof QueryResponse> |
| 41 | + |
| 42 | +class SparkRunner implements BenchmarkRunner { |
| 43 | + private url = 'http://localhost:9003'; |
| 44 | + |
| 45 | + constructor(private readonly options: {}) { |
| 46 | + } |
| 47 | + |
| 48 | + async executeQuery(sql: string): Promise<{ rowCount: number }> { |
| 49 | + // Fix TPCH query 4: Add DATE prefix to date literals |
| 50 | + sql = sql.replace(/(?<!date\s)('[\d]{4}-[\d]{2}-[\d]{2}')/gi, 'DATE $1'); |
| 51 | + |
| 52 | + // Fix ClickBench queries: Spark uses from_unixtime |
| 53 | + sql = sql.replace(/to_timestamp_seconds\(/gi, 'from_unixtime('); |
| 54 | + |
| 55 | + let response |
| 56 | + if (sql.includes("create view")) { |
| 57 | + // Query 15 |
| 58 | + let [createView, query, dropView] = sql.split(";") |
| 59 | + await this.query(createView); |
| 60 | + response = await this.query(query) |
| 61 | + await this.query(dropView); |
| 62 | + } else { |
| 63 | + response = await this.query(sql) |
| 64 | + } |
| 65 | + |
| 66 | + return { rowCount: response.count }; |
| 67 | + } |
| 68 | + |
| 69 | + private async query(sql: string): Promise<QueryResponse> { |
| 70 | + const response = await fetch(`${this.url}/query`, { |
| 71 | + method: 'POST', |
| 72 | + headers: { |
| 73 | + 'Content-Type': 'application/json', |
| 74 | + }, |
| 75 | + body: JSON.stringify({ |
| 76 | + query: sql.trim().replace(/;+$/, '') |
| 77 | + }) |
| 78 | + }); |
| 79 | + |
| 80 | + if (!response.ok) { |
| 81 | + const msg = await response.text(); |
| 82 | + throw new Error(`Query failed: ${response.status} ${msg}`); |
| 83 | + } |
| 84 | + |
| 85 | + return QueryResponse.parse(await response.json()); |
| 86 | + } |
| 87 | + |
| 88 | + async createTables(tables: TableSpec[]): Promise<void> { |
| 89 | + for (const table of tables) { |
| 90 | + // Spark requires s3a:// protocol, not s3:// |
| 91 | + const s3aPath = table.s3Path.replace('s3://', 's3a://'); |
| 92 | + |
| 93 | + // Create temporary view from Parquet files |
| 94 | + const createViewStmt = ` |
| 95 | + CREATE OR REPLACE TEMPORARY VIEW ${table.name} |
| 96 | + USING parquet |
| 97 | + OPTIONS (path '${s3aPath}') |
| 98 | + `; |
| 99 | + await this.query(createViewStmt); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | +} |
| 104 | + |
| 105 | +main() |
| 106 | + .catch(err => { |
| 107 | + console.error(err) |
| 108 | + process.exit(1) |
| 109 | + }) |
0 commit comments