o
    8Di+                     @  s   d dl mZ d dlZd dlmZ d dlmZmZm	Z	m
Z
mZmZmZ d dlmZ d dlmZ d dlmZ dddZG dd deZdS )    )annotationsN)exp)approx_count_distinct_sqlarrow_json_extract_sqlbuild_timestamp_truncrename_funcunit_to_strinline_array_sqlproperty_sql)MySQL)seq_get)	TokenType
expressionexp.StDistancereturnstrc                 C  sN   |j }|j}| d|}| d|}| d|}| d|}| d||||S )NST_XST_YST_Distance_Sphere)thisr   func)selfr   point1point2point1_xpoint1_ypoint2_xpoint2_y r   S/var/www/Datamplify/venv/lib/python3.10/site-packages/sqlglot/dialects/starrocks.pyst_distance_sphere   s   r    c                   @  sF   e Zd ZdZG dd dejZG dd dejZG dd dejZdS )		StarRocksFc                   @  s"   e Zd Zi ejjdejiZdS )zStarRocks.TokenizerLARGEINTN)__name__
__module____qualname__r   	TokenizerKEYWORDSr   INT128r   r   r   r   r&   $   s    
r&   c                      s   e Zd Zi ejjedd dd ejj	dZi ejj
dd dd dd dZ
d fddZdd fddZdddZd fddZ  ZS )zStarRocks.Parserc                 C  s$   t jt| dt| dt jddS )Nr      DAYr   r   unit)r   DateDiffr   Literalstringargsr   r   r   <lambda>.   s    zStarRocks.Parser.<lambda>c                 C  s"   t jt| dt| dt| ddS )Nr)      r   r+   )r   r-   r   r0   r   r   r   r2   1   s    )
DATE_TRUNCDATEDIFF	DATE_DIFFREGEXPc                 C  s   |  tjS N)_parse_composite_key_propertyr   UniqueKeyPropertyr   r   r   r   r2   9   s    c                 C     |   S r8   )_parse_wrapped_propertiesr;   r   r   r   r2   :       c                 C  r<   r8   )_parse_partition_by_opt_ranger;   r   r   r   r2   ;   r>   )UNIQUE
PROPERTIESzPARTITION BYr   exp.Create | exp.Commandc                   sZ   t   }t|tjr+t|jtjr+|jd}|r+|	tj
}|r+|jd|  |S )N
propertiesexpressions)super_parse_create
isinstancer   Creater   Schemar1   getfind
PrimaryKeyappendpop)r   createpropsprimary_key	__class__r   r   rF   >   s   
zStarRocks.Parser._parse_createT
with_aliasboolt.Optional[exp.Unnest]c                   sr   t  j|d}|r7|jd}|s'tjtdtdgd}|d| |S |jds7|dtdg |S )N)rT   aliasunnest)r   columnsrY   )rE   _parse_unnestr1   rJ   r   
TableAliasto_identifierset)r   rT   rX   rW   rR   r   r   rZ   L   s   zStarRocks.Parser._parse_unnest#exp.PartitionByRangePropertyDynamicc                   s\     d   j}  d   j}  d   fdd} jtj|||dS )NSTARTENDEVERYc                     s      p  S r8   )_parse_interval_parse_numberr   r;   r   r   r2   e   s    zJStarRocks.Parser._parse_partitioning_granularity_dynamic.<locals>.<lambda>)startendevery)_match_text_seq_parse_wrapped_parse_stringr   r   PartitionByRangePropertyDynamic)r   rd   re   rf   r   r;   r   '_parse_partitioning_granularity_dynamic_   s   



z8StarRocks.Parser._parse_partitioning_granularity_dynamic8exp.PartitionedByProperty | exp.PartitionByRangePropertyc                   s:   |  dr|  }| | j}| jtj||dS t  S )NRANGE)partition_expressionscreate_expressions)	rg   _parse_wrapped_id_vars_parse_wrapped_csvrk   r   r   PartitionByRangePropertyrE   _parse_partitioned_by)r   rn   ro   rR   r   r   r?   j   s   

z.StarRocks.Parser._parse_partition_by_opt_range)r   rB   )T)rT   rU   r   rV   )r   r^   )r   rl   )r#   r$   r%   r   Parser	FUNCTIONSr   r   
RegexpLikefrom_arg_listPROPERTY_PARSERSrF   rZ   rk   r?   __classcell__r   r   rR   r   rt   *   s(    
rt   c                	      sx  e Zd ZU dZdZdZdZded< dZi Z	i e
jjejjjdejjjdejjjdejjjdiZi e
jjejejjjejejjjejejjjiZi e
jji ejeejed	ej ed
ej!edej"e#ej$dd ej%e&ej'e&ej(e)ej*edej+e,ej-dd ej.dd ej/edej0dd ej1edZe2ej3 h dZ4d fddZ5  Z6S )zStarRocks.GeneratorF
PARSE_JSONzt.Optional[str]PARSE_JSON_NAMErA   r"   STRINGDATETIME	ARRAY_AGGARRAY_FILTER
ARRAY_JOINc                 C  s   |  dt||j|jS )Nr6   )r   r   r   r   r   er   r   r   r2      s    zStarRocks.Generator.<lambda>r7   c                 C     |  d|j| |S )NUNIX_TIMESTAMPr   r   format_timer   r   r   r   r2          c                 C  s   |  dt||jS )Nr4   )r   r   r   r   r   r   r   r2      s    TO_DATEc                 C  r   )NFROM_UNIXTIMEr   r   r   r   r   r2      r   r   >   asbyifinisonortoaddallandascforhllintkeylagmodnotrowr]   usebothcasecharcubedescdropdualelsefromfullhostintojoinjsonkeyskillleadleftlikeloadnulloverrankreadrowsshowtextthentruewhenwithalterarraycheckcrossfalsefloatforcegrantgroupindexinnerlimitminusntileorderouterrangerightrliketableunionusingwherebigintbitmapcolumnrO   deletedoubleexceptexistsgroupshavingignoreinfileinsertregexprenamerevokeschemaselectsystemuniqueupdatevaluesanalyzebetweencollateconvertdecimaldefaultexplainintegerlateraloutfileprimaryqualifyreleasereplaceschemasset_vartinyintvarchardatabasedeferreddescribedistinctfunctiongroupinglargeintmaxvaluesmallintunsigned	character	databases	decimal32	decimal64	decimalv2	immediate	intersect	localtime	partition	procedure
compaction
decimal128
dense_rank
last_value
percentile
row_number
terminatedfirst_valuegrouping_idcurrent_datecurrent_rolecurrent_timecurrent_userlocaltimestampcurrent_timestampr   
exp.Creater   r   c                   s   |j }t|tjrA|tj}|rA|jd}|s%tjg d}|	d| |tj
}|r2|jp1dnd}|j	d| |d dd t |S )	NrC   )rD   r   rD   r)   F)	overwrite)r   rG   r   rI   rK   rL   r1   rJ   
Propertiesr]   EnginePropertyr   rN   rE   
create_sql)r   r   r   rQ   rP   engineengine_indexrR   r   r   r$  D  s    zStarRocks.Generator.create_sql)r   r  r   r   )7r#   r$   r%   #EXCEPT_INTERSECT_SUPPORT_ALL_CLAUSE!JSON_TYPE_REQUIRED_FOR_EXTRACTIONVARCHAR_REQUIRES_SIZEr{   __annotations__WITH_PROPERTIES_PREFIXCAST_MAPPINGr   	GeneratorTYPE_MAPPINGr   DataTypeTyper(   TEXT	TIMESTAMPTIMESTAMPTZPROPERTIES_LOCATIONrL   r"  LocationPOST_SCHEMAr:   rr   
TRANSFORMSArrayr	   ArrayAggr   ArrayFilterArrayToStringApproxDistinctr   r-   JSONExtractScalarr   JSONExtractPropertyr
   rv   
StDistancer    	StrToUnixTimestampTruncTimeStrToDate	UnixToStr
UnixToTimerN   	DateTruncRESERVED_KEYWORDSr$  ry   r   r   rR   r   r-  y   sz   
 














 r-  N)r#   r$   r%   STRICT_JSON_PATH_SYNTAXr   r&   rt   r-  r   r   r   r   r!   !   s
    Or!   )r   r   r   r   )
__future__r   typingtsqlglotr   sqlglot.dialects.dialectr   r   r   r   r   r	   r
   sqlglot.dialects.mysqlr   sqlglot.helperr   sqlglot.tokensr   r    r!   r   r   r   r   <module>   s    $	
