Skip to content

Commit

Permalink
Allow Smalldatetime type and date type compare to choose index clause. (
Browse files Browse the repository at this point in the history
#3085)

Index scan would not be chosen by optimiser if there is any filter that involves operator between Smalldatetime and date even though index is created on Smalldatetime column. 

We have defined an operator class between smalldatetime and date.

Issues Resolved
Task: BABEL-4709

Authored-by: yashneet vinayak [email protected]
Signed-off-by: yashneet vinayak <[email protected]>
  • Loading branch information
Yvinayak07 authored Dec 2, 2024
1 parent b9e4d53 commit cd20d5f
Show file tree
Hide file tree
Showing 53 changed files with 10,630 additions and 18 deletions.
9 changes: 8 additions & 1 deletion contrib/babelfishpg_common/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ GENERATED_UPGRADES = \
sql/$(EXTENSION)--4.0.0--4.1.0.sql \
sql/$(EXTENSION)--4.1.0--4.2.0.sql \
sql/$(EXTENSION)--4.2.0--4.3.0.sql \
sql/$(EXTENSION)--4.3.0--4.4.0.sql
sql/$(EXTENSION)--4.3.0--4.4.0.sql \
sql/$(EXTENSION)--4.4.0--4.5.0.sql

ifdef PREV_EXTVERSION
DATA = sql/$(EXTENSION)--$(PREV_EXTVERSION).sql
Expand Down Expand Up @@ -126,6 +127,9 @@ ifeq (,$(filter $(FLAG_TO_CHECK),$(PG_CPPFLAGS)))

sql/$(EXTENSION)--4.3.0--4.4.0.sql: sql/upgrades/babelfishpg_upgrades.in
cpp -D PREV_VERSION=4.3.0 -D CUR_VERSION=4.4.0 $< | sed 's/^# /-- /g' > $@

sql/$(EXTENSION)--4.4.0--4.5.0.sql: sql/upgrades/babelfishpg_upgrades.in
cpp -D PREV_VERSION=4.4.0 -D CUR_VERSION=4.5.0 $< | sed 's/^# /-- /g' > $@
else
# The flag is present build the .in file including the spatial type files
sql/$(EXTENSION)--$(EXTVERSION).sql: sql/$(EXTENSION).in
Expand All @@ -152,6 +156,9 @@ else
sql/$(EXTENSION)--4.3.0--4.4.0.sql: sql/upgrades/babelfishpg_upgrades.in
cpp -D ENABLE_SPATIAL_TYPES=1 -D PREV_VERSION=4.3.0 -D CUR_VERSION=4.4.0 $< | sed 's/^# /-- /g' > $@

sql/$(EXTENSION)--4.4.0--4.5.0.sql: sql/upgrades/babelfishpg_upgrades.in
cpp -D ENABLE_SPATIAL_TYPES=1 -D PREV_VERSION=4.4.0 -D CUR_VERSION=4.5.0 $< | sed 's/^# /-- /g' > $@

endif

sql/babelfishpg_common--%.sql: sql/upgrades/babelfishpg_common--%.sql
Expand Down
2 changes: 1 addition & 1 deletion contrib/babelfishpg_common/Version.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
BBFPGCMN_MAJOR_VERSION=4
BBFPGCMN_MINOR_VERSION=4
BBFPGCMN_MINOR_VERSION=5
BBFPGCMN_MICRO_VERSION=0

30 changes: 29 additions & 1 deletion contrib/babelfishpg_common/sql/smalldatetime.sql
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,16 @@ RETURNS INT4
AS 'timestamp_cmp'
LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE;

CREATE FUNCTION sys.smalldatetime_date_cmp(sys.SMALLDATETIME, date)
RETURNS INT4
AS 'timestamp_cmp_date'
LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE;

CREATE FUNCTION sys.date_smalldatetime_cmp(date, sys.SMALLDATETIME)
RETURNS INT4
AS 'date_cmp_timestamp'
LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE;

CREATE FUNCTION smalldatetime_hash(sys.SMALLDATETIME)
RETURNS INT4
AS 'timestamp_hash'
Expand All @@ -482,7 +492,25 @@ DEFAULT FOR TYPE sys.SMALLDATETIME USING btree AS
OPERATOR 3 = (sys.SMALLDATETIME, sys.SMALLDATETIME),
OPERATOR 4 >= (sys.SMALLDATETIME, sys.SMALLDATETIME),
OPERATOR 5 > (sys.SMALLDATETIME, sys.SMALLDATETIME),
FUNCTION 1 smalldatetime_cmp(sys.SMALLDATETIME, sys.SMALLDATETIME);
FUNCTION 1 sys.smalldatetime_cmp(sys.SMALLDATETIME, sys.SMALLDATETIME);

CREATE OPERATOR CLASS sys.smalldatetime_date_ops
FOR TYPE sys.SMALLDATETIME USING btree FAMILY smalldatetime_ops AS
OPERATOR 1 sys.< (sys.SMALLDATETIME, date),
OPERATOR 2 sys.<= (sys.SMALLDATETIME, date),
OPERATOR 3 sys.= (sys.SMALLDATETIME, date),
OPERATOR 4 sys.>= (sys.SMALLDATETIME, date),
OPERATOR 5 sys.> (sys.SMALLDATETIME, date),
FUNCTION 1 sys.smalldatetime_date_cmp(sys.SMALLDATETIME, date);

CREATE OPERATOR CLASS sys.date_smalldatetime_ops
FOR TYPE sys.SMALLDATETIME USING btree FAMILY smalldatetime_ops AS
OPERATOR 1 sys.< (date, sys.SMALLDATETIME),
OPERATOR 2 sys.<= (date, sys.SMALLDATETIME),
OPERATOR 3 sys.= (date, sys.SMALLDATETIME),
OPERATOR 4 sys.>= (date, sys.SMALLDATETIME),
OPERATOR 5 sys.> (date, sys.SMALLDATETIME),
FUNCTION 1 sys.date_smalldatetime_cmp(date, sys.SMALLDATETIME);

CREATE OPERATOR CLASS sys.smalldatetime_ops
DEFAULT FOR TYPE sys.SMALLDATETIME USING hash AS
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
------------------------------------------------------------------------------
---- Include changes related to other datatypes except spatial types here ----
------------------------------------------------------------------------------

-- complain if script is sourced in psql, rather than via ALTER EXTENSION
\echo Use "ALTER EXTENSION ""babelfishpg_common"" UPDATE TO "4.5.0"" to load this file. \quit

SELECT set_config('search_path', 'sys, '||current_setting('search_path'), false);

CREATE OR REPLACE FUNCTION sys.smalldatetime_date_cmp(sys.SMALLDATETIME, date)
RETURNS INT4
AS 'timestamp_cmp_date'
LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.date_smalldatetime_cmp(date, sys.SMALLDATETIME)
RETURNS INT4
AS 'date_cmp_timestamp'
LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE;

-- Operator class for smalldatetime_ops to incorporate various operator between smalldatetime and date for Index scan
DO $$
BEGIN
IF NOT EXISTS(SELECT 1 FROM pg_opclass opc JOIN pg_opfamily opf ON opc.opcfamily = opf.oid WHERE opc.opcname = 'smalldatetime_date_ops' AND opc.opcnamespace = (SELECT oid FROM pg_namespace WHERE nspname = 'sys') AND opf.opfname = 'smalldatetime_ops') THEN
CREATE OPERATOR CLASS sys.smalldatetime_date_ops
FOR TYPE sys.SMALLDATETIME USING btree FAMILY smalldatetime_ops AS
OPERATOR 1 sys.< (sys.SMALLDATETIME, date),
OPERATOR 2 sys.<= (sys.SMALLDATETIME, date),
OPERATOR 3 sys.= (sys.SMALLDATETIME, date),
OPERATOR 4 sys.>= (sys.SMALLDATETIME, date),
OPERATOR 5 sys.> (sys.SMALLDATETIME, date),
FUNCTION 1 sys.smalldatetime_date_cmp(sys.SMALLDATETIME, date);
END IF;
END $$;

-- Operator class for smalldatetime_ops to incorporate various operator between date and smalldatetime for Index scan
DO $$
BEGIN
IF NOT EXISTS(SELECT 1 FROM pg_opclass opc JOIN pg_opfamily opf ON opc.opcfamily = opf.oid WHERE opc.opcname = 'date_smalldatetime_ops' AND opc.opcnamespace = (SELECT oid FROM pg_namespace WHERE nspname = 'sys') AND opf.opfname = 'smalldatetime_ops') THEN
CREATE OPERATOR CLASS sys.date_smalldatetime_ops
FOR TYPE sys.SMALLDATETIME USING btree FAMILY smalldatetime_ops AS
OPERATOR 1 sys.< (date, sys.SMALLDATETIME),
OPERATOR 2 sys.<= (date, sys.SMALLDATETIME),
OPERATOR 3 sys.= (date, sys.SMALLDATETIME),
OPERATOR 4 sys.>= (date, sys.SMALLDATETIME),
OPERATOR 5 sys.> (date, sys.SMALLDATETIME),
FUNCTION 1 sys.date_smalldatetime_cmp(date, sys.SMALLDATETIME);
END IF;
END $$;

-- Reset search_path to not affect any subsequent scripts
SELECT set_config('search_path', trim(leading 'sys, ' from current_setting('search_path')), false);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- complain if script is sourced in psql, rather than via ALTER EXTENSION
\echo Use "ALTER EXTENSION ""babelfishpg_common"" UPDATE TO '4.0.0'" to load this file. \quit
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-------------------------------------------------------
---- Include changes related to spatial types here ----
-------------------------------------------------------
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
DROP PROCEDURE test_smalldatetime_date_p1;
GO

DROP PROCEDURE test_smalldatetime_date_p2;
GO

DROP PROCEDURE test_smalldatetime_date_p3;
GO

DROP PROCEDURE test_smalldatetime_date_p4;
GO

DROP PROCEDURE test_smalldatetime_date_p5;
GO

DROP PROCEDURE test_smalldatetime_date_p6;
GO

DROP PROCEDURE test_smalldatetime_date_p7;
GO

DROP PROCEDURE test_smalldatetime_date_p8;
GO

DROP PROCEDURE test_smalldatetime_date_p9;
GO

DROP PROCEDURE test_smalldatetime_date_p10;
GO

DROP PROCEDURE test_smalldatetime_date_p11;
GO

DROP PROCEDURE test_smalldatetime_date_p12;
GO

DROP PROCEDURE test_smalldatetime_date_p13;
GO

DROP PROCEDURE test_smalldatetime_date_p14;
GO

DROP PROCEDURE test_smalldatetime_date_p15;
GO

DROP PROCEDURE test_smalldatetime_date_p16;
GO

DROP PROCEDURE test_smalldatetime_date_p17;
GO

DROP PROCEDURE test_smalldatetime_date_p18;
GO

DROP PROCEDURE test_smalldatetime_date_p19;
GO

DROP PROCEDURE test_smalldatetime_date_p20;
GO

DROP VIEW test_smalldatetime_date_v1;
GO

DROP VIEW test_smalldatetime_date_v2;
GO

DROP VIEW test_smalldatetime_date_v3;
GO

DROP VIEW test_smalldatetime_date_v4;
GO

DROP VIEW test_smalldatetime_date_v5;
GO

DROP VIEW test_smalldatetime_date_v6;
GO

DROP VIEW test_smalldatetime_date_v7;
GO

DROP VIEW test_smalldatetime_date_v8;
GO

DROP VIEW test_smalldatetime_date_v9;
GO

DROP VIEW test_smalldatetime_date_v10;
GO

DROP VIEW test_smalldatetime_date_v11;
GO

DROP VIEW test_smalldatetime_date_v12;
GO

DROP VIEW test_smalldatetime_date_v13;
GO

DROP VIEW test_smalldatetime_date_v14;
GO

DROP VIEW test_smalldatetime_date_v15;
GO

DROP VIEW test_smalldatetime_date_v16;
GO

DROP VIEW test_smalldatetime_date_v17;
GO

DROP VIEW test_smalldatetime_date_v18;
GO

DROP VIEW test_smalldatetime_date_v19;
GO

DROP VIEW test_smalldatetime_date_v20;
GO

DROP INDEX smalldate_date_cmp_ind1 on smalldate_date_cmp_t1;
GO

DROP TABLE smalldate_date_cmp_t1;
GO

-- tests for index on date column
DROP PROCEDURE test_date_smalldatetime_p1;
GO

DROP PROCEDURE test_date_smalldatetime_p2;
GO

DROP PROCEDURE test_date_smalldatetime_p3;
GO

DROP PROCEDURE test_date_smalldatetime_p4;
GO

DROP PROCEDURE test_date_smalldatetime_p5;
GO

DROP PROCEDURE test_date_smalldatetime_p6;
GO

DROP PROCEDURE test_date_smalldatetime_p7;
GO

DROP PROCEDURE test_date_smalldatetime_p8;
GO

DROP PROCEDURE test_date_smalldatetime_p9;
GO

DROP PROCEDURE test_date_smalldatetime_p10;
GO

DROP PROCEDURE test_date_smalldatetime_p11;
GO

DROP PROCEDURE test_date_smalldatetime_p12;
GO

DROP PROCEDURE test_date_smalldatetime_p13;
GO

DROP PROCEDURE test_date_smalldatetime_p15;
GO

DROP PROCEDURE test_date_smalldatetime_p16;
GO

DROP PROCEDURE test_date_smalldatetime_p17;
GO

DROP PROCEDURE test_date_smalldatetime_p18;
GO

DROP PROCEDURE test_date_smalldatetime_p19;
GO

DROP PROCEDURE test_date_smalldatetime_p20;
GO

DROP VIEW test_date_smalldatetime_v1;
GO

DROP VIEW test_date_smalldatetime_v2;
GO

DROP VIEW test_date_smalldatetime_v3;
GO

DROP VIEW test_date_smalldatetime_v4;
GO

DROP VIEW test_date_smalldatetime_v5;
GO

DROP VIEW test_date_smalldatetime_v6;
GO

DROP VIEW test_date_smalldatetime_v7;
GO

DROP VIEW test_date_smalldatetime_v8;
GO

DROP VIEW test_date_smalldatetime_v9;
GO

DROP VIEW test_date_smalldatetime_v10;
GO

DROP VIEW test_date_smalldatetime_v11;
GO

DROP VIEW test_date_smalldatetime_v12;
GO

DROP VIEW test_date_smalldatetime_v13;
GO

DROP VIEW test_date_smalldatetime_v15;
GO

DROP VIEW test_date_smalldatetime_v16;
GO

DROP VIEW test_date_smalldatetime_v17;
GO

DROP VIEW test_date_smalldatetime_v18;
GO

DROP VIEW test_date_smalldatetime_v19;
GO

DROP VIEW test_date_smalldatetime_v20;
GO

DROP INDEX date_smalldatetime_cmp_ind1 on date_smalldatetime_cmp_t1;
GO

DROP TABLE date_smalldatetime_cmp_t1;
GO
Loading

0 comments on commit cd20d5f

Please sign in to comment.