# Requirements Document

## Introduction

This feature addresses a critical bug in the SQL function extraction logic where column names and SQL keywords within function bodies are incorrectly identified as function names. The bug causes test failures in the develop branch due to incorrect parsing of CREATE FUNCTION statements, specifically when column names like "price" appear in SELECT statements within function bodies.

## Glossary

- **SQL Function**: A CREATE FUNCTION statement in SQL that defines a reusable function
- **Function Body**: The code between BEGIN and END in a SQL function definition
- **Function Signature**: The CREATE FUNCTION line that declares the function name, parameters, and return type
- **Tree-sitter AST**: Abstract Syntax Tree generated by the tree-sitter parser
- **Regex Pattern**: Regular expression used to match text patterns
- **Golden Master Test**: Regression test that compares current output against a known-good reference output

## Requirements

### Requirement 1

**User Story:** As a developer using the SQL analyzer, I want CREATE FUNCTION statements to be correctly parsed, so that only actual function names are extracted and not column names or keywords within function bodies.

#### Acceptance Criteria

1. WHEN the system parses a CREATE FUNCTION statement THEN the system SHALL extract only the function name from the function signature line
2. WHEN the system encounters column names within a function body THEN the system SHALL NOT extract those column names as function names
3. WHEN the system encounters SQL keywords within a function body THEN the system SHALL NOT extract those keywords as function names
4. WHEN the system parses the sample_database.sql file THEN the system SHALL extract exactly 2 functions: calculate_order_total and is_user_active
5. WHEN the system parses a CREATE FUNCTION statement THEN the system SHALL correctly identify the start line as the CREATE FUNCTION line and the end line as the END statement line

### Requirement 2

**User Story:** As a developer maintaining the codebase, I want the SQL function extraction logic to use a precise regex pattern, so that false positives are eliminated and the code is maintainable.

#### Acceptance Criteria

1. WHEN the regex pattern matches a CREATE FUNCTION statement THEN the system SHALL ensure the pattern only matches at the beginning of a CREATE FUNCTION declaration
2. WHEN the regex pattern is evaluated THEN the system SHALL require that CREATE and FUNCTION keywords appear together without intervening SQL statements
3. WHEN the system validates a potential function name THEN the system SHALL reject names that are common SQL column names
4. WHEN the system validates a potential function name THEN the system SHALL reject names that are SQL reserved keywords
5. WHEN the system processes a function body THEN the system SHALL NOT apply the CREATE FUNCTION regex pattern to lines within the function body

### Requirement 3

**User Story:** As a developer running tests, I want the golden master regression tests to pass consistently across all branches, so that I can confidently merge code changes.

#### Acceptance Criteria

1. WHEN the golden master test runs on the develop branch THEN the system SHALL produce output matching the golden master CSV file
2. WHEN the system extracts SQL elements from sample_database.sql THEN the system SHALL produce exactly 19 lines of CSV output
3. WHEN the system outputs SQL functions in CSV format THEN the system SHALL list functions in the order they appear in the source file
4. WHEN the system completes SQL extraction THEN the system SHALL NOT include any spurious "price" function in the output
5. WHEN tests run on different branches THEN the system SHALL produce identical output for the same input files
