Skip to content

Commit 49e23fb

Browse files
authored
Index created for UNIQUE table/column constraint in pltsql should be NULLS FIRST (#347)
Pltsql always defaults to NULLS FIRST ordering when ASC for index. One case which was not covered yet was UNIQUE constraint in CREATE TABLE command, which still creates an index with SORTBY_NULLS_DEFAULT. (PG default is NULLS LAST in ASC) This UNIQUE constraint could be a column constraint or table constraint and PG does not support NULLS ORDERING for UNIQUE constraint in CREATE TABLE. CREATE TABLE name (col1 type UNIQUE) -- column constraint or CREATE TABLE name (col1 type1, col2 type2, UNIQUE (col1)) -- table constraint To fix this we change the NULLS ORDERING for unique index created from table/column constraints. The hook is placed in transformIndexConstraint which is responsible for converting the constraint node to create index node for create stmts. We modify the NULLS ORDERING value here. Also only index which have amcanorder set to true can accept ordering. Right now only b-tree are supported as unique index and b-tree does support ordering. Issues Resolved [BABEL-3571] Signed-off-by: Tanzeel Khan <[email protected]>
1 parent 2f30a5a commit 49e23fb

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/backend/parser/parse_utilcmd.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070

7171
/* Hook for pltsql plugin */
7272
pltsql_identity_datatype_hook_type pltsql_identity_datatype_hook = NULL;
73+
pltsql_unique_constraint_nulls_ordering_hook_type pltsql_unique_constraint_nulls_ordering_hook = NULL;
7374
post_transform_column_definition_hook_type post_transform_column_definition_hook = NULL;
7475
post_transform_table_definition_hook_type post_transform_table_definition_hook = NULL;
7576

@@ -2575,7 +2576,12 @@ transformIndexConstraint(Constraint *constraint, CreateStmtContext *cxt)
25752576
IndexElem * i = (IndexElem *) lfirst(lc);
25762577
iparam->ordering = i->ordering;
25772578
}
2578-
2579+
2580+
if (sql_dialect == SQL_DIALECT_TSQL && pltsql_unique_constraint_nulls_ordering_hook)
2581+
{
2582+
iparam->nulls_ordering = (* pltsql_unique_constraint_nulls_ordering_hook) (constraint->contype, iparam->ordering);
2583+
}
2584+
25792585
/*
25802586
* For a primary-key column, also create an item for ALTER TABLE
25812587
* SET NOT NULL if we couldn't ensure it via is_not_null above.

src/include/parser/parse_utilcmd.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ struct AttrMap; /* avoid including attmap.h here */
2222
/* IDENTITY datatype hook */
2323
typedef void (*pltsql_identity_datatype_hook_type) (ParseState *pstate,
2424
ColumnDef *column);
25-
extern PGDLLEXPORT pltsql_identity_datatype_hook_type pltsql_identity_datatype_hook;
25+
typedef SortByNulls (*pltsql_unique_constraint_nulls_ordering_hook_type) (ConstrType constraint_type,
26+
SortByDir ordering);
27+
extern PGDLLIMPORT pltsql_identity_datatype_hook_type pltsql_identity_datatype_hook;
28+
extern PGDLLIMPORT pltsql_unique_constraint_nulls_ordering_hook_type pltsql_unique_constraint_nulls_ordering_hook;
2629
typedef void (*post_transform_column_definition_hook_type) (ParseState *pstate, RangeVar* relation, ColumnDef *column, List **alist);
2730
typedef void (*post_transform_table_definition_hook_type) (ParseState *pstate, RangeVar* relation, char *relname, List **alist);
2831
extern PGDLLEXPORT post_transform_column_definition_hook_type post_transform_column_definition_hook;

0 commit comments

Comments
 (0)