
    mBNj(                     X    d dl Z d dlmZmZ  G d d      Z G d d      Z G d d      Zy)	    N)ListOptionalc                        e Zd ZdZdedefdZy)ProfileStatsz
    ProfileStats Class for representing runtime execution statistics of an operation.

    Attributes:
        records_produced (int): The number of records produced.
        execution_time (float): The execution time in milliseconds.
    records_producedexecution_timec                      || _         || _        y)a  
        Initializes a new ProfileStats instance with the given
        records_produced and execution_time.

        Args:
            records_produced (int): The number of records produced.
            execution_time (float): The execution time in milliseconds.
        N)r   r   )selfr   r   s      h/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/falkordb/execution_plan.py__init__zProfileStats.__init__   s     - 0    N)__name__
__module____qualname____doc__intfloatr    r   r   r   r      s    
1 
1e 
1r   r   c                       e Zd ZdZ	 	 ddedee   dee   fdZede	fd       Z
edefd	       Zd
 ZdefdZdedefdZdefdZy)	Operationa?  
    Operation Class for representing a single operation within an execution plan.

    Attributes:
        name (str): The name of the operation.
        args (str): Operation arguments.
        children (list): List of child operations.
        profile_stats (ProfileStats): Profile statistics for the operation.
    Nnameargsprofile_statsc                 <    || _         || _        g | _        || _        y)a  
        Creates a new Operation instance.

        Args:
            name (str): The name of the operation.
            args (str, optional): Operation arguments.
            profile_stats (ProfileStats, optional): Profile statistics
                for the operation.
        N)r   r   childrenr   )r
   r   r   r   s       r   r   zOperation.__init__&   s!     		)+*r   returnc                 J    | j                   J | j                   j                  S )z:
        returns operation's execution time in ms
        )r   r   r
   s    r   r   zOperation.execution_time:   s(    
 !!---!!000r   c                 J    | j                   J | j                   j                  S )zB
        returns number of records produced by operation.
        )r   r   r   s    r   r   zOperation.records_producedB   s(    
 !!---!!222r   c                 r    t        |t              st        d      | j                  j	                  |       | S )z
        Appends a child operation to the current operation.

        Args:
            child (Operation): The child operation to append.

        Returns:
            Operation: The updated operation instance.
        zchild must be Operation)
isinstancer   	Exceptionr   append)r
   childs     r   append_childzOperation.append_childJ   s1     %+566U#r   c                 ,    t        | j                        S )zx
        Returns the number of child operations.

        Returns:
            int: Number of child operations.
        )lenr   r   s    r   child_countzOperation.child_countZ   s     4==!!r   oc                     t        |t              sy| j                  |j                  k(  xr | j                  |j                  k(  S )a  
        Compares two Operation instances for equality based on their name and arguments.

        Args:
            o (object): Another Operation instance for comparison.

        Returns:
            bool: True if the operations are equal, False otherwise.
        F)r!   r   r   r   )r
   r)   s     r   __eq__zOperation.__eq__c   s6     !Y'yyAFF":tyyAFF'::r   c                 \    | j                   dnd| j                   z   }| j                   | S )z
        Returns a string representation of the operation.

        Returns:
            str: String representation of the operation.
         z | )r   r   )r
   args_strs     r   __str__zOperation.__str__r   s0     *2		0A))XJ''r   )NN)r   r   r   r   strr   r   r   propertyr   r   r   r   r%   r(   objectboolr+   r/   r   r   r   r   r      s     #04	++ sm+  -	+( 1 1 1 3# 3 3 "S "; ;4 ;( (r   r   c                   V    e Zd ZdZd Zd ZdefdZdefdZ	de
defdZd	 Zd
 Zd Zy)ExecutionPlanz
    ExecutionPlan Class for representing a collection of operations.

    Attributes:
        plan (list): List of strings representing the collection of operations.
        structured_plan (Operation): Root of the structured operation tree.
    c                 F   t        |t              st        d      t        |d   t              r|D cg c]  }|j	                          }}|| _        i | _        | j                         | _        | j                  D ]  }| j                  |   j                          ! yc c}w )z
        Creates a new ExecutionPlan instance.

        Args:
            plan (list): List of strings representing the collection of operations.
        zplan must be an arrayr   N)
r!   listr"   bytesdecodeplan
operations_operation_treestructured_planreverse)r
   r:   bkeys       r   r   zExecutionPlan.__init__   s     $%344d1gu%(,-1AHHJD-	#335??COOC ((* # .s   Bc                 @    || j                   v r| j                   |   S g S )z
        Collects all operations with specified name from plan

        Args:
            op_name (string): Name of operation to collect

        Returns:
            List[Operation]: All operations with the specified name
        )r;   r   r#   )r
   op_nameopsops       r   collect_operationsz ExecutionPlan.collect_operations   s$     doo%??7++	r   r   c                     ||k7  ry|j                         |j                         k7  ryt        |j                               D ]0  }| j                  |j                  |   |j                  |         r0 y y)z
        Compares execution plan operation trees.

        Returns:
            bool: True if operation trees are equal, False otherwise.
        FT)r(   range"_ExecutionPlan__compare_operationsr   )r
   root_aroot_bis       r   __compare_operationsz"ExecutionPlan.__compare_operations   ss     V 6#5#5#77 v))+,A,,V__Q-?QRAST - r   c                 R    d }d }| j                  | j                  t        ||      S )z
        Returns a string representation of the execution plan.

        Returns:
            str: String representation of the execution plan.
        c           	          dj                  | D cg c]  }|j                         D ]  }d|z   	  c}}      S c c}}w )N
    )join
splitlines)str_children	str_childlines      r   aggregate_strz,ExecutionPlan.__str__.<locals>.aggregate_str   sO    99 &2%1	 ) 4 4 6 TM 6 "%1 s   !:
c                     |  d| S )NrO   r   )xys     r   combine_strz*ExecutionPlan.__str__.<locals>.combine_str   s    S1#;r   )_operation_traverser=   r0   )r
   rV   rZ   s      r   r/   zExecutionPlan.__str__   s0    		 ''  #}k
 	
r   r)   c                 x    t        |t              sy| j                  }|j                  }| j                  ||      S )z
        Compares two execution plans.

        Returns:
            bool: True if the two plans are equal, False otherwise.
        F)r!   r5   r=   rH   )r
   r)   rI   rJ   s       r   r+   zExecutionPlan.__eq__   s>     !]+ %%"" ((88r   c                 ,    t        | j                        S )N)iterr;   r   s    r   __iter__zExecutionPlan.__iter__   s    DOO$$r   c           	           ||      }t        |j                        dk(  r|S |j                  D cg c]  }| j                  ||||       }} || ||            S c c}w )a  
        Traverses the operation tree recursively applying functions.

        Args:
            op: Operation to traverse.
            op_f: Function applied for each operation.
            aggregate_f: Aggregation function applied for all
                children of a single operation.
            combine_f: Combine function applied for the operation
                result and the children result.
        r   )r'   r   r[   )r
   rD   op_faggregate_f	combine_fop_resr$   r   s           r   r[   z!ExecutionPlan._operation_traverse   ss     br{{q M
 
$ $$UD+yI$ 	 

 X!677
s   Ac                    d}d}g }d}d }|t        | j                        k  r| j                  |   }|j                  d      }||k(  r ||j                  d            }|j                  | j
                  vrg | j
                  |j                  <   | j
                  |j                     j                  |       |r!|j                         }|j                  |       |}|dz  }|j                  |       n||dz   k(  r ||j                  d            }|j                  | j
                  vrg | j
                  |j                  <   | j
                  |j                     j                  |       |j                  |       |j                  |       |}|dz  }|dz  }n>||k  r.||z
  dz   }	t        |	      D ]  }
|j                         } ||	z  }nt        d      |t        | j                        k  r|d   S )z
        Builds the operation tree from the string representation.

        Returns:
            Operation: Root of the structured operation tree.
        r   Nc                    d }| d   j                         }| j                  d       t        |       dkD  rd| d   v rt        t	        j
                  d| d         j                  d            }t        t	        j
                  d| d         j                  d            }t        ||      }| j                  d       t        |t        |       dk(  rd |      S | d   j                         |      S )Nr   zRecords producedzRecords produced: (\d+)   zExecution time: (\d+.\d+) ms)
strippopr'   r   researchgroupr   r   r   )r   r   r   r   r   s        r   create_operationz7ExecutionPlan._operation_tree.<locals>.create_operation  s     M7==?DHHQK4y1}!3tBx!?#&II8$r(CII!L$  "'II>RIOOPQR" !--=~ Nc$i1nd= 26q'--/= r   rP   |rh   zcorrupted plan)r'   r:   countsplitr   r;   r#   rj   r%   rG   r"   )r
   rK   levelstackcurrentrn   
current_opop_levelr$   levels_back_s              r   r<   zExecutionPlan._operation_tree  s    	$ #dii. 1J!''/H5  ))9)9#)>?::T__424DOOEJJ/

+2259#iikG((/QU#UQY& ))9)9#)>?::T__424DOOEJJ/

+2259$$U+W%
QE! $h.2{+A#iikG ,$ 011M #dii. N Qxr   N)r   r   r   r   r   rE   r3   rH   r0   r/   r2   r+   r_   r[   r<   r   r   r   r5   r5   }   sL    +&,d ,
 
09 94 9$%82Fr   r5   )rk   typingr   r   r   r   r5   r   r   r   <module>rz      s.    	 !1 1,_( _(DT Tr   