Skip to content

feat(extract): add Oracle PL/SQL support via tree-sitter-plsql grammar #1992

Open

Description

@ezadEzanee

Feature: Oracle PL/SQL Support via tree-sitter-plsql

Summary

Add first-class Oracle PL/SQL support to graphify by integrating the njank/tree-sitter-plsql grammar, enabling extraction of packages, functions, procedures, and their table dependencies from .sql, .pkb, .pks, .pls, and .plb files.

Motivation

Many enterprise codebases use Oracle databases with PL/SQL packages — bundles of stored procedures, functions, cursors, and types. These packages are the "glue logic" between application code and database schemas. Without PL/SQL support, graphify cannot see this critical middle layer.

The existing tree_sitter_sql grammar handles standard SQL DDL (CREATE TABLE/VIEW) but cannot parse Oracle PL/SQL constructs like CREATE PACKAGE BODY, CREATE FUNCTION, PROCEDURE, FORALL, BULK COLLECT, REF CURSOR, or Oracle-specific outer join syntax.

Tree-Sitter Grammar: njank/tree-sitter-plsql

A fork of andreasmaierde/tree-sitter-plsql (12 ★) with significant enhancements for enterprise Oracle codebases:

Feature Status
CREATE [OR REPLACE] PACKAGE / PACKAGE BODY ✅
FUNCTION/PROCEDURE declarations (spec) and definitions (body) ✅
Parameters with IN/OUT/IN OUT and %TYPE ✅
CREATE TABLE/VIEW with constraints, REFERENCES ✅
Embedded DML (SELECT, INSERT, UPDATE, DELETE, MERGE) ✅
CURSOR definitions, TYPE definitions (REF CURSOR, collections) ✅
FORALL, BULK COLLECT ✅
Conditional compilation ($IF/$THEN/$ELSE) ✅
Analytic functions with OVER/ WITHIN GROUP ✅
Oracle (+) outer joins ✅
Quoted identifiers, EDITIONABLE packages ✅
External scanner for Oracle keyword disambiguation ✅

Evidence: Python Bindings Work

I built Python bindings for this grammar (following the same pattern as tree_sitter_sql) and tested against production Oracle PL/SQL:

Test file Error rate Notes
Clean hand-written spec 0.0% No grammar bugs on valid PL/SQL
Hand-written spec with EDITIONABLE + quoted schema 0.0% Handles enterprise patterns
Advanced constructs (REF CURSOR, FORALL, analytic) 0.0% Full coverage
XXDB_EPP_PRJ.pkb (204 KB production body) 0.98% All errors are line-truncation artifacts from extraction, not grammar
Various extracted .pkb files (avg 23-56 KB) <5.5% All errors are truncated-line artifacts (80-char line split)

Critically: on clean Oracle PL/SQL, the grammar parses with 0% error rate. The errors seen are from SQL*Plus extraction artifacts where long lines were truncated mid-identifier.

Proposed Integration

1. Python package

The njank/tree-sitter-plsql grammar compiles to a C .so and exports language() as a PyCapsule, matching the tree_sitter_sql API exactly. The package structure:

tree-sitter-plsql/
├── pyproject.toml    # setuptools build (C extension)
├── binding.c         # Python capsule wrapper
├── src/
│   ├── parser.c      # Generated parser (1.44M lines, committed)
│   ├── scanner.c     # 57-keyword external scanner (committed)
│   └── ...
└── tree_sitter_plsql/
    ├── __init__.py    # Same pattern as tree_sitter_sql
    └── queries/

Currently built as an editable install; ready for PyPI publication.

2. Changes to graphify upstream

graphify/extractors/sql.py (~50-80 lines added)

  • Try tree-sitter-plsql first, fall back to tree_sitter_sql
  • Handle PL/SQL-specific node types: create_package, create_package_body, function_declaration, procedure_declaration, function_definition, procedure_definition, cursor_definition, type_definition_*
  • Walk SQL statements embedded inside PL/SQL bodies for table references
  • Adapt to different node naming (e.g., identifier instead of object_reference for table names)

Node type mapping:

Concept tree_sitter_sql tree-sitter-plsql
Table name object_reference identifier
Column def column_definition table_column_definition
FK constraint keyword_references constraint_references
Function (spec) create_function function_declaration
Procedure (spec) create_procedure procedure_declaration
Function (body) — function_definition
Procedure (body) — procedure_definition
Package — create_package / create_package_body
Statement wrapper statement not used (top-level)

graphify/detect.py (~5 lines)

Add .pkb, .pks, .pls, .plb to CODE_EXTENSIONS and the extension→extractor mapping.

pyproject.toml (~2 lines)

Add tree-sitter-plsql as an optional dependency alongside tree-sitter-sql.

Next Steps

  1. Publish tree-sitter-plsql on PyPI (can be under any org/account)
  2. Submit PR to graphify with the extractor + detect changes
  3. Add queries (highlights.scm, locals.scm, tags.scm) for code navigation

I have a working prototype with the compiled bindings, tested against production code. Happy to submit a PR with the integration once the package is published.

Related

  • Grammar repo: github.com/njank/tree-sitter-plsql
  • Upstream grammar (original): github.com/andreasmaierde/tree-sitter-plsql

Activity

  1. ezadEzanee commented on Jul 18, 2026

    @ezadEzanee
    Author

    Integration Update — Working Prototype ✅

    The extraction, detection, and graph building pipeline has been verified end-to-end on production Oracle PL/SQL code.

    What's been done

    1. ✅ Python bindings: Built, hard-installed in graphify's venv (no longer editable)
    2. ✅ Dual import: extractors/sql.py now tries tree-sitter-plsql first, falls back to tree-sitter-sql
    3. ✅ PL/SQL-specific extraction: Handles create_package, create_package_body, function/procedure declarations/definitions, cursor definitions, type definitions
    4. ✅ Regex fallback for SQL*Plus extracts: Files starting with PACKAGE BODY name (without CREATE OR REPLACE) are recovered via regex scanning — critical for real-world codebases where extraction pipelines strip the header
    5. ✅ Extension registration: .pkb, .pks, .pls, .plb added to CODE_EXTENSIONS
    6. ✅ End-to-end test: graphify update . on a .pkb file correctly extracts package, procedures, and functions into the graph

    Tested with production code

    File Size Nodes extracted Edges
    XXDB_EPP_ATTACHMENT.pkb 5.7 KB 3 + file 10
    XXDB_EPP_PO.pkb 31.5 KB 15 + file 32
    XXDB_EPP_INV.pkb 29 KB 16 + file 32

    Next steps for PR readiness

    • Publish tree-sitter-plsql to PyPI so graphify can declare it as an optional dependency
    • Clean up the extractor changes and submit as a formal PR
    • Add locals.scm and tags.scm queries for code navigation features
  2. cmalek commented on Aug 4, 2026

    @cmalek

    this would be extremely handy for me also

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions