-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add base datatype API doc and examples. #220
Conversation
docs/09_datatypes.md
Outdated
@@ -0,0 +1,384 @@ | |||
# Trusted Language Base types | |||
|
|||
PostgreSQL provides `CREATE TYPE` command to register a new base type (scalar type) for use in the current database. Base type allows you to customized how the data is stored interally and how to converts it from/to an external textual representation. The support functions `input_function` and `output_function` are required. The `input_function` converts the type's external textual representation to the internal representation. `output_function` performs the reverse transformation. Generally they have to be coded in C or another low-level language. Also, you must be a superuser to create a new base type. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Base type allows you to customized --> A base type allows you to customize
spelling : internally
how to converts --> how to convert
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
docs/09_datatypes.md
Outdated
|
||
### `pgtle.create_shell_type(typenamespace regnamespace, typename name)` | ||
|
||
`create_shell_type` provides a way to create a shell type, which is simply a placeholder for a type to be defined later. This is similar to shell type form of `CREATE TYPE`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we embed a link here to Postgres docs for shell type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
docs/09_datatypes.md
Outdated
|
||
### `pgtle.create_base_type(typenamespace regnamespace, typename name, infunc regprocedure, outfunc regprocedure, internallength int4)` | ||
|
||
`create_base_type` provides a way to create a new base data type. The type must be a shell type previously defined by `create_shell_type`. This is similar to base type form of `CREATE TYPE` command. Internally, a base data type created by `pg_tle` is stored as `bytea`, it can be casted to `bytea` explicitly after creation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
casted --> cast
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
docs/09_datatypes.md
Outdated
|
||
* `typenamespace`: The namespace where the base data type will be created. | ||
* `typename`: The name of the base data type. | ||
* `infunc`: The name of a previously defined function to converts the type's external textual representation to the internal representation (`bytea`). The function must be declared as taking one argument of type `text` and returning `bytea`. The function must also be declared `IMMUTABLE` and `STRICT`. It will not be called with a NULL parameter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
previously defined function to coverts --> previously defined function to covert
The function must take one argument...and return bytea
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
docs/09_datatypes.md
Outdated
* `typenamespace`: The namespace where the base data type will be created. | ||
* `typename`: The name of the base data type. | ||
* `infunc`: The name of a previously defined function to converts the type's external textual representation to the internal representation (`bytea`). The function must be declared as taking one argument of type `text` and returning `bytea`. The function must also be declared `IMMUTABLE` and `STRICT`. It will not be called with a NULL parameter. | ||
* `outfunc`: The name of a previously defined function to converts the type's internal binary representation (`bytea`) to the external textual representation. The function must be declared as taking one argument of type `bytea` and returning `text`. The function must also be declared `IMMUTABLE` and `STRICT`. It will not be called with a NULL parameter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same comments as for infunc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
|
||
```sql | ||
SELECT pgtle.create_base_type('public', 'test_citext', 'test_citext_in(text)'::regprocedure, 'test_citext_out(bytea)'::regprocedure, -1); | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we include a fixed-length example as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
|
||
* `typenamespace`: The namespace where the new operator function will be created. It must be the same namespace where the base data type is. | ||
* `typename`: The name of the base data type. | ||
* `opfunc`: The name of a previously defined operator function. The function must be declared as taking one or two arguments of type `bytea`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
must take exactly one or two ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
docs/09_datatypes.md
Outdated
``` | ||
|
||
## Examples | ||
The following examples demonstrates how to use `pg_tle` data type APIs functions to create a base data type. After running this example, a base data type called `test_citext` (case-insentive text) will be available for use in the current database. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
demonstrates -> demonstrate
APIs -> API
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
docs/09_datatypes.md
Outdated
The following examples demonstrates how to use `pg_tle` data type APIs functions to create a base data type. After running this example, a base data type called `test_citext` (case-insentive text) will be available for use in the current database. | ||
|
||
### Create shell type | ||
First, you will need to use `pgtle.create_shell_type` to create a shell type in the target namespace (PUBLIC in our example). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd drop 'you' from the docs. Maybe 'we'. Or rephrase as
First, pgtle.create_shell_type
is called to create a shell type in the target namespace (PUBLIC in this example)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll drop most you
from the docs, and use either an imperative sentence or we
.
docs/09_datatypes.md
Outdated
``` | ||
|
||
### Create I/O functions | ||
Second, you will need to define the input/output function on the data type. Remember the input function takes one argument of type `text` and returns `bytea`; while the output function takes one argument of type `bytea` and returns `text`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
similar comment to above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
docs/09_datatypes.md
Outdated
INSERT INTO test_dt VALUES ('SELECT'), ('INSERT'), ('UPDATE'), ('DELETE'); | ||
INSERT INTO test_dt VALUES ('select'); | ||
``` | ||
`'SELECT'` and `'select'` is considered different values at this moment because we haven't defined related operators and operator class for the type. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is -> are
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
docs/09_datatypes.md
Outdated
`'SELECT'` and `'select'` is considered different values at this moment because we haven't defined related operators and operator class for the type. | ||
|
||
### Create operator functions | ||
After the base data type is created, you can define operators and operator class if needed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
similar as comments before. "... can be defined"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated
docs/09_datatypes.md
Outdated
``` | ||
|
||
### Alternative way to operator functions | ||
Alternatively, you can use `pgtle.create_operator_func` to create operator functions without explicit cast. It can be really helpful in certains languages such as plrust where the newly created type is not available. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can be used
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated
docs/09_datatypes.md
Outdated
); | ||
``` | ||
|
||
Run following command to create operator class. Note you need superuser permission here. If you are using Amazon RDS, superuser permission is not required. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
superuser permission is required...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
docs/09_datatypes.md
Outdated
``` | ||
|
||
## Examples | ||
The following examples demonstrates how to use `pg_tle` data type APIs functions to create a base data type. After running this example, a base data type called `test_citext` (case-insentive text) will be available for use in the current database. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the example is fairly long, I'm thinking that it helps to have a sql file for the example in addition to the step by step walkthrough in the doc. We don't have an examples directory, which me might want to add now. Or we may have some other place outside of this repo for examples, but I'm not sure where and that could be set up in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea! We already have a few example files, each example file corresponds to one language. I'm thinking if we should add the data type examples to the existing ones or create separate one for data type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think an example in a single language in the docs is enough for now -- we can/should revisit adding examples in other languages and pulling out the examples into .sql files
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. I've added the datatype examples in plpgsql example file. I guess we can revisit/re-structure the example files later.
Description of changes:
Add data type API doc and examples
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.