From 34c02e1e34bf698bf0eac412fa3d21bb3e6e4e5c Mon Sep 17 00:00:00 2001
From: Denis Smirnov <sd@picodata.io>
Date: Wed, 20 Jul 2022 10:02:47 +0700
Subject: [PATCH] feat(sql)!: don't produce redundant aliases in the output sql

BREAKING CHANGE: reqiures Picodata Tarantool with a string quoting
fix (e9360c989129a755b86486c7bcd6e2d21a078e22).
---
 .gitlab-ci.yml                                |   2 +-
 .../engine/cartridge/backend/sql/ir/tests.rs  |  52 ++---
 .../engine/cartridge/backend/sql/tree.rs      |  13 ++
 .../cartridge/backend/sql/tree/tests.rs       |   3 +-
 src/executor/tests.rs                         | 136 ++++++------
 src/frontend/sql/ir/tests.rs                  |  68 +++---
 src/ir/transformation/bool_in/tests.rs        |   6 +-
 src/ir/transformation/dnf/tests.rs            |  12 +-
 .../equality_propagation/tests.rs             |  10 +-
 src/ir/transformation/merge_tuples/tests.rs   |  10 +-
 src/ir/transformation/split_columns/tests.rs  |  14 +-
 test_app/test/integration/api_test.lua        |  28 +--
 .../test/integration/target_queries_test.lua  | 200 +++++++++---------
 .../sql_order_selection_syntax_nodes.yaml     | 120 +++++------
 14 files changed, 332 insertions(+), 342 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 1eef199e11..2c7ed70d58 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -13,7 +13,7 @@ cache:
 default:
     tags:
         - picodata
-    image: docker-public.binary.picodata.io/picodata/picodata/sbroad/sbroad-builder:0.3.0
+    image: docker-public.binary.picodata.io/picodata/picodata/sbroad/sbroad-builder:0.4.0
 build:
     before_script:
       - make init
diff --git a/src/executor/engine/cartridge/backend/sql/ir/tests.rs b/src/executor/engine/cartridge/backend/sql/ir/tests.rs
index a328a42218..79069d1fde 100644
--- a/src/executor/engine/cartridge/backend/sql/ir/tests.rs
+++ b/src/executor/engine/cartridge/backend/sql/ir/tests.rs
@@ -29,8 +29,8 @@ fn one_table_projection() {
         PatternWithParams::new(
             format!(
                 "{} {} {} {}",
-                r#"SELECT "hash_testing"."identification_number" as "identification_number","#,
-                r#""hash_testing"."product_code" as "product_code""#,
+                r#"SELECT "hash_testing"."identification_number","#,
+                r#""hash_testing"."product_code""#,
                 r#"FROM "hash_testing""#,
                 r#"WHERE ("hash_testing"."identification_number") = (?)"#,
             ),
@@ -60,9 +60,9 @@ fn one_table_with_asterisk() {
         PatternWithParams::new(
             format!(
                 "{} {} {} {} {}",
-                r#"SELECT "hash_testing"."identification_number" as "identification_number","#,
-                r#""hash_testing"."product_code" as "product_code","#,
-                r#""hash_testing"."product_units" as "product_units", "hash_testing"."sys_op" as "sys_op""#,
+                r#"SELECT "hash_testing"."identification_number","#,
+                r#""hash_testing"."product_code","#,
+                r#""hash_testing"."product_units", "hash_testing"."sys_op""#,
                 r#"FROM "hash_testing""#,
                 r#"WHERE ("hash_testing"."identification_number") = (?)"#
             ),
@@ -97,10 +97,10 @@ fn union_all() {
         PatternWithParams::new(
             format!(
                 "{} {} {} {} {}",
-                r#"SELECT "hash_testing"."product_code" as "product_code" FROM "hash_testing""#,
+                r#"SELECT "hash_testing"."product_code" FROM "hash_testing""#,
                 r#"WHERE ("hash_testing"."identification_number") = (?)"#,
                 r#"UNION ALL"#,
-                r#"SELECT "hash_testing_hist"."product_code" as "product_code" FROM "hash_testing_hist""#,
+                r#"SELECT "hash_testing_hist"."product_code" FROM "hash_testing_hist""#,
                 r#"WHERE ("hash_testing_hist"."product_code") = (?)"#
             ),
             vec![Value::from(1_u64), Value::from("a")],
@@ -131,8 +131,8 @@ fn from_sub_query() {
         PatternWithParams::new(
             format!(
                 "{} {} {} {}",
-                r#"SELECT t1."product_code" as "product_code" FROM"#,
-                r#"(SELECT "hash_testing"."product_code" as "product_code" FROM "hash_testing""#,
+                r#"SELECT t1."product_code" FROM"#,
+                r#"(SELECT "hash_testing"."product_code" FROM "hash_testing""#,
                 r#"WHERE ("hash_testing"."identification_number") = (?)) as t1"#,
                 r#"WHERE (t1."product_code") = (?)"#
             ),
@@ -168,11 +168,11 @@ fn from_sub_query_with_union() {
         PatternWithParams::new(
             format!(
                 "{} {} {} {} {} {} {}",
-                r#"SELECT "t1"."product_code" as "product_code" FROM"#,
-                r#"(SELECT "hash_testing"."product_code" as "product_code" FROM "hash_testing""#,
+                r#"SELECT "t1"."product_code" FROM"#,
+                r#"(SELECT "hash_testing"."product_code" FROM "hash_testing""#,
                 r#"WHERE ("hash_testing"."identification_number") = (?)"#,
                 r#"UNION ALL"#,
-                r#"SELECT "hash_testing_hist"."product_code" as "product_code" FROM "hash_testing_hist""#,
+                r#"SELECT "hash_testing_hist"."product_code" FROM "hash_testing_hist""#,
                 r#"WHERE ("hash_testing_hist"."product_code") = (?)) as "t1""#,
                 r#"WHERE ("t1"."product_code") = (?)"#,
             ),
@@ -202,12 +202,12 @@ fn inner_join() {
         PatternWithParams::new(
             format!(
                 "{} {} {} {} {} {} {} {}",
-                r#"SELECT "hash_testing"."product_code" as "product_code""#,
-                r#"FROM (SELECT "hash_testing"."identification_number" as "identification_number","#,
-                r#""hash_testing"."product_code" as "product_code","#,
-                r#""hash_testing"."product_units" as "product_units","#,
-                r#""hash_testing"."sys_op" as "sys_op" FROM "hash_testing") as "hash_testing""#,
-                r#"INNER JOIN (SELECT "history"."id" as "id" FROM "history") as "history""#,
+                r#"SELECT "hash_testing"."product_code""#,
+                r#"FROM (SELECT "hash_testing"."identification_number","#,
+                r#""hash_testing"."product_code","#,
+                r#""hash_testing"."product_units","#,
+                r#""hash_testing"."sys_op" FROM "hash_testing") as "hash_testing""#,
+                r#"INNER JOIN (SELECT "history"."id" FROM "history") as "history""#,
                 r#"ON ("hash_testing"."identification_number") = ("history"."id")"#,
                 r#"WHERE ("hash_testing"."product_code") = (?)"#,
             ),
@@ -238,13 +238,13 @@ fn inner_join_with_sq() {
         PatternWithParams::new(
             format!(
                 "{} {} {} {} {} {} {} {} {}",
-                r#"SELECT "hash_testing"."product_code" as "product_code" FROM (SELECT"#,
-                r#""hash_testing"."identification_number" as "identification_number","#,
-                r#""hash_testing"."product_code" as "product_code","#,
-                r#""hash_testing"."product_units" as "product_units","#,
-                r#""hash_testing"."sys_op" as "sys_op" FROM "hash_testing") as "hash_testing""#,
+                r#"SELECT "hash_testing"."product_code" FROM (SELECT"#,
+                r#""hash_testing"."identification_number","#,
+                r#""hash_testing"."product_code","#,
+                r#""hash_testing"."product_units","#,
+                r#""hash_testing"."sys_op" FROM "hash_testing") as "hash_testing""#,
                 r#"INNER JOIN"#,
-                r#"(SELECT "history"."id" as "id" FROM "history" WHERE ("history"."id") = (?)) as "t""#,
+                r#"(SELECT "history"."id" FROM "history" WHERE ("history"."id") = (?)) as "t""#,
                 r#"ON ("hash_testing"."identification_number") = ("t"."id")"#,
                 r#"WHERE ("hash_testing"."product_code") = (?)"#,
             ),
@@ -273,9 +273,9 @@ fn selection_with_sq() {
         PatternWithParams::new(
             format!(
                 "{} {} {} {} {}",
-                r#"SELECT "hash_testing"."product_code" as "product_code" FROM "hash_testing""#,
+                r#"SELECT "hash_testing"."product_code" FROM "hash_testing""#,
                 r#"WHERE ("hash_testing"."identification_number") in"#,
-                r#"(SELECT "hash_testing_hist"."identification_number" as "identification_number" FROM "hash_testing_hist""#,
+                r#"(SELECT "hash_testing_hist"."identification_number" FROM "hash_testing_hist""#,
                 r#"WHERE ("hash_testing_hist"."product_code") = (?))"#,
                 r#"and ("hash_testing"."product_code") < (?)"#,
             ),
diff --git a/src/executor/engine/cartridge/backend/sql/tree.rs b/src/executor/engine/cartridge/backend/sql/tree.rs
index d89042fa19..2c441905b4 100644
--- a/src/executor/engine/cartridge/backend/sql/tree.rs
+++ b/src/executor/engine/cartridge/backend/sql/tree.rs
@@ -629,6 +629,19 @@ impl<'p> SyntaxPlan<'p> {
                     Ok(self.nodes.push_syntax_node(sn))
                 }
                 Expression::Alias { child, name, .. } => {
+                    // Do not generate an alias in SQL when a column has exactly the same name.
+                    let child_expr = ir_plan.get_expression_node(*child)?;
+                    if let Expression::Reference { .. } = child_expr {
+                        let alias = &ir_plan.get_alias_from_reference_node(child_expr)?;
+                        if alias == name {
+                            let sn = SyntaxNode::new_pointer(
+                                id,
+                                None,
+                                vec![self.nodes.get_syntax_node_id(*child)?],
+                            );
+                            return Ok(self.nodes.push_syntax_node(sn));
+                        }
+                    }
                     let sn = SyntaxNode::new_pointer(
                         id,
                         Some(self.nodes.get_syntax_node_id(*child)?),
diff --git a/src/executor/engine/cartridge/backend/sql/tree/tests.rs b/src/executor/engine/cartridge/backend/sql/tree/tests.rs
index 5955489b53..92777984ba 100644
--- a/src/executor/engine/cartridge/backend/sql/tree/tests.rs
+++ b/src/executor/engine/cartridge/backend/sql/tree/tests.rs
@@ -67,9 +67,8 @@ fn sql_order_selection() {
     let nodes = exec_plan.get_sql_order(top_id).unwrap();
     let mut nodes_iter = nodes.into_iter();
     assert_eq!(Some(SyntaxData::PlanId(16)), nodes_iter.next()); // projection
-    assert_eq!(Some(SyntaxData::PlanId(13)), nodes_iter.next()); // ref
     assert_eq!(Some(SyntaxData::PlanId(14)), nodes_iter.next()); // alias
-    assert_eq!(Some(SyntaxData::Alias("a".into())), nodes_iter.next()); // name a
+    assert_eq!(Some(SyntaxData::PlanId(13)), nodes_iter.next()); // ref
     assert_eq!(Some(SyntaxData::From), nodes_iter.next()); // from
     assert_eq!(Some(SyntaxData::PlanId(3)), nodes_iter.next()); // scan
     assert_eq!(Some(SyntaxData::PlanId(12)), nodes_iter.next()); // selection
diff --git a/src/executor/tests.rs b/src/executor/tests.rs
index 7389df24c8..d4e8a8364c 100644
--- a/src/executor/tests.rs
+++ b/src/executor/tests.rs
@@ -32,7 +32,7 @@ fn shard_query() {
         Value::from(String::from(PatternWithParams::new(
             format!(
                 "{} {}",
-                r#"SELECT "test_space"."FIRST_NAME" as "FIRST_NAME" FROM "test_space""#,
+                r#"SELECT "test_space"."FIRST_NAME" FROM "test_space""#,
                 r#"WHERE ("test_space"."id") = (?)"#
             ),
             vec![param1],
@@ -66,25 +66,22 @@ fn shard_union_query() {
     let mut expected = ProducerResult::new();
     let param1 = Value::from(1_u64);
     let bucket = query.coordinator.determine_bucket_id(&[&param1]);
-    expected
-        .rows
-        .push(vec![
-            Value::String(format!("Execute query on a bucket [{}]", bucket)),
-            Value::String(
-                String::from(
-                    PatternWithParams::new(
-                        format!(
-                            "{} {}{} {} {}{} {}",
-                            r#"SELECT "t3"."id" as "id""#,
-                            r#"FROM ("#,
-                            r#"SELECT "test_space"."id" as "id" FROM "test_space" WHERE ("test_space"."sys_op") = (?)"#,
-                            r#"UNION ALL"#,
-                            r#"SELECT "test_space"."id" as "id" FROM "test_space" WHERE ("test_space"."sys_op") > (?)"#,
-                            r#") as "t3""#,
-                            r#"WHERE ("t3"."id") = (?)"#,
-                        ), vec![Value::from(1_u64), Value::from(1_u64), Value::from(1_u64)]),
-                ))
-        ]);
+    expected.rows.push(vec![
+        Value::String(format!("Execute query on a bucket [{}]", bucket)),
+        Value::String(String::from(PatternWithParams::new(
+            format!(
+                "{} {}{} {} {}{} {}",
+                r#"SELECT "t3"."id""#,
+                r#"FROM ("#,
+                r#"SELECT "test_space"."id" FROM "test_space" WHERE ("test_space"."sys_op") = (?)"#,
+                r#"UNION ALL"#,
+                r#"SELECT "test_space"."id" FROM "test_space" WHERE ("test_space"."sys_op") > (?)"#,
+                r#") as "t3""#,
+                r#"WHERE ("t3"."id") = (?)"#,
+            ),
+            vec![Value::from(1_u64), Value::from(1_u64), Value::from(1_u64)],
+        ))),
+    ]);
 
     assert_eq!(expected, result);
 }
@@ -115,7 +112,7 @@ fn map_reduce_query() {
                 PatternWithParams::new(
                     format!(
                         "{} {} {}",
-                        r#"SELECT "hash_testing"."product_code" as "product_code""#,
+                        r#"SELECT "hash_testing"."product_code""#,
                         r#"FROM "hash_testing""#,
                         r#"WHERE ("hash_testing"."identification_number", "hash_testing"."product_code") = (?, ?)"#,
                     ), vec![param1, param457],
@@ -168,7 +165,7 @@ fn linker_test() {
                     PatternWithParams::new(
                         format!(
                         "{} {} {}",
-                        r#"SELECT "test_space"."FIRST_NAME" as "FIRST_NAME""#,
+                        r#"SELECT "test_space"."FIRST_NAME""#,
                         r#"FROM "test_space""#,
                         r#"WHERE ("test_space"."id") in (SELECT COLUMN_1 as "identification_number" FROM (VALUES (?)))"#,
                         ), vec![param3],
@@ -183,7 +180,7 @@ fn linker_test() {
                     PatternWithParams::new(
                         format!(
                         "{} {} {}",
-                        r#"SELECT "test_space"."FIRST_NAME" as "FIRST_NAME""#,
+                        r#"SELECT "test_space"."FIRST_NAME""#,
                         r#"FROM "test_space""#,
                         r#"WHERE ("test_space"."id") in (SELECT COLUMN_1 as "identification_number" FROM (VALUES (?)))"#,
                         ), vec![param2],
@@ -246,13 +243,13 @@ fn union_linker_test() {
                 String::from(PatternWithParams::new(
                 format!(
                     "{} {}{} {} {} {} {} {} {}{} {}",
-                    r#"SELECT "t1"."id" as "id", "t1"."FIRST_NAME" as "FIRST_NAME""#,
+                    r#"SELECT "t1"."id", "t1"."FIRST_NAME""#,
                     r#"FROM ("#,
-                    r#"SELECT "test_space"."id" as "id", "test_space"."FIRST_NAME" as "FIRST_NAME""#,
+                    r#"SELECT "test_space"."id", "test_space"."FIRST_NAME""#,
                     r#"FROM "test_space""#,
                     r#"WHERE ("test_space"."sys_op") < (?)"#,
                     r#"UNION ALL"#,
-                    r#"SELECT "test_space_hist"."id" as "id", "test_space_hist"."FIRST_NAME" as "FIRST_NAME""#,
+                    r#"SELECT "test_space_hist"."id", "test_space_hist"."FIRST_NAME""#,
                     r#"FROM "test_space_hist""#,
                     r#"WHERE ("test_space_hist"."sys_op") > (?)"#,
                     r#") as "t1""#,
@@ -266,13 +263,13 @@ fn union_linker_test() {
                 String::from(PatternWithParams::new(
                 format!(
                     "{} {}{} {} {} {} {} {} {}{} {}",
-                    r#"SELECT "t1"."id" as "id", "t1"."FIRST_NAME" as "FIRST_NAME""#,
+                    r#"SELECT "t1"."id", "t1"."FIRST_NAME""#,
                     r#"FROM ("#,
-                    r#"SELECT "test_space"."id" as "id", "test_space"."FIRST_NAME" as "FIRST_NAME""#,
+                    r#"SELECT "test_space"."id", "test_space"."FIRST_NAME""#,
                     r#"FROM "test_space""#,
                     r#"WHERE ("test_space"."sys_op") < (?)"#,
                     r#"UNION ALL"#,
-                    r#"SELECT "test_space_hist"."id" as "id", "test_space_hist"."FIRST_NAME" as "FIRST_NAME""#,
+                    r#"SELECT "test_space_hist"."id", "test_space_hist"."FIRST_NAME""#,
                     r#"FROM "test_space_hist""#,
                     r#"WHERE ("test_space_hist"."sys_op") > (?)"#,
                     r#") as "t1""#,
@@ -343,15 +340,15 @@ WHERE "t3"."id" = 2 AND "t8"."identification_number" = 2"#;
                     PatternWithParams::new(
                         format!(
                             "{}, {}, {} {}{} {} {} {} {} {} {}{} {} {}{} {} {}",
-                            r#"SELECT "t3"."id" as "id""#,
-                            r#""t3"."FIRST_NAME" as "FIRST_NAME""#,
-                            r#""t8"."identification_number" as "identification_number""#,
+                            r#"SELECT "t3"."id""#,
+                            r#""t3"."FIRST_NAME""#,
+                            r#""t8"."identification_number""#,
                             r#"FROM ("#,
-                            r#"SELECT "test_space"."id" as "id", "test_space"."FIRST_NAME" as "FIRST_NAME""#,
+                            r#"SELECT "test_space"."id", "test_space"."FIRST_NAME""#,
                             r#"FROM "test_space""#,
                             r#"WHERE ("test_space"."sysFrom") >= (?) and ("test_space"."sys_op") < (?)"#,
                             r#"UNION ALL"#,
-                            r#"SELECT "test_space_hist"."id" as "id", "test_space_hist"."FIRST_NAME" as "FIRST_NAME""#,
+                            r#"SELECT "test_space_hist"."id", "test_space_hist"."FIRST_NAME""#,
                             r#"FROM "test_space_hist""#,
                             r#"WHERE ("test_space_hist"."sysFrom") <= (?)"#,
                             r#") as "t3""#,
@@ -429,10 +426,9 @@ fn join_linker2_test() {
         Value::String(format!("Execute query on a bucket [{}]", bucket1)),
         Value::String(String::from(PatternWithParams::new(
             format!(
-                "{} {} {} {} {} {} {}",
-                r#"SELECT "t1"."id" as "id" FROM (SELECT"#,
-                r#""t1"."id" as "id", "t1"."sysFrom" as "sysFrom","#,
-                r#""t1"."FIRST_NAME" as "FIRST_NAME", "t1"."sys_op" as "sys_op""#,
+                "{} {} {} {} {} {}",
+                r#"SELECT "t1"."id" FROM (SELECT"#,
+                r#""t1"."id", "t1"."sysFrom", "t1"."FIRST_NAME", "t1"."sys_op""#,
                 r#"FROM "test_space" as "t1") as "t1""#,
                 r#"INNER JOIN"#,
                 r#"(SELECT COLUMN_1 as "id1",COLUMN_2 as "id2" FROM (VALUES (?,?)))"#,
@@ -498,8 +494,8 @@ fn join_linker3_test() {
         Value::String(String::from(PatternWithParams::new(
             format!(
                 "{} {} {} {} {}",
-                r#"SELECT "t2"."id1" as "id1" FROM"#,
-                r#"(SELECT "test_space"."id" as "id" FROM "test_space") as "t1""#,
+                r#"SELECT "t2"."id1" FROM"#,
+                r#"(SELECT "test_space"."id" FROM "test_space") as "t1""#,
                 r#"INNER JOIN"#,
                 r#"(SELECT COLUMN_3 as "id1",COLUMN_4 as "id2" FROM (VALUES (?,?),(?,?))) as "t2""#,
                 r#"ON ("t2"."id1") = (?)"#,
@@ -587,10 +583,9 @@ fn join_linker4_test() {
             Value::String(format!("Execute query on a bucket [{}]", bucket2)),
             Value::String(String::from(PatternWithParams::new(
                 format!(
-                    "{} {} {} {} {} {} {} {}",
-                    r#"SELECT t1."id" as "id" FROM (SELECT"#,
-                    r#"t1."id" as "id", t1."sysFrom" as "sysFrom","#,
-                    r#"t1."FIRST_NAME" as "FIRST_NAME", t1."sys_op" as "sys_op""#,
+                    "{} {} {} {} {} {} {}",
+                    r#"SELECT t1."id" FROM (SELECT"#,
+                    r#"t1."id", t1."sysFrom", t1."FIRST_NAME", t1."sys_op""#,
                     r#"FROM "test_space" as t1) as t1"#,
                     r#"INNER JOIN"#,
                     r#"(SELECT COLUMN_1 as "r_id" FROM (VALUES (?))) as t2"#,
@@ -604,10 +599,9 @@ fn join_linker4_test() {
             Value::String(format!("Execute query on a bucket [{}]", bucket1)),
             Value::String(String::from(PatternWithParams::new(
                 format!(
-                    "{} {} {} {} {} {} {} {}",
-                    r#"SELECT t1."id" as "id" FROM (SELECT"#,
-                    r#"t1."id" as "id", t1."sysFrom" as "sysFrom","#,
-                    r#"t1."FIRST_NAME" as "FIRST_NAME", t1."sys_op" as "sys_op""#,
+                    "{} {} {} {} {} {} {}",
+                    r#"SELECT t1."id" FROM (SELECT"#,
+                    r#"t1."id", t1."sysFrom", t1."FIRST_NAME", t1."sys_op""#,
                     r#"FROM "test_space" as t1) as t1"#,
                     r#"INNER JOIN"#,
                     r#"(SELECT COLUMN_1 as "r_id" FROM (VALUES (?))) as t2"#,
@@ -673,10 +667,10 @@ fn anonymous_col_index_test() {
                 format!(
                     "{} {} {} {} {} {} {} {} {} {}",
                     "SELECT",
-                    r#""test_space"."id" as "id","#,
-                    r#""test_space"."sysFrom" as "sysFrom","#,
-                    r#""test_space"."FIRST_NAME" as "FIRST_NAME","#,
-                    r#""test_space"."sys_op" as "sys_op""#,
+                    r#""test_space"."id","#,
+                    r#""test_space"."sysFrom","#,
+                    r#""test_space"."FIRST_NAME","#,
+                    r#""test_space"."sys_op""#,
                     r#"FROM "test_space""#,
                     r#"WHERE (("test_space"."id") in"#,
                     r#"(SELECT COLUMN_1 as "identification_number" FROM (VALUES (?)))"#,
@@ -692,10 +686,10 @@ fn anonymous_col_index_test() {
                 format!(
                     "{} {} {} {} {} {} {} {} {} {}",
                     "SELECT",
-                    r#""test_space"."id" as "id","#,
-                    r#""test_space"."sysFrom" as "sysFrom","#,
-                    r#""test_space"."FIRST_NAME" as "FIRST_NAME","#,
-                    r#""test_space"."sys_op" as "sys_op""#,
+                    r#""test_space"."id","#,
+                    r#""test_space"."sysFrom","#,
+                    r#""test_space"."FIRST_NAME","#,
+                    r#""test_space"."sys_op""#,
                     r#"FROM "test_space""#,
                     r#"WHERE (("test_space"."id") in"#,
                     r#"(SELECT COLUMN_1 as "identification_number" FROM (VALUES (?)))"#,
@@ -731,8 +725,8 @@ fn sharding_column1_test() {
         Value::String(String::from(PatternWithParams::new(
             format!(
                 "{} {} {}",
-                r#"SELECT "test_space"."id" as "id", "test_space"."sysFrom" as "sysFrom","#,
-                r#""test_space"."FIRST_NAME" as "FIRST_NAME", "test_space"."sys_op" as "sys_op""#,
+                r#"SELECT "test_space"."id", "test_space"."sysFrom","#,
+                r#""test_space"."FIRST_NAME", "test_space"."sys_op""#,
                 r#"FROM "test_space" WHERE ("test_space"."id") = (?)"#,
             ),
             vec![Value::from(1_u64)],
@@ -759,18 +753,15 @@ fn sharding_column2_test() {
     let bucket = query.coordinator.determine_bucket_id(&[&param1]);
     expected.rows.push(vec![
         Value::String(format!("Execute query on a bucket [{}]", bucket)),
-        Value::String(
-            String::from(
-                PatternWithParams::new(
-                    format!(
-                    "{} {} {}",
-                    r#"SELECT "test_space"."id" as "id", "test_space"."sysFrom" as "sysFrom","#,
-                    r#""test_space"."FIRST_NAME" as "FIRST_NAME", "test_space"."sys_op" as "sys_op","#,
-                    r#""test_space"."bucket_id" as "bucket_id" FROM "test_space" WHERE ("test_space"."id") = (?)"#,
-                    ), vec![Value::from(1_u64)],
-                )
-            )
-        ),
+        Value::String(String::from(PatternWithParams::new(
+            format!(
+                "{} {} {}",
+                r#"SELECT "test_space"."id", "test_space"."sysFrom","#,
+                r#""test_space"."FIRST_NAME", "test_space"."sys_op","#,
+                r#""test_space"."bucket_id" FROM "test_space" WHERE ("test_space"."id") = (?)"#,
+            ),
+            vec![Value::from(1_u64)],
+        ))),
     ]);
     assert_eq!(expected, result);
 }
@@ -793,9 +784,8 @@ fn bucket1_test() {
         Value::String(format!("Execute query on all buckets")),
         Value::String(String::from(PatternWithParams::new(
             format!(
-                "{} {}",
-                r#"SELECT "t1"."a" as "a", "t1"."b" as "b","#,
-                r#""t1"."bucket_id" as "bucket_id" FROM "t1""#,
+                "{}",
+                r#"SELECT "t1"."a", "t1"."b", "t1"."bucket_id" FROM "t1""#,
             ),
             vec![],
         ))),
diff --git a/src/frontend/sql/ir/tests.rs b/src/frontend/sql/ir/tests.rs
index 9dffa5a10c..628ce00be2 100644
--- a/src/frontend/sql/ir/tests.rs
+++ b/src/frontend/sql/ir/tests.rs
@@ -17,8 +17,8 @@ fn front_sql1() {
     let expected = PatternWithParams::new(
         format!(
             "{} {} {}",
-            r#"SELECT "hash_testing"."identification_number" as "identification_number","#,
-            r#""hash_testing"."product_code" as "product_code""#,
+            r#"SELECT "hash_testing"."identification_number","#,
+            r#""hash_testing"."product_code""#,
             r#"FROM "hash_testing" WHERE ("hash_testing"."identification_number") = (?)"#,
         ),
         vec![Value::from(1_u64)],
@@ -36,7 +36,7 @@ fn front_sql2() {
     let expected = PatternWithParams::new(
         format!(
             "{} {} {}",
-            r#"SELECT "hash_testing"."identification_number" as "identification_number", "hash_testing"."product_code" as "product_code""#,
+            r#"SELECT "hash_testing"."identification_number", "hash_testing"."product_code""#,
             r#"FROM "hash_testing" WHERE (("hash_testing"."identification_number") = (?) and ("hash_testing"."product_code") = (?)"#,
             r#"or ("hash_testing"."identification_number") = (?) and ("hash_testing"."product_code") = (?))"#,
         ),
@@ -66,11 +66,11 @@ fn front_sql3() {
     let expected = PatternWithParams::new(
         format!(
             "{} {} {} {} {} {} {}",
-            r#"SELECT "t3"."identification_number" as "identification_number", "t3"."product_code" as "product_code" FROM"#,
-            r#"(SELECT "hash_testing"."identification_number" as "identification_number", "hash_testing"."product_code" as "product_code""#,
+            r#"SELECT "t3"."identification_number", "t3"."product_code" FROM"#,
+            r#"(SELECT "hash_testing"."identification_number", "hash_testing"."product_code""#,
             r#"FROM "hash_testing" WHERE ("hash_testing"."sys_op") = (?)"#,
             r#"UNION ALL"#,
-            r#"SELECT "hash_testing_hist"."identification_number" as "identification_number", "hash_testing_hist"."product_code" as "product_code""#,
+            r#"SELECT "hash_testing_hist"."identification_number", "hash_testing_hist"."product_code""#,
             r#"FROM "hash_testing_hist" WHERE ("hash_testing_hist"."sys_op") > (?)) as "t3""#,
             r#"WHERE ("t3"."identification_number") = (?)"#,
         ),
@@ -99,11 +99,11 @@ fn front_sql4() {
     let expected = PatternWithParams::new(
         format!(
             "{} {} {} {} {} {} {} {}",
-            r#"SELECT "t3"."identification_number" as "identification_number", "t3"."product_code" as "product_code" FROM"#,
-            r#"(SELECT "hash_testing"."identification_number" as "identification_number", "hash_testing"."product_code" as "product_code""#,
+            r#"SELECT "t3"."identification_number", "t3"."product_code" FROM"#,
+            r#"(SELECT "hash_testing"."identification_number", "hash_testing"."product_code""#,
             r#"FROM "hash_testing" WHERE ("hash_testing"."sys_op") = (?)"#,
             r#"UNION ALL"#,
-            r#"SELECT "hash_testing_hist"."identification_number" as "identification_number", "hash_testing_hist"."product_code" as "product_code""#,
+            r#"SELECT "hash_testing_hist"."identification_number", "hash_testing_hist"."product_code""#,
             r#"FROM "hash_testing_hist" WHERE ("hash_testing_hist"."sys_op") > (?)) as "t3""#,
             r#"WHERE (("t3"."identification_number") = (?) or (("t3"."identification_number") = (?) or ("t3"."identification_number") = (?)))"#,
             r#"and (("t3"."product_code") = (?) or ("t3"."product_code") = (?))"#,
@@ -130,9 +130,9 @@ fn front_sql5() {
     let expected = PatternWithParams::new(
         format!(
             "{} {} {} {}",
-            r#"SELECT "hash_testing"."identification_number" as "identification_number", "hash_testing"."product_code" as "product_code""#,
+            r#"SELECT "hash_testing"."identification_number", "hash_testing"."product_code""#,
             r#"FROM "hash_testing" WHERE ("hash_testing"."identification_number") in"#,
-            r#"(SELECT "hash_testing_hist"."identification_number" as "identification_number" FROM "hash_testing_hist""#,
+            r#"(SELECT "hash_testing_hist"."identification_number" FROM "hash_testing_hist""#,
             r#"WHERE ("hash_testing_hist"."product_code") = (?))"#,
         ),
         vec![Value::from("a")],
@@ -150,12 +150,12 @@ fn front_sql6() {
     let expected = PatternWithParams::new(
         format!(
             "{} {} {} {} {} {} {} {}",
-            r#"SELECT t."id" as "id", "hash_testing"."product_units" as "product_units""#,
-            r#"FROM (SELECT "hash_testing"."identification_number" as "identification_number","#,
-            r#""hash_testing"."product_code" as "product_code","#,
-            r#""hash_testing"."product_units" as "product_units","#,
-            r#""hash_testing"."sys_op" as "sys_op" FROM "hash_testing") as "hash_testing""#,
-            r#"INNER JOIN (SELECT "test_space"."id" as "id" FROM "test_space") as t"#,
+            r#"SELECT t."id", "hash_testing"."product_units""#,
+            r#"FROM (SELECT "hash_testing"."identification_number","#,
+            r#""hash_testing"."product_code","#,
+            r#""hash_testing"."product_units","#,
+            r#""hash_testing"."sys_op" FROM "hash_testing") as "hash_testing""#,
+            r#"INNER JOIN (SELECT "test_space"."id" FROM "test_space") as t"#,
             r#"ON ("hash_testing"."identification_number") = (t."id")"#,
             r#"WHERE ("hash_testing"."identification_number") = (?) and ("hash_testing"."product_code") = (?)"#,
         ),
@@ -196,7 +196,7 @@ fn front_sql8() {
     let expected = PatternWithParams::new(
         format!(
             "{} {}",
-            r#"SELECT t."identification_number" as "identification_number", t."product_code" as "product_code""#,
+            r#"SELECT t."identification_number", t."product_code""#,
             r#"FROM "hash_testing" as t WHERE (t."identification_number") = (?)"#,
         ),
         vec![Value::from(1_u64)],
@@ -230,21 +230,21 @@ fn front_sql9() {
     let expected = PatternWithParams::new(
         format!(
             "{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}",
-            r#"SELECT "t3"."id" as "id", "t3"."FIRST_NAME" as "FIRST_NAME","#,
-            r#""t8"."identification_number" as "identification_number","#,
-            r#""t8"."product_code" as "product_code" FROM"#,
-            r#"(SELECT "test_space"."id" as "id", "test_space"."FIRST_NAME" as "FIRST_NAME""#,
+            r#"SELECT "t3"."id", "t3"."FIRST_NAME","#,
+            r#""t8"."identification_number","#,
+            r#""t8"."product_code" FROM"#,
+            r#"(SELECT "test_space"."id", "test_space"."FIRST_NAME""#,
             r#"FROM "test_space" WHERE ("test_space"."sys_op") < (?) and ("test_space"."sysFrom") >= (?)"#,
             r#"UNION ALL"#,
-            r#"SELECT "test_space_hist"."id" as "id", "test_space_hist"."FIRST_NAME" as "FIRST_NAME""#,
+            r#"SELECT "test_space_hist"."id", "test_space_hist"."FIRST_NAME""#,
             r#"FROM "test_space_hist" WHERE ("test_space_hist"."sysFrom") <= (?))"#,
             r#"as "t3""#,
             r#"INNER JOIN"#,
-            r#"(SELECT "hash_testing_hist"."identification_number" as "identification_number","#,
-            r#""hash_testing_hist"."product_code" as "product_code" FROM "hash_testing_hist" WHERE ("hash_testing_hist"."sys_op") > (?)"#,
+            r#"(SELECT "hash_testing_hist"."identification_number","#,
+            r#""hash_testing_hist"."product_code" FROM "hash_testing_hist" WHERE ("hash_testing_hist"."sys_op") > (?)"#,
             r#"UNION ALL"#,
-            r#"SELECT "hash_single_testing_hist"."identification_number" as "identification_number","#,
-            r#""hash_single_testing_hist"."product_code" as "product_code" FROM "hash_single_testing_hist""#,
+            r#"SELECT "hash_single_testing_hist"."identification_number","#,
+            r#""hash_single_testing_hist"."product_code" FROM "hash_single_testing_hist""#,
             r#"WHERE ("hash_single_testing_hist"."sys_op") <= (?))"#,
             r#"as "t8" ON ("t3"."id") = ("t8"."identification_number")"#,
             r#"WHERE ("t3"."id") = (?) and ("t8"."identification_number") = (?) and ("t8"."product_code") = (?)"#,
@@ -330,7 +330,7 @@ fn front_sql14() {
     let expected = PatternWithParams::new(
         format!(
             "{} {}",
-            r#"INSERT INTO "t" ("a", "c")"#, r#"SELECT "t"."b" as "b", "t"."d" as "d" FROM "t""#,
+            r#"INSERT INTO "t" ("a", "c")"#, r#"SELECT "t"."b", "t"."d" FROM "t""#,
         ),
         vec![],
     );
@@ -361,8 +361,8 @@ fn front_sql16() {
     let expected = PatternWithParams::new(
         format!(
             "{} {} {}",
-            r#"SELECT "hash_testing"."identification_number" as "identification_number","#,
-            r#""hash_testing"."product_code" as "product_code""#,
+            r#"SELECT "hash_testing"."identification_number","#,
+            r#""hash_testing"."product_code""#,
             r#"FROM "hash_testing" WHERE ("hash_testing"."product_code") = (?)"#,
         ),
         vec![Value::from("кириллица")],
@@ -379,7 +379,7 @@ fn front_params1() {
     let expected = PatternWithParams::new(
         format!(
             "{} {}",
-            r#"SELECT "test_space"."id" as "id", "test_space"."FIRST_NAME" as "FIRST_NAME" FROM "test_space""#,
+            r#"SELECT "test_space"."id", "test_space"."FIRST_NAME" FROM "test_space""#,
             r#"WHERE ("test_space"."sys_op") = (?) and ("test_space"."sysFrom") > (?)"#,
         ),
         params.clone(),
@@ -396,7 +396,7 @@ fn front_params2() {
     let expected = PatternWithParams::new(
         format!(
             "{} {}",
-            r#"SELECT "test_space"."id" as "id" FROM "test_space""#,
+            r#"SELECT "test_space"."id" FROM "test_space""#,
             r#"WHERE ("test_space"."sys_op") = (?) and ("test_space"."FIRST_NAME") = (?)"#,
         ),
         params.clone(),
@@ -414,7 +414,7 @@ fn front_params3() {
     let expected = PatternWithParams::new(
         format!(
             "{} {}",
-            r#"SELECT "test_space"."id" as "id" FROM "test_space""#,
+            r#"SELECT "test_space"."id" FROM "test_space""#,
             r#"WHERE ("test_space"."sys_op") = (?) and ("test_space"."FIRST_NAME") = (?)"#,
         ),
         params.clone(),
@@ -433,7 +433,7 @@ fn front_params4() {
     let expected = PatternWithParams::new(
         format!(
             "{} {}",
-            r#"SELECT "test_space"."id" as "id" FROM "test_space""#,
+            r#"SELECT "test_space"."id" FROM "test_space""#,
             r#"WHERE ("test_space"."FIRST_NAME") = (?)"#,
         ),
         params,
diff --git a/src/ir/transformation/bool_in/tests.rs b/src/ir/transformation/bool_in/tests.rs
index e6034c73c9..28a57918a8 100644
--- a/src/ir/transformation/bool_in/tests.rs
+++ b/src/ir/transformation/bool_in/tests.rs
@@ -14,7 +14,7 @@ fn bool_in1() {
     let expected = PatternWithParams::new(
         format!(
             "{} {}",
-            r#"SELECT "t"."a" as "a" FROM "t""#,
+            r#"SELECT "t"."a" FROM "t""#,
             r#"WHERE ((("t"."a") = (?) or ("t"."a") = (?)) or ("t"."a") = (?))"#,
         ),
         vec![Value::from(1_u64), Value::from(2_u64), Value::from(3_u64)],
@@ -30,7 +30,7 @@ fn bool_in2() {
     let expected = PatternWithParams::new(
         format!(
             "{} {}",
-            r#"SELECT "t"."a" as "a" FROM "t""#,
+            r#"SELECT "t"."a" FROM "t""#,
             r#"WHERE ((("t"."a", "t"."b") = (?, ?) or ("t"."a", "t"."b") = (?, ?)) or ("t"."a", "t"."b") = (?, ?))"#,
         ),
         vec![
@@ -52,7 +52,7 @@ fn bool_in3() {
     let expected = PatternWithParams::new(
         format!(
             "{} {}",
-            r#"SELECT "t"."a" as "a" FROM "t""#,
+            r#"SELECT "t"."a" FROM "t""#,
             r#"WHERE (("t"."a") = (?) or ("t"."a") = (?)) and ("t"."b") = (?)"#,
         ),
         vec![Value::from(1_u64), Value::from(2_u64), Value::from(3_u64)],
diff --git a/src/ir/transformation/dnf/tests.rs b/src/ir/transformation/dnf/tests.rs
index d50cb50d0d..56c84ab4d4 100644
--- a/src/ir/transformation/dnf/tests.rs
+++ b/src/ir/transformation/dnf/tests.rs
@@ -15,7 +15,7 @@ fn dnf1() {
     let expected = PatternWithParams::new(
         format!(
             "{} {} {}",
-            r#"SELECT "t"."a" as "a" FROM "t""#,
+            r#"SELECT "t"."a" FROM "t""#,
             r#"WHERE (("t"."a") = (?) and ("t"."b") = (?) and ("t"."c") = (?)"#,
             r#"or ("t"."a") = (?) and ("t"."c") = (?))"#,
         ),
@@ -38,7 +38,7 @@ fn dnf2() {
     let expected = PatternWithParams::new(
         format!(
             "{} {} {}",
-            r#"SELECT "t"."a" as "a" FROM "t""#,
+            r#"SELECT "t"."a" FROM "t""#,
             r#"WHERE (((("t"."a") = (?) and ("t"."a") = (?) or ("t"."c") = (?) and ("t"."a") = (?))"#,
             r#"or ("t"."a") = (?) and ("t"."b") = (?)) or ("t"."c") = (?) and ("t"."b") = (?))"#,
         ),
@@ -64,7 +64,7 @@ fn dnf3() {
     let expected = PatternWithParams::new(
         format!(
             "{} {}",
-            r#"SELECT "t"."a" as "a" FROM "t""#,
+            r#"SELECT "t"."a" FROM "t""#,
             r#"WHERE (("t"."a") = (?) and (?) or ("t"."b") = (?) and (?))"#,
         ),
         vec![
@@ -85,7 +85,7 @@ fn dnf4() {
     let expected = PatternWithParams::new(
         format!(
             "{} {}",
-            r#"SELECT "t"."a" as "a" FROM "t""#,
+            r#"SELECT "t"."a" FROM "t""#,
             r#"WHERE (("t"."a") = (?) and (?) or ("t"."b") = (?) and (?))"#,
         ),
         vec![
@@ -106,7 +106,7 @@ fn dnf5() {
     let expected = PatternWithParams::new(
         format!(
             "{} {}",
-            r#"SELECT "t"."a" as "a" FROM "t""#,
+            r#"SELECT "t"."a" FROM "t""#,
             r#"WHERE (("t"."a") = (?) and ((?)) or ("t"."b") = (?) and ((?)))"#,
         ),
         vec![
@@ -127,7 +127,7 @@ fn dnf6() {
     let expected = PatternWithParams::new(
         format!(
             "{} {}",
-            r#"SELECT "t"."a" as "a" FROM "t""#,
+            r#"SELECT "t"."a" FROM "t""#,
             r#"WHERE (("t"."a") = (?) and ("t"."c") = (?) or ("t"."b") = (?))"#,
         ),
         vec![Value::from(1_u64), Value::from(1_u64), Value::from(2_u64)],
diff --git a/src/ir/transformation/equality_propagation/tests.rs b/src/ir/transformation/equality_propagation/tests.rs
index 797712d576..b7aa75d6d9 100644
--- a/src/ir/transformation/equality_propagation/tests.rs
+++ b/src/ir/transformation/equality_propagation/tests.rs
@@ -16,7 +16,7 @@ fn equality_propagation1() {
     let expected = PatternWithParams::new(
         format!(
             "{} {} {}",
-            r#"SELECT "t"."a" as "a" FROM "t""#,
+            r#"SELECT "t"."a" FROM "t""#,
             r#"WHERE (("t"."a") = (?) and ("t"."b") = (?) and ("t"."c") = (?)"#,
             r#"and ("t"."c") = ("t"."a") or ("t"."d") = (?))"#,
         ),
@@ -39,7 +39,7 @@ fn equality_propagation2() {
     let expected = PatternWithParams::new(
         format!(
             "{}",
-            r#"SELECT "t"."a" as "a" FROM "t" WHERE ("t"."a") = (?) and ("t"."b") = (?)"#,
+            r#"SELECT "t"."a" FROM "t" WHERE ("t"."a") = (?) and ("t"."b") = (?)"#,
         ),
         vec![Value::Null, Value::Null],
     );
@@ -55,7 +55,7 @@ fn equality_propagation3() {
     let expected = PatternWithParams::new(
         format!(
             "{} {}",
-            r#"SELECT "t"."a" as "a" FROM "t""#,
+            r#"SELECT "t"."a" FROM "t""#,
             r#"WHERE ("t"."a") = (?) and ("t"."b") = (?) and ("t"."a") = (?)"#,
         ),
         vec![Value::from(1_u64), Value::Null, Value::Null],
@@ -72,7 +72,7 @@ fn equality_propagation4() {
     let expected = PatternWithParams::new(
         format!(
             "{} {} {}",
-            r#"SELECT "t"."a" as "a" FROM "t""#,
+            r#"SELECT "t"."a" FROM "t""#,
             r#"WHERE ("t"."a") = (?) and ("t"."b") = (?) and ("t"."a") = (?)"#,
             r#"and ("t"."b") = (?) and ("t"."b") = ("t"."a")"#,
         ),
@@ -95,7 +95,7 @@ fn equality_propagation5() {
     let expected = PatternWithParams::new(
         format!(
             "{} {} {} {} {}",
-            r#"SELECT "t"."a" as "a" FROM "t""#,
+            r#"SELECT "t"."a" FROM "t""#,
             r#"WHERE ("t"."a") = (?) and ("t"."b") = (?)"#,
             r#"and ("t"."c") = (?) and ("t"."d") = (?)"#,
             r#"and ("t"."c") = ("t"."b") and ("t"."b") = ("t"."a")"#,
diff --git a/src/ir/transformation/merge_tuples/tests.rs b/src/ir/transformation/merge_tuples/tests.rs
index 18559b5381..ce4511c428 100644
--- a/src/ir/transformation/merge_tuples/tests.rs
+++ b/src/ir/transformation/merge_tuples/tests.rs
@@ -14,7 +14,7 @@ fn merge_tuples1() {
     let expected = PatternWithParams::new(
         format!(
             "{} {}",
-            r#"SELECT "t"."a" as "a" FROM "t""#,
+            r#"SELECT "t"."a" FROM "t""#,
             r#"WHERE ("t"."a", "t"."b") = (?, ?) and (?) < ("t"."a") and ("t"."c") < (?)"#,
         ),
         vec![
@@ -36,7 +36,7 @@ fn merge_tuples2() {
     let expected = PatternWithParams::new(
         format!(
             "{} {} {}",
-            r#"SELECT "t"."a" as "a" FROM "t""#,
+            r#"SELECT "t"."a" FROM "t""#,
             r#"WHERE (("t"."a", "t"."b") = (?, ?) and (?)"#,
             r#"or (?) <= ("t"."a") and ("t"."c") >= (?) and (?))"#,
         ),
@@ -57,7 +57,7 @@ fn merge_tuples2() {
 fn merge_tuples3() {
     let input = r#"SELECT "a" FROM "t" WHERE true"#;
     let expected = PatternWithParams::new(
-        format!("{}", r#"SELECT "t"."a" as "a" FROM "t" WHERE ?"#),
+        format!("{}", r#"SELECT "t"."a" FROM "t" WHERE ?"#),
         vec![Value::Boolean(true)],
     );
 
@@ -70,7 +70,7 @@ fn merge_tuples4() {
     let expected = PatternWithParams::new(
         format!(
             "{} {}",
-            r#"SELECT "t"."a" as "a" FROM "t""#, r#"WHERE ("t"."a", "t"."b", "t"."c") = (?, ?, ?)"#,
+            r#"SELECT "t"."a" FROM "t""#, r#"WHERE ("t"."a", "t"."b", "t"."c") = (?, ?, ?)"#,
         ),
         vec![Value::from(1_u64), Value::from(2_u64), Value::from(3_u64)],
     );
@@ -84,7 +84,7 @@ fn merge_tuples5() {
     let expected = PatternWithParams::new(
         format!(
             "{} {}",
-            r#"SELECT "t"."a" as "a" FROM "t""#,
+            r#"SELECT "t"."a" FROM "t""#,
             r#"WHERE ("t"."a", "t"."b") > (?, ?) and (?) < ("t"."c")"#,
         ),
         vec![Value::from(1_u64), Value::from(2_u64), Value::from(3_u64)],
diff --git a/src/ir/transformation/split_columns/tests.rs b/src/ir/transformation/split_columns/tests.rs
index d5b51ac934..8dbdcf5a2b 100644
--- a/src/ir/transformation/split_columns/tests.rs
+++ b/src/ir/transformation/split_columns/tests.rs
@@ -17,7 +17,7 @@ fn split_columns1() {
     let expected = PatternWithParams::new(
         format!(
             "{}",
-            r#"SELECT "t"."a" as "a" FROM "t" WHERE ("t"."a") = (?) and (?) = ("t"."b")"#,
+            r#"SELECT "t"."a" FROM "t" WHERE ("t"."a") = (?) and (?) = ("t"."b")"#,
         ),
         vec![Value::from(1_u64), Value::from(2_u64)],
     );
@@ -29,10 +29,7 @@ fn split_columns1() {
 fn split_columns2() {
     let input = r#"SELECT "a" FROM "t" WHERE "a" = 1"#;
     let expected = PatternWithParams::new(
-        format!(
-            "{}",
-            r#"SELECT "t"."a" as "a" FROM "t" WHERE ("t"."a") = (?)"#,
-        ),
+        format!("{}", r#"SELECT "t"."a" FROM "t" WHERE ("t"."a") = (?)"#,),
         vec![Value::from(1_u64)],
     );
 
@@ -63,10 +60,7 @@ fn split_columns3() {
 fn split_columns4() {
     let input = r#"SELECT "a" FROM "t" WHERE "a" in (1, 2)"#;
     let expected = PatternWithParams::new(
-        format!(
-            "{}",
-            r#"SELECT "t"."a" as "a" FROM "t" WHERE ("t"."a") in (?, ?)"#,
-        ),
+        format!("{}", r#"SELECT "t"."a" FROM "t" WHERE ("t"."a") in (?, ?)"#,),
         vec![Value::from(1_u64), Value::from(2_u64)],
     );
 
@@ -79,7 +73,7 @@ fn split_columns5() {
     let expected = PatternWithParams::new(
         format!(
             "{} {}",
-            r#"SELECT "t"."a" as "a" FROM "t" WHERE ("t"."a") < (?) and (?) < ("t"."b")"#,
+            r#"SELECT "t"."a" FROM "t" WHERE ("t"."a") < (?) and (?) < ("t"."b")"#,
             r#"and ("t"."a") > (?)"#,
         ),
         vec![Value::from(1_u64), Value::from(2_u64), Value::from(2_u64)],
diff --git a/test_app/test/integration/api_test.lua b/test_app/test/integration/api_test.lua
index cdb9a53a81..4c3fc251b2 100644
--- a/test_app/test/integration/api_test.lua
+++ b/test_app/test/integration/api_test.lua
@@ -137,8 +137,8 @@ g.test_simple_shard_key_union_query = function()
     t.assert_equals(err, nil)
     t.assert_equals(r, {
         metadata = {
-            {name = "id", type = "integer"},
-            {name = "name", type = "string"},
+            {name = "t1.id", type = "integer"},
+            {name = "t1.name", type = "string"},
         },
         rows = {
             { 1, "ok_hist" },
@@ -188,9 +188,9 @@ g.test_complex_shard_key_union_query = function()
     t.assert_equals(err, nil)
     t.assert_equals(r, {
         metadata = {
-            {name = "id", type = "integer"},
-            {name = "name", type = "string"},
-            {name = "product_units", type = "integer"}
+            {name = "t1.id", type = "integer"},
+            {name = "t1.name", type = "string"},
+            {name = "t1.product_units", type = "integer"}
         },
         rows = {
             { 1, "123", 1 },
@@ -236,8 +236,8 @@ g.test_motion_query = function()
     t.assert_equals(err, nil)
     t.assert_equals(r, {
         metadata = {
-            {name = "id", type = "integer"},
-            {name = "name", type = "string"},
+            {name = "t1.id", type = "integer"},
+            {name = "t1.name", type = "string"},
         },
         rows = {
             { 1, "ok" },
@@ -289,9 +289,9 @@ g.test_join_motion_query = function()
     t.assert_equals(err, nil)
     t.assert_equals(r, {
         metadata = {
-            {name = "id", type = "integer"},
-            {name = "name", type = "string"},
-            {name = "product_units", type = "any"},
+            {name = "t3.id", type = "integer"},
+            {name = "t3.name", type = "string"},
+            {name = "t8.product_units", type = "any"},
         },
         rows = {
             { 1, "ok", 5 },
@@ -585,10 +585,10 @@ g.test_bucket_id_in_join = function()
     t.assert_equals(err, nil)
     t.assert_equals(r, {
         metadata = {
-            {name = "id", type = "integer"},                                                                                                                                                                    
-            {name = "name", type = "string"},                                                             
-            {name = "sysOp", type = "integer"},
-            {name = "a", type = "any"},
+            {name = "t1.id", type = "integer"},                                                                                                                                                                    
+            {name = "t1.name", type = "string"},                                                             
+            {name = "t1.sysOp", type = "integer"},
+            {name = "t2.a", type = "any"},
         },
         rows = {},
     })
diff --git a/test_app/test/integration/target_queries_test.lua b/test_app/test/integration/target_queries_test.lua
index ba11bd4cf0..4964303c66 100644
--- a/test_app/test/integration/target_queries_test.lua
+++ b/test_app/test/integration/target_queries_test.lua
@@ -169,9 +169,9 @@ FROM
 WHERE "col1" = ?]], { 0, 0, 1} })
 
     t.assert_equals(r.metadata, {
-        { name = "col1", type = "integer" },
-        { name = "account_id", type = "integer" },
-        { name = "amount", type = "integer" },
+        { name = "t3.col1", type = "integer" },
+        { name = "t3.account_id", type = "integer" },
+        { name = "t3.amount", type = "integer" },
     })
     t.assert_items_equals(r.rows, {
         { 1, 1, 2 },
@@ -198,10 +198,10 @@ WHERE "col1" = ?
 
     t.assert_equals(err, nil)
     t.assert_equals(r.metadata, {
-        { name = "col1", type = "integer" },
-        { name = "col2", type = "integer" },
-        { name = "account_id", type = "integer" },
-        { name = "amount", type = "integer" },
+        { name = "t3.col1", type = "integer" },
+        { name = "t3.col2", type = "integer" },
+        { name = "t3.account_id", type = "integer" },
+        { name = "t3.amount", type = "integer" },
     })
     t.assert_items_equals(r.rows, {
         { 1, 2, 1, 2 },
@@ -230,10 +230,10 @@ WHERE "col1" = ?
     t.assert_equals(err, nil)
     t.assert_equals(r, {
         metadata = {
-            { name = "col1", type = "integer" },
-            { name = "col2", type = "integer" },
-            { name = "account_id", type = "integer" },
-            { name = "amount", type = "integer" },
+            { name = "t3.col1", type = "integer" },
+            { name = "t3.col2", type = "integer" },
+            { name = "t3.account_id", type = "integer" },
+            { name = "t3.amount", type = "integer" },
         },
         rows = {
             { 1, 2, 1, 3 }
@@ -258,9 +258,9 @@ WHERE "col1" = ? OR "col1" = ?]], { 0, 0, 0, 1, 3} })
 
     t.assert_equals(err, nil)
     t.assert_equals(r.metadata, {
-        { name = "col1", type = "integer" },
-        { name = "account_id", type = "integer" },
-        { name = "amount", type = "integer" },
+        { name = "t3.col1", type = "integer" },
+        { name = "t3.account_id", type = "integer" },
+        { name = "t3.amount", type = "integer" },
     })
     t.assert_items_equals(r.rows, {
         { 3, 1, 3 },
@@ -290,10 +290,10 @@ WHERE "col1" = ?
 
     t.assert_equals(err, nil)
     t.assert_equals(r.metadata, {
-        { name = "col1", type = "integer" },
-        { name = "col2", type = "integer" },
-        { name = "account_id", type = "integer" },
-        { name = "amount", type = "integer" },
+        { name = "t3.col1", type = "integer" },
+        { name = "t3.col2", type = "integer" },
+        { name = "t3.account_id", type = "integer" },
+        { name = "t3.amount", type = "integer" },
     })
     t.assert_items_equals(r.rows, {
         { 1, 2, 1, 2 },
@@ -322,9 +322,9 @@ WHERE "col1" = ?
 
     t.assert_equals(err, nil)
     t.assert_equals(r.metadata, {
-        { name = "col1", type = "integer" },
-        { name = "account_id", type = "integer" },
-        { name = "amount", type = "integer" },
+        { name = "t3.col1", type = "integer" },
+        { name = "t3.account_id", type = "integer" },
+        { name = "t3.amount", type = "integer" },
     })
     t.assert_items_equals(r.rows, {
         { 3, 1, 3 },
@@ -355,10 +355,10 @@ WHERE ("col1" = ?
 
     t.assert_equals(err, nil)
     t.assert_equals(r.metadata, {
-        { name = "col1", type = "integer" },
-        { name = "col2", type = "integer" },
-        { name = "account_id", type = "integer" },
-        { name = "amount", type = "integer" },
+        { name = "t3.col1", type = "integer" },
+        { name = "t3.col2", type = "integer" },
+        { name = "t3.account_id", type = "integer" },
+        { name = "t3.amount", type = "integer" },
     })
     t.assert_items_equals(r.rows, {
         { 1, 1, 1, 3 },
@@ -390,10 +390,10 @@ WHERE ("col1" = ?
 
     t.assert_equals(err, nil)
     t.assert_equals(r.metadata, {
-        { name = "col1", type = "integer" },
-        { name = "col2", type = "integer" },
-        { name = "account_id", type = "integer" },
-        { name = "amount", type = "integer" },
+        { name = "t3.col1", type = "integer" },
+        { name = "t3.col2", type = "integer" },
+        { name = "t3.account_id", type = "integer" },
+        { name = "t3.amount", type = "integer" },
     })
     t.assert_items_equals(r.rows, {
         { 1, 1, 1, 3 },
@@ -428,9 +428,9 @@ WHERE "col1" IN
 
     t.assert_equals(err, nil)
     t.assert_equals(r.metadata, {
-        { name = "col1", type = "integer" },
-        { name = "account_id", type = "integer" },
-        { name = "amount", type = "integer" },
+        { name = "t3.col1", type = "integer" },
+        { name = "t3.account_id", type = "integer" },
+        { name = "t3.amount", type = "integer" },
     })
     t.assert_items_equals(r.rows, {
         { 1, 1, 2 },
@@ -466,9 +466,9 @@ WHERE "col1" IN
 
     t.assert_equals(err, nil)
     t.assert_equals(r.metadata, {
-        { name = "col1", type = "integer" },
-        { name = "account_id", type = "integer" },
-        { name = "amount", type = "integer" },
+        { name = "t3.col1", type = "integer" },
+        { name = "t3.account_id", type = "integer" },
+        { name = "t3.amount", type = "integer" },
     })
     t.assert_items_equals(r.rows, {
         { 1, 1, 2 },
@@ -504,10 +504,10 @@ WHERE ROW("col1", "col2") IN
 
     t.assert_equals(err, nil)
     t.assert_equals(r.metadata, {
-        { name = "col1", type = "integer" },
-        { name = "col2", type = "integer" },
-        { name = "account_id", type = "integer" },
-        { name = "amount", type = "integer" },
+        { name = "t3.col1", type = "integer" },
+        { name = "t3.col2", type = "integer" },
+        { name = "t3.account_id", type = "integer" },
+        { name = "t3.amount", type = "integer" },
     })
     t.assert_items_equals(r.rows, {
         { 1, 1, 1, 3 }
@@ -540,12 +540,12 @@ WHERE "t3"."col1" = 1 AND "t8"."cola" = 1]], {} })
 
     t.assert_equals(err, nil)
     t.assert_equals(r.metadata, {
-        { name = "col1", type = "integer" },
-        { name = "account_id", type = "integer" },
-        { name = "amount", type = "integer" },
-        { name = "id", type = "any" },
-        { name = "cola", type = "any" },
-        { name = "colb", type = "any" },
+        { name = "t3.col1", type = "integer" },
+        { name = "t3.account_id", type = "integer" },
+        { name = "t3.amount", type = "integer" },
+        { name = "t8.id", type = "any" },
+        { name = "t8.cola", type = "any" },
+        { name = "t8.colb", type = "any" },
     })
 
     t.assert_items_equals(r.rows, {
@@ -585,12 +585,12 @@ WHERE "t3"."col1" = ? AND ("t8"."cola" = ?
 
     t.assert_equals(err, nil)
     t.assert_equals(r.metadata, {
-        { name = "col1", type = "integer" },
-        { name = "account_id", type = "integer" },
-        { name = "amount", type = "integer" },
-        { name = "id", type = "any" },
-        { name = "cola", type = "any" },
-        { name = "colb", type = "any" },
+        { name = "t3.col1", type = "integer" },
+        { name = "t3.account_id", type = "integer" },
+        { name = "t3.amount", type = "integer" },
+        { name = "t8.id", type = "any" },
+        { name = "t8.cola", type = "any" },
+        { name = "t8.colb", type = "any" },
     })
 
     t.assert_items_equals(r.rows, {
@@ -625,13 +625,13 @@ AND ("t8"."cola" = ? AND "t8"."colb" = ?)]], { 0, 0, 0, 0, 0, 0, 1, 2, 1, 2 } })
 
     t.assert_equals(err, nil)
     t.assert_equals(r.metadata, {
-        { name = "col1", type = "integer" },
-        { name = "col2", type = "integer" },
-        { name = "account_id", type = "integer" },
-        { name = "amount", type = "integer" },
-        { name = "id", type = "any" },
-        { name = "cola", type = "any" },
-        { name = "colb", type = "any" },
+        { name = "t3.col1", type = "integer" },
+        { name = "t3.col2", type = "integer" },
+        { name = "t3.account_id", type = "integer" },
+        { name = "t3.amount", type = "integer" },
+        { name = "t8.id", type = "any" },
+        { name = "t8.cola", type = "any" },
+        { name = "t8.colb", type = "any" },
     })
 
     t.assert_items_equals(r.rows, {
@@ -667,13 +667,13 @@ AND ("t8"."cola" = 1 AND ("t8"."colb" = 2 AND "t3"."amount" > 0))]], {} })
 
     t.assert_equals(err, nil)
     t.assert_equals(r.metadata, {
-        { name = "col1", type = "integer" },
-        { name = "col2", type = "integer" },
-        { name = "account_id", type = "integer" },
-        { name = "amount", type = "integer" },
-        { name = "id", type = "any" },
-        { name = "cola", type = "any" },
-        { name = "colb", type = "any" },
+        { name = "t3.col1", type = "integer" },
+        { name = "t3.col2", type = "integer" },
+        { name = "t3.account_id", type = "integer" },
+        { name = "t3.amount", type = "integer" },
+        { name = "t8.id", type = "any" },
+        { name = "t8.cola", type = "any" },
+        { name = "t8.colb", type = "any" },
     })
 
     t.assert_items_equals(r.rows, {
@@ -708,12 +708,12 @@ WHERE "t3"."col1" = ? AND "t8"."cola" = ?]], { 1, 2 } })
 
     t.assert_equals(err, nil)
     t.assert_equals(r.metadata, {
-        { name = "col1", type = "integer" },
-        { name = "account_id", type = "integer" },
-        { name = "amount", type = "integer" },
-        { name = "id", type = "any" },
-        { name = "cola", type = "any" },
-        { name = "colb", type = "any" },
+        { name = "t3.col1", type = "integer" },
+        { name = "t3.account_id", type = "integer" },
+        { name = "t3.amount", type = "integer" },
+        { name = "t8.id", type = "any" },
+        { name = "t8.cola", type = "any" },
+        { name = "t8.colb", type = "any" },
     })
 
     t.assert_items_equals(r.rows, {
@@ -748,13 +748,13 @@ WHERE "t3"."col1" = ? AND "t3"."col2" = 2 AND ("t8"."cola" = 1 AND "t8"."colb" =
 
     t.assert_equals(err, nil)
     t.assert_equals(r.metadata, {
-        { name = "col1", type = "number" },
-        { name = "col2", type = "integer" },
-        { name = "account_id", type = "integer" },
-        { name = "amount", type = "integer" },
-        { name = "id", type = "any" },
-        { name = "cola", type = "any" },
-        { name = "colb", type = "any" },
+        { name = "t3.col1", type = "number" },
+        { name = "t3.col2", type = "integer" },
+        { name = "t3.account_id", type = "integer" },
+        { name = "t3.amount", type = "integer" },
+        { name = "t8.id", type = "any" },
+        { name = "t8.cola", type = "any" },
+        { name = "t8.colb", type = "any" },
     })
 
     t.assert_items_equals(r.rows, {
@@ -788,13 +788,13 @@ WHERE "t3"."col1" = 1 AND ("t3"."col2" = 1 AND "t8"."colb" = 2)]], {} })
 
     t.assert_equals(err, nil)
     t.assert_equals(r.metadata, {
-        { name = "col1", type = "integer" },
-        { name = "col2", type = "integer" },
-        { name = "account_id", type = "integer" },
-        { name = "amount", type = "integer" },
-        { name = "id", type = "any" },
-        { name = "cola", type = "any" },
-        { name = "colb", type = "any" },
+        { name = "t3.col1", type = "integer" },
+        { name = "t3.col2", type = "integer" },
+        { name = "t3.account_id", type = "integer" },
+        { name = "t3.amount", type = "integer" },
+        { name = "t8.id", type = "any" },
+        { name = "t8.cola", type = "any" },
+        { name = "t8.colb", type = "any" },
     })
 
     t.assert_items_equals(r.rows, {
@@ -827,13 +827,13 @@ WHERE "t3"."col1" = 1 AND "t3"."col2" = 1]], {} })
 
     t.assert_equals(err, nil)
     t.assert_equals(r.metadata, {
-        { name = "col1", type = "integer" },
-        { name = "col2", type = "integer" },
-        { name = "account_id", type = "integer" },
-        { name = "amount", type = "integer" },
-        { name = "id", type = "any" },
-        { name = "cola", type = "any" },
-        { name = "colb", type = "any" },
+        { name = "t3.col1", type = "integer" },
+        { name = "t3.col2", type = "integer" },
+        { name = "t3.account_id", type = "integer" },
+        { name = "t3.amount", type = "integer" },
+        { name = "t8.id", type = "any" },
+        { name = "t8.cola", type = "any" },
+        { name = "t8.colb", type = "any" },
     })
 
     t.assert_items_equals(r.rows, {
@@ -912,10 +912,10 @@ WHERE "account_id" IN
 
     t.assert_equals(err, nil)
     t.assert_equals(r.metadata, {
-        { name = "col1", type = "integer" },
-        { name = "col2", type = "integer" },
-        { name = "account_id", type = "integer" },
-        { name = "amount", type = "integer" },
+        { name = "t3.col1", type = "integer" },
+        { name = "t3.col2", type = "integer" },
+        { name = "t3.account_id", type = "integer" },
+        { name = "t3.amount", type = "integer" },
     })
 
     t.assert_items_equals(r.rows, {
@@ -952,10 +952,10 @@ WHERE "account_id" IN
 
     t.assert_equals(err, nil)
     t.assert_equals(r.metadata, {
-        { name = "col1", type = "integer" },
-        { name = "col2", type = "integer" },
-        { name = "account_id", type = "integer" },
-        { name = "amount", type = "integer" },
+        { name = "t3.col1", type = "integer" },
+        { name = "t3.col2", type = "integer" },
+        { name = "t3.account_id", type = "integer" },
+        { name = "t3.amount", type = "integer" },
     })
 
     t.assert_items_equals(r.rows, {
diff --git a/tests/artifactory/backend/sql/tree/sql_order_selection_syntax_nodes.yaml b/tests/artifactory/backend/sql/tree/sql_order_selection_syntax_nodes.yaml
index 8787c2e238..1dc1e5a2b2 100644
--- a/tests/artifactory/backend/sql/tree/sql_order_selection_syntax_nodes.yaml
+++ b/tests/artifactory/backend/sql/tree/sql_order_selection_syntax_nodes.yaml
@@ -1,20 +1,19 @@
-# selection 17
-#   - scan 6
-#     - projection 19
-#       - alias 2
+# selection 16
+#   - scan 5
+#     - projection 18
+#       - alias 1
 #         - reference 0
-#         - alias 1
-#       - from 18
-#   - bool 16
-#     - row 10
-#       - ( 8
-#       - reference 7
-#       - ) 9
-#     - = 15
-#     - row 14
-#       - ( 12
-#       - parameter 11
-#       - ) 13
+#       - from 17
+#   - bool 15
+#     - row 9
+#       - ( 7
+#       - reference 6
+#       - ) 8
+#     - = 14
+#     - row 13
+#       - ( 11
+#       - parameter 10
+#       - ) 12
 ---
 arena:
 #0
@@ -23,117 +22,112 @@ arena:
     left: ~
     right: []
 #1
-  - data:
-      Alias: a
-    left: ~
-    right: []
-#2
   - data:
       PlanId: 14
-    left: 0
+    left: ~
     right:
-      - 1
-#3
+      - 0
+#2
   - data: OpenParenthesis
     left: ~
     right: []
-#4
+#3
   - data: CloseParenthesis
     left: ~
     right: []
-#5
+#4
   - data:
       PlanId: 15
     left: ~
     right:
-      - 3
       - 2
-      - 4
-#6
+      - 1
+      - 3
+#5
   - data:
       PlanId: 3
-    left: 19
+    left: 18
     right: []
-#7
+#6
   - data:
       PlanId: 4
     left: ~
     right: []
-#8
+#7
   - data: OpenParenthesis
     left: ~
     right: []
-#9
+#8
   - data: CloseParenthesis
     left: ~
     right: []
-#10
+#9
   - data:
       PlanId: 5
     left: ~
     right:
-      - 8
       - 7
-      - 9
-#11
+      - 6
+      - 8
+#10
   - data:
       Parameter: 6
     left: ~
     right: []
-#12
+#11
   - data: OpenParenthesis
     left: ~
     right: []
-#13
+#12
   - data: CloseParenthesis
     left: ~
     right: []
-#14
+#13
   - data:
       PlanId: 7
     left: ~
     right:
-      - 12
       - 11
-      - 13
-#15
+      - 10
+      - 12
+#14
   - data:
       Operator: =
     left: ~
     right: []
-#16
+#15
   - data:
       PlanId: 8
-    left: 10
+    left: 9
     right:
-      - 15
       - 14
-#17
+      - 13
+#16
   - data:
       PlanId: 12
-    left: 6
+    left: 5
     right:
-      - 16
-#18
+      - 15
+#17
   - data: From
     left: ~
     right: []
-#19
+#18
   - data:
       PlanId: 16
     left: ~
     right:
-      - 2
-      - 18
+      - 1
+      - 17
 map:
-  14: 2
-  6: 11
-  3: 6
-  7: 14
-  4: 7
+  8: 15
+  16: 18
+  4: 6
+  3: 5
+  14: 1
   13: 0
-  15: 5
-  16: 19
-  8: 16
-  5: 10
-  12: 17
+  6: 10
+  5: 9
+  7: 13
+  12: 16
+  15: 4
-- 
GitLab