Skip to content

Commit

Permalink
Add support for filter in pc_translator. (#2287)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #2287

# 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: b60909247067a97a7b8a5d13c9e80fc400f96456
  • Loading branch information
Ajinkya Ghonge authored and facebook-github-bot committed Apr 6, 2023
1 parent b9aa883 commit 7236864
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 24 deletions.
24 changes: 19 additions & 5 deletions fbpcs/pc_translator/PCTranslator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

namespace pc_translator {

using IFilter = fbpcf::mpc_std_lib::oram::IFilter;

std::string PCTranslator::encode(const std::string& inputDatasetPath) {
auto validInstructionSetNames =
PCTranslator::retrieveInstructionSetNamesForRun(pcsFeatures_);
Expand Down Expand Up @@ -89,26 +91,40 @@ std::string PCTranslator::transformDataset(
std::vector<std::vector<uint32_t>> inputColums;
std::vector<std::string> outputHeader;
std::vector<std::vector<std::string>> outputContent;
auto filters = std::make_unique<std::vector<std::unique_ptr<IFilter>>>(0);
private_measurement::csv::readCsv(
inputDatasetPath,
[&](const std::vector<std::string>& header,
const std::vector<std::string>& parts) {
std::vector<uint32_t> inputColumnPerRow;
std::string column;
std::uint32_t value;
bool found = false;
auto groupByIndex = 0;
std::vector<std::string> outputContentPerRow;
for (std::vector<std::string>::size_type i = 0; i < header.size();
++i) {
bool foundGroupByField = false;
column = header[i];
value = std::atoi(parts[i].c_str());
found =
foundGroupByField =
(std::find(
pcInstructionSet->getGroupByIds().begin(),
pcInstructionSet->getGroupByIds().end(),
column) != pcInstructionSet->getGroupByIds().end());
if (found) {

if (foundGroupByField) {
inputColumnPerRow.push_back(value);
for (const auto& filterConstraint :
pcInstructionSet->getFilterConstraints()) {
if (filterConstraint.getName() == column) {
filters->push_back(std::make_unique<
fbpcf::mpc_std_lib::oram::SingleValueFilter>(
filterConstraint.getType(),
groupByIndex,
filterConstraint.getValue()));
}
}
groupByIndex++;
} else {
if (lineNo == 0) {
outputHeader.push_back(header[i]);
Expand All @@ -122,8 +138,6 @@ std::string PCTranslator::transformDataset(
lineNo++;
});

auto filters = std::make_unique<
std::vector<std::unique_ptr<fbpcf::mpc_std_lib::oram::IFilter>>>(0);
std::unique_ptr<fbpcf::mpc_std_lib::oram::IOramEncoder> encoder =
std::make_unique<fbpcf::mpc_std_lib::oram::OramEncoder>(
std::move(filters));
Expand Down
9 changes: 7 additions & 2 deletions fbpcs/pc_translator/input_processing/FilterConstraint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,29 @@
*/

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

#include <cstdint>
#include <memory>
#include <string>
#include <vector>

using fbpcf::mpc_std_lib::oram::IFilter;
namespace pc_translator {

using IFilter = fbpcf::mpc_std_lib::oram::IFilter;

FilterConstraint::FilterConstraint(
const std::string& name,
const std::string& type,
const IFilter::FilterType type,
int value)
: name_(name), type_(type), value_(value) {}

std::string FilterConstraint::getName() const {
return name_;
}

std::string FilterConstraint::getType() const {
IFilter::FilterType FilterConstraint::getType() const {
return type_;
}

Expand Down
11 changes: 8 additions & 3 deletions fbpcs/pc_translator/input_processing/FilterConstraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,24 @@

#pragma once

#include <fbpcf/mpc_std_lib/oram/encoder/IFilter.h>
#include <cstdint>
#include <memory>
#include <string>
#include <vector>

namespace pc_translator {

using IFilter = fbpcf::mpc_std_lib::oram::IFilter;
/*
* Class to store each filter constraint include in the PC instruction set.
*/
class FilterConstraint {
public:
FilterConstraint(const std::string& name, const std::string& type, int value);
FilterConstraint(
const std::string& name,
const IFilter::FilterType type,
int value);

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

int getValue() const;

private:
std::string name_;
std::string type_;
IFilter::FilterType type_;
int value_;
};

Expand Down
21 changes: 19 additions & 2 deletions fbpcs/pc_translator/input_processing/PCInstructionSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
*/

#include "fbpcs/pc_translator/input_processing/PCInstructionSet.h"

#include <fbpcf/mpc_std_lib/oram/encoder/IFilter.h>
#include <folly/json.h>
#include <cstdint>
#include <memory>
#include <sstream>
#include <string>
#include <vector>

namespace pc_translator {

using IFilter = fbpcf::mpc_std_lib::oram::IFilter;
const std::vector<std::string>& PCInstructionSet::getGroupByIds() const {
return groupByIds;
}
Expand All @@ -40,7 +42,22 @@ PCInstructionSet PCInstructionSet::fromDynamic(const folly::dynamic& obj) {
for (auto constraint : constraints) {
auto constraintType = constraint["constraint_type"].asString();
auto constraintValue = constraint["value"].asInt();
FilterConstraint filterConstraint(name, constraintType, constraintValue);

std::map<std::string, IFilter::FilterType> filterMap = {
{"EQ", IFilter::FilterType::EQ},
{"NEQ", IFilter::FilterType::NEQ},
{"LT", IFilter::FilterType::LT},
{"LTE", IFilter::FilterType::LTE},
{"GT", IFilter::FilterType::GT},
{"GTE", IFilter::FilterType::GTE}};

auto it = filterMap.find(constraintType);
if (it == filterMap.end()) {
throw std::invalid_argument(
"Constraint type must be - GT, LT, GTE, LTE, EQ, NEQ");
}

FilterConstraint filterConstraint(name, it->second, constraintValue);
pcInstructionSet.filterConstraints.push_back(filterConstraint);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
id_,opportunity,test_flag,opportunity_timestamp,breakdown_id
cfcd208495d565ef66e7dff9f98764da,1,0,1600000430,0
c4ca4238a0b923820dcc509a6f75849b,1,1,1600000401,1
c81e728d9d4c2f636f067f89cc14862c,0,0,0,2
eccbc87e4b5ce2fe28308fd9f2a7baf3,0,0,0,3
a87ff679a2f3e71d9181a67b7542122c,0,0,0,0
e4da3b7fbbce2345d7772b0674a318d5,1,1,1600000461,4
1679091c5a880faf6fb5e6087eb1b2dc,1,0,1600000052,5
8f14e45fceea167a5a36dedd4bea2543,1,0,1600000831,6
c9f0f895fb98ab9159f51fd0297e236d,1,0,1600000530,7
45c48cce2e2d7fbdea1afc51c7c6ad26,1,0,1600000972,5
c81e728d9d4c2f636f067f89cc14862c,0,0,0,1
eccbc87e4b5ce2fe28308fd9f2a7baf3,0,0,0,1
a87ff679a2f3e71d9181a67b7542122c,0,0,0,2
e4da3b7fbbce2345d7772b0674a318d5,1,1,1600000461,1
1679091c5a880faf6fb5e6087eb1b2dc,1,0,1600000052,1
8f14e45fceea167a5a36dedd4bea2543,1,0,1600000831,2
c9f0f895fb98ab9159f51fd0297e236d,1,0,1600000530,1
45c48cce2e2d7fbdea1afc51c7c6ad26,1,0,1600000972,1
d3d9446802a44259755d38e6d163e820,0,0,0,0
6512bd43d9caa6e02c990b0a82652dca,0,0,0,0
6512bd43d9caa6e02c990b0a82652dca,0,0,0,2
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#include "fbpcs/pc_translator/input_processing/PCInstructionSet.h"

namespace pc_translator {

using IFilter = fbpcf::mpc_std_lib::oram::IFilter;

class TestPCInstructionSet : public ::testing::Test {
public:
protected:
Expand All @@ -36,7 +39,7 @@ TEST_F(TestPCInstructionSet, TestStandardWorkflowTest) {
EXPECT_EQ(groupByIds.size(), 2);
EXPECT_EQ(filterConstraints.size(), 3);
EXPECT_EQ(filterConstraints[0].getName(), "gender");
EXPECT_EQ(filterConstraints[0].getType(), "EQ");
EXPECT_EQ(filterConstraints[0].getType(), IFilter::FilterType::EQ);
EXPECT_EQ(filterConstraints[0].getValue(), 0);
}

Expand Down
4 changes: 2 additions & 2 deletions fbpcs/pc_translator/tests/publisher_unittest.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ cfcd208495d565ef66e7dff9f98764da,1,0,1600000430, 25, 0
c4ca4238a0b923820dcc509a6f75849b,1,1,1600000401, 26, 1
c81e728d9d4c2f636f067f89cc14862c,0,0,0, 44, 0
eccbc87e4b5ce2fe28308fd9f2a7baf3,0,0,0, 23, 0
a87ff679a2f3e71d9181a67b7542122c,0,0,0, 25, 0
a87ff679a2f3e71d9181a67b7542122c,0,0,0, 26, 0
e4da3b7fbbce2345d7772b0674a318d5,1,1,1600000461, 24, 1
1679091c5a880faf6fb5e6087eb1b2dc,1,0,1600000052, 25, 1
8f14e45fceea167a5a36dedd4bea2543,1,0,1600000831, 26, 0
c9f0f895fb98ab9159f51fd0297e236d,1,0,1600000530, 50, 0
45c48cce2e2d7fbdea1afc51c7c6ad26,1,0,1600000972, 25, 1
d3d9446802a44259755d38e6d163e820,0,0,0, 25, 0
6512bd43d9caa6e02c990b0a82652dca,0,0,0, 25, 0
6512bd43d9caa6e02c990b0a82652dca,0,0,0, 26, 0

0 comments on commit 7236864

Please sign in to comment.