-
Notifications
You must be signed in to change notification settings - Fork 5
/
VerticalSlice.ecl
88 lines (76 loc) · 2.75 KB
/
VerticalSlice.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* ECL's TABLE() function has two modes. The more common mode is the
* "CrossTab Report" form, which is an aggregation. The other mode is a
* "vertical slice" mode, which is a way of extracting a subset of fields
* from a dataset. The "vertical slice" mode has a limitation that
* prevents you from citing a child dataset as one of the fields to
* include in the result.
*
* This function macro codifies the workaround for that limitation, at
* the expense of using a comma-delimited string instead of a record
* definition for the second argument for TABLE().
*
* An example is included at the end of this file.
*
* @param inFile The dataset to slice; REQUIRED
* @param fieldListStr A comma-delimited list of fields within
* inFile that you would like to extract;
* REQUIRED
*
* @return A copy of inFile containing only those fields referenced
* in fieldListStr.
*
* Origin: https://github.com/hpccsystems-solutions-lab/Useful_ECL
*/
VerticalSlice(inFile, fieldListStr) := FUNCTIONMACRO
#UNIQUENAME(needsDelim);
#UNIQUENAME(fieldName);
#UNIQUENAME(fieldNamePos);
#UNIQUENAME(trimmedFieldList);
LOCAL %trimmedFieldList% := TRIM((STRING)fieldListStr, ALL);
RETURN TABLE
(
inFile,
{
#SET(needsDelim, 0)
#SET(fieldNamePos, 1)
#LOOP
#SET(fieldName, REGEXFIND('^([^,]+)', %trimmedFieldList%[%fieldNamePos%..], 1))
#IF(%'fieldName'% != '')
#IF(%needsDelim% = 1) , #END
TYPEOF(inFile.%fieldName%) %fieldName% := %fieldName%
#SET(needsDelim, 1)
#SET(fieldNamePos, %fieldNamePos% + LENGTH(%'fieldName'%) + 1)
#ELSE
#BREAK
#END
#END
}
);
ENDMACRO;
/******************************************************************************
// Example:
ChildRec := {UNSIGNED1 age};
ParentRec := {STRING name, DATASET(ChildRec) ages};
ds0 := DATASET
(
5,
TRANSFORM
(
ParentRec,
SELF.name := 'blah',
SELF.ages := DATASET
(
2,
TRANSFORM
(
ChildRec,
SELF.age := RANDOM() % 100 + 1
)
)
)
);
ds := NOFOLD(ds0);
res := VerticalSlice(ds, 'ages'); // Would be TABLE(ds, {ages}) if that worked
OUTPUT(res);
******************************************************************************/