{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9d5dfbe1",
   "metadata": {},
   "source": [
    "# Using the Session Builder to Answer Private Join Queries"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53db2a7e",
   "metadata": {},
   "source": [
    "## Import Libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1dc2854d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyspark.sql import SparkSession\n",
    "\n",
    "from tmlt.analytics.privacy_budget import RhoZCDPBudget\n",
    "from tmlt.analytics.session import Session\n",
    "from tmlt.analytics.query_builder import QueryBuilder\n",
    "from tmlt.analytics.truncation_strategy import TruncationStrategy"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1804017",
   "metadata": {},
   "source": [
    "## Build Session"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "39ec2bc8",
   "metadata": {},
   "outputs": [],
   "source": [
    "spark = SparkSession.builder.getOrCreate()\n",
    "\n",
    "left_df = spark.createDataFrame(\n",
    "    [(1, \"A\"), (1, \"B\"), (2, \"B\"), (1, \"C\"), (2, \"C\"), (3, \"C\")], schema=[\"X\", \"Y\"],\n",
    ")\n",
    "\n",
    "right_df = spark.createDataFrame(\n",
    "    [(\"A\", 4), (\"A\", 5), (\"B\", 4), (\"C\", 4), (\"C\", 5),], schema=[\"Y\", \"Z\"],\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8b71b3ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "session = ( \n",
    "    Session.Builder()\n",
    "    .with_privacy_budget(privacy_budget=RhoZCDPBudget(float(\"inf\")))\n",
    "    .with_private_dataframe(source_id=\"left\", dataframe=left_df, stability=1)\n",
    "    .with_private_dataframe(source_id=\"right\", dataframe=right_df, stability=1)\n",
    "    .build()\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "756e1e7f",
   "metadata": {},
   "source": [
    "## Private Join Queries\n",
    "Using budget=inf to illustrate the truncation methods."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "826ad336",
   "metadata": {},
   "source": [
    "Truncate both tables by dropping all records with duplicate join keys."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3034589c",
   "metadata": {},
   "outputs": [],
   "source": [
    "query1 = QueryBuilder(\"left\").join_private(\n",
    "    right_operand=QueryBuilder(\"right\"),\n",
    "    truncation_strategy_left=TruncationStrategy.DropNonUnique(),\n",
    "    truncation_strategy_right=TruncationStrategy.DropNonUnique(),\n",
    ").count()\n",
    "\n",
    "answer = session.evaluate(query1, RhoZCDPBudget(float(\"inf\")))\n",
    "answer.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f629220",
   "metadata": {},
   "source": [
    "Truncate both tables with threshold = 1, dropping records above that threshold."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8502cf60",
   "metadata": {},
   "outputs": [],
   "source": [
    "query2 = QueryBuilder(\"left\").join_private(\n",
    "    right_operand=QueryBuilder(\"right\"),\n",
    "    truncation_strategy_left=TruncationStrategy.DropExcess(1),\n",
    "    truncation_strategy_right=TruncationStrategy.DropExcess(1),\n",
    ").count()\n",
    "\n",
    "answer = session.evaluate(query2, RhoZCDPBudget(float(\"inf\")))\n",
    "answer.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d767f455",
   "metadata": {},
   "source": [
    "Truncate both tables with left threshold = 2 and right threshold = 1, dropping records above that threshold."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "24c16af4",
   "metadata": {},
   "outputs": [],
   "source": [
    "query4 = QueryBuilder(\"left\").join_private(\n",
    "    right_operand=QueryBuilder(\"right\"),\n",
    "    truncation_strategy_left=TruncationStrategy.DropExcess(2),\n",
    "    truncation_strategy_right=TruncationStrategy.DropExcess(1),\n",
    "    join_columns=[\"Y\"]\n",
    ").count()\n",
    "\n",
    "answer = session.evaluate(query4, RhoZCDPBudget(float(\"inf\")))\n",
    "answer.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a81527a",
   "metadata": {},
   "source": [
    "Truncate both tables, with left threshold = 2 dropping records above that threshold, and dropping all records with duplicate join keys in the right table."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d6d8e27",
   "metadata": {},
   "outputs": [],
   "source": [
    "query5 = QueryBuilder(\"left\").join_private(\n",
    "    right_operand=QueryBuilder(\"right\"),\n",
    "    truncation_strategy_left=TruncationStrategy.DropExcess(2),\n",
    "    truncation_strategy_right=TruncationStrategy.DropNonUnique(),\n",
    "    join_columns=[\"Y\"]\n",
    ").count()\n",
    "\n",
    "answer = session.evaluate(query5, RhoZCDPBudget(float(\"inf\")))\n",
    "answer.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
