<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>array_recruitment_for_parallel</name>
    <name_sync_with_filename>Y</name_sync_with_filename>
    <description/>
    <extended_description/>
    <pipeline_version/>
    <pipeline_type>Normal</pipeline_type>
    <parameters>
    </parameters>
    <capture_transform_performance>N</capture_transform_performance>
    <transform_performance_capturing_delay>1000</transform_performance_capturing_delay>
    <transform_performance_capturing_size_limit>100</transform_performance_capturing_size_limit>
    <created_user>-</created_user>
    <created_date>2025/03/31 10:14:04.435</created_date>
    <modified_user>-</modified_user>
    <modified_date>2025/03/31 10:14:04.435</modified_date>
  </info>
  <notepads>
  </notepads>
  <order>
    <hop>
      <from>Table input</from>
      <to>Copy rows to result</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>Copy rows to result</name>
    <type>RowsToResult</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <attributes/>
    <GUI>
      <xloc>1040</xloc>
      <yloc>384</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Table input</name>
    <type>TableInput</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <connection>ervu-dashboard</connection>
    <execute_each_row>N</execute_each_row>
    <limit>0</limit>
    <sql>WITH ids AS (
    SELECT idm_id, 
           NTILE(5) OVER (ORDER BY idm_id) AS part -- Разбиваем на 5 частей
    FROM ervu_dashboard.recruitment
    WHERE schema IN ('Department', 'Organization', 'Region')
),
grouped AS (
    SELECT 
        array_agg(idm_id) FILTER (WHERE part = 1) AS part_1,
        array_agg(idm_id) FILTER (WHERE part = 2) AS part_2,
        array_agg(idm_id) FILTER (WHERE part = 3) AS part_3,
        array_agg(idm_id) FILTER (WHERE part = 4) AS part_4,
        array_agg(idm_id) FILTER (WHERE part = 5) AS part_5
    FROM ids
),
split_parts AS (
    SELECT 
        gs.row_num,
        (SELECT array_agg(val) FROM unnest(part_1) WITH ORDINALITY AS t(val, ord) 
         WHERE ord > array_length(part_1, 1) * (gs.row_num - 1) / 2 
           AND ord &lt;= array_length(part_1, 1) * gs.row_num / 2) AS PART_RECRUITMENT_1,
        (SELECT array_agg(val) FROM unnest(part_2) WITH ORDINALITY AS t(val, ord) 
         WHERE ord > array_length(part_2, 1) * (gs.row_num - 1) / 2 
           AND ord &lt;= array_length(part_2, 1) * gs.row_num / 2) AS PART_RECRUITMENT_2,
        (SELECT array_agg(val) FROM unnest(part_3) WITH ORDINALITY AS t(val, ord) 
         WHERE ord > array_length(part_3, 1) * (gs.row_num - 1) / 2 
           AND ord &lt;= array_length(part_3, 1) * gs.row_num / 2) AS PART_RECRUITMENT_3,
        (SELECT array_agg(val) FROM unnest(part_4) WITH ORDINALITY AS t(val, ord) 
         WHERE ord > array_length(part_4, 1) * (gs.row_num - 1) / 2 
           AND ord &lt;= array_length(part_4, 1) * gs.row_num / 2) AS PART_RECRUITMENT_4,
        (SELECT array_agg(val) FROM unnest(part_5) WITH ORDINALITY AS t(val, ord) 
         WHERE ord > array_length(part_5, 1) * (gs.row_num - 1) / 2 
           AND ord &lt;= array_length(part_5, 1) * gs.row_num / 2) AS PART_RECRUITMENT_5
    FROM grouped
    CROSS JOIN generate_series(1, 2) gs(row_num)
)
SELECT 
PART_RECRUITMENT_1,
PART_RECRUITMENT_2,
PART_RECRUITMENT_3,
PART_RECRUITMENT_4,
PART_RECRUITMENT_5 
FROM split_parts;</sql>
    <variables_active>N</variables_active>
    <attributes/>
    <GUI>
      <xloc>672</xloc>
      <yloc>384</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
