CREATE_ ARRAY
On this page
Creates a new array of the specified length that is initialized with default values for the array’s inferred data type.
Syntax
CREATE_ARRAY(length_expression)
Arguments
length_
An expression that evaluates to an integer value, which is used to specify the length of the array.
Remarks
The CREATE_
function uses data type inference to determine the appropriate type for each element in the array.
-
In assignment statements, such as assigning the value of an array variable
-
As an input parameter to functions that accept an array type
-
In a
RETURN
statement for a function that returns an array type -
In expressions that use a valid operator for an array type, such as concatenation (
+
) or type casting (:>
)
See the Examples section for examples of these expressions.
When CREATE_
is executed, a new array is initialized and each element is given one of the following default values:
-
If the array’s element data type is specified without a data type modifier, the default element values will be
NULL
for each index.For example, given myarray ARRAY(INT) = CREATE_
, each of the three element values in the array will beARRAY(3); NULL
.Additionally, if the element data type has a NULL
modifier, such asmyarray ARRAY(INT NULL) = CREATE_
, then each of the three element values in the array will beARRAY(3) NULL
. -
If the array’s element data type is specified with a
NOT NULL
modifier, the element values will be assigned the appropriate default value for the data type.For example, given myarray ARRAY(INT NOT NULL) = CREATE_
, each of the three element values will beARRAY(3) 0
, which is the default value for theINT
data type.For a complete list of default values for each data type, see Array Default Values.
Examples
The following examples demonstrate how the type of an array can be inferred depending on the usage of CREATE_
.
Example: Variable Assignment
The following snippet from a function creates a new integer array and assigns it to a variable:
DELIMITER //CREATE FUNCTION example_func() RETURNS INT ASDECLAREa ARRAY(INT) = CREATE_ARRAY(10);BEGIN...END //DELIMITER ;
Example: Input Parameter
The following example demonstrates a function that accepts an array as an input parameter and returns its length.
DELIMITER //CREATE OR REPLACE FUNCTION get_length(a ARRAY(INT)) RETURNS INT ASBEGINRETURN LENGTH(a);END //DELIMITER ;
SELECT get_length(CREATE_ARRAY(10));
+------------------------------+
| get_length(CREATE_ARRAY(10)) |
+------------------------------+
| 10 |
+------------------------------+
1 row in set (0.07 sec)
When this function is called by passing CREATE_
as the input parameter, the type of the new array is inferred from the function definition’s input parameter type.
Example: RETURN Statement
The following example demonstrates two functions: one that simply creates a new array and returns it, and another which returns the size of the array.create_
in a SELECT
statement because returning array type values to the client is not supported.
DELIMITER //CREATE OR REPLACE FUNCTION create_array_of_size(a INT) RETURNS ARRAY(INT) ASBEGINRETURN CREATE_ARRAY(a);END //CREATE OR REPLACE FUNCTION get_length(b ARRAY(INT)) RETURNS INT ASBEGINRETURN LENGTH(b);END //DELIMITER ;
When the create_
function is called, the returned array’s type is inferred from the function definition’s return type, which is an integer array.
SELECT get_length(create_array_of_size(10));
+--------------------------------+
| get_length(get_array_size(10)) |
+--------------------------------+
| 10 |
+--------------------------------+
1 row in set (0.10 sec)
Example: Type Casting
The following example demonstrates creation of an array, where its type is inferred from a typecast.
DELIMITER //CREATE OR REPLACE FUNCTION get_length(a INT) RETURNS INT ASDECLAREb INT;BEGINb = LENGTH(CREATE_ARRAY(a) :> ARRAY(INT));RETURN b;END //DELIMITER ;
SELECT get_length(10);
+----------------+
| get_length(10) |
+----------------+
| 10 |
+----------------+
1 row in set (0.12 sec)
Example: Array Concatenation
The following example demonstrates how to extend the length of an existing array by concatenating it with the result of CREATE_
.
DELIMITER //CREATE OR REPLACE FUNCTION extend_array(a ARRAY(INT NOT NULL)) RETURNS ARRAY(INT NOT NULL) ASDECLAREc ARRAY(INT NOT NULL) = a;BEGINc += CREATE_ARRAY(5);RETURN c;END //CREATE OR REPLACE FUNCTION list_array_elements(a ARRAY(INT NOT NULL)) RETURNS VARCHAR(255) ASDECLAREresult VARCHAR(255) = "";BEGINIF LENGTH(a) = 0 THENRETURN "[]";END IF;result = CONCAT(result, "[", a[0]);FOR i IN 1..LENGTH(a) - 1 LOOPresult = CONCAT(result, ", ", a[i]);END LOOP;result = CONCAT(result, "]");RETURN result;END //CREATE OR REPLACE FUNCTION before_after_concat() RETURNS VARCHAR(255) ASDECLAREprimes ARRAY(INT NOT NULL) = [2, 3, 5, 7, 11];result VARCHAR(255);BEGINresult = CONCAT("Original: ", list_array_elements(primes));primes = extend_array(primes);result = CONCAT(result, "; Updated: ", list_array_elements(primes));RETURN result;END //DELIMITER ;
SELECT before_after_concat();
+----------------------------------------------------------------------+
| before_after_concat() |
+----------------------------------------------------------------------+
| Original: [2, 3, 5, 7, 11]; Updated: [2, 3, 5, 7, 11, 0, 0, 0, 0, 0] |
+----------------------------------------------------------------------+
1 row in set (0.00 sec)
Related Topics
Last modified: May 22, 2023