-
Notifications
You must be signed in to change notification settings - Fork 0
Documentation for Enum support #129
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| # Enum Types | ||
|
|
||
| Just like in many programming languages, an enum types consist of a static, ordered set of labels defined by the user. | ||
| They combine the clarity of text with the compactness of numerics, when the inherent meaning of a value is more than should be encoded by a single number, but the number of possible values is relatively small. | ||
penguincarol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ## Creation of Enum types | ||
|
|
||
| Enum types can be created via the Create Type command. | ||
|
|
||
| ```sql | ||
| create type enum_name as enum (['label' [,...]]); | ||
| ``` | ||
|
|
||
| An example usage could look like this: | ||
|
|
||
| ```sql | ||
| create type importance as enum ('minor', 'major', 'critical'); | ||
| ``` | ||
|
|
||
| ## Usage of Enum types | ||
|
|
||
| Enums can be used just like any other type inside of tables, views, queries, etc. . | ||
|
|
||
| ```sql | ||
| create table tasks (id int, priority importance); | ||
| insert into tasks values (1, 'major'), (2, 'minor'), (3, 'critical'), (4, 'major'); | ||
| ``` | ||
|
|
||
| The enum labels are case sensitive, whereas the enum names are not. | ||
|
|
||
| This does not work: | ||
| ```sql | ||
| select 'mInOr'::importance; | ||
| ``` | ||
| ``` | ||
| ERROR: invalid input value for enum importance: "mInOr" | ||
| ``` | ||
|
|
||
| This works: | ||
| ```sql | ||
| select 'minor'::iMpOrTaNcE; | ||
| ``` | ||
| ``` | ||
| enum importance | ||
| --------------- | ||
| minor | ||
| ``` | ||
|
|
||
| ## Comparison of Enum types | ||
|
|
||
| Values of the same enum type are comparable. Their ordering corresponds the order in which they were listed at creation time. Values of different enum types are incomparable. Similarly, an enum cannot be compared with a builtin type. | ||
|
|
||
|
|
||
| In this example ID2 gets filtered out as its corresponding priority is too low in the enum ordering. | ||
| ```sql | ||
| select id, priority from tasks where priority >= 'major'; | ||
| ``` | ||
|
|
||
| ``` | ||
| id | priority | ||
| ------------- | ||
| 1 | major | ||
| 3 | critical | ||
| 4 | major | ||
| ``` | ||
|
|
||
| ```sql | ||
| select id from tasks where priority > 1; | ||
| ``` | ||
| ``` | ||
| ERROR: cannot compare enum importance and integer | ||
| ``` | ||
| ## Deletion of Enum types | ||
|
|
||
| Enum types can be removed via the Drop Type command. | ||
|
|
||
| ```sql | ||
| drop type [if exists] name; | ||
| ``` | ||
| The deletion of an enum type is not possible, when any other object still depends on it. Trying to do so regardless results in an error. | ||
|
|
||
| ## Alter Enum types | ||
|
|
||
| ### Add new label | ||
| A new label can be added to an existing enum via | ||
| ```sql | ||
| alter type enum_name add value [if not exists] added_enum_label; | ||
| ``` | ||
| The newly inserted label is the new maximum in this enum type. Inserting a new label at another location is currently not supported. | ||
| If the label is already present, the insertion fails with an error. Specifying "if not exists" suppresses this error. | ||
|
|
||
| ### Change ownership | ||
| The owner of an enum can be changed via | ||
| ```sql | ||
| alter type enum_name owner to new_owner; | ||
| ``` | ||
|
Comment on lines
+92
to
+96
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am unsure if we should maybe add some info about necessary permissions. Or should we only add this once the permissions are done? @ChrisWint
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In a previous draft of this page I explained the permissions as such: This change can only be performed by the current owner or a superuser. The user requires permissions to SET ROLE to the new owner aswell as USAGE on the schema of the enum. However, I removed this before opening the PR as I do not describe the required permissions for the other statements either. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,9 @@ The complete list of supported SQL statements in CedarDB is as follows: | |
| Alter table | ||
| : modify a table definition | ||
|
|
||
| [Alter type](/docs/references/datatypes/enums/#alter-enum-types) | ||
| : modify a user-defined type | ||
|
|
||
| [Alter table rename column](altertable) | ||
| : rename a column of a given table | ||
|
|
||
|
|
@@ -66,6 +69,9 @@ Create sequence | |
| [Create table as](createtableas) | ||
| : create and populate a new table with contents from a query | ||
|
|
||
| [Create type](/docs/references/datatypes/enums/#creation-of-enum-types) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The create type statement is not only used for enums but we can also change this once we have implemented more of it.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I was thinking the same thing. Currently it makes sense, to simply refer to the enum page for this, but as more types become available, this would be highly confusing. I guess that's also why the postgres docu has its own entry for all the statements (for example create type), while the entry for Enums does not really touch on all of the statements acting on enums. |
||
| : define a new datatype | ||
|
|
||
| [Create user](createrole) | ||
| : create a new database role | ||
|
|
||
|
|
@@ -84,6 +90,9 @@ Create sequence | |
| Drop | ||
| : remove schema definitions | ||
|
|
||
| [Drop Type](/docs/references/datatypes/enums/#deletion-of-enum-types) | ||
| : remove a user-defined type | ||
|
|
||
| [End](/docs/references/sqlreference/transaction) | ||
| : commit the transaction | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.