Generate random data and INSERT into MySQL.
(
conn_params: dict[str, Any],
table: str,
column_dicts: list[dict[str, Any]],
num_rows: int,
rng_seed: int,
)
| 176 | |
| 177 | |
| 178 | def _mysql_chunk( |
| 179 | conn_params: dict[str, Any], |
| 180 | table: str, |
| 181 | column_dicts: list[dict[str, Any]], |
| 182 | num_rows: int, |
| 183 | rng_seed: int, |
| 184 | ) -> int: |
| 185 | """Generate random data and INSERT into MySQL.""" |
| 186 | import pymysql |
| 187 | |
| 188 | rng = random.Random(rng_seed) |
| 189 | columns = [ |
| 190 | Column(c["name"], c["type"], c["nullable"], c["default"], c.get("data_shape")) |
| 191 | for c in column_dicts |
| 192 | ] |
| 193 | |
| 194 | conn = pymysql.connect(**conn_params) |
| 195 | |
| 196 | batch_size = 10000 |
| 197 | for start in range(0, num_rows, batch_size): |
| 198 | batch_rows = min(batch_size, num_rows - start) |
| 199 | rows_sql = [] |
| 200 | for _ in range(batch_rows): |
| 201 | row = [col.value(rng) for col in columns] |
| 202 | rows_sql.append("(" + ", ".join(row) + ")") |
| 203 | stmt = f"INSERT INTO {table} VALUES " + ", ".join(rows_sql) |
| 204 | with conn.cursor() as cur: |
| 205 | cur.execute(stmt) |
| 206 | |
| 207 | conn.close() |
| 208 | return num_rows |
| 209 | |
| 210 | |
| 211 | def _submit_chunks( |