| |
| import re |
| from pyspark.sql import SparkSession |
|
|
| spark = SparkSession.builder \ |
| .appName('prestosql_bench_init') \ |
| .enableHiveSupport() \ |
| .config('spark.sql.warehouse.dir', '/tmp/hive_warehouse') \ |
| .getOrCreate() |
|
|
| spark.sql('CREATE DATABASE IF NOT EXISTS internal_platform_db') |
|
|
|
|
| def _execute_sql_file(spark, sql_path): |
| """Read SQL file, remove SuperSQL SET headers, split by semicolons, execute.""" |
| with open(sql_path, 'r', encoding='utf-8') as f: |
| content = f.read() |
| content = re.sub(r'^\s*set\s+query_engine\.\S+\n?', '', content, flags=re.IGNORECASE) |
| stmts, cur, in_sq, in_dq, i = [], [], False, False, 0 |
| while i < len(content): |
| ch = content[i] |
| if ch == '\\' and i + 1 < len(content): |
| cur.append(ch); cur.append(content[i + 1]); i += 2; continue |
| if ch == '-' and i + 1 < len(content) and content[i + 1] == '-' and not in_sq and not in_dq: |
| while i < len(content) and content[i] != '\n': |
| i += 1 |
| cur.append('\n'); continue |
| if ch == "'" and not in_dq: |
| in_sq = not in_sq |
| elif ch == '"' and not in_sq: |
| in_dq = not in_dq |
| if ch == ';' and not in_sq and not in_dq: |
| s = ''.join(cur).strip() |
| if s: |
| stmts.append(s) |
| cur = [] |
| else: |
| cur.append(ch) |
| i += 1 |
| last = ''.join(cur).strip() |
| if last: |
| stmts.append(last) |
| for stmt in stmts: |
| spark.sql(stmt) |
|
|
|
|
| _execute_sql_file(spark, '/tmp_workspace/init_db.sql') |
|
|
| tables = spark.sql('SHOW TABLES IN internal_platform_db').collect() |
| print(f'Init complete, {len(tables)} tables created') |
| for t in tables: |
| print(f' - {t.namespace}.{t.tableName}') |
| spark.stop() |
|
|