Skip to content
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

Merged
merged 6 commits into from
Jul 24, 2023
Merged

Conversation

lyupan
Copy link
Contributor

@lyupan lyupan commented Jul 21, 2023

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.

@@ -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.
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


### `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`.
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


### `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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

casted --> cast

Copy link
Contributor Author

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 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.
Copy link
Contributor

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.

Copy link
Contributor Author

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 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.
Copy link
Contributor

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

Copy link
Contributor Author

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);
```
Copy link
Contributor

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?

Copy link
Contributor Author

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`.
Copy link
Contributor

@formalLogicGirl formalLogicGirl Jul 21, 2023

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 ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

```

## 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

demonstrates -> demonstrate
APIs -> API

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

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).
Copy link
Contributor

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)

Copy link
Contributor Author

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.

```

### 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`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar comment to above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is -> are

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

`'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.
Copy link
Contributor

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"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

```

### 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be used

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

);
```

Run following command to create operator class. Note you need superuser permission here. If you are using Amazon RDS, superuser permission is not required.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

superuser permission is required...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

```

## 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.
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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

Copy link
Contributor Author

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.

@formalLogicGirl formalLogicGirl merged commit 7c06d9c into aws:main Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants