{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "900d571541564c6bab73cc58acbc73e9",
   "metadata": {},
   "source": [
    "# Delta Ingestion\n",
    "\n",
    "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/lakelogic/LakeLogic/blob/main/examples/03_data_sources/files/delta/delta_demo.ipynb) \n",
    "[![GitHub Repo](https://img.shields.io/badge/GitHub-Repo-blue?logo=github)](https://github.com/lakelogic/LakeLogic/blob/main/examples/03_data_sources/files/delta/delta_demo.ipynb)\n",
    "\n",
    "## Business Scenario\n",
    "\n",
    "Delta tables are common in lakehouses. You need validation and rule enforcement before downstream use.\n",
    "\n",
    "## Value Proposition\n",
    "\n",
    "- Validate Delta inputs\n",
    "- Apply quality rules\n",
    "- Standardize outputs\n",
    "\n",
    "---\n",
    "\n",
    "## Goals\n",
    "\n",
    "1. Read from Delta\n",
    "2. Validate using contract\n",
    "3. Review results\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "987a7357a85d4725a3a9d5abf0ed08b5",
   "metadata": {},
   "source": [
    "## Step 1: Install Dependencies\n",
    "\n",
    "Install the required extras before running this example.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff6e55d33c3a43e2b2f7572fa0615cd0",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "from lakelogic import DataProcessor\n",
    "\n",
    "BASE = Path.cwd()\n",
    "contract_path = BASE / \"delta_contract.yaml\"\n",
    "if not contract_path.exists():\n",
    "    candidate = BASE / \"examples\" / \"03_data_sources\" / \"files\" / \"delta\" / \"delta_contract.yaml\"\n",
    "    if candidate.exists():\n",
    "        BASE = candidate.parent\n",
    "        contract_path = candidate\n",
    "\n",
    "data_dir = BASE / \"data\"\n",
    "data_dir.mkdir(parents=True, exist_ok=True)\n",
    "\n",
    "delta_path = data_dir / \"delta_table\"\n",
    "\n",
    "try:\n",
    "    import deltalake  # noqa: F401\n",
    "\n",
    "    HAS_DELTA = True\n",
    "except Exception:\n",
    "    HAS_DELTA = False\n",
    "\n",
    "if not HAS_DELTA:\n",
    "    print(\"Delta support not installed. Install the LakeLogic delta extra to run this demo.\")\n",
    "else:\n",
    "    seed_contract = {\n",
    "        \"version\": \"1.0.0\",\n",
    "        \"dataset\": \"seed_delta\",\n",
    "        \"materialization\": {\n",
    "            \"strategy\": \"overwrite\",\n",
    "            \"target_path\": str(delta_path),\n",
    "            \"format\": \"delta\",\n",
    "        },\n",
    "        \"server\": {\"format\": \"delta\"},\n",
    "    }\n",
    "    seed_rows = [\n",
    "        {\"id\": 1, \"email\": \"alice@example.com\"},\n",
    "        {\"id\": 2, \"email\": \"\"},\n",
    "        {\"id\": 3, \"email\": \"bob@example.com\"},\n",
    "    ]\n",
    "    try:\n",
    "        DataProcessor(contract=seed_contract).run(seed_rows, materialize=True)\n",
    "    except Exception as exc:\n",
    "        print(f\"Unable to create sample delta table: {exc}\")\n",
    "\n",
    "    if not delta_path.exists():\n",
    "        print(f\"Delta table not available: {delta_path.resolve()}\")\n",
    "    else:\n",
    "        processor = DataProcessor(contract=contract_path)\n",
    "        result = processor.run_source(delta_path)\n",
    "        print(result)\n",
    "        print(f\"Good: {len(result.good)} | Bad: {len(result.bad)}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "610d3bb829c04294af20113cea86f5c4",
   "metadata": {},
   "source": [
    "## Step 2: Review the Contract\n",
    "\n",
    "Open `delta_contract.yaml` to see the schema, source config, and quality rules.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0c4ec93fb6f423092a506a6da6fc93f",
   "metadata": {},
   "source": [
    "## Step 3: Run the Contract\n",
    "\n",
    "Run LakeLogic using the YAML contract. Update credentials as needed.\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}