From 0bb13a94e7b32a6a670362b37143c2334c2f3e1b Mon Sep 17 00:00:00 2001
From: zhyass <mytesla@live.com>
Date: Wed, 15 Jan 2025 20:25:36 +0800
Subject: [PATCH] fix quoted cluster key

---
 .../sql/src/executor/physical_plan_visitor.rs |  1 -
 src/query/sql/src/planner/binder/ddl/table.rs | 31 +++++++++----------
 2 files changed, 15 insertions(+), 17 deletions(-)

diff --git a/src/query/sql/src/executor/physical_plan_visitor.rs b/src/query/sql/src/executor/physical_plan_visitor.rs
index 6072f9cad1329..dfab899623cf7 100644
--- a/src/query/sql/src/executor/physical_plan_visitor.rs
+++ b/src/query/sql/src/executor/physical_plan_visitor.rs
@@ -338,7 +338,6 @@ pub trait PhysicalPlanReplacer {
     }
 
     fn replace_exchange(&mut self, plan: &Exchange) -> Result<PhysicalPlan> {
-        println!("replace_exchange trait");
         let input = self.replace(&plan.input)?;
 
         Ok(PhysicalPlan::Exchange(Exchange {
diff --git a/src/query/sql/src/planner/binder/ddl/table.rs b/src/query/sql/src/planner/binder/ddl/table.rs
index 219247b538dc8..47aa44e6a1327 100644
--- a/src/query/sql/src/planner/binder/ddl/table.rs
+++ b/src/query/sql/src/planner/binder/ddl/table.rs
@@ -16,6 +16,7 @@ use std::collections::BTreeMap;
 use std::collections::HashSet;
 use std::sync::Arc;
 
+use chrono::Utc;
 use databend_common_ast::ast::AddColumnOption as AstAddColumnOption;
 use databend_common_ast::ast::AlterTableAction;
 use databend_common_ast::ast::AlterTableStmt;
@@ -1141,22 +1142,20 @@ impl Binder {
 
             let partitions = settings.get_hilbert_num_range_ids()?;
             let sample_size = settings.get_hilbert_sample_size_per_block()?;
-            let keys_bounds_str = cluster_key_strs
-                .iter()
-                .map(|s| format!("range_bound({partitions}, {sample_size})({s}) AS {s}_bound"))
-                .collect::<Vec<_>>()
-                .join(", ");
-
-            let hilbert_keys_str = cluster_key_strs
-                .iter()
-                .map(|s| {
-                    format!(
-                        "hilbert_key(cast(ifnull(range_partition_id({table}.{s}, _keys_bound.{s}_bound), {}) as uint16))",
-                        partitions
-                    )
-                })
-                .collect::<Vec<_>>()
-                .join(", ");
+            let mut keys_bounds = Vec::with_capacity(cluster_key_strs.len());
+            let mut hilbert_keys = Vec::with_capacity(cluster_key_strs.len());
+            let suffix = format!("{:08x}", Utc::now().timestamp());
+            for (index, cluster_key_str) in cluster_key_strs.into_iter().enumerate() {
+                hilbert_keys.push(format!(
+                    "hilbert_key(cast(ifnull(range_partition_id({table}.{cluster_key_str}, \
+                    _keys_bound._bound{index}_{suffix}), {partitions}) as uint16))"
+                ));
+                keys_bounds.push(format!(
+                    "range_bound({partitions}, {sample_size})({cluster_key_str}) AS _bound{index}_{suffix}"
+                ));
+            }
+            let keys_bounds_str = keys_bounds.join(", ");
+            let hilbert_keys_str = hilbert_keys.join(", ");
 
             let quote = settings.get_sql_dialect()?.default_ident_quote();
             let schema = tbl.schema_with_stream();