Repository navigation
Expand file tree
/
Copy pathduckdb_engine.py
More file actions
289 lines (232 loc) · 9.63 KB
/
Copy pathduckdb_engine.py
File metadata and controls
289 lines (232 loc) · 9.63 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
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
# duckdb_engine.py
"""
Движок для больших данных на DuckDB.
"""
import os
import tempfile
import duckdb
DUCKDB_THRESHOLD = 250 * 1024 * 1024 # 250 МБ
MAX_PRINT_ROWS = 20
MAX_SHOW_ROWS = 1000
# ============================================================
# ГЛОБАЛЬНОЕ СОЕДИНЕНИЕ
# ============================================================
# Все DuckDBTable используют ОДНО соединение, чтобы JOIN, UNPIVOT
# и другие операции могли ссылаться на таблицы друг друга по имени.
_GLOBAL_CON = None
def get_global_connection():
"""Возвращает глобальное соединение DuckDB (создаёт при первом вызове)."""
global _GLOBAL_CON
if _GLOBAL_CON is None:
_GLOBAL_CON = duckdb.connect()
return _GLOBAL_CON
def should_use_duckdb(path):
if not os.path.exists(path):
return False
return os.path.getsize(path) >= DUCKDB_THRESHOLD
def _detect_encoding(path):
"""Определяет кодировку CSV по первой строке."""
for enc in ['utf-8', 'cp1251', 'utf-16', 'latin-1', 'koi8-r']:
try:
with open(path, 'r', encoding=enc) as f:
f.readline()
return enc
except (UnicodeDecodeError, LookupError):
continue
return 'utf-8'
def _ensure_utf8(path):
"""Проверяет, что файл в UTF-8. Если нет — создаёт временную копию."""
encoding = _detect_encoding(path)
if encoding == 'utf-8':
return path, False
try:
with open(path, 'r', encoding=encoding, errors='replace') as f:
content = f.read()
except Exception as e:
raise RuntimeError(f"Не удалось прочитать файл как {encoding}: {e}")
tmp = tempfile.NamedTemporaryFile(
mode='w',
suffix='.utf8.csv',
delete=False,
encoding='utf-8',
newline='',
)
tmp.write(content)
tmp.close()
return tmp.name, True
class DuckDBTable:
def __init__(self, path, table_name=None, connection=None):
self.path = path
self.table_name = table_name or self._make_table_name(path)
# ============================================================
# ВАЖНО: используем ГЛОБАЛЬНОЕ соединение,
# чтобы все таблицы были видны друг другу (JOIN, UNPIVOT).
# ============================================================
self.con = connection or get_global_connection()
self.utf8_path, self.is_temp = _ensure_utf8(path)
self._tmp_path = self.utf8_path if self.is_temp else None
self._register()
def _make_table_name(self, path):
base = os.path.basename(path).replace('.', '_').replace(' ', '_')
clean = ''.join(c if c.isalnum() or c == '_' else '_' for c in base)
return f"t_{clean}"
def _detect_delimiter(self):
try:
with open(self.utf8_path, 'r', encoding='utf-8', errors='replace') as f:
first_line = f.readline()
candidates = [',', ';', '\t', '|']
counts = {d: first_line.count(d) for d in candidates}
best = max(counts, key=counts.get)
if counts[best] == 0:
return ','
return best
except Exception:
return ','
def _register(self):
"""
Регистрирует view на CSV-файл.
ВАЖНО:
sample_size=20000 — читать первые 20000 строк для детекции типов
ignore_errors=false — падать при ошибке парсинга, а не молча терять строки
new_line — НЕ указываем, DuckDB сам определяет \\n / \\r\\n / \\r
"""
safe_path = self.utf8_path.replace("'", "''").replace("\\", "\\\\")
delimiter = self._detect_delimiter()
if delimiter == '\t':
delim_sql = '\\t'
else:
delim_sql = delimiter
self.con.execute(f"""
CREATE OR REPLACE VIEW {self.table_name} AS
SELECT * FROM read_csv_auto('{safe_path}',
sample_size=20000,
ignore_errors=false,
header=true,
delim='{delim_sql}')
""")
# ============================================================
# ПОДСЧЁТ СТРОК
# ============================================================
def _count_data(self):
"""
Количество ДАННЫХ (без заголовка).
Для внутренних нужд движка (LIMIT, превью и т.п.).
"""
return self.con.execute(
f"SELECT COUNT(*) FROM {self.table_name}"
).fetchone()[0]
def get_row_count(self):
"""
Количество строк ВКЛЮЧАЯ ЗАГОЛОВОК.
Согласовано с Matrix: lenrow(m) = 5 для 4 строк данных.
В Matrix data[0] — заголовок, поэтому lenrow = 5.
В DuckDB view содержит только данные, поэтому +1.
"""
return self._count_data() + 1
# ============================================================
# ДОСТУП К ДАННЫМ
# ============================================================
def get_columns(self):
result = self.con.execute(
f"SELECT * FROM {self.table_name} LIMIT 0"
).description
return [col[0] for col in result]
def query(self, sql):
return self.con.execute(sql).fetchall()
def query_to_matrix(self, sql):
from runtime.matrix import MatrExMatrix
rows = self.con.execute(sql).fetchall()
data = [list(row) for row in rows]
return MatrExMatrix(data, True)
def get_data_for_show(self, limit=MAX_SHOW_ROWS):
try:
data = self.query(
f"SELECT * FROM {self.table_name} LIMIT {limit}"
)
return [list(row) for row in data]
except Exception as e:
print(f"DuckDB get_data_for_show error: {e}")
return [[""]]
def get_headers(self):
return self.get_columns()
def get_column_type(self, col_name):
try:
safe_col = '"' + str(col_name).replace('"', '""') + '"'
result = self.con.execute(
f'SELECT typeof({safe_col}) FROM {self.table_name} LIMIT 1'
).fetchone()
return result[0] if result else 'VARCHAR'
except Exception:
return 'VARCHAR'
def close(self):
# ============================================================
# Не закрываем глобальное соединение —
# оно общее для всех таблиц.
# ============================================================
try:
if self.con is not None and self.con is not get_global_connection():
self.con.close()
except Exception:
pass
self.con = None
if self._tmp_path and os.path.exists(self._tmp_path):
try:
os.remove(self._tmp_path)
except Exception:
pass
self._tmp_path = None
def __str__(self):
try:
# Внутренний подсчёт использует _count_data (без заголовка)
data_rows = self._count_data()
cols = len(self.get_columns())
columns = self.get_columns()
preview_rows = min(MAX_PRINT_ROWS, data_rows)
data = self.query(
f"SELECT * FROM {self.table_name} LIMIT {preview_rows}"
)
lines = self._format_table(columns, data)
if data_rows > MAX_PRINT_ROWS:
lines.append(f"... (всего {data_rows} строк × {cols} столбцов)")
else:
lines.append(f"({data_rows} строк × {cols} столбцов)")
return "\n".join(lines)
except Exception as e:
import traceback
return f"DuckDBTable({self.table_name}, ошибка: {e})\n{traceback.format_exc()}"
def _format_table(self, columns, rows):
if not rows:
return ["(пусто)"]
widths = []
for i, col in enumerate(columns):
max_width = len(str(col))
for row in rows:
if i < len(row):
max_width = max(max_width, len(str(row[i])))
widths.append(max_width)
lines = []
header = " ".join(
str(col).ljust(widths[i])
for i, col in enumerate(columns)
)
lines.append(header)
lines.append("-" * len(header))
for row in rows:
line = " ".join(
str(row[i] if i < len(row) else "").ljust(widths[i])
for i in range(len(columns))
)
lines.append(line)
return lines
def __repr__(self):
return self.__str__()
def escape_identifier(name):
return '"' + str(name).replace('"', '""') + '"'
def escape_value(value):
if value is None:
return "NULL"
if isinstance(value, bool):
return "TRUE" if value else "FALSE"
if isinstance(value, (int, float)):
return str(value)
return "'" + str(value).replace("'", "''") + "'"