/var/opt/nydus/ops/mysql/ai/utils/__pycache__
NameSizeModeActions
atomic_cursor.cpython-312.pyc26420644editdlrm
dependencies.cpython-312.pyc22940644editdlrm
utils.cpython-312.pyc199220644editdlrm
__init__.cpython-312.pyc7330644editdlrm
Edit: /var/opt/nydus/ops/mysql/ai/utils/__pycache__/utils.cpython-312.pyc (19922B)
N jE dZddlZddlZddlZddlZddlZddlmZddlm Z m Z m Z m Z m Z mZmZmZddlZddlZddlmZddlmZddlmZddlmZd Zd Zd d d dddZd Z dee!de!fdZ"edede e#e$e%e%ffdZ& d1dede%deddfdZ'de%fdZ(d2de e%ge)fde*de%fdZ+de dee%e e ffdZ,dedejZfd Z.ded!e%d"e%dejZfd#Z/ded!e%d"e%de)fd$Z0ded!e%d"e%ddfd%Z1 d1ded!e%d"e%d&ejZd'e e%e%fddf d(Z2ded!e%d&ejZdee%e%ffd)Z3d*e%de%fd+Z4dede%fd,Z5ded!e%d"e%de)fd-Z6 d3d.eeejZejnejpfd/e%deejZfd0Z9y)4a.General utilities for AI features in MySQL Connector/Python. Includes helpers for: - defensive dict copying - temporary table lifecycle management - SQL execution and result conversions - DataFrame to/from SQL table utilities - schema/table/column name validation - array-like to DataFrame conversion N)contextmanager)AnyCallableDictIteratorListOptionalTupleUnion)atomic_transaction)MySQLConnectionAbstract)MySQLCursorAbstract)ParamsSequenceOrDictTypemysql_ai BIGINTDOUBLELONGTEXTBOOLEANDATETIME)int64float64objectboolzdatetime64[ns]optionsreturnc4|iStj|S)z Make a defensive copy of a dictionary, or return an empty dict if None. Args: options: param dict or None Returns: dict )copydeepcopy)rs E/opt/nydus/tmp/pip-target-a90h98xg/lib/python/mysql/ai/utils/utils.py copy_dictr!Is == !! db_connectionc #Kg} |t|5}|D]\}}t||| dddy#1swYyxYw#t|5}|D]\}}t||| dddw#1swYwxYwxYww)a Context manager to track and automatically clean up temporary SQL tables. Args: db_connection: Database connection object used to create and delete tables. Returns: None Raises: DatabaseError: If a database connection issue occurs. If an operational error occurs during execution. Yields: temporary_tables: List of (schema_name, table_name) tuples created during the context. All tables in this list are deleted on context exit. N)r delete_sql_table)r#temporary_tablescursor schema_name table_names r temporary_sql_tablesr*Ys{,/1B  .&+;' Z jA,</ . .  .&+;' Z jA,</ . .sFBA B7 BAB A=A1( A=1A: 6A==Br'queryparamsc0|j||xsdy)aB Execute an SQL query with optional parameters using the given cursor. Args: cursor: MySQLCursorAbstract object to execute the query. query: SQL query string to execute. params: Optional sequence or dict providing parameters for the query. Raises: DatabaseError: If the provided SQL query/params are invalid If the query is valid but the sql raises as an exception If a database connection issue occurs. If an operational error occurs during execution. Returns: None N)execute)r'r+r,s r execute_sqlr0xs* NN5&,B'r"cvtj}djtj|t S)z Generate a random uppercase string of fixed length for table names. Returns: Random string of length RANDOM_TABLE_NAME_LENGTH. )k)stringascii_uppercasejoinrandomchoicesRANDOM_TABLE_NAME_LENGTH)char_sets r _get_namer;s*%%H 776>>(.FG HHr" condition max_callscdt|D]}|tx}s|cStd)a Generate a random string name that satisfies a given condition. Args: condition: Callable that takes a generated name and returns True if it is valid. max_calls: Maximum number of attempts before giving up (default 100). Returns: A random string that fulfills the provided condition. Raises: RuntimeError: If the maximum number of attempts is reached without success. z._json_processors#'#3tzz$==r"c|SrPr.rRs r _default_processorz.sql_response_to_df.._default_processors r")columns) r strrGr enumerate descriptionfetchallrHappendpd DataFrame column_names) r'rSrUidx_to_processoridxcolrowsprocessed_rowsrow processed_rowrNs r sql_response_to_dfrhs">hsm>>f001S q6S=$3 S !$6 S ! 2 ?? DNS "3IC!6!1#!6t!99\2/01z$4#5Q 8  FJv6+>s5D< cptfd}d|}tt||jD]}tt|g}|jj D]N\}}t jt|d}tt||j|d|Pdj|} td|jD} | r| dz } d|d | d } t|  t||||fS#t$rt|wxYw) a Create a new SQL table with a random name, and populate it with data from a DataFrame. If an 'id' column is defined in the dataframe, it will be used as the primary key. Args: cursor: MySQLCursorAbstract for executing SQL. schema_name: Schema in which to create the table. df: DataFrame containing the data to be inserted. Returns: Tuple (qualified_table_name, table_name): The schema-qualified and unqualified table names. Raises: RuntimeError: If a random available table name could not be found. ValueError: If any schema, table, or a column name is invalid. DatabaseError: If a database connection issue occurs. If an operational error occurs during execution. c t| SrP)rq)r)r'r(s r z#sql_table_from_df..s|FKLLr"rjr rwc3BK|]}|jdk(yw)idN)lower).0rcs r z$sql_table_from_df..s?JSSYY[D(Jsz, PRIMARY KEY (id)z CREATE TABLE rxry)rCrkrXrYdtypesitemsPD_TO_SQL_DTYPE_MAPPINGgetr]r6anyr0r Exceptionr%) r'r(rsr)r~rc columns_sqldtypesql_type columns_str has_id_colcreate_table_sqls `` r sql_table_from_dfrs@0!LJ*]!J<8+*zzc#hKiioo' U*..s5z:Fc#hcU!H:./ ( ))K(K?BJJ??J++ '';&rs8  %NNN;=6:   "x~ "$ " B*B d5c?#$BB>QU( ((+(5M( (0I3IWxt 4WWsW0CE#tCy.$9&+E2+Er||+E\& &.1&?B&\\&> )  ).1 )?B )  )FL L.1L?BL L>6: =7 =7=7=7 =7 %)cN =7  =7@:, :,.1:,79||:, 38_:,z(!8S:% %.1%?B% %< #< % bii;< =#<#<bll#