Skip to content

Commit

Permalink
Fix the issue datetime/datetime2 may not choose index scan (#2280)
Browse files Browse the repository at this point in the history
When comparing between datetime to date or datetime2 to date, it would not
choose index scan because we wrongly defined the date2datetime and
date2datetime2 cast function as VOLATILE. They should be defined as
STABLE.

Signed-off-by: Zhibai Song <[email protected]>
Task: BABEL-4517
  • Loading branch information
forestkeeper authored Jan 25, 2024
1 parent 4e200d1 commit 732ccd0
Show file tree
Hide file tree
Showing 32 changed files with 413 additions and 8 deletions.
2 changes: 1 addition & 1 deletion contrib/babelfishpg_common/sql/datetime.sql
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ WITH FUNCTION sys.timestamptz2datetime (TIMESTAMPTZ) AS ASSIGNMENT;
CREATE OR REPLACE FUNCTION sys.date2datetime(DATE)
RETURNS DATETIME
AS 'babelfishpg_common', 'date_datetime'
LANGUAGE C VOLATILE STRICT PARALLEL SAFE;
LANGUAGE C STABLE STRICT PARALLEL SAFE;

CREATE CAST (DATE AS DATETIME)
WITH FUNCTION sys.date2datetime (DATE) AS IMPLICIT;
Expand Down
2 changes: 1 addition & 1 deletion contrib/babelfishpg_common/sql/datetime2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ WITH FUNCTION sys.timestamptz2datetime2 (TIMESTAMPTZ) AS ASSIGNMENT;
CREATE OR REPLACE FUNCTION sys.date2datetime2(DATE)
RETURNS DATETIME2
AS 'babelfishpg_common', 'date_datetime2'
LANGUAGE C VOLATILE STRICT PARALLEL SAFE;
LANGUAGE C STABLE STRICT PARALLEL SAFE;

CREATE CAST (DATE AS DATETIME2)
WITH FUNCTION sys.date2datetime2 (DATE) AS IMPLICIT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,16 @@ EXCEPTION WHEN OTHERS THEN
END;
$$;

CREATE OR REPLACE FUNCTION sys.date2datetime(DATE)
RETURNS DATETIME
AS 'babelfishpg_common', 'date_datetime'
LANGUAGE C STABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.date2datetime2(DATE)
RETURNS DATETIME2
AS 'babelfishpg_common', 'date_datetime2'
LANGUAGE C STABLE STRICT PARALLEL SAFE;

-- Drops the temporary procedure used by the upgrade script.
-- Please have this be one of the last statements executed in this upgrade script.
DROP PROCEDURE sys.babelfish_drop_deprecated_object(varchar, varchar, varchar);
Expand Down
11 changes: 11 additions & 0 deletions test/JDBC/expected/babel-4517-vu-cleanup.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
drop view view_4517_date
GO

drop view view_4517_datetime
GO

drop view view_4517_datetime2
GO

drop table babel_4517
GO
33 changes: 33 additions & 0 deletions test/JDBC/expected/babel-4517-vu-prepare.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
create table babel_4517 (
date_col date NULL,
datetime_col datetime NULL,
datetime2_col datetime2 NULL
)
GO

create NONCLUSTERED INDEX date_col_indx on babel_4517
(
date_col ASC
)
GO

create NONCLUSTERED INDEX datetime_col_indx on babel_4517
(
datetime_col ASC
)
GO

create NONCLUSTERED INDEX datetime2_col_indx on babel_4517
(
datetime2_col ASC
)
GO

create view view_4517_date as select * from babel_4517 where date_col <= cast('2023-08-31' as date) and date_col >= cast('2023-08-31' as date);
GO

create view view_4517_datetime as select * from babel_4517 where datetime_col <= cast('2023-08-31' as date) and datetime_col >= cast('2023-08-31' as date);
GO

create view view_4517_datetime2 as select * from babel_4517 where datetime2_col <= cast('2023-08-31' as date) and datetime2_col >= cast('2023-08-31' as date);
GO
223 changes: 223 additions & 0 deletions test/JDBC/expected/babel-4517-vu-verify.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
BEGIN TRANSACTION babel_4517
GO

SELECT set_config('babelfishpg_tsql.explain_costs', 'off', true)
GO
~~START~~
text
off
~~END~~


SELECT set_config('debug_parallel_query', '0', true)
GO
~~START~~
text
off
~~END~~


SET BABELFISH_SHOWPLAN_ALL on
GO

select * from view_4517_date;
GO
~~START~~
text
Query Text: select * from view_4517_date
Bitmap Heap Scan on babel_4517
Recheck Cond: ((date_col <= '2023-08-31'::date) AND (date_col >= '2023-08-31'::date))
-> Bitmap Index Scan on date_col_indxbabel_4517ff7ff8b3790b9274b0932920e1e110d9
Index Cond: ((date_col <= '2023-08-31'::date) AND (date_col >= '2023-08-31'::date))
~~END~~


select * from view_4517_datetime
GO
~~START~~
text
Query Text: select * from view_4517_datetime
Bitmap Heap Scan on babel_4517
Recheck Cond: ((datetime_col <= ('2023-08-31'::date)::datetime) AND (datetime_col >= ('2023-08-31'::date)::datetime))
-> Bitmap Index Scan on datetime_col_indxbabel_4517f212471e91ba8fdbefe418ea0d8f877d
Index Cond: ((datetime_col <= ('2023-08-31'::date)::datetime) AND (datetime_col >= ('2023-08-31'::date)::datetime))
~~END~~


select * from view_4517_datetime2
GO
~~START~~
text
Query Text: select * from view_4517_datetime2
Bitmap Heap Scan on babel_4517
Recheck Cond: ((datetime2_col <= ('2023-08-31'::date)::datetime2) AND (datetime2_col >= ('2023-08-31'::date)::datetime2))
-> Bitmap Index Scan on datetime2_col_indxbabel_451706006b35cf68c461b9e87edb9222fcdd
Index Cond: ((datetime2_col <= ('2023-08-31'::date)::datetime2) AND (datetime2_col >= ('2023-08-31'::date)::datetime2))
~~END~~


select * from babel_4517 where date_col <= cast('2023-08-31' as date) and date_col >= cast('2023-08-31' as date);
GO
~~START~~
text
Query Text: select * from babel_4517 where date_col <= cast('2023-08-31' as date) and date_col >= cast('2023-08-31' as date)
Bitmap Heap Scan on babel_4517
Recheck Cond: ((date_col <= '2023-08-31'::date) AND (date_col >= '2023-08-31'::date))
-> Bitmap Index Scan on date_col_indxbabel_4517ff7ff8b3790b9274b0932920e1e110d9
Index Cond: ((date_col <= '2023-08-31'::date) AND (date_col >= '2023-08-31'::date))
~~END~~


select * from babel_4517 where datetime_col <= cast('2023-08-31' as date) and datetime_col >= cast('2023-08-31' as date);
GO
~~START~~
text
Query Text: select * from babel_4517 where datetime_col <= cast('2023-08-31' as date) and datetime_col >= cast('2023-08-31' as date)
Bitmap Heap Scan on babel_4517
Recheck Cond: ((datetime_col <= ('2023-08-31'::date)::datetime) AND (datetime_col >= ('2023-08-31'::date)::datetime))
-> Bitmap Index Scan on datetime_col_indxbabel_4517f212471e91ba8fdbefe418ea0d8f877d
Index Cond: ((datetime_col <= ('2023-08-31'::date)::datetime) AND (datetime_col >= ('2023-08-31'::date)::datetime))
~~END~~


select * from babel_4517 where datetime2_col <= cast('2023-08-31' as date) and datetime2_col >= cast('2023-08-31' as date);
GO
~~START~~
text
Query Text: select * from babel_4517 where datetime2_col <= cast('2023-08-31' as date) and datetime2_col >= cast('2023-08-31' as date)
Bitmap Heap Scan on babel_4517
Recheck Cond: ((datetime2_col <= ('2023-08-31'::date)::datetime2) AND (datetime2_col >= ('2023-08-31'::date)::datetime2))
-> Bitmap Index Scan on datetime2_col_indxbabel_451706006b35cf68c461b9e87edb9222fcdd
Index Cond: ((datetime2_col <= ('2023-08-31'::date)::datetime2) AND (datetime2_col >= ('2023-08-31'::date)::datetime2))
~~END~~


SET BABELFISH_SHOWPLAN_ALL off
GO

SELECT set_config('debug_parallel_query', '1', true)
SELECT set_config('parallel_setup_cost', '0', true)
SELECT set_config('parallel_tuple_cost', '0', true)
GO
~~START~~
text
on
~~END~~

~~START~~
text
0
~~END~~

~~START~~
text
0
~~END~~


SET BABELFISH_SHOWPLAN_ALL on
GO

select * from view_4517_date;
GO
~~START~~
text
Query Text: select * from view_4517_date
Gather
Workers Planned: 1
Single Copy: true
-> Bitmap Heap Scan on babel_4517
Recheck Cond: ((date_col <= '2023-08-31'::date) AND (date_col >= '2023-08-31'::date))
-> Bitmap Index Scan on date_col_indxbabel_4517ff7ff8b3790b9274b0932920e1e110d9
Index Cond: ((date_col <= '2023-08-31'::date) AND (date_col >= '2023-08-31'::date))
~~END~~


select * from view_4517_datetime
GO
~~START~~
text
Query Text: select * from view_4517_datetime
Gather
Workers Planned: 1
Single Copy: true
-> Bitmap Heap Scan on babel_4517
Recheck Cond: ((datetime_col <= ('2023-08-31'::date)::datetime) AND (datetime_col >= ('2023-08-31'::date)::datetime))
-> Bitmap Index Scan on datetime_col_indxbabel_4517f212471e91ba8fdbefe418ea0d8f877d
Index Cond: ((datetime_col <= ('2023-08-31'::date)::datetime) AND (datetime_col >= ('2023-08-31'::date)::datetime))
~~END~~


select * from view_4517_datetime2
GO
~~START~~
text
Query Text: select * from view_4517_datetime2
Gather
Workers Planned: 1
Single Copy: true
-> Bitmap Heap Scan on babel_4517
Recheck Cond: ((datetime2_col <= ('2023-08-31'::date)::datetime2) AND (datetime2_col >= ('2023-08-31'::date)::datetime2))
-> Bitmap Index Scan on datetime2_col_indxbabel_451706006b35cf68c461b9e87edb9222fcdd
Index Cond: ((datetime2_col <= ('2023-08-31'::date)::datetime2) AND (datetime2_col >= ('2023-08-31'::date)::datetime2))
~~END~~


select * from babel_4517 where date_col <= cast('2023-08-31' as date) and date_col >= cast('2023-08-31' as date);
GO
~~START~~
text
Query Text: select * from babel_4517 where date_col <= cast('2023-08-31' as date) and date_col >= cast('2023-08-31' as date)
Gather
Workers Planned: 1
Single Copy: true
-> Bitmap Heap Scan on babel_4517
Recheck Cond: ((date_col <= '2023-08-31'::date) AND (date_col >= '2023-08-31'::date))
-> Bitmap Index Scan on date_col_indxbabel_4517ff7ff8b3790b9274b0932920e1e110d9
Index Cond: ((date_col <= '2023-08-31'::date) AND (date_col >= '2023-08-31'::date))
~~END~~


select * from babel_4517 where datetime_col <= cast('2023-08-31' as date) and datetime_col >= cast('2023-08-31' as date);
GO
~~START~~
text
Query Text: select * from babel_4517 where datetime_col <= cast('2023-08-31' as date) and datetime_col >= cast('2023-08-31' as date)
Gather
Workers Planned: 1
Single Copy: true
-> Bitmap Heap Scan on babel_4517
Recheck Cond: ((datetime_col <= ('2023-08-31'::date)::datetime) AND (datetime_col >= ('2023-08-31'::date)::datetime))
-> Bitmap Index Scan on datetime_col_indxbabel_4517f212471e91ba8fdbefe418ea0d8f877d
Index Cond: ((datetime_col <= ('2023-08-31'::date)::datetime) AND (datetime_col >= ('2023-08-31'::date)::datetime))
~~END~~


select * from babel_4517 where datetime2_col <= cast('2023-08-31' as date) and datetime2_col >= cast('2023-08-31' as date);
GO
~~START~~
text
Query Text: select * from babel_4517 where datetime2_col <= cast('2023-08-31' as date) and datetime2_col >= cast('2023-08-31' as date)
Gather
Workers Planned: 1
Single Copy: true
-> Bitmap Heap Scan on babel_4517
Recheck Cond: ((datetime2_col <= ('2023-08-31'::date)::datetime2) AND (datetime2_col >= ('2023-08-31'::date)::datetime2))
-> Bitmap Index Scan on datetime2_col_indxbabel_451706006b35cf68c461b9e87edb9222fcdd
Index Cond: ((datetime2_col <= ('2023-08-31'::date)::datetime2) AND (datetime2_col >= ('2023-08-31'::date)::datetime2))
~~END~~


SET BABELFISH_SHOWPLAN_ALL off
GO

COMMIT TRANSACTION babel_4517
GO

SELECT set_config('babelfishpg_tsql.explain_costs', 'on', false)
GO
~~START~~
text
on
~~END~~

11 changes: 11 additions & 0 deletions test/JDBC/input/babel-4517-vu-cleanup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
drop view view_4517_date
GO

drop view view_4517_datetime
GO

drop view view_4517_datetime2
GO

drop table babel_4517
GO
33 changes: 33 additions & 0 deletions test/JDBC/input/babel-4517-vu-prepare.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
create table babel_4517 (
date_col date NULL,
datetime_col datetime NULL,
datetime2_col datetime2 NULL
)
GO

create NONCLUSTERED INDEX date_col_indx on babel_4517
(
date_col ASC
)
GO

create NONCLUSTERED INDEX datetime_col_indx on babel_4517
(
datetime_col ASC
)
GO

create NONCLUSTERED INDEX datetime2_col_indx on babel_4517
(
datetime2_col ASC
)
GO

create view view_4517_date as select * from babel_4517 where date_col <= cast('2023-08-31' as date) and date_col >= cast('2023-08-31' as date);
GO

create view view_4517_datetime as select * from babel_4517 where datetime_col <= cast('2023-08-31' as date) and datetime_col >= cast('2023-08-31' as date);
GO

create view view_4517_datetime2 as select * from babel_4517 where datetime2_col <= cast('2023-08-31' as date) and datetime2_col >= cast('2023-08-31' as date);
GO
Loading

0 comments on commit 732ccd0

Please sign in to comment.