-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcolumnencoder.cpp
707 lines (552 loc) · 21.1 KB
/
columnencoder.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
//
// Copyright (C) 2013-2018 University of Amsterdam
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#include "columnencoder.h"
#include "stringutils.h"
#include <regex>
#ifdef BUILDING_JASP
#include "log.h"
#define LOGGER Log::log()
#else
#include <Rcpp.h>
#define LOGGER Rcpp::Rcout
#endif
ColumnEncoder * ColumnEncoder::_columnEncoder = nullptr;
std::set<ColumnEncoder*> * ColumnEncoder::_otherEncoders = nullptr;
bool ColumnEncoder::_encodingMapInvalidated = true;
bool ColumnEncoder::_decodingMapInvalidated = true;
bool ColumnEncoder::_decodingTypeInvalidated = true;
bool ColumnEncoder::_decoSafeMapInvalidated = true;
bool ColumnEncoder::_originalNamesInvalidated = true;
bool ColumnEncoder::_encodedNamesInvalidated = true;
ColumnEncoder * ColumnEncoder::columnEncoder()
{
if(!_columnEncoder)
_columnEncoder = new ColumnEncoder();
return _columnEncoder;
}
void ColumnEncoder::invalidateAll()
{
_encodingMapInvalidated = true;
_decodingMapInvalidated = true;
_decodingTypeInvalidated = true;
_decoSafeMapInvalidated = true;
_originalNamesInvalidated = true;
_encodedNamesInvalidated = true;
}
ColumnEncoder::ColumnEncoder(std::string prefix, std::string postfix)
: _encodePrefix(prefix), _encodePostfix(postfix)
{
if(!_otherEncoders)
{
_otherEncoders = new ColumnEncoder::ColumnEncoders();
invalidateAll();
}
_otherEncoders->insert(this);
}
ColumnEncoder::ColumnEncoder(const std::map<std::string, std::string> & decodeDifferently)
: _encodePrefix("JASPColumn_"), _encodePostfix("_For_Replacement")
{
std::vector<std::string> originalNames;
originalNames.reserve(decodeDifferently.size());
for(const auto & oriNew : decodeDifferently)
originalNames.push_back(oriNew.first);
setCurrentNames(originalNames);
for(const std::string & encodedName : _encodedNames)
if(decodeDifferently.count(_decodingMap[encodedName]) > 0)
_decodingMap[encodedName] = decodeDifferently.at(_decodingMap[encodedName]);
}
ColumnEncoder::~ColumnEncoder()
{
if(this != _columnEncoder)
{
if(_otherEncoders && _otherEncoders->count(this) > 0) //The special "replacer-encoder" doesn't add itself to otherEncoders.
_otherEncoders->erase(this);
}
else
{
_columnEncoder = nullptr;
ColumnEncoders others = *_otherEncoders;
for(ColumnEncoder * colEnc : others)
delete colEnc;
if(_otherEncoders->size() > 0)
LOGGER << "Something went wrong removing other ColumnEncoders..." << std::endl;
delete _otherEncoders;
_otherEncoders = nullptr;
invalidateAll();
}
}
std::string ColumnEncoder::encode(const std::string &in)
{
if(in == "") return "";
if(encodingMap().count(in) == 0)
throw std::runtime_error("Trying to encode columnName but '" + in + "' is not a columnName!");
return encodingMap().at(in);
}
std::string ColumnEncoder::decode(const std::string &in)
{
if(in == "") return "";
if(decodingMap().count(in) == 0)
throw std::runtime_error("Trying to decode columnName but '" + in + "' is not an encoded columnName!");
return decodingMap().at(in);
}
columnType ColumnEncoder::columnTypeFromEncoded(const std::string &in)
{
if(in == "" || decodingTypes().count(in) == 0)
return columnType::unknown;
return decodingTypes().at(in);
}
void ColumnEncoder::setCurrentNames(const std::vector<std::string> & names, bool generateTypesEncoding)
{
//LOGGER << "ColumnEncoder::setCurrentNames(#"<< names.size() << ")" << std::endl;
_encodingMap.clear();
_decodingMap.clear();
_encodedNames.clear();
_encodedNames.reserve(names.size());
size_t runningCounter = 0;
_originalNames = names;
//First normal encoding decoding: (Although im not sure we would ever need those again?)
for(size_t col = 0; col < names.size(); col++)
{
std::string newName = _encodePrefix + std::to_string(runningCounter++) + _encodePostfix; //Slightly weird (but R-syntactically valid) name to avoid collisions with user stuff.
_encodingMap[names[col]] = newName;
_decodingMap[newName] = names[col];
_encodedNames.push_back(newName);
}
if(generateTypesEncoding)
for(size_t col = 0; col < names.size(); col++)
for(columnType colType : { columnType::scale, columnType::ordinal, columnType::nominal })
{
std::string qualifiedName = names[col] + "." + columnTypeToString(colType),
newName = _encodePrefix + std::to_string(runningCounter++) + _encodePostfix; //Slightly weird (but R-syntactically valid) name to avoid collisions with user stuff.
_encodingMap[qualifiedName] = newName;
_decodingMap[newName] = names[col]; //Decoding is back to the actual name in the data!
_decodingTypes[newName] = colType;
_encodedNames .push_back(newName);
_originalNames .push_back(qualifiedName);
}
sortVectorBigToSmall(_originalNames);
invalidateAll();
}
void ColumnEncoder::sortVectorBigToSmall(std::vector<std::string> & vec)
{
std::sort(vec.begin(), vec.end(), [](std::string & a, std::string & b) { return a.size() > b.size(); }); //We need this to make sure smaller columnNames do not bite chunks off of larger ones
}
const ColumnEncoder::colMap & ColumnEncoder::encodingMap()
{
static ColumnEncoder::colMap map;
if(_encodingMapInvalidated)
{
map = _columnEncoder->_encodingMap;
if(_otherEncoders)
for(const ColumnEncoder * other : *_otherEncoders)
for(const auto & keyVal : other->_encodingMap)
if(map.count(keyVal.first) == 0)
map[keyVal.first] = keyVal.second;
_encodingMapInvalidated = false;
}
return map;
}
const ColumnEncoder::colMap & ColumnEncoder::decodingMap()
{
static ColumnEncoder::colMap map;
if(_decodingMapInvalidated)
{
map = _columnEncoder->_decodingMap;
if(_otherEncoders)
for(const ColumnEncoder * other : *_otherEncoders)
for(const auto & keyVal : other->_decodingMap)
if(map.count(keyVal.first) == 0)
map[keyVal.first] = keyVal.second;
_decodingMapInvalidated = false;
}
return map;
}
const ColumnEncoder::colTypeMap &ColumnEncoder::decodingTypes()
{
static ColumnEncoder::colTypeMap map;
if(_decodingTypeInvalidated)
{
map = _columnEncoder->_decodingTypes;
if(_otherEncoders)
for(const ColumnEncoder * other : *_otherEncoders)
for(const auto & keyVal : other->_decodingTypes)
if(map.count(keyVal.first) == 0)
map[keyVal.first] = keyVal.second;
_decodingTypeInvalidated = false;
}
return map;
}
const ColumnEncoder::colMap & ColumnEncoder::decodingMapSafeHtml()
{
static ColumnEncoder::colMap map;
if(_decoSafeMapInvalidated)
{
map.clear();
for(const auto & keyVal : _columnEncoder->_decodingMap)
if(map.count(keyVal.first) == 0)
map[keyVal.first] = stringUtils::escapeHtmlStuff(keyVal.second, true);
if(_otherEncoders)
for(const ColumnEncoder * other : *_otherEncoders)
for(const auto & keyVal : other->_decodingMap)
if(map.count(keyVal.first) == 0)
map[keyVal.first] = stringUtils::escapeHtmlStuff(keyVal.second, true); // replace square brackets for https://github.com/jasp-stats/jasp-issues/issues/2625
_decoSafeMapInvalidated = false;
}
return map;
}
const ColumnEncoder::colVec & ColumnEncoder::originalNames()
{
static ColumnEncoder::colVec vec;
if(_originalNamesInvalidated)
{
vec = _columnEncoder->_originalNames;
if(_otherEncoders)
for(const ColumnEncoder * other : *_otherEncoders)
for(const std::string & name : other->_originalNames)
vec.push_back(name);
_originalNamesInvalidated = false;
}
sortVectorBigToSmall(vec);
return vec;
}
const ColumnEncoder::colVec & ColumnEncoder::encodedNames()
{
static ColumnEncoder::colVec vec;
if(_encodedNamesInvalidated)
{
vec = _columnEncoder->_encodedNames;
if(_otherEncoders)
for(const ColumnEncoder * other : *_otherEncoders)
for(const std::string & name : other->_encodedNames)
vec.push_back(name);
_encodedNamesInvalidated = false;
}
sortVectorBigToSmall(vec);
return vec;
}
bool ColumnEncoder::shouldEncode(const std::string & in)
{
return _encodingMap.count(in) > 0;
}
bool ColumnEncoder::shouldDecode(const std::string & in)
{
return _decodingMap.count(in) > 0;
}
std::string ColumnEncoder::replaceAllStrict(const std::string & text, const std::map<std::string, std::string> & map)
{
if (map.count(text) > 0)
return map.at(text);
else
return text;
}
std::string ColumnEncoder::replaceAll(std::string text, const std::map<std::string, std::string> & map, const std::vector<std::string> & names)
{
size_t foundPos = 0;
while(foundPos < std::string::npos)
{
size_t firstFoundPos = std::string::npos;
std::string replaceThis;
//First we find the first occurence of a replaceable text.
for(const std::string & replaceMe : names) //We follow names instead of keyvals from map because they ought to be sorted from largest to smallest string (_originalNames) to not make sub-replacements
{
size_t pos = text.find(replaceMe, foundPos);
if(pos < firstFoundPos)
{
firstFoundPos = pos;
replaceThis = replaceMe;
}
}
//We found something to replace and this will be the first occurence of anything like that. Replace it!
if(firstFoundPos != std::string::npos)
{
foundPos = firstFoundPos;
const std::string & replacement = map.at(replaceThis);
text.replace(foundPos, replaceThis.length(), replacement);
foundPos += replacement.length(); //Let's make sure we start replacing from after where we just replaced
}
else
foundPos = std::string::npos;
}
return text;
}
std::string ColumnEncoder::encodeRScript(std::string text, std::set<std::string> * columnNamesFound)
{
return encodeRScript(text, encodingMap(), originalNames(), columnNamesFound);
}
std::string ColumnEncoder::encodeRScript(std::string text, const std::map<std::string, std::string> & map, const std::vector<std::string> & names, std::set<std::string> * columnNamesFound)
{
if(columnNamesFound)
columnNamesFound->clear();
static std::regex nonNameChar("[^\\.A-Za-z0-9_]");
//for now we simply replace any found columnname by its encoded variant if found
for(const std::string & oldCol : names)
{
std::string newCol = map.at(oldCol);
std::vector<size_t> foundColPositions = getPositionsColumnNameMatches(text, oldCol);
std::reverse(foundColPositions.begin(), foundColPositions.end());
for (size_t foundPos : foundColPositions)
{
size_t foundPosEnd = foundPos + oldCol.length();
//First check if it is a "free columnname" aka is there some space or a kind in front of it. We would not want to replace a part of another term (Imagine what happens when you use a columname such as "E" and a filter that includes the term TRUE, it does not end well..)
bool startIsFree = foundPos == 0 || std::regex_match(text.substr(foundPos - 1, 1), nonNameChar);
bool endIsFree = foundPosEnd == text.length() || std::regex_match(text.substr(foundPosEnd, 1), nonNameChar);
//Check for "(" as well because maybe someone has a columnname such as rep or if or something weird like that. This might however have some whitespace in between...
bool keepGoing = true;
for(size_t bracePos = foundPosEnd; bracePos < text.size() && endIsFree && keepGoing; bracePos++)
if(text[bracePos] == '(')
endIsFree = false;
else if(text[bracePos] != '\t' && text[bracePos] != ' ')
keepGoing = false; //Aka something else than whitespace or a brace and that means that we can replace it!
if(startIsFree && endIsFree)
{
text.replace(foundPos, oldCol.length(), newCol);
if(columnNamesFound)
columnNamesFound->insert(oldCol);
}
}
}
return text;
}
std::vector<size_t> ColumnEncoder::getPositionsColumnNameMatches(const std::string & text, const std::string & columnName)
{
std::vector<size_t> positions;
bool inString = false;
char delim = '?';
for (std::string::size_type pos = 0; pos < text.length(); ++pos)
if (!inString && text.substr(pos, columnName.length()) == columnName)
positions.push_back(int(pos));
else if (text[pos] == '"' || text[pos] == '\'') //string starts or ends. This does not take into account escape characters though...
{
if (!inString)
{
delim = text[pos];
inString = true;
}
else if(text[pos] == delim)
inString = false;
}
return positions;
}
void ColumnEncoder::encodeJson(Json::Value & json, bool replaceNames, bool replaceStrict)
{
//std::cout << "Json before encoding:\n" << json.toStyledString();
replaceAll(json, encodingMap(), originalNames(), replaceNames, replaceStrict);
//std::cout << "Json after encoding:\n" << json.toStyledString() << std::endl;
}
void ColumnEncoder::decodeJson(Json::Value & json, bool replaceNames)
{
//std::cout << "Json before encoding:\n" << json.toStyledString();
replaceAll(json, decodingMap(), encodedNames(), replaceNames, false);
//std::cout << "Json after encoding:\n" << json.toStyledString() << std::endl;
}
void ColumnEncoder::decodeJsonSafeHtml(Json::Value & json)
{
replaceAll(json, decodingMapSafeHtml(), encodedNames(), true, false);
}
void ColumnEncoder::replaceAll(Json::Value & json, const std::map<std::string, std::string> & map, const std::vector<std::string> & names, bool replaceNames, bool replaceStrict)
{
switch(json.type())
{
case Json::arrayValue:
for(Json::Value & option : json)
replaceAll(option, map, names, replaceNames, replaceStrict);
return;
case Json::objectValue:
{
std::map<std::string, std::string> changedMembers;
for(const std::string & optionName : json.getMemberNames())
{
replaceAll(json[optionName], map, names, replaceNames, replaceStrict);
if(replaceNames)
{
std::string replacedName = replaceStrict ? replaceAllStrict(optionName, map) : replaceAll(optionName, map, names);
if(replacedName != optionName)
changedMembers[optionName] = replacedName;
}
}
for(const auto & origNew : changedMembers) //map is empty if !replaceNames
{
json[origNew.second] = json[origNew.first];
json.removeMember(origNew.first);
}
return;
}
case Json::stringValue:
json = replaceStrict ? replaceAllStrict(json.asString(), map) : replaceAll(json.asString(), map, names);
return;
default:
return;
}
}
void ColumnEncoder::setCurrentNamesFromOptionsMeta(const Json::Value & options)
{
std::vector<std::string> namesFound;
if(!options.isNull() && options.isMember(".meta"))
collectExtraEncodingsFromMetaJson(options[".meta"], namesFound);
setCurrentNames(namesFound);
}
void ColumnEncoder::collectExtraEncodingsFromMetaJson(const Json::Value & json, std::vector<std::string> & namesCollected) const
{
switch(json.type())
{
case Json::arrayValue:
for(const Json::Value & option : json)
collectExtraEncodingsFromMetaJson(option, namesCollected);
return;
case Json::objectValue:
if(json.isMember("encodeThis"))
{
if(json["encodeThis"].isString())
namesCollected.push_back(json["encodeThis"].asString());
else if(json["encodeThis"].isArray())
for(const Json::Value & enc : json["encodeThis"])
namesCollected.push_back(enc.asString());
}
else
for(const std::string & optionName : json.getMemberNames())
collectExtraEncodingsFromMetaJson(json[optionName], namesCollected);
return;
default:
return;
}
}
std::string ColumnEncoder::removeColumnNamesFromRScript(const std::string & rCode, const std::vector<std::string> & colsToRemove)
{
std::map<std::string, std::string> replaceBy;
for(const std::string & col : colsToRemove)
replaceBy[col] = "stop('column " + col + " was removed from this RScript')";
return replaceColumnNamesInRScript(rCode, replaceBy);
}
std::string ColumnEncoder::replaceColumnNamesInRScript(const std::string & rCode, const std::map<std::string, std::string> & changedNames)
{
//Ok the trick here is to reuse the encoding code, we will first encode the original names and then change the encodings to point back to the replaced names.
ColumnEncoder tempEncoder(changedNames);
return
tempEncoder.replaceAll(
tempEncoder.encodeRScript(
rCode,
tempEncoder._encodingMap,
tempEncoder._originalNames
),
tempEncoder._decodingMap,
tempEncoder._encodedNames
);
}
ColumnEncoder::colVec ColumnEncoder::columnNames()
{
return _columnEncoder ? _columnEncoder->_originalNames : colVec();
}
ColumnEncoder::colVec ColumnEncoder::columnNamesEncoded()
{
return _columnEncoder ? _columnEncoder->_encodedNames : colVec();
}
ColumnEncoder::colsPlusTypes ColumnEncoder::encodeColumnNamesinOptions(Json::Value & options, bool preloadingData)
{
colsPlusTypes getTheseCols;
if (options.isObject())
{
if(!preloadingData) //make sure "optionname".types is available for analyses incapable of preloadingData, this should be considered deprecated
{
// For variables list the types of each variable are added in the option itself.
// To ensure the analyses still work as before, remove these types from the option, and add them in a new option with name '<option mame>.types'
// very much deprecated though as analyses should announce being capable of "preloadingData" and then using that instead.
for (const std::string& optionName : options.getMemberNames())
if (options[optionName].isObject() && options[optionName].isMember("value") && options[optionName].isMember("types"))
{
options[optionName + ".types"] = options[optionName]["types"];
options[optionName] = options[optionName]["value"];
}
}
else //Here we make sure all the requested columns + their types are collected
{ // they then are encoded with type included so that everything is accessible easily via those encoded names
// some functionality has been added to ask for the type of an encoded column as well.
for (const std::string& optionName : options.getMemberNames())
if (options[optionName].isObject() && options[optionName].isMember("value") && options[optionName].isMember("types"))
{
if (options[optionName].isObject() && options[optionName].isMember("value") && options[optionName].isMember("types"))
{
Json::Value newOption = Json::arrayValue,
& typeList = options[optionName]["types"],
valueList = options[optionName]["value"];
bool useSingleVal = false;
if(!options[optionName]["value"].isArray())
{
valueList = Json::arrayValue;
valueList.append(options[optionName]["value"].asString());
useSingleVal = true; //Otherwise we break things like "splitBy" it seems
}
//I wanted to do a sanity check, but actually the data is insane by design atm.
// data can be { value: "", types: [] } but also { value: [], types: [] } which is great...
//if(typeList.size() != valueList.size())
// std::runtime_error("Expecting the same amount of values and types");
for(int i=0; i<valueList.size(); i++)
{
std::string name = valueList[i].asString(),
type = typeList.size() > i ? typeList[i].asString() : "";
if(type == "unknown" || !columnTypeValidName(type))
newOption.append(name);
else
{
std::string nameWithType = name + "." + type;
newOption.append(nameWithType);
getTheseCols.insert(std::make_pair(nameWithType, columnTypeFromString(type)));
}
}
options[optionName] = !useSingleVal ? newOption : newOption[0];
}
}
}
}
_encodeColumnNamesinOptions(options, options[".meta"]);
return getTheseCols;
}
void ColumnEncoder::_encodeColumnNamesinOptions(Json::Value & options, Json::Value & meta)
{
if(meta.isNull())
return;
bool encodePlease = meta.isObject() && meta.get("shouldEncode", false).asBool(),
isRCode = meta.isObject() && meta.get("rCode", false).asBool();
switch(options.type())
{
case Json::arrayValue:
if(encodePlease)
columnEncoder()->encodeJson(options, false, true); //If we already think we have columnNames just change it all
else if(meta.type() == Json::arrayValue)
for(int i=0; i<options.size() && i < meta.size(); i++)
_encodeColumnNamesinOptions(options[i], meta[i]);
else if(isRCode)
for(int i=0; i<options.size(); i++)
if(options[i].isString())
options[i] = columnEncoder()->encodeRScript(options[i].asString());
return;
case Json::objectValue:
for(const std::string & memberName : options.getMemberNames())
if(memberName != ".meta" && meta.isMember(memberName))
_encodeColumnNamesinOptions(options[memberName], meta[memberName]);
else if(isRCode && options[memberName].isString())
options[memberName] = columnEncoder()->encodeRScript(options[memberName].asString());
else if(encodePlease)
columnEncoder()->encodeJson(options, false, true); //If we already think we have columnNames just change it all I guess?
return;
case Json::stringValue:
if(isRCode) options = columnEncoder()->encodeRScript(options.asString());
else if(encodePlease) options = columnEncoder()->encodeAll(options.asString());
return;
default:
return;
}
}