Skip to content

Commit

Permalink
Implement SQL text -> Garble enum conversion in Postgres example
Browse files Browse the repository at this point in the history
  • Loading branch information
fkettelhoit committed Jun 11, 2024
1 parent 11420c1 commit ce0cefa
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 12 deletions.
25 changes: 20 additions & 5 deletions examples/postgres-integration/.example.garble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,34 @@ const ROWS_1: usize = PARTY_1::ROWS;
const ROWS_RESULT: usize = max(PARTY_0::ROWS, PARTY_1::ROWS);
const STR_LEN: usize = max(PARTY_0::STR_LEN, PARTY_1::STR_LEN);

enum InsuranceStatus {
Foo,
Bar,
FooBar,
}

pub fn main(
residents: [(i32, [u8; STR_LEN], i32); ROWS_0],
insurance: [(i32, [u8; STR_LEN], bool); ROWS_1],
insurance: [(i32, [u8; STR_LEN], InsuranceStatus); ROWS_1],
) -> [[u8; STR_LEN]; ROWS_RESULT] {
let mut result = [[0u8; STR_LEN]; ROWS_RESULT];
let mut i = 0usize;
for resident in residents {
for insurance in insurance {
let (_, name0, age) = resident;
let (_, name1, health_problems) = insurance;
if name0 == name1 && health_problems == true && age > 65i32 {
result[i] = name0;
i = i + 1usize;
let (_, name1, insurance_status) = insurance;
if name0 == name1 && age > 65i32 {
match insurance_status {
InsuranceStatus::Foo => {
result[i] = name0;
i = i + 1usize;
}
InsuranceStatus::Bar => {
result[i] = name0;
i = i + 1usize;
}
_ => {}
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/postgres-integration/policies1.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"program": ".example.garble.rs",
"leader": 0,
"party": 1,
"input": "SELECT id, name, disabled FROM insurance",
"input": "SELECT id, name, status FROM insurance",
"input_db": "postgres://postgres:test@localhost:5551/postgres",
"constants": {
"ROWS": {
Expand Down
2 changes: 1 addition & 1 deletion examples/postgres-integration/seeds/db1/init.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CREATE TABLE insurance (
id SERIAL PRIMARY KEY,
name TEXT,
disabled BOOL,
status TEXT,
address TEXT
);

Expand Down
7 changes: 4 additions & 3 deletions examples/postgres-integration/seeds/db1/insurance.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Id,Name,Disabled,Address
0,"Max Doe",true,"Parkstraße 1, 12345 Berlin"
1,"Bob Doe",false,"Schlossallee 2, 12345 Berlin"
Id,Name,Status,Address
0,"Max Doe","foo","Parkstraße 1, 12345 Berlin"
1,"Bob Doe","bar","Schlossallee 2, 12345 Berlin"
2,"Jane Doe","foobar","Schlossallee 2, 12345 Berlin"
23 changes: 21 additions & 2 deletions examples/postgres-integration/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use parlay::{
channel::Channel,
fpre::fpre,
garble_lang::{
ast::Type,
ast::{Type, Variant},
compile_with_constants,
literal::Literal,
literal::{Literal, VariantLiteral},
token::{SignedNumType, UnsignedNumType},
},
protocol::{mpc, Preprocessor},
Expand Down Expand Up @@ -507,6 +507,25 @@ async fn execute_mpc(
literal = Some(Literal::Array(fixed_str))
}
}
Type::Enum(name) => {
let Some(enum_def) = prg.program.enum_defs.get(name) else {
bail!("Could not find definition for enum {name} in program");
};
if let Ok(variant) = row.try_get::<String, _>(c) {
for v in &enum_def.variants {
if let Variant::Unit(variant_name) = v {
if variant_name.to_lowercase() == variant.to_lowercase() {
literal = Some(Literal::Enum(
name.clone(),
variant_name.clone(),
VariantLiteral::Unit,
));
break;
}
}
}
}
}
_ => {}
}
if let Some(literal) = literal {
Expand Down

0 comments on commit ce0cefa

Please sign in to comment.