-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBestRecordStructure.ecl
472 lines (428 loc) · 19.7 KB
/
BestRecordStructure.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/***
* Function macro that leverages DataPatterns to return a string defining the
* best ECL record structure for the input data.
*
* @param inFile The dataset to process; REQUIRED
* @param sampling A positive integer representing a percentage of
* inFile to examine, which is useful when analyzing a
* very large dataset and only an estimatation is
* sufficient; valid range for this argument is
* 1-100; values outside of this range will be
* clamped; OPTIONAL, defaults to 100 (which indicates
* that the entire dataset will be analyzed)
* @param emitTransform Boolean governing whether the function emits a
* TRANSFORM function that could be used to rewrite
* the dataset into the 'best' record definition;
* OPTIONAL, defaults to FALSE.
* @param textOutput Boolean governing the type of result that is
* delivered by this function; if FALSE then a
* recordset of STRINGs will be returned; if TRUE
* then a dataset with a single STRING field, with
* the contents formatted for HTML, will be
* returned (this is the ideal output if the
* intention is to copy the output from ECL Watch);
* OPTIONAL, defaults to FALSE
*
* @return A recordset defining the best ECL record structure for the data.
* If textOutput is FALSE (the default) then each record will contain
* one field declaration, and the list of declarations will be wrapped
* with RECORD and END strings; if the emitTransform argument was
* TRUE, there will also be a set of records that that comprise a
* stand-alone TRANSFORM function. If textOutput is TRUE then only
* one record will be returned, containing an HTML-formatted string
* containing the new field declarations (and optionally the
* TRANSFORM); this is the ideal format if the intention is to copy
* the result from ECL Watch.
*/
EXPORT BestRecordStructure(inFile, sampling = 100, emitTransform = FALSE, textOutput = FALSE) := FUNCTIONMACRO
LOADXML('<xml/>');
#EXPORTXML(bestInFileFields, RECORDOF(inFile));
#UNIQUENAME(bestFieldStack);
#UNIQUENAME(bestStructType);
#UNIQUENAME(bestLayoutType);
#UNIQUENAME(bestCapturedPos);
#UNIQUENAME(bestPrevCapturedPos);
#UNIQUENAME(bestLayoutName);
#UNIQUENAME(bestNeedsDelim);
#UNIQUENAME(bestNamePrefix);
#UNIQUENAME(recLevel);
IMPORT DataPatterns;
IMPORT Std;
// Attribute naming note: In order to reduce symbol collisions with calling
// code, all LOCAL attributes are prefixed with two underscore characters;
// normally, a #UNIQUENAME would be used instead, but there is apparently
// a problem with using that for ECL attributes when another function
// macro is called (namely, Profile); using double underscores is not an
// optimal solution but the chance of symbol collision should at least be
// reduced
LOCAL __DATAREC_NAME := 'DataRec';
LOCAL __LAYOUT_NAME := 'Layout';
LOCAL __StringRec := {STRING s};
// Helper function for determining if old and new data types need
// explicit type casting
LOCAL __NeedCoercion(STRING oldType, STRING newType) := FUNCTION
GenericType(STRING theType) := MAP
(
theType[..6] = 'string' => 'string',
theType[..13] = 'ebcdic string' => 'string',
theType[..7] = 'qstring' => 'string',
theType[..9] = 'varstring' => 'string',
theType[..3] = 'utf' => 'string',
theType[..7] = 'unicode' => 'string',
theType[..10] = 'varunicode' => 'string',
theType[..4] = 'data' => 'data',
theType[..7] = 'boolean' => 'boolean',
theType[..7] = 'integer' => 'numeric',
theType[..18] = 'big_endian integer' => 'numeric',
theType[..4] = 'real' => 'numeric',
theType[..7] = 'decimal' => 'numeric',
theType[..8] = 'udecimal' => 'numeric',
theType[..8] = 'unsigned' => 'numeric',
theType[..19] = 'big_endian unsigned' => 'numeric',
theType
);
oldGenericType := GenericType(Std.Str.ToLowerCase(oldType));
newGenericType := GenericType(Std.Str.ToLowerCase(newType));
RETURN oldGenericType != newGenericType;
END;
// Build a dataset containing information about embedded records and
// child datasets; we need to track the beginning and ending positions
// of the fields defined within those structures
LOCAL __ChildRecInfoLayout := RECORD
STRING layoutType;
STRING layoutName;
STRING fieldName;
UNSIGNED2 startPos;
UNSIGNED2 endPos;
END;
LOCAL __childRecInfo := DATASET
(
[
#SET(bestFieldStack, '')
#SET(bestNeedsDelim, 0)
#FOR(bestInFileFields)
#FOR(Field)
#IF(%{@isRecord}% = 1 OR %{@isDataset}% = 1)
#IF(%{@isRecord}% = 1)
#SET(bestStructType, 'r')
#ELSE
#SET(bestStructType, 'd')
#END
#IF(%'bestFieldStack'% != '')
#SET(bestFieldStack, ';' + %'bestFieldStack'%)
#END
#SET(bestFieldStack, %'bestStructType'% + ':' + %'@position'% + ':' + %'@ecltype'% + %'bestFieldStack'%)
#ELSEIF(%{@isEnd}% = 1)
#SET(bestLayoutType, %'bestFieldStack'%[1])
#SET(bestCapturedPos, REGEXFIND('.:(\\d+)', %'bestFieldStack'%, 1))
#SET(bestLayoutName, REGEXFIND('.:\\d+:([^;]+)', %'bestFieldStack'%, 1))
#SET(bestFieldStack, REGEXFIND('^[^;]+;(.*)', %'bestFieldStack'%, 1))
#IF(%bestNeedsDelim% = 1) , #END
{
%'bestLayoutType'%,
%'bestLayoutName'%,
%'@name'%,
%bestCapturedPos%,
%bestPrevCapturedPos%
}
#SET(bestNeedsDelim, 1)
#ELSE
#SET(bestPrevCapturedPos, %@position%)
#END
#END
#END
],
__ChildRecInfoLayout
);
// Extract the original data type and position of the fields within the
// input dataset
LOCAL __FieldInfoLayout := RECORD
STRING eclType;
STRING name;
STRING fullName;
BOOLEAN isRecord;
BOOLEAN isDataset;
UNSIGNED2 depth;
UNSIGNED2 position;
END;
LOCAL __fieldInfo0 := DATASET
(
[
#SET(bestFieldStack, '')
#SET(bestNeedsDelim, 0)
#SET(bestNamePrefix, '')
#SET(recLevel, 0)
#FOR(bestInFileFields)
#FOR(Field)
#IF(%@isEnd% != 1)
#IF(%bestNeedsDelim% = 1) , #END
{
%'@ecltype'%,
%'@name'%,
%'bestNamePrefix'% + %'@name'%,
#IF(%@isRecord% = 1) TRUE #ELSE FALSE #END,
#IF(%@isDataset% = 1) TRUE #ELSE FALSE #END,
%recLevel%,
%@position%
}
#SET(bestNeedsDelim, 1)
#END
#IF(%{@isRecord}% = 1 OR %{@isDataset}% = 1)
#APPEND(bestNamePrefix, %'@name'% + '.')
#SET(recLevel, %recLevel% + 1)
#ELSEIF(%{@isEnd}% = 1)
#SET(bestNamePrefix, REGEXREPLACE('\\w+\\.$', %'bestNamePrefix'%, ''))
#SET(recLevel, %recLevel% - 1)
#END
#END
#END
],
__FieldInfoLayout
);
// Attach the record end positions for embedded records and child datasets
LOCAL __fieldInfo10 := JOIN
(
__fieldInfo0,
__childRecInfo,
LEFT.name = RIGHT.fieldName AND LEFT.position = RIGHT.startPos,
TRANSFORM
(
{
RECORDOF(LEFT),
UNSIGNED2 endPosition
},
SELF.endPosition := RIGHT.endPos,
SELF := LEFT
),
LEFT OUTER
);
// Get the best data types from the Profile() function
LOCAL __patternRes := DataPatterns.Profile(inFile, features := 'best_ecl_types', sampleSize := sampling);
// Append the derived 'best' data types to the field information we
// already collected
LOCAL __fieldInfo15 := JOIN
(
__fieldInfo10,
__patternRes,
LEFT.fullName = RIGHT.attribute,
TRANSFORM
(
{
RECORDOF(LEFT),
STRING bestAttributeType
},
SELF.bestAttributeType := IF(RIGHT.best_attribute_type != '', Std.Str.ToUpperCase(RIGHT.best_attribute_type), LEFT.eclType),
SELF := LEFT
),
LEFT OUTER
);
// Determine fields that must have explicit coercion if we are supplying
// transform information
LOCAL __fieldInfo20 := PROJECT
(
__fieldInfo15,
TRANSFORM
(
{
RECORDOF(LEFT),
STRING bestAssignment
},
shouldRewriteType := ((LEFT.isDataset OR LEFT.isRecord) AND LEFT.bestAttributeType IN ['<unnamed>', 'table of <unnamed>']);
tempDSName := __DATAREC_NAME + '_' + INTFORMAT(COUNTER, 4, 1);
SELF.eclType := IF(NOT shouldRewriteType, Std.Str.ToUpperCase(LEFT.eclType), tempDSName),
SELF.bestAttributeType := IF(NOT shouldRewriteType, LEFT.bestAttributeType, tempDSName),
SELF.bestAssignment := IF
(
__NeedCoercion(SELF.eclType, SELF.bestAttributeType),
' SELF.' + LEFT.name + ' := (' + Std.Str.ToUppercase(SELF.bestAttributeType) + ')r.' + LEFT.name + ';',
''
),
SELF := LEFT
)
);
LOCAL __LayoutItems := RECORD(__StringRec)
STRING fullName {DEFAULT('')};
STRING bestAssignment {DEFAULT('')};
END;
LOCAL __ChildRecLayout := RECORD
STRING layoutName;
UNSIGNED2 startPos;
UNSIGNED2 endPos;
UNSIGNED2 depth;
DATASET(__LayoutItems) items;
END;
// Function for creating ECL TRANSFORM assignment statements
LOCAL __MakeRecDefinition(DATASET(RECORDOF(__fieldInfo20)) ds, STRING layoutName, BOOLEAN useBest = TRUE) := FUNCTION
displayPrefix := IF(useBest, 'New', 'Old');
displayedLayoutName := displayPrefix + layoutName;
RETURN (+)
(
DATASET([{displayedLayoutName + ' := RECORD'}], __LayoutItems),
PROJECT
(
SORT(DISTRIBUTE(ds, 0), position, LOCAL),
TRANSFORM
(
__LayoutItems,
attrType := IF(useBest, LEFT.bestAttributeType, LEFT.eclType);
attrPrefix := IF(LEFT.isDataset OR LEFT.isRecord, displayPrefix, '');
fullAttrType := attrPrefix + attrType;
namedDataType := IF(NOT LEFT.isDataset, fullAttrType, 'DATASET(' + fullAttrType + ')');
SELF.s := ' ' + namedDataType + ' ' + LEFT.name + ';',
SELF.bestAssignment := MAP
(
LEFT.bestAssignment != '' => LEFT.bestAssignment,
LEFT.isRecord => ' SELF.' + LEFT.name + ' := ROW(Make_' + fullAttrType + '(r.' + LEFT.name + '));',
LEFT.isDataset => ' SELF.' + LEFT.name + ' := PROJECT(r.' + LEFT.name + ', Make_' + fullAttrType + '(LEFT));',
''
),
SELF := LEFT
)
),
DATASET([{'END;'}], __LayoutItems),
ORDERED(TRUE)
);
END;
// Iteratively process embedded records and child dataset definitions,
// extracting each into its own record
LOCAL __ProcessChildRecs(DATASET(__ChildRecLayout) layoutDS, UNSIGNED2 aDepth, BOOLEAN useBest = TRUE) := FUNCTION
__bestNamedChildRecs := DENORMALIZE
(
__fieldInfo20(depth = (aDepth - 1) AND (isRecord OR isDataset)),
__fieldInfo20(depth = aDepth),
RIGHT.position BETWEEN LEFT.position + 1 AND LEFT.endPosition,
GROUP,
TRANSFORM
(
__ChildRecLayout,
SELF.layoutName := LEFT.bestAttributeType,
SELF.items := __MakeRecDefinition(ROWS(RIGHT), SELF.layoutName, useBest),
SELF.startPos := LEFT.position,
SELF.endPos := LEFT.endPosition,
SELF.depth := aDepth,
SELF := LEFT
),
ALL, ORDERED(TRUE)
) : ONWARNING(4531, IGNORE);
RETURN layoutDS + __bestNamedChildRecs;
END;
// Create a list of embedded records and child dataset definitions for the
// original input dataset
LOCAL __oldNamedChildRecs0 := LOOP
(
DATASET([], __ChildRecLayout),
MAX(__fieldInfo20, depth),
__ProcessChildRecs(ROWS(LEFT), MAX(__fieldInfo20, depth) + 1 - COUNTER, FALSE)
);
LOCAL __oldNamedChildRecs := SORT(__oldNamedChildRecs0, endPos, -startPos);
LOCAL __topLevelOldRecDef := DATASET
(
[
{
__LAYOUT_NAME,
0,
0,
0,
__MakeRecDefinition(__fieldInfo20(depth = 0), __LAYOUT_NAME, FALSE)
}
],
__ChildRecLayout
);
LOCAL __allOldRecDefs := __oldNamedChildRecs & __topLevelOldRecDef;
// Create a list of embedded records and child dataset definitions using the
// the recommended ECL datatypes
LOCAL __bestNamedChildRecs0 := LOOP
(
DATASET([], __ChildRecLayout),
MAX(__fieldInfo20, depth),
__ProcessChildRecs(ROWS(LEFT), MAX(__fieldInfo20, depth) + 1 - COUNTER, TRUE)
);
LOCAL __bestNamedChildRecs := SORT(__bestNamedChildRecs0, endPos, -startPos);
LOCAL __topLevelBestRecDef := DATASET
(
[
{
__LAYOUT_NAME,
0,
0,
0,
__MakeRecDefinition(__fieldInfo20(depth = 0), __LAYOUT_NAME, TRUE)
}
],
__ChildRecLayout
);
LOCAL __allBestRecDefs := __bestNamedChildRecs & __topLevelBestRecDef;
// Creates an ECL TRANSFORM function based on the collected information
// about a record definition
LOCAL __MakeTransforms(__ChildRecLayout recInfo) := FUNCTION
RETURN (+)
(
DATASET(['New' + recInfo.layoutName + ' Make_New' + recInfo.layoutName + '(Old' + recInfo.layoutName + ' r) := TRANSFORM'], __StringRec),
PROJECT
(
DISTRIBUTE(recInfo.items, 0),
TRANSFORM
(
__StringRec,
assignment := LEFT.bestAssignment;
SELF.s := IF(assignment != '', assignment, SKIP)
)
),
DATASET([' SELF := r;'], __StringRec),
DATASET(['END;'], __StringRec),
ORDERED(TRUE)
);
END;
LOCAL __allTransforms := PROJECT
(
__allBestRecDefs,
TRANSFORM
(
{
DATASET(__StringRec) lines
},
SELF.lines := __MakeTransforms(LEFT)
)
);
// Create a dataset of STRINGS that contain record definitions for the
// input dataset, TRANSFORMs for converting between the old and new
// definitions, and a sample PROJECT for kicking it all off
LOCAL __conditionalBR := #IF((BOOLEAN)textOutput) '<br/>' #ELSE '' #END;
LOCAL __oldRecDefsPlusTransforms := (+)
(
DATASET(['//----------' + __conditionalBR], __StringRec),
PROJECT(__allOldRecDefs.items, __StringRec),
DATASET(['//----------' + __conditionalBR], __StringRec),
__allTransforms.lines,
DATASET(['//----------' + __conditionalBR], __StringRec),
DATASET(['oldDS := DATASET([], OldLayout);' + __conditionalBR], __StringRec),
DATASET(['newDS := PROJECT(oldDS, Make_NewLayout(LEFT));' + __conditionalBR], __StringRec),
ORDERED(TRUE)
);
// Combine old definitions and transforms conditionally
LOCAL __conditionalOldStuff :=
#IF((BOOLEAN)emitTransform)
__oldRecDefsPlusTransforms
#ELSE
DATASET([], __StringRec)
#END;
LOCAL __allOutput := PROJECT(__allBestRecDefs.items, __StringRec) & __conditionalOldStuff;
// Roll everything up to one string with HTML line breaks
LOCAL __htmlString := ROLLUP
(
__allOutput,
TRUE,
TRANSFORM
(
RECORDOF(LEFT),
rightString := IF(RIGHT.s = 'END;', RIGHT.s + '<br/>', RIGHT.s);
SELF.s := LEFT.s + '<br/>' + rightString
)
);
// Stuff the HTML result into a single record, wrapped with <pre> so it
// looks right in the browser
LOCAL __htmlResult := DATASET(['<pre>' + __htmlString[1].s + '</pre>'], {STRING result__html});
// Choose the result (dataset with each line a string, or a text blob)
LOCAL __finalResult := #IF((BOOLEAN)textOutput) __htmlResult #ELSE __allOutput #END;
RETURN __finalResult;
ENDMACRO;