Datasets:
File size: 1,788 Bytes
e8c001c | 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 | #!/usr/bin/env python3
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()
|