HAWQ-1767. enable new insert in univplan
diff --git a/depends/storage/src/storage/format/format.h b/depends/storage/src/storage/format/format.h
index 63559ee..97c9fa6 100644
--- a/depends/storage/src/storage/format/format.h
+++ b/depends/storage/src/storage/format/format.h
@@ -201,6 +201,11 @@
   void setUserCommand(std::string command) { userCommand = command; }
   virtual void setCancelled() {}
 
+  virtual void setupHasher(
+      const dbcommon::TupleDesc &td, const std::vector<uint64_t> &hashKeys,
+      const std::unordered_map<uint16_t, uint32_t> &r2rg,
+      const std::unordered_map<uint16_t, std::string> &r2u) {}
+
   static std::unique_ptr<Format> createFormat(
       univplan::UNIVPLANFORMATTYPE type);
   static std::unique_ptr<Format> createFormat(univplan::UNIVPLANFORMATTYPE type,
diff --git a/depends/univplan/src/univplan/cwrapper/univplan-c.cc b/depends/univplan/src/univplan/cwrapper/univplan-c.cc
index 0bce15e..df1e275 100644
--- a/depends/univplan/src/univplan/cwrapper/univplan-c.cc
+++ b/depends/univplan/src/univplan/cwrapper/univplan-c.cc
@@ -35,6 +35,7 @@
 #include "univplan/common/plannode-util.h"
 #include "univplan/common/stagize.h"
 #include "univplan/common/var-util.h"
+#include "univplan/univplanbuilder/univplanbuilder-connector.h"
 #include "univplan/univplanbuilder/univplanbuilder-expr-tree.h"
 #include "univplan/univplanbuilder/univplanbuilder-plan.h"
 #include "univplan/univplanbuilder/univplanbuilder-table.h"
@@ -93,7 +94,8 @@
                                    const char *optStrInJson, uint32_t columnNum,
                                    const char **columnName,
                                    int32_t *columnDataType,
-                                   int64_t *columnDataTypeMod) {
+                                   int64_t *columnDataTypeMod,
+                                   const char *targetName) {
   univplan::UnivPlanBuilderPlan *bld = up->upb->getPlanBuilderPlan();
   univplan::UnivPlanBuilderRangeTblEntry::uptr rte =
       bld->addRangeTblEntryAndGetBuilder();
@@ -113,6 +115,7 @@
       break;
     case FormatType::UnivPlanMagmaFormat:
       fmtType = univplan::MAGMA_FORMAT;
+      table->setTargetName(targetName);
       break;
     default:
       LOG_ERROR(ERRCODE_INTERNAL_ERROR,
@@ -673,6 +676,15 @@
       ->setInsertRelId(relId);
 }
 
+void univPlanInsertSetHasher(UnivPlanC *up, int32_t nDistKeyIndex,
+                             int16_t *distKeyIndex, int32_t nRanges,
+                             uint32_t *rangeToRgMap, int16_t nRg,
+                             uint16_t *rgIds, const char **rgUrls) {
+  dynamic_cast<univplan::UnivPlanBuilderInsert *>(up->curNode.get())
+      ->setInsertHasher(nDistKeyIndex, distKeyIndex, nRanges, rangeToRgMap, nRg,
+                        rgIds, rgUrls);
+}
+
 void univPlanAddToPlanNode(UnivPlanC *up, bool isLeft) {
   up->upb->addPlanNode(isLeft, std::move(up->curNode));
 }
diff --git a/depends/univplan/src/univplan/cwrapper/univplan-c.h b/depends/univplan/src/univplan/cwrapper/univplan-c.h
index ae7ddfe..e7d913d 100644
--- a/depends/univplan/src/univplan/cwrapper/univplan-c.h
+++ b/depends/univplan/src/univplan/cwrapper/univplan-c.h
@@ -70,7 +70,8 @@
                                    const char *optStrInJson, uint32_t columnNum,
                                    const char **columnName,
                                    int32_t *columnDataType,
-                                   int64_t *columnDataTypeMod);
+                                   int64_t *columnDataTypeMod,
+                                   const char *targetName);
 void univPlanRangeTblEntryAddDummy(UnivPlanC *up);
 
 // construct interconnect info
@@ -239,6 +240,11 @@
 // construct Insert
 int32_t univPlanInsertNewInstance(UnivPlanC *up, int32_t pid);
 void univPlanInsertSetRelId(UnivPlanC *up, uint32_t relId);
+// set magma table hasher when insert
+void univPlanInsertSetHasher(UnivPlanC *up, int32_t nDistKeyIndex,
+                             int16_t *distKeyIndex, int32_t nRanges,
+                             uint32_t *rangeToRgMap, int16_t nRg,
+                             uint16_t *rgIds, const char **rgUrls);
 
 void univPlanAddToPlanNode(UnivPlanC *up, bool isLeft);
 
diff --git a/depends/univplan/src/univplan/proto/universal-plan-catalog.proto b/depends/univplan/src/univplan/proto/universal-plan-catalog.proto
index eab5e16..830276a 100644
--- a/depends/univplan/src/univplan/proto/universal-plan-catalog.proto
+++ b/depends/univplan/src/univplan/proto/universal-plan-catalog.proto
@@ -54,9 +54,11 @@
 //
 
 message UnivPlanTable {
-	required int64					    tableId = 1;
-	required UNIVPLANFORMATTYPE	format = 2;
-	required string					    location = 3;
-	required bytes					    tableOptionsInJson = 4;
-	repeated UnivPlanColumn 		columns = 5;
+	required int64                 tableId = 1;
+	required UNIVPLANFORMATTYPE    format = 2;
+	required string                location = 3;
+	required bytes                 tableOptionsInJson = 4;
+	repeated UnivPlanColumn        columns = 5;
+	// for magma insert
+	optional string                targetName = 6;
 }
diff --git a/depends/univplan/src/univplan/proto/universal-plan.proto b/depends/univplan/src/univplan/proto/universal-plan.proto
index fb1cf95..625b21a 100644
--- a/depends/univplan/src/univplan/proto/universal-plan.proto
+++ b/depends/univplan/src/univplan/proto/universal-plan.proto
@@ -299,9 +299,18 @@
   repeated int32               uniqColIdxs = 2;
 }
 
+message UnivPlanMagmaHashInfo {
+  // hash info
+  repeated uint64     hashKeys = 1;
+  map<int32, uint32>  range2rg = 2;
+  map<uint32, string>      r2u = 3;
+}
+
 message UnivPlanInsert {
   required UnivPlanPlanNode    super = 1;
   required uint32 relId = 2;
+  // for magma insert hash
+  optional UnivPlanMagmaHashInfo hasher = 3;
 }
 
 ///////////////////////////////////////////////////////////////////////////////
diff --git a/depends/univplan/src/univplan/testutil/univplan-proto-util.cc b/depends/univplan/src/univplan/testutil/univplan-proto-util.cc
index 6bb0a76..7a1620f 100644
--- a/depends/univplan/src/univplan/testutil/univplan-proto-util.cc
+++ b/depends/univplan/src/univplan/testutil/univplan-proto-util.cc
@@ -497,7 +497,7 @@
 
   univPlanRangeTblEntryAddTable(univplan, 2, UnivPlanOrcFormat, location,
                                 fmtOptsJson, columnNum, columnName.get(),
-                                columnDataType, columnDataTypeMod);
+                                columnDataType, columnDataTypeMod, nullptr);
 }
 
 void UnivPlanProtoUtility::constructReceiver() {
diff --git a/depends/univplan/src/univplan/univplanbuilder/univplanbuilder-insert.cc b/depends/univplan/src/univplan/univplanbuilder/univplanbuilder-insert.cc
index a43aba8..91c915b 100644
--- a/depends/univplan/src/univplan/univplanbuilder/univplanbuilder-insert.cc
+++ b/depends/univplan/src/univplan/univplanbuilder/univplanbuilder-insert.cc
@@ -47,4 +47,23 @@
 
 void UnivPlanBuilderInsert::setInsertRelId(uint32_t id) { ref->set_relid(id); }
 
+void UnivPlanBuilderInsert::setInsertHasher(
+    int32_t nDistKeyIndex, int16_t *distKeyIndex, int32_t nRanges,
+    uint32_t *rangeToRgMap, int16_t nRg, uint16_t *rgIds, const char **rgUrls) {
+  for (int32_t i = 0; i < nDistKeyIndex; i++) {
+    if (distKeyIndex[i] > 0) {
+      ref->mutable_hasher()->mutable_hashkeys()->Add(distKeyIndex[i]);
+    }
+  }
+  if (ref->mutable_hasher()->mutable_hashkeys()->size() == 0) {
+    ref->mutable_hasher()->mutable_hashkeys()->Add(1);
+  }
+  for (int i = 0; i < nRanges; i++) {
+    (*ref->mutable_hasher()->mutable_range2rg())[i] = rangeToRgMap[i];
+  }
+  for (int16_t i = 0; i < nRg; i++) {
+    (*ref->mutable_hasher()->mutable_r2u())[rgIds[i]] = rgUrls[i];
+  }
+}
+
 }  // namespace univplan
diff --git a/depends/univplan/src/univplan/univplanbuilder/univplanbuilder-insert.h b/depends/univplan/src/univplan/univplanbuilder/univplanbuilder-insert.h
index 910891a..e5b262d 100644
--- a/depends/univplan/src/univplan/univplanbuilder/univplanbuilder-insert.h
+++ b/depends/univplan/src/univplan/univplanbuilder/univplanbuilder-insert.h
@@ -38,6 +38,10 @@
 
   void setInsertRelId(uint32_t id);
 
+  void setInsertHasher(int32_t nDistKeyIndex, int16_t *distKeyIndex,
+                       int32_t nRanges, uint32_t *rangeToRgMap, int16_t nRg,
+                       uint16_t *rgIds, const char **rgUrls);
+
  private:
   UnivPlanInsert *ref;
   std::unique_ptr<UnivPlanInsert> planNode;
diff --git a/depends/univplan/src/univplan/univplanbuilder/univplanbuilder-table.h b/depends/univplan/src/univplan/univplanbuilder/univplanbuilder-table.h
index e5e0b6b..32d6e03 100644
--- a/depends/univplan/src/univplan/univplanbuilder/univplanbuilder-table.h
+++ b/depends/univplan/src/univplan/univplanbuilder/univplanbuilder-table.h
@@ -47,6 +47,10 @@
     ref->set_location(location);
   }
 
+  void setTargetName(const std::string &targetName) {
+    ref->set_targetname(targetName);
+  }
+
   void setTableOptionsInJson(const std::string &optStr) {
     ref->set_tableoptionsinjson(optStr);
   }