-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathConcatFieldValues.ecl
65 lines (63 loc) · 2.74 KB
/
ConcatFieldValues.ecl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* Function concatenates the values within a field from every record in the
* input dataset. This is similar to using the standard library function
* Std.Str.CombineWords(SET(inFile, inField), delim) but is not confined to
* only STRING input and output.
*
* Callers can mandate the result datatype, and care should be taken that the
* result datatype is string- or data-like (in other words, a datatype that
* actually allows concatenation). In addition, input values are cast to
* that datatype so callers should be aware of type casting rules.
*
* Obviously, some care should be taken with very large datasets. It is
* entirely possible to run out of memory if there are many inFile records.
*
* @param inFile The dataset to process; REQUIRED
* @param outFieldType The datatype of the result; this is not a string;
* REQUIRED
* @param inField The name of the field within inFile whose values
* will be concatenated; this is not a string;
* REQUIRED
* @param delim The delimiter to use between values in the final
* result; will be type cast to outFieldType;
* OPTIONAL, defaults to an empty STRING
* @param doTrim If TRUE, perform a TRIM(LEFT, RIGHT) on the inField
* values; note that inField's datatype needs to be
* able to support a TRIM() call -- so, a STRING,
* UNICODE, or UTF8 type; OPTIONAL, defaults to FALSE
*
* @return A scalar value of type outFieldType containing all of the values
* from the inFile.inField, with the value of delim as a separator,
* in record order.
*
* Origin: https://github.com/hpccsystems-solutions-lab/Useful_ECL
*/
ConcatFieldValues(inFile, outFieldType, inField, delim = '\'\'', doTrim = FALSE) := FUNCTIONMACRO
#UNIQUENAME(outField);
LOCAL onlyFieldData := PROJECT
(
inFile,
TRANSFORM
(
{
outFieldType %outField%
},
#IF((BOOLEAN)doTrim)
SELF.%outField% := TRIM((outFieldType)LEFT.inField, LEFT, RIGHT)
#ELSE
SELF.%outField% := (outFieldType)LEFT.inField
#END
)
);
LOCAL rolledUpData := ROLLUP
(
onlyFieldData,
TRUE,
TRANSFORM
(
RECORDOF(LEFT),
SELF.%outField% := LEFT.%outField% + (outFieldType)delim + RIGHT.%outField%
)
);
RETURN rolledUpData[1].%outField%;
ENDMACRO;