U
    -eb                     @   sl  d dl Z d dlZd dlZd dlZd dlmZ d dlmZ zd dlm	Z	 W n e
k
r`   dZ	Y nX ejjejj	gZd,ddZdd Zejd	d
dgdd Zejdedddfdd Zdd Zejd	d
dgdd Zdd Zdd Zejd	d
dgdd Zdd Zd d! Zejd	d
dgd"d# Zd$d% Zejd	d
dgd&d' Zd(d) Zd*d+ ZdS )-    N)tobytes)ArrowInvalid
   c                 C   s   ddl m} |t | S )Nr   )_get_udf_context)Zpyarrow._computer   paZdefault_memory_pool)Zbatch_lengthr    r   ]/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/pyarrow/tests/test_substrait.pymock_udf_context%   s    r	   c              	   C   s>   t jt| |}tjj||jd}|| W 5 Q R X |S )N)schema)	ospathjoinstrr   ZipcZRecordBatchFileWriterr
   Zwrite_table)tmpdir	file_nametabler   writerr   r   r   _write_dummy_data_to_disk*   s    r   use_threadsTFc           
      C   s   d}d}t jdddddggdgd	}t| ||}t|d
t| }t j	|}t
j||d}| }	|dg|	dgkstd S )Nax  
    {
        "version": { "major": 9999 },
        "relations": [
        {"rel": {
            "read": {
            "base_schema": {
                "struct": {
                "types": [
                            {"i64": {}}
                        ]
                },
                "names": [
                        "foo"
                        ]
            },
            "local_files": {
                "items": [
                {
                    "uri_file": "FILENAME_PLACEHOLDER",
                    "arrow": {}
                }
                ]
            }
            }
        }}
        ]
    }
    zread_data.arrow               ZfoonamesFILENAME_PLACEHOLDERr   r   r   r   r   replacepathlibPathas_uri
_substrait_parse_json_plan	substrait	run_queryread_allselectAssertionError
r   r   substrait_queryr   r   r   querybufreaderres_tbr   r   r   test_run_serialized_query1   s     r0   r,   s   buffers   bytesr   c              	   C   st   t |tjtfsHdt| d}tjt|d t	| W 5 Q R X d S d}tjt
|d t	| W 5 Q R X d S )Nz)Expected 'pyarrow.Buffer' or bytes, got ''matchz1ParseFromZeroCopyStream failed for substrait.Plan)
isinstancer   Bufferbytestypepytestraises	TypeErrorr%   r&   OSError)r   r,   msgr   r   r   test_run_query_input_types_   s    r=   c               	   C   s@   d} t jt| }d}tjt|d t| W 5 Q R X d S )Nz2
    {
        "relations": [
        ]
    }
    zPlan has no relationsr2   )	r   r#   r$   r   r8   r9   r   r%   r&   )r,   r-   exec_messager   r   r   test_invalid_plano   s
    r?   c           
      C   s   d}d}t jdddddggdgd	}t| ||}t|d
t| }t j	t|}t
j||d}| }	|dg|	dgkstd S )Na  
    {
        "version": { "major": 9999 },
        "relations": [
        {"rel": {
            "read": {
            "base_schema": {
                "struct": {
                "types": [
                            {"i64": {}}
                        ]
                },
                "names": [
                        "bar"
                        ]
            },
            "local_files": {
                "items": [
                {
                    "uri_file": "FILENAME_PLACEHOLDER",
                    "arrow": {},
                    "metadata" : {
                      "created_by" : {},
                    }
                }
                ]
            }
            }
        }}
        ]
    }
    zbinary_json_data.arrowr   r   r   r   r   barr   r   r   r   r*   r   r   r   (test_binary_conversion_with_json_options|   s    ! rA   c                 C   s,   | d| }| D ]}| |r dS qdS )N#TF)endswith)fnsZext_filefn_namesuffixfnr   r   r   has_function   s
    
rH   c                  C   s.   t j } t| ddstt| dds*td S )Nzfunctions_arithmetic.yamladdsum)r   r#   Zget_supported_functionsrH   r)   )Zsupported_functionsr   r   r   test_get_supported_functions   s    
  rK   c                    s   t jddddgit jddddgit t dt  g  fdd	}d
}t jt|}t j	j
||| d}| }|kstd S )Nxr   r   r   r   r      c                    sF   | st dn4| d dkr*| ks&tS | d dkr:S t dd S )NNo names providedr   t1r   t2Unrecognized table name)	Exceptionr)   r   r
   Zschema_1test_table_1Ztest_table_2r   r   table_provider   s    
z(test_named_table.<locals>.table_providera  
    {
        "version": { "major": 9999 },
        "relations": [
        {"rel": {
            "read": {
            "base_schema": {
                "struct": {
                "types": [
                            {"i64": {}}
                        ]
                },
                "names": [
                        "x"
                        ]
            },
            "namedTable": {
                    "names": ["t1"]
            }
            }
        }}
        ]
    }
    rV   r   )r   Tablefrom_pydictr
   fieldZint64r#   r$   r   r%   r&   r'   r)   )r   rV   r+   r-   r.   r/   r   rT   r   test_named_table   s      r[   c               	      sf   t jddddgi  fdd} d}t jt|}d}tjt|d	 t	j
|| d
 W 5 Q R X d S )NrL   r   r   r   c                    s*   | st dn| d dkr S t dd S NrN   r   rO   rQ   rR   r   _rU   r   r   rV      s
    
z;test_named_table_invalid_table_name.<locals>.table_providera  
    {
        "version": { "major": 9999 },
        "relations": [
        {"rel": {
            "read": {
            "base_schema": {
                "struct": {
                "types": [
                            {"i64": {}}
                        ]
                },
                "names": [
                        "x"
                        ]
            },
            "namedTable": {
                    "names": ["t3"]
            }
            }
        }}
        ]
    }
    zInvalid NamedTable Sourcer2   rV   )r   rX   rY   r#   r$   r   r8   r9   r   r%   r&   )rV   r+   r-   r>   r   r`   r   #test_named_table_invalid_table_name   s    rb   c               	      sn   t jddddgi  fdd} d}t|}t jt|}d}tjt|d	 t	j
|| d
 W 5 Q R X d S )NrL   r   r   r   c                    s*   | st dn| d dkr S t dd S r\   r]   r^   r`   r   r   rV     s
    
z4test_named_table_empty_names.<locals>.table_providera  
    {
        "version": { "major": 9999 },
        "relations": [
        {"rel": {
            "read": {
            "base_schema": {
                "struct": {
                "types": [
                            {"i64": {}}
                        ]
                },
                "names": [
                        "x"
                        ]
            },
            "namedTable": {
                    "names": []
            }
            }
        }}
        ]
    }
    z!names for NamedTable not providedr2   ra   )r   rX   rY   r   r#   r$   r8   r9   r   r%   r&   )rV   r+   r,   r-   r>   r   r`   r   test_named_table_empty_names  s    rc   c           
         s   t jddddgi  fdd}d}t j|}t jj|||d}| }| \}} dd	|t	d
 d }	||	ks|t
d S )NrL   r   r   r   c                    s*   | st dn| d dkr S t dd S r\   r]   r^   Z
test_tabler   r   rV   I  s
    
z.test_udf_via_substrait.<locals>.table_providers	  
    {
  "extensionUris": [
    {
      "extensionUriAnchor": 1
    },
    {
      "extensionUriAnchor": 2,
      "uri": "urn:arrow:substrait_simple_extension_function"
    }
  ],
  "extensions": [
    {
      "extensionFunction": {
        "extensionUriReference": 2,
        "functionAnchor": 1,
        "name": "y=x+1"
      }
    }
  ],
  "relations": [
    {
      "root": {
        "input": {
          "project": {
            "common": {
              "emit": {
                "outputMapping": [
                  1,
                  2,
                ]
              }
            },
            "input": {
              "read": {
                "baseSchema": {
                  "names": [
                    "t",
                  ],
                  "struct": {
                    "types": [
                      {
                        "i64": {
                          "nullability": "NULLABILITY_REQUIRED"
                        }
                      },
                    ],
                    "nullability": "NULLABILITY_REQUIRED"
                  }
                },
                "namedTable": {
                  "names": [
                    "t1"
                  ]
                }
              }
            },
            "expressions": [
              {
                "selection": {
                  "directReference": {
                    "structField": {}
                  },
                  "rootReference": {}
                }
              },
              {
                "scalarFunction": {
                  "functionReference": 1,
                  "outputType": {
                    "i64": {
                      "nullability": "NULLABILITY_NULLABLE"
                    }
                  },
                  "arguments": [
                    {
                      "value": {
                        "selection": {
                          "directReference": {
                            "structField": {}
                          },
                          "rootReference": {}
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        },
        "names": [
          "x",
          "y",
        ]
      }
    }
  ]
}
    rW   yr   )r   rX   rY   r#   r$   r%   r&   r'   
add_columnr	   r)   )
Zunary_func_fixturer   rV   r+   r-   r.   r/   functionnameexpected_tbr   rd   r   test_udf_via_substraitE  s     e  
 rj   c               	      sp   t jddddgi  fdd} d}t j|}tt j}t jj	|| d W 5 Q R X d	t
|jksltd S )
NrL   r   r   r   c                    s*   | st dn| d dkr S t dd S r\   r]   r^   rd   r   r   rV     s
    
z=test_udf_via_substrait_wrong_udf_name.<locals>.table_providers	  
    {
  "extensionUris": [
    {
      "extensionUriAnchor": 1
    },
    {
      "extensionUriAnchor": 2,
      "uri": "urn:arrow:substrait_simple_extension_function"
    }
  ],
  "extensions": [
    {
      "extensionFunction": {
        "extensionUriReference": 2,
        "functionAnchor": 1,
        "name": "wrong_udf_name"
      }
    }
  ],
  "relations": [
    {
      "root": {
        "input": {
          "project": {
            "common": {
              "emit": {
                "outputMapping": [
                  1,
                  2,
                ]
              }
            },
            "input": {
              "read": {
                "baseSchema": {
                  "names": [
                    "t",
                  ],
                  "struct": {
                    "types": [
                      {
                        "i64": {
                          "nullability": "NULLABILITY_REQUIRED"
                        }
                      },
                    ],
                    "nullability": "NULLABILITY_REQUIRED"
                  }
                },
                "namedTable": {
                  "names": [
                    "t1"
                  ]
                }
              }
            },
            "expressions": [
              {
                "selection": {
                  "directReference": {
                    "structField": {}
                  },
                  "rootReference": {}
                }
              },
              {
                "scalarFunction": {
                  "functionReference": 1,
                  "outputType": {
                    "i64": {
                      "nullability": "NULLABILITY_NULLABLE"
                    }
                  },
                  "arguments": [
                    {
                      "value": {
                        "selection": {
                          "directReference": {
                            "structField": {}
                          },
                          "rootReference": {}
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        },
        "names": [
          "x",
          "y",
        ]
      }
    }
  ]
}
    ra   zNo function registered)r   rX   rY   r#   r$   r8   r9   ZArrowKeyErrorr%   r&   r   valuer)   )rV   r+   r-   excinfor   rd   r   %test_udf_via_substrait_wrong_udf_name  s    erm   c                    sv   t jddddgi  fdd}d}t jt|}t jj||| d}| }t jd	dddgi}||ksrt	d S )
NrL   r   r   r   c                    s    S Nr   rS   Zin_tabler   r   rV   ;  s    z/test_output_field_names.<locals>.table_providera  
    {
      "version": { "major": 9999 },
      "relations": [
        {
          "root": {
            "input": {
              "read": {
                "base_schema": {
                  "struct": {
                    "types": [{"i64": {}}]
                  },
                  "names": ["x"]
                },
                "namedTable": {
                  "names": ["t1"]
                }
              }
            },
            "names": ["out"]
          }
        }
      ]
    }
    rW   out)
r   rX   rY   r#   r$   r   r%   r&   r'   r)   )r   rV   r+   r-   r.   r/   expectedr   ro   r   test_output_field_names7  s      rr   c                    s   t jddddgddddgddddgd  fdd}d	}t j|}t jj||d
d}| }t jddgddgd}||kstd S )Nr   r   r   r         ?)kv1v2c                    s    S rn   r   r^   rd   r   r   rV   i  s    z7test_scalar_aggregate_udf_basic.<locals>.table_providers  
{
  "extensionUris": [
    {
      "extensionUriAnchor": 1,
      "uri": "urn:arrow:substrait_simple_extension_function"
    },
  ],
  "extensions": [
    {
      "extensionFunction": {
        "extensionUriReference": 1,
        "functionAnchor": 1,
        "name": "sum_mean"
      }
    }
  ],
  "relations": [
    {
      "root": {
        "input": {
          "extensionSingle": {
            "common": {
              "emit": {
                "outputMapping": [
                  0,
                  1
                ]
              }
            },
            "input": {
              "read": {
                "baseSchema": {
                  "names": [
                    "k",
                    "v1",
                    "v2",
                  ],
                  "struct": {
                    "types": [
                      {
                        "i64": {
                          "nullability": "NULLABILITY_REQUIRED"
                        }
                      },
                      {
                        "i64": {
                          "nullability": "NULLABILITY_NULLABLE"
                        }
                      },
                      {
                        "fp64": {
                          "nullability": "NULLABILITY_NULLABLE"
                        }
                      }
                    ],
                    "nullability": "NULLABILITY_REQUIRED"
                  }
                },
                "namedTable": {
                  "names": ["t1"]
                }
              }
            },
            "detail": {
              "@type": "/arrow.substrait_ext.SegmentedAggregateRel",
              "segmentKeys": [
                {
                  "directReference": {
                    "structField": {}
                  },
                  "rootReference": {}
                }
              ],
              "measures": [
                {
                  "measure": {
                    "functionReference": 1,
                    "phase": "AGGREGATION_PHASE_INITIAL_TO_RESULT",
                    "outputType": {
                      "fp64": {
                        "nullability": "NULLABILITY_NULLABLE"
                      }
                    },
                    "arguments": [
                      {
                        "value": {
                          "selection": {
                            "directReference": {
                              "structField": {
                                "field": 1
                              }
                            },
                            "rootReference": {}
                          }
                        }
                      },
                      {
                        "value": {
                          "selection": {
                            "directReference": {
                              "structField": {
                                "field": 2
                              }
                            },
                            "rootReference": {}
                          }
                        }
                      }
                    ]
                  }
                }
              ]
            }
          }
        },
        "names": [
          "k",
          "v_avg"
        ]
      }
    }
  ],
}
FrW   g      @g      @)rt   v_avg	r   rX   rY   r#   r$   r%   r&   r'   r)   Zvarargs_agg_func_fixturerV   r+   r-   r.   r/   ri   r   rd   r   test_scalar_aggregate_udf_basicb  s&    
}  rz   c                    s   t jddddddddgddddddddgdddddddd	gd
d
d
d
ddddgd  fdd}d}t j|}t jj||dd}| }t jddddgddddgddddgd}||kstd S )Nr   r   r   r   r   r   rM         rs   g       @g      @g      @g      @)trt   ru   rv   c                    s    S rn   r   r^   rd   r   r   rV     s    z5test_hash_aggregate_udf_basic.<locals>.table_providers  
{
  "extensionUris": [
    {
      "extensionUriAnchor": 1,
      "uri": "urn:arrow:substrait_simple_extension_function"
    },
  ],
  "extensions": [
    {
      "extensionFunction": {
        "extensionUriReference": 1,
        "functionAnchor": 1,
        "name": "sum_mean"
      }
    }
  ],
  "relations": [
    {
      "root": {
        "input": {
          "extensionSingle": {
            "common": {
              "emit": {
                "outputMapping": [
                  0,
                  1,
                  2
                ]
              }
            },
            "input": {
              "read": {
                "baseSchema": {
                  "names": [
                    "t",
                    "k",
                    "v1",
                    "v2",
                  ],
                  "struct": {
                    "types": [
                      {
                        "i64": {
                          "nullability": "NULLABILITY_REQUIRED"
                        }
                      },
                      {
                        "i64": {
                          "nullability": "NULLABILITY_REQUIRED"
                        }
                      },
                      {
                        "i64": {
                          "nullability": "NULLABILITY_NULLABLE"
                        }
                      },
                      {
                        "fp64": {
                          "nullability": "NULLABILITY_NULLABLE"
                        }
                      }
                    ],
                    "nullability": "NULLABILITY_REQUIRED"
                  }
                },
                "namedTable": {
                  "names": ["t1"]
                }
              }
            },
            "detail": {
              "@type": "/arrow.substrait_ext.SegmentedAggregateRel",
              "groupingKeys": [
                {
                  "directReference": {
                    "structField": {
                      "field": 1
                    }
                  },
                  "rootReference": {}
                }
              ],
              "segmentKeys": [
                {
                  "directReference": {
                    "structField": {}
                  },
                  "rootReference": {}
                }
              ],
              "measures": [
                {
                  "measure": {
                    "functionReference": 1,
                    "phase": "AGGREGATION_PHASE_INITIAL_TO_RESULT",
                    "outputType": {
                      "fp64": {
                        "nullability": "NULLABILITY_NULLABLE"
                      }
                    },
                    "arguments": [
                      {
                        "value": {
                          "selection": {
                            "directReference": {
                              "structField": {
                                "field": 2
                              }
                            },
                            "rootReference": {}
                          }
                        }
                      },
                      {
                        "value": {
                          "selection": {
                            "directReference": {
                              "structField": {
                                "field": 3
                              }
                            },
                            "rootReference": {}
                          }
                        }
                      }
                    ]
                  }
                }
              ]
            }
          }
        },
        "names": [
          "t",
          "k",
          "v_avg"
        ]
      }
    }
  ],
}
FrW   g      @g      "@g      &@)r}   rt   rw   rx   ry   r   rd   r   test_hash_aggregate_udf_basic  s.       


r~   )r   ) r   r    r8   Zpyarrowr   Zpyarrow.libr   r   Zpyarrow.substraitr%   ImportErrormarkZdatasetZ
pytestmarkr	   r   Zparametrizer0   Z	py_bufferr=   r?   rA   rH   rK   r[   rb   rc   rj   rm   rr   rz   r~   r   r   r   r   <module>   sB   


-

1

0**
{v
* 