diff --git a/contrib/babelfishpg_tsql/sql/sys_functions.sql b/contrib/babelfishpg_tsql/sql/sys_functions.sql index 1cd7ea7333a..0b8cdb45435 100644 --- a/contrib/babelfishpg_tsql/sql/sys_functions.sql +++ b/contrib/babelfishpg_tsql/sql/sys_functions.sql @@ -4014,93 +4014,61 @@ GRANT EXECUTE ON FUNCTION sys.fn_listextendedproperty TO PUBLIC; -- Matches and returns column length of the corresponding column of the given table CREATE OR REPLACE FUNCTION sys.COL_LENGTH(IN object_name TEXT, IN column_name TEXT) RETURNS SMALLINT AS $BODY$ - DECLARE - col_name TEXT; - object_id oid; - column_id INT; - column_length INT; - column_data_type TEXT; - column_precision INT; - BEGIN - -- Get the object ID for the provided object_name - object_id = sys.OBJECT_ID(object_name); - IF object_id IS NULL THEN - RETURN NULL; - END IF; +DECLARE + col_name TEXT; + object_id oid; + column_id INT; + column_length SMALLINT; + column_data_type TEXT; + typeid oid; + typelen INT; + typemod INT; +BEGIN + -- Get the object ID for the provided object_name + object_id := sys.OBJECT_ID(object_name, 'U'); + IF object_id IS NULL THEN + RETURN NULL; + END IF; - -- Truncate and normalize the column name - col_name = sys.babelfish_truncate_identifier(sys.babelfish_remove_delimiter_pair(lower(column_name))); + -- Truncate and normalize the column name + col_name := sys.babelfish_truncate_identifier(sys.babelfish_remove_delimiter_pair(lower(column_name))); - -- Get the column ID for the provided column_name - SELECT attnum INTO column_id FROM pg_attribute - WHERE attrelid = object_id AND lower(attname) = col_name - COLLATE sys.database_default; + -- Get the column ID, typeid, length, and typmod for the provided column_name + SELECT attnum, a.atttypid, a.attlen, a.atttypmod + INTO column_id, typeid, typelen, typemod + FROM pg_attribute a + WHERE attrelid = object_id AND lower(attname) = col_name COLLATE sys.database_default; - IF column_id IS NULL THEN - RETURN NULL; - END IF; + IF column_id IS NULL THEN + RETURN NULL; + END IF; + + -- Get the correct data type + column_data_type := sys.translate_pg_type_to_tsql(typeid); + + IF column_data_type = 'sysname' THEN + column_length := 256; + ELSIF column_data_type IS NULL THEN + + -- Check if it's a user-defined data type + SELECT sys.translate_pg_type_to_tsql(typbasetype), typlen, typtypmod + INTO column_data_type, typelen, typemod + FROM pg_type + WHERE oid = typeid; - -- Retrieve the data type, precision, scale, and column length in characters - SELECT a.atttypid::regtype, - CASE - WHEN a.atttypmod > 0 THEN ((a.atttypmod - 4) >> 16) & 65535 - ELSE NULL - END, - CASE - WHEN a.atttypmod > 0 THEN ((a.atttypmod - 4) & 65535) - ELSE a.atttypmod - END - INTO column_data_type, column_precision, column_length - FROM pg_attribute a - WHERE a.attrelid = object_id AND a.attnum = column_id; - - -- Remove delimiters - column_data_type := sys.babelfish_remove_delimiter_pair(column_data_type); - - IF column_data_type IS NOT NULL THEN - column_length := CASE - -- Columns declared with max specifier case - WHEN column_length = -1 AND column_data_type IN ('varchar', 'nvarchar', 'varbinary') - THEN -1 - WHEN column_data_type = 'xml' - THEN -1 - WHEN column_data_type IN ('tinyint', 'bit') - THEN 1 - WHEN column_data_type = 'smallint' - THEN 2 - WHEN column_data_type = 'date' - THEN 3 - WHEN column_data_type IN ('int', 'integer', 'real', 'smalldatetime', 'smallmoney') - THEN 4 - WHEN column_data_type IN ('time', 'time without time zone') - THEN 5 - WHEN column_data_type IN ('double precision', 'bigint', 'datetime', 'datetime2', 'money') - THEN 8 - WHEN column_data_type = 'datetimeoffset' - THEN 10 - WHEN column_data_type IN ('uniqueidentifier', 'text', 'image', 'ntext') - THEN 16 - WHEN column_data_type = 'sysname' - THEN 256 - WHEN column_data_type = 'sql_variant' - THEN 8016 - WHEN column_data_type IN ('bpchar', 'char', 'varchar', 'binary', 'varbinary') - THEN column_length - WHEN column_data_type IN ('nchar', 'nvarchar') - THEN column_length * 2 - WHEN column_data_type IN ('numeric', 'decimal') - THEN - CASE - WHEN column_precision IS NULL - THEN NULL - ELSE ((column_precision + 8) / 9 * 4 + 1) - END - ELSE NULL - END; + IF column_data_type = 'sysname' THEN + column_length := 256; + ELSE + -- Calculate column length based on base type information + column_length := sys.tsql_type_max_length_helper(column_data_type, typelen, typemod); END IF; + ELSE + -- Calculate column length based on base type information + column_length := sys.tsql_type_max_length_helper(column_data_type, typelen, typemod); + END IF; - RETURN column_length::SMALLINT; - END; + RETURN column_length; +END; $BODY$ LANGUAGE plpgsql IMMUTABLE diff --git a/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--3.3.0--3.4.0.sql b/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--3.3.0--3.4.0.sql index e4beff00cc5..2965f71f921 100644 --- a/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--3.3.0--3.4.0.sql +++ b/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--3.3.0--3.4.0.sql @@ -437,93 +437,61 @@ LANGUAGE plpgsql; -- Matches and returns column length of the corresponding column of the given table CREATE OR REPLACE FUNCTION sys.COL_LENGTH(IN object_name TEXT, IN column_name TEXT) RETURNS SMALLINT AS $BODY$ - DECLARE - col_name TEXT; - object_id oid; - column_id INT; - column_length INT; - column_data_type TEXT; - column_precision INT; - BEGIN - -- Get the object ID for the provided object_name - object_id = sys.OBJECT_ID(object_name); - IF object_id IS NULL THEN - RETURN NULL; - END IF; +DECLARE + col_name TEXT; + object_id oid; + column_id INT; + column_length SMALLINT; + column_data_type TEXT; + typeid oid; + typelen INT; + typemod INT; +BEGIN + -- Get the object ID for the provided object_name + object_id := sys.OBJECT_ID(object_name, 'U'); + IF object_id IS NULL THEN + RETURN NULL; + END IF; - -- Truncate and normalize the column name - col_name = sys.babelfish_truncate_identifier(sys.babelfish_remove_delimiter_pair(lower(column_name))); + -- Truncate and normalize the column name + col_name := sys.babelfish_truncate_identifier(sys.babelfish_remove_delimiter_pair(lower(column_name))); - -- Get the column ID for the provided column_name - SELECT attnum INTO column_id FROM pg_attribute - WHERE attrelid = object_id AND lower(attname) = col_name - COLLATE sys.database_default; + -- Get the column ID, typeid, length, and typmod for the provided column_name + SELECT attnum, a.atttypid, a.attlen, a.atttypmod + INTO column_id, typeid, typelen, typemod + FROM pg_attribute a + WHERE attrelid = object_id AND lower(attname) = col_name COLLATE sys.database_default; - IF column_id IS NULL THEN - RETURN NULL; - END IF; + IF column_id IS NULL THEN + RETURN NULL; + END IF; + + -- Get the correct data type + column_data_type := sys.translate_pg_type_to_tsql(typeid); + + IF column_data_type = 'sysname' THEN + column_length := 256; + ELSIF column_data_type IS NULL THEN + + -- Check if it's a user-defined data type + SELECT sys.translate_pg_type_to_tsql(typbasetype), typlen, typtypmod + INTO column_data_type, typelen, typemod + FROM pg_type + WHERE oid = typeid; - -- Retrieve the data type, precision, scale, and column length in characters - SELECT a.atttypid::regtype, - CASE - WHEN a.atttypmod > 0 THEN ((a.atttypmod - 4) >> 16) & 65535 - ELSE NULL - END, - CASE - WHEN a.atttypmod > 0 THEN ((a.atttypmod - 4) & 65535) - ELSE a.atttypmod - END - INTO column_data_type, column_precision, column_length - FROM pg_attribute a - WHERE a.attrelid = object_id AND a.attnum = column_id; - - -- Remove delimiters - column_data_type := sys.babelfish_remove_delimiter_pair(column_data_type); - - IF column_data_type IS NOT NULL THEN - column_length := CASE - -- Columns declared with max specifier case - WHEN column_length = -1 AND column_data_type IN ('varchar', 'nvarchar', 'varbinary') - THEN -1 - WHEN column_data_type = 'xml' - THEN -1 - WHEN column_data_type IN ('tinyint', 'bit') - THEN 1 - WHEN column_data_type = 'smallint' - THEN 2 - WHEN column_data_type = 'date' - THEN 3 - WHEN column_data_type IN ('int', 'integer', 'real', 'smalldatetime', 'smallmoney') - THEN 4 - WHEN column_data_type IN ('time', 'time without time zone') - THEN 5 - WHEN column_data_type IN ('double precision', 'bigint', 'datetime', 'datetime2', 'money') - THEN 8 - WHEN column_data_type = 'datetimeoffset' - THEN 10 - WHEN column_data_type IN ('uniqueidentifier', 'text', 'image', 'ntext') - THEN 16 - WHEN column_data_type = 'sysname' - THEN 256 - WHEN column_data_type = 'sql_variant' - THEN 8016 - WHEN column_data_type IN ('bpchar', 'char', 'varchar', 'binary', 'varbinary') - THEN column_length - WHEN column_data_type IN ('nchar', 'nvarchar') - THEN column_length * 2 - WHEN column_data_type IN ('numeric', 'decimal') - THEN - CASE - WHEN column_precision IS NULL - THEN NULL - ELSE ((column_precision + 8) / 9 * 4 + 1) - END - ELSE NULL - END; + IF column_data_type = 'sysname' THEN + column_length := 256; + ELSE + -- Calculate column length based on base type information + column_length := sys.tsql_type_max_length_helper(column_data_type, typelen, typemod); END IF; + ELSE + -- Calculate column length based on base type information + column_length := sys.tsql_type_max_length_helper(column_data_type, typelen, typemod); + END IF; - RETURN column_length::SMALLINT; - END; + RETURN column_length; +END; $BODY$ LANGUAGE plpgsql IMMUTABLE diff --git a/test/JDBC/expected/col_length-vu-cleanup.out b/test/JDBC/expected/col_length-vu-cleanup.out index 97b139d4646..d6a6590e215 100644 --- a/test/JDBC/expected/col_length-vu-cleanup.out +++ b/test/JDBC/expected/col_length-vu-cleanup.out @@ -60,3 +60,38 @@ DROP FUNCTION IF EXISTS col_length_prepare_f14(); DROP FUNCTION IF EXISTS col_length_prepare_f15(); DROP FUNCTION IF EXISTS col_length_prepare_f16(); GO + +-- Drop types +DROP TYPE IF EXISTS custom_char_10; +DROP TYPE IF EXISTS custom_varchar_20; +DROP TYPE IF EXISTS custom_binary_5; +DROP TYPE IF EXISTS custom_varbinary_15; +DROP TYPE IF EXISTS custom_nchar_8; +DROP TYPE IF EXISTS custom_nvarchar_16; +DROP TYPE IF EXISTS custom_text; +DROP TYPE IF EXISTS custom_image; +DROP TYPE IF EXISTS custom_ntext; +DROP TYPE IF EXISTS custom_sysname; +DROP TYPE IF EXISTS custom_sql_variant; +DROP TYPE IF EXISTS custom_xml; +DROP TYPE IF EXISTS custom_varcharmax; +DROP TYPE IF EXISTS custom_nvarcharmax; +DROP TYPE IF EXISTS custom_varbinarymax; +DROP TYPE IF EXISTS custom_bit; +DROP TYPE IF EXISTS custom_tinyint; +DROP TYPE IF EXISTS custom_bigint; +DROP TYPE IF EXISTS custom_smallint; +DROP TYPE IF EXISTS custom_smallmoney; +DROP TYPE IF EXISTS custom_money; +DROP TYPE IF EXISTS custom_smalldatetime; +DROP TYPE IF EXISTS custom_real; +DROP TYPE IF EXISTS custom_float; +DROP TYPE IF EXISTS custom_time; +DROP TYPE IF EXISTS custom_datetime; +DROP TYPE IF EXISTS custom_datetime2; +DROP TYPE IF EXISTS custom_datetimeoffset; +DROP TYPE IF EXISTS custom_uniqueidentifier; +DROP TYPE IF EXISTS custom_date; +DROP TYPE IF EXISTS custom_decimal_10_5; +DROP TYPE IF EXISTS custom_numeric_3_0; +GO diff --git a/test/JDBC/expected/col_length-vu-prepare.out b/test/JDBC/expected/col_length-vu-prepare.out index d158bba536b..e2dcc424084 100644 --- a/test/JDBC/expected/col_length-vu-prepare.out +++ b/test/JDBC/expected/col_length-vu-prepare.out @@ -51,11 +51,140 @@ CREATE TABLE sys_col_length_test_schema.test_table( ); GO -INSERT INTO sys_col_length_test_schema.test_table (col_char, col_varchar, col_varbinary) -VALUES ('ABCDEF', 'Hello, World!', 0x0123456789ABCDEF) +-- for user defined data types +-- Create User-Defined Types +CREATE TYPE custom_char_10 FROM CHAR(10); GO -~~ROW COUNT: 1~~ +CREATE TYPE custom_varchar_20 FROM VARCHAR(20); +GO + +CREATE TYPE custom_binary_5 FROM BINARY(5); +GO + +CREATE TYPE custom_varbinary_15 FROM VARBINARY(15); +GO + +CREATE TYPE custom_nchar_8 FROM NCHAR(8); +GO + +CREATE TYPE custom_nvarchar_16 FROM NVARCHAR(16); +GO + +CREATE TYPE custom_text FROM TEXT; +GO + +CREATE TYPE custom_image FROM IMAGE; +GO + +CREATE TYPE custom_ntext FROM NTEXT; +GO + +CREATE TYPE custom_sysname FROM sysname; +GO + +CREATE TYPE custom_sql_variant FROM SQL_VARIANT; +GO + +CREATE TYPE custom_xml FROM XML; +GO + +CREATE TYPE custom_varcharmax FROM VARCHAR(MAX); +GO + +CREATE TYPE custom_nvarcharmax FROM NVARCHAR(MAX); +GO + +CREATE TYPE custom_varbinarymax FROM VARBINARY(MAX); +GO + +CREATE TYPE custom_bit FROM BIT; +GO + +CREATE TYPE custom_tinyint FROM TINYINT; +GO + +CREATE TYPE custom_bigint FROM BIGINT; +GO + +CREATE TYPE custom_smallint FROM SMALLINT; +GO + +CREATE TYPE custom_smallmoney FROM SMALLMONEY; +GO + +CREATE TYPE custom_money FROM MONEY; +GO + +CREATE TYPE custom_smalldatetime FROM SMALLDATETIME; +GO + +CREATE TYPE custom_real FROM REAL; +GO + +CREATE TYPE custom_float FROM FLOAT; +GO + +CREATE TYPE custom_time FROM TIME; +GO + +CREATE TYPE custom_datetime FROM DATETIME; +GO + +CREATE TYPE custom_datetime2 FROM DATETIME2; +GO + +CREATE TYPE custom_datetimeoffset FROM DATETIMEOFFSET; +GO + +CREATE TYPE custom_uniqueidentifier FROM UNIQUEIDENTIFIER; +GO + +CREATE TYPE custom_date FROM DATE; +GO + +CREATE TYPE custom_decimal_10_5 FROM DECIMAL(10,5); +GO + +CREATE TYPE custom_numeric_3_0 FROM NUMERIC(3,0); +GO + +-- Create Table with User-Defined Data Types +CREATE TABLE udd_test_table ( + col_customchar custom_char_10, + col_customvarchar custom_varchar_20, + col_custombinary custom_binary_5, + col_customvarbinary custom_varbinary_15, + col_customnchar custom_nchar_8, + col_customnvarchar custom_nvarchar_16, + col_customtext custom_text, + col_customimage custom_image, + col_customntext custom_ntext, + col_customsysname custom_sysname, + col_customsqlvariant custom_sql_variant, + col_customxml custom_xml, + col_customvarcharmax custom_varcharmax, + col_customnvarcharmax custom_nvarcharmax, + col_customvarbinarymax custom_varbinarymax, + col_custombit custom_bit, + col_customtinyint custom_tinyint, + col_custombigint custom_bigint, + col_customsmallint custom_smallint, + col_customsmallmoney custom_smallmoney, + col_custommoney custom_money, + col_customsmalldatetime custom_smalldatetime, + col_customreal custom_real, + col_customfloat custom_float, + col_customtime custom_time, + col_customdatetime custom_datetime, + col_customdatetime2 custom_datetime2, + col_customdatetimeoffset custom_datetimeoffset, + col_customuniqueidentifier custom_uniqueidentifier, + col_customdate custom_date, + col_customdecimal custom_decimal_10_5, + col_customnumeric custom_numeric_3_0 +); +GO CREATE VIEW col_length_prepare_v1 AS (SELECT COL_LENGTH('sys_column_length_test_table', 'ID')); GO @@ -120,84 +249,84 @@ CREATE PROCEDURE col_length_prepare_p10 AS (SELECT COL_LENGTH('sys_column_length GO CREATE FUNCTION col_length_prepare_f1() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH('sys_column_length_test_table', 'col_xml')); END GO CREATE FUNCTION col_length_prepare_f2() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH('sys_column_length_test_table', 'col_varcharmax')); END GO CREATE FUNCTION col_length_prepare_f3() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH('sys_column_length_test_table', 'col_nvarcharmax')); END GO CREATE FUNCTION col_length_prepare_f4() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH('sys_column_length_test_table', 'col_varbinarymax')); END GO CREATE FUNCTION col_length_prepare_f5() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH('sys_column_length_test_table', 'col_bit')); END GO CREATE FUNCTION col_length_prepare_f6() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH('sys_column_length_test_table', 'col_tinyint')); END GO CREATE FUNCTION col_length_prepare_f7() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH('sys_column_length_test_table', 'col_bigint')); END GO CREATE FUNCTION col_length_prepare_f8() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH('sys_column_length_test_table', 'col_smallint')); END GO CREATE FUNCTION col_length_prepare_f9() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH('sys_column_length_test_table', 'col_smallmoney')); END GO CREATE FUNCTION col_length_prepare_f10() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH('sys_column_length_test_table', 'col_money')); END GO CREATE FUNCTION col_length_prepare_f11() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH('sys_column_length_test_table', 'col_smalldatetime')); END GO CREATE FUNCTION col_length_prepare_f12() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH('sys_column_length_test_table', 'col_real')); END @@ -205,7 +334,7 @@ GO -- Invalid column, should return NULL CREATE FUNCTION col_length_prepare_f13() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH('sys_column_length_test_table', 1)); END @@ -213,7 +342,7 @@ GO -- Invalid column, should return NULL CREATE FUNCTION col_length_prepare_f14() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH('sys_column_length_test_table', -1)); END @@ -221,7 +350,7 @@ GO -- Invalid table, should return NULL CREATE FUNCTION col_length_prepare_f15() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH(NULL, 'col_char')); END @@ -229,7 +358,7 @@ GO -- NULL column, should return NULL CREATE FUNCTION col_length_prepare_f16() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH('sys_column_length_test_table', NULL)); END diff --git a/test/JDBC/expected/col_length-vu-verify.out b/test/JDBC/expected/col_length-vu-verify.out index 9ccf584b7f4..a5cae67a16b 100644 --- a/test/JDBC/expected/col_length-vu-verify.out +++ b/test/JDBC/expected/col_length-vu-verify.out @@ -164,7 +164,7 @@ smallint SELECT col_length_prepare_f1() GO ~~START~~ -int +smallint -1 ~~END~~ @@ -172,7 +172,7 @@ int SELECT col_length_prepare_f2() GO ~~START~~ -int +smallint -1 ~~END~~ @@ -180,7 +180,7 @@ int SELECT col_length_prepare_f3() GO ~~START~~ -int +smallint -1 ~~END~~ @@ -188,7 +188,7 @@ int SELECT col_length_prepare_f4() GO ~~START~~ -int +smallint -1 ~~END~~ @@ -196,7 +196,7 @@ int SELECT col_length_prepare_f5() GO ~~START~~ -int +smallint 1 ~~END~~ @@ -204,7 +204,7 @@ int SELECT col_length_prepare_f6() GO ~~START~~ -int +smallint 1 ~~END~~ @@ -212,7 +212,7 @@ int SELECT col_length_prepare_f7() GO ~~START~~ -int +smallint 8 ~~END~~ @@ -220,7 +220,7 @@ int SELECT col_length_prepare_f8() GO ~~START~~ -int +smallint 2 ~~END~~ @@ -228,7 +228,7 @@ int SELECT col_length_prepare_f9() GO ~~START~~ -int +smallint 4 ~~END~~ @@ -236,7 +236,7 @@ int SELECT col_length_prepare_f10() GO ~~START~~ -int +smallint 8 ~~END~~ @@ -244,7 +244,7 @@ int SELECT col_length_prepare_f11() GO ~~START~~ -int +smallint 4 ~~END~~ @@ -252,7 +252,7 @@ int SELECT col_length_prepare_f12() GO ~~START~~ -int +smallint 4 ~~END~~ @@ -260,7 +260,7 @@ int SELECT col_length_prepare_f13() GO ~~START~~ -int +smallint ~~END~~ @@ -268,7 +268,7 @@ int SELECT col_length_prepare_f14() GO ~~START~~ -int +smallint ~~END~~ @@ -276,7 +276,7 @@ int SELECT col_length_prepare_f15() GO ~~START~~ -int +smallint ~~END~~ @@ -284,7 +284,7 @@ int SELECT col_length_prepare_f16() GO ~~START~~ -int +smallint ~~END~~ @@ -453,3 +453,259 @@ SQL Variant Column ~~END~~ +-- Test Cases for User-Defined Data Types +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customchar'); +GO +~~START~~ +smallint +10 +~~END~~ + + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customvarchar'); +GO +~~START~~ +smallint +20 +~~END~~ + + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_custombinary'); +GO +~~START~~ +smallint +5 +~~END~~ + + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customvarbinary'); +GO +~~START~~ +smallint +15 +~~END~~ + + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customnchar'); +GO +~~START~~ +smallint +16 +~~END~~ + + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customnvarchar'); +GO +~~START~~ +smallint +32 +~~END~~ + + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customtext'); +GO +~~START~~ +smallint +16 +~~END~~ + + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customimage'); +GO +~~START~~ +smallint +16 +~~END~~ + + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customntext'); +GO +~~START~~ +smallint +16 +~~END~~ + + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customsysname'); +GO +~~START~~ +smallint +256 +~~END~~ + + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customsqlvariant'); +GO +~~START~~ +smallint +8016 +~~END~~ + + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customxml'); +GO +~~START~~ +smallint +-1 +~~END~~ + + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customvarcharmax'); +GO +~~START~~ +smallint +-1 +~~END~~ + + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customnvarcharmax'); +GO +~~START~~ +smallint +-1 +~~END~~ + + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customvarbinarymax'); +GO +~~START~~ +smallint +-1 +~~END~~ + + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_custombit'); +GO +~~START~~ +smallint +1 +~~END~~ + + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customtinyint'); +GO +~~START~~ +smallint +1 +~~END~~ + + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_custombigint'); +GO +~~START~~ +smallint +8 +~~END~~ + + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customsmallint'); +GO +~~START~~ +smallint +2 +~~END~~ + + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customsmallmoney'); +GO +~~START~~ +smallint +4 +~~END~~ + + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_custommoney'); +GO +~~START~~ +smallint +8 +~~END~~ + + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customsmalldatetime'); +GO +~~START~~ +smallint +4 +~~END~~ + + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customreal'); +GO +~~START~~ +smallint +4 +~~END~~ + + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customfloat'); +GO +~~START~~ +smallint +8 +~~END~~ + + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customtime'); +GO +~~START~~ +smallint +5 +~~END~~ + + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customdatetime'); +GO +~~START~~ +smallint +8 +~~END~~ + + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customdatetime2'); +GO +~~START~~ +smallint +8 +~~END~~ + + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customdatetimeoffset'); +GO +~~START~~ +smallint +10 +~~END~~ + + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customuniqueidentifier'); +GO +~~START~~ +smallint +16 +~~END~~ + + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customdate'); +GO +~~START~~ +smallint +3 +~~END~~ + + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customdecimal'); +GO +~~START~~ +smallint +9 +~~END~~ + + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customnumeric'); +GO +~~START~~ +smallint +5 +~~END~~ + diff --git a/test/JDBC/input/col_length-vu-cleanup.sql b/test/JDBC/input/col_length-vu-cleanup.sql index 97b139d4646..d6a6590e215 100644 --- a/test/JDBC/input/col_length-vu-cleanup.sql +++ b/test/JDBC/input/col_length-vu-cleanup.sql @@ -60,3 +60,38 @@ DROP FUNCTION IF EXISTS col_length_prepare_f14(); DROP FUNCTION IF EXISTS col_length_prepare_f15(); DROP FUNCTION IF EXISTS col_length_prepare_f16(); GO + +-- Drop types +DROP TYPE IF EXISTS custom_char_10; +DROP TYPE IF EXISTS custom_varchar_20; +DROP TYPE IF EXISTS custom_binary_5; +DROP TYPE IF EXISTS custom_varbinary_15; +DROP TYPE IF EXISTS custom_nchar_8; +DROP TYPE IF EXISTS custom_nvarchar_16; +DROP TYPE IF EXISTS custom_text; +DROP TYPE IF EXISTS custom_image; +DROP TYPE IF EXISTS custom_ntext; +DROP TYPE IF EXISTS custom_sysname; +DROP TYPE IF EXISTS custom_sql_variant; +DROP TYPE IF EXISTS custom_xml; +DROP TYPE IF EXISTS custom_varcharmax; +DROP TYPE IF EXISTS custom_nvarcharmax; +DROP TYPE IF EXISTS custom_varbinarymax; +DROP TYPE IF EXISTS custom_bit; +DROP TYPE IF EXISTS custom_tinyint; +DROP TYPE IF EXISTS custom_bigint; +DROP TYPE IF EXISTS custom_smallint; +DROP TYPE IF EXISTS custom_smallmoney; +DROP TYPE IF EXISTS custom_money; +DROP TYPE IF EXISTS custom_smalldatetime; +DROP TYPE IF EXISTS custom_real; +DROP TYPE IF EXISTS custom_float; +DROP TYPE IF EXISTS custom_time; +DROP TYPE IF EXISTS custom_datetime; +DROP TYPE IF EXISTS custom_datetime2; +DROP TYPE IF EXISTS custom_datetimeoffset; +DROP TYPE IF EXISTS custom_uniqueidentifier; +DROP TYPE IF EXISTS custom_date; +DROP TYPE IF EXISTS custom_decimal_10_5; +DROP TYPE IF EXISTS custom_numeric_3_0; +GO diff --git a/test/JDBC/input/col_length-vu-prepare.sql b/test/JDBC/input/col_length-vu-prepare.sql index a602c7a69c0..1f777a404fe 100644 --- a/test/JDBC/input/col_length-vu-prepare.sql +++ b/test/JDBC/input/col_length-vu-prepare.sql @@ -51,8 +51,139 @@ CREATE TABLE sys_col_length_test_schema.test_table( ); GO -INSERT INTO sys_col_length_test_schema.test_table (col_char, col_varchar, col_varbinary) -VALUES ('ABCDEF', 'Hello, World!', 0x0123456789ABCDEF) +-- for user defined data types +-- Create User-Defined Types +CREATE TYPE custom_char_10 FROM CHAR(10); +GO + +CREATE TYPE custom_varchar_20 FROM VARCHAR(20); +GO + +CREATE TYPE custom_binary_5 FROM BINARY(5); +GO + +CREATE TYPE custom_varbinary_15 FROM VARBINARY(15); +GO + +CREATE TYPE custom_nchar_8 FROM NCHAR(8); +GO + +CREATE TYPE custom_nvarchar_16 FROM NVARCHAR(16); +GO + +CREATE TYPE custom_text FROM TEXT; +GO + +CREATE TYPE custom_image FROM IMAGE; +GO + +CREATE TYPE custom_ntext FROM NTEXT; +GO + +CREATE TYPE custom_sysname FROM sysname; +GO + +CREATE TYPE custom_sql_variant FROM SQL_VARIANT; +GO + +CREATE TYPE custom_xml FROM XML; +GO + +CREATE TYPE custom_varcharmax FROM VARCHAR(MAX); +GO + +CREATE TYPE custom_nvarcharmax FROM NVARCHAR(MAX); +GO + +CREATE TYPE custom_varbinarymax FROM VARBINARY(MAX); +GO + +CREATE TYPE custom_bit FROM BIT; +GO + +CREATE TYPE custom_tinyint FROM TINYINT; +GO + +CREATE TYPE custom_bigint FROM BIGINT; +GO + +CREATE TYPE custom_smallint FROM SMALLINT; +GO + +CREATE TYPE custom_smallmoney FROM SMALLMONEY; +GO + +CREATE TYPE custom_money FROM MONEY; +GO + +CREATE TYPE custom_smalldatetime FROM SMALLDATETIME; +GO + +CREATE TYPE custom_real FROM REAL; +GO + +CREATE TYPE custom_float FROM FLOAT; +GO + +CREATE TYPE custom_time FROM TIME; +GO + +CREATE TYPE custom_datetime FROM DATETIME; +GO + +CREATE TYPE custom_datetime2 FROM DATETIME2; +GO + +CREATE TYPE custom_datetimeoffset FROM DATETIMEOFFSET; +GO + +CREATE TYPE custom_uniqueidentifier FROM UNIQUEIDENTIFIER; +GO + +CREATE TYPE custom_date FROM DATE; +GO + +CREATE TYPE custom_decimal_10_5 FROM DECIMAL(10,5); +GO + +CREATE TYPE custom_numeric_3_0 FROM NUMERIC(3,0); +GO + +-- Create Table with User-Defined Data Types +CREATE TABLE udd_test_table ( + col_customchar custom_char_10, + col_customvarchar custom_varchar_20, + col_custombinary custom_binary_5, + col_customvarbinary custom_varbinary_15, + col_customnchar custom_nchar_8, + col_customnvarchar custom_nvarchar_16, + col_customtext custom_text, + col_customimage custom_image, + col_customntext custom_ntext, + col_customsysname custom_sysname, + col_customsqlvariant custom_sql_variant, + col_customxml custom_xml, + col_customvarcharmax custom_varcharmax, + col_customnvarcharmax custom_nvarcharmax, + col_customvarbinarymax custom_varbinarymax, + col_custombit custom_bit, + col_customtinyint custom_tinyint, + col_custombigint custom_bigint, + col_customsmallint custom_smallint, + col_customsmallmoney custom_smallmoney, + col_custommoney custom_money, + col_customsmalldatetime custom_smalldatetime, + col_customreal custom_real, + col_customfloat custom_float, + col_customtime custom_time, + col_customdatetime custom_datetime, + col_customdatetime2 custom_datetime2, + col_customdatetimeoffset custom_datetimeoffset, + col_customuniqueidentifier custom_uniqueidentifier, + col_customdate custom_date, + col_customdecimal custom_decimal_10_5, + col_customnumeric custom_numeric_3_0 +); GO CREATE VIEW col_length_prepare_v1 AS (SELECT COL_LENGTH('sys_column_length_test_table', 'ID')); @@ -118,84 +249,84 @@ CREATE PROCEDURE col_length_prepare_p10 AS (SELECT COL_LENGTH('sys_column_length GO CREATE FUNCTION col_length_prepare_f1() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH('sys_column_length_test_table', 'col_xml')); END GO CREATE FUNCTION col_length_prepare_f2() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH('sys_column_length_test_table', 'col_varcharmax')); END GO CREATE FUNCTION col_length_prepare_f3() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH('sys_column_length_test_table', 'col_nvarcharmax')); END GO CREATE FUNCTION col_length_prepare_f4() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH('sys_column_length_test_table', 'col_varbinarymax')); END GO CREATE FUNCTION col_length_prepare_f5() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH('sys_column_length_test_table', 'col_bit')); END GO CREATE FUNCTION col_length_prepare_f6() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH('sys_column_length_test_table', 'col_tinyint')); END GO CREATE FUNCTION col_length_prepare_f7() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH('sys_column_length_test_table', 'col_bigint')); END GO CREATE FUNCTION col_length_prepare_f8() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH('sys_column_length_test_table', 'col_smallint')); END GO CREATE FUNCTION col_length_prepare_f9() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH('sys_column_length_test_table', 'col_smallmoney')); END GO CREATE FUNCTION col_length_prepare_f10() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH('sys_column_length_test_table', 'col_money')); END GO CREATE FUNCTION col_length_prepare_f11() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH('sys_column_length_test_table', 'col_smalldatetime')); END GO CREATE FUNCTION col_length_prepare_f12() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH('sys_column_length_test_table', 'col_real')); END @@ -203,7 +334,7 @@ GO -- Invalid column, should return NULL CREATE FUNCTION col_length_prepare_f13() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH('sys_column_length_test_table', 1)); END @@ -211,7 +342,7 @@ GO -- Invalid column, should return NULL CREATE FUNCTION col_length_prepare_f14() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH('sys_column_length_test_table', -1)); END @@ -219,7 +350,7 @@ GO -- Invalid table, should return NULL CREATE FUNCTION col_length_prepare_f15() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH(NULL, 'col_char')); END @@ -227,7 +358,7 @@ GO -- NULL column, should return NULL CREATE FUNCTION col_length_prepare_f16() -RETURNS INT AS +RETURNS SMALLINT AS BEGIN RETURN (SELECT COL_LENGTH('sys_column_length_test_table', NULL)); END diff --git a/test/JDBC/input/col_length-vu-verify.sql b/test/JDBC/input/col_length-vu-verify.sql index 60a9a77d52c..856eab8448a 100644 --- a/test/JDBC/input/col_length-vu-verify.sql +++ b/test/JDBC/input/col_length-vu-verify.sql @@ -174,3 +174,99 @@ SELECT END AS ColumnStatus; GO +-- Test Cases for User-Defined Data Types +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customchar'); +GO + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customvarchar'); +GO + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_custombinary'); +GO + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customvarbinary'); +GO + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customnchar'); +GO + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customnvarchar'); +GO + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customtext'); +GO + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customimage'); +GO + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customntext'); +GO + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customsysname'); +GO + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customsqlvariant'); +GO + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customxml'); +GO + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customvarcharmax'); +GO + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customnvarcharmax'); +GO + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customvarbinarymax'); +GO + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_custombit'); +GO + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customtinyint'); +GO + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_custombigint'); +GO + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customsmallint'); +GO + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customsmallmoney'); +GO + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_custommoney'); +GO + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customsmalldatetime'); +GO + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customreal'); +GO + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customfloat'); +GO + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customtime'); +GO + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customdatetime'); +GO + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customdatetime2'); +GO + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customdatetimeoffset'); +GO + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customuniqueidentifier'); +GO + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customdate'); +GO + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customdecimal'); +GO + +SELECT * FROM sys.COL_LENGTH('udd_test_table', 'col_customnumeric'); +GO