Add btree_gin support for enum types
authorAndrew Dunstan <andrew@dunslane.net>
Tue, 21 Mar 2017 15:04:17 +0000 (11:04 -0400)
committerAndrew Dunstan <andrew@dunslane.net>
Tue, 21 Mar 2017 15:04:17 +0000 (11:04 -0400)
Reviewed by Tom Lane and Anastasia Lubennikova

Discussion:  http://postgr.es/m/56EA8A71.8060107@dunslane.net

contrib/btree_gin/Makefile
contrib/btree_gin/btree_gin--1.1--1.2.sql [new file with mode: 0644]
contrib/btree_gin/btree_gin.c
contrib/btree_gin/btree_gin.control
contrib/btree_gin/expected/enum.out [new file with mode: 0644]
contrib/btree_gin/sql/enum.sql [new file with mode: 0644]

index f22e4af7dfb2105b1d17fed6066264447d627ef1..690e1d7602678c65f57dd8c4d6bd4682020f7235 100644 (file)
@@ -4,13 +4,14 @@ MODULE_big = btree_gin
 OBJS = btree_gin.o $(WIN32RES)
 
 EXTENSION = btree_gin
-DATA = btree_gin--1.0.sql btree_gin--1.0--1.1.sql btree_gin--unpackaged--1.0.sql
+DATA = btree_gin--1.0.sql btree_gin--1.0--1.1.sql btree_gin--1.1--1.2.sql \
+   btree_gin--unpackaged--1.0.sql
 PGFILEDESC = "btree_gin - B-tree equivalent GIN operator classes"
 
 REGRESS = install_btree_gin int2 int4 int8 float4 float8 money oid \
    timestamp timestamptz time timetz date interval \
    macaddr macaddr8 inet cidr text varchar char bytea bit varbit \
-   numeric
+   numeric enum
 
 ifdef USE_PGXS
 PG_CONFIG = pg_config
diff --git a/contrib/btree_gin/btree_gin--1.1--1.2.sql b/contrib/btree_gin/btree_gin--1.1--1.2.sql
new file mode 100644 (file)
index 0000000..2a16837
--- /dev/null
@@ -0,0 +1,47 @@
+/* contrib/btree_gin/btree_gin--1.1--1.2.sql */
+
+-- complain if script is sourced in psql, rather than via CREATE EXTENSION
+\echo Use "ALTER EXTENSION btree_gin UPDATE TO '1.1'" to load this file. \quit
+
+--
+--
+--
+-- enum ops
+--
+--
+
+
+CREATE FUNCTION gin_extract_value_anyenum(anyenum, internal)
+RETURNS internal
+AS 'MODULE_PATHNAME'
+LANGUAGE C STRICT IMMUTABLE;
+
+CREATE FUNCTION gin_compare_prefix_anyenum(anyenum, anyenum, int2, internal)
+RETURNS int4
+AS 'MODULE_PATHNAME'
+LANGUAGE C STRICT IMMUTABLE;
+
+CREATE FUNCTION gin_extract_query_anyenum(anyenum, internal, int2, internal, internal)
+RETURNS internal
+AS 'MODULE_PATHNAME'
+LANGUAGE C STRICT IMMUTABLE;
+
+CREATE FUNCTION gin_enum_cmp(anyenum, anyenum)
+RETURNS int4
+AS 'MODULE_PATHNAME'
+LANGUAGE C STRICT IMMUTABLE;
+
+CREATE OPERATOR CLASS enum_ops
+DEFAULT FOR TYPE anyenum USING gin
+AS
+    OPERATOR        1       <,
+    OPERATOR        2       <=,
+    OPERATOR        3       =,
+    OPERATOR        4       >=,
+    OPERATOR        5       >,
+    FUNCTION        1       gin_enum_cmp(anyenum,anyenum),
+    FUNCTION        2       gin_extract_value_anyenum(anyenum, internal),
+    FUNCTION        3       gin_extract_query_anyenum(anyenum, internal, int2, internal, internal),
+    FUNCTION        4       gin_btree_consistent(internal, int2, anyelement, int4, internal, internal),
+    FUNCTION        5       gin_compare_prefix_anyenum(anyenum,anyenum,int2, internal),
+STORAGE         anyenum;
index 725456e940444602f316a0f5bd68aee977feb264..7191fbf54f7dc8eed034f5be987391320c292ce8 100644 (file)
@@ -25,7 +25,6 @@ typedef struct QueryInfo
    Datum       (*typecmp) (FunctionCallInfo);
 } QueryInfo;
 
-
 /*** GIN support functions shared by all datatypes ***/
 
 static Datum
@@ -112,13 +111,14 @@ gin_btree_compare_prefix(FunctionCallInfo fcinfo)
    int32       res,
                cmp;
 
-   cmp = DatumGetInt32(DirectFunctionCall2Coll(
-                                               data->typecmp,
-                                               PG_GET_COLLATION(),
-                                  (data->strategy == BTLessStrategyNumber ||
-                                data->strategy == BTLessEqualStrategyNumber)
-                                               ? data->datum : a,
-                                               b));
+   cmp = DatumGetInt32(CallerFInfoFunctionCall2(
+                           data->typecmp,
+                           fcinfo->flinfo,
+                           PG_GET_COLLATION(),
+                           (data->strategy == BTLessStrategyNumber ||
+                            data->strategy == BTLessEqualStrategyNumber)
+                           ? data->datum : a,
+                           b));
 
    switch (data->strategy)
    {
@@ -426,3 +426,54 @@ leftmostvalue_numeric(void)
 }
 
 GIN_SUPPORT(numeric, true, leftmostvalue_numeric, gin_numeric_cmp)
+
+/*
+ * Use a similar trick to that used for numeric for enums, since we don't
+ * actually know the leftmost value of any enum without knowing the concrete
+ * type, so we use a dummy leftmost value of InvalidOid.
+ *
+ * Note that we use CallerFInfoFunctionCall2 here so that enum_cmp
+ * gets a valid fn_extra to work with. Unlike most other type comparison
+ * routines it needs it, so we can't use DirectFunctionCall2.
+ */
+
+
+#define ENUM_IS_LEFTMOST(x)    ((x) == InvalidOid)
+
+PG_FUNCTION_INFO_V1(gin_enum_cmp);
+
+Datum
+gin_enum_cmp(PG_FUNCTION_ARGS)
+{
+   Oid     a = PG_GETARG_OID(0);
+   Oid     b = PG_GETARG_OID(1);
+   int     res = 0;
+
+   if (ENUM_IS_LEFTMOST(a))
+   {
+       res = (ENUM_IS_LEFTMOST(b)) ? 0 : -1;
+   }
+   else if (ENUM_IS_LEFTMOST(b))
+   {
+       res = 1;
+   }
+   else
+   {
+       res = DatumGetInt32(CallerFInfoFunctionCall2(
+                               enum_cmp,
+                               fcinfo->flinfo,
+                               PG_GET_COLLATION(),
+                               ObjectIdGetDatum(a),
+                               ObjectIdGetDatum(b)));
+   }
+
+   PG_RETURN_INT32(res);
+}
+
+static Datum
+leftmostvalue_enum(void)
+{
+   return ObjectIdGetDatum(InvalidOid);
+}
+
+GIN_SUPPORT(anyenum, false, leftmostvalue_enum, gin_enum_cmp)
index d96436e8ec422ee453c03014263451319a9200e2..3acc5af1a7d002f4d10b68bed2b1a4b2f119965c 100644 (file)
@@ -1,5 +1,5 @@
 # btree_gin extension
 comment = 'support for indexing common datatypes in GIN'
-default_version = '1.1'
+default_version = '1.2'
 module_pathname = '$libdir/btree_gin'
 relocatable = true
diff --git a/contrib/btree_gin/expected/enum.out b/contrib/btree_gin/expected/enum.out
new file mode 100644 (file)
index 0000000..71e0c4b
--- /dev/null
@@ -0,0 +1,63 @@
+set enable_seqscan=off;
+CREATE TYPE rainbow AS ENUM ('r','o','y','g','b','i','v');
+CREATE TABLE test_enum (
+   i rainbow
+);
+INSERT INTO test_enum VALUES ('v'),('y'),('r'),('g'),('o'),('i'),('b');
+CREATE INDEX idx_enum ON test_enum USING gin (i);
+SELECT * FROM test_enum WHERE i<'g'::rainbow ORDER BY i;
+ i 
+---
+ r
+ o
+ y
+(3 rows)
+
+SELECT * FROM test_enum WHERE i<='g'::rainbow ORDER BY i;
+ i 
+---
+ r
+ o
+ y
+ g
+(4 rows)
+
+SELECT * FROM test_enum WHERE i='g'::rainbow ORDER BY i;
+ i 
+---
+ g
+(1 row)
+
+SELECT * FROM test_enum WHERE i>='g'::rainbow ORDER BY i;
+ i 
+---
+ g
+ b
+ i
+ v
+(4 rows)
+
+SELECT * FROM test_enum WHERE i>'g'::rainbow ORDER BY i;
+ i 
+---
+ b
+ i
+ v
+(3 rows)
+
+explain (costs off) SELECT * FROM test_enum WHERE i>='g'::rainbow ORDER BY i;
+                  QUERY PLAN                   
+-----------------------------------------------
+ Sort
+   Sort Key: i
+   ->  Bitmap Heap Scan on test_enum
+         Recheck Cond: (i >= 'g'::rainbow)
+         ->  Bitmap Index Scan on idx_enum
+               Index Cond: (i >= 'g'::rainbow)
+(6 rows)
+
+-- make sure we handle the non-evenly-numbered oid case for enums
+create type e as enum ('0', '2', '3');
+alter type e add value '1' after '0';
+create table t as select (i % 4)::text::e from generate_series(0, 100000) as i;
+create index on t using gin (e);
diff --git a/contrib/btree_gin/sql/enum.sql b/contrib/btree_gin/sql/enum.sql
new file mode 100644 (file)
index 0000000..ec2a622
--- /dev/null
@@ -0,0 +1,26 @@
+set enable_seqscan=off;
+
+CREATE TYPE rainbow AS ENUM ('r','o','y','g','b','i','v');
+
+CREATE TABLE test_enum (
+   i rainbow
+);
+
+INSERT INTO test_enum VALUES ('v'),('y'),('r'),('g'),('o'),('i'),('b');
+
+CREATE INDEX idx_enum ON test_enum USING gin (i);
+
+SELECT * FROM test_enum WHERE i<'g'::rainbow ORDER BY i;
+SELECT * FROM test_enum WHERE i<='g'::rainbow ORDER BY i;
+SELECT * FROM test_enum WHERE i='g'::rainbow ORDER BY i;
+SELECT * FROM test_enum WHERE i>='g'::rainbow ORDER BY i;
+SELECT * FROM test_enum WHERE i>'g'::rainbow ORDER BY i;
+
+explain (costs off) SELECT * FROM test_enum WHERE i>='g'::rainbow ORDER BY i;
+
+
+-- make sure we handle the non-evenly-numbered oid case for enums
+create type e as enum ('0', '2', '3');
+alter type e add value '1' after '0';
+create table t as select (i % 4)::text::e from generate_series(0, 100000) as i;
+create index on t using gin (e);