CREATE TYPE
Defines a new data type.
Synopsis
CREATE TYPE <name> AS ( <attribute_name>
<data_type> [, ... ] )
CREATE TYPE <name> (
INPUT = <input_function>,
OUTPUT = <output_function>
[, RECEIVE = <receive_function>]
[, SEND = <send_function>]
[, INTERNALLENGTH = {<internallength> | VARIABLE}]
[, PASSEDBYVALUE]
[, ALIGNMENT = <alignment>]
[, STORAGE = <storage>]
[, DEFAULT = <default>]
[, ELEMENT = <element>]
[, DELIMITER = <delimiter>] )
CREATE TYPE name
Description
CREATE TYPE
registers a new data type for use in the current database. The user who defines a type becomes its owner.
If a schema name is given then the type is created in the specified schema. Otherwise it is created in the current schema. The type name must be distinct from the name of any existing type or domain in the same schema. The type name must also be distinct from the name of any existing table in the same schema.
Composite Types
The first form of CREATE TYPE
creates a composite type. This is the only form currently supported by HAWQ. The composite type is specified by a list of attribute names and data types. This is essentially the same as the row type of a table, but using CREATE TYPE
avoids the need to create an actual table when all that is wanted is to define a type. A stand-alone composite type is useful as the argument or return type of a function.
Base Types
The second form of CREATE TYPE
creates a new base type (scalar type). The parameters may appear in any order, not only that shown in the syntax, and most are optional. You must register two or more functions (using CREATE FUNCTION
) before defining the type. The support functions and
The converts the type’s external textual representation to the internal representation used by the operators and functions defined for the type.
The optional internal
, or as taking three arguments of types internal
, oid
, integer
. The first argument is a pointer to a StringInfo
buffer holding the received byte string; the optional arguments are the same as for the text input function. The receive function must return a value of the data type itself. Usually, a receive function should be declared STRICT
; if it is not, it will be called with a NULL
first parameter when reading a NULL input value. The function must still return NULL
in this case, unless it raises an error. (This case is mainly meant to support domain receive functions, which may need to reject NULL
inputs.) Similarly, the optional bytea
. Send functions are not invoked for NULL
values.
You should at this point be wondering how the input and output functions can be declared to have results or arguments of the new type, when they have to be created before the new type can be created. The answer is that the type should first be defined as a shell type, which is a placeholder type that has no properties except a name and an owner. This is done by issuing the command CREATE TYPE name
, with no additional parameters. Then the I/O functions can be defined referencing the shell type. Finally, CREATE TYPE
with a full definition replaces the shell entry with a complete, valid type definition, after which the new type can be used normally.
While the details of the new type’s internal representation are only known to the I/O functions and other functions you create to work with the type, there are several properties of the internal representation that must be declared to HAWQ. Foremost of these is VARIABLE
. (Internally, this is represented by setting typlen
to -1
.) The internal representation of all variable-length types must start with a 4-byte integer giving the total length of this value of the type.
The optional flag PASSEDBYVALUE
indicates that values of this data type are passed by value, rather than by reference. You may not pass by value types whose internal representation is larger than the size of the Datum
type (4 bytes on most machines, 8 bytes on a few).
The int4
as their first component.
The plain
is allowed for fixed-length types.) plain
specifies that data of the type will always be stored in-line and not compressed. extended
specifies that the system will first try to compress a long data value, and will move the value out of the main table row if it’s still too long. external
allows the value to be moved out of the main table, but the system will not try to compress it. main
allows compression, but discourages moving the value out of the main table. (Data items with this storage strategy may still be moved out of the main table if there is no other way to make a row fit, but they will be kept in the main table preferentially over extended
and external
items.)
A default value may be specified, in case a user wants columns of the data type to default to something other than the null value. Specify the default with the DEFAULT
key word. (Such a default may be overridden by an explicit DEFAULT
clause attached to a particular column.)
To indicate that a type is an array, specify the type of the array elements using the ELEMENT
key word. For example, to define an array of 4-byte integers (int4), specify ELEMENT = int4
. More details about array types appear below.
To indicate the delimiter to be used between values in the external representation of arrays of this type, delimiter
can be set to a specific character. The default delimiter is the comma (,). Note that the delimiter is associated with the array element type, not the array type itself.
Array Types
Whenever a user-defined base data type is created, HAWQ automatically creates an associated array type, whose name consists of the base type’s name prepended with an underscore. The parser understands this naming convention, and translates requests for columns of type foo[]
into requests for type _foo
. The implicitly-created array type is variable length and uses the built-in input and output functions array_in
and array_out
.
You might reasonably ask why there is an ELEMENT
option, if the system makes the correct array type automatically. The only case where it’s useful to use ELEMENT
is when you are making a fixed-length type that happens to be internally an array of a number of identical things, and you want to allow these things to be accessed directly by subscripting, in addition to whatever operations you plan to provide for the type as a whole. For example, type name
allows its constituent char
elements to be accessed this way. A 2-D point type could allow its two component numbers to be accessed like point[0] and point[1]. Note that this facility only works for fixed-length types whose internal form is exactly a sequence of identical fixed-length fields. A subscriptable variable-length type must have the generalized internal representation used by array_in
and array_out
. For historical reasons, subscripting of fixed-length array types starts from zero, rather than from one as for variable-length arrays.
Parameters
The name (optionally schema-qualified) of a type to be created.
The name of an attribute (column) for the composite type.
The name of an existing data type to become a column of the composite type.
The name of a function that converts data from the type’s external textual form to its internal form.