Skip to content

Commit

Permalink
PgDatabaseMetaData.getIndexInfo() cast operands to smallint (#2242)
Browse files Browse the repository at this point in the history
It is possible to break method PgDatabaseMetaData.getIndexInfo() by adding certain custom operators.
This PR casts operands in a query so that PgDatabaseMetaData.getIndexInfo() works in presence
of such operator config.

Creating this operator triggers this issue:

CREATE OR REPLACE FUNCTION f6(numeric, integer) returns integer as 'BEGIN return $1::integer & $2;END;' language plpgsql immutable;
CREATE OPERATOR & (LEFTARG = numeric, RIGHTARG = integer, PROCEDURE = f6);

Fixes #2241
  • Loading branch information
jsyrjala committed Sep 13, 2021
1 parent 18939c4 commit 3a2bbd7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
Expand Up @@ -2526,14 +2526,14 @@ public ResultSet getIndexInfo(
+ " trim(both '\"' from pg_catalog.pg_get_indexdef(tmp.CI_OID, tmp.ORDINAL_POSITION, false)) AS COLUMN_NAME, "
+ (connection.haveMinimumServerVersion(ServerVersion.v9_6)
? " CASE tmp.AM_NAME "
+ " WHEN 'btree' THEN CASE tmp.I_INDOPTION[tmp.ORDINAL_POSITION - 1] & 1 "
+ " WHEN 'btree' THEN CASE tmp.I_INDOPTION[tmp.ORDINAL_POSITION - 1] & 1::smallint "
+ " WHEN 1 THEN 'D' "
+ " ELSE 'A' "
+ " END "
+ " ELSE NULL "
+ " END AS ASC_OR_DESC, "
: " CASE tmp.AM_CANORDER "
+ " WHEN true THEN CASE tmp.I_INDOPTION[tmp.ORDINAL_POSITION - 1] & 1 "
+ " WHEN true THEN CASE tmp.I_INDOPTION[tmp.ORDINAL_POSITION - 1] & 1::smallint "
+ " WHEN 1 THEN 'D' "
+ " ELSE 'A' "
+ " END "
Expand Down
Expand Up @@ -120,6 +120,12 @@ public void setUp() throws Exception {
"CREATE OR REPLACE FUNCTION f5() RETURNS TABLE (i int) LANGUAGE sql AS 'SELECT 1'");
}

// create a custom `&` operator, which caused failure with `&` usage in getIndexInfo()
stmt.execute(
"CREATE OR REPLACE FUNCTION f6(numeric, integer) returns integer as 'BEGIN return $1::integer & $2;END;' language plpgsql immutable;");
stmt.execute("DROP OPERATOR IF EXISTS & (numeric, integer)");
stmt.execute("CREATE OPERATOR & (LEFTARG = numeric, RIGHTARG = integer, PROCEDURE = f6)");

TestUtil.createDomain(con, "nndom", "int not null");
TestUtil.createDomain(con, "varbit2", "varbit(3)");
TestUtil.createDomain(con, "float83", "numeric(8,3)");
Expand Down Expand Up @@ -154,6 +160,8 @@ public void tearDown() throws Exception {
stmt.execute("DROP FUNCTION f1(int, varchar)");
stmt.execute("DROP FUNCTION f2(int, varchar)");
stmt.execute("DROP FUNCTION f3(int, varchar)");
stmt.execute("DROP OPERATOR IF EXISTS & (numeric, integer)");
stmt.execute("DROP FUNCTION f6(numeric, integer)");
TestUtil.dropTable(con, "domaintable");
TestUtil.dropDomain(con, "nndom");
TestUtil.dropDomain(con, "varbit2");
Expand Down

0 comments on commit 3a2bbd7

Please sign in to comment.