Cluster assignment is a process used to assign data to clusters that have been generated by clustering methods such as K-means, DBSCAN, SOM, and GMM. The algorithm requires the cluster information or model to be saved, including the control parameters. For K-means, the distance between new data and cluster centers is calculated to assign the data to the cluster with the smallest distance. For DBSCAN, the algorithm looks for core objects within a certain distance of the new data to assign it to the corresponding cluster. If no core object is found, the data is assigned to cluster -1 as noise. For GMM, probabilities of the new data belonging to each cluster are calculated. For SOM, the distance between the new data and weight vectors is calculated to assign it to the cluster with the smallest distance.
------

SET SCHEMA DM_PAL;

DROP TABLE PAL_DBSCAN_DATA_TBL;
CREATE COLUMN TABLE PAL_DBSCAN_DATA_TBL (
	"ID" INTEGER, 
	"V1" DOUBLE, 
	"V2" DOUBLE,
	"V3" VARCHAR(10)
);
INSERT INTO PAL_DBSCAN_DATA_TBL VALUES(1, 0.10, 0.10,  'B');
INSERT INTO PAL_DBSCAN_DATA_TBL VALUES(2, 0.11, 0.10,  'A');
INSERT INTO PAL_DBSCAN_DATA_TBL VALUES(3, 0.10, 0.11,  'C');
INSERT INTO PAL_DBSCAN_DATA_TBL VALUES(4, 0.11, 0.11,  'B');
INSERT INTO PAL_DBSCAN_DATA_TBL VALUES(5, 0.12, 0.11,  'A');

DROP TABLE PAL_PARAMETER_TBL;
CREATE COLUMN TABLE PAL_PARAMETER_TBL (
	"PARAM_NAME" VARCHAR(100), 
	"INT_VALUE" INTEGER, 
	"DOUBLE_VALUE" DOUBLE, 
	"STRING_VALUE" VARCHAR(100)
);
INSERT INTO PAL_PARAMETER_TBL VALUES ('THREAD_RATIO', NULL, 0.2, NULL);
INSERT INTO PAL_PARAMETER_TBL VALUES ('AUTO_PARAM', NULL, NULL, 'true');
INSERT INTO PAL_PARAMETER_TBL VALUES ('DISTANCE_METHOD', 1, NULL, NULL);

DROP TABLE PAL_DBSCAN_MODEL_TBL;
CREATE COLUMN TABLE PAL_DBSCAN_MODEL_TBL (
	"ROW_INDEX" INTEGER, 
	"MODEL_CONTENT" NVARCHAR(5000)
); 

DO BEGIN
	LT_DATA = SELECT * FROM PAL_DBSCAN_DATA_TBL;
	LT_PARAM = SELECT * FROM PAL_PARAMETER_TBL;
	CALL _SYS_AFL.PAL_DBSCAN(:LT_DATA, :LT_PARAM, LT_RES, LT_MODEL, LT_STAT, LT_TMP);
	INSERT INTO PAL_DBSCAN_MODEL_TBL SELECT * FROM :LT_MODEL;
END;

DROP TABLE PAL_DBSCAN_DATA_TBL;
CREATE COLUMN TABLE PAL_DBSCAN_DATA_TBL (
	"ID" INTEGER, 
	"V1" DOUBLE, 
	"V2" DOUBLE,
	"V3" VARCHAR(10)
);

INSERT INTO PAL_DBSCAN_DATA_TBL VALUES (1, 0.10, 0.10, 'B');
INSERT INTO PAL_DBSCAN_DATA_TBL VALUES (2, 0.10, -2.10, 'A');
INSERT INTO PAL_DBSCAN_DATA_TBL VALUES (3, 3.10, 0.10, 'B');
INSERT INTO PAL_DBSCAN_DATA_TBL VALUES (4, 10.10, 10.10, 'D');
INSERT INTO PAL_DBSCAN_DATA_TBL VALUES (5, 3.10, -0.50, 'C');

CALL _SYS_AFL.PAL_CLUSTER_ASSIGNMENT(PAL_DBSCAN_DATA_TBL, PAL_DBSCAN_MODEL_TBL, PAL_PARAMETER_TBL,  ?);

