apache / apache/datafusion-sqlparser-rs
Parsing of SQL Functions body
- Dominant language
- Rust
- Stars
- 3.5k
- Forks
- 772
- Avg merge
- 4d 9h
- Merged PRs (30d)
- 17
Description
It is my understanding that the current implementation of CREATE FUNCTION does not support the parsing of function blocks.
For instance, the following function body gets converted to a simple `DollarQuotedString`, as shown below. I need to be able to parse the function bodies to write any script that may be able to execute them, so I will be getting started doing that - is there any pull request that has already attempted this that I should be aware of?
```sql
CREATE FUNCTION emp_stamp() RETURNS trigger AS $emp_stamp$
BEGIN
-- Check that empname and salary are given
IF NEW.empname IS NULL THEN
RAISE EXCEPTION 'empname cannot be null';
END IF;
IF NEW.salary IS NULL THEN
RAISE EXCEPTION '% cannot have null salary', NEW.empname;
END IF;
-- Who works for us when they must pay for it?
IF NEW.salary < 0 THEN
RAISE EXCEPTION '% cannot have a negative salary', NEW.empname;
END IF;
-- Remember who changed the payroll when
NEW.last_date := current_timestamp;
NEW.last_user := current_user;
RETURN NEW;
END;
$emp_stamp$ LANGUAGE plpgsql;
```
```rust
Statement::CreateFunction {
or_replace: false,
temporary: false,
if_not_exists: false,
name: ObjectName(vec![Ident::new("emp_stamp")]),
args: None,
return_type: Some(DataType::Trigger),
function_body: Some(
CreateFunctionBody::AsBeforeOptions(
Expr::Value(
Value::DollarQuotedString(
DollarQuotedString {
value: "\n BEGIN\n -- Check that empname and salary are given\n IF NEW.empname IS NULL THEN\n RAISE EXCEPTION 'empname cannot be null';\n END IF;\n IF NEW.salary IS NULL THEN\n RAISE EXCEPTION '% cannot have null salary', NEW.empname;\n END IF;\n \n -- Who works for us when they must pay for it?\n IF NEW.salary < 0 THEN\n RAISE EXCEPTION '% cannot have a negative salary', NEW.empname;\n END IF;\n \n -- Remember who changed the payroll when\n NEW.last_date := current_timestamp;\n NEW.last_user := current_user;\n RETURN NEW;\n END;\n ".to_owned(),
tag: Some(
"emp_stamp".to_owned(),
),
},
),
),
),
),
behavior: None,
called_on_null: None,
parallel: None,
using: None,
language: Some(Ident::new("plpgsql")),
determinism_specifier: None,
options: None,
remote_connection: None
}
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by reading the CREATE FUNCTION parser and the CreateFunctionBody, DollarQuotedString, and Statement::CreateFunction types shown in the issue. Compare the current opaque function-body representation with the requested parsing behavior and identify the parser tests covering CREATE FUNCTION. Done means supported function blocks are parsed in a representation suitable for downstream script execution, with tests for the supplied PL/pgSQL example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust, sql
- Domain
- compilers, databases
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100