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 option to not use SmartnoiseSQL #77

Merged
merged 5 commits into from
Apr 26, 2023
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pyyaml = "^5.0"
sqlalchemy = "^1.4"
sphinx-rtd-theme = {version = "^1.2.0", optional = true}
sphinxcontrib-napoleon = {version = "^0.7", optional = true}
smartnoise-sql = "^0.2.9.1"
smartnoise-sql = "^0.2.11"

[tool.poetry.group.dev.dependencies]
black = "^22.10.0"
Expand Down
33 changes: 24 additions & 9 deletions sqlsynthgen/make.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
import inspect
from sys import stderr
from types import ModuleType
from typing import Any, Final, Optional
from typing import Any, Dict, Final, Optional

import snsql
from mimesis.providers.base import BaseProvider
from pydantic import PostgresDsn
from sqlacodegen.generators import DeclarativeGenerator
from sqlalchemy import MetaData, create_engine
from sqlalchemy import MetaData, create_engine, text
from sqlalchemy.sql import sqltypes

from sqlsynthgen import providers
Expand Down Expand Up @@ -256,12 +256,15 @@ def make_src_stats(
else:
engine = create_engine(dsn, echo=False, future=True)

dp_config = config.get("smartnoise-sql", {})
snsql_metadata = {"": dp_config}
src_stats = {}
for stat_data in config.get("src-stats", []):
privacy = snsql.Privacy(epsilon=stat_data["epsilon"], delta=stat_data["delta"])
with engine.connect() as conn:
use_smartnoise_sql = config.get("use-smartnoise-sql", True)
if use_smartnoise_sql:
dp_config = config.get("smartnoise-sql", {})
snsql_metadata = {"": dp_config}

def execute_query(conn: Any, stat_data: Dict[str, Any]) -> Any:
privacy = snsql.Privacy(
epsilon=stat_data["epsilon"], delta=stat_data["delta"]
)
reader = snsql.from_connection(
conn.connection,
engine="postgres",
Expand All @@ -270,5 +273,17 @@ def make_src_stats(
)
private_result = reader.execute(stat_data["query"])
# The first entry in the list names the columns, skip that.
src_stats[stat_data["name"]] = private_result[1:]
return private_result[1:]

else:

def execute_query(conn: Any, stat_data: Dict[str, Any]) -> Any:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should type the return as List[List] (or Iterable[Iterable]) or similar?

Suggested change
def execute_query(conn: Any, stat_data: Dict[str, Any]) -> Any:
def execute_query(conn: Any, stat_data: Dict[str, Any]) -> List[List]:

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is we need to change the type signature of execute_query in both branches of the if statement to keep mypy happy (All conditional function variants must have identical signatures), and mypy doesn't know what the return type of SNSQL's private_reader.execute is.

result = conn.execute(text(stat_data["query"])).fetchall()
return [list(r) for r in result]

with engine.connect() as conn:
src_stats = {
stat_data["name"]: execute_query(conn, stat_data)
for stat_data in config.get("src-stats", [])
}
return src_stats
2 changes: 2 additions & 0 deletions tests/test_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,13 @@ def test_make_stats(self) -> None:
conf_path = Path("example_config.yaml")
with open(conf_path, "r", encoding="utf8") as f:
config = yaml.safe_load(f)
config_no_snsql = {**config, "use-smartnoise-sql": False}

# Check that make_src_stats works with, or without, a schema
for args in (
(connection_string, config),
(connection_string, config, "public"),
(connection_string, config_no_snsql),
):

src_stats = make_src_stats(*args)
Expand Down