o
    8Di                     @  s&  d dl mZ d dlZd dlZd dlmZmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dl m!Z! d dl"m#Z# d dl$m%Z% d dl&m'Z' eeee%eeeeeee!eee#fZ(dddde(fd#d!d"Z)dS )$    )annotationsN)Schemaexp)DialectType)annotate_types)canonicalize)eliminate_ctes)eliminate_joins)eliminate_subqueries)merge_subqueries)	normalize)optimize_joins)pushdown_predicates)pushdown_projections)qualify)quote_identifiers)simplify)unnest_subqueries)ensure_schema
expressionstr | exp.Expressionschemat.Optional[dict | Schema]db t.Optional[str | exp.Identifier]catalogdialectr   rulest.Sequence[t.Callable]returnexp.Expressionc                   sn   t ||d}||||ddd| tj| |dd}|D ]}t|j}	 fdd|	D }
||fi |
}q|S )ag  
    Rewrite a sqlglot AST into an optimized form.

    Args:
        expression: expression to optimize
        schema: database schema.
            This can either be an instance of `sqlglot.optimizer.Schema` or a mapping in one of
            the following forms:
                1. {table: {col: type}}
                2. {db: {table: {col: type}}}
                3. {catalog: {db: {table: {col: type}}}}
            If no schema is provided then the default schema defined at `sqlgot.schema` will be used
        db: specify the default database, as might be set by a `USE DATABASE db` statement
        catalog: specify the default catalog, as might be set by a `USE CATALOG c` statement
        dialect: The dialect to parse the sql string.
        rules: sequence of optimizer rules to use.
            Many of the rules require tables and columns to be qualified.
            Do not remove `qualify` from the sequence of rules unless you know what you're doing!
        **kwargs: If a rule has a keyword argument with a same name in **kwargs, it will be passed in.

    Returns:
        The optimized expression.
    )r   TF)r   r   r   r   isolate_tablesr   )r   copyc                   s   i | ]}| v r| | qS  r#   ).0parampossible_kwargsr#   T/var/www/Datamplify/venv/lib/python3.10/site-packages/sqlglot/optimizer/optimizer.py
<dictcomp>Y   s    zoptimize.<locals>.<dictcomp>)r   r   maybe_parseinspectgetfullargspecargs)r   r   r   r   r   r   kwargs	optimizedrulerule_paramsrule_kwargsr#   r&   r(   optimize*   s$    

r3   )r   r   r   r   r   r   r   r   r   r   r   r   r   r    )*
__future__r   r+   typingtsqlglotr   r   sqlglot.dialects.dialectr    sqlglot.optimizer.annotate_typesr   sqlglot.optimizer.canonicalizer    sqlglot.optimizer.eliminate_ctesr   !sqlglot.optimizer.eliminate_joinsr	   &sqlglot.optimizer.eliminate_subqueriesr
   "sqlglot.optimizer.merge_subqueriesr   sqlglot.optimizer.normalizer    sqlglot.optimizer.optimize_joinsr   %sqlglot.optimizer.pushdown_predicatesr   &sqlglot.optimizer.pushdown_projectionsr   sqlglot.optimizer.qualifyr   !sqlglot.optimizer.qualify_columnsr   sqlglot.optimizer.simplifyr   #sqlglot.optimizer.unnest_subqueriesr   sqlglot.schemar   RULESr3   r#   r#   r#   r(   <module>   sR    