---
phase: 11-create-semantic-view-parser-hook
plan: 03
type: execute
wave: 2
depends_on: [11-01]
files_modified:
  - src/shim/mod.rs
  - src/lib.rs
  - src/ddl/mod.rs
autonomous: true
requirements: [DDL-05]

must_haves:
  truths:
    - "define_semantic_view() and drop_semantic_view() scalar functions are unregistered and their source files deleted"
    - "lib.rs init_extension passes catalog_raw_ptr and persist_conn to semantic_views_register_shim"
    - "src/ddl/define.rs and src/ddl/drop.rs are deleted from the repository"
    - "ddl/mod.rs no longer exports define or drop modules"
    - "cargo build --features extension succeeds after cleanup"
  artifacts:
    - path: "src/shim/mod.rs"
      provides: "No new FFI declarations needed — catalog FFI functions are no_mangle Rust exports; shim/mod.rs just removed old define/drop references if any"
    - path: "src/lib.rs"
      provides: "Updated init_extension — removes DefineSemanticView/DropSemanticView registrations; passes catalog_state raw pointer and persist_conn to semantic_views_register_shim; persist_conn creation moved before shim call"
    - path: "src/ddl/mod.rs"
      provides: "Only describe and list modules remain"
  key_links:
    - from: "src/lib.rs init_extension"
      to: "src/shim/shim.cpp semantic_views_register_shim"
      via: "Arc::as_ptr for catalog_raw_ptr; persist_conn passed as duckdb_connection"
      pattern: "semantic_views_register_shim.*catalog.*persist"
---

<objective>
Remove the scalar define_semantic_view/drop_semantic_view DDL functions and wire the updated semantic_views_register_shim signature in lib.rs.

Purpose: Satisfies DDL-05 (legacy function removal) and wires the catalog pointer and persist_conn through to the C++ parser hook so it can mutate the in-memory catalog from the scan function.

Output: define.rs and drop.rs deleted; lib.rs updated; ddl/mod.rs trimmed; shim/mod.rs checked for any cleanup.
</objective>

<execution_context>
@/Users/paul/.claude/get-shit-done/workflows/execute-plan.md
@/Users/paul/.claude/get-shit-done/templates/summary.md
</execution_context>

<context>
@.planning/PROJECT.md
@.planning/phases/11-create-semantic-view-parser-hook/11-CONTEXT.md
@.planning/phases/11-create-semantic-view-parser-hook/11-RESEARCH.md
@.planning/phases/11-create-semantic-view-parser-hook/11-01-SUMMARY.md

<interfaces>
<!-- Key code in lib.rs that will be modified. -->

From src/lib.rs (current init_extension — key sections):
```rust
use crate::{
    catalog::init_catalog,
    ddl::{
        define::{DefineSemanticView, DefineState},  // TO BE REMOVED
        describe::DescribeSemanticViewVTab,
        drop::{DropSemanticView, DropState},         // TO BE REMOVED
        list::ListSemanticViewsVTab,
    },
    ...
};

unsafe extern "C" {
    fn semantic_views_register_shim(db_instance_ptr: *mut std::ffi::c_void);
    // UPDATING SIGNATURE to add catalog_raw_ptr and persist_conn
}

fn init_extension(con: &Connection, db_handle: ffi::duckdb_database)
    -> Result<(), Box<dyn Error>> {
    // ... catalog init ...
    let catalog_state = init_catalog(con, &db_path)?;

    // REMOVE: register DefineSemanticView
    con.register_scalar_function_with_state::<DefineSemanticView>(...)?;
    // REMOVE: register DropSemanticView
    con.register_scalar_function_with_state::<DropSemanticView>(...)?;

    // KEEP: list, describe registrations
    con.register_table_function_with_extra_info::<ListSemanticViewsVTab, _>(...)?;
    con.register_table_function_with_extra_info::<DescribeSemanticViewVTab, _>(...)?;

    // persist_conn created here — MOVE BEFORE shim call
    let persist_conn: Option<ffi::duckdb_connection> = ...;

    // ... query_conn, semantic_query, explain_semantic_view registrations ...

    // SHIM CALL — UPDATE SIGNATURE
    unsafe {
        semantic_views_register_shim(db_handle.cast());
        // UPDATE TO:
        // semantic_views_register_shim(
        //     db_handle.cast(),
        //     Arc::as_ptr(&catalog_state) as *const std::ffi::c_void,
        //     persist_conn.unwrap_or(std::ptr::null_mut()),
        // );
    }
}
```

From src/ddl/mod.rs (current):
```rust
pub mod define;   // DELETE
pub mod describe;
pub mod drop;     // DELETE
pub mod list;
```

From src/shim/mod.rs (current):
```rust
// Contains FFI declarations for semantic_views_pragma_define and _drop
// No changes required — these are still used by the C++ scan function indirectly
// The new catalog FFI functions (semantic_views_catalog_*) are #[no_mangle]
// Rust functions — no extern "C" declaration in mod.rs needed
```
</interfaces>
</context>

<tasks>

<task type="auto">
  <name>Task 1: Delete define.rs, drop.rs; trim ddl/mod.rs; update lib.rs imports and shim call</name>
  <files>src/ddl/mod.rs, src/lib.rs</files>
  <action>
  This task removes the legacy DDL functions and updates the Rust wiring.

  **Step 1: Delete src/ddl/define.rs and src/ddl/drop.rs:**
  ```bash
  git rm src/ddl/define.rs src/ddl/drop.rs
  ```
  Use `git rm` so the deletion is staged.

  **Step 2: Update src/ddl/mod.rs** — remove the define and drop pub mod declarations:
  ```rust
  // After: only describe and list remain
  pub mod describe;
  pub mod list;
  ```

  **Step 3: Update src/lib.rs** — four changes:

  a) Update the `use crate::ddl` import block — remove `define` and `drop`:
  ```rust
  use crate::{
      catalog::init_catalog,
      ddl::{
          describe::DescribeSemanticViewVTab,
          list::ListSemanticViewsVTab,
      },
      query::explain::ExplainSemanticViewVTab,
      query::table_function::{QueryState, SemanticViewVTab},
  };
  ```

  b) Update the `unsafe extern "C"` declaration for `semantic_views_register_shim` to match the new C++ signature (from plan 11-02):
  ```rust
  unsafe extern "C" {
      fn semantic_views_register_shim(
          db_instance_ptr: *mut std::ffi::c_void,
          catalog_raw_ptr: *const std::ffi::c_void,
          persist_conn: ffi::duckdb_connection,
      );
  }
  ```

  c) Remove the two `register_scalar_function_with_state` calls for `DefineSemanticView` and `DropSemanticView` from `init_extension`.

  d) Update the shim call at the bottom of `init_extension` to pass catalog and persist_conn:
  ```rust
  // The persist_conn is already created above (for file-backed DBs).
  // Pass the raw Arc pointer (non-owning — Arc stays alive in DefineState/DropState scopes,
  // but those are gone now). The catalog_state Arc lives in QueryState, which is alive
  // for the extension lifetime. Use Arc::as_ptr for a non-owning raw pointer.
  let catalog_raw = Arc::as_ptr(&catalog_state) as *const std::ffi::c_void;
  let raw_persist_conn = persist_conn.unwrap_or(std::ptr::null_mut());

  unsafe {
      semantic_views_register_shim(
          db_handle.cast(),
          catalog_raw,
          raw_persist_conn,
      );
  }
  ```

  **Important:** The `catalog_state` Arc must remain alive for the extension lifetime. It is stored in `QueryState` (passed to `SemanticViewVTab` and `ExplainSemanticViewVTab`). Confirm `QueryState` struct holds `catalog: CatalogState` (an `Arc` clone) — this keeps the Arc's refcount elevated so the raw pointer remains valid.

  **Step 4: Check for any remaining references** to DefineSemanticView, DropSemanticView, DefineState, DropState in lib.rs and remove them.

  After editing, verify the build:
  ```bash
  cargo build --features extension 2>&1 | grep "^error" | head -20
  cargo nextest run 2>&1 | tail -10
  ```
  </action>
  <verify>
    <automated>cargo build --features extension 2>&1 | grep "^error" | wc -l</automated>
  </verify>
  <done>Zero build errors; src/ddl/define.rs and src/ddl/drop.rs no longer exist; ddl/mod.rs has only describe and list; lib.rs passes catalog_raw_ptr and persist_conn to semantic_views_register_shim</done>
</task>

<task type="auto">
  <name>Task 2: Confirm no remaining references to removed functions; verify unit tests pass</name>
  <files>src/shim/mod.rs</files>
  <action>
  **Step 1: Verify DDL-05 — no references to define_semantic_view/drop_semantic_view functions in source:**
  ```bash
  grep -rn "define_semantic_view\|drop_semantic_view\|DefineSemanticView\|DropSemanticView\|DefineState\|DropState" \
      src/ --include="*.rs" --include="*.cpp" --include="*.h"
  ```
  Expected: zero results (the PRAGMA callback names in shim.cpp `define_semantic_view_internal` / `drop_semantic_view_internal` are fine — they are the internal PRAGMA names used by the parser hook, not the scalar functions).

  **Step 2: Check shim/mod.rs** — confirm it does not need new FFI declarations. The four `semantic_views_catalog_*` functions are defined as `#[no_mangle] pub unsafe extern "C"` in `catalog.rs`. C++ calls them via declarations in `shim.h`. The Rust side does NOT need `extern "C"` declarations in `mod.rs` for functions it defines (only for functions it calls from C). Verify mod.rs needs no changes — if the old mod.rs had any references to define/drop that need pruning, update accordingly.

  **Step 3: Run full unit test suite:**
  ```bash
  cargo nextest run 2>&1
  ```
  Expected: all tests pass (model and catalog modules; extension-gated code is excluded from unit tests).

  **Step 4: Run clippy to catch dead code or unused imports:**
  ```bash
  cargo clippy -- -D warnings 2>&1 | grep "^error" | head -20
  ```
  Fix any warnings that become errors.
  </action>
  <verify>
    <automated>cargo nextest run 2>&1 | tail -5</automated>
  </verify>
  <done>cargo nextest run passes; grep for removed function names returns zero results in src/; clippy passes with no errors</done>
</task>

</tasks>

<verification>
Check DDL-05 compliance:
```bash
grep -rn "define_semantic_view\b\|drop_semantic_view\b" \
    src/ --include="*.rs" --include="*.cpp" --include="*.h" 2>&1
```
Expected: zero matches for the scalar function names (PRAGMA internal names like `define_semantic_view_internal` are fine).

Check deleted files:
```bash
ls src/ddl/define.rs src/ddl/drop.rs 2>&1
```
Expected: "No such file or directory".

Full build:
```bash
cargo build --features extension 2>&1 | grep "^error" | wc -l
```
Expected: 0.
</verification>

<success_criteria>
- `src/ddl/define.rs` and `src/ddl/drop.rs` are deleted (git rm)
- `src/ddl/mod.rs` contains only `pub mod describe` and `pub mod list`
- `src/lib.rs` passes `catalog_raw_ptr` and `persist_conn` to `semantic_views_register_shim`
- `cargo build --features extension` succeeds
- `cargo nextest run` passes
- No references to DefineSemanticView, DropSemanticView, DefineState, DropState in any .rs/.cpp/.h file
</success_criteria>

<output>
After completion, create `.planning/phases/11-create-semantic-view-parser-hook/11-03-SUMMARY.md`
</output>
