fn build_sort_plan(
        chunk: Arc<C>,
        input: Arc<dyn ExecutionPlan>,
        output_sort_key: &SortKey<'_>,
    ) -> Result<Arc<dyn ExecutionPlan>> {
        // output_sort_key cannot be empty
        if output_sort_key.is_empty() {
            panic!("Super sort key is empty");
        }

        trace!(output_sort_key=?output_sort_key, "Super sort key input to build_sort_plan");

        // Check to see if the plan is sorted on the subset of the output_sort_key
        let sort_key = chunk.sort_key();
        if let Some(chunk_sort_key) = sort_key {
            if let Some(merge_key) = SortKey::try_merge_key(output_sort_key, &chunk_sort_key) {
                if merge_key == *output_sort_key {
                    // the chunk is already sorted on the subset of the o_sort_key,
                    // no need to resort it
                    trace!(ChunkID=?chunk.id(), "Chunk is sorted and no need the sort operator");
                    return Ok(input);
                }
            } else {
                // The chunk is sorted but not on different order with super sort key.
                // Log it for investigating data set to improve performance further
                info!(chunk_type=?chunk.chunk_type(),
                    chunk_ID=?chunk.id(),
                    chunk_current_sort_order=?chunk_sort_key,
                    chunk_super_sort_key=?output_sort_key,
                    "Chunk will get resorted in build_sort_plan due to new cardinality rate between key columns");
            }
        } else {
            info!(chunk_type=?chunk.chunk_type(),
                chunk_ID=?chunk.id(),
                "Chunk is not yet sorted and will get sorted in build_sort_plan");
        }

        // Build the chunk's sort key that is a subset of the output_sort_key
        //
        // First get the chunk pk columns
        let schema = chunk.schema();
        let key_columns = schema.primary_key();

        // Now get the key subset of the super key that includes the chunk's pk columns
        let chunk_sort_key = output_sort_key.selected_sort_key(key_columns.clone());

        info!(chunk_type=?chunk.chunk_type(),
            chunk_ID=?chunk.id(),
            pk_columns=?key_columns,
            sort_key=?chunk_sort_key,
             "Chunk is getting sorted");

        // Build arrow sort expression for the chunk sort key
        let input_schema = input.schema();
        let sort_exprs = arrow_sort_key_exprs(&chunk_sort_key, &input_schema);

        trace!(Sort_Exprs=?sort_exprs, Chunk_ID=?chunk.id(), "Sort Expression for the sort operator of chunk");

        // Create SortExec operator
        Ok(Arc::new(
            SortExec::try_new(sort_exprs, input).context(InternalSort)?,
        ))
    }
/// Add SortExec operator on top of the input plan of the given chunk
/// The plan will be sorted on the chunk's primary key
Add SortExec operator on top of the input plan of the given chunk
The plan will be sorted on the chunk's primary key
Add SortExec operator on top of the input plan of the given chunk The plan will be sorted on the chunk ' s primary key
fn build_sort_plan(
        chunk: Arc<C>,
        input: Arc<dyn ExecutionPlan>,
        output_sort_key: &SortKey<'_>,
    ) -> Result<Arc<dyn ExecutionPlan>> {
        // output_sort_key cannot be empty
        if output_sort_key.is_empty() {
            panic!("Super sort key is empty");
        }
        trace!(output_sort_key=?output_sort_key, "Super sort key input to build_sort_plan");
        // Check to see if the plan is sorted on the subset of the output_sort_key
        let sort_key = chunk.sort_key();
        if let Some(chunk_sort_key) = sort_key {
            if let Some(merge_key) = SortKey::try_merge_key(output_sort_key, &chunk_sort_key) {
                if merge_key == *output_sort_key {
                    // the chunk is already sorted on the subset of the o_sort_key,
                    // no need to resort it
                    trace!(ChunkID=?chunk.id(), "Chunk is sorted and no need the sort operator");
                    return Ok(input);
                }
            } else {
                // The chunk is sorted but not on different order with super sort key.
                // Log it for investigating data set to improve performance further
                info!(chunk_type=?chunk.chunk_type(),
                    chunk_ID=?chunk.id(),
                    chunk_current_sort_order=?chunk_sort_key,
                    chunk_super_sort_key=?output_sort_key,
                    "Chunk will get resorted in build_sort_plan due to new cardinality rate between key columns");
            }
        } else {
            info!(chunk_type=?chunk.chunk_type(),
                chunk_ID=?chunk.id(),
                "Chunk is not yet sorted and will get sorted in build_sort_plan");
        }
        // Build the chunk's sort key that is a subset of the output_sort_key
        //
        // First get the chunk pk columns
        let schema = chunk.schema();
        let key_columns = schema.primary_key();
        // Now get the key subset of the super key that includes the chunk's pk columns
        let chunk_sort_key = output_sort_key.selected_sort_key(key_columns.clone());
        info!(chunk_type=?chunk.chunk_type(),
            chunk_ID=?chunk.id(),
            pk_columns=?key_columns,
            sort_key=?chunk_sort_key,
             "Chunk is getting sorted");
        // Build arrow sort expression for the chunk sort key
        let input_schema = input.schema();
        let sort_exprs = arrow_sort_key_exprs(&chunk_sort_key, &input_schema);
        trace!(Sort_Exprs=?sort_exprs, Chunk_ID=?chunk.id(), "Sort Expression for the sort operator of chunk");
        // Create SortExec operator
        Ok(Arc::new(
            SortExec::try_new(sort_exprs, input).context(InternalSort)?,
        ))
    }
fn build_sort_plan ( chunk : Arc < C > , input : Arc < dyn ExecutionPlan > , output_sort_key : & SortKey < ' _ > , ) -> Result < Arc < dyn ExecutionPlan > > { // output_sort_key cannot be empty if output_sort_key . is_empty ( ) { panic ! ( " " ) ; } trace ! ( output_sort_key output_sort_key " " ) ; // Check to see if the plan is sorted on the subset of the output_sort_key let sort_key = chunk . sort_key ( ) ; if let Some ( chunk_sort_key ) = sort_key { if let Some ( merge_key ) = SortKey :: try_merge_key ( output_sort_key , & chunk_sort_key ) { if merge_key == * output_sort_key { // the chunk is already sorted on the subset of the o_sort_key, // no need to resort it trace ! ( ChunkID chunk id ( ) " " ) ; return Ok ( input ) ; } } else { // The chunk is sorted but not on different order with super sort key. // Log it for investigating data set to improve performance further info ! ( chunk_type chunk chunk_type ( ) chunk_ID chunk id ( ) chunk_current_sort_order chunk_sort_key chunk_super_sort_key output_sort_key " " ) ; } } else { info ! ( chunk_type chunk chunk_type ( ) chunk_ID chunk id ( ) " " ) ; } // Build the chunk's sort key that is a subset of the output_sort_key // // First get the chunk pk columns let schema = chunk . schema ( ) ; let key_columns = schema . primary_key ( ) ; // Now get the key subset of the super key that includes the chunk's pk columns let chunk_sort_key = output_sort_key . selected_sort_key ( key_columns . clone ( ) ) ; info ! ( chunk_type chunk chunk_type ( ) chunk_ID chunk id ( ) pk_columns key_columns sort_key chunk_sort_key " " ) ; // Build arrow sort expression for the chunk sort key let input_schema = input . schema ( ) ; let sort_exprs = arrow_sort_key_exprs ( & chunk_sort_key , & input_schema ) ; trace ! ( Sort_Exprs sort_exprs Chunk_ID chunk id ( ) " " ) ; // Create SortExec operator Ok ( Arc :: new ( SortExec :: try_new ( sort_exprs , input ) . context ( InternalSort ) ? , ) ) }
Add SortExec operator on top of the input plan of the given chunk
The plan will be sorted on the chunk's primary key
Add SortExec operator on top of the input plan of the given chunk The plan will be sorted on the chunk ' s primary key
chunk
Arc<C>
input
Arc<dyn ExecutionPlan>
output_sort_key
&SortKey<'_>
chunk
Arc<C>
input
Arc<dyn ExecutionPlan>
output_sort_key
&SortKey<'_>