Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for GROUP BY with cs_grouped_value_v1 #55

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,16 @@ And is the EQL equivalent of the following plaintext query.
SELECT id FROM examples ORDER BY field DESC;
```

**Grouping example:**

ORE indexes can be used along with the `cs_grouped_value_v1` aggregate function to group by an encrypted column:

```
SELECT cs_grouped_value_v1(encrypted_field) COUNT(*)
FROM users
GROUP BY cs_ore_64_8_v1(encrypted_field)
```

## Querying JSONB data with EQL

### `cs_ste_term_v1(val JSONB, epath TEXT)`
Expand Down Expand Up @@ -354,7 +364,6 @@ Which is the equivalent to the following SQL query:
SELECT attrs->'login_count' FROM users;
```


### Extraction (in WHERE, ORDER BY)

Select rows that match a field in a JSONB object:
Expand All @@ -366,7 +375,20 @@ SELECT * FROM users WHERE cs_ste_term_v1(attrs, 'DQ1rbhWJXmmqi/+niUG6qw') > 'QAJ
Which is the equivalent to the following SQL query:

```sql
SELECT * FROM users WHERE attrs->'login_count' > 10;
SELECT * FROM users WHERE attrs->'login_count' > 10;
```

### Grouping

`cs_ste_vec_term_v1` can be used along with the `cs_grouped_value_v1` aggregate function to group by a field in an encrypted JSONB column:

```
-- $1 here is a param that containts the EQL payload for an ejson path.
-- Example EQL payload for the path `$.field_one`:
-- '{"k": "pt", "p": "$.field_one", "q": "ejson_path", "i": {"t": "users", "c": "attrs"}, "v": 1}'
SELECT cs_grouped_value_v1(cs_ste_vec_value_v1(attrs), $1) COUNT(*)
FROM users
GROUP BY cs_ste_vec_term_v1(attrs, $1);
```

## Managing indexes with EQL
Expand Down
14 changes: 14 additions & 0 deletions sql/010-core.sql
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,17 @@ CREATE FUNCTION cs_ore_64_8_v1(col jsonb)
BEGIN ATOMIC
RETURN cs_ore_64_8_v1_v0_0(col);
END;

DROP FUNCTION IF EXISTS _cs_first_grouped_value(jsonb, jsonb);

CREATE FUNCTION _cs_first_grouped_value(jsonb, jsonb)
RETURNS jsonb AS $$
SELECT COALESCE($1, $2);
$$ LANGUAGE sql IMMUTABLE;

DROP AGGREGATE IF EXISTS cs_grouped_value_v1(jsonb);

CREATE AGGREGATE cs_grouped_value_v1(jsonb) (
SFUNC = _cs_first_grouped_value,
STYPE = jsonb
);
Loading