Skip to content

Commit

Permalink
Add sort operators for MAX/MIN of Babelfish datatypes (babelfish-for-…
Browse files Browse the repository at this point in the history
…postgresql#1989)


Previously, the MAX/MIN aggregation with index cannot be correctly
optimized into LIMIT 1 + index scan. This affects most of existing
Babelfish-defined datatypes.

This commit fixes this issue by adding corresponding sort operators
to the MAX/MIN aggregation functions.

Task: BABEL-4311
Signed-off-by: Xiaohui Fanhe <[email protected]>
  • Loading branch information
xhfanhe authored Nov 7, 2023
1 parent a26d448 commit cee66be
Show file tree
Hide file tree
Showing 34 changed files with 1,207 additions and 17 deletions.
2 changes: 2 additions & 0 deletions contrib/babelfishpg_common/sql/bpchar.sql
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ CREATE OR REPLACE AGGREGATE sys.max(sys.BPCHAR)
sfunc = sys.bpchar_larger,
stype = sys.bpchar,
combinefunc = sys.bpchar_larger,
sortop = >,
parallel = safe
);

Expand All @@ -302,6 +303,7 @@ CREATE OR REPLACE AGGREGATE sys.min(sys.BPCHAR)
sfunc = sys.bpchar_smaller,
stype = sys.bpchar,
combinefunc = sys.bpchar_smaller,
sortop = <,
parallel = safe
);

Expand Down
4 changes: 3 additions & 1 deletion contrib/babelfishpg_common/sql/datetime.sql
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ CREATE OR REPLACE AGGREGATE sys.max(sys.DATETIME)
sfunc = sys.datetime_larger,
stype = sys.datetime,
combinefunc = sys.datetime_larger,
sortop = >,
parallel = safe
);

Expand All @@ -160,6 +161,7 @@ CREATE OR REPLACE AGGREGATE sys.min(sys.DATETIME)
sfunc = sys.datetime_smaller,
stype = sys.datetime,
combinefunc = sys.datetime_smaller,
sortop = <,
parallel = safe
);

Expand Down Expand Up @@ -564,4 +566,4 @@ LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE OR REPLACE FUNCTION sys.datetime_to_numeric(IN arg sys.DATETIME)
RETURNS NUMERIC
AS 'babelfishpg_common', 'datetime_to_numeric'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
2 changes: 2 additions & 0 deletions contrib/babelfishpg_common/sql/datetime2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ CREATE OR REPLACE AGGREGATE sys.max(sys.DATETIME2)
sfunc = sys.datetime2_larger,
stype = sys.datetime2,
combinefunc = sys.datetime2_larger,
sortop = >,
parallel = safe
);

Expand All @@ -184,6 +185,7 @@ CREATE OR REPLACE AGGREGATE sys.min(sys.DATETIME2)
sfunc = sys.datetime2_smaller,
stype = sys.datetime2,
combinefunc = sys.datetime2_smaller,
sortop = <,
parallel = safe
);

Expand Down
2 changes: 2 additions & 0 deletions contrib/babelfishpg_common/sql/datetimeoffset.sql
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ CREATE OR REPLACE AGGREGATE sys.max(sys.DATETIMEOFFSET)
sfunc = sys.datetimeoffset_larger,
stype = sys.datetimeoffset,
combinefunc = sys.datetimeoffset_larger,
sortop = >,
parallel = safe
);

Expand All @@ -226,6 +227,7 @@ CREATE OR REPLACE AGGREGATE sys.min(sys.DATETIMEOFFSET)
sfunc = sys.datetimeoffset_smaller,
stype = sys.datetimeoffset,
combinefunc = sys.datetimeoffset_smaller,
sortop = <,
parallel = safe
);

Expand Down
4 changes: 3 additions & 1 deletion contrib/babelfishpg_common/sql/smalldatetime.sql
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ CREATE OR REPLACE AGGREGATE sys.max(sys.SMALLDATETIME)
sfunc = sys.smalldatetime_larger,
stype = sys.smalldatetime,
combinefunc = sys.smalldatetime_larger,
sortop = >,
parallel = safe
);

Expand All @@ -160,6 +161,7 @@ CREATE OR REPLACE AGGREGATE sys.min(sys.SMALLDATETIME)
sfunc = sys.smalldatetime_smaller,
stype = sys.smalldatetime,
combinefunc = sys.smalldatetime_smaller,
sortop = <,
parallel = safe
);

Expand Down Expand Up @@ -762,4 +764,4 @@ LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE OR REPLACE FUNCTION sys.smalldatetime_to_numeric(IN arg sys.SMALLDATETIME)
RETURNS NUMERIC
AS 'babelfishpg_common', 'smalldatetime_to_numeric'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
Original file line number Diff line number Diff line change
@@ -1,3 +1,126 @@
------------------------------------------------------------------------------
---- 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 "3.3.0"" to load this file. \quit

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

-- Add SORTOP for aggregations
-- bpchar
CREATE OR REPLACE AGGREGATE sys.max(sys.BPCHAR)
(
sfunc = sys.bpchar_larger,
stype = sys.bpchar,
combinefunc = sys.bpchar_larger,
sortop = >,
parallel = safe
);

CREATE OR REPLACE AGGREGATE sys.min(sys.BPCHAR)
(
sfunc = sys.bpchar_smaller,
stype = sys.bpchar,
combinefunc = sys.bpchar_smaller,
sortop = <,
parallel = safe
);

-- varchar
CREATE OR REPLACE AGGREGATE sys.max(sys.VARCHAR)
(
sfunc = sys.varchar_larger,
stype = sys.varchar,
combinefunc = sys.varchar_larger,
sortop = >,
parallel = safe
);

CREATE OR REPLACE AGGREGATE sys.min(sys.VARCHAR)
(
sfunc = sys.varchar_smaller,
stype = sys.varchar,
combinefunc = sys.varchar_smaller,
sortop = <,
parallel = safe
);

-- datetime
CREATE OR REPLACE AGGREGATE sys.max(sys.DATETIME)
(
sfunc = sys.datetime_larger,
stype = sys.datetime,
combinefunc = sys.datetime_larger,
sortop = >,
parallel = safe
);

CREATE OR REPLACE AGGREGATE sys.min(sys.DATETIME)
(
sfunc = sys.datetime_smaller,
stype = sys.datetime,
combinefunc = sys.datetime_smaller,
sortop = <,
parallel = safe
);

-- datetime2
CREATE OR REPLACE AGGREGATE sys.max(sys.DATETIME2)
(
sfunc = sys.datetime2_larger,
stype = sys.datetime2,
combinefunc = sys.datetime2_larger,
sortop = >,
parallel = safe
);

CREATE OR REPLACE AGGREGATE sys.min(sys.DATETIME2)
(
sfunc = sys.datetime2_smaller,
stype = sys.datetime2,
combinefunc = sys.datetime2_smaller,
sortop = <,
parallel = safe
);

-- datetimeoffset
CREATE OR REPLACE AGGREGATE sys.max(sys.DATETIMEOFFSET)
(
sfunc = sys.datetimeoffset_larger,
stype = sys.datetimeoffset,
combinefunc = sys.datetimeoffset_larger,
sortop = >,
parallel = safe
);

CREATE OR REPLACE AGGREGATE sys.min(sys.DATETIMEOFFSET)
(
sfunc = sys.datetimeoffset_smaller,
stype = sys.datetimeoffset,
combinefunc = sys.datetimeoffset_smaller,
sortop = <,
parallel = safe
);

-- smalldatetime
CREATE OR REPLACE AGGREGATE sys.max(sys.SMALLDATETIME)
(
sfunc = sys.smalldatetime_larger,
stype = sys.smalldatetime,
combinefunc = sys.smalldatetime_larger,
sortop = >,
parallel = safe
);

CREATE OR REPLACE AGGREGATE sys.min(sys.SMALLDATETIME)
(
sfunc = sys.smalldatetime_smaller,
stype = sys.smalldatetime,
combinefunc = sys.smalldatetime_smaller,
sortop = <,
parallel = safe
);

-- Reset search_path to not affect any subsequent scripts
SELECT set_config('search_path', trim(leading 'sys, ' from current_setting('search_path')), false);
2 changes: 2 additions & 0 deletions contrib/babelfishpg_common/sql/varchar.sql
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ CREATE OR REPLACE AGGREGATE sys.max(sys.VARCHAR)
sfunc = sys.varchar_larger,
stype = sys.varchar,
combinefunc = sys.varchar_larger,
sortop = >,
parallel = safe
);

Expand All @@ -248,6 +249,7 @@ CREATE OR REPLACE AGGREGATE sys.min(sys.VARCHAR)
sfunc = sys.varchar_smaller,
stype = sys.varchar,
combinefunc = sys.varchar_smaller,
sortop = <,
parallel = safe
);

Expand Down
33 changes: 33 additions & 0 deletions test/JDBC/expected/TestDatatypeAggSort-vu-cleanup.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
DROP VIEW TestDatatypeAggSort_vu_prepare_char_view_max
go
DROP VIEW TestDatatypeAggSort_vu_prepare_char_view_min
go
DROP VIEW TestDatatypeAggSort_vu_prepare_varchar_view_max
go
DROP VIEW TestDatatypeAggSort_vu_prepare_varchar_view_min
go
DROP VIEW TestDatatypeAggSort_vu_prepare_datetime_view_max
go
DROP VIEW TestDatatypeAggSort_vu_prepare_datetime_view_min
go
DROP VIEW TestDatatypeAggSort_vu_prepare_datetime2_view_max
go
DROP VIEW TestDatatypeAggSort_vu_prepare_datetime2_view_min
go
DROP VIEW TestDatatypeAggSort_vu_prepare_datetimeoffset_view_max
go
DROP VIEW TestDatatypeAggSort_vu_prepare_datetimeoffset_view_min
go
DROP VIEW TestDatatypeAggSort_vu_prepare_smalldatetime_view_max
go
DROP VIEW TestDatatypeAggSort_vu_prepare_smalldatetime_view_min
go

DROP PROCEDURE TestDatatypeAggSort_vu_prepare_proc
go

DROP TABLE TestDatatypeAggSort_vu_prepare_tbl
go

DROP TABLE TestDatatypeAggSort_vu_prepare_tbl2
go
Loading

0 comments on commit cee66be

Please sign in to comment.