From 1949801f0adc3773e14bb90f7f0f7cc461976a87 Mon Sep 17 00:00:00 2001
From: Serhii Filonenko <91055067+serhii-filonenko@users.noreply.github.com>
Date: Thu, 2 May 2024 20:17:07 +0300
Subject: [PATCH] HCK-5631: fix handle of multiple sample data for map
 properties (#127)

* RE: fix handle of multiple sample data for map properties

* fix formatting
---
 .../helpers/filterComplexUdt.js                | 11 ++++++++++-
 reverse_engineering/typesHelper.js             | 18 ++++++++++++++++--
 2 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/reverse_engineering/helpers/filterComplexUdt.js b/reverse_engineering/helpers/filterComplexUdt.js
index 9416493..8fe88fc 100644
--- a/reverse_engineering/helpers/filterComplexUdt.js
+++ b/reverse_engineering/helpers/filterComplexUdt.js
@@ -24,6 +24,15 @@ module.exports = (_) => {
 
 		return handlerRecords.some(record => record === undefined) ? [] : handlerRecords; 
 	};
+
+	const handleMap = ({ properties, records }) => {
+		return Object.entries(properties).reduce((result, [propertyName, propertyValue]) => {
+			return {
+				...result,
+				[propertyName]: filterUdt(propertyValue?.properties, records?.[propertyName])
+			}
+		}, {});
+	};
 	
 	const filterUdt = (properties, records) => {
 		if (!_.isPlainObject(records)) {
@@ -45,7 +54,7 @@ module.exports = (_) => {
 			} else if (type === 'map' && propertyValue.properties) {
 				return {
 					...records,
-					[recordName]: filterUdt(propertyValue.properties, recordValues),
+					[recordName]: handleMap({ properties: propertyValue.properties, records: recordValues }),
 				}
 			}
 	
diff --git a/reverse_engineering/typesHelper.js b/reverse_engineering/typesHelper.js
index 4018f1a..1dab0ba 100644
--- a/reverse_engineering/typesHelper.js
+++ b/reverse_engineering/typesHelper.js
@@ -51,7 +51,7 @@ module.exports = (_) => {
 		const keyData = (column.info || column.type.info)[0];
 		const valueData = (column.info || column.type.info)[1];
 		const handledKeyData = getColumnType(keyData);
-		const properties = getProperties(valueData, sample, udtHash);
+		const properties = getMapProperties({ valueData, sample, udtHash });
 		const keySubtype = handledKeyData.mode
 			? { keySubtype: handledKeyData.mode }
 			: {};
@@ -176,10 +176,24 @@ module.exports = (_) => {
 		const name = handledValueData.refName || defaultColumnName;
 
 		return {
-			[name]: _.omit(handledValueData, 'refName')
+			[name]: _.omit(handledValueData, 'refName'),
 		};
 	};
 
+	const getMapProperties = ({ valueData, sample, udtHash }) => {
+		const properties = getProperties(valueData, sample, udtHash);
+		const propertyPairs = Object.entries(properties);
+		const uniqueProperties = _.uniqWith(
+			propertyPairs,
+			([, propertyA], [, propertyB]) =>
+				propertyA.type === propertyB.type &&
+				propertyA.$ref === propertyB.$ref &&
+				propertyA.mode === propertyB.mode
+		);
+
+		return Object.fromEntries(uniqueProperties);
+	};
+
 	const getJsonType = (type) => {
 		switch (type) {
 			case "smallint":