Skip to content

Commit

Permalink
Catalog meta
Browse files Browse the repository at this point in the history
  • Loading branch information
KushaalShroff committed Jan 18, 2024
1 parent dce7b5e commit 39b554e
Show file tree
Hide file tree
Showing 12 changed files with 311 additions and 6 deletions.
3 changes: 2 additions & 1 deletion contrib/babelfishpg_common/src/typecode.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ type_info_t type_infos[TOTAL_TYPECODE_COUNT] =
{0, 1, "decimal", "decimal", 5, 30, 5},
{0, 1, "sysname", "sysname", 5, 31, 5},
{0, 1, "rowversion", "timestamp", 8, 32, 3},
{0, 1, "timestamp", "timestamp", 8, 33, 3}
{0, 1, "timestamp", "timestamp", 8, 33, 3},
{0, 1, "vector", "vector", 9, 34, 3}
};

/* Hash tables to help backward searching (from OID to Persist ID) */
Expand Down
2 changes: 1 addition & 1 deletion contrib/babelfishpg_common/src/typecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
#define FIXEDDECIMAL_MULTIPLIER 10000LL
#endif

#define TOTAL_TYPECODE_COUNT 33
#define TOTAL_TYPECODE_COUNT 34

struct Node;

Expand Down
2 changes: 1 addition & 1 deletion contrib/babelfishpg_tsql/antlr/TSqlParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -5158,7 +5158,7 @@ column_name_list_with_order

/* We introduce specific index methods so as to avoid PG syntax leaks. */
column_name_list_with_order_for_vector
: simple_column_name (ASC | DESC)? (VECTOR_COSINE_OPS | VECTOR_IP_OPS | VECTOR_L2_OPS)? (COMMA simple_column_name (ASC | DESC)?)*
: simple_column_name (ASC | DESC)? (VECTOR_COSINE_OPS | VECTOR_IP_OPS | VECTOR_L2_OPS)? (COMMA simple_column_name (ASC | DESC)? (VECTOR_COSINE_OPS | VECTOR_IP_OPS | VECTOR_L2_OPS)?)*
;

//For some reason, sql server allows any number of prefixes: Here, h is the column: a.b.c.d.e.f.g.h
Expand Down
1 change: 1 addition & 0 deletions contrib/babelfishpg_tsql/sql/babelfishpg_tsql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,7 @@ BEGIN
WHEN 'sql_variant' THEN tds_id = 98;
WHEN 'datetimeoffset' THEN tds_id = 43;
WHEN 'timestamp' THEN tds_id = 173;
WHEN 'vector' THEN tds_id = 167; -- Same as varchar
ELSE tds_id = 0;
END CASE;
RETURN tds_id;
Expand Down
2 changes: 1 addition & 1 deletion contrib/babelfishpg_tsql/sql/information_schema_tsql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ $$SELECT
THEN 1073741823
WHEN type = 'sysname'
THEN 128
WHEN type = 'xml'
WHEN type IN ('xml', 'vector')
THEN -1
WHEN type = 'sql_variant'
THEN 0
Expand Down
1 change: 1 addition & 0 deletions contrib/babelfishpg_tsql/sql/sys_views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ BEGIN
ELSIF typemod <= 7 THEN max_length = 5;
END IF;
WHEN 'timestamp' THEN max_length = 8;
WHEN 'vector' THEN max_length = -1; -- dummy as varchar max
ELSE max_length = typelen;
END CASE;
RETURN max_length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2201,6 +2201,176 @@ AS SELECT
WHERE FALSE;
GRANT SELECT ON sys.availability_groups TO PUBLIC;

CREATE OR REPLACE FUNCTION sys.tsql_type_max_length_helper(IN type TEXT, IN typelen INT, IN typemod INT, IN for_sys_types boolean DEFAULT false, IN used_typmod_array boolean DEFAULT false)
RETURNS SMALLINT
AS $$
DECLARE
max_length SMALLINT;
precision INT;
v_type TEXT COLLATE sys.database_default := type;
BEGIN
-- unknown tsql type
IF v_type IS NULL THEN
RETURN CAST(typelen as SMALLINT);
END IF;

-- if using typmod_array from pg_proc.probin
IF used_typmod_array THEN
IF v_type = 'sysname' THEN
RETURN 256;
ELSIF (v_type in ('char', 'bpchar', 'varchar', 'binary', 'varbinary', 'nchar', 'nvarchar'))
THEN
IF typemod < 0 THEN -- max value.
RETURN -1;
ELSIF v_type in ('nchar', 'nvarchar') THEN
RETURN (2 * typemod);
ELSE
RETURN typemod;
END IF;
END IF;
END IF;

IF typelen != -1 THEN
CASE v_type
WHEN 'tinyint' THEN max_length = 1;
WHEN 'date' THEN max_length = 3;
WHEN 'smalldatetime' THEN max_length = 4;
WHEN 'smallmoney' THEN max_length = 4;
WHEN 'datetime2' THEN
IF typemod = -1 THEN max_length = 8;
ELSIF typemod <= 2 THEN max_length = 6;
ELSIF typemod <= 4 THEN max_length = 7;
ELSEIF typemod <= 7 THEN max_length = 8;
-- typemod = 7 is not possible for datetime2 in Babel
END IF;
WHEN 'datetimeoffset' THEN
IF typemod = -1 THEN max_length = 10;
ELSIF typemod <= 2 THEN max_length = 8;
ELSIF typemod <= 4 THEN max_length = 9;
ELSIF typemod <= 7 THEN max_length = 10;
-- typemod = 7 is not possible for datetimeoffset in Babel
END IF;
WHEN 'time' THEN
IF typemod = -1 THEN max_length = 5;
ELSIF typemod <= 2 THEN max_length = 3;
ELSIF typemod <= 4 THEN max_length = 4;
ELSIF typemod <= 7 THEN max_length = 5;
END IF;
WHEN 'timestamp' THEN max_length = 8;
WHEN 'vector' THEN max_length = -1; -- dummy as varchar max
ELSE max_length = typelen;
END CASE;
RETURN max_length;
END IF;

IF typemod = -1 THEN
CASE
WHEN v_type in ('image', 'text', 'ntext') THEN max_length = 16;
WHEN v_type = 'sql_variant' THEN max_length = 8016;
WHEN v_type in ('varbinary', 'varchar', 'nvarchar') THEN
IF for_sys_types THEN max_length = 8000;
ELSE max_length = -1;
END IF;
WHEN v_type in ('binary', 'char', 'bpchar', 'nchar') THEN max_length = 8000;
WHEN v_type in ('decimal', 'numeric') THEN max_length = 17;
ELSE max_length = typemod;
END CASE;
RETURN max_length;
END IF;

CASE
WHEN v_type in ('char', 'bpchar', 'varchar', 'binary', 'varbinary') THEN max_length = typemod - 4;
WHEN v_type in ('nchar', 'nvarchar') THEN max_length = (typemod - 4) * 2;
WHEN v_type = 'sysname' THEN max_length = (typemod - 4) * 2;
WHEN v_type in ('numeric', 'decimal') THEN
precision = ((typemod - 4) >> 16) & 65535;
IF precision >= 1 and precision <= 9 THEN max_length = 5;
ELSIF precision <= 19 THEN max_length = 9;
ELSIF precision <= 28 THEN max_length = 13;
ELSIF precision <= 38 THEN max_length = 17;
ELSE max_length = typelen;
END IF;
ELSE
max_length = typemod;
END CASE;
RETURN max_length;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT;

create or replace function sys.get_tds_id(
datatype sys.varchar(50)
)
returns INT
AS $$
DECLARE
tds_id INT;
BEGIN
IF datatype IS NULL THEN
RETURN 0;
END IF;
CASE datatype
WHEN 'text' THEN tds_id = 35;
WHEN 'uniqueidentifier' THEN tds_id = 36;
WHEN 'tinyint' THEN tds_id = 38;
WHEN 'smallint' THEN tds_id = 38;
WHEN 'int' THEN tds_id = 38;
WHEN 'bigint' THEN tds_id = 38;
WHEN 'ntext' THEN tds_id = 99;
WHEN 'bit' THEN tds_id = 104;
WHEN 'float' THEN tds_id = 109;
WHEN 'real' THEN tds_id = 109;
WHEN 'varchar' THEN tds_id = 167;
WHEN 'nvarchar' THEN tds_id = 231;
WHEN 'nchar' THEN tds_id = 239;
WHEN 'money' THEN tds_id = 110;
WHEN 'smallmoney' THEN tds_id = 110;
WHEN 'char' THEN tds_id = 175;
WHEN 'date' THEN tds_id = 40;
WHEN 'datetime' THEN tds_id = 111;
WHEN 'smalldatetime' THEN tds_id = 111;
WHEN 'numeric' THEN tds_id = 108;
WHEN 'xml' THEN tds_id = 241;
WHEN 'decimal' THEN tds_id = 106;
WHEN 'varbinary' THEN tds_id = 165;
WHEN 'binary' THEN tds_id = 173;
WHEN 'image' THEN tds_id = 34;
WHEN 'time' THEN tds_id = 41;
WHEN 'datetime2' THEN tds_id = 42;
WHEN 'sql_variant' THEN tds_id = 98;
WHEN 'datetimeoffset' THEN tds_id = 43;
WHEN 'timestamp' THEN tds_id = 173;
WHEN 'vector' THEN tds_id = 167; -- Same as varchar
ELSE tds_id = 0;
END CASE;
RETURN tds_id;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION information_schema_tsql._pgtsql_char_max_length(type text, typmod int4) RETURNS integer
LANGUAGE sql
IMMUTABLE
PARALLEL SAFE
RETURNS NULL ON NULL INPUT
AS
$$SELECT
CASE WHEN type IN ('char', 'nchar', 'varchar', 'nvarchar', 'binary', 'varbinary')
THEN CASE WHEN typmod = -1
THEN -1
ELSE typmod - 4
END
WHEN type IN ('text', 'image')
THEN 2147483647
WHEN type = 'ntext'
THEN 1073741823
WHEN type = 'sysname'
THEN 128
WHEN type IN ('xml', 'vector')
THEN -1
WHEN type = 'sql_variant'
THEN 0
ELSE null
END$$;

CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'sysforeignkeys_deprecated_3_5_0');
CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'system_objects_deprecated_3_5_0');
CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'syscolumns_deprecated_3_5_0');
Expand Down
16 changes: 15 additions & 1 deletion contrib/babelfishpg_tsql/src/datatype_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#define NULLVAL PG_INT32_MIN
#define NULLVAL_STR "NULL"

#define DATATYPE_INFO_TABLE_ROWS 37
#define DATATYPE_INFO_TABLE_ROWS 38

typedef struct DatatypeInfo
{
Expand Down Expand Up @@ -487,6 +487,20 @@ static const DatatypeInfo datatype_info_table[DATATYPE_INFO_TABLE_ROWS] = {
"smalldatetime",
0, 0, 9, 3, NULLVAL, NULLVAL, 22, 16, 111,
"smalldatetime"
},
{
/* Dummy Entry for vector datatype since its not a TSQL datatype. */
"vector",
NULLVAL, NULLVAL, NULLVAL, NULLVAL,
0,
"'",
"'",
NULLVAL_STR,
1, 0, 0, NULLVAL, 0, NULLVAL,
"vector",
NULLVAL, NULLVAL, NULLVAL, NULLVAL, NULLVAL, NULLVAL, NULLVAL, NULLVAL, NULLVAL,
"vector"

}
};

Expand Down
2 changes: 1 addition & 1 deletion contrib/babelfishpg_tsql/src/procedures.c
Original file line number Diff line number Diff line change
Expand Up @@ -1787,7 +1787,7 @@ const allowed_extensions_data allowed_extns[] =
{"vector", true}
};

const int allowed_extns_size = sizeof(allowed_extns) / sizeof(allowed_extns[0]);
const int allowed_extns_size = sizeof(allowed_extns) / sizeof(allowed_extensions_data);

Datum
sp_execute_postgresql(PG_FUNCTION_ARGS)
Expand Down
47 changes: 47 additions & 0 deletions test/JDBC/expected/TestVectorDatatype.out
Original file line number Diff line number Diff line change
Expand Up @@ -1419,6 +1419,53 @@ on
~~END~~


-- system metadata, all objects get their datatype info from one of:
-- typecodes or datatype_info data we have created. Testing sys.types for typecodes
-- and sp_sproc_columns_100 for datatype_info is enough
select count(*) from sys.types where name = 'vector';
go
~~START~~
int
1
~~END~~


create procedure vector_proc_1 @a vector, @b varchar(max) as select @a as a, @b as b;
go

exec sp_sproc_columns_100 @procedure_name= 'vector_proc_1'
go
~~START~~
varchar#!#varchar#!#nvarchar#!#varchar#!#smallint#!#smallint#!#varchar#!#int#!#int#!#smallint#!#smallint#!#smallint#!#varchar#!#nvarchar#!#smallint#!#smallint#!#int#!#int#!#varchar#!#tinyint
vector_db#!#dbo#!#vector_proc_1;1#!#@RETURN_VALUE#!#5#!#4#!#int#!#10#!#4#!#0#!#10#!#0#!#<NULL>#!#<NULL>#!#4#!#<NULL>#!#<NULL>#!#0#!#NO#!#56
vector_db#!#dbo#!#vector_proc_1;1#!#@a#!#1#!#<NULL>#!#vector#!#0#!#-1#!#0#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#1#!#NO#!#<NULL>
vector_db#!#dbo#!#vector_proc_1;1#!#@b#!#1#!#12#!#varchar#!#0#!#8000#!#0#!#<NULL>#!#1#!#<NULL>#!#<NULL>#!#12#!#<NULL>#!#<NULL>#!#2#!#YES#!#39
~~END~~


drop procedure vector_proc_1;
go

create table t(a vector(3), b varchar(max))
go
select * from information_schema.columns where table_name = 't'
go
~~START~~
nvarchar#!#nvarchar#!#nvarchar#!#nvarchar#!#int#!#nvarchar#!#varchar#!#nvarchar#!#int#!#int#!#tinyint#!#smallint#!#int#!#smallint#!#nvarchar#!#nvarchar#!#nvarchar#!#nvarchar#!#nvarchar#!#nvarchar#!#nvarchar#!#nvarchar#!#nvarchar
vector_db#!#dbo#!#t#!#a#!#1#!#<NULL>#!#YES#!#vector#!#-1#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>
vector_db#!#dbo#!#t#!#b#!#2#!#<NULL>#!#YES#!#varchar#!#-1#!#-1#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#bbf_unicode_cp1_ci_as#!#<NULL>#!#<NULL>#!#<NULL>
~~END~~

select count(*) from sys.columns where object_id = sys.object_id('t')
go
~~START~~
int
2
~~END~~

drop table t;
go

-- psql
-- Need to terminate active session before cleaning up the login
SELECT pg_terminate_backend(pid) FROM pg_stat_get_activity(NULL)
Expand Down
47 changes: 47 additions & 0 deletions test/JDBC/expected/parallel_query/TestVectorDatatype.out
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,53 @@ on
~~END~~


-- system metadata, all objects get their datatype info from one of:
-- typecodes or datatype_info data we have created. Testing sys.types for typecodes
-- and sp_sproc_columns_100 for datatype_info is enough
select count(*) from sys.types where name = 'vector';
go
~~START~~
int
1
~~END~~


create procedure vector_proc_1 @a vector, @b varchar(max) as select @a as a, @b as b;
go

exec sp_sproc_columns_100 @procedure_name= 'vector_proc_1'
go
~~START~~
varchar#!#varchar#!#nvarchar#!#varchar#!#smallint#!#smallint#!#varchar#!#int#!#int#!#smallint#!#smallint#!#smallint#!#varchar#!#nvarchar#!#smallint#!#smallint#!#int#!#int#!#varchar#!#tinyint
vector_db#!#dbo#!#vector_proc_1;1#!#@RETURN_VALUE#!#5#!#4#!#int#!#10#!#4#!#0#!#10#!#0#!#<NULL>#!#<NULL>#!#4#!#<NULL>#!#<NULL>#!#0#!#NO#!#56
vector_db#!#dbo#!#vector_proc_1;1#!#@a#!#1#!#<NULL>#!#vector#!#0#!#-1#!#0#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#1#!#NO#!#<NULL>
vector_db#!#dbo#!#vector_proc_1;1#!#@b#!#1#!#12#!#varchar#!#0#!#8000#!#0#!#<NULL>#!#1#!#<NULL>#!#<NULL>#!#12#!#<NULL>#!#<NULL>#!#2#!#YES#!#39
~~END~~


drop procedure vector_proc_1;
go

create table t(a vector(3), b varchar(max))
go
select * from information_schema.columns where table_name = 't'
go
~~START~~
nvarchar#!#nvarchar#!#nvarchar#!#nvarchar#!#int#!#nvarchar#!#varchar#!#nvarchar#!#int#!#int#!#tinyint#!#smallint#!#int#!#smallint#!#nvarchar#!#nvarchar#!#nvarchar#!#nvarchar#!#nvarchar#!#nvarchar#!#nvarchar#!#nvarchar#!#nvarchar
vector_db#!#dbo#!#t#!#a#!#1#!#<NULL>#!#YES#!#vector#!#-1#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>
vector_db#!#dbo#!#t#!#b#!#2#!#<NULL>#!#YES#!#varchar#!#-1#!#-1#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#<NULL>#!#bbf_unicode_cp1_ci_as#!#<NULL>#!#<NULL>#!#<NULL>
~~END~~

select count(*) from sys.columns where object_id = sys.object_id('t')
go
~~START~~
int
2
~~END~~

drop table t;
go

-- psql
-- Need to terminate active session before cleaning up the login
SELECT pg_terminate_backend(pid) FROM pg_stat_get_activity(NULL)
Expand Down
Loading

0 comments on commit 39b554e

Please sign in to comment.