forked from hpcc-systems/HPCC-Platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thorcommon.cpp
1675 lines (1511 loc) · 50 KB
/
thorcommon.cpp
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*##############################################################################
HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
############################################################################## */
#include "jexcept.hpp"
#include "jmisc.hpp"
#include "jthread.hpp"
#include "jsocket.hpp"
#include "jprop.hpp"
#include "jdebug.hpp"
#include "jlzw.hpp"
#include "junicode.hpp"
#include "eclhelper.hpp"
#include "thorcommon.ipp"
#include "eclrtl.hpp"
#include "rtlread_imp.hpp"
#include "thorstep.hpp"
#define ROWAGG_PERROWOVERHEAD (sizeof(AggregateRowBuilder))
RowAggregator::RowAggregator(IHThorHashAggregateExtra &_extra, IHThorRowAggregator & _helper) : helper(_helper)
{
comparer = _extra.queryCompareRowElement();
hasher = _extra.queryHash();
elementHasher = _extra.queryHashElement();
elementComparer = _extra.queryCompareElements();
cursor = NULL;
eof = false;
totalSize = overhead = 0;
}
RowAggregator::~RowAggregator()
{
reset();
}
void RowAggregator::start(IEngineRowAllocator *_rowAllocator)
{
rowAllocator.set(_rowAllocator);
}
void RowAggregator::reset()
{
while (!eof)
{
AggregateRowBuilder *n = nextResult();
if (n)
n->Release();
}
SuperHashTable::releaseAll();
eof = false;
cursor = NULL;
rowAllocator.clear();
totalSize = overhead = 0;
}
AggregateRowBuilder &RowAggregator::addRow(const void * row)
{
AggregateRowBuilder *result;
unsigned hash = hasher->hash(row);
void * match = find(hash, row);
if (match)
{
result = static_cast<AggregateRowBuilder *>(match);
totalSize -= result->querySize();
size32_t sz = helper.processNext(*result, row);
result->setSize(sz);
totalSize += sz;
}
else
{
Owned<AggregateRowBuilder> rowBuilder = new AggregateRowBuilder(rowAllocator, hash);
helper.clearAggregate(*rowBuilder);
size32_t sz = helper.processFirst(*rowBuilder, row);
rowBuilder->setSize(sz);
result = rowBuilder.getClear();
addNew(result, hash);
totalSize += sz;
overhead += ROWAGG_PERROWOVERHEAD;
}
return *result;
}
void RowAggregator::mergeElement(const void * otherElement)
{
unsigned hash = elementHasher->hash(otherElement);
void * match = findElement(hash, otherElement);
if (match)
{
AggregateRowBuilder *rowBuilder = static_cast<AggregateRowBuilder *>(match);
totalSize -= rowBuilder->querySize();
size32_t sz = helper.mergeAggregate(*rowBuilder, otherElement);
rowBuilder->setSize(sz);
totalSize += sz;
}
else
{
Owned<AggregateRowBuilder> rowBuilder = new AggregateRowBuilder(rowAllocator, hash);
rowBuilder->setSize(cloneRow(*rowBuilder, otherElement, rowAllocator->queryOutputMeta()));
addNew(rowBuilder.getClear(), hash);
}
}
const void * RowAggregator::getFindParam(const void *et) const
{
// Slightly odd name for this function... it actually gets the comparable element
const AggregateRowBuilder *rb = static_cast<const AggregateRowBuilder*>(et);
return rb->row();
}
bool RowAggregator::matchesFindParam(const void *et, const void *key, unsigned fphash) const
{
if (fphash != hashFromElement(et))
return false;
// et = element in the table (an AggregateRowBuilder) key = new row (in input row layout).
return comparer->docompare(key, getFindParam(et)) == 0;
}
bool RowAggregator::matchesElement(const void *et, const void * searchET) const
{
return elementComparer->docompare(getFindParam(et), searchET) == 0;
}
AggregateRowBuilder *RowAggregator::nextResult()
{
void *ret = next(cursor);
if (!ret)
{
eof = true;
return NULL;
}
cursor = ret;
return static_cast<AggregateRowBuilder *>(ret);
}
//=====================================================================================================
void CStreamMerger::fillheap(const void * seek, unsigned numFields, const SmartStepExtra * stepExtra)
{
assertex(activeInputs == 0);
for(unsigned i = 0; i < numInputs; i++)
if(pullInput(i, seek, numFields, stepExtra))
mergeheap[activeInputs++] = i;
}
void CStreamMerger::permute(const void * seek, unsigned numFields, const SmartStepExtra * stepExtra)
{
// the tree structure: element p has children p*2+1 and p*2+2, or element c has parent (unsigned)(c-1)/2
// the heap property: no element should be smaller than its parent
// the dedup variant: if(dedup), the top of the heap should also not be equal to either child
// the method: establish this by starting with the parent of the bottom element and working up to the top element, sifting each down to its correct place
if (activeInputs >= 2)
for(unsigned p = (activeInputs-2)/2; p > 0; --p)
siftDown(p);
if(dedup)
siftDownDedupTop(seek, numFields, stepExtra);
else
siftDown(0);
}
const void * CStreamMerger::consumeTop()
{
unsigned top = mergeheap[0];
if (!pullConsumes)
consumeInput(top);
const void *next = pending[top];
pending[top] = NULL;
return next;
}
bool CStreamMerger::ensureNext(const void * seek, unsigned numFields, bool & wasCompleteMatch, const SmartStepExtra * stepExtra)
{
//wasCompleteMatch must be initialised from the actual row returned. (See bug #30388)
if (first)
{
fillheap(seek, numFields, stepExtra);
permute(seek, numFields, stepExtra);
first = false;
if (activeInputs == 0)
return false;
unsigned top = mergeheap[0];
wasCompleteMatch = pendingMatches[top];
return true;
}
while (activeInputs)
{
unsigned top = mergeheap[0];
const void *next = pending[top];
if (next)
{
if (seek)
{
int c = rangeCompare->docompare(next, seek, numFields);
if (c >= 0)
{
if (stepExtra->returnMismatches() && (c > 0))
{
wasCompleteMatch = pendingMatches[top];
return true;
}
else
{
if (pendingMatches[top])
return true;
}
}
}
else
{
if (pendingMatches[top])
return true;
}
skipInput(top);
}
if(!pullInput(top, seek, numFields, stepExtra))
if(!promote(0))
return false;
// we have changed the element at the top of the heap, so need to sift it down to maintain the heap property
if(dedup)
siftDownDedupTop(seek, numFields, stepExtra);
else
siftDown(0);
}
return false;
}
bool CStreamMerger::ensureNext()
{
bool isCompleteMatch = true;
return ensureNext(NULL, 0, isCompleteMatch, NULL);
}
void CStreamMerger::permute()
{
permute(NULL, 0, NULL);
}
bool CStreamMerger::promote(unsigned p)
{
activeInputs--;
if(activeInputs == p)
return false;
mergeheap[p] = mergeheap[activeInputs];
return true;
}
void CStreamMerger::siftDownDedupTop(const void * seek, unsigned numFields, const SmartStepExtra * stepExtra)
{
// same as siftDown(0), except that it also ensures that the top of the heap is not equal to either of its children
if(activeInputs < 2)
return;
unsigned c = 1;
int childcmp = 1;
if(activeInputs >= 3)
{
childcmp = compare->docompare(pending[mergeheap[2]], pending[mergeheap[1]]);
if(childcmp < 0)
c = 2;
}
int cmp = compare->docompare(pending[mergeheap[c]], pending[mergeheap[0]]);
if(cmp > 0)
return;
// the following loop ensures the correct property holds on the smaller branch, and that childcmp==0 iff the top matches the other branch
while(cmp <= 0)
{
if(cmp == 0)
{
if(mergeheap[c] < mergeheap[0])
{
unsigned r = mergeheap[c];
mergeheap[c] = mergeheap[0];
mergeheap[0] = r;
}
unsigned top = mergeheap[c];
skipInput(top);
if(!pullInput(top, seek, numFields, stepExtra))
if(!promote(c))
break;
siftDown(c);
}
else
{
unsigned r = mergeheap[c];
mergeheap[c] = mergeheap[0];
mergeheap[0] = r;
if(siftDown(c))
break;
}
cmp = compare->docompare(pending[mergeheap[c]], pending[mergeheap[0]]);
}
// the following loop ensures the uniqueness property holds on the other branch too
c = 3-c;
if(activeInputs <= c)
return;
while(childcmp == 0)
{
if(mergeheap[c] < mergeheap[0])
{
unsigned r = mergeheap[c];
mergeheap[c] = mergeheap[0];
mergeheap[0] = r;
}
unsigned top = mergeheap[c];
skipInput(top);
if(!pullInput(top, seek, numFields, stepExtra))
if(!promote(c))
break;
siftDown(c);
childcmp = compare->docompare(pending[mergeheap[c]], pending[mergeheap[0]]);
}
}
void CStreamMerger::cleanup()
{
clearPending();
delete [] pending;
pending = NULL;
delete [] pendingMatches;
pendingMatches = NULL;
delete [] mergeheap;
mergeheap = NULL;
}
void CStreamMerger::clearPending()
{
if (pending && activeInputs)
{
for(unsigned i = 0; i < numInputs; i++)
{
if (pullConsumes)
releaseRow(pending[i]);
pending[i] = NULL;
}
activeInputs = 0;
}
first = true;
}
CStreamMerger::CStreamMerger(bool _pullConsumes)
{
pending = NULL;
pendingMatches = NULL;
mergeheap = NULL;
compare = NULL;
rangeCompare = NULL;
dedup = false;
activeInputs = 0;
pullConsumes = _pullConsumes;
numInputs = 0;
first = true;
}
CStreamMerger::~CStreamMerger()
{
//can't call cleanup() because virtual releaseRow() won't be defined.
// NOTE: use assert rather than assertex as exceptions from within destructors are not handled well.
assert(!pending && !mergeheap);
}
void CStreamMerger::init(ICompare * _compare, bool _dedup, IRangeCompare * _rangeCompare)
{
compare = _compare;
dedup = _dedup;
rangeCompare = _rangeCompare;
}
void CStreamMerger::initInputs(unsigned _numInputs)
{
assertex(!pending); // cleanup should have been called before reinitializing
numInputs = _numInputs;
mergeheap = new unsigned[numInputs];
pending = new const void *[numInputs];
pendingMatches = new bool [numInputs];
for (unsigned i = 0; i < numInputs; i++)
pending[i] = NULL;
activeInputs = 0;
first = true;
}
void CStreamMerger::consumeInput(unsigned i)
{
//should be over-ridden if pullConsumes is false;
throwUnexpected();
}
void CStreamMerger::skipInput(unsigned i)
{
if (!pullConsumes)
consumeInput(i);
releaseRow(pending[i]);
pending[i] = NULL;
}
void CStreamMerger::primeRows(const void * * rows)
{
assertex(first && (activeInputs == 0));
first = false;
for(unsigned i = 0; i < numInputs; i++)
{
if ((pending[i] = rows[i]) != NULL)
{
mergeheap[activeInputs++] = i;
pendingMatches[i] = true;
}
}
permute();
}
const void * CStreamMerger::nextRow()
{
if (ensureNext())
return consumeTop();
return NULL;
}
const void * CStreamMerger::queryNextRow()
{
if (ensureNext())
return pending[mergeheap[0]];
return NULL;
}
unsigned CStreamMerger::queryNextInput()
{
if (ensureNext())
return mergeheap[0];
return NotFound;
}
const void * CStreamMerger::nextRowGE(const void * seek, unsigned numFields, bool & wasCompleteMatch, const SmartStepExtra & stepExtra)
{
if (ensureNext(seek, numFields, wasCompleteMatch, &stepExtra))
return consumeTop();
return NULL;
}
void CStreamMerger::skipRow()
{
assertex(!first);
skipInput(mergeheap[0]);
}
//=====================================================================================================
CThorDemoRowSerializer::CThorDemoRowSerializer(MemoryBuffer & _buffer) : buffer(_buffer)
{
nesting = 0;
}
void CThorDemoRowSerializer::put(size32_t len, const void * ptr)
{
buffer.append(len, ptr);
//ok to flush if nesting == 0;
}
size32_t CThorDemoRowSerializer::beginNested()
{
nesting++;
unsigned pos = buffer.length();
buffer.append((size32_t)0);
return pos;
}
void CThorDemoRowSerializer::endNested(size32_t sizePos)
{
unsigned pos = buffer.length();
buffer.rewrite(sizePos);
buffer.append((size32_t)(pos - (sizePos + sizeof(size32_t))));
buffer.rewrite(pos);
nesting--;
}
IOutputRowSerializer * CachedOutputMetaData::createRowSerializer(ICodeContext * ctx, unsigned activityId) const
{
if (metaFlags & (MDFhasserialize|MDFneedserialize))
return meta->createRowSerializer(ctx, activityId);
if (isFixedSize())
return new CSimpleFixedRowSerializer(getFixedSize());
return new CSimpleVariableRowSerializer(this);
}
IOutputRowDeserializer * CachedOutputMetaData::createRowDeserializer(ICodeContext * ctx, unsigned activityId) const
{
if (metaFlags & (MDFhasserialize|MDFneedserialize))
return meta->createRowDeserializer(ctx, activityId);
if (isFixedSize())
return new CSimpleFixedRowDeserializer(getFixedSize());
assertex(!"createRowDeserializer variable meta has no serializer");
//return new CSimpleVariableRowDeserializer(this);
return NULL;
}
void CSizingSerializer::put(size32_t len, const void * ptr)
{
totalsize += len;
}
size32_t CSizingSerializer::beginNested()
{
totalsize += sizeof(size32_t);
return totalsize;
}
void CSizingSerializer::endNested(size32_t position)
{
}
void CMemoryRowSerializer::put(size32_t len, const void * ptr)
{
buffer.append(len, ptr);
}
size32_t CMemoryRowSerializer::beginNested()
{
nesting++;
unsigned pos = buffer.length();
buffer.append((size32_t)0);
return pos;
}
void CMemoryRowSerializer::endNested(size32_t sizePos)
{
size32_t sz = buffer.length()-(sizePos + sizeof(size32_t));
buffer.writeDirect(sizePos,sizeof(sz),&sz);
nesting--;
}
static void ensureClassesAreNotAbstract()
{
MemoryBuffer temp;
CThorStreamDeserializerSource x1(NULL);
CThorContiguousRowBuffer x2(NULL);
CSizingSerializer x3;
CMemoryRowSerializer x4(temp);
}
//=====================================================================================================
class ChildRowLinkerWalker : implements IIndirectMemberVisitor
{
public:
virtual void visitRowset(size32_t count, byte * * rows)
{
rtlLinkRowset(rows);
}
virtual void visitRow(const byte * row)
{
rtlLinkRow(row);
}
};
//the visitor callback is used to ensure link counts for children are updated.
size32_t cloneRow(ARowBuilder & rowBuilder, const void * row, IOutputMetaData * meta)
{
size32_t rowSize = meta->getRecordSize(row); // TBD could be better?
byte * self = rowBuilder.ensureCapacity(rowSize, NULL);
memcpy(self, row, rowSize);
if (meta->getMetaFlags() & MDFneedserialize)
{
ChildRowLinkerWalker walker;
meta->walkIndirectMembers(self, walker);
}
return rowSize;
}
//---------------------------------------------------------------------------------------------------
extern const char * getActivityText(ThorActivityKind kind)
{
switch (kind)
{
case TAKnone: return "None";
case TAKdiskwrite: return "Disk Write";
case TAKsort: return "Sort";
case TAKdedup: return "Dedup";
case TAKfilter: return "Filter";
case TAKsplit: return "Split";
case TAKproject: return "Project";
case TAKrollup: return "Rollup";
case TAKiterate: return "Iterate";
case TAKaggregate: return "Aggregate";
case TAKhashaggregate: return "Hash Aggregate";
case TAKfirstn: return "Firstn";
case TAKsample: return "Sample";
case TAKdegroup: return "Degroup";
case TAKjoin: return "Join";
case TAKhashjoin: return "Hash Join";
case TAKlookupjoin: return "Lookup Join";
case TAKselfjoin: return "Self Join";
case TAKkeyedjoin: return "Keyed Join";
case TAKgroup: return "Group";
case TAKworkunitwrite: return "Output";
case TAKfunnel: return "Funnel";
case TAKapply: return "Apply";
case TAKtemptable: return "Inline Dataset";
case TAKinlinetable: return "Inline Dataset";
case TAKtemprow: return "Inline Row";
case TAKhashdistribute: return "Hash Distribute";
case TAKhashdedup: return "Hash Dedup";
case TAKnormalize: return "Normalize";
case TAKremoteresult: return "Remote Result";
case TAKpull: return "Pull";
case TAKdenormalize: return "Denormalize";
case TAKnormalizechild: return "Normalize Child";
case TAKchilddataset: return "Child Dataset";
case TAKselectn: return "Select Nth";
case TAKenth: return "Enth";
case TAKif: return "If";
case TAKnull: return "Null";
case TAKdistribution: return "Distribution";
case TAKcountproject: return "Count Project";
case TAKchoosesets: return "Choose Sets";
case TAKpiperead: return "Pipe Read";
case TAKpipewrite: return "Pipe Write";
case TAKcsvwrite: return "Csv Write";
case TAKpipethrough: return "Pipe Through";
case TAKindexwrite: return "Index Write";
case TAKchoosesetsenth: return "Choose Sets Enth";
case TAKchoosesetslast: return "Choose Sets Last";
case TAKfetch: return "Fetch";
case TAKhashdenormalize: return "Hash Denormalize";
case TAKworkunitread: return "Read";
case TAKthroughaggregate: return "Through Aggregate";
case TAKspill: return "Spill";
case TAKcase: return "Case";
case TAKlimit: return "Limit";
case TAKcsvfetch: return "Csv Fetch";
case TAKxmlwrite: return "Xml Write";
case TAKparse: return "Parse";
case TAKcountdisk: return "Count Disk";
case TAKsideeffect: return "Simple Action";
case TAKtopn: return "Top N";
case TAKmerge: return "Merge";
case TAKxmlfetch: return "Xml Fetch";
case TAKxmlparse: return "Parse Xml";
case TAKkeyeddistribute: return "Keyed Distribute";
case TAKjoinlight: return "Lightweight Join";
case TAKalljoin: return "All Join";
case TAKsoap_rowdataset: return "SOAP dataset";
case TAKsoap_rowaction: return "SOAP action";
case TAKsoap_datasetdataset: return "SOAP dataset";
case TAKsoap_datasetaction: return "SOAP action";
case TAKkeydiff: return "Key Difference";
case TAKkeypatch: return "Key Patch";
case TAKkeyeddenormalize: return "Keyed Denormalize";
case TAKsequential: return "Sequential";
case TAKparallel: return "Parallel";
case TAKchilditerator: return "Child Dataset";
case TAKdatasetresult: return "Dataset Result";
case TAKrowresult: return "Row Result";
case TAKchildif: return "If";
case TAKpartition: return "Partition Distribute";
case TAKsubgraph: return "Sub Graph";
case TAKlocalgraph: return "Local Graph";
case TAKifaction: return "If Action";
case TAKemptyaction: return "Empty Action";
case TAKskiplimit: return "Skip Limit";
case TAKdiskread: return "Disk Read";
case TAKdisknormalize: return "Disk Normalize";
case TAKdiskaggregate: return "Disk Aggregate";
case TAKdiskcount: return "Disk Count";
case TAKdiskgroupaggregate: return "Disk Grouped Aggregate";
case TAKindexread: return "Index Read";
case TAKindexnormalize: return "Index Normalize";
case TAKindexaggregate: return "Index Aggregate";
case TAKindexcount: return "Index Count";
case TAKindexgroupaggregate: return "Index Grouped Aggregate";
case TAKchildnormalize: return "Child Normalize";
case TAKchildaggregate: return "Child Aggregate";
case TAKchildgroupaggregate: return "Child Grouped Aggregate";
case TAKchildthroughnormalize: return "Normalize";
case TAKcsvread: return "Csv Read";
case TAKxmlread: return "Xml Read";
case TAKlocalresultread: return "Read Local Result";
case TAKlocalresultwrite: return "Local Result";
case TAKcombine: return "Combine";
case TAKregroup: return "Regroup";
case TAKrollupgroup: return "Rollup Group";
case TAKcombinegroup: return "Combine Group";
case TAKlookupdenormalize: return "Lookup Denormalize";
case TAKalldenormalize: return "All Denormalize";
case TAKdenormalizegroup: return "Denormalize Group";
case TAKhashdenormalizegroup: return "Hash Denormalize Group";
case TAKlookupdenormalizegroup: return "Lookup Denormalize Group";
case TAKkeyeddenormalizegroup: return "Keyed Denormalize Group";
case TAKalldenormalizegroup: return "All Denormalize Group";
case TAKlocalresultspill: return "Spill Local Result";
case TAKsimpleaction: return "Action";
case TAKloopcount: return "Loop";
case TAKlooprow: return "Loop";
case TAKloopdataset: return "Loop";
case TAKchildcase: return "Case";
case TAKremotegraph: return "Remote";
case TAKlibrarycall: return "Library Call";
case TAKrawiterator: return "Child Dataset";
case TAKlocalstreamread: return "Read Input";
case TAKprocess: return "Process";
case TAKgraphloop: return "Graph";
case TAKparallelgraphloop: return "Graph";
case TAKgraphloopresultread: return "Graph Input";
case TAKgraphloopresultwrite: return "Graph Result";
case TAKgrouped: return "Grouped";
case TAKsorted: return "Sorted";
case TAKdistributed: return "Distributed";
case TAKnwayjoin: return "Join";
case TAKnwaymerge: return "Merge";
case TAKnwaymergejoin: return "Merge Join";
case TAKnwayinput: return "Nway Input";
case TAKnwaygraphloopresultread: return "Nway Graph Input";
case TAKnwayselect: return "Select Nway Input";
case TAKnonempty: return "Non Empty";
case TAKcreaterowlimit: return "OnFail Limit";
case TAKexistsaggregate: return "Exists";
case TAKcountaggregate: return "Count";
case TAKprefetchproject: return "Prefetch Project";
case TAKprefetchcountproject: return "Prefetch Count Project";
case TAKfiltergroup: return "Filter Group";
case TAKmemoryspillread: return "Read Spill";
case TAKmemoryspillwrite: return "Write Spill";
case TAKmemoryspillsplit: return "Spill";
case TAKsection: return "Section";
case TAKlinkedrawiterator: return "Child Dataset";
case TAKnormalizelinkedchild: return "Normalize";
case TAKfilterproject: return "Filtered Project";
case TAKcatch: return "Catch";
case TAKskipcatch: return "Skip Catch";
case TAKcreaterowcatch: return "OnFail Catch";
case TAKsectioninput: return "Section Input";
case TAKindexgroupcount: return "Index Grouped Count";
case TAKindexgroupexists: return "Index Grouped Exists";
case TAKhashdistributemerge: return "Distribute Merge";
case TAKselfjoinlight: return "Lightweight Self Join";
case TAKwhen_dataset: return "When";
case TAKhttp_rowdataset: return "HTTP dataset";
case TAKstreamediterator: return "Streamed Dataset";
case TAKexternalsource: return "User Source";
case TAKexternalsink: return "User Output";
case TAKexternalprocess: return "User Proceess";
case TAKwhen_action: return "When";
case TAKshuffle: return "Shuffle";
}
throwUnexpected();
}
extern bool isActivitySource(ThorActivityKind kind)
{
switch (kind)
{
case TAKpiperead:
case TAKtemptable:
case TAKinlinetable:
case TAKtemprow:
case TAKworkunitread:
case TAKnull:
case TAKsideeffect:
case TAKsoap_rowdataset:
case TAKsoap_rowaction:
case TAKkeydiff:
case TAKkeypatch:
case TAKchilditerator:
case TAKlocalgraph:
case TAKemptyaction:
case TAKdiskread:
case TAKdisknormalize:
case TAKdiskaggregate:
case TAKdiskcount:
case TAKdiskgroupaggregate:
case TAKindexread:
case TAKindexnormalize:
case TAKindexaggregate:
case TAKindexcount:
case TAKindexgroupaggregate:
case TAKchildnormalize:
case TAKchildaggregate:
case TAKchildgroupaggregate:
case TAKcsvread:
case TAKxmlread:
case TAKlocalresultread:
case TAKsimpleaction:
case TAKrawiterator:
case TAKlocalstreamread:
case TAKgraphloopresultread:
case TAKnwaygraphloopresultread:
case TAKlinkedrawiterator:
case TAKindexgroupexists:
case TAKindexgroupcount:
case TAKstreamediterator:
case TAKexternalsource:
return true;
}
return false;
}
extern bool isActivitySink(ThorActivityKind kind)
{
switch (kind)
{
case TAKdiskwrite:
case TAKworkunitwrite:
case TAKapply:
case TAKremoteresult:
case TAKdistribution:
case TAKpipewrite:
case TAKcsvwrite:
case TAKindexwrite:
case TAKxmlwrite:
case TAKsoap_rowaction:
case TAKsoap_datasetaction:
case TAKkeydiff:
case TAKkeypatch:
case TAKdatasetresult:
case TAKrowresult:
case TAKemptyaction:
case TAKlocalresultwrite:
case TAKgraphloopresultwrite:
case TAKsimpleaction:
case TAKexternalsink:
case TAKifaction:
case TAKparallel:
case TAKsequential:
case TAKwhen_action:
return true;
}
return false;
}
//------------------------------------------------------------------------------------------------
byte * CStaticRowBuilder::ensureCapacity(size32_t required, const char * fieldName)
{
if (required <= maxLength)
return static_cast<byte *>(self);
rtlReportFieldOverflow(required, maxLength, fieldName);
return NULL;
}
//=====================================================================================================
CThorContiguousRowBuffer::CThorContiguousRowBuffer(ISerialStream * _in) : in(_in)
{
buffer = NULL;
maxOffset = 0;
readOffset = 0;
}
void CThorContiguousRowBuffer::doRead(size32_t len, void * ptr)
{
ensureAccessible(readOffset + len);
memcpy(ptr, buffer+readOffset, len);
readOffset += len;
}
size32_t CThorContiguousRowBuffer::read(size32_t len, void * ptr)
{
doRead(len, ptr);
return len;
}
size32_t CThorContiguousRowBuffer::readSize()
{
size32_t value;
doRead(sizeof(value), &value);
return value;
}
size32_t CThorContiguousRowBuffer::readPackedInt(void * ptr)
{
size32_t size = sizePackedInt();
doRead(size, ptr);
return size;
}
size32_t CThorContiguousRowBuffer::readUtf8(ARowBuilder & target, size32_t offset, size32_t fixedSize, size32_t len)
{
if (len == 0)
return 0;
size32_t size = sizeUtf8(len);
byte * self = target.ensureCapacity(fixedSize + size, NULL);
doRead(size, self+offset);
return size;
}
size32_t CThorContiguousRowBuffer::readVStr(ARowBuilder & target, size32_t offset, size32_t fixedSize)
{
size32_t size = sizeVStr();
byte * self = target.ensureCapacity(fixedSize + size, NULL);
doRead(size, self+offset);
return size;
}
size32_t CThorContiguousRowBuffer::readVUni(ARowBuilder & target, size32_t offset, size32_t fixedSize)
{
size32_t size = sizeVUni();
byte * self = target.ensureCapacity(fixedSize + size, NULL);
doRead(size, self+offset);
return size;
}
size32_t CThorContiguousRowBuffer::sizePackedInt()
{
ensureAccessible(readOffset+1);
return rtlGetPackedSizeFromFirst(buffer[readOffset]);
}
size32_t CThorContiguousRowBuffer::sizeUtf8(size32_t len)
{
if (len == 0)
return 0;
//The len is the number of utf characters, size depends on which characters are included.
size32_t nextOffset = readOffset;
while (len)
{
ensureAccessible(nextOffset+1);
for (;nextOffset < maxOffset;)
{
nextOffset += readUtf8Size(buffer+nextOffset); // This function only accesses the first byte
if (--len == 0)
break;
}
}
return nextOffset - readOffset;
}
size32_t CThorContiguousRowBuffer::sizeVStr()
{
size32_t nextOffset = readOffset;
loop
{
ensureAccessible(nextOffset+1);
for (; nextOffset < maxOffset; nextOffset++)
{
if (buffer[nextOffset] == 0)
return (nextOffset + 1) - readOffset;
}
}
}
size32_t CThorContiguousRowBuffer::sizeVUni()
{
size32_t nextOffset = readOffset;
const size32_t sizeOfUChar = 2;
loop
{
ensureAccessible(nextOffset+sizeOfUChar);
for (; nextOffset+1 < maxOffset; nextOffset += sizeOfUChar)
{
if (buffer[nextOffset] == 0 && buffer[nextOffset+1] == 0)
return (nextOffset + sizeOfUChar) - readOffset;
}
}
}
void CThorContiguousRowBuffer::reportReadFail()
{
throwUnexpected();
}
const byte * CThorContiguousRowBuffer::peek(size32_t maxSize)