-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathread_schema.py
More file actions
71 lines (62 loc) · 2.3 KB
/
read_schema.py
File metadata and controls
71 lines (62 loc) · 2.3 KB
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
import os
import tempfile
import time
import duckdb
dbFile = os.getenv('DBFILE', None)
if dbFile is None:
print("Error, Please provide a database file.")
exit(1)
file = os.getenv('FILE', None)
if file is None:
print("Error, Please provide a file.")
exit(1)
if not os.path.isabs(file):
workspace_file = os.environ['GPTSCRIPT_WORKSPACE_DIR'] + '/' + file
if os.path.exists(workspace_file):
file = workspace_file
else:
pwd_file = os.getcwd() + '/' + file
if os.path.exists(pwd_file):
file = pwd_file
if not os.path.exists(file):
print("Error, File does not exist.")
exit(1)
if not os.path.isabs(dbFile):
dbFile = os.environ['GPTSCRIPT_WORKSPACE_DIR'] + '/' + dbFile
max_retries = 10
attempts = 0
success = False
while not success and attempts < max_retries:
try:
cursor = duckdb.connect(database=dbFile, config={'temp_directory': tempfile.gettempdir()})
success = True
except:
time.sleep(1)
attempts += 1
if success:
filename = os.path.basename(file)
_, file_extension = os.path.splitext(filename)
table_name = os.path.basename(dbFile)
table_name, _ = os.path.splitext(table_name)
if file_extension == '.csv':
load_query = f"CREATE TABLE IF NOT EXISTS {table_name} AS SELECT * FROM read_csv('{file}', escape = '\', header = true);"
elif file_extension == '.xlsx':
cursor.install_extension("spatial")
cursor.load_extension("spatial")
load_query = f"CREATE TABLE IF NOT EXISTS {table_name} AS SELECT * FROM st_read('{file}', open_options=['HEADERS=FORCE']);"
elif file_extension == '.jsonl' or file_extension == '.ndjson' or file_extension == '.json':
load_query = f"CREATE TABLE IF NOT EXISTS {table_name} AS SELECT * FROM read_json_auto('{file}');"
else:
print("Error, Unsupported file type. Please provide a .json, .ndjson, .jsonl, .csv, or .xlsx file.")
exit(1)
cursor.execute(load_query)
try:
schema_query = f"PRAGMA table_info('{table_name}');"
print(f"Schema for table '{table_name}':")
print(cursor.sql(schema_query).show(max_rows=10000000, max_width=10000000))
except Exception as e:
print(f"Failed to read schema: {e}")
exit(1)
else:
print("Failed to connect to database")
exit(1)