Skip to content
This repository was archived by the owner on Jun 20, 2025. It is now read-only.

Commit b4c540b

Browse files
Ajinkya Ghongefacebook-github-bot
authored andcommitted
Add support for filter in pc_translator.
Summary: # Context As per PC Translator design, we need a runtime library will be called during PC run. This library will be called at the beginning of PC run to encode specified fields in publisher side input into a encoded breakdown (aggregation) Ids based on active PC instruction sets for the run. The library will filter the active PC Instruction sets for the run based on parsing the pcs_features i.e. gatekeepers for the particular run. # Product decisions In this stack we would focus solely on functionality required for private lift runs. We would focus on the MVP implementation of the library and its integration with fbpcf ORAM encoder library in this stack. # Stack 1. Create runtime pc_translator library. 2. Add logic to retrieve and parse PC instruction set, filtered based on the active gatekeepers for the run. 3. Integrate pc_translator library with fbpcf ORAM encoder. 4. Add logic to generate transformed publisher output with encoded breakdown ID and write the output. 5. Add support for filter constraints in pc_translator. # In this diff Add support for filter constraints in pc_translator. Differential Revision: D44702838 Privacy Context Container: L416713 fbshipit-source-id: f293bbfa9285eed7e06898d9cc52b4396d4fd3d9
1 parent 8fdb96e commit b4c540b

7 files changed

Lines changed: 68 additions & 24 deletions

File tree

fbpcs/pc_translator/PCTranslator.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
namespace pc_translator {
2626

27+
using IFilter = fbpcf::mpc_std_lib::oram::IFilter;
28+
2729
std::string PCTranslator::encode(const std::string& inputDatasetPath) {
2830
auto validInstructionSetNames =
2931
PCTranslator::retrieveInstructionSetNamesForRun(pcsFeatures_);
@@ -89,26 +91,40 @@ std::string PCTranslator::transformDataset(
8991
std::vector<std::vector<uint32_t>> inputColums;
9092
std::vector<std::string> outputHeader;
9193
std::vector<std::vector<std::string>> outputContent;
94+
auto filters = std::make_unique<std::vector<std::unique_ptr<IFilter>>>(0);
9295
private_measurement::csv::readCsv(
9396
inputDatasetPath,
9497
[&](const std::vector<std::string>& header,
9598
const std::vector<std::string>& parts) {
9699
std::vector<uint32_t> inputColumnPerRow;
97100
std::string column;
98101
std::uint32_t value;
99-
bool found = false;
102+
auto groupByIndex = 0;
100103
std::vector<std::string> outputContentPerRow;
101104
for (std::vector<std::string>::size_type i = 0; i < header.size();
102105
++i) {
106+
bool foundGroupByField = false;
103107
column = header[i];
104108
value = std::atoi(parts[i].c_str());
105-
found =
109+
foundGroupByField =
106110
(std::find(
107111
pcInstructionSet->getGroupByIds().begin(),
108112
pcInstructionSet->getGroupByIds().end(),
109113
column) != pcInstructionSet->getGroupByIds().end());
110-
if (found) {
114+
115+
if (foundGroupByField) {
111116
inputColumnPerRow.push_back(value);
117+
for (const auto& filterConstraint :
118+
pcInstructionSet->getFilterConstraints()) {
119+
if (filterConstraint.getName() == column) {
120+
filters->push_back(std::make_unique<
121+
fbpcf::mpc_std_lib::oram::SingleValueFilter>(
122+
filterConstraint.getType(),
123+
groupByIndex,
124+
filterConstraint.getValue()));
125+
}
126+
}
127+
groupByIndex++;
112128
} else {
113129
if (lineNo == 0) {
114130
outputHeader.push_back(header[i]);
@@ -122,8 +138,6 @@ std::string PCTranslator::transformDataset(
122138
lineNo++;
123139
});
124140

125-
auto filters = std::make_unique<
126-
std::vector<std::unique_ptr<fbpcf::mpc_std_lib::oram::IFilter>>>(0);
127141
std::unique_ptr<fbpcf::mpc_std_lib::oram::IOramEncoder> encoder =
128142
std::make_unique<fbpcf::mpc_std_lib::oram::OramEncoder>(
129143
std::move(filters));

fbpcs/pc_translator/input_processing/FilterConstraint.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,29 @@
66
*/
77

88
#include "fbpcs/pc_translator/input_processing/FilterConstraint.h"
9+
#include <fbpcf/mpc_std_lib/oram/encoder/IFilter.h>
910

1011
#include <cstdint>
1112
#include <memory>
1213
#include <string>
1314
#include <vector>
1415

16+
using fbpcf::mpc_std_lib::oram::IFilter;
1517
namespace pc_translator {
18+
19+
using IFilter = fbpcf::mpc_std_lib::oram::IFilter;
20+
1621
FilterConstraint::FilterConstraint(
1722
const std::string& name,
18-
const std::string& type,
23+
const IFilter::FilterType type,
1924
int value)
2025
: name_(name), type_(type), value_(value) {}
2126

2227
std::string FilterConstraint::getName() const {
2328
return name_;
2429
}
2530

26-
std::string FilterConstraint::getType() const {
31+
IFilter::FilterType FilterConstraint::getType() const {
2732
return type_;
2833
}
2934

fbpcs/pc_translator/input_processing/FilterConstraint.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,24 @@
77

88
#pragma once
99

10+
#include <fbpcf/mpc_std_lib/oram/encoder/IFilter.h>
1011
#include <cstdint>
1112
#include <memory>
1213
#include <string>
1314
#include <vector>
1415

1516
namespace pc_translator {
1617

18+
using IFilter = fbpcf::mpc_std_lib::oram::IFilter;
1719
/*
1820
* Class to store each filter constraint include in the PC instruction set.
1921
*/
2022
class FilterConstraint {
2123
public:
22-
FilterConstraint(const std::string& name, const std::string& type, int value);
24+
FilterConstraint(
25+
const std::string& name,
26+
const IFilter::FilterType type,
27+
int value);
2328

2429
/*
2530
* Name of the filter constraint i.e. the field on which this filter is to be
@@ -30,13 +35,13 @@ class FilterConstraint {
3035
/*
3136
* Constraint type i.e. LT, LTE, EQ, NEQ etc.
3237
*/
33-
std::string getType() const;
38+
IFilter::FilterType getType() const;
3439

3540
int getValue() const;
3641

3742
private:
3843
std::string name_;
39-
std::string type_;
44+
IFilter::FilterType type_;
4045
int value_;
4146
};
4247

fbpcs/pc_translator/input_processing/PCInstructionSet.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66
*/
77

88
#include "fbpcs/pc_translator/input_processing/PCInstructionSet.h"
9-
9+
#include <fbpcf/mpc_std_lib/oram/encoder/IFilter.h>
1010
#include <folly/json.h>
1111
#include <cstdint>
1212
#include <memory>
13+
#include <sstream>
1314
#include <string>
1415
#include <vector>
1516

1617
namespace pc_translator {
1718

19+
using IFilter = fbpcf::mpc_std_lib::oram::IFilter;
1820
const std::vector<std::string>& PCInstructionSet::getGroupByIds() const {
1921
return groupByIds;
2022
}
@@ -40,7 +42,22 @@ PCInstructionSet PCInstructionSet::fromDynamic(const folly::dynamic& obj) {
4042
for (auto constraint : constraints) {
4143
auto constraintType = constraint["constraint_type"].asString();
4244
auto constraintValue = constraint["value"].asInt();
43-
FilterConstraint filterConstraint(name, constraintType, constraintValue);
45+
46+
std::map<std::string, IFilter::FilterType> filterMap = {
47+
{"EQ", IFilter::FilterType::EQ},
48+
{"NEQ", IFilter::FilterType::NEQ},
49+
{"LT", IFilter::FilterType::LT},
50+
{"LTE", IFilter::FilterType::LTE},
51+
{"GT", IFilter::FilterType::GT},
52+
{"GTE", IFilter::FilterType::GTE}};
53+
54+
auto it = filterMap.find(constraintType);
55+
if (it == filterMap.end()) {
56+
throw std::invalid_argument(
57+
"Constraint type must be - GT, LT, GTE, LTE, EQ, NEQ");
58+
}
59+
60+
FilterConstraint filterConstraint(name, it->second, constraintValue);
4461
pcInstructionSet.filterConstraints.push_back(filterConstraint);
4562
}
4663
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
id_,opportunity,test_flag,opportunity_timestamp,breakdown_id
22
cfcd208495d565ef66e7dff9f98764da,1,0,1600000430,0
33
c4ca4238a0b923820dcc509a6f75849b,1,1,1600000401,1
4-
c81e728d9d4c2f636f067f89cc14862c,0,0,0,2
5-
eccbc87e4b5ce2fe28308fd9f2a7baf3,0,0,0,3
6-
a87ff679a2f3e71d9181a67b7542122c,0,0,0,0
7-
e4da3b7fbbce2345d7772b0674a318d5,1,1,1600000461,4
8-
1679091c5a880faf6fb5e6087eb1b2dc,1,0,1600000052,5
9-
8f14e45fceea167a5a36dedd4bea2543,1,0,1600000831,6
10-
c9f0f895fb98ab9159f51fd0297e236d,1,0,1600000530,7
11-
45c48cce2e2d7fbdea1afc51c7c6ad26,1,0,1600000972,5
4+
c81e728d9d4c2f636f067f89cc14862c,0,0,0,1
5+
eccbc87e4b5ce2fe28308fd9f2a7baf3,0,0,0,1
6+
a87ff679a2f3e71d9181a67b7542122c,0,0,0,2
7+
e4da3b7fbbce2345d7772b0674a318d5,1,1,1600000461,1
8+
1679091c5a880faf6fb5e6087eb1b2dc,1,0,1600000052,1
9+
8f14e45fceea167a5a36dedd4bea2543,1,0,1600000831,2
10+
c9f0f895fb98ab9159f51fd0297e236d,1,0,1600000530,1
11+
45c48cce2e2d7fbdea1afc51c7c6ad26,1,0,1600000972,1
1212
d3d9446802a44259755d38e6d163e820,0,0,0,0
13-
6512bd43d9caa6e02c990b0a82652dca,0,0,0,0
13+
6512bd43d9caa6e02c990b0a82652dca,0,0,0,2

fbpcs/pc_translator/tests/input_processing/TestPCInstructionSet.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#include "fbpcs/pc_translator/input_processing/PCInstructionSet.h"
1616

1717
namespace pc_translator {
18+
19+
using IFilter = fbpcf::mpc_std_lib::oram::IFilter;
20+
1821
class TestPCInstructionSet : public ::testing::Test {
1922
public:
2023
protected:
@@ -36,7 +39,7 @@ TEST_F(TestPCInstructionSet, TestStandardWorkflowTest) {
3639
EXPECT_EQ(groupByIds.size(), 2);
3740
EXPECT_EQ(filterConstraints.size(), 3);
3841
EXPECT_EQ(filterConstraints[0].getName(), "gender");
39-
EXPECT_EQ(filterConstraints[0].getType(), "EQ");
42+
EXPECT_EQ(filterConstraints[0].getType(), IFilter::FilterType::EQ);
4043
EXPECT_EQ(filterConstraints[0].getValue(), 0);
4144
}
4245

fbpcs/pc_translator/tests/publisher_unittest.csv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ cfcd208495d565ef66e7dff9f98764da,1,0,1600000430, 25, 0
33
c4ca4238a0b923820dcc509a6f75849b,1,1,1600000401, 26, 1
44
c81e728d9d4c2f636f067f89cc14862c,0,0,0, 44, 0
55
eccbc87e4b5ce2fe28308fd9f2a7baf3,0,0,0, 23, 0
6-
a87ff679a2f3e71d9181a67b7542122c,0,0,0, 25, 0
6+
a87ff679a2f3e71d9181a67b7542122c,0,0,0, 26, 0
77
e4da3b7fbbce2345d7772b0674a318d5,1,1,1600000461, 24, 1
88
1679091c5a880faf6fb5e6087eb1b2dc,1,0,1600000052, 25, 1
99
8f14e45fceea167a5a36dedd4bea2543,1,0,1600000831, 26, 0
1010
c9f0f895fb98ab9159f51fd0297e236d,1,0,1600000530, 50, 0
1111
45c48cce2e2d7fbdea1afc51c7c6ad26,1,0,1600000972, 25, 1
1212
d3d9446802a44259755d38e6d163e820,0,0,0, 25, 0
13-
6512bd43d9caa6e02c990b0a82652dca,0,0,0, 25, 0
13+
6512bd43d9caa6e02c990b0a82652dca,0,0,0, 26, 0

0 commit comments

Comments
 (0)