From d766074e174ad36402706c02a548c33de9ab3e8a Mon Sep 17 00:00:00 2001
From: EmirVildanov <reddog201030@gmail.com>
Date: Mon, 9 Sep 2024 11:21:34 +0300
Subject: [PATCH] fix: refactoring after rebase on new protocol, tiers and
 subarenas

---
 sbroad-core/src/backend/sql/ir.rs             |   2 +-
 .../src/backend/sql/ir/tests/selection.rs     |   4 +-
 sbroad-core/src/backend/sql/space.rs          |   3 +-
 sbroad-core/src/backend/sql/tree.rs           |  67 +-
 sbroad-core/src/executor/engine/helpers.rs    |   4 +-
 sbroad-core/src/executor/ir.rs                |  55 +-
 sbroad-core/src/executor/result.rs            |   1 -
 sbroad-core/src/executor/tests.rs             |   5 +-
 sbroad-core/src/executor/tests/exec_plan.rs   |  76 +--
 sbroad-core/src/frontend/sql.rs               |   3 +-
 sbroad-core/src/frontend/sql/ir/tests.rs      | 583 +++++++++---------
 .../src/frontend/sql/ir/tests/global.rs       |  80 +--
 .../src/frontend/sql/ir/tests/limit.rs        |  12 +-
 sbroad-core/src/ir.rs                         |  29 +-
 sbroad-core/src/ir/distribution/tests.rs      |   4 +-
 sbroad-core/src/ir/explain.rs                 |   4 +-
 sbroad-core/src/ir/expression.rs              |  37 +-
 sbroad-core/src/ir/helpers.rs                 |   6 +-
 sbroad-core/src/ir/helpers/tests.rs           | 256 ++++----
 sbroad-core/src/ir/node.rs                    |  36 +-
 sbroad-core/src/ir/operator.rs                |   9 +-
 sbroad-core/src/ir/operator/tests.rs          |   8 +-
 .../ir/transformation/equality_propagation.rs |   4 +-
 .../equality_propagation/tests.rs             |   6 +-
 .../transformation/redistribution/eq_cols.rs  |   4 +-
 .../transformation/redistribution/groupby.rs  |  90 +--
 .../ir/transformation/split_columns/tests.rs  |   4 +-
 sbroad-core/src/ir/tree/subtree.rs            |   8 +-
 sbroad-core/src/ir/tree/tests.rs              |   2 +-
 sbroad-core/src/utils.rs                      | 119 ++++
 30 files changed, 777 insertions(+), 744 deletions(-)

diff --git a/sbroad-core/src/backend/sql/ir.rs b/sbroad-core/src/backend/sql/ir.rs
index ce28164de..a9a360295 100644
--- a/sbroad-core/src/backend/sql/ir.rs
+++ b/sbroad-core/src/backend/sql/ir.rs
@@ -449,7 +449,7 @@ impl ExecutionPlan {
                                     }
                                     Expression::Reference(Reference { position, .. }) => {
                                         let rel_id =
-                                            *ir_plan.get_relational_from_reference_node(*id)?;
+                                            ir_plan.get_relational_from_reference_node(*id)?;
                                         let rel_node = ir_plan.get_relation_node(rel_id)?;
 
                                         if rel_node.is_motion() {
diff --git a/sbroad-core/src/backend/sql/ir/tests/selection.rs b/sbroad-core/src/backend/sql/ir/tests/selection.rs
index 20080afa2..7e1b97e2c 100644
--- a/sbroad-core/src/backend/sql/ir/tests/selection.rs
+++ b/sbroad-core/src/backend/sql/ir/tests/selection.rs
@@ -67,9 +67,9 @@ fn selection2_latest() {
     let expected = PatternWithParams::new(
         [
             r#"SELECT "hash_testing"."product_code" FROM "hash_testing""#,
-            r#"WHERE ("hash_testing"."identification_number", "hash_testing"."product_units", "hash_testing"."identification_number") = ("hash_testing"."product_units", ?, ?)"#,
+            r#"WHERE ("hash_testing"."product_units", "hash_testing"."product_units", "hash_testing"."identification_number") = ("hash_testing"."identification_number", ?, ?)"#,
             r#"and ("hash_testing"."product_units") <> ("hash_testing"."sys_op")"#,
-            r#"or ("hash_testing"."identification_number", "hash_testing"."product_units", "hash_testing"."identification_number") = ("hash_testing"."product_units", ?, ?)"#,
+            r#"or ("hash_testing"."product_units", "hash_testing"."product_units", "hash_testing"."identification_number") = ("hash_testing"."identification_number", ?, ?)"#,
             r#"and ("hash_testing"."product_units") is null"#
         ].join(" "),
         vec![Value::Unsigned(1), Value::Unsigned(1), Value::Unsigned(1), Value::Unsigned(1)],
diff --git a/sbroad-core/src/backend/sql/space.rs b/sbroad-core/src/backend/sql/space.rs
index cb29896d3..38a548bf9 100644
--- a/sbroad-core/src/backend/sql/space.rs
+++ b/sbroad-core/src/backend/sql/space.rs
@@ -12,8 +12,7 @@ mod prod_imports {
     pub use crate::error;
     pub use crate::errors::{Action, Entity, SbroadError};
     pub use crate::executor::engine::helpers::{pk_name, table_name};
-    pub use crate::executor::ir::{ExecutionPlan, Column};
-    pub use crate::ir::value::{EncodedValue, Value};
+    pub use crate::executor::ir::ExecutionPlan;
     use crate::executor::protocol::VTablesMeta;
     pub use crate::ir::node::NodeId;
     pub use crate::ir::relation::SpaceEngine;
diff --git a/sbroad-core/src/backend/sql/tree.rs b/sbroad-core/src/backend/sql/tree.rs
index c3cb4f9bf..e29b0a6c2 100644
--- a/sbroad-core/src/backend/sql/tree.rs
+++ b/sbroad-core/src/backend/sql/tree.rs
@@ -10,9 +10,9 @@ use crate::ir::node::expression::Expression;
 use crate::ir::node::relational::Relational;
 use crate::ir::node::{
     Alias, ArithmeticExpr, BoolExpr, Case, Cast, Concat, Except, ExprInParentheses, GroupBy,
-    Having, Intersect, Join, Limit, Motion, Node, NodeId, OrderBy, Projection, Reference, Row,
-    ScanCte, ScanRelation, ScanSubQuery, Selection, StableFunction, Trim, UnaryExpr, Union,
-    UnionAll, Values, ValuesRow,
+    Having, Intersect, Join, Limit, Motion, Node, NodeId, OrderBy, Projection, Reference,
+    ReferenceAsteriskSource, Row, ScanCte, ScanRelation, ScanSubQuery, Selection, StableFunction,
+    Trim, UnaryExpr, Union, UnionAll, Values, ValuesRow,
 };
 use crate::ir::operator::{Bool, OrderByElement, OrderByEntity, OrderByType, Unary};
 use crate::ir::transformation::redistribution::{MotionOpcode, MotionPolicy};
@@ -624,7 +624,7 @@ impl<'p> SyntaxPlan<'p> {
             .get_node(plan_id)
             .expect("Plan node expected for popping.");
 
-        if let Node::Relational(Relational::Motion { children, .. }) = requested_plan_node {
+        if let Node::Relational(Relational::Motion(Motion { children, .. })) = requested_plan_node {
             let motion_child_id = children.first();
             let motion_to_fix_id = if let Some(motion_child_id) = motion_child_id {
                 let motion_child_node = self
@@ -649,9 +649,9 @@ impl<'p> SyntaxPlan<'p> {
                 .get_ir_plan()
                 .get_relation_node(motion_to_fix_id)
                 .expect("Plan node expected for popping.");
-            if let Relational::Motion {
+            if let Relational::Motion(Motion {
                 policy, program, ..
-            } = motion_to_fix_node
+            }) = motion_to_fix_node
             {
                 let mut should_cover_with_parentheses = true;
 
@@ -876,11 +876,11 @@ impl<'p> SyntaxPlan<'p> {
         let mut syntax_gr_cols = Vec::with_capacity(plan_gr_cols.len());
         // Reuse the same vector to avoid extra allocations
         // (replace plan node ids with syntax node ids).
-        for col_id in &mut sn_gr_cols {
-            *col_id = self.pop_from_stack(*col_id, id);
+        for col_id in &plan_gr_cols {
+            syntax_gr_cols.push(self.pop_from_stack(*col_id, id));
         }
         let child_sn_id = self.pop_from_stack(child_plan_id, id);
-        let mut sn_children = Vec::with_capacity(sn_gr_cols.len() * 2 - 1);
+        let mut sn_children = Vec::with_capacity(syntax_gr_cols.len() * 2 - 1);
         // The columns are in reverse order, so we need to reverse them back.
         if let Some((first, others)) = syntax_gr_cols.split_first() {
             for id in others.iter().rev() {
@@ -1175,7 +1175,7 @@ impl<'p> SyntaxPlan<'p> {
         let mut syntax_children = Vec::with_capacity(plan_children.len());
 
         for child_id in &plan_children {
-            syntax_children.push(self.pop_from_stack(*child_id));
+            syntax_children.push(self.pop_from_stack(*child_id, id));
         }
 
         // Consume the output from the stack.
@@ -1414,7 +1414,7 @@ impl<'p> SyntaxPlan<'p> {
                         .get_expression_node(*child_id)
                         .expect("row child is expression");
                     if matches!(expr, Expression::Reference(_)) {
-                        let referred_id = *plan
+                        let referred_id = plan
                             .get_relational_from_reference_node(*child_id)
                             .expect("referred id");
                         self.pop_from_stack(referred_id, id);
@@ -1455,7 +1455,7 @@ impl<'p> SyntaxPlan<'p> {
                         .get_expression_node(*child_id)
                         .expect("row child is expression");
                     if matches!(expr, Expression::Reference(_)) {
-                        let referred_id = *plan
+                        let referred_id = plan
                             .get_relational_from_reference_node(*child_id)
                             .expect("referred id");
                         sq_sn_id = Some(self.pop_from_stack(referred_id, id));
@@ -1514,14 +1514,14 @@ impl<'p> SyntaxPlan<'p> {
                 };
 
                 let mut nodes_to_add = Vec::new();
-                if let Expression::Reference {
+                if let Expression::Reference(Reference {
                     asterisk_source:
                         Some(ReferenceAsteriskSource {
                             relation_name,
                             asterisk_id,
                         }),
                     ..
-                } = expr_node
+                }) = expr_node
                 {
                     // If we reference ScanNode, we don't want to transform asterisks
                     // in order not to select "bucket_id". That's why we save them as a
@@ -1583,23 +1583,24 @@ impl<'p> SyntaxPlan<'p> {
             let sn_node = self.nodes.get_sn(sn_id);
             let sn_plan_node_pair = self.get_plan_node(&sn_node.data)?;
 
-            let nodes_to_add = if let Some((Node::Expression(node_expr), sn_plan_node_id)) =
-                sn_plan_node_pair
-            {
-                match node_expr {
-                    Expression::Alias { child, .. } => handle_reference(sn_id, need_comma, *child),
-                    _ => handle_reference(sn_id, need_comma, sn_plan_node_id),
-                }
-            } else {
-                // As it's not ad Alias under Projection output, we don't have to
-                // dead with its machinery flags.
-                let mut nodes_to_add = Vec::new();
-                nodes_to_add.push(NodeToAdd::SnId(sn_id));
-                if need_comma {
-                    nodes_to_add.push(NodeToAdd::Comma)
-                }
-                nodes_to_add
-            };
+            let nodes_to_add =
+                if let Some((Node::Expression(node_expr), sn_plan_node_id)) = sn_plan_node_pair {
+                    match node_expr {
+                        Expression::Alias(Alias { child, .. }) => {
+                            handle_reference(sn_id, need_comma, *child)
+                        }
+                        _ => handle_reference(sn_id, need_comma, sn_plan_node_id),
+                    }
+                } else {
+                    // As it's not ad Alias under Projection output, we don't have to
+                    // dead with its machinery flags.
+                    let mut nodes_to_add = Vec::new();
+                    nodes_to_add.push(NodeToAdd::SnId(sn_id));
+                    if need_comma {
+                        nodes_to_add.push(NodeToAdd::Comma)
+                    }
+                    nodes_to_add
+                };
 
             for node in nodes_to_add {
                 match node {
@@ -1748,7 +1749,7 @@ impl<'p> SyntaxPlan<'p> {
     ///
     /// # Errors
     /// - syntax node wraps an invalid plan node
-    pub fn get_plan_node(&self, data: &SyntaxData) -> Result<Option<(&Node, NodeId)>, SbroadError> {
+    pub fn get_plan_node(&self, data: &SyntaxData) -> Result<Option<(Node, NodeId)>, SbroadError> {
         if let SyntaxData::PlanId(id) = data {
             Ok(Some((self.plan.get_ir_plan().get_node(*id)?, *id)))
         } else {
@@ -1761,7 +1762,7 @@ impl<'p> SyntaxPlan<'p> {
     /// # Errors
     /// - plan node is invalid
     /// - syntax tree node doesn't have a plan node
-    pub fn plan_node_or_err(&self, data: &SyntaxData) -> Result<(&Node, NodeId), SbroadError> {
+    pub fn plan_node_or_err(&self, data: &SyntaxData) -> Result<(Node, NodeId), SbroadError> {
         self.get_plan_node(data)?.ok_or_else(|| {
             SbroadError::Invalid(
                 Entity::SyntaxPlan,
diff --git a/sbroad-core/src/executor/engine/helpers.rs b/sbroad-core/src/executor/engine/helpers.rs
index 5107e702f..09213ebc2 100644
--- a/sbroad-core/src/executor/engine/helpers.rs
+++ b/sbroad-core/src/executor/engine/helpers.rs
@@ -1088,7 +1088,7 @@ pub fn materialize_motion(
 
     // We should get a motion alias name before we take the subtree in `dispatch` method.
     let motion_node = plan.get_ir_plan().get_relation_node(motion_node_id)?;
-    let alias = if let Relational::Motion { alias, .. } = motion_node {
+    let alias = if let Relational::Motion(Motion { alias, .. }) = motion_node {
         alias.clone()
     } else {
         panic!("Expected motion node, got {motion_node:?}");
@@ -1103,7 +1103,7 @@ pub fn materialize_motion(
         .expect("must've failed earlier");
     // Unlink motion node's child sub tree (it is already replaced with invalid values).
     plan.unlink_motion_subtree(motion_node_id)?;
-    let mut vtable = result.as_virtual_table(column_names, possibly_incorrect_types)?;
+    let mut vtable = result.as_virtual_table(possibly_incorrect_types)?;
 
     if let Some(name) = alias {
         vtable.set_alias(name.as_str());
diff --git a/sbroad-core/src/executor/ir.rs b/sbroad-core/src/executor/ir.rs
index 959c4ad8c..f68740ef3 100644
--- a/sbroad-core/src/executor/ir.rs
+++ b/sbroad-core/src/executor/ir.rs
@@ -9,12 +9,12 @@ use crate::errors::{Action, Entity, SbroadError};
 use crate::executor::engine::Vshard;
 use crate::executor::vtable::{VirtualTable, VirtualTableMap};
 use crate::ir::node::expression::ExprOwned;
+use crate::ir::node::expression::{Expression, MutExpression};
 use crate::ir::node::relational::{MutRelational, RelOwned, Relational};
 use crate::ir::node::{
     Alias, ArenaType, ArithmeticExpr, BoolExpr, Case, Cast, Concat, Delete, ExprInParentheses,
     GroupBy, Having, Insert, Join, Motion, Node, Node136, NodeId, NodeOwned, OrderBy, Reference,
-    Row, ScanCte, ScanRelation, ScanSubQuery, Selection, StableFunction, Trim, UnaryExpr, Update,
-    ValuesRow,
+    Row, ScanCte, ScanRelation, Selection, StableFunction, Trim, UnaryExpr, Update, ValuesRow,
 };
 use crate::ir::operator::{OrderByElement, OrderByEntity};
 use crate::ir::relation::SpaceEngine;
@@ -453,13 +453,12 @@ impl ExecutionPlan {
             HashMap::with_capacity(vtables_capacity);
 
         let mut new_plan = Plan::new();
-        new_plan.nodes.reserve(nodes.len());
 
         // In case we have a Motion among rel node children (maybe not direct), we
         // need to rename rel output aliases, because Motion
         // may have changed them according to its vtable column names.
         // This map tracks outputs of rel nodes that have changed their aliases.
-        let mut rel_renamed_output_lists: HashMap<usize, Vec<usize>> = HashMap::new();
+        let mut rel_renamed_output_lists: HashMap<NodeId, Vec<NodeId>> = HashMap::new();
 
         for LevelNode(_, node_id) in nodes {
             // We have already processed this node (sub-queries in BETWEEN
@@ -520,7 +519,7 @@ impl ExecutionPlan {
                                 .clone();
                             new_plan.add_rel(table);
                         }
-                        Relational::Motion {
+                        RelOwned::Motion(Motion {
                             children,
                             policy,
                             output,
@@ -535,7 +534,7 @@ impl ExecutionPlan {
                                 // We need to fix motion aliases based on materialized
                                 // vtable. Motion's aliases will copy "COL_i" naming of
                                 // vtable.
-                                let output_list: Vec<usize> =
+                                let output_list: Vec<NodeId> =
                                     new_plan.get_row_list(subtree_map.get_id(*output))?.to_vec();
 
                                 if node_id != top {
@@ -549,7 +548,7 @@ impl ExecutionPlan {
                                             let alias = new_plan.get_mut_expression_node(
                                                 *alias_id
                                             )?;
-                                            let Expression::Alias { ref mut name, .. } = alias else {
+                                            let MutExpression::Alias(Alias { ref mut name, .. }) = alias else {
                                                 return Err(SbroadError::Invalid(
                                                     Entity::Expression,
                                                     Some(format_smolstr!("Expected Alias under Motion output, got {alias:?}"))
@@ -629,8 +628,8 @@ impl ExecutionPlan {
                         *child_id = subtree_map.get_id(*child_id);
 
                         let child_rel_node = new_plan.get_relation_node(*child_id)?;
-                        if let Relational::Motion { output, .. } = child_rel_node {
-                            let motion_output_list: Vec<usize> =
+                        if let Relational::Motion(Motion { output, .. }) = child_rel_node {
+                            let motion_output_list: Vec<NodeId> =
                                 new_plan.get_row_list(*output)?.to_vec();
                             rel_renamed_output_lists.insert(*child_id, motion_output_list);
                         }
@@ -644,20 +643,20 @@ impl ExecutionPlan {
                     // only References that have an Asterisk source.
                     // References without asterisks would be covered with aliases like
                     // "COL_1 as <alias>".
-                    let is_projection = matches!(rel, Relational::Projection { .. });
+                    let is_projection = matches!(rel, RelOwned::Projection(_));
                     if !rel_renamed_output_lists.is_empty() {
-                        let rel_output_list: Vec<usize> =
-                            new_plan.get_row_list(rel.output())?.to_vec();
+                        let rel_output_list: Vec<NodeId> =
+                            new_plan.get_row_list(*rel.mut_output())?.to_vec();
 
                         for output_id in &rel_output_list {
                             let ref_under_alias = new_plan.get_child_under_alias(*output_id)?;
                             let ref_expr = new_plan.get_expression_node(ref_under_alias)?;
-                            let Expression::Reference {
+                            let Expression::Reference(Reference {
                                 position,
                                 targets,
                                 asterisk_source,
                                 ..
-                            } = ref_expr
+                            }) = ref_expr
                             else {
                                 continue;
                             };
@@ -699,7 +698,8 @@ impl ExecutionPlan {
                                 let child_alias_name = child_alias.get_alias_name()?.to_smolstr();
 
                                 let rel_alias = new_plan.get_mut_expression_node(*output_id)?;
-                                if let Expression::Alias { ref mut name, .. } = rel_alias {
+                                if let MutExpression::Alias(Alias { ref mut name, .. }) = rel_alias
+                                {
                                     *name = child_alias_name;
                                 } else {
                                     panic!("Expected alias under Row output list");
@@ -707,10 +707,31 @@ impl ExecutionPlan {
                             }
                         }
 
+                        let arena_type = match rel {
+                            RelOwned::Union(_)
+                            | RelOwned::UnionAll(_)
+                            | RelOwned::Except(_)
+                            | RelOwned::Values(_)
+                            | RelOwned::Intersect(_)
+                            | RelOwned::Limit(_) => ArenaType::Arena32,
+                            RelOwned::ScanCte(_)
+                            | RelOwned::Selection(_)
+                            | RelOwned::Having(_)
+                            | RelOwned::ValuesRow(_)
+                            | RelOwned::OrderBy(_)
+                            | RelOwned::ScanRelation(_)
+                            | RelOwned::Join(_)
+                            | RelOwned::Delete(_)
+                            | RelOwned::ScanSubQuery(_)
+                            | RelOwned::GroupBy(_)
+                            | RelOwned::Projection(_) => ArenaType::Arena64,
+                            RelOwned::Insert(_) => ArenaType::Arena96,
+                            RelOwned::Update(_) | RelOwned::Motion(_) => ArenaType::Arena136,
+                        };
+                        let next_id = new_plan.nodes.next_id(arena_type);
+
                         rel_renamed_output_lists.insert(next_id, rel_output_list);
                     }
-
-                    new_plan.replace_parent_in_subtree(rel.output(), None, Some(next_id))?;
                 }
                 NodeOwned::Expression(ref mut expr) => match expr {
                     ExprOwned::Alias(Alias { ref mut child, .. })
diff --git a/sbroad-core/src/executor/result.rs b/sbroad-core/src/executor/result.rs
index 8c0ed784e..422757460 100644
--- a/sbroad-core/src/executor/result.rs
+++ b/sbroad-core/src/executor/result.rs
@@ -12,7 +12,6 @@
 use core::fmt::Debug;
 use serde::ser::{Serialize, SerializeMap, Serializer};
 use serde::{Deserialize, Deserializer};
-use smol_str::{SmolStr, ToSmolStr};
 use std::collections::HashMap;
 use tarantool::tlua::{self, LuaRead};
 use tarantool::tuple::Encode;
diff --git a/sbroad-core/src/executor/tests.rs b/sbroad-core/src/executor/tests.rs
index 859cb4777..35c3a860d 100644
--- a/sbroad-core/src/executor/tests.rs
+++ b/sbroad-core/src/executor/tests.rs
@@ -5,7 +5,6 @@ use crate::backend::sql::ir::PatternWithParams;
 use crate::executor::engine::mock::RouterRuntimeMock;
 use crate::executor::result::ProducerResult;
 use crate::executor::vtable::VirtualTable;
-use crate::ir::operator::Relational;
 use crate::ir::tests::vcolumn_integer_user_non_null;
 use crate::ir::transformation::redistribution::MotionPolicy;
 use smol_str::SmolStr;
@@ -976,9 +975,9 @@ fn groupby_linker_test() {
             LuaValue::String(String::from(PatternWithParams::new(
                 format!(
                     "{} {} {}",
-                    r#"SELECT "column_764" as "ii" FROM"#,
+                    r#"SELECT "column_596" as "ii" FROM"#,
                     r#"(SELECT "COL_1" FROM "TMP_test_0136")"#,
-                    r#"GROUP BY "column_764""#,
+                    r#"GROUP BY "column_596""#,
                 ),
                 vec![],
             ))),
diff --git a/sbroad-core/src/executor/tests/exec_plan.rs b/sbroad-core/src/executor/tests/exec_plan.rs
index 00cc91b2d..38a1c4464 100644
--- a/sbroad-core/src/executor/tests/exec_plan.rs
+++ b/sbroad-core/src/executor/tests/exec_plan.rs
@@ -130,7 +130,7 @@ fn exec_plan_subtree_two_stage_groupby_test() {
     assert_eq!(
         sql,
         PatternWithParams::new(
-            r#"SELECT "T1"."FIRST_NAME" as "column_764" FROM "test_space" as "T1" GROUP BY "T1"."FIRST_NAME""#
+            r#"SELECT "T1"."FIRST_NAME" as "column_596" FROM "test_space" as "T1" GROUP BY "T1"."FIRST_NAME""#
                 .to_string(),
             vec![]
         )
@@ -187,9 +187,9 @@ fn exec_plan_subtree_two_stage_groupby_test_2() {
         sql,
         PatternWithParams::new(
             f_sql(
-                r#"SELECT "T1"."FIRST_NAME" as "column_12",
-"T1"."sys_op" as "column_13",
-"T1"."sysFrom" as "column_14"
+                r#"SELECT "T1"."FIRST_NAME" as "column_596",
+"T1"."sys_op" as "column_696",
+"T1"."sysFrom" as "column_796"
 FROM "test_space" as "T1"
 GROUP BY "T1"."FIRST_NAME", "T1"."sys_op", "T1"."sysFrom""#
             ),
@@ -205,7 +205,7 @@ GROUP BY "T1"."FIRST_NAME", "T1"."sys_op", "T1"."sysFrom""#
             f_sql(
                 r#"SELECT "COL_1" as "FIRST_NAME",
 "COL_2" as "sys_op", "COL_3" as "sysFrom"
-FROM (SELECT "COL_1","COL_2","COL_3" FROM "TMP_test_14")
+FROM (SELECT "COL_1","COL_2","COL_3" FROM "TMP_test_0136")
 GROUP BY "COL_1", "COL_2", "COL_3""#
             ),
             vec![]
@@ -265,11 +265,11 @@ fn exec_plan_subtree_aggregates() {
         PatternWithParams::new(
             format!(
                 "{} {} {} {} {} {} {}",
-                r#"SELECT "T1"."sys_op" as "column_764", "T1"."id" as "column_2864","#,
-                r#"("T1"."id") * ("T1"."sys_op") as "column_1632", group_concat ("T1"."FIRST_NAME", ?) as "group_concat_496","#,
-                r#"count ("T1"."sysFrom") as "count_096", total ("T1"."id") as "total_696","#,
-                r#"min ("T1"."id") as "min_796", count ("T1"."id") as "count_596","#,
-                r#"max ("T1"."id") as "max_896", sum ("T1"."id") as "sum_196""#,
+                r#"SELECT "T1"."sys_op" as "column_596", ("T1"."id") * ("T1"."sys_op") as "column_1632","#,
+                r#""T1"."id" as "column_2096", group_concat ("T1"."FIRST_NAME", ?) as "group_concat_2496","#,
+                r#"count ("T1"."sysFrom") as "count_1596", total ("T1"."id") as "total_2896","#,
+                r#"min ("T1"."id") as "min_3096", count ("T1"."id") as "count_2696","#,
+                r#"max ("T1"."id") as "max_3296", sum ("T1"."id") as "sum_1796""#,
                 r#"FROM "test_space" as "T1""#,
                 r#"GROUP BY "T1"."sys_op", ("T1"."id") * ("T1"."sys_op"), "T1"."id""#,
             ),
@@ -337,7 +337,7 @@ fn exec_plan_subtree_aggregates_no_groupby() {
     assert_eq!(
         sql,
         PatternWithParams::new(
-            r#"SELECT ("T1"."id") + ("T1"."sysFrom") as "column_632", count ("T1"."sysFrom") as "count_096" FROM "test_space" as "T1" GROUP BY ("T1"."id") + ("T1"."sysFrom")"#.to_string(),
+            r#"SELECT ("T1"."id") + ("T1"."sysFrom") as "column_632", count ("T1"."sysFrom") as "count_696" FROM "test_space" as "T1" GROUP BY ("T1"."id") + ("T1"."sysFrom")"#.to_string(),
             vec![]
         ));
 
@@ -546,7 +546,7 @@ fn exec_plan_subtree_count_asterisk() {
     assert_eq!(
         sql,
         PatternWithParams::new(
-            r#"SELECT count (*) as "count_096" FROM "test_space""#.to_string(),
+            r#"SELECT count (*) as "count_596" FROM "test_space""#.to_string(),
             vec![]
         )
     );
@@ -615,8 +615,8 @@ fn exec_plan_subtree_having() {
         PatternWithParams::new(
             format!(
                 "{} {} {}",
-                r#"SELECT "T1"."sys_op" as "column_764", ("T1"."sys_op") * (?) as "column_2032","#,
-                r#"count (("T1"."sys_op") * (?)) as "count_196" FROM "test_space" as "T1""#,
+                r#"SELECT "T1"."sys_op" as "column_596", ("T1"."sys_op") * (?) as "column_2032","#,
+                r#"count (("T1"."sys_op") * (?)) as "count_2296" FROM "test_space" as "T1""#,
                 r#"GROUP BY "T1"."sys_op", ("T1"."sys_op") * (?)"#,
             ),
             vec![Value::Unsigned(2), Value::Unsigned(2), Value::Unsigned(2)]
@@ -699,7 +699,7 @@ fn exec_plan_subtree_having_without_groupby() {
             format!(
                 "{} {} {}",
                 r#"SELECT ("T1"."sys_op") * (?) as "column_1332","#,
-                r#"count (("T1"."sys_op") * (?)) as "count_196" FROM "test_space" as "T1""#,
+                r#"count (("T1"."sys_op") * (?)) as "count_1496" FROM "test_space" as "T1""#,
                 r#"GROUP BY ("T1"."sys_op") * (?)"#,
             ),
             vec![Value::Unsigned(2), Value::Unsigned(2), Value::Unsigned(2)]
@@ -1125,8 +1125,7 @@ fn local_translation_asterisk_single() {
     let exec_plan = query.get_mut_exec_plan();
     let top_id = exec_plan.get_ir_plan().get_top().unwrap();
 
-    let sql =
-        get_sql_from_execution_plan(exec_plan, top_id, Snapshot::Oldest, &Buckets::All, "test");
+    let sql = get_sql_from_execution_plan(exec_plan, top_id, Snapshot::Oldest, TEMPLATE);
     assert_eq!(
         sql,
         PatternWithParams::new(
@@ -1145,8 +1144,7 @@ fn local_translation_asterisk_several() {
     let exec_plan = query.get_mut_exec_plan();
     let top_id = exec_plan.get_ir_plan().get_top().unwrap();
 
-    let sql =
-        get_sql_from_execution_plan(exec_plan, top_id, Snapshot::Oldest, &Buckets::All, "test");
+    let sql = get_sql_from_execution_plan(exec_plan, top_id, Snapshot::Oldest, TEMPLATE);
     assert_eq!(
         sql,
         PatternWithParams::new(
@@ -1168,8 +1166,7 @@ fn local_translation_asterisk_named() {
     let exec_plan = query.get_mut_exec_plan();
     let top_id = exec_plan.get_ir_plan().get_top().unwrap();
 
-    let sql =
-        get_sql_from_execution_plan(exec_plan, top_id, Snapshot::Oldest, &Buckets::All, "test");
+    let sql = get_sql_from_execution_plan(exec_plan, top_id, Snapshot::Oldest, TEMPLATE);
     assert_eq!(
         sql,
         PatternWithParams::new(
@@ -1191,8 +1188,7 @@ fn local_translation_asterisk_with_additional_columns() {
     let exec_plan = query.get_mut_exec_plan();
     let top_id = exec_plan.get_ir_plan().get_top().unwrap();
 
-    let sql =
-        get_sql_from_execution_plan(exec_plan, top_id, Snapshot::Oldest, &Buckets::All, "test");
+    let sql = get_sql_from_execution_plan(exec_plan, top_id, Snapshot::Oldest, TEMPLATE);
     assert_eq!(
         sql,
         PatternWithParams::new(
@@ -1225,7 +1221,7 @@ fn exec_plan_order_by() {
     {
         virtual_table.reshard(key, &query.coordinator).unwrap();
     }
-    let mut vtables: HashMap<usize, Rc<VirtualTable>> = HashMap::new();
+    let mut vtables: HashMap<NodeId, Rc<VirtualTable>> = HashMap::new();
     vtables.insert(motion_id, Rc::new(virtual_table));
 
     let exec_plan = query.get_mut_exec_plan();
@@ -1234,13 +1230,7 @@ fn exec_plan_order_by() {
     let motion_child_id = exec_plan.get_motion_subtree_root(motion_id).unwrap();
 
     // Check sub-query
-    let sql = get_sql_from_execution_plan(
-        exec_plan,
-        motion_child_id,
-        Snapshot::Oldest,
-        &Buckets::All,
-        "test",
-    );
+    let sql = get_sql_from_execution_plan(exec_plan, motion_child_id, Snapshot::Oldest, TEMPLATE);
     assert_eq!(
         sql,
         PatternWithParams::new(
@@ -1250,8 +1240,7 @@ fn exec_plan_order_by() {
     );
 
     // Check main query
-    let sql =
-        get_sql_from_execution_plan(exec_plan, top_id, Snapshot::Oldest, &Buckets::All, "test");
+    let sql = get_sql_from_execution_plan(exec_plan, top_id, Snapshot::Oldest, TEMPLATE);
     assert_eq!(
         sql,
         PatternWithParams::new(
@@ -1321,7 +1310,7 @@ fn exec_plan_order_by_with_join() {
 
     let mut query = Query::new(&coordinator, sql, vec![]).unwrap();
     let slices = query.exec_plan.get_ir_plan().clone_slices();
-    let mut sq_vtables: HashMap<usize, Rc<VirtualTable>> = HashMap::new();
+    let mut sq_vtables: HashMap<NodeId, Rc<VirtualTable>> = HashMap::new();
 
     let sq_motion_id = *slices.slice(0).unwrap().position(0).unwrap();
     let mut sq_vtable = VirtualTable::new();
@@ -1353,13 +1342,8 @@ fn exec_plan_order_by_with_join() {
 
     // Check sub-query.
     let sq_motion_child_id = exec_plan.get_motion_subtree_root(sq_motion_id).unwrap();
-    let sql = get_sql_from_execution_plan(
-        exec_plan,
-        sq_motion_child_id,
-        Snapshot::Oldest,
-        &Buckets::All,
-        "test",
-    );
+    let sql =
+        get_sql_from_execution_plan(exec_plan, sq_motion_child_id, Snapshot::Oldest, TEMPLATE);
     assert_eq!(
         sql,
         PatternWithParams::new(r#"SELECT "t"."a" FROM "t""#.to_string(), vec![])
@@ -1373,24 +1357,22 @@ fn exec_plan_order_by_with_join() {
         exec_plan,
         order_by_motion_child_id,
         Snapshot::Oldest,
-        &Buckets::All,
-        "test",
+        TEMPLATE,
     );
     assert_eq!(
         sql,
         PatternWithParams::new(
-            r#"SELECT * FROM (SELECT "t"."a" FROM "t") as "f" INNER JOIN (SELECT "COL_1" FROM "TMP_test_28") as "s" ON ?"#.to_string(),
+            r#"SELECT * FROM (SELECT "t"."a" FROM "t") as "f" INNER JOIN (SELECT "COL_1" FROM "TMP_test_0136") as "s" ON ?"#.to_string(),
             vec![Value::Boolean(true)]
         )
     );
 
     // Check main query.
-    let sql =
-        get_sql_from_execution_plan(exec_plan, top_id, Snapshot::Oldest, &Buckets::All, "test");
+    let sql = get_sql_from_execution_plan(exec_plan, top_id, Snapshot::Oldest, TEMPLATE);
     assert_eq!(
         sql,
         PatternWithParams::new(
-            r#"SELECT "COL_1" as "a", "COL_2" as "a" FROM (SELECT "COL_1","COL_2" FROM "TMP_test_10") ORDER BY 1"#.to_string(),
+            r#"SELECT "COL_1" as "a", "COL_2" as "a" FROM (SELECT "COL_1","COL_2" FROM "TMP_test_0136") ORDER BY 1"#.to_string(),
             vec![]
         ));
 }
diff --git a/sbroad-core/src/frontend/sql.rs b/sbroad-core/src/frontend/sql.rs
index 7a552669b..092c7dcca 100644
--- a/sbroad-core/src/frontend/sql.rs
+++ b/sbroad-core/src/frontend/sql.rs
@@ -3,6 +3,7 @@
 //! Parses an SQL statement to the abstract syntax tree (AST)
 //! and builds the intermediate representation (IR).
 
+use crate::ir::node::ReferenceAsteriskSource;
 use ahash::{AHashMap, AHashSet};
 use core::panic;
 use itertools::Itertools;
@@ -33,7 +34,7 @@ use crate::ir::expression::{
     ColumnPositionMap, ColumnWithScan, ColumnsRetrievalSpec, ExpressionId, FunctionFeature,
     Position, TrimKind,
 };
-use crate::ir::node::expression::{Expression, MutExpression, ReferenceAsteriskSource};
+use crate::ir::node::expression::{Expression, MutExpression};
 use crate::ir::node::relational::Relational;
 use crate::ir::node::{
     AlterSystem, AlterUser, BoolExpr, Constant, CountAsterisk, CreateIndex, CreateProc, CreateRole,
diff --git a/sbroad-core/src/frontend/sql/ir/tests.rs b/sbroad-core/src/frontend/sql/ir/tests.rs
index 7c23ef7c7..412118e26 100644
--- a/sbroad-core/src/frontend/sql/ir/tests.rs
+++ b/sbroad-core/src/frontend/sql/ir/tests.rs
@@ -822,9 +822,9 @@ fn front_order_by_over_single_distribution_must_not_add_motion() {
     order by ("id_count"::integer)
         projection ("id_count"::integer -> "id_count")
             scan
-                projection (sum(("count_096"::integer))::decimal -> "id_count")
+                projection (sum(("count_696"::integer))::decimal -> "id_count")
                     motion [policy: full]
-                        projection (count(("test_space"."id"::unsigned))::integer -> "count_096")
+                        projection (count(("test_space"."id"::unsigned))::integer -> "count_696")
                             scan "test_space"
 execution options:
 sql_vdbe_max_steps = 45000
@@ -1319,10 +1319,10 @@ fn front_sql_groupby() {
 
     let plan = sql_to_optimized_ir(input, vec![]);
     let expected_explain = String::from(
-        r#"projection ("column_764"::integer -> "identification_number", "column_864"::string -> "product_code")
-    group by ("column_764"::integer, "column_864"::string) output: ("column_764"::integer -> "column_764", "column_864"::string -> "column_864")
-        motion [policy: segment([ref("column_764"), ref("column_864")])]
-            projection ("hash_testing"."identification_number"::integer -> "column_764", "hash_testing"."product_code"::string -> "column_864")
+        r#"projection ("column_596"::integer -> "identification_number", "column_696"::string -> "product_code")
+    group by ("column_596"::integer, "column_696"::string) output: ("column_596"::integer -> "column_596", "column_696"::string -> "column_696")
+        motion [policy: segment([ref("column_596"), ref("column_696")])]
+            projection ("hash_testing"."identification_number"::integer -> "column_596", "hash_testing"."product_code"::string -> "column_696")
                 group by ("hash_testing"."identification_number"::integer, "hash_testing"."product_code"::string) output: ("hash_testing"."identification_number"::integer -> "identification_number", "hash_testing"."product_code"::string -> "product_code", "hash_testing"."product_units"::boolean -> "product_units", "hash_testing"."sys_op"::unsigned -> "sys_op", "hash_testing"."bucket_id"::unsigned -> "bucket_id")
                     scan "hash_testing"
 execution options:
@@ -1331,6 +1331,7 @@ vtable_max_rows = 5000
 "#,
     );
 
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 }
 
@@ -1344,10 +1345,10 @@ fn front_sql_groupby_less_cols_in_proj() {
     let plan = sql_to_optimized_ir(input, vec![]);
 
     let expected_explain = String::from(
-        r#"projection ("column_764"::integer -> "identification_number")
-    group by ("column_764"::integer, "column_864"::boolean) output: ("column_764"::integer -> "column_764", "column_864"::boolean -> "column_864")
-        motion [policy: segment([ref("column_764"), ref("column_864")])]
-            projection ("hash_testing"."identification_number"::integer -> "column_764", "hash_testing"."product_units"::boolean -> "column_864")
+        r#"projection ("column_596"::integer -> "identification_number")
+    group by ("column_596"::integer, "column_696"::boolean) output: ("column_596"::integer -> "column_596", "column_696"::boolean -> "column_696")
+        motion [policy: segment([ref("column_596"), ref("column_696")])]
+            projection ("hash_testing"."identification_number"::integer -> "column_596", "hash_testing"."product_units"::boolean -> "column_696")
                 group by ("hash_testing"."identification_number"::integer, "hash_testing"."product_units"::boolean) output: ("hash_testing"."identification_number"::integer -> "identification_number", "hash_testing"."product_code"::string -> "product_code", "hash_testing"."product_units"::boolean -> "product_units", "hash_testing"."sys_op"::unsigned -> "sys_op", "hash_testing"."bucket_id"::unsigned -> "bucket_id")
                     scan "hash_testing"
 execution options:
@@ -1356,6 +1357,7 @@ vtable_max_rows = 5000
 "#,
     );
 
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 }
 
@@ -1370,10 +1372,10 @@ fn front_sql_groupby_union_1() {
 
     let expected_explain = String::from(
         r#"union all
-    projection ("column_764"::integer -> "identification_number")
-        group by ("column_764"::integer) output: ("column_764"::integer -> "column_764")
-            motion [policy: segment([ref("column_764")])]
-                projection ("hash_testing"."identification_number"::integer -> "column_764")
+    projection ("column_596"::integer -> "identification_number")
+        group by ("column_596"::integer) output: ("column_596"::integer -> "column_596")
+            motion [policy: segment([ref("column_596")])]
+                projection ("hash_testing"."identification_number"::integer -> "column_596")
                     group by ("hash_testing"."identification_number"::integer) output: ("hash_testing"."identification_number"::integer -> "identification_number", "hash_testing"."product_code"::string -> "product_code", "hash_testing"."product_units"::boolean -> "product_units", "hash_testing"."sys_op"::unsigned -> "sys_op", "hash_testing"."bucket_id"::unsigned -> "bucket_id")
                         scan "hash_testing"
     projection ("hash_testing"."identification_number"::integer -> "identification_number")
@@ -1404,13 +1406,12 @@ fn front_sql_groupby_union_2() {
     projection ("identification_number"::integer -> "identification_number")
         scan
             union all
-                projection ("column_28"::integer -> "identification_number")
-                    group by ("column_28"::integer) output: ("column_28"::integer -> "column_28")
-                        motion [policy: segment([ref("column_28")])]
-                            scan
-                                projection ("hash_testing"."identification_number"::integer -> "column_28")
-                                    group by ("hash_testing"."identification_number"::integer) output: ("hash_testing"."identification_number"::integer -> "identification_number", "hash_testing"."product_code"::string -> "product_code", "hash_testing"."product_units"::boolean -> "product_units", "hash_testing"."sys_op"::unsigned -> "sys_op", "hash_testing"."bucket_id"::unsigned -> "bucket_id")
-                                        scan "hash_testing"
+                projection ("column_1196"::integer -> "identification_number")
+                    group by ("column_1196"::integer) output: ("column_1196"::integer -> "column_1196")
+                        motion [policy: segment([ref("column_1196")])]
+                            projection ("hash_testing"."identification_number"::integer -> "column_1196")
+                                group by ("hash_testing"."identification_number"::integer) output: ("hash_testing"."identification_number"::integer -> "identification_number", "hash_testing"."product_code"::string -> "product_code", "hash_testing"."product_units"::boolean -> "product_units", "hash_testing"."sys_op"::unsigned -> "sys_op", "hash_testing"."bucket_id"::unsigned -> "bucket_id")
+                                    scan "hash_testing"
                 projection ("hash_testing"."identification_number"::integer -> "identification_number")
                     scan "hash_testing"
 execution options:
@@ -1419,6 +1420,7 @@ vtable_max_rows = 5000
 "#,
     );
 
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 }
 
@@ -1432,12 +1434,11 @@ fn front_sql_groupby_join_1() {
         "#;
 
     let plan = sql_to_optimized_ir(input, vec![]);
-    println!("{}", plan.as_explain().unwrap());
     let expected_explain = String::from(
-        r#"projection ("column_63"::string -> "product_code", "column_64"::boolean -> "product_units")
-    group by ("column_63"::string, "column_64"::boolean) output: ("column_63"::string -> "column_63", "column_64"::boolean -> "column_64")
-        motion [policy: segment([ref("column_63"), ref("column_64")])]
-            projection ("t2"."product_code"::string -> "column_63", "t2"."product_units"::boolean -> "column_64")
+        r#"projection ("column_2496"::string -> "product_code", "column_2596"::boolean -> "product_units")
+    group by ("column_2496"::string, "column_2596"::boolean) output: ("column_2496"::string -> "column_2496", "column_2596"::boolean -> "column_2596")
+        motion [policy: segment([ref("column_2496"), ref("column_2596")])]
+            projection ("t2"."product_code"::string -> "column_2496", "t2"."product_units"::boolean -> "column_2596")
                 group by ("t2"."product_code"::string, "t2"."product_units"::boolean) output: ("t2"."product_units"::boolean -> "product_units", "t2"."product_code"::string -> "product_code", "t2"."identification_number"::integer -> "identification_number", "t"."id"::unsigned -> "id")
                     join on ROW("t2"."identification_number"::integer) = ROW("t"."id"::unsigned)
                         scan "t2"
@@ -1453,6 +1454,7 @@ vtable_max_rows = 5000
 "#,
     );
     assert_eq!(expected_explain, plan.as_explain().unwrap());
+    println!("{}", plan.as_explain().unwrap());
 }
 
 #[test]
@@ -1513,7 +1515,6 @@ vtable_max_rows = 5000
         "#;
 
     let plan = sql_to_optimized_ir(input, vec![]);
-    println!("{}", plan.as_explain().unwrap());
 
     // TODO: For the  hash function in the cartrisge runtime we can apply
     //       `motion [policy: segment([ref("id")])]` instead of the `motion [policy: full]`.
@@ -1525,17 +1526,17 @@ vtable_max_rows = 5000
                 scan "hash_single_testing"
         motion [policy: full]
             scan "t2"
-                projection (sum(("sum_41"::decimal))::decimal -> "id")
+                projection (sum(("sum_1796"::decimal))::decimal -> "id")
                     motion [policy: full]
-                        scan
-                            projection (sum(("test_space"."id"::unsigned))::decimal -> "sum_41")
-                                scan "test_space"
+                        projection (sum(("test_space"."id"::unsigned))::decimal -> "sum_1796")
+                            scan "test_space"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
 "#,
     );
     assert_eq!(expected_explain, plan.as_explain().unwrap());
+    println!("{}", plan.as_explain().unwrap());
 }
 
 #[test]
@@ -1544,14 +1545,13 @@ fn front_sql_groupby_insert() {
     SELECT "b", "d" FROM "t" group by "b", "d" ON CONFLICT DO FAIL"#;
 
     let plan = sql_to_optimized_ir(input, vec![]);
-    println!("{}", plan.as_explain().unwrap());
     let expected_explain = String::from(
         r#"insert "t" on conflict: fail
     motion [policy: segment([value(NULL), ref("d")])]
-        projection ("column_12"::unsigned -> "b", "column_13"::unsigned -> "d")
-            group by ("column_12"::unsigned, "column_13"::unsigned) output: ("column_12"::unsigned -> "column_12", "column_13"::unsigned -> "column_13")
-                motion [policy: segment([ref("column_12"), ref("column_13")])]
-                    projection ("t"."b"::unsigned -> "column_12", "t"."d"::unsigned -> "column_13")
+        projection ("column_596"::unsigned -> "b", "column_696"::unsigned -> "d")
+            group by ("column_596"::unsigned, "column_696"::unsigned) output: ("column_596"::unsigned -> "column_596", "column_696"::unsigned -> "column_696")
+                motion [policy: segment([ref("column_596"), ref("column_696")])]
+                    projection ("t"."b"::unsigned -> "column_596", "t"."d"::unsigned -> "column_696")
                         group by ("t"."b"::unsigned, "t"."d"::unsigned) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
                             scan "t"
 execution options:
@@ -1561,6 +1561,7 @@ vtable_max_rows = 5000
     );
 
     assert_eq!(expected_explain, plan.as_explain().unwrap());
+    println!("{}", plan.as_explain().unwrap());
 }
 
 #[test]
@@ -1595,16 +1596,14 @@ fn front_sql_aggregates() {
         group by "b""#;
 
     let plan = sql_to_optimized_ir(input, vec![]);
-    println!("{}", plan.as_explain().unwrap());
 
     let expected_explain = String::from(
-        r#"projection ("column_764"::unsigned -> "b", ROW(sum(("count_29"::integer))::decimal) + ROW(sum(("count_31"::integer))::decimal) -> "col_1")
-    group by ("column_764"::unsigned) output: ("column_764"::unsigned -> "column_764", "count_31"::integer -> "count_31", "count_29"::integer -> "count_29")
-        motion [policy: segment([ref("column_764")])]
-            scan
-                projection ("t"."b"::unsigned -> "column_764", count(("t"."b"::unsigned))::integer -> "count_31", count(("t"."a"::unsigned))::integer -> "count_29")
-                    group by ("t"."b"::unsigned) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
-                        scan "t"
+        r#"projection ("column_596"::unsigned -> "b", ROW(sum(("count_1496"::integer))::decimal) + ROW(sum(("count_1596"::integer))::decimal) -> "col_1")
+    group by ("column_596"::unsigned) output: ("column_596"::unsigned -> "column_596", "count_1596"::integer -> "count_1596", "count_1496"::integer -> "count_1496")
+        motion [policy: segment([ref("column_596")])]
+            projection ("t"."b"::unsigned -> "column_596", count(("t"."b"::unsigned))::integer -> "count_1596", count(("t"."a"::unsigned))::integer -> "count_1496")
+                group by ("t"."b"::unsigned) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
+                    scan "t"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
@@ -1622,12 +1621,11 @@ fn front_sql_avg_aggregate() {
     println!("{}", plan.as_explain().unwrap());
 
     let expected_explain = String::from(
-        r#"projection (sum(("sum_13"::decimal::double))::decimal / sum(("count_13"::decimal::double))::decimal -> "col_1", avg(distinct ("column_15"::decimal::double))::decimal -> "col_2", ROW(sum(("sum_13"::decimal::double))::decimal / sum(("count_13"::decimal::double))::decimal) * ROW(sum(("sum_13"::decimal::double))::decimal / sum(("count_13"::decimal::double))::decimal) -> "col_3")
+        r#"projection (sum(("sum_696"::decimal::double))::decimal / sum(("count_696"::decimal::double))::decimal -> "col_1", avg(distinct ("column_796"::decimal::double))::decimal -> "col_2", ROW(sum(("sum_696"::decimal::double))::decimal / sum(("count_696"::decimal::double))::decimal) * ROW(sum(("sum_696"::decimal::double))::decimal / sum(("count_696"::decimal::double))::decimal) -> "col_3")
     motion [policy: full]
-        scan
-            projection ("t"."b"::unsigned -> "column_15", count(("t"."b"::unsigned))::integer -> "count_13", sum(("t"."b"::unsigned))::decimal -> "sum_13")
-                group by ("t"."b"::unsigned) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
-                    scan "t"
+        projection ("t"."b"::unsigned -> "column_796", count(("t"."b"::unsigned))::integer -> "count_696", sum(("t"."b"::unsigned))::decimal -> "sum_696")
+            group by ("t"."b"::unsigned) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
+                scan "t"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
@@ -1644,18 +1642,18 @@ fn front_sql_total_aggregate() {
     let plan = sql_to_optimized_ir(input, vec![]);
 
     let expected_explain = String::from(
-        r#"projection (total(("total_13"::double))::double -> "col_1", total(distinct ("column_15"::double))::double -> "col_2")
+        r#"projection (total(("total_696"::double))::double -> "col_1", total(distinct ("column_796"::double))::double -> "col_2")
     motion [policy: full]
-        scan
-            projection ("t"."b"::unsigned -> "column_15", total(("t"."b"::unsigned))::double -> "total_13")
-                group by ("t"."b"::unsigned) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
-                    scan "t"
+        projection ("t"."b"::unsigned -> "column_796", total(("t"."b"::unsigned))::double -> "total_696")
+            group by ("t"."b"::unsigned) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
+                scan "t"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
 "#,
     );
 
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 }
 
@@ -1666,18 +1664,18 @@ fn front_sql_min_aggregate() {
     let plan = sql_to_optimized_ir(input, vec![]);
 
     let expected_explain = String::from(
-        r#"projection (min(("min_13"::unsigned))::scalar -> "col_1", min(distinct ("column_15"::unsigned))::scalar -> "col_2")
+        r#"projection (min(("min_696"::unsigned))::scalar -> "col_1", min(distinct ("column_796"::unsigned))::scalar -> "col_2")
     motion [policy: full]
-        scan
-            projection ("t"."b"::unsigned -> "column_15", min(("t"."b"::unsigned))::scalar -> "min_13")
-                group by ("t"."b"::unsigned) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
-                    scan "t"
+        projection ("t"."b"::unsigned -> "column_796", min(("t"."b"::unsigned))::scalar -> "min_696")
+            group by ("t"."b"::unsigned) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
+                scan "t"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
 "#,
     );
 
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 }
 
@@ -1688,18 +1686,18 @@ fn front_sql_max_aggregate() {
     let plan = sql_to_optimized_ir(input, vec![]);
 
     let expected_explain = String::from(
-        r#"projection (max(("max_13"::unsigned))::scalar -> "col_1", max(distinct ("column_15"::unsigned))::scalar -> "col_2")
+        r#"projection (max(("max_696"::unsigned))::scalar -> "col_1", max(distinct ("column_796"::unsigned))::scalar -> "col_2")
     motion [policy: full]
-        scan
-            projection ("t"."b"::unsigned -> "column_15", max(("t"."b"::unsigned))::scalar -> "max_13")
-                group by ("t"."b"::unsigned) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
-                    scan "t"
+        projection ("t"."b"::unsigned -> "column_796", max(("t"."b"::unsigned))::scalar -> "max_696")
+            group by ("t"."b"::unsigned) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
+                scan "t"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
 "#,
     );
 
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 }
 
@@ -1710,18 +1708,18 @@ fn front_sql_group_concat_aggregate() {
     let plan = sql_to_optimized_ir(input, vec![]);
 
     let expected_explain = String::from(
-        r#"projection (group_concat(("group_concat_13"::string))::string -> "col_1", group_concat(distinct ("column_15"::string))::string -> "col_2")
+        r#"projection (group_concat(("group_concat_696"::string))::string -> "col_1", group_concat(distinct ("column_796"::string))::string -> "col_2")
     motion [policy: full]
-        scan
-            projection ("test_space"."FIRST_NAME"::string -> "column_15", group_concat(("test_space"."FIRST_NAME"::string))::string -> "group_concat_13")
-                group by ("test_space"."FIRST_NAME"::string) output: ("test_space"."id"::unsigned -> "id", "test_space"."sysFrom"::unsigned -> "sysFrom", "test_space"."FIRST_NAME"::string -> "FIRST_NAME", "test_space"."sys_op"::unsigned -> "sys_op", "test_space"."bucket_id"::unsigned -> "bucket_id")
-                    scan "test_space"
+        projection ("test_space"."FIRST_NAME"::string -> "column_796", group_concat(("test_space"."FIRST_NAME"::string))::string -> "group_concat_696")
+            group by ("test_space"."FIRST_NAME"::string) output: ("test_space"."id"::unsigned -> "id", "test_space"."sysFrom"::unsigned -> "sysFrom", "test_space"."FIRST_NAME"::string -> "FIRST_NAME", "test_space"."sys_op"::unsigned -> "sys_op", "test_space"."bucket_id"::unsigned -> "bucket_id")
+                scan "test_space"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
 "#,
     );
 
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 }
 
@@ -1732,18 +1730,18 @@ fn front_sql_group_concat_aggregate2() {
     let plan = sql_to_optimized_ir(input, vec![]);
 
     let expected_explain = String::from(
-        r#"projection (group_concat(("group_concat_14"::string, ' '::string))::string -> "col_1", group_concat(distinct ("column_16"::string))::string -> "col_2")
+        r#"projection (group_concat(("group_concat_696"::string, ' '::string))::string -> "col_1", group_concat(distinct ("column_796"::string))::string -> "col_2")
     motion [policy: full]
-        scan
-            projection ("test_space"."FIRST_NAME"::string -> "column_16", group_concat(("test_space"."FIRST_NAME"::string, ' '::string))::string -> "group_concat_14")
-                group by ("test_space"."FIRST_NAME"::string) output: ("test_space"."id"::unsigned -> "id", "test_space"."sysFrom"::unsigned -> "sysFrom", "test_space"."FIRST_NAME"::string -> "FIRST_NAME", "test_space"."sys_op"::unsigned -> "sys_op", "test_space"."bucket_id"::unsigned -> "bucket_id")
-                    scan "test_space"
+        projection ("test_space"."FIRST_NAME"::string -> "column_796", group_concat(("test_space"."FIRST_NAME"::string, ' '::string))::string -> "group_concat_696")
+            group by ("test_space"."FIRST_NAME"::string) output: ("test_space"."id"::unsigned -> "id", "test_space"."sysFrom"::unsigned -> "sysFrom", "test_space"."FIRST_NAME"::string -> "FIRST_NAME", "test_space"."sys_op"::unsigned -> "sys_op", "test_space"."bucket_id"::unsigned -> "bucket_id")
+                scan "test_space"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
 "#,
     );
 
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 }
 
@@ -1754,17 +1752,17 @@ fn front_sql_count_asterisk1() {
     let plan = sql_to_optimized_ir(input, vec![]);
 
     let expected_explain = String::from(
-        r#"projection (sum(("count_13"::integer))::decimal -> "col_1", sum(("count_13"::integer))::decimal -> "col_2")
+        r#"projection (sum(("count_596"::integer))::decimal -> "col_1", sum(("count_596"::integer))::decimal -> "col_2")
     motion [policy: full]
-        scan
-            projection (count((*::integer))::integer -> "count_13")
-                scan "t"
+        projection (count((*::integer))::integer -> "count_596")
+            scan "t"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
 "#,
     );
 
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 }
 
@@ -1775,19 +1773,19 @@ fn front_sql_count_asterisk2() {
     let plan = sql_to_optimized_ir(input, vec![]);
 
     let expected_explain = String::from(
-        r#"projection (sum(("count_26"::integer))::decimal -> "col_1", "column_764"::unsigned -> "b")
-    group by ("column_764"::unsigned) output: ("column_764"::unsigned -> "column_764", "count_26"::integer -> "count_26")
-        motion [policy: segment([ref("column_764")])]
-            scan
-                projection ("t"."b"::unsigned -> "column_764", count((*::integer))::integer -> "count_26")
-                    group by ("t"."b"::unsigned) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
-                        scan "t"
+        r#"projection (sum(("count_1196"::integer))::decimal -> "col_1", "column_596"::unsigned -> "b")
+    group by ("column_596"::unsigned) output: ("column_596"::unsigned -> "column_596", "count_1196"::integer -> "count_1196")
+        motion [policy: segment([ref("column_596")])]
+            projection ("t"."b"::unsigned -> "column_596", count((*::integer))::integer -> "count_1196")
+                group by ("t"."b"::unsigned) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
+                    scan "t"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
 "#,
     );
 
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 }
 
@@ -1815,13 +1813,12 @@ fn front_sql_aggregates_with_subexpressions() {
     println!("{}", plan.as_explain().unwrap());
 
     let expected_explain = String::from(
-        r#"projection ("column_764"::unsigned -> "b", sum(("count_35"::integer))::decimal -> "col_1", sum(("count_39"::integer))::decimal -> "col_2")
-    group by ("column_764"::unsigned) output: ("column_764"::unsigned -> "column_764", "count_39"::integer -> "count_39", "count_35"::integer -> "count_35")
-        motion [policy: segment([ref("column_764")])]
-            scan
-                projection ("t"."b"::unsigned -> "column_764", count(("func"(("t"."a"::unsigned))::integer))::integer -> "count_39", count((ROW("t"."a"::unsigned) * ROW("t"."b"::unsigned) + ROW(1::unsigned)))::integer -> "count_35")
-                    group by ("t"."b"::unsigned) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
-                        scan "t"
+        r#"projection ("column_596"::unsigned -> "b", sum(("count_1496"::integer))::decimal -> "col_1", sum(("count_1796"::integer))::decimal -> "col_2")
+    group by ("column_596"::unsigned) output: ("column_596"::unsigned -> "column_596", "count_1796"::integer -> "count_1796", "count_1496"::integer -> "count_1496")
+        motion [policy: segment([ref("column_596")])]
+            projection ("t"."b"::unsigned -> "column_596", count(("func"(("t"."a"::unsigned))::integer))::integer -> "count_1796", count((ROW("t"."a"::unsigned) * ROW("t"."b"::unsigned) + ROW(1::unsigned)))::integer -> "count_1496")
+                group by ("t"."b"::unsigned) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
+                    scan "t"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
@@ -1838,19 +1835,19 @@ fn front_sql_aggregates_with_distinct1() {
 
     let plan = sql_to_optimized_ir(input, vec![]);
     let expected_explain = String::from(
-        r#"projection ("column_764"::unsigned -> "b", count(distinct ("column_27"::integer))::integer -> "col_1", count(distinct ("column_764"::integer))::integer -> "col_2")
-    group by ("column_764"::unsigned) output: ("column_764"::unsigned -> "column_764", "column_27"::unsigned -> "column_27")
-        motion [policy: segment([ref("column_764")])]
-            scan
-                projection ("t"."b"::unsigned -> "column_764", "t"."a"::unsigned -> "column_27")
-                    group by ("t"."b"::unsigned, "t"."a"::unsigned) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
-                        scan "t"
+        r#"projection ("column_596"::unsigned -> "b", count(distinct ("column_1296"::integer))::integer -> "col_1", count(distinct ("column_596"::integer))::integer -> "col_2")
+    group by ("column_596"::unsigned) output: ("column_596"::unsigned -> "column_596", "column_1296"::unsigned -> "column_1296")
+        motion [policy: segment([ref("column_596")])]
+            projection ("t"."b"::unsigned -> "column_596", "t"."a"::unsigned -> "column_1296")
+                group by ("t"."b"::unsigned, "t"."a"::unsigned) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
+                    scan "t"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
 "#,
     );
 
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 }
 
@@ -1862,19 +1859,19 @@ fn front_sql_aggregates_with_distinct2() {
     let plan = sql_to_optimized_ir(input, vec![]);
 
     let expected_explain = String::from(
-        r#"projection ("column_764"::unsigned -> "b", sum(distinct ("column_34"::decimal))::decimal -> "col_1")
-    group by ("column_764"::unsigned) output: ("column_764"::unsigned -> "column_764", "column_34"::unsigned -> "column_34")
-        motion [policy: segment([ref("column_764")])]
-            scan
-                projection ("t"."b"::unsigned -> "column_764", ROW("t"."a"::unsigned) + ROW("t"."b"::unsigned) + ROW(3::unsigned) -> "column_34")
-                    group by ("t"."b"::unsigned, ROW("t"."a"::unsigned) + ROW("t"."b"::unsigned) + ROW(3::unsigned)) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
-                        scan "t"
+        r#"projection ("column_596"::unsigned -> "b", sum(distinct ("column_1232"::decimal))::decimal -> "col_1")
+    group by ("column_596"::unsigned) output: ("column_596"::unsigned -> "column_596", "column_1232"::unsigned -> "column_1232")
+        motion [policy: segment([ref("column_596")])]
+            projection ("t"."b"::unsigned -> "column_596", ROW("t"."a"::unsigned) + ROW("t"."b"::unsigned) + ROW(3::unsigned) -> "column_1232")
+                group by ("t"."b"::unsigned, ROW("t"."a"::unsigned) + ROW("t"."b"::unsigned) + ROW(3::unsigned)) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
+                    scan "t"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
 "#,
     );
 
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 }
 
@@ -1885,18 +1882,18 @@ fn front_sql_aggregates_with_distinct3() {
     let plan = sql_to_optimized_ir(input, vec![]);
 
     let expected_explain = String::from(
-        r#"projection (sum(distinct ("column_19"::decimal))::decimal -> "col_1")
+        r#"projection (sum(distinct ("column_632"::decimal))::decimal -> "col_1")
     motion [policy: full]
-        scan
-            projection (ROW("t"."a"::unsigned) + ROW("t"."b"::unsigned) + ROW(3::unsigned) -> "column_19")
-                group by (ROW("t"."a"::unsigned) + ROW("t"."b"::unsigned) + ROW(3::unsigned)) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
-                    scan "t"
+        projection (ROW("t"."a"::unsigned) + ROW("t"."b"::unsigned) + ROW(3::unsigned) -> "column_632")
+            group by (ROW("t"."a"::unsigned) + ROW("t"."b"::unsigned) + ROW(3::unsigned)) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
+                scan "t"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
 "#,
     );
 
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 }
 
@@ -2013,20 +2010,20 @@ fn front_sql_pg_style_params3() {
 
     let plan = sql_to_optimized_ir(input, vec![Value::Unsigned(42)]);
     let expected_explain = String::from(
-        r#"projection ("column_31"::unsigned -> "col_1")
-    having ROW(sum(("count_46"::integer))::decimal) > ROW(42::unsigned)
-        group by ("column_31"::unsigned) output: ("column_31"::unsigned -> "column_31", "count_46"::integer -> "count_46")
-            motion [policy: segment([ref("column_31")])]
-                scan
-                    projection (ROW("t"."a"::unsigned) + ROW(42::unsigned) -> "column_31", count(("t"."b"::unsigned))::integer -> "count_46")
-                        group by (ROW("t"."a"::unsigned) + ROW(42::unsigned)) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
-                            selection ROW("t"."a"::unsigned) = ROW(42::unsigned)
-                                scan "t"
+        r#"projection ("column_1132"::unsigned -> "col_1")
+    having ROW(sum(("count_1896"::integer))::decimal) > ROW(42::unsigned)
+        group by ("column_1132"::unsigned) output: ("column_1132"::unsigned -> "column_1132", "count_1896"::integer -> "count_1896")
+            motion [policy: segment([ref("column_1132")])]
+                projection (ROW("t"."a"::unsigned) + ROW(42::unsigned) -> "column_1132", count(("t"."b"::unsigned))::integer -> "count_1896")
+                    group by (ROW("t"."a"::unsigned) + ROW(42::unsigned)) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
+                        selection ROW("t"."a"::unsigned) = ROW(42::unsigned)
+                            scan "t"
 execution options:
 sql_vdbe_max_steps = 42
 vtable_max_rows = 42
 "#,
     );
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 }
 
@@ -2096,11 +2093,10 @@ fn front_sql_aggregate_without_groupby() {
     let plan = sql_to_optimized_ir(input, vec![]);
 
     let expected_explain = String::from(
-        r#"projection (sum(("sum_20"::decimal))::decimal -> "col_1")
+        r#"projection (sum(("sum_796"::decimal))::decimal -> "col_1")
     motion [policy: full]
-        scan
-            projection (sum((ROW("t"."a"::unsigned) * ROW("t"."b"::unsigned) + ROW(1::unsigned)))::decimal -> "sum_20")
-                scan "t"
+        projection (sum((ROW("t"."a"::unsigned) * ROW("t"."b"::unsigned) + ROW(1::unsigned)))::decimal -> "sum_796")
+            scan "t"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
@@ -2119,11 +2115,10 @@ fn front_sql_aggregate_without_groupby2() {
     let expected_explain = String::from(
         r#"projection ("t1"."col_1"::integer -> "col_1")
     scan "t1"
-        projection (sum(("count_13"::integer))::decimal -> "col_1")
+        projection (sum(("count_696"::integer))::decimal -> "col_1")
             motion [policy: full]
-                scan
-                    projection (count(("test_space"."id"::unsigned))::integer -> "count_13")
-                        scan "test_space"
+                projection (count(("test_space"."id"::unsigned))::integer -> "count_696")
+                    scan "test_space"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
@@ -2143,11 +2138,10 @@ fn front_sql_aggregate_on_aggregate() {
     let expected_explain = String::from(
         r#"projection (max(("t1"."c"::integer))::scalar -> "col_1")
     scan "t1"
-        projection (sum(("count_13"::integer))::decimal -> "c")
+        projection (sum(("count_696"::integer))::decimal -> "c")
             motion [policy: full]
-                scan
-                    projection (count(("test_space"."id"::unsigned))::integer -> "count_13")
-                        scan "test_space"
+                projection (count(("test_space"."id"::unsigned))::integer -> "count_696")
+                    scan "test_space"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
@@ -2172,17 +2166,17 @@ fn front_sql_union_single_left() {
     projection ("t"."a"::unsigned -> "a")
         scan "t"
     motion [policy: segment([ref("col_1")])]
-        projection (sum(("sum_29"::decimal))::decimal -> "col_1")
+        projection (sum(("sum_1296"::decimal))::decimal -> "col_1")
             motion [policy: full]
-                scan
-                    projection (sum(("t"."a"::unsigned))::decimal -> "sum_29")
-                        scan "t"
+                projection (sum(("t"."a"::unsigned))::decimal -> "sum_1296")
+                    scan "t"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
 "#,
     );
 
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 }
 
@@ -2199,11 +2193,10 @@ fn front_sql_union_single_right() {
     let expected_explain = String::from(
         r#"union all
     motion [policy: segment([ref("col_1")])]
-        projection (sum(("sum_13"::decimal))::decimal -> "col_1")
+        projection (sum(("sum_696"::decimal))::decimal -> "col_1")
             motion [policy: full]
-                scan
-                    projection (sum(("t"."a"::unsigned))::decimal -> "sum_13")
-                        scan "t"
+                projection (sum(("t"."a"::unsigned))::decimal -> "sum_696")
+                    scan "t"
     projection ("t"."a"::unsigned -> "a")
         scan "t"
 execution options:
@@ -2212,6 +2205,7 @@ vtable_max_rows = 5000
 "#,
     );
 
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 }
 
@@ -2228,23 +2222,22 @@ fn front_sql_union_single_both() {
     let expected_explain = String::from(
         r#"union all
     motion [policy: segment([ref("col_1")])]
-        projection (sum(("sum_13"::decimal))::decimal -> "col_1")
+        projection (sum(("sum_696"::decimal))::decimal -> "col_1")
             motion [policy: full]
-                scan
-                    projection (sum(("t"."a"::unsigned))::decimal -> "sum_13")
-                        scan "t"
+                projection (sum(("t"."a"::unsigned))::decimal -> "sum_696")
+                    scan "t"
     motion [policy: segment([ref("col_1")])]
-        projection (sum(("sum_30"::decimal))::decimal -> "col_1")
+        projection (sum(("sum_1396"::decimal))::decimal -> "col_1")
             motion [policy: full]
-                scan
-                    projection (sum(("t"."a"::unsigned))::decimal -> "sum_30")
-                        scan "t"
+                projection (sum(("t"."a"::unsigned))::decimal -> "sum_1396")
+                    scan "t"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
 "#,
     );
 
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 }
 
@@ -2257,17 +2250,17 @@ fn front_sql_insert_single() {
     let expected_explain = String::from(
         r#"insert "t" on conflict: fail
     motion [policy: segment([value(NULL), ref("col_2")])]
-        projection (sum(("sum_13"::decimal))::decimal -> "col_1", sum(("count_16"::integer))::decimal -> "col_2")
+        projection (sum(("sum_696"::decimal))::decimal -> "col_1", sum(("count_896"::integer))::decimal -> "col_2")
             motion [policy: full]
-                scan
-                    projection (count(("t"."d"::unsigned))::integer -> "count_16", sum(("t"."b"::unsigned))::decimal -> "sum_13")
-                        scan "t"
+                projection (count(("t"."d"::unsigned))::integer -> "count_896", sum(("t"."b"::unsigned))::decimal -> "sum_696")
+                    scan "t"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
 "#,
     );
 
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 }
 
@@ -2284,17 +2277,17 @@ fn front_sql_except_single_right() {
     projection ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b")
         scan "t"
     motion [policy: segment([ref("col_1"), ref("col_2")])]
-        projection (sum(("sum_31"::decimal))::decimal -> "col_1", sum(("count_34"::integer))::decimal -> "col_2")
+        projection (sum(("sum_1396"::decimal))::decimal -> "col_1", sum(("count_1596"::integer))::decimal -> "col_2")
             motion [policy: full]
-                scan
-                    projection (sum(("t"."a"::unsigned))::decimal -> "sum_31", count(("t"."b"::unsigned))::integer -> "count_34")
-                        scan "t"
+                projection (sum(("t"."a"::unsigned))::decimal -> "sum_1396", count(("t"."b"::unsigned))::integer -> "count_1596")
+                    scan "t"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
 "#,
     );
 
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 
     let input = r#"SELECT "b", "a" from "t"
@@ -2309,17 +2302,17 @@ vtable_max_rows = 5000
     projection ("t"."b"::unsigned -> "b", "t"."a"::unsigned -> "a")
         scan "t"
     motion [policy: segment([ref("col_2"), ref("col_1")])]
-        projection (sum(("sum_31"::decimal))::decimal -> "col_1", sum(("count_34"::integer))::decimal -> "col_2")
+        projection (sum(("sum_1396"::decimal))::decimal -> "col_1", sum(("count_1596"::integer))::decimal -> "col_2")
             motion [policy: full]
-                scan
-                    projection (sum(("t"."a"::unsigned))::decimal -> "sum_31", count(("t"."b"::unsigned))::integer -> "count_34")
-                        scan "t"
+                projection (sum(("t"."a"::unsigned))::decimal -> "sum_1396", count(("t"."b"::unsigned))::integer -> "count_1596")
+                    scan "t"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
 "#,
     );
 
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 }
 
@@ -2334,11 +2327,10 @@ fn front_sql_except_single_left() {
     let expected_explain = String::from(
         r#"except
     motion [policy: segment([ref("col_1"), ref("col_2")])]
-        projection (sum(("sum_13"::decimal))::decimal -> "col_1", sum(("count_16"::integer))::decimal -> "col_2")
+        projection (sum(("sum_696"::decimal))::decimal -> "col_1", sum(("count_896"::integer))::decimal -> "col_2")
             motion [policy: full]
-                scan
-                    projection (sum(("t"."a"::unsigned))::decimal -> "sum_13", count(("t"."b"::unsigned))::integer -> "count_16")
-                        scan "t"
+                projection (sum(("t"."a"::unsigned))::decimal -> "sum_696", count(("t"."b"::unsigned))::integer -> "count_896")
+                    scan "t"
     projection ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b")
         scan "t"
 execution options:
@@ -2347,6 +2339,7 @@ vtable_max_rows = 5000
 "#,
     );
 
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 }
 
@@ -2362,17 +2355,15 @@ fn front_sql_except_single_both() {
     let expected_explain = String::from(
         r#"except
     motion [policy: segment([ref("col_1")])]
-        projection (sum(("sum_13"::decimal))::decimal -> "col_1", sum(("count_16"::integer))::decimal -> "col_2")
+        projection (sum(("sum_696"::decimal))::decimal -> "col_1", sum(("count_896"::integer))::decimal -> "col_2")
             motion [policy: full]
-                scan
-                    projection (sum(("t"."a"::unsigned))::decimal -> "sum_13", count(("t"."b"::unsigned))::integer -> "count_16")
-                        scan "t"
+                projection (sum(("t"."a"::unsigned))::decimal -> "sum_696", count(("t"."b"::unsigned))::integer -> "count_896")
+                    scan "t"
     motion [policy: segment([ref("col_1")])]
-        projection (sum(("sum_33"::decimal))::decimal -> "col_1", sum(("sum_36"::decimal))::decimal -> "col_2")
+        projection (sum(("sum_1596"::decimal))::decimal -> "col_1", sum(("sum_1796"::decimal))::decimal -> "col_2")
             motion [policy: full]
-                scan
-                    projection (sum(("t"."a"::unsigned))::decimal -> "sum_33", sum(("t"."b"::unsigned))::decimal -> "sum_36")
-                        scan "t"
+                projection (sum(("t"."a"::unsigned))::decimal -> "sum_1596", sum(("t"."b"::unsigned))::decimal -> "sum_1796")
+                    scan "t"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
@@ -2390,19 +2381,19 @@ fn front_sql_groupby_expression() {
     let plan = sql_to_optimized_ir(input, vec![]);
 
     let expected_explain = String::from(
-        r#"projection ("column_16"::unsigned -> "col_1")
-    group by ("column_16"::unsigned) output: ("column_16"::unsigned -> "column_16")
-        motion [policy: segment([ref("column_16")])]
-            scan
-                projection (ROW("t"."a"::unsigned) + ROW("t"."b"::unsigned) -> "column_16")
-                    group by (ROW("t"."a"::unsigned) + ROW("t"."b"::unsigned)) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
-                        scan "t"
+        r#"projection ("column_532"::unsigned -> "col_1")
+    group by ("column_532"::unsigned) output: ("column_532"::unsigned -> "column_532")
+        motion [policy: segment([ref("column_532")])]
+            projection (ROW("t"."a"::unsigned) + ROW("t"."b"::unsigned) -> "column_532")
+                group by (ROW("t"."a"::unsigned) + ROW("t"."b"::unsigned)) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
+                    scan "t"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
 "#,
     );
 
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 }
 
@@ -2414,19 +2405,19 @@ fn front_sql_groupby_expression2() {
     let plan = sql_to_optimized_ir(input, vec![]);
 
     let expected_explain = String::from(
-        r#"projection ("column_17"::unsigned + ROW(sum(("count_37"::integer))::decimal) -> "col_1")
-    group by ("column_17"::unsigned) output: ("column_17"::unsigned -> "column_17", "count_37"::integer -> "count_37")
-        motion [policy: segment([ref("column_17")])]
-            scan
-                projection ((ROW("t"."a"::unsigned) + ROW("t"."b"::unsigned)) -> "column_17", count(("t"."a"::unsigned))::integer -> "count_37")
-                    group by ((ROW("t"."a"::unsigned) + ROW("t"."b"::unsigned))) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
-                        scan "t"
+        r#"projection ("column_632"::unsigned + ROW(sum(("count_1596"::integer))::decimal) -> "col_1")
+    group by ("column_632"::unsigned) output: ("column_632"::unsigned -> "column_632", "count_1596"::integer -> "count_1596")
+        motion [policy: segment([ref("column_632")])]
+            projection ((ROW("t"."a"::unsigned) + ROW("t"."b"::unsigned)) -> "column_632", count(("t"."a"::unsigned))::integer -> "count_1596")
+                group by ((ROW("t"."a"::unsigned) + ROW("t"."b"::unsigned))) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
+                    scan "t"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
 "#,
     );
 
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 }
 
@@ -2436,12 +2427,11 @@ fn front_sql_groupby_expression3() {
         group by "a"+"b", "a"+"b", ("c"*"d")"#;
 
     let plan = sql_to_optimized_ir(input, vec![]);
-    println!("{}", plan.as_explain().unwrap());
     let expected_explain = String::from(
-        r#"projection ("column_16"::unsigned -> "col_1", "column_27"::unsigned * ROW(sum(("sum_59"::decimal))::decimal) / ROW(sum(("count_65"::integer))::decimal) -> "col_2")
-    group by ("column_16"::unsigned, "column_27"::unsigned) output: ("column_16"::unsigned -> "column_16", "column_27"::unsigned -> "column_27", "count_65"::integer -> "count_65", "sum_59"::decimal -> "sum_59")
-        motion [policy: segment([ref("column_16"), ref("column_27")])]
-            projection (ROW("t"."a"::unsigned) + ROW("t"."b"::unsigned) -> "column_16", (ROW("t"."c"::unsigned) * ROW("t"."d"::unsigned)) -> "column_27", count((ROW("t"."a"::unsigned) * ROW("t"."b"::unsigned)))::integer -> "count_65", sum((ROW("t"."c"::unsigned) * ROW("t"."d"::unsigned)))::decimal -> "sum_59")
+        r#"projection ("column_532"::unsigned -> "col_1", "column_832"::unsigned * ROW(sum(("sum_2496"::decimal))::decimal) / ROW(sum(("count_2596"::integer))::decimal) -> "col_2")
+    group by ("column_532"::unsigned, "column_832"::unsigned) output: ("column_532"::unsigned -> "column_532", "column_832"::unsigned -> "column_832", "count_2596"::integer -> "count_2596", "sum_2496"::decimal -> "sum_2496")
+        motion [policy: segment([ref("column_532"), ref("column_832")])]
+            projection (ROW("t"."a"::unsigned) + ROW("t"."b"::unsigned) -> "column_532", (ROW("t"."c"::unsigned) * ROW("t"."d"::unsigned)) -> "column_832", count((ROW("t"."a"::unsigned) * ROW("t"."b"::unsigned)))::integer -> "count_2596", sum((ROW("t"."c"::unsigned) * ROW("t"."d"::unsigned)))::decimal -> "sum_2496")
                 group by (ROW("t"."a"::unsigned) + ROW("t"."b"::unsigned), (ROW("t"."c"::unsigned) * ROW("t"."d"::unsigned))) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
                     scan "t"
 execution options:
@@ -2451,6 +2441,7 @@ vtable_max_rows = 5000
     );
 
     assert_eq!(expected_explain, plan.as_explain().unwrap());
+    println!("{}", plan.as_explain().unwrap());
 }
 
 #[test]
@@ -2459,12 +2450,11 @@ fn front_sql_groupby_expression4() {
         group by "a"+"b", "a""#;
 
     let plan = sql_to_optimized_ir(input, vec![]);
-    println!("{}", plan.as_explain().unwrap());
     let expected_explain = String::from(
-        r#"projection ("column_16"::unsigned -> "col_1", "column_17"::unsigned -> "a")
-    group by ("column_16"::unsigned, "column_17"::unsigned) output: ("column_16"::unsigned -> "column_16", "column_17"::unsigned -> "column_17")
-        motion [policy: segment([ref("column_16"), ref("column_17")])]
-            projection (ROW("t"."a"::unsigned) + ROW("t"."b"::unsigned) -> "column_16", "t"."a"::unsigned -> "column_17")
+        r#"projection ("column_532"::unsigned -> "col_1", "column_796"::unsigned -> "a")
+    group by ("column_532"::unsigned, "column_796"::unsigned) output: ("column_532"::unsigned -> "column_532", "column_796"::unsigned -> "column_796")
+        motion [policy: segment([ref("column_532"), ref("column_796")])]
+            projection (ROW("t"."a"::unsigned) + ROW("t"."b"::unsigned) -> "column_532", "t"."a"::unsigned -> "column_796")
                 group by (ROW("t"."a"::unsigned) + ROW("t"."b"::unsigned), "t"."a"::unsigned) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
                     scan "t"
 execution options:
@@ -2474,6 +2464,7 @@ vtable_max_rows = 5000
     );
 
     assert_eq!(expected_explain, plan.as_explain().unwrap());
+    println!("{}", plan.as_explain().unwrap());
 }
 
 #[test]
@@ -2484,23 +2475,22 @@ fn front_sql_groupby_with_aggregates() {
         on (t1."a", t2."g") = (t2."e", t1."b")"#;
 
     let plan = sql_to_optimized_ir(input, vec![]);
-    println!("{}", plan.as_explain().unwrap());
     let expected_explain = String::from(
         r#"projection ("t1"."a"::unsigned -> "a", "t1"."b"::unsigned -> "b", "t1"."c"::decimal -> "c", "t2"."g"::unsigned -> "g", "t2"."e"::unsigned -> "e", "t2"."f"::decimal -> "f")
     join on ROW("t1"."a"::unsigned, "t2"."g"::unsigned) = ROW("t2"."e"::unsigned, "t1"."b"::unsigned)
         scan "t1"
-            projection ("column_12"::unsigned -> "a", "column_13"::unsigned -> "b", sum(("sum_31"::decimal))::decimal -> "c")
-                group by ("column_12"::unsigned, "column_13"::unsigned) output: ("column_12"::unsigned -> "column_12", "column_13"::unsigned -> "column_13", "sum_31"::decimal -> "sum_31")
-                    motion [policy: segment([ref("column_12"), ref("column_13")])]
-                        projection ("t"."a"::unsigned -> "column_12", "t"."b"::unsigned -> "column_13", sum(("t"."c"::unsigned))::decimal -> "sum_31")
+            projection ("column_596"::unsigned -> "a", "column_696"::unsigned -> "b", sum(("sum_1596"::decimal))::decimal -> "c")
+                group by ("column_596"::unsigned, "column_696"::unsigned) output: ("column_596"::unsigned -> "column_596", "column_696"::unsigned -> "column_696", "sum_1596"::decimal -> "sum_1596")
+                    motion [policy: segment([ref("column_596"), ref("column_696")])]
+                        projection ("t"."a"::unsigned -> "column_596", "t"."b"::unsigned -> "column_696", sum(("t"."c"::unsigned))::decimal -> "sum_1596")
                             group by ("t"."a"::unsigned, "t"."b"::unsigned) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
                                 scan "t"
         motion [policy: full]
             scan "t2"
-                projection ("column_55"::unsigned -> "g", "column_56"::unsigned -> "e", sum(("sum_74"::decimal))::decimal -> "f")
-                    group by ("column_55"::unsigned, "column_56"::unsigned) output: ("column_55"::unsigned -> "column_55", "column_56"::unsigned -> "column_56", "sum_74"::decimal -> "sum_74")
-                        motion [policy: segment([ref("column_55"), ref("column_56")])]
-                            projection ("t2"."g"::unsigned -> "column_55", "t2"."e"::unsigned -> "column_56", sum(("t2"."f"::unsigned))::decimal -> "sum_74")
+                projection ("column_2496"::unsigned -> "g", "column_2596"::unsigned -> "e", sum(("sum_3496"::decimal))::decimal -> "f")
+                    group by ("column_2496"::unsigned, "column_2596"::unsigned) output: ("column_2496"::unsigned -> "column_2496", "column_2596"::unsigned -> "column_2596", "sum_3496"::decimal -> "sum_3496")
+                        motion [policy: segment([ref("column_2496"), ref("column_2596")])]
+                            projection ("t2"."g"::unsigned -> "column_2496", "t2"."e"::unsigned -> "column_2596", sum(("t2"."f"::unsigned))::decimal -> "sum_3496")
                                 group by ("t2"."g"::unsigned, "t2"."e"::unsigned) output: ("t2"."e"::unsigned -> "e", "t2"."f"::unsigned -> "f", "t2"."g"::unsigned -> "g", "t2"."h"::unsigned -> "h", "t2"."bucket_id"::unsigned -> "bucket_id")
                                     scan "t2"
 execution options:
@@ -2510,6 +2500,7 @@ vtable_max_rows = 5000
     );
 
     assert_eq!(expected_explain, plan.as_explain().unwrap());
+    println!("{}", plan.as_explain().unwrap());
 }
 
 #[test]
@@ -2549,18 +2540,16 @@ fn front_sql_left_join_single_left() {
         "#;
 
     let plan = sql_to_optimized_ir(input, vec![]);
-    println!("{}", plan.as_explain().unwrap());
 
     let expected_explain = String::from(
         r#"projection ("t1"."a"::decimal -> "a", "t2"."b"::unsigned -> "b")
     left join on ROW("t1"."a"::decimal) = ROW("t2"."b"::unsigned)
         motion [policy: segment([ref("a")])]
             scan "t1"
-                projection (ROW(sum(("sum_14"::decimal))::decimal) / ROW(3::unsigned) -> "a")
+                projection (ROW(sum(("sum_696"::decimal))::decimal) / ROW(3::unsigned) -> "a")
                     motion [policy: full]
-                        scan
-                            projection (sum(("test_space"."id"::unsigned))::decimal -> "sum_14")
-                                scan "test_space"
+                        projection (sum(("test_space"."id"::unsigned))::decimal -> "sum_696")
+                            scan "test_space"
         motion [policy: full]
             scan "t2"
                 projection ("test_space"."id"::unsigned -> "b")
@@ -2572,6 +2561,7 @@ vtable_max_rows = 5000
     );
 
     assert_eq!(expected_explain, plan.as_explain().unwrap());
+    println!("{}", plan.as_explain().unwrap());
 }
 
 #[test]
@@ -2583,7 +2573,6 @@ fn front_sql_left_join_single_left2() {
         "#;
 
     let plan = sql_to_optimized_ir(input, vec![]);
-    println!("{}", plan.as_explain().unwrap());
 
     // full motion should be under outer child
     let expected_explain = String::from(
@@ -2591,11 +2580,10 @@ fn front_sql_left_join_single_left2() {
     left join on ROW("t1"."a"::decimal) + ROW(3::unsigned) <> ROW("t2"."b"::unsigned)
         motion [policy: segment([ref("a")])]
             scan "t1"
-                projection (ROW(sum(("sum_14"::decimal))::decimal) / ROW(3::unsigned) -> "a")
+                projection (ROW(sum(("sum_696"::decimal))::decimal) / ROW(3::unsigned) -> "a")
                     motion [policy: full]
-                        scan
-                            projection (sum(("test_space"."id"::unsigned))::decimal -> "sum_14")
-                                scan "test_space"
+                        projection (sum(("test_space"."id"::unsigned))::decimal -> "sum_696")
+                            scan "test_space"
         motion [policy: full]
             scan "t2"
                 projection ("test_space"."id"::unsigned -> "b")
@@ -2607,6 +2595,7 @@ vtable_max_rows = 5000
     );
 
     assert_eq!(expected_explain, plan.as_explain().unwrap());
+    println!("{}", plan.as_explain().unwrap());
 }
 
 #[test]
@@ -2618,24 +2607,21 @@ fn front_sql_left_join_single_both() {
         "#;
 
     let plan = sql_to_optimized_ir(input, vec![]);
-    println!("{}", plan.as_explain().unwrap());
 
     // full motion should be under outer child
     let expected_explain = String::from(
         r#"projection ("t1"."a"::decimal -> "a", "t2"."b"::integer -> "b")
     left join on ROW("t1"."a"::decimal) <> ROW("t2"."b"::integer)
         scan "t1"
-            projection (ROW(sum(("sum_14"::decimal))::decimal) / ROW(3::unsigned) -> "a")
+            projection (ROW(sum(("sum_696"::decimal))::decimal) / ROW(3::unsigned) -> "a")
                 motion [policy: full]
-                    scan
-                        projection (sum(("test_space"."id"::unsigned))::decimal -> "sum_14")
-                            scan "test_space"
+                    projection (sum(("test_space"."id"::unsigned))::decimal -> "sum_696")
+                        scan "test_space"
         scan "t2"
-            projection (sum(("count_38"::integer))::decimal -> "b")
+            projection (sum(("count_1496"::integer))::decimal -> "b")
                 motion [policy: full]
-                    scan
-                        projection (count(("test_space"."id"::unsigned))::integer -> "count_38")
-                            scan "test_space"
+                    projection (count(("test_space"."id"::unsigned))::integer -> "count_1496")
+                        scan "test_space"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
@@ -2643,6 +2629,7 @@ vtable_max_rows = 5000
     );
 
     assert_eq!(expected_explain, plan.as_explain().unwrap());
+    println!("{}", plan.as_explain().unwrap());
 }
 
 #[test]
@@ -2685,20 +2672,20 @@ fn front_sql_having1() {
 
     let plan = sql_to_optimized_ir(input, vec![]);
     let expected_explain = String::from(
-        r#"projection ("column_764"::unsigned -> "a", sum(("sum_52"::decimal))::decimal -> "col_1")
-    having ROW("column_764"::unsigned) > ROW(1::unsigned) and ROW(sum(distinct ("column_27"::decimal))::decimal) > ROW(1::unsigned)
-        group by ("column_764"::unsigned) output: ("column_764"::unsigned -> "column_764", "column_27"::unsigned -> "column_27", "sum_52"::decimal -> "sum_52")
-            motion [policy: segment([ref("column_764")])]
-                scan
-                    projection ("t"."a"::unsigned -> "column_764", "t"."b"::unsigned -> "column_27", sum(("t"."b"::unsigned))::decimal -> "sum_52")
-                        group by ("t"."a"::unsigned, "t"."b"::unsigned) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
-                            scan "t"
+        r#"projection ("column_596"::unsigned -> "a", sum(("sum_2196"::decimal))::decimal -> "col_1")
+    having ROW("column_596"::unsigned) > ROW(1::unsigned) and ROW(sum(distinct ("column_1296"::decimal))::decimal) > ROW(1::unsigned)
+        group by ("column_596"::unsigned) output: ("column_596"::unsigned -> "column_596", "column_1296"::unsigned -> "column_1296", "sum_2196"::decimal -> "sum_2196")
+            motion [policy: segment([ref("column_596")])]
+                projection ("t"."a"::unsigned -> "column_596", "t"."b"::unsigned -> "column_1296", sum(("t"."b"::unsigned))::decimal -> "sum_2196")
+                    group by ("t"."a"::unsigned, "t"."b"::unsigned) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
+                        scan "t"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
 "#,
     );
 
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 }
 
@@ -2710,19 +2697,19 @@ fn front_sql_having2() {
 
     let plan = sql_to_optimized_ir(input, vec![]);
     let expected_explain = String::from(
-        r#"projection (ROW(sum(("sum_39"::decimal))::decimal) * ROW(count(distinct ("column_38"::integer))::integer) -> "col_1", sum(("sum_39"::decimal))::decimal -> "col_2")
-    having ROW(sum(distinct ("column_38"::decimal))::decimal) > ROW(1::unsigned) and ROW(sum(("sum_39"::decimal))::decimal) > ROW(1::unsigned)
+        r#"projection (ROW(sum(("sum_1696"::decimal))::decimal) * ROW(count(distinct ("column_1596"::integer))::integer) -> "col_1", sum(("sum_1696"::decimal))::decimal -> "col_2")
+    having ROW(sum(distinct ("column_1596"::decimal))::decimal) > ROW(1::unsigned) and ROW(sum(("sum_1696"::decimal))::decimal) > ROW(1::unsigned)
         motion [policy: full]
-            scan
-                projection ("t"."b"::unsigned -> "column_38", sum(("t"."a"::unsigned))::decimal -> "sum_39")
-                    group by ("t"."b"::unsigned) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
-                        scan "t"
+            projection ("t"."b"::unsigned -> "column_1596", sum(("t"."a"::unsigned))::decimal -> "sum_1696")
+                group by ("t"."b"::unsigned) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
+                    scan "t"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
 "#,
     );
 
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 }
 
@@ -2734,18 +2721,18 @@ fn front_sql_having3() {
 
     let plan = sql_to_optimized_ir(input, vec![]);
     let expected_explain = String::from(
-        r#"projection (sum(("sum_31"::decimal))::decimal -> "col_1")
-    having ROW(sum(("sum_31"::decimal))::decimal) > ROW(1::unsigned)
+        r#"projection (sum(("sum_1396"::decimal))::decimal -> "col_1")
+    having ROW(sum(("sum_1396"::decimal))::decimal) > ROW(1::unsigned)
         motion [policy: full]
-            scan
-                projection (sum(("t"."a"::unsigned))::decimal -> "sum_31")
-                    scan "t"
+            projection (sum(("t"."a"::unsigned))::decimal -> "sum_1396")
+                scan "t"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
 "#,
     );
 
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 }
 
@@ -2775,14 +2762,13 @@ fn front_sql_having_with_sq() {
     let plan = sql_to_optimized_ir(input, vec![]);
 
     let expected_explain = String::from(
-        r#"projection ("column_764"::unsigned -> "sysFrom", sum(distinct ("column_80"::decimal))::decimal -> "sum", count(distinct ("column_80"::integer))::integer -> "count")
-    having ROW($0) > ROW(count(distinct ("column_80"::integer))::integer)
-        group by ("column_764"::unsigned) output: ("column_764"::unsigned -> "column_764", "column_80"::unsigned -> "column_80")
-            motion [policy: segment([ref("column_764")])]
-                scan
-                    projection ("test_space"."sysFrom"::unsigned -> "column_764", "test_space"."id"::unsigned -> "column_80")
-                        group by ("test_space"."sysFrom"::unsigned, "test_space"."id"::unsigned) output: ("test_space"."id"::unsigned -> "id", "test_space"."sysFrom"::unsigned -> "sysFrom", "test_space"."FIRST_NAME"::string -> "FIRST_NAME", "test_space"."sys_op"::unsigned -> "sys_op", "test_space"."bucket_id"::unsigned -> "bucket_id")
-                            scan "test_space"
+        r#"projection ("column_596"::unsigned -> "sysFrom", sum(distinct ("column_3296"::decimal))::decimal -> "sum", count(distinct ("column_3296"::integer))::integer -> "count")
+    having ROW($0) > ROW(count(distinct ("column_3296"::integer))::integer)
+        group by ("column_596"::unsigned) output: ("column_596"::unsigned -> "column_596", "column_3296"::unsigned -> "column_3296")
+            motion [policy: segment([ref("column_596")])]
+                projection ("test_space"."sysFrom"::unsigned -> "column_596", "test_space"."id"::unsigned -> "column_3296")
+                    group by ("test_space"."sysFrom"::unsigned, "test_space"."id"::unsigned) output: ("test_space"."id"::unsigned -> "id", "test_space"."sysFrom"::unsigned -> "sysFrom", "test_space"."FIRST_NAME"::string -> "FIRST_NAME", "test_space"."sys_op"::unsigned -> "sys_op", "test_space"."bucket_id"::unsigned -> "bucket_id")
+                        scan "test_space"
 subquery $0:
 motion [policy: full]
             scan
@@ -2795,6 +2781,7 @@ vtable_max_rows = 5000
 "#,
     );
 
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 }
 
@@ -2824,14 +2811,13 @@ fn front_sql_having_with_sq_segment_motion() {
     "#;
 
     let plan = sql_to_optimized_ir(input, vec![]);
-    println!("{}", plan.as_explain().unwrap());
 
     let expected_explain = String::from(
-        r#"projection ("column_12"::unsigned -> "sysFrom", "column_13"::unsigned -> "sys_op", sum(distinct ("column_70"::decimal))::decimal -> "sum", count(distinct ("column_70"::integer))::integer -> "count")
-    having ROW("column_12"::unsigned, "column_13"::unsigned) in ROW($0, $0)
-        group by ("column_12"::unsigned, "column_13"::unsigned) output: ("column_12"::unsigned -> "column_12", "column_13"::unsigned -> "column_13", "column_70"::unsigned -> "column_70")
-            motion [policy: segment([ref("column_12"), ref("column_13")])]
-                projection ("test_space"."sysFrom"::unsigned -> "column_12", "test_space"."sys_op"::unsigned -> "column_13", "test_space"."id"::unsigned -> "column_70")
+        r#"projection ("column_596"::unsigned -> "sysFrom", "column_696"::unsigned -> "sys_op", sum(distinct ("column_3096"::decimal))::decimal -> "sum", count(distinct ("column_3096"::integer))::integer -> "count")
+    having ROW("column_596"::unsigned, "column_696"::unsigned) in ROW($0, $0)
+        group by ("column_596"::unsigned, "column_696"::unsigned) output: ("column_596"::unsigned -> "column_596", "column_696"::unsigned -> "column_696", "column_3096"::unsigned -> "column_3096")
+            motion [policy: segment([ref("column_596"), ref("column_696")])]
+                projection ("test_space"."sysFrom"::unsigned -> "column_596", "test_space"."sys_op"::unsigned -> "column_696", "test_space"."id"::unsigned -> "column_3096")
                     group by ("test_space"."sysFrom"::unsigned, "test_space"."sys_op"::unsigned, "test_space"."id"::unsigned) output: ("test_space"."id"::unsigned -> "id", "test_space"."sysFrom"::unsigned -> "sysFrom", "test_space"."FIRST_NAME"::string -> "FIRST_NAME", "test_space"."sys_op"::unsigned -> "sys_op", "test_space"."bucket_id"::unsigned -> "bucket_id")
                         scan "test_space"
 subquery $0:
@@ -2846,6 +2832,7 @@ vtable_max_rows = 5000
     );
 
     assert_eq!(expected_explain, plan.as_explain().unwrap());
+    println!("{}", plan.as_explain().unwrap());
 }
 
 #[test]
@@ -2861,11 +2848,11 @@ fn front_sql_having_with_sq_segment_local_motion() {
     let plan = sql_to_optimized_ir(input, vec![]);
 
     let expected_explain = String::from(
-        r#"projection ("column_12"::unsigned -> "sysFrom", "column_13"::unsigned -> "sys_op", sum(distinct ("column_70"::decimal))::decimal -> "sum", count(distinct ("column_70"::integer))::integer -> "count")
-    having ROW("column_12"::unsigned, "column_13"::unsigned) in ROW($0, $0)
-        group by ("column_12"::unsigned, "column_13"::unsigned) output: ("column_12"::unsigned -> "column_12", "column_13"::unsigned -> "column_13", "column_70"::unsigned -> "column_70")
-            motion [policy: segment([ref("column_12"), ref("column_13")])]
-                projection ("test_space"."sysFrom"::unsigned -> "column_12", "test_space"."sys_op"::unsigned -> "column_13", "test_space"."id"::unsigned -> "column_70")
+        r#"projection ("column_596"::unsigned -> "sysFrom", "column_696"::unsigned -> "sys_op", sum(distinct ("column_3096"::decimal))::decimal -> "sum", count(distinct ("column_3096"::integer))::integer -> "count")
+    having ROW("column_596"::unsigned, "column_696"::unsigned) in ROW($0, $0)
+        group by ("column_596"::unsigned, "column_696"::unsigned) output: ("column_596"::unsigned -> "column_596", "column_696"::unsigned -> "column_696", "column_3096"::unsigned -> "column_3096")
+            motion [policy: segment([ref("column_596"), ref("column_696")])]
+                projection ("test_space"."sysFrom"::unsigned -> "column_596", "test_space"."sys_op"::unsigned -> "column_696", "test_space"."id"::unsigned -> "column_3096")
                     group by ("test_space"."sysFrom"::unsigned, "test_space"."sys_op"::unsigned, "test_space"."id"::unsigned) output: ("test_space"."id"::unsigned -> "id", "test_space"."sysFrom"::unsigned -> "sysFrom", "test_space"."FIRST_NAME"::string -> "FIRST_NAME", "test_space"."sys_op"::unsigned -> "sys_op", "test_space"."bucket_id"::unsigned -> "bucket_id")
                         scan "test_space"
 subquery $0:
@@ -2878,6 +2865,7 @@ vtable_max_rows = 5000
 "#,
     );
 
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 }
 
@@ -2887,14 +2875,12 @@ fn front_sql_unique_local_aggregates() {
     let input = r#"SELECT sum("a"), count("a"), sum("a") + count("a") FROM "t""#;
 
     let plan = sql_to_optimized_ir(input, vec![]);
-    println!("{}", plan.as_explain().unwrap());
     // here we must compute only two aggregates at local stage: sum(a), count(a)
     let expected_explain = String::from(
-        r#"projection (sum(("sum_13"::decimal))::decimal -> "col_1", sum(("count_16"::integer))::decimal -> "col_2", ROW(sum(("sum_13"::decimal))::decimal) + ROW(sum(("count_16"::integer))::decimal) -> "col_3")
+        r#"projection (sum(("sum_696"::decimal))::decimal -> "col_1", sum(("count_896"::integer))::decimal -> "col_2", ROW(sum(("sum_696"::decimal))::decimal) + ROW(sum(("count_896"::integer))::decimal) -> "col_3")
     motion [policy: full]
-        scan
-            projection (sum(("t"."a"::unsigned))::decimal -> "sum_13", count(("t"."a"::unsigned))::integer -> "count_16")
-                scan "t"
+        projection (sum(("t"."a"::unsigned))::decimal -> "sum_696", count(("t"."a"::unsigned))::integer -> "count_896")
+            scan "t"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
@@ -2902,6 +2888,7 @@ vtable_max_rows = 5000
     );
 
     assert_eq!(expected_explain, plan.as_explain().unwrap());
+    println!("{}", plan.as_explain().unwrap());
 }
 
 #[test]
@@ -2914,19 +2901,19 @@ fn front_sql_unique_local_groupings() {
     let plan = sql_to_optimized_ir(input, vec![]);
     // here we must compute only two groupby columns at local stage: a, b
     let expected_explain = String::from(
-        r#"projection (sum(distinct ("column_25"::decimal))::decimal -> "col_1", count(distinct ("column_25"::integer))::integer -> "col_2", count(distinct ("column_764"::integer))::integer -> "col_3")
-    group by ("column_764"::unsigned) output: ("column_764"::unsigned -> "column_764", "column_25"::unsigned -> "column_25")
-        motion [policy: segment([ref("column_764")])]
-            scan
-                projection ("t"."b"::unsigned -> "column_764", "t"."a"::unsigned -> "column_25")
-                    group by ("t"."b"::unsigned, "t"."a"::unsigned) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
-                        scan "t"
+        r#"projection (sum(distinct ("column_1196"::decimal))::decimal -> "col_1", count(distinct ("column_1196"::integer))::integer -> "col_2", count(distinct ("column_596"::integer))::integer -> "col_3")
+    group by ("column_596"::unsigned) output: ("column_596"::unsigned -> "column_596", "column_1196"::unsigned -> "column_1196")
+        motion [policy: segment([ref("column_596")])]
+            projection ("t"."b"::unsigned -> "column_596", "t"."a"::unsigned -> "column_1196")
+                group by ("t"."b"::unsigned, "t"."a"::unsigned) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
+                    scan "t"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
 "#,
     );
 
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 }
 
@@ -2976,13 +2963,12 @@ fn front_sql_select_distinct() {
     let input = r#"SELECT distinct "a", "a" + "b" FROM "t""#;
 
     let plan = sql_to_optimized_ir(input, vec![]);
-    println!("{}", plan.as_explain().unwrap());
     // here we must compute only two groupby columns at local stage: a, b
     let expected_explain = String::from(
-        r#"projection ("column_22"::unsigned -> "a", "column_27"::unsigned -> "col_1")
-    group by ("column_22"::unsigned, "column_27"::unsigned) output: ("column_22"::unsigned -> "column_22", "column_27"::unsigned -> "column_27")
-        motion [policy: segment([ref("column_22"), ref("column_27")])]
-            projection ("t"."a"::unsigned -> "column_22", ROW("t"."a"::unsigned) + ROW("t"."b"::unsigned) -> "column_27")
+        r#"projection ("column_896"::unsigned -> "a", "column_832"::unsigned -> "col_1")
+    group by ("column_896"::unsigned, "column_832"::unsigned) output: ("column_896"::unsigned -> "column_896", "column_832"::unsigned -> "column_832")
+        motion [policy: segment([ref("column_896"), ref("column_832")])]
+            projection ("t"."a"::unsigned -> "column_896", ROW("t"."a"::unsigned) + ROW("t"."b"::unsigned) -> "column_832")
                 group by ("t"."a"::unsigned, ROW("t"."a"::unsigned) + ROW("t"."b"::unsigned)) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
                     scan "t"
 execution options:
@@ -2992,6 +2978,7 @@ vtable_max_rows = 5000
     );
 
     assert_eq!(expected_explain, plan.as_explain().unwrap());
+    println!("{}", plan.as_explain().unwrap());
 }
 
 #[test]
@@ -2999,12 +2986,11 @@ fn front_sql_select_distinct_asterisk() {
     let input = r#"SELECT distinct * FROM "t""#;
 
     let plan = sql_to_optimized_ir(input, vec![]);
-    println!("{}", plan.as_explain().unwrap());
     let expected_explain = String::from(
-        r#"projection ("column_23"::unsigned -> "a", "column_24"::unsigned -> "b", "column_25"::unsigned -> "c", "column_26"::unsigned -> "d")
-    group by ("column_23"::unsigned, "column_24"::unsigned, "column_25"::unsigned, "column_26"::unsigned) output: ("column_23"::unsigned -> "column_23", "column_24"::unsigned -> "column_24", "column_25"::unsigned -> "column_25", "column_26"::unsigned -> "column_26")
-        motion [policy: segment([ref("column_23"), ref("column_24"), ref("column_25"), ref("column_26")])]
-            projection ("t"."a"::unsigned -> "column_23", "t"."b"::unsigned -> "column_24", "t"."c"::unsigned -> "column_25", "t"."d"::unsigned -> "column_26")
+        r#"projection ("column_996"::unsigned -> "a", "column_1096"::unsigned -> "b", "column_1196"::unsigned -> "c", "column_1296"::unsigned -> "d")
+    group by ("column_996"::unsigned, "column_1096"::unsigned, "column_1196"::unsigned, "column_1296"::unsigned) output: ("column_996"::unsigned -> "column_996", "column_1096"::unsigned -> "column_1096", "column_1196"::unsigned -> "column_1196", "column_1296"::unsigned -> "column_1296")
+        motion [policy: segment([ref("column_996"), ref("column_1096"), ref("column_1196"), ref("column_1296")])]
+            projection ("t"."a"::unsigned -> "column_996", "t"."b"::unsigned -> "column_1096", "t"."c"::unsigned -> "column_1196", "t"."d"::unsigned -> "column_1296")
                 group by ("t"."a"::unsigned, "t"."b"::unsigned, "t"."c"::unsigned, "t"."d"::unsigned) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
                     scan "t"
 execution options:
@@ -3014,6 +3000,7 @@ vtable_max_rows = 5000
     );
 
     assert_eq!(expected_explain, plan.as_explain().unwrap());
+    println!("{}", plan.as_explain().unwrap());
 }
 
 #[test]
@@ -3039,23 +3026,19 @@ fn front_sql_select_distinct_with_aggr() {
 
     let plan = sql_to_optimized_ir(input, vec![]);
     let expected_explain = String::from(
-        r#"projection (sum(("sum_26"::decimal))::decimal -> "col_1", "column_12"::unsigned -> "b")
-    group by ("column_12"::unsigned) output: ("column_12"::unsigned -> "column_12", "sum_26"::decimal -> "sum_26")
-        motion [policy: segment([ref("column_12")])]
-            projection ("t"."b"::unsigned -> "column_12", sum(("t"."a"::unsigned))::decimal -> "sum_26")
-        r#"projection (sum(("sum_196"::decimal))::decimal -> "col_1")
-    having ROW(sum(("sum_196"::decimal))::decimal) > ROW(1::unsigned)
-            projection (sum(("t"."a"::unsigned))::decimal -> "sum_31")
-                scan "t"
-        scan
-            projection (sum(("t"."a"::unsigned))::decimal -> "sum_096", count(("t"."a"::unsigned))::integer -> "count_196")
-                scan "t"
+        r#"projection (sum(("sum_1296"::decimal))::decimal -> "col_1", "column_596"::unsigned -> "b")
+    group by ("column_596"::unsigned) output: ("column_596"::unsigned -> "column_596", "sum_1296"::decimal -> "sum_1296")
+        motion [policy: segment([ref("column_596")])]
+            projection ("t"."b"::unsigned -> "column_596", sum(("t"."a"::unsigned))::decimal -> "sum_1296")
+                group by ("t"."b"::unsigned) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
+                    scan "t"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
 "#,
     );
 
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 }
 
@@ -3065,9 +3048,9 @@ fn front_sql_select_distinct_with_aggr2() {
 
     let plan = sql_to_optimized_ir(input, vec![]);
     let expected_explain = String::from(
-        r#"projection (sum(("sum_096"::decimal))::decimal -> "col_1")
+        r#"projection (sum(("sum_696"::decimal))::decimal -> "col_1")
     motion [policy: full]
-        projection (sum(("t"."a"::unsigned))::decimal -> "sum_13")
+        projection (sum(("t"."a"::unsigned))::decimal -> "sum_696")
             scan "t"
 execution options:
 sql_vdbe_max_steps = 45000
@@ -3075,6 +3058,7 @@ vtable_max_rows = 5000
 "#,
     );
 
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 }
 
@@ -3423,15 +3407,16 @@ fn front_sql_update6() {
 subquery $0:
 motion [policy: full]
                     scan
-                        projection (sum(("sum_096"::decimal))::decimal -> "s")
+                        projection (sum(("sum_796"::decimal))::decimal -> "s")
                             motion [policy: full]
-                                projection (sum(("t3"."b"::integer))::decimal -> "sum_17")
+                                projection (sum(("t3"."b"::integer))::decimal -> "sum_796")
                                     scan "t3"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
 "#,
     );
+    println!("{}", plan.as_explain().unwrap());
     assert_eq!(expected_explain, plan.as_explain().unwrap());
 }
 
diff --git a/sbroad-core/src/frontend/sql/ir/tests/global.rs b/sbroad-core/src/frontend/sql/ir/tests/global.rs
index 924226037..21cbc775d 100644
--- a/sbroad-core/src/frontend/sql/ir/tests/global.rs
+++ b/sbroad-core/src/frontend/sql/ir/tests/global.rs
@@ -82,13 +82,13 @@ fn front_sql_global_tbl_sq1() {
     let plan = sql_to_optimized_ir(input, vec![]);
     let expected_explain = String::from(
         r#"projection ("global_t"."a"::integer -> "a", "global_t"."b"::integer -> "b")
-    selection ROW("global_t"."a"::integer) in ROW($0) or ROW("global_t"."a"::integer) in ROW($1)
+    selection ROW("global_t"."a"::integer) in ROW($1) or ROW("global_t"."a"::integer) in ROW($0)
         scan "global_t"
 subquery $0:
 scan
-            projection (sum(("sum_39"::decimal))::decimal -> "col_1")
+            projection (sum(("sum_1596"::decimal))::decimal -> "col_1")
                 motion [policy: full]
-                    projection (sum(("t"."a"::unsigned))::decimal -> "sum_39")
+                    projection (sum(("t"."a"::unsigned))::decimal -> "sum_1596")
                         scan "t"
 subquery $1:
 motion [policy: full]
@@ -118,18 +118,18 @@ fn front_sql_global_tbl_multiple_sqs1() {
     let plan = sql_to_optimized_ir(input, vec![]);
     let expected_explain = String::from(
         r#"projection ("global_t"."a"::integer -> "a", "global_t"."b"::integer -> "b")
-    selection ROW("global_t"."a"::integer, "global_t"."b"::integer) in ROW($1, $1) and ROW("global_t"."a"::integer) in ROW($0)
+    selection ROW("global_t"."a"::integer, "global_t"."b"::integer) in ROW($0, $0) and ROW("global_t"."a"::integer) in ROW($1)
         scan "global_t"
 subquery $0:
-scan
-            projection (sum(("sum_096"::decimal))::decimal -> "col_1")
-                motion [policy: full]
-                    projection (sum(("t"."a"::unsigned))::decimal -> "sum_096")
-                        scan "t"
-subquery $1:
 scan
             projection ("t"."a"::unsigned -> "a1", "t"."b"::unsigned -> "b1")
                 scan "t"
+subquery $1:
+scan
+            projection (sum(("sum_1796"::decimal))::decimal -> "col_1")
+                motion [policy: full]
+                    projection (sum(("t"."a"::unsigned))::decimal -> "sum_1796")
+                        scan "t"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
@@ -155,19 +155,19 @@ fn front_sql_global_tbl_multiple_sqs2() {
     let plan = sql_to_optimized_ir(input, vec![]);
     let expected_explain = String::from(
         r#"projection ("global_t"."a"::integer -> "a", "global_t"."b"::integer -> "b")
-    selection ROW("global_t"."a"::integer, "global_t"."b"::integer) in ROW($1, $1) or ROW("global_t"."a"::integer) in ROW($0)
+    selection ROW("global_t"."a"::integer, "global_t"."b"::integer) in ROW($0, $0) or ROW("global_t"."a"::integer) in ROW($1)
         scan "global_t"
 subquery $0:
-scan
-            projection (sum(("sum_096"::decimal))::decimal -> "col_1")
-                motion [policy: full]
-                    projection (sum(("t"."a"::unsigned))::decimal -> "sum_096")
-                        scan "t"
-subquery $1:
 motion [policy: full]
             scan
                 projection ("t"."a"::unsigned -> "a1", "t"."b"::unsigned -> "b1")
                     scan "t"
+subquery $1:
+scan
+            projection (sum(("sum_1796"::decimal))::decimal -> "col_1")
+                motion [policy: full]
+                    projection (sum(("t"."a"::unsigned))::decimal -> "sum_1796")
+                        scan "t"
 execution options:
 sql_vdbe_max_steps = 45000
 vtable_max_rows = 5000
@@ -215,7 +215,7 @@ fn front_sql_global_tbl_sq3() {
     let plan = sql_to_optimized_ir(input, vec![]);
     let expected_explain = String::from(
         r#"projection ("global_t"."a"::integer -> "a", "global_t"."b"::integer -> "b")
-    selection not ROW("global_t"."a"::integer, "global_t"."b"::integer) in ROW($0, $0) or ROW("global_t"."a"::integer, "global_t"."b"::integer) < ROW($1, $1)
+    selection not ROW("global_t"."a"::integer, "global_t"."b"::integer) in ROW($1, $1) or ROW("global_t"."a"::integer, "global_t"."b"::integer) < ROW($0, $0)
         scan "global_t"
 subquery $0:
 motion [policy: full]
@@ -315,7 +315,7 @@ fn front_sql_global_tbl_sq6() {
     let expected_explain = String::from(
         r#"projection ("t"."a"::unsigned -> "a", "t2"."f"::unsigned -> "f")
     selection ROW("t2"."e"::unsigned) in ROW($3)
-        join on ROW("t"."a"::unsigned, "t"."b"::unsigned) = ROW("t2"."e"::unsigned, "t2"."f"::unsigned) or ROW("t"."c"::unsigned) in ROW($2) or exists ROW($1) and not ROW("t"."d"::unsigned) in ROW($0)
+        join on ROW("t"."a"::unsigned, "t"."b"::unsigned) = ROW("t2"."e"::unsigned, "t2"."f"::unsigned) or ROW("t"."c"::unsigned) in ROW($2) or exists ROW($0) and not ROW("t"."d"::unsigned) in ROW($1)
             scan "t"
                 projection ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d")
                     scan "t"
@@ -324,14 +324,14 @@ fn front_sql_global_tbl_sq6() {
                     projection ("t2"."e"::unsigned -> "e", "t2"."f"::unsigned -> "f", "t2"."g"::unsigned -> "g", "t2"."h"::unsigned -> "h")
                         scan "t2"
 subquery $0:
-scan
-                projection ("global_t"."a"::integer -> "a1")
-                    scan "global_t"
-subquery $1:
 scan
                 projection (ROW("global_t"."a"::integer) * ROW(20::unsigned) -> "a1")
                     selection ROW("global_t"."a"::integer) = ROW(1::unsigned)
                         scan "global_t"
+subquery $1:
+scan
+                projection ("global_t"."a"::integer -> "a1")
+                    scan "global_t"
 subquery $2:
 scan
                 projection ("global_t"."a"::integer -> "a1")
@@ -361,7 +361,7 @@ fn front_sql_global_tbl_sq7() {
     let plan = sql_to_optimized_ir(input, vec![]);
     let expected_explain = String::from(
         r#"projection ("t"."a"::unsigned -> "a", "t2"."f"::unsigned -> "f")
-    join on ROW("t"."a"::unsigned, "t"."b"::unsigned) = ROW("t2"."e"::unsigned, "t2"."f"::unsigned) or ROW("t"."c"::unsigned) in ROW($0) or not ROW("t"."d"::unsigned) in ROW($1)
+    join on ROW("t"."a"::unsigned, "t"."b"::unsigned) = ROW("t2"."e"::unsigned, "t2"."f"::unsigned) or ROW("t"."c"::unsigned) in ROW($1) or not ROW("t"."d"::unsigned) in ROW($0)
         scan "t"
             projection ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d")
                 scan "t"
@@ -492,9 +492,9 @@ fn front_sql_global_join4() {
         r#"projection ("s"."e"::decimal -> "e")
     left join on true::boolean
         scan "s"
-            projection (sum(("sum_096"::decimal))::decimal -> "e")
+            projection (sum(("sum_696"::decimal))::decimal -> "e")
                 motion [policy: full]
-                    projection (sum(("t2"."e"::unsigned))::decimal -> "sum_096")
+                    projection (sum(("t2"."e"::unsigned))::decimal -> "sum_696")
                         scan "t2"
         scan "global_t"
             projection ("global_t"."a"::integer -> "a", "global_t"."b"::integer -> "b")
@@ -525,9 +525,9 @@ fn front_sql_global_join5() {
             projection ("global_t"."a"::integer -> "a", "global_t"."b"::integer -> "b")
                 scan "global_t"
         scan "s"
-            projection (sum(("sum_096"::decimal))::decimal -> "e")
+            projection (sum(("sum_896"::decimal))::decimal -> "e")
                 motion [policy: full]
-                    projection (sum(("t2"."e"::unsigned))::decimal -> "sum_096")
+                    projection (sum(("t2"."e"::unsigned))::decimal -> "sum_896")
                         scan "t2"
 execution options:
 sql_vdbe_max_steps = 45000
@@ -808,11 +808,11 @@ fn front_sql_global_aggregate5() {
     let plan = sql_to_optimized_ir(input, vec![]);
 
     let expected_explain = String::from(
-        r#"projection ("column_1432"::integer -> "col_1", sum(("sum_196"::decimal))::decimal -> "col_2")
-    having ROW(sum(("sum_096"::decimal::double))::decimal / sum(("count_096"::decimal::double))::decimal) > ROW(3::unsigned)
-        group by ("column_1432"::integer) output: ("column_1432"::integer -> "column_1432", "sum_196"::decimal -> "sum_196", "sum_096"::decimal -> "sum_096", "count_096"::integer -> "count_096")
+        r#"projection ("column_1432"::integer -> "col_1", sum(("sum_2696"::decimal))::decimal -> "col_2")
+    having ROW(sum(("sum_2096"::decimal::double))::decimal / sum(("count_2096"::decimal::double))::decimal) > ROW(3::unsigned)
+        group by ("column_1432"::integer) output: ("column_1432"::integer -> "column_1432", "sum_2696"::decimal -> "sum_2696", "sum_2096"::decimal -> "sum_2096", "count_2096"::integer -> "count_2096")
             motion [policy: segment([ref("column_1432")])]
-                projection (ROW("global_t"."b"::integer) + ROW("global_t"."a"::integer) -> "column_1432", sum(("global_t"."a"::integer))::decimal -> "sum_196", sum(("global_t"."b"::integer))::decimal -> "sum_096", count(("global_t"."b"::integer))::integer -> "count_096")
+                projection (ROW("global_t"."b"::integer) + ROW("global_t"."a"::integer) -> "column_1432", sum(("global_t"."a"::integer))::decimal -> "sum_2696", sum(("global_t"."b"::integer))::decimal -> "sum_2096", count(("global_t"."b"::integer))::integer -> "count_2096")
                     group by (ROW("global_t"."b"::integer) + ROW("global_t"."a"::integer)) output: ("global_t"."a"::integer -> "a", "global_t"."b"::integer -> "b")
                         selection ROW("global_t"."a"::integer, "global_t"."b"::integer) in ROW($0, $0)
                             scan "global_t"
@@ -1055,9 +1055,9 @@ fn front_sql_global_union_all3() {
                 projection ("global_t"."a"::integer -> "a")
                     scan "global_t"
                 motion [policy: segment([ref("col_1")])]
-                    projection (sum(("sum_096"::decimal))::decimal -> "col_1")
+                    projection (sum(("sum_996"::decimal))::decimal -> "col_1")
                         motion [policy: full]
-                            projection (sum(("t2"."e"::unsigned))::decimal -> "sum_096")
+                            projection (sum(("t2"."e"::unsigned))::decimal -> "sum_996")
                                 scan "t2"
     motion [policy: local]
         projection ("global_t"."b"::integer -> "b")
@@ -1164,9 +1164,9 @@ fn front_sql_global_union2() {
         projection ("global_t"."a"::integer -> "a")
             scan "global_t"
         motion [policy: segment([ref("col_1")])]
-            projection (sum(("sum_096"::decimal))::decimal -> "col_1")
+            projection (sum(("sum_996"::decimal))::decimal -> "col_1")
                 motion [policy: full]
-                    projection (sum(("t2"."e"::unsigned))::decimal -> "sum_096")
+                    projection (sum(("t2"."e"::unsigned))::decimal -> "sum_996")
                         scan "t2"
 execution options:
 sql_vdbe_max_steps = 45000
@@ -1313,9 +1313,9 @@ fn check_plan_except_global_vs_single() {
         r#"except
     projection ("global_t"."a"::integer -> "a")
         scan "global_t"
-    projection (sum(("sum_096"::decimal))::decimal -> "col_1")
+    projection (sum(("sum_996"::decimal))::decimal -> "col_1")
         motion [policy: full]
-            projection (sum(("t2"."e"::unsigned))::decimal -> "sum_096")
+            projection (sum(("t2"."e"::unsigned))::decimal -> "sum_996")
                 scan "t2"
 execution options:
 sql_vdbe_max_steps = 45000
@@ -1337,9 +1337,9 @@ fn check_plan_except_single_vs_global() {
 
     let expected_explain = String::from(
         r#"except
-    projection (sum(("sum_096"::decimal))::decimal -> "col_1")
+    projection (sum(("sum_696"::decimal))::decimal -> "col_1")
         motion [policy: full]
-            projection (sum(("t2"."e"::unsigned))::decimal -> "sum_096")
+            projection (sum(("t2"."e"::unsigned))::decimal -> "sum_696")
                 scan "t2"
     projection ("global_t"."a"::integer -> "a")
         scan "global_t"
diff --git a/sbroad-core/src/frontend/sql/ir/tests/limit.rs b/sbroad-core/src/frontend/sql/ir/tests/limit.rs
index c8f133ac1..cb962ad59 100644
--- a/sbroad-core/src/frontend/sql/ir/tests/limit.rs
+++ b/sbroad-core/src/frontend/sql/ir/tests/limit.rs
@@ -55,9 +55,9 @@ fn aggregate() {
 
     let expected_explain = String::from(
         r#"limit 1
-    projection (min(("min_096"::unsigned))::scalar -> "col_1", min(distinct ("column_864"::unsigned))::scalar -> "col_2")
+    projection (min(("min_696"::unsigned))::scalar -> "col_1", min(distinct ("column_796"::unsigned))::scalar -> "col_2")
         motion [policy: full]
-            projection ("t"."b"::unsigned -> "column_864", min(("t"."b"::unsigned))::scalar -> "min_096")
+            projection ("t"."b"::unsigned -> "column_796", min(("t"."b"::unsigned))::scalar -> "min_696")
                 group by ("t"."b"::unsigned) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
                     scan "t"
 execution options:
@@ -79,10 +79,10 @@ fn group_by() {
         r#"limit 555
     motion [policy: full]
         limit 555
-            projection (sum(("count_096"::integer))::decimal -> "col_1", "column_764"::unsigned -> "b")
-                group by ("column_764"::unsigned) output: ("column_764"::unsigned -> "column_764", "count_096"::integer -> "count_096")
-                    motion [policy: segment([ref("column_764")])]
-                        projection ("t"."b"::unsigned -> "column_764", count((*::integer))::integer -> "count_096")
+            projection (sum(("count_1196"::integer))::decimal -> "col_1", "column_596"::unsigned -> "b")
+                group by ("column_596"::unsigned) output: ("column_596"::unsigned -> "column_596", "count_1196"::integer -> "count_1196")
+                    motion [policy: segment([ref("column_596")])]
+                        projection ("t"."b"::unsigned -> "column_596", count((*::integer))::integer -> "count_1196")
                             group by ("t"."b"::unsigned) output: ("t"."a"::unsigned -> "a", "t"."b"::unsigned -> "b", "t"."c"::unsigned -> "c", "t"."d"::unsigned -> "d", "t"."bucket_id"::unsigned -> "bucket_id")
                                 scan "t"
 execution options:
diff --git a/sbroad-core/src/ir.rs b/sbroad-core/src/ir.rs
index 75472cbf5..e9094fd3e 100644
--- a/sbroad-core/src/ir.rs
+++ b/sbroad-core/src/ir.rs
@@ -29,8 +29,9 @@ use crate::executor::engine::TableVersionMap;
 use crate::ir::helpers::RepeatableState;
 use crate::ir::node::{
     Alias, ArenaType, ArithmeticExpr, BoolExpr, Case, Cast, Concat, Constant, ExprInParentheses,
-    GroupBy, Insert, Motion, MutNode, Node, Node136, Node224, Node32, Node64, Node96, NodeId,
-    NodeOwned, Projection, Reference, Row, ScanRelation, StableFunction, Trim, UnaryExpr, Values,
+    GroupBy, Having, Insert, Limit, Motion, MutNode, Node, Node136, Node224, Node32, Node64,
+    Node96, NodeId, NodeOwned, OrderBy, Projection, Reference, Row, ScanRelation, Selection,
+    StableFunction, Trim, UnaryExpr, Values,
 };
 use crate::ir::operator::Bool;
 use crate::ir::relation::Column;
@@ -119,7 +120,6 @@ impl Nodes {
                 Node64::Parameter(param) => Node::Parameter(param),
                 Node64::Procedure(proc) => Node::Block(Block::Procedure(proc)),
                 Node64::Projection(proj) => Node::Relational(Relational::Projection(proj)),
-                Node64::Reference(reference) => Node::Expression(Expression::Reference(reference)),
                 Node64::ScanCte(scan_cte) => Node::Relational(Relational::ScanCte(scan_cte)),
                 Node64::ScanRelation(scan_rel) => {
                     Node::Relational(Relational::ScanRelation(scan_rel))
@@ -135,6 +135,7 @@ impl Nodes {
                 }
             }),
             ArenaType::Arena96 => self.arena96.get(id.offset as usize).map(|node| match node {
+                Node96::Reference(reference) => Node::Expression(Expression::Reference(reference)),
                 Node96::DropProc(drop_proc) => Node::Ddl(Ddl::DropProc(drop_proc)),
                 Node96::Insert(insert) => Node::Relational(Relational::Insert(insert)),
                 Node96::Invalid(inv) => Node::Invalid(inv),
@@ -238,9 +239,6 @@ impl Nodes {
                     Node64::Projection(proj) => {
                         MutNode::Relational(MutRelational::Projection(proj))
                     }
-                    Node64::Reference(reference) => {
-                        MutNode::Expression(MutExpression::Reference(reference))
-                    }
                     Node64::ScanCte(scan_cte) => {
                         MutNode::Relational(MutRelational::ScanCte(scan_cte))
                     }
@@ -263,6 +261,9 @@ impl Nodes {
                 .arena96
                 .get_mut(id.offset as usize)
                 .map(|node| match node {
+                    Node96::Reference(reference) => {
+                        MutNode::Expression(MutExpression::Reference(reference))
+                    }
                     Node96::DropProc(drop_proc) => MutNode::Ddl(MutDdl::DropProc(drop_proc)),
                     Node96::Insert(insert) => MutNode::Relational(MutRelational::Insert(insert)),
                     Node96::Invalid(inv) => MutNode::Invalid(inv),
@@ -1030,26 +1031,26 @@ impl Plan {
     ///     Selection
     ///         Scan
     /// the source would be the Scan. It could also be a Join or Union nodes.
-    pub fn get_reference_source_relation(&self, ref_id: usize) -> Result<usize, SbroadError> {
+    pub fn get_reference_source_relation(&self, ref_id: NodeId) -> Result<NodeId, SbroadError> {
         let mut ref_id = ref_id;
         let mut ref_node = self.get_expression_node(ref_id)?;
-        if let Expression::Alias { child, .. } = ref_node {
+        if let Expression::Alias(Alias { child, .. }) = ref_node {
             ref_node = self.get_expression_node(*child)?;
             ref_id = *child;
         }
-        let Expression::Reference { position, .. } = ref_node else {
+        let Expression::Reference(Reference { position, .. }) = ref_node else {
             panic!("Expected reference")
         };
-        let ref_parent_node_id = *self.get_relational_from_reference_node(ref_id)?;
+        let ref_parent_node_id = self.get_relational_from_reference_node(ref_id)?;
         let ref_source_node = self.get_relation_node(ref_parent_node_id)?;
         match ref_source_node {
             Relational::Delete { .. } | Relational::Insert { .. } | Relational::Update { .. } => {
                 panic!("Reference source search shouldn't reach DML node.")
             }
-            Relational::Selection { output, .. }
-            | Relational::Having { output, .. }
-            | Relational::OrderBy { output, .. }
-            | Relational::Limit { output, .. } => {
+            Relational::Selection(Selection { output, .. })
+            | Relational::Having(Having { output, .. })
+            | Relational::OrderBy(OrderBy { output, .. })
+            | Relational::Limit(Limit { output, .. }) => {
                 let source_output_list = self.get_row_list(*output)?;
                 let source_ref_id = source_output_list[*position];
                 self.get_reference_source_relation(source_ref_id)
diff --git a/sbroad-core/src/ir/distribution/tests.rs b/sbroad-core/src/ir/distribution/tests.rs
index d29d65065..170ac1c6a 100644
--- a/sbroad-core/src/ir/distribution/tests.rs
+++ b/sbroad-core/src/ir/distribution/tests.rs
@@ -64,9 +64,9 @@ fn projection_any_dist_for_expr() {
 
     // check explain first
     let expected_explain = SmolStr::from(
-        r#"projection (sum(("count_096"::integer))::decimal -> "col_1")
+        r#"projection (sum(("count_696"::integer))::decimal -> "col_1")
     motion [policy: full]
-        projection (count(("test_space"."id"::unsigned))::integer -> "count_096")
+        projection (count(("test_space"."id"::unsigned))::integer -> "count_696")
             scan "test_space"
 execution options:
 sql_vdbe_max_steps = 45000
diff --git a/sbroad-core/src/ir/explain.rs b/sbroad-core/src/ir/explain.rs
index 9e94e18c1..80413f2e1 100644
--- a/sbroad-core/src/ir/explain.rs
+++ b/sbroad-core/src/ir/explain.rs
@@ -199,7 +199,7 @@ impl ColExpr {
                 Expression::Reference(Reference { position, .. }) => {
                     let mut col_name = String::new();
 
-                    let rel_id: NodeId = *plan.get_relational_from_reference_node(id)?;
+                    let rel_id: NodeId = plan.get_relational_from_reference_node(id)?;
 
                     if let Some(name) = plan.scan_name(rel_id, *position)? {
                         col_name.push('"');
@@ -729,7 +729,7 @@ impl Row {
 
             match &current_node {
                 Expression::Reference { .. } => {
-                    let rel_id: NodeId = *plan.get_relational_from_reference_node(expr_id)?;
+                    let rel_id: NodeId = plan.get_relational_from_reference_node(expr_id)?;
 
                     let rel_node = plan.get_relation_node(rel_id)?;
                     if plan.is_additional_child(rel_id)? {
diff --git a/sbroad-core/src/ir/expression.rs b/sbroad-core/src/ir/expression.rs
index 5700fcdba..6bb43457a 100644
--- a/sbroad-core/src/ir/expression.rs
+++ b/sbroad-core/src/ir/expression.rs
@@ -21,6 +21,7 @@ use super::{
 };
 use crate::errors::{Entity, SbroadError};
 use crate::executor::engine::helpers::to_user;
+use crate::ir::node::ReferenceAsteriskSource;
 use crate::ir::operator::Bool;
 use crate::ir::relation::Type;
 use crate::ir::tree::traversal::{PostOrderWithFilter, EXPR_CAPACITY};
@@ -32,25 +33,6 @@ pub mod types;
 
 pub(crate) type ExpressionId = NodeId;
 
-/// Helper structure for cases of references generated from asterisk.
-#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, Hash)]
-pub struct ReferenceAsteriskSource {
-    /// None                -> generated from simple asterisk: `select * from t`
-    /// Some(relation_name) -> generated from table asterisk: `select t.* from t`
-    pub relation_name: Option<SmolStr>,
-    /// Unique asterisk id local for single Projection
-    pub asterisk_id: usize,
-}
-
-impl ReferenceAsteriskSource {
-    pub fn new(relation_name: Option<SmolStr>, asterisk_id: usize) -> Self {
-        Self {
-            relation_name,
-            asterisk_id,
-        }
-    }
-}
-
 #[derive(Clone, Debug, Hash, Deserialize, PartialEq, Eq, Serialize)]
 pub enum FunctionFeature {
     /// Current function is an aggregate function and is marked as DISTINCT.
@@ -597,7 +579,7 @@ impl<'plan> Comparator<'plan> {
                 func_type,
                 feature,
                 is_system: is_aggr,
-            } => {
+            }) => {
                 feature.hash(state);
                 func_type.hash(state);
                 name.hash(state);
@@ -1318,17 +1300,20 @@ impl Plan {
     ///
     /// # Panics
     /// - Plan is in inconsistent state.
-    pub fn get_relational_from_reference_node(&self, ref_id: NodeId) -> Result<&usize, SbroadError> {
+    pub fn get_relational_from_reference_node(
+        &self,
+        ref_id: NodeId,
+    ) -> Result<NodeId, SbroadError> {
         if let Node::Expression(Expression::Reference(Reference {
-                                                          targets, parent, ..
-                                                      })) = self.get_node(ref_id)?
+            targets, parent, ..
+        })) = self.get_node(ref_id)?
         {
             let Some(referred_rel_id) = parent else {
                 panic!("Reference ({ref_id}) parent node not found.");
             };
             let rel = self.get_relation_node(*referred_rel_id)?;
             if let Relational::Insert { .. } = rel {
-                return Ok(referred_rel_id);
+                return Ok(*referred_rel_id);
             }
             let children = self.children(*referred_rel_id);
             match targets {
@@ -1340,7 +1325,7 @@ impl Plan {
                 Some(positions) => match (positions.first(), positions.get(1)) {
                     (Some(first), None) => {
                         if let Some(child_id) = children.get(*first) {
-                            return Ok(child_id);
+                            return Ok(*child_id);
                         }
                         // When we dispatch IR to the storage, we truncate the
                         // subtree below the Motion node. So, the references in
@@ -1348,7 +1333,7 @@ impl Plan {
                         // a special way: we return the Motion node itself. Be
                         // aware of the circular references in the tree!
                         if let Relational::Motion { .. } = rel {
-                            return Ok(referred_rel_id);
+                            return Ok(*referred_rel_id);
                         }
                         return Err(SbroadError::UnexpectedNumberOfValues(format_smolstr!(
                             "Relational node {rel:?} has no children"
diff --git a/sbroad-core/src/ir/helpers.rs b/sbroad-core/src/ir/helpers.rs
index 33557db97..e006e4ba1 100644
--- a/sbroad-core/src/ir/helpers.rs
+++ b/sbroad-core/src/ir/helpers.rs
@@ -144,7 +144,7 @@ impl Plan {
                     col_type,
                     ..
                 }) => {
-                    let alias_name = self.get_alias_from_reference_node(expr).unwrap();
+                    let alias_name = self.get_alias_from_reference_node(&expr).unwrap();
 
                     writeln!(buf, "Reference")?;
                     writeln_with_tabulation(
@@ -156,9 +156,9 @@ impl Plan {
                     // See explain logic for Reference node
                     let rel_id = self.get_relational_from_reference_node(node_id);
                     if let Ok(rel_id) = rel_id {
-                        let rel_node = self.get_relation_node(*rel_id);
+                        let rel_node = self.get_relation_node(rel_id);
                         if rel_node.is_ok() {
-                            if let Ok(Some(name)) = self.scan_name(*rel_id, *position) {
+                            if let Ok(Some(name)) = self.scan_name(rel_id, *position) {
                                 writeln_with_tabulation(
                                     buf,
                                     tabulation_number + 1,
diff --git a/sbroad-core/src/ir/helpers/tests.rs b/sbroad-core/src/ir/helpers/tests.rs
index cbb7b7291..f13bf7a13 100644
--- a/sbroad-core/src/ir/helpers/tests.rs
+++ b/sbroad-core/src/ir/helpers/tests.rs
@@ -14,26 +14,26 @@ fn simple_select() {
     let mut expected_arena = String::new();
     expected_arena.push_str(
         r#"---------------------------------------------
-[id: 664] relation: ScanRelation
+[id: 164] relation: ScanRelation
 	Relation: hash_testing
 	[No children]
-	Output_id: 564
-		[id: 564] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [0, 1] }}) })]
+	Output_id: 064
+		[id: 064] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [0, 1] }}) })]
 			List:
-				[id: 032] expression: Alias [name = identification_number, child = Reference(Reference { parent: Some(NodeId { offset: 6, arena_type: Arena64 }), targets: None, position: 0, col_type: Integer, asterisk_source: None })]
-				[id: 132] expression: Alias [name = product_code, child = Reference(Reference { parent: Some(NodeId { offset: 6, arena_type: Arena64 }), targets: None, position: 1, col_type: String, asterisk_source: None })]
-				[id: 232] expression: Alias [name = product_units, child = Reference(Reference { parent: Some(NodeId { offset: 6, arena_type: Arena64 }), targets: None, position: 2, col_type: Boolean, asterisk_source: None })]
-				[id: 332] expression: Alias [name = sys_op, child = Reference(Reference { parent: Some(NodeId { offset: 6, arena_type: Arena64 }), targets: None, position: 3, col_type: Unsigned, asterisk_source: None })]
-				[id: 432] expression: Alias [name = bucket_id, child = Reference(Reference { parent: Some(NodeId { offset: 6, arena_type: Arena64 }), targets: None, position: 4, col_type: Unsigned, asterisk_source: None })]
+				[id: 032] expression: Alias [name = identification_number, child = Reference(Reference { parent: Some(NodeId { offset: 1, arena_type: Arena64 }), targets: None, position: 0, col_type: Integer, asterisk_source: None })]
+				[id: 132] expression: Alias [name = product_code, child = Reference(Reference { parent: Some(NodeId { offset: 1, arena_type: Arena64 }), targets: None, position: 1, col_type: String, asterisk_source: None })]
+				[id: 232] expression: Alias [name = product_units, child = Reference(Reference { parent: Some(NodeId { offset: 1, arena_type: Arena64 }), targets: None, position: 2, col_type: Boolean, asterisk_source: None })]
+				[id: 332] expression: Alias [name = sys_op, child = Reference(Reference { parent: Some(NodeId { offset: 1, arena_type: Arena64 }), targets: None, position: 3, col_type: Unsigned, asterisk_source: None })]
+				[id: 432] expression: Alias [name = bucket_id, child = Reference(Reference { parent: Some(NodeId { offset: 1, arena_type: Arena64 }), targets: None, position: 4, col_type: Unsigned, asterisk_source: None })]
 ---------------------------------------------
 ---------------------------------------------
-[id: 964] relation: Projection
+[id: 364] relation: Projection
 	Children:
-		Child_id = 664
-	Output_id: 864
-		[id: 864] expression: Row [distribution = Some(Any)]
+		Child_id = 164
+	Output_id: 264
+		[id: 264] expression: Row [distribution = Some(Any)]
 			List:
-				[id: 532] expression: Alias [name = product_code, child = Reference(Reference { parent: Some(NodeId { offset: 9, arena_type: Arena64 }), targets: Some([0]), position: 1, col_type: String, asterisk_source: None })]
+				[id: 532] expression: Alias [name = product_code, child = Reference(Reference { parent: Some(NodeId { offset: 3, arena_type: Arena64 }), targets: Some([0]), position: 1, col_type: String, asterisk_source: None })]
 ---------------------------------------------
 "#);
 
@@ -53,117 +53,117 @@ fn simple_join() {
     let mut expected_arena = String::new();
     expected_arena.push_str(
         r#"---------------------------------------------
-[id: 664] relation: ScanRelation
+[id: 164] relation: ScanRelation
 	Relation: test_space
 	[No children]
-	Output_id: 564
-		[id: 564] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [0] }}) })]
+	Output_id: 064
+		[id: 064] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [0] }}) })]
 			List:
-				[id: 032] expression: Alias [name = id, child = Reference(Reference { parent: Some(NodeId { offset: 6, arena_type: Arena64 }), targets: None, position: 0, col_type: Unsigned, asterisk_source: None })]
-				[id: 132] expression: Alias [name = sysFrom, child = Reference(Reference { parent: Some(NodeId { offset: 6, arena_type: Arena64 }), targets: None, position: 1, col_type: Unsigned, asterisk_source: None })]
-				[id: 232] expression: Alias [name = FIRST_NAME, child = Reference(Reference { parent: Some(NodeId { offset: 6, arena_type: Arena64 }), targets: None, position: 2, col_type: String, asterisk_source: None })]
-				[id: 332] expression: Alias [name = sys_op, child = Reference(Reference { parent: Some(NodeId { offset: 6, arena_type: Arena64 }), targets: None, position: 3, col_type: Unsigned, asterisk_source: None })]
-				[id: 432] expression: Alias [name = bucket_id, child = Reference(Reference { parent: Some(NodeId { offset: 6, arena_type: Arena64 }), targets: None, position: 4, col_type: Unsigned, asterisk_source: None })]
+				[id: 032] expression: Alias [name = id, child = Reference(Reference { parent: Some(NodeId { offset: 1, arena_type: Arena64 }), targets: None, position: 0, col_type: Unsigned, asterisk_source: None })]
+				[id: 132] expression: Alias [name = sysFrom, child = Reference(Reference { parent: Some(NodeId { offset: 1, arena_type: Arena64 }), targets: None, position: 1, col_type: Unsigned, asterisk_source: None })]
+				[id: 232] expression: Alias [name = FIRST_NAME, child = Reference(Reference { parent: Some(NodeId { offset: 1, arena_type: Arena64 }), targets: None, position: 2, col_type: String, asterisk_source: None })]
+				[id: 332] expression: Alias [name = sys_op, child = Reference(Reference { parent: Some(NodeId { offset: 1, arena_type: Arena64 }), targets: None, position: 3, col_type: Unsigned, asterisk_source: None })]
+				[id: 432] expression: Alias [name = bucket_id, child = Reference(Reference { parent: Some(NodeId { offset: 1, arena_type: Arena64 }), targets: None, position: 4, col_type: Unsigned, asterisk_source: None })]
 ---------------------------------------------
 ---------------------------------------------
-[id: 964] relation: Projection
+[id: 364] relation: Projection
 	Children:
-		Child_id = 664
-	Output_id: 864
-		[id: 864] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [0] }}) })]
+		Child_id = 164
+	Output_id: 264
+		[id: 264] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [0] }}) })]
 			List:
-				[id: 532] expression: Alias [name = id, child = Reference(Reference { parent: Some(NodeId { offset: 9, arena_type: Arena64 }), targets: Some([0]), position: 0, col_type: Unsigned, asterisk_source: None })]
+				[id: 532] expression: Alias [name = id, child = Reference(Reference { parent: Some(NodeId { offset: 3, arena_type: Arena64 }), targets: Some([0]), position: 0, col_type: Unsigned, asterisk_source: None })]
 ---------------------------------------------
 ---------------------------------------------
-[id: 1264] relation: ScanSubQuery
+[id: 564] relation: ScanSubQuery
 	Alias: t1
 	Children:
-		Child_id = 964
-	Output_id: 1164
-		[id: 1164] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [0] }}) })]
+		Child_id = 364
+	Output_id: 464
+		[id: 464] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [0] }}) })]
 			List:
-				[id: 632] expression: Alias [name = id, child = Reference(Reference { parent: Some(NodeId { offset: 12, arena_type: Arena64 }), targets: Some([0]), position: 0, col_type: Unsigned, asterisk_source: None })]
+				[id: 632] expression: Alias [name = id, child = Reference(Reference { parent: Some(NodeId { offset: 5, arena_type: Arena64 }), targets: Some([0]), position: 0, col_type: Unsigned, asterisk_source: None })]
 ---------------------------------------------
 ---------------------------------------------
-[id: 1964] relation: ScanRelation
+[id: 764] relation: ScanRelation
 	Relation: hash_testing
 	[No children]
-	Output_id: 1864
-		[id: 1864] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [0, 1] }}) })]
+	Output_id: 664
+		[id: 664] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [0, 1] }}) })]
 			List:
-				[id: 732] expression: Alias [name = identification_number, child = Reference(Reference { parent: Some(NodeId { offset: 19, arena_type: Arena64 }), targets: None, position: 0, col_type: Integer, asterisk_source: None })]
-				[id: 832] expression: Alias [name = product_code, child = Reference(Reference { parent: Some(NodeId { offset: 19, arena_type: Arena64 }), targets: None, position: 1, col_type: String, asterisk_source: None })]
-				[id: 932] expression: Alias [name = product_units, child = Reference(Reference { parent: Some(NodeId { offset: 19, arena_type: Arena64 }), targets: None, position: 2, col_type: Boolean, asterisk_source: None })]
-				[id: 1032] expression: Alias [name = sys_op, child = Reference(Reference { parent: Some(NodeId { offset: 19, arena_type: Arena64 }), targets: None, position: 3, col_type: Unsigned, asterisk_source: None })]
-				[id: 1132] expression: Alias [name = bucket_id, child = Reference(Reference { parent: Some(NodeId { offset: 19, arena_type: Arena64 }), targets: None, position: 4, col_type: Unsigned, asterisk_source: None })]
+				[id: 732] expression: Alias [name = identification_number, child = Reference(Reference { parent: Some(NodeId { offset: 7, arena_type: Arena64 }), targets: None, position: 0, col_type: Integer, asterisk_source: None })]
+				[id: 832] expression: Alias [name = product_code, child = Reference(Reference { parent: Some(NodeId { offset: 7, arena_type: Arena64 }), targets: None, position: 1, col_type: String, asterisk_source: None })]
+				[id: 932] expression: Alias [name = product_units, child = Reference(Reference { parent: Some(NodeId { offset: 7, arena_type: Arena64 }), targets: None, position: 2, col_type: Boolean, asterisk_source: None })]
+				[id: 1032] expression: Alias [name = sys_op, child = Reference(Reference { parent: Some(NodeId { offset: 7, arena_type: Arena64 }), targets: None, position: 3, col_type: Unsigned, asterisk_source: None })]
+				[id: 1132] expression: Alias [name = bucket_id, child = Reference(Reference { parent: Some(NodeId { offset: 7, arena_type: Arena64 }), targets: None, position: 4, col_type: Unsigned, asterisk_source: None })]
 ---------------------------------------------
 ---------------------------------------------
-[id: 2264] relation: Projection
+[id: 964] relation: Projection
 	Children:
-		Child_id = 1964
-	Output_id: 2164
-		[id: 2164] expression: Row [distribution = Some(Any)]
+		Child_id = 764
+	Output_id: 864
+		[id: 864] expression: Row [distribution = Some(Any)]
 			List:
-				[id: 1232] expression: Alias [name = identification_number, child = Reference(Reference { parent: Some(NodeId { offset: 22, arena_type: Arena64 }), targets: Some([0]), position: 0, col_type: Integer, asterisk_source: None })]
+				[id: 1232] expression: Alias [name = identification_number, child = Reference(Reference { parent: Some(NodeId { offset: 9, arena_type: Arena64 }), targets: Some([0]), position: 0, col_type: Integer, asterisk_source: None })]
 ---------------------------------------------
 ---------------------------------------------
-[id: 2564] relation: ScanSubQuery
+[id: 1164] relation: ScanSubQuery
 	Alias: t2
 	Children:
-		Child_id = 2264
-	Output_id: 2464
-		[id: 2464] expression: Row [distribution = Some(Any)]
+		Child_id = 964
+	Output_id: 1064
+		[id: 1064] expression: Row [distribution = Some(Any)]
 			List:
-				[id: 1332] expression: Alias [name = identification_number, child = Reference(Reference { parent: Some(NodeId { offset: 25, arena_type: Arena64 }), targets: Some([0]), position: 0, col_type: Integer, asterisk_source: None })]
+				[id: 1332] expression: Alias [name = identification_number, child = Reference(Reference { parent: Some(NodeId { offset: 11, arena_type: Arena64 }), targets: Some([0]), position: 0, col_type: Integer, asterisk_source: None })]
 ---------------------------------------------
 ---------------------------------------------
 [id: 0136] relation: Motion [policy = Segment(MotionKey { targets: [Reference(0)] }), alias = t2]
 	Children:
-		Child_id = 2564
-	Output_id: 4064
-		[id: 4064] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [0] }}) })]
+		Child_id = 1164
+	Output_id: 2064
+		[id: 2064] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [0] }}) })]
 			List:
 				[id: 1932] expression: Alias [name = identification_number, child = Reference(Reference { parent: Some(NodeId { offset: 0, arena_type: Arena136 }), targets: Some([0]), position: 0, col_type: Integer, asterisk_source: None })]
 ---------------------------------------------
 ---------------------------------------------
-[id: 3364] relation: InnerJoin
+[id: 1564] relation: InnerJoin
 	Condition:
 		[id: 1832] expression: Bool [op: =]
 			Left child
-			[id: 3764] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [0] }}) })]
+			[id: 1864] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [0] }}) })]
 				List:
-					[id: 2664] expression: Reference
+					[id: 1496] expression: Reference
 						Alias: id
 						Referenced table name (or alias): t1
-						Parent: Some(NodeId { offset: 33, arena_type: Arena64 })
+						Parent: Some(NodeId { offset: 15, arena_type: Arena64 })
 						target_id: 0
 						Column type: unsigned
 			Right child
-			[id: 3864] expression: Row [distribution = Some(Any)]
+			[id: 1964] expression: Row [distribution = Some(Any)]
 				List:
-					[id: 2864] expression: Reference
+					[id: 1596] expression: Reference
 						Alias: identification_number
 						Referenced table name (or alias): t2
-						Parent: Some(NodeId { offset: 33, arena_type: Arena64 })
+						Parent: Some(NodeId { offset: 15, arena_type: Arena64 })
 						target_id: 1
 						Column type: integer
 	Children:
-		Child_id = 1264
+		Child_id = 564
 		Child_id = 0136
-	Output_id: 3264
-		[id: 3264] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [1] }, Key { positions: [0] }}) })]
+	Output_id: 1464
+		[id: 1464] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [1] }, Key { positions: [0] }}) })]
 			List:
-				[id: 1532] expression: Alias [name = id, child = Reference(Reference { parent: Some(NodeId { offset: 33, arena_type: Arena64 }), targets: Some([0]), position: 0, col_type: Unsigned, asterisk_source: None })]
-				[id: 1632] expression: Alias [name = identification_number, child = Reference(Reference { parent: Some(NodeId { offset: 33, arena_type: Arena64 }), targets: Some([1]), position: 0, col_type: Integer, asterisk_source: None })]
+				[id: 1532] expression: Alias [name = id, child = Reference(Reference { parent: Some(NodeId { offset: 15, arena_type: Arena64 }), targets: Some([0]), position: 0, col_type: Unsigned, asterisk_source: None })]
+				[id: 1632] expression: Alias [name = identification_number, child = Reference(Reference { parent: Some(NodeId { offset: 15, arena_type: Arena64 }), targets: Some([1]), position: 0, col_type: Integer, asterisk_source: None })]
 ---------------------------------------------
 ---------------------------------------------
-[id: 3664] relation: Projection
+[id: 1764] relation: Projection
 	Children:
-		Child_id = 3364
-	Output_id: 3564
-		[id: 3564] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [0] }}) })]
+		Child_id = 1564
+	Output_id: 1664
+		[id: 1664] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [0] }}) })]
 			List:
-				[id: 1732] expression: Alias [name = id, child = Reference(Reference { parent: Some(NodeId { offset: 36, arena_type: Arena64 }), targets: Some([0]), position: 0, col_type: Unsigned, asterisk_source: None })]
+				[id: 1732] expression: Alias [name = id, child = Reference(Reference { parent: Some(NodeId { offset: 17, arena_type: Arena64 }), targets: Some([0]), position: 0, col_type: Unsigned, asterisk_source: None })]
 ---------------------------------------------
 "#);
 
@@ -191,43 +191,43 @@ fn simple_join_subtree() {
     let mut expected_arena_subtree = String::new();
     expected_arena_subtree.push_str(
         r#"---------------------------------------------
-[id: 1964] relation: ScanRelation
+[id: 764] relation: ScanRelation
 	Relation: hash_testing
 	[No children]
-	Output_id: 1864
-		[id: 1864] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [0, 1] }}) })]
+	Output_id: 664
+		[id: 664] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [0, 1] }}) })]
 			List:
-				[id: 732] expression: Alias [name = identification_number, child = Reference(Reference { parent: Some(NodeId { offset: 19, arena_type: Arena64 }), targets: None, position: 0, col_type: Integer, asterisk_source: None })]
-				[id: 832] expression: Alias [name = product_code, child = Reference(Reference { parent: Some(NodeId { offset: 19, arena_type: Arena64 }), targets: None, position: 1, col_type: String, asterisk_source: None })]
-				[id: 932] expression: Alias [name = product_units, child = Reference(Reference { parent: Some(NodeId { offset: 19, arena_type: Arena64 }), targets: None, position: 2, col_type: Boolean, asterisk_source: None })]
-				[id: 1032] expression: Alias [name = sys_op, child = Reference(Reference { parent: Some(NodeId { offset: 19, arena_type: Arena64 }), targets: None, position: 3, col_type: Unsigned, asterisk_source: None })]
-				[id: 1132] expression: Alias [name = bucket_id, child = Reference(Reference { parent: Some(NodeId { offset: 19, arena_type: Arena64 }), targets: None, position: 4, col_type: Unsigned, asterisk_source: None })]
+				[id: 732] expression: Alias [name = identification_number, child = Reference(Reference { parent: Some(NodeId { offset: 7, arena_type: Arena64 }), targets: None, position: 0, col_type: Integer, asterisk_source: None })]
+				[id: 832] expression: Alias [name = product_code, child = Reference(Reference { parent: Some(NodeId { offset: 7, arena_type: Arena64 }), targets: None, position: 1, col_type: String, asterisk_source: None })]
+				[id: 932] expression: Alias [name = product_units, child = Reference(Reference { parent: Some(NodeId { offset: 7, arena_type: Arena64 }), targets: None, position: 2, col_type: Boolean, asterisk_source: None })]
+				[id: 1032] expression: Alias [name = sys_op, child = Reference(Reference { parent: Some(NodeId { offset: 7, arena_type: Arena64 }), targets: None, position: 3, col_type: Unsigned, asterisk_source: None })]
+				[id: 1132] expression: Alias [name = bucket_id, child = Reference(Reference { parent: Some(NodeId { offset: 7, arena_type: Arena64 }), targets: None, position: 4, col_type: Unsigned, asterisk_source: None })]
 ---------------------------------------------
 ---------------------------------------------
-[id: 2264] relation: Projection
+[id: 964] relation: Projection
 	Children:
-		Child_id = 1964
-	Output_id: 2164
-		[id: 2164] expression: Row [distribution = Some(Any)]
+		Child_id = 764
+	Output_id: 864
+		[id: 864] expression: Row [distribution = Some(Any)]
 			List:
-				[id: 1232] expression: Alias [name = identification_number, child = Reference(Reference { parent: Some(NodeId { offset: 22, arena_type: Arena64 }), targets: Some([0]), position: 0, col_type: Integer, asterisk_source: None })]
+				[id: 1232] expression: Alias [name = identification_number, child = Reference(Reference { parent: Some(NodeId { offset: 9, arena_type: Arena64 }), targets: Some([0]), position: 0, col_type: Integer, asterisk_source: None })]
 ---------------------------------------------
 ---------------------------------------------
-[id: 2564] relation: ScanSubQuery
+[id: 1164] relation: ScanSubQuery
 	Alias: t2
 	Children:
-		Child_id = 2264
-	Output_id: 2464
-		[id: 2464] expression: Row [distribution = Some(Any)]
+		Child_id = 964
+	Output_id: 1064
+		[id: 1064] expression: Row [distribution = Some(Any)]
 			List:
-				[id: 1332] expression: Alias [name = identification_number, child = Reference(Reference { parent: Some(NodeId { offset: 25, arena_type: Arena64 }), targets: Some([0]), position: 0, col_type: Integer, asterisk_source: None })]
+				[id: 1332] expression: Alias [name = identification_number, child = Reference(Reference { parent: Some(NodeId { offset: 11, arena_type: Arena64 }), targets: Some([0]), position: 0, col_type: Integer, asterisk_source: None })]
 ---------------------------------------------
 ---------------------------------------------
 [id: 0136] relation: Motion [policy = Segment(MotionKey { targets: [Reference(0)] }), alias = t2]
 	Children:
-		Child_id = 2564
-	Output_id: 4064
-		[id: 4064] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [0] }}) })]
+		Child_id = 1164
+	Output_id: 2064
+		[id: 2064] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [0] }}) })]
 			List:
 				[id: 1932] expression: Alias [name = identification_number, child = Reference(Reference { parent: Some(NodeId { offset: 0, arena_type: Arena136 }), targets: Some([0]), position: 0, col_type: Integer, asterisk_source: None })]
 ---------------------------------------------
@@ -246,70 +246,70 @@ fn simple_aggregation_with_group_by() {
     let mut expected_arena = String::new();
     expected_arena.push_str(
         r#"---------------------------------------------
-[id: 664] relation: ScanRelation
+[id: 164] relation: ScanRelation
 	Relation: hash_testing
 	[No children]
-	Output_id: 564
-		[id: 564] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [0, 1] }}) })]
+	Output_id: 064
+		[id: 064] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [0, 1] }}) })]
 			List:
-				[id: 032] expression: Alias [name = identification_number, child = Reference(Reference { parent: Some(NodeId { offset: 6, arena_type: Arena64 }), targets: None, position: 0, col_type: Integer, asterisk_source: None })]
-				[id: 132] expression: Alias [name = product_code, child = Reference(Reference { parent: Some(NodeId { offset: 6, arena_type: Arena64 }), targets: None, position: 1, col_type: String, asterisk_source: None })]
-				[id: 232] expression: Alias [name = product_units, child = Reference(Reference { parent: Some(NodeId { offset: 6, arena_type: Arena64 }), targets: None, position: 2, col_type: Boolean, asterisk_source: None })]
-				[id: 332] expression: Alias [name = sys_op, child = Reference(Reference { parent: Some(NodeId { offset: 6, arena_type: Arena64 }), targets: None, position: 3, col_type: Unsigned, asterisk_source: None })]
-				[id: 432] expression: Alias [name = bucket_id, child = Reference(Reference { parent: Some(NodeId { offset: 6, arena_type: Arena64 }), targets: None, position: 4, col_type: Unsigned, asterisk_source: None })]
+				[id: 032] expression: Alias [name = identification_number, child = Reference(Reference { parent: Some(NodeId { offset: 1, arena_type: Arena64 }), targets: None, position: 0, col_type: Integer, asterisk_source: None })]
+				[id: 132] expression: Alias [name = product_code, child = Reference(Reference { parent: Some(NodeId { offset: 1, arena_type: Arena64 }), targets: None, position: 1, col_type: String, asterisk_source: None })]
+				[id: 232] expression: Alias [name = product_units, child = Reference(Reference { parent: Some(NodeId { offset: 1, arena_type: Arena64 }), targets: None, position: 2, col_type: Boolean, asterisk_source: None })]
+				[id: 332] expression: Alias [name = sys_op, child = Reference(Reference { parent: Some(NodeId { offset: 1, arena_type: Arena64 }), targets: None, position: 3, col_type: Unsigned, asterisk_source: None })]
+				[id: 432] expression: Alias [name = bucket_id, child = Reference(Reference { parent: Some(NodeId { offset: 1, arena_type: Arena64 }), targets: None, position: 4, col_type: Unsigned, asterisk_source: None })]
 ---------------------------------------------
 ---------------------------------------------
-[id: 1464] relation: GroupBy [is_final = false]
+[id: 364] relation: GroupBy [is_final = false]
 	Gr_cols:
-		Gr_col: Reference(Reference { parent: Some(NodeId { offset: 14, arena_type: Arena64 }), targets: Some([0]), position: 1, col_type: String, asterisk_source: None })
+		Gr_col: Reference(Reference { parent: Some(NodeId { offset: 3, arena_type: Arena64 }), targets: Some([0]), position: 1, col_type: String, asterisk_source: None })
 	Children:
-		Child_id = 664
-	Output_id: 1364
-		[id: 1364] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [0, 1] }}) })]
+		Child_id = 164
+	Output_id: 264
+		[id: 264] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [0, 1] }}) })]
 			List:
-				[id: 532] expression: Alias [name = identification_number, child = Reference(Reference { parent: Some(NodeId { offset: 14, arena_type: Arena64 }), targets: Some([0]), position: 0, col_type: Integer, asterisk_source: None })]
-				[id: 632] expression: Alias [name = product_code, child = Reference(Reference { parent: Some(NodeId { offset: 14, arena_type: Arena64 }), targets: Some([0]), position: 1, col_type: String, asterisk_source: None })]
-				[id: 732] expression: Alias [name = product_units, child = Reference(Reference { parent: Some(NodeId { offset: 14, arena_type: Arena64 }), targets: Some([0]), position: 2, col_type: Boolean, asterisk_source: None })]
-				[id: 832] expression: Alias [name = sys_op, child = Reference(Reference { parent: Some(NodeId { offset: 14, arena_type: Arena64 }), targets: Some([0]), position: 3, col_type: Unsigned, asterisk_source: None })]
-				[id: 932] expression: Alias [name = bucket_id, child = Reference(Reference { parent: Some(NodeId { offset: 14, arena_type: Arena64 }), targets: Some([0]), position: 4, col_type: Unsigned, asterisk_source: None })]
+				[id: 532] expression: Alias [name = identification_number, child = Reference(Reference { parent: Some(NodeId { offset: 3, arena_type: Arena64 }), targets: Some([0]), position: 0, col_type: Integer, asterisk_source: None })]
+				[id: 632] expression: Alias [name = product_code, child = Reference(Reference { parent: Some(NodeId { offset: 3, arena_type: Arena64 }), targets: Some([0]), position: 1, col_type: String, asterisk_source: None })]
+				[id: 732] expression: Alias [name = product_units, child = Reference(Reference { parent: Some(NodeId { offset: 3, arena_type: Arena64 }), targets: Some([0]), position: 2, col_type: Boolean, asterisk_source: None })]
+				[id: 832] expression: Alias [name = sys_op, child = Reference(Reference { parent: Some(NodeId { offset: 3, arena_type: Arena64 }), targets: Some([0]), position: 3, col_type: Unsigned, asterisk_source: None })]
+				[id: 932] expression: Alias [name = bucket_id, child = Reference(Reference { parent: Some(NodeId { offset: 3, arena_type: Arena64 }), targets: Some([0]), position: 4, col_type: Unsigned, asterisk_source: None })]
 ---------------------------------------------
 ---------------------------------------------
-[id: 1964] relation: Projection
+[id: 764] relation: Projection
 	Children:
-		Child_id = 1464
-	Output_id: 1864
-		[id: 1864] expression: Row [distribution = Some(Any)]
+		Child_id = 364
+	Output_id: 664
+		[id: 664] expression: Row [distribution = Some(Any)]
 			List:
-				[id: 1132] expression: Alias [name = column_764, child = Reference(Reference { parent: Some(NodeId { offset: 14, arena_type: Arena64 }), targets: Some([0]), position: 1, col_type: String, asterisk_source: None })]
+				[id: 1132] expression: Alias [name = column_596, child = Reference(Reference { parent: Some(NodeId { offset: 3, arena_type: Arena64 }), targets: Some([0]), position: 1, col_type: String, asterisk_source: None })]
 ---------------------------------------------
 ---------------------------------------------
-[id: 41] relation: Motion [policy = Segment(MotionKey { targets: [Reference(0)] }), alias = None]
+[id: 0136] relation: Motion [policy = Segment(MotionKey { targets: [Reference(0)] }), alias = None]
 	Children:
-		Child_id = 31
-	Output_id: 40
-		[id: 40] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [0] }}) })]
+		Child_id = 764
+	Output_id: 1064
+		[id: 1064] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [0] }}) })]
 			List:
-				[id: 39] expression: Alias [name = column_12, child = Reference { parent: Some(41), targets: Some([0]), position: 0, col_type: String, asterisk_source: None }]
+				[id: 1332] expression: Alias [name = column_596, child = Reference(Reference { parent: Some(NodeId { offset: 0, arena_type: Arena136 }), targets: Some([0]), position: 0, col_type: String, asterisk_source: None })]
 ---------------------------------------------
 ---------------------------------------------
-[id: 2664] relation: GroupBy [is_final = true]
+[id: 964] relation: GroupBy [is_final = true]
 	Gr_cols:
-		Gr_col: Reference(Reference { parent: Some(NodeId { offset: 26, arena_type: Arena64 }), targets: Some([0]), position: 0, col_type: String, asterisk_source: None })
+		Gr_col: Reference(Reference { parent: Some(NodeId { offset: 9, arena_type: Arena64 }), targets: Some([0]), position: 0, col_type: String, asterisk_source: None })
 	Children:
 		Child_id = 0136
-	Output_id: 2564
-		[id: 2564] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [0] }}) })]
+	Output_id: 864
+		[id: 864] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [0] }}) })]
 			List:
-				[id: 1332] expression: Alias [name = column_764, child = Reference(Reference { parent: Some(NodeId { offset: 26, arena_type: Arena64 }), targets: Some([0]), position: 0, col_type: String, asterisk_source: None })]
+				[id: 1232] expression: Alias [name = column_596, child = Reference(Reference { parent: Some(NodeId { offset: 9, arena_type: Arena64 }), targets: Some([0]), position: 0, col_type: String, asterisk_source: None })]
 ---------------------------------------------
 ---------------------------------------------
-[id: 1764] relation: Projection
+[id: 564] relation: Projection
 	Children:
-		Child_id = 2664
-	Output_id: 1664
-		[id: 1664] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [0] }}) })]
+		Child_id = 964
+	Output_id: 464
+		[id: 464] expression: Row [distribution = Some(Segment { keys: KeySet({Key { positions: [0] }}) })]
 			List:
-				[id: 1032] expression: Alias [name = product_code, child = Reference(Reference { parent: Some(NodeId { offset: 17, arena_type: Arena64 }), targets: Some([0]), position: 0, col_type: String, asterisk_source: None })]
+				[id: 1032] expression: Alias [name = product_code, child = Reference(Reference { parent: Some(NodeId { offset: 5, arena_type: Arena64 }), targets: Some([0]), position: 0, col_type: String, asterisk_source: None })]
 ---------------------------------------------
 "#);
 
diff --git a/sbroad-core/src/ir/node.rs b/sbroad-core/src/ir/node.rs
index 4d8dcf638..bcd47df43 100644
--- a/sbroad-core/src/ir/node.rs
+++ b/sbroad-core/src/ir/node.rs
@@ -194,6 +194,25 @@ impl From<Constant> for NodeAligned {
     }
 }
 
+/// Helper structure for cases of references generated from asterisk.
+#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, Hash)]
+pub struct ReferenceAsteriskSource {
+    /// None                -> generated from simple asterisk: `select * from t`
+    /// Some(relation_name) -> generated from table asterisk: `select t.* from t`
+    pub relation_name: Option<SmolStr>,
+    /// Unique asterisk id local for single Projection
+    pub asterisk_id: usize,
+}
+
+impl ReferenceAsteriskSource {
+    pub fn new(relation_name: Option<SmolStr>, asterisk_id: usize) -> Self {
+        Self {
+            relation_name,
+            asterisk_id,
+        }
+    }
+}
+
 /// Reference to the position in the incoming tuple(s).
 /// Uses a relative pointer as a coordinate system:
 /// - relational node (containing this reference)
@@ -212,11 +231,14 @@ pub struct Reference {
     pub position: usize,
     /// Referred column type in the input tuple.
     pub col_type: Type,
+    /// Field indicating whether this reference resulted
+    /// from an asterisk "*" under projection.
+    pub asterisk_source: Option<ReferenceAsteriskSource>,
 }
 
 impl From<Reference> for NodeAligned {
     fn from(value: Reference) -> Self {
-        Self::Node64(Node64::Reference(value))
+        Self::Node96(Node96::Reference(value))
     }
 }
 
@@ -511,12 +533,6 @@ pub struct Motion {
     pub program: Program,
     /// Outputs tuple node index in the plan node arena.
     pub output: NodeId,
-    /// A helper field indicating whether first element of
-    /// `children` vec is a `Relational::SubQuery`.
-    /// We need it on the stage of translating Plan to SQL, because
-    /// by that moment we've already erased `children` information using
-    /// `unlink_motion_subtree` function.
-    pub is_child_subquery: bool,
 }
 
 impl From<Motion> for NodeAligned {
@@ -1031,8 +1047,7 @@ pub enum Node64 {
     OrderBy(OrderBy),
     Procedure(Procedure),
     Join(Join),
-    Row(Row), // move to Node64
-    Reference(Reference),
+    Row(Row),
     Delete(Delete),
     ScanRelation(ScanRelation),
     ScanSubQuery(ScanSubQuery),
@@ -1066,7 +1081,6 @@ impl Node64 {
             Node64::Row(row) => NodeOwned::Expression(ExprOwned::Row(row)),
             Node64::Procedure(proc) => NodeOwned::Block(BlockOwned::Procedure(proc)),
             Node64::Projection(proj) => NodeOwned::Relational(RelOwned::Projection(proj)),
-            Node64::Reference(reference) => NodeOwned::Expression(ExprOwned::Reference(reference)),
             Node64::ScanCte(scan_cte) => NodeOwned::Relational(RelOwned::ScanCte(scan_cte)),
             Node64::ScanRelation(scan_rel) => {
                 NodeOwned::Relational(RelOwned::ScanRelation(scan_rel))
@@ -1087,6 +1101,7 @@ impl Node64 {
 #[allow(clippy::module_name_repetitions)]
 #[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
 pub enum Node96 {
+    Reference(Reference),
     Invalid(Invalid),
     StableFunction(StableFunction),
     DropProc(DropProc),
@@ -1097,6 +1112,7 @@ impl Node96 {
     #[must_use]
     pub fn into_owned(self) -> NodeOwned {
         match self {
+            Node96::Reference(reference) => NodeOwned::Expression(ExprOwned::Reference(reference)),
             Node96::DropProc(drop_proc) => NodeOwned::Ddl(DdlOwned::DropProc(drop_proc)),
             Node96::Insert(insert) => NodeOwned::Relational(RelOwned::Insert(insert)),
             Node96::Invalid(inv) => NodeOwned::Invalid(inv),
diff --git a/sbroad-core/src/ir/operator.rs b/sbroad-core/src/ir/operator.rs
index 3e7e23d4f..e4477d3b4 100644
--- a/sbroad-core/src/ir/operator.rs
+++ b/sbroad-core/src/ir/operator.rs
@@ -916,10 +916,9 @@ impl Plan {
     ) -> Result<NodeId, SbroadError> {
         let child_rel_node = self.get_relation_node(child_id)?;
         let alias = match child_rel_node {
-            Relational::ScanSubQuery { alias, .. } | Relational::ScanRelation { alias, .. } => {
-                alias.clone()
-            }
-            Relational::ScanCte { alias, .. } => Some(alias.clone()),
+            Relational::ScanSubQuery(ScanSubQuery { alias, .. })
+            | Relational::ScanRelation(ScanRelation { alias, .. }) => alias.clone(),
+            Relational::ScanCte(ScanCte { alias, .. }) => Some(alias.clone()),
             _ => None,
         };
 
@@ -1722,7 +1721,7 @@ impl Plan {
                 if let Expression::Alias(Alias { child, .. }) = col_node {
                     let child_node = self.get_expression_node(*child)?;
                     if let Expression::Reference(Reference { position: pos, .. }) = child_node {
-                        let rel_id = *self.get_relational_from_reference_node(*child)?;
+                        let rel_id = self.get_relational_from_reference_node(*child)?;
                         let rel_node = self.get_relation_node(rel_id)?;
                         if rel_node == node {
                             return Err(SbroadError::DuplicatedValue(format_smolstr!(
diff --git a/sbroad-core/src/ir/operator/tests.rs b/sbroad-core/src/ir/operator/tests.rs
index f3a2f6859..48bade8fa 100644
--- a/sbroad-core/src/ir/operator/tests.rs
+++ b/sbroad-core/src/ir/operator/tests.rs
@@ -31,11 +31,11 @@ fn scan_rel() {
     plan.add_rel(t);
 
     let scan_output = NodeId {
-        offset: 4,
+        offset: 0,
         arena_type: ArenaType::Arena64,
     };
     let scan_node = NodeId {
-        offset: 5,
+        offset: 1,
         arena_type: ArenaType::Arena64,
     };
 
@@ -94,7 +94,7 @@ fn projection() {
         SbroadError::Invalid(
             Entity::Node,
             Some(
-                "node is not Relational type: Expression(Alias(Alias { name: \"a\", child: NodeId { offset: 0, arena_type: Arena64 } }))".into()
+                "node is not Relational type: Expression(Alias(Alias { name: \"a\", child: NodeId { offset: 0, arena_type: Arena96 } }))".into()
             )
         ),
         plan.add_proj(test_node, &["a"], false, false).unwrap_err()
@@ -374,7 +374,7 @@ fn sub_query() {
     assert_eq!(
         SbroadError::Invalid(
             Entity::Node,
-            Some("node is not Relational type: Expression(Alias(Alias { name: \"a\", child: NodeId { offset: 0, arena_type: Arena64 } }))".into())
+            Some("node is not Relational type: Expression(Alias(Alias { name: \"a\", child: NodeId { offset: 0, arena_type: Arena96 } }))".into())
         ),
         plan.add_sub_query(a, Some("sq")).unwrap_err()
     );
diff --git a/sbroad-core/src/ir/transformation/equality_propagation.rs b/sbroad-core/src/ir/transformation/equality_propagation.rs
index ea6aa2bba..5f1f68d1a 100644
--- a/sbroad-core/src/ir/transformation/equality_propagation.rs
+++ b/sbroad-core/src/ir/transformation/equality_propagation.rs
@@ -91,8 +91,8 @@
 
 use crate::errors::{Entity, SbroadError};
 use crate::ir::helpers::RepeatableState;
-use crate::ir::node::expression::{Expression, ReferenceAsteriskSource};
-use crate::ir::node::{Constant, NodeId, Reference, Row};
+use crate::ir::node::expression::Expression;
+use crate::ir::node::{Constant, NodeId, Reference, ReferenceAsteriskSource, Row};
 use crate::ir::operator::Bool;
 use crate::ir::relation::Type;
 use crate::ir::transformation::merge_tuples::Chain;
diff --git a/sbroad-core/src/ir/transformation/equality_propagation/tests.rs b/sbroad-core/src/ir/transformation/equality_propagation/tests.rs
index f8c6cf6f5..1f18868f8 100644
--- a/sbroad-core/src/ir/transformation/equality_propagation/tests.rs
+++ b/sbroad-core/src/ir/transformation/equality_propagation/tests.rs
@@ -80,7 +80,7 @@ fn equality_propagation4() {
             "{} {} {}",
             r#"SELECT "t"."a" FROM "t""#,
             r#"WHERE ("t"."b") = (?) and ("t"."a") = (?) and ("t"."a") = (?)"#,
-            r#"and ("t"."b") = (?) and ("t"."b") = ("t"."a")"#,
+            r#"and ("t"."b") = (?) and ("t"."a") = ("t"."b")"#,
         ),
         vec![
             Value::from(1_u64),
@@ -107,8 +107,8 @@ fn equality_propagation5() {
             r#"SELECT "t"."a" FROM "t""#,
             r#"WHERE ("t"."d") = (?) and ("t"."c") = (?)"#,
             r#"and ("t"."a") = (?) and ("t"."b") = (?)"#,
-            r#"and ("t"."b") = ("t"."c") and ("t"."c") = ("t"."a")"#,
-            r#"and ("t"."a") = ("t"."d")"#,
+            r#"and ("t"."a") = ("t"."d") and ("t"."d") = ("t"."c")"#,
+            r#"and ("t"."c") = ("t"."b")"#,
         ),
         vec![
             Value::from(1_u64),
diff --git a/sbroad-core/src/ir/transformation/redistribution/eq_cols.rs b/sbroad-core/src/ir/transformation/redistribution/eq_cols.rs
index bc532cfe0..640c5dd31 100644
--- a/sbroad-core/src/ir/transformation/redistribution/eq_cols.rs
+++ b/sbroad-core/src/ir/transformation/redistribution/eq_cols.rs
@@ -302,7 +302,7 @@ impl EqualityCols {
                         col_type: col_type_left,
                         asterisk_source: asterisk_source_left,
                     }),
-                    Expression::Reference {
+                    Expression::Reference(Reference {
                         targets: targets_right,
                         position: pos_right,
                         parent: parent_right,
@@ -320,7 +320,7 @@ impl EqualityCols {
                         && asterisk_source_left == asterisk_source_right
                     {
                         let left_referred_child_id =
-                            *plan.get_relational_from_reference_node(*left_id)?;
+                            plan.get_relational_from_reference_node(*left_id)?;
                         let (inner_pos, outer_pos) = if left_referred_child_id == inner_id {
                             (*pos_left, *pos_right)
                         } else {
diff --git a/sbroad-core/src/ir/transformation/redistribution/groupby.rs b/sbroad-core/src/ir/transformation/redistribution/groupby.rs
index 37f04a3ec..829554c7c 100644
--- a/sbroad-core/src/ir/transformation/redistribution/groupby.rs
+++ b/sbroad-core/src/ir/transformation/redistribution/groupby.rs
@@ -23,7 +23,8 @@ use std::collections::{HashMap, HashSet};
 
 use crate::ir::function::{Behavior, Function};
 use crate::ir::helpers::RepeatableState;
-use std::hash::{BuildHasher, Hash, Hasher, RandomState};
+use crate::utils::{OrderedMap, OrderedSet};
+use std::hash::{Hash, Hasher};
 use std::rc::Rc;
 
 const AGGR_CAPACITY: usize = 10;
@@ -299,7 +300,7 @@ impl<'plan> ExpressionMapper<'plan> {
         let is_sq_ref = is_ref
             && self.plan.is_additional_child_of_rel(
                 node_id,
-                *self.plan.get_relational_from_reference_node(current)?,
+                self.plan.get_relational_from_reference_node(current)?,
             )?;
         // Because subqueries are replaced with References, we must not
         // try to match these references against any GroupBy expressions
@@ -353,75 +354,6 @@ impl<'plan> ExpressionMapper<'plan> {
     }
 }
 
-struct OrderedMap<K, V, S = RandomState> {
-    map: HashMap<K, V, S>,
-    order: Vec<(K, V)>,
-}
-
-impl<K: Clone + Hash + Eq, V: Clone, S: BuildHasher> OrderedMap<K, V, S> {
-    fn with_hasher(hasher: S) -> Self {
-        Self {
-            map: HashMap::<K, V, S>::with_hasher(hasher),
-            order: Vec::<(K, V)>::new(),
-        }
-    }
-
-    fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self {
-        Self {
-            map: HashMap::<K, V, S>::with_capacity_and_hasher(capacity, hasher),
-            order: Vec::<(K, V)>::new(),
-        }
-    }
-
-    fn len(&self) -> usize {
-        self.map.len()
-    }
-
-    fn get(&self, key: &K) -> Option<&V> {
-        self.map.get(key)
-    }
-
-    fn remove(&mut self, key: &K) -> Option<V> {
-        self.order.retain(|(k, _)| k != key);
-        self.map.remove(key)
-    }
-
-    fn insert(&mut self, key: K, value: V) {
-        if self.map.insert(key.clone(), value.clone()).is_none() {
-            self.order.push((key, value));
-        }
-    }
-
-    fn get_ordered(&self) -> &Vec<(K, V)> {
-        &self.order
-    }
-}
-
-struct OrderedSet<V, S = RandomState> {
-    set: OrderedMap<V, (), S>,
-}
-
-impl<V: Clone + Hash + Eq, S: BuildHasher> OrderedSet<V, S> {
-    fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self {
-        Self {
-            set: OrderedMap::<V, (), S>::with_capacity_and_hasher(capacity, hasher),
-        }
-    }
-
-    fn insert(&mut self, value: V) {
-        self.set.insert(value, ())
-    }
-
-    fn get_ordered(&self) -> Vec<V> {
-        self.set
-            .get_ordered()
-            .iter()
-            .map(|(v, _)| v)
-            .cloned()
-            .collect()
-    }
-}
-
 impl Plan {
     #[allow(unreachable_code)]
     fn generate_local_alias(id: NodeId) -> String {
@@ -968,11 +900,7 @@ impl Plan {
             for gr_expr in old_gr_cols {
                 unique_grouping_exprs.insert(GroupingExpression::new(*gr_expr, self));
             }
-            let grouping_exprs: Vec<NodeId> = unique_grouping_exprs
-                .get_ordered()
-                .iter()
-                .map(|e| e.id)
-                .collect();
+            let grouping_exprs: Vec<NodeId> = unique_grouping_exprs.iter().map(|e| e.id).collect();
             grouping_expr.extend(grouping_exprs.iter());
             self.set_grouping_cols(upper, grouping_exprs)?;
 
@@ -1292,16 +1220,14 @@ impl Plan {
             unique_grouping_exprs_for_local_stage_full.len(),
             RepeatableState,
         );
-        for (gr_expr, name) in unique_grouping_exprs_for_local_stage_full.get_ordered() {
+        for (gr_expr, name) in unique_grouping_exprs_for_local_stage_full.iter() {
             unique_grouping_exprs_for_local_stage.insert(gr_expr.id, name.clone())
         }
 
         let mut alias_to_pos: HashMap<Rc<String>, usize> = HashMap::new();
         // add grouping expressions to local projection
-        for (pos, (gr_expr, local_alias)) in unique_grouping_exprs_for_local_stage
-            .get_ordered()
-            .iter()
-            .enumerate()
+        for (pos, (gr_expr, local_alias)) in
+            unique_grouping_exprs_for_local_stage.iter().enumerate()
         {
             let new_alias = self.nodes.add_alias(local_alias, *gr_expr)?;
             output_cols.push(new_alias);
@@ -1841,7 +1767,7 @@ impl Plan {
                 let child_id = self.get_relational_from_reference_node(*expr_id)?;
                 let mut context = self.context_mut();
                 if let Some(shard_positions) =
-                    context.get_shard_columns_positions(*child_id, self)?
+                    context.get_shard_columns_positions(child_id, self)?
                 {
                     if shard_positions[0] == Some(*position)
                         || shard_positions[1] == Some(*position)
diff --git a/sbroad-core/src/ir/transformation/split_columns/tests.rs b/sbroad-core/src/ir/transformation/split_columns/tests.rs
index 2bc461321..bddae63dd 100644
--- a/sbroad-core/src/ir/transformation/split_columns/tests.rs
+++ b/sbroad-core/src/ir/transformation/split_columns/tests.rs
@@ -53,8 +53,8 @@ fn split_columns3() {
             "{} {} {} {}",
             r#"unexpected number of values:"#,
             r#"left and right rows have different number of columns:"#,
-            r#"Row(Row { list: [NodeId { offset: 7, arena_type: Arena64 }, NodeId { offset: 8, arena_type: Arena64 }, NodeId { offset: 9, arena_type: Arena64 }], distribution: None }),"#,
-            r#"Row(Row { list: [NodeId { offset: 10, arena_type: Arena64 }, NodeId { offset: 11, arena_type: Arena64 }], distribution: None })"#,
+            r#"Row(Row { list: [NodeId { offset: 5, arena_type: Arena96 }, NodeId { offset: 2, arena_type: Arena64 }, NodeId { offset: 6, arena_type: Arena96 }], distribution: None }),"#,
+            r#"Row(Row { list: [NodeId { offset: 3, arena_type: Arena64 }, NodeId { offset: 7, arena_type: Arena96 }], distribution: None })"#,
         ),
         format!("{plan_err}")
     );
diff --git a/sbroad-core/src/ir/tree/subtree.rs b/sbroad-core/src/ir/tree/subtree.rs
index fa3421862..0db2a6332 100644
--- a/sbroad-core/src/ir/tree/subtree.rs
+++ b/sbroad-core/src/ir/tree/subtree.rs
@@ -244,7 +244,7 @@ fn subtree_next<'plan>(
                             .get_plan()
                             .get_relational_from_reference_node(iter.get_current())
                         {
-                            match iter.get_plan().get_relation_node(*rel_id) {
+                            match iter.get_plan().get_relation_node(rel_id) {
                                 Ok(rel_node)
                                     if rel_node.is_subquery_or_cte() || rel_node.is_motion() =>
                                 {
@@ -256,17 +256,17 @@ fn subtree_next<'plan>(
                                         | Relational::Having(Having { children, .. }),
                                     ) = parent
                                     {
-                                        if children.iter().skip(1).any(|&c| c == *rel_id) {
+                                        if children.iter().skip(1).any(|&c| c == rel_id) {
                                             is_additional = true;
                                         }
                                     }
                                     if let Ok(Relational::Join(Join { children, .. })) = parent {
-                                        if children.iter().skip(2).any(|&c| c == *rel_id) {
+                                        if children.iter().skip(2).any(|&c| c == rel_id) {
                                             is_additional = true;
                                         }
                                     }
                                     if is_additional {
-                                        return Some(*rel_id);
+                                        return Some(rel_id);
                                     }
                                 }
                                 _ => {}
diff --git a/sbroad-core/src/ir/tree/tests.rs b/sbroad-core/src/ir/tree/tests.rs
index 4ae29bf83..2a36ff61a 100644
--- a/sbroad-core/src/ir/tree/tests.rs
+++ b/sbroad-core/src/ir/tree/tests.rs
@@ -223,7 +223,7 @@ fn subtree_dfs_post() {
     .unwrap();
     plan.add_rel(t1);
     let scan_t1_id = plan.add_scan("t1", None).unwrap();
-    let a_ref = plan.nodes.next_id(ArenaType::Arena64);
+    let a_ref = plan.nodes.next_id(ArenaType::Arena96);
     let a = plan.add_row_from_child(scan_t1_id, &["a"]).unwrap();
     let const1 = plan.add_const(Value::from(1_i64));
     let eq_op = plan.nodes.add_bool(a, Bool::Eq, const1).unwrap();
diff --git a/sbroad-core/src/utils.rs b/sbroad-core/src/utils.rs
index b8ac0c753..21a30c688 100644
--- a/sbroad-core/src/utils.rs
+++ b/sbroad-core/src/utils.rs
@@ -1,4 +1,6 @@
 use std::cell::{RefCell, RefMut};
+use std::collections::HashMap;
+use std::hash::{BuildHasher, Hash, RandomState};
 use std::io::{Error, Result, Write};
 use std::ops::DerefMut;
 
@@ -92,3 +94,120 @@ impl<'b> Write for SliceWriter<'b> {
         Ok(())
     }
 }
+
+pub struct OrderedMap<K, V, S = RandomState> {
+    map: HashMap<K, V, S>,
+    order: Vec<(K, V)>,
+}
+
+pub struct OrderedMapIterator<'map, K, V, S> {
+    map: &'map OrderedMap<K, V, S>,
+    index: usize,
+}
+
+impl<'map, K: Clone + Hash + Eq, V: Clone, S: BuildHasher> OrderedMapIterator<'map, K, V, S> {
+    fn new(map: &'map OrderedMap<K, V, S>) -> Self {
+        Self { map, index: 0 }
+    }
+}
+
+impl<'map, K: Clone + Hash + Eq, V: Clone, S: BuildHasher> Iterator
+    for OrderedMapIterator<'map, K, V, S>
+{
+    type Item = &'map (K, V);
+
+    fn next(&mut self) -> Option<Self::Item> {
+        if self.index < self.map.len() {
+            let res = &self.map.order[self.index];
+            self.index += 1;
+            Some(res)
+        } else {
+            None
+        }
+    }
+}
+
+impl<K: Clone + Hash + Eq, V: Clone, S: BuildHasher> OrderedMap<K, V, S> {
+    pub fn with_hasher(hasher: S) -> Self {
+        Self {
+            map: HashMap::<K, V, S>::with_hasher(hasher),
+            order: Vec::<(K, V)>::new(),
+        }
+    }
+
+    pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self {
+        Self {
+            map: HashMap::<K, V, S>::with_capacity_and_hasher(capacity, hasher),
+            order: Vec::<(K, V)>::new(),
+        }
+    }
+
+    pub fn len(&self) -> usize {
+        self.map.len()
+    }
+
+    pub fn is_empty(&self) -> bool {
+        self.map.is_empty()
+    }
+
+    pub fn get(&self, key: &K) -> Option<&V> {
+        self.map.get(key)
+    }
+
+    pub fn remove(&mut self, key: &K) -> Option<V> {
+        self.order.retain(|(k, _)| k != key);
+        self.map.remove(key)
+    }
+
+    pub fn insert(&mut self, key: K, value: V) {
+        if self.map.insert(key.clone(), value.clone()).is_none() {
+            self.order.push((key, value));
+        }
+    }
+
+    pub fn iter(&self) -> OrderedMapIterator<K, V, S> {
+        OrderedMapIterator::new(self)
+    }
+}
+
+pub struct OrderedSet<V, S = RandomState> {
+    map: OrderedMap<V, (), S>,
+}
+
+pub struct OrderedSetIterator<'set, V, S> {
+    map_iterator: OrderedMapIterator<'set, V, (), S>,
+}
+
+impl<'set, V: Clone + Hash + Eq, S: BuildHasher> Iterator for OrderedSetIterator<'set, V, S> {
+    type Item = &'set V;
+
+    fn next(&mut self) -> Option<Self::Item> {
+        self.map_iterator.next().map(|(k, _)| k)
+    }
+}
+
+impl<V: Clone + Hash + Eq, S: BuildHasher> OrderedSet<V, S> {
+    pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self {
+        Self {
+            map: OrderedMap::<V, (), S>::with_capacity_and_hasher(capacity, hasher),
+        }
+    }
+
+    pub fn len(&self) -> usize {
+        self.map.len()
+    }
+
+    pub fn is_empty(&self) -> bool {
+        self.map.is_empty()
+    }
+
+    pub fn insert(&mut self, value: V) {
+        self.map.insert(value, ())
+    }
+
+    pub fn iter(&self) -> OrderedSetIterator<V, S> {
+        OrderedSetIterator {
+            map_iterator: OrderedMapIterator::new(&self.map),
+        }
+    }
+}
-- 
GitLab