1 /*-------------------------------------------------------------------------
2  *
3  * qualify_statistics_stmt.c
4  *	  Functions specialized in fully qualifying all statistics statements.
5  *    These functions are dispatched from qualify.c
6  *
7  *	  Goal would be that the deparser functions for these statements can
8  *	  serialize the statement without any external lookups.
9  *
10  * Copyright (c) Citus Data, Inc.
11  *
12  *-------------------------------------------------------------------------
13  */
14 
15 #include "postgres.h"
16 
17 #include "catalog/namespace.h"
18 #include "distributed/commands.h"
19 #include "distributed/deparser.h"
20 #include "distributed/listutils.h"
21 #include "nodes/parsenodes.h"
22 #include "nodes/value.h"
23 #include "utils/lsyscache.h"
24 #include "utils/rel.h"
25 #include "utils/relcache.h"
26 
27 void
QualifyCreateStatisticsStmt(Node * node)28 QualifyCreateStatisticsStmt(Node *node)
29 {
30 	CreateStatsStmt *stmt = castNode(CreateStatsStmt, node);
31 
32 	RangeVar *relation = (RangeVar *) linitial(stmt->relations);
33 
34 	if (relation->schemaname == NULL)
35 	{
36 		Oid tableOid = RelnameGetRelid(relation->relname);
37 		Oid schemaOid = get_rel_namespace(tableOid);
38 		relation->schemaname = get_namespace_name(schemaOid);
39 	}
40 
41 	RangeVar *stat = makeRangeVarFromNameList(stmt->defnames);
42 
43 	if (stat->schemaname == NULL)
44 	{
45 		Oid schemaOid = RangeVarGetCreationNamespace(stat);
46 		stat->schemaname = get_namespace_name(schemaOid);
47 
48 		stmt->defnames = MakeNameListFromRangeVar(stat);
49 	}
50 }
51 
52 
53 /*
54  * QualifyDropStatisticsStmt qualifies DropStmt's with schema name for
55  * DROP STATISTICS statements.
56  */
57 void
QualifyDropStatisticsStmt(Node * node)58 QualifyDropStatisticsStmt(Node *node)
59 {
60 	DropStmt *dropStatisticsStmt = castNode(DropStmt, node);
61 	Assert(dropStatisticsStmt->removeType == OBJECT_STATISTIC_EXT);
62 
63 	List *objectNameListWithSchema = NIL;
64 	List *objectNameList = NULL;
65 	foreach_ptr(objectNameList, dropStatisticsStmt->objects)
66 	{
67 		RangeVar *stat = makeRangeVarFromNameList(objectNameList);
68 
69 		if (stat->schemaname == NULL)
70 		{
71 			Oid schemaOid = RangeVarGetCreationNamespace(stat);
72 			stat->schemaname = get_namespace_name(schemaOid);
73 		}
74 
75 		objectNameListWithSchema = lappend(objectNameListWithSchema,
76 										   MakeNameListFromRangeVar(stat));
77 	}
78 
79 	dropStatisticsStmt->objects = objectNameListWithSchema;
80 }
81 
82 
83 /*
84  * QualifyAlterStatisticsRenameStmt qualifies RenameStmt's with schema name for
85  * ALTER STATISTICS RENAME statements.
86  */
87 void
QualifyAlterStatisticsRenameStmt(Node * node)88 QualifyAlterStatisticsRenameStmt(Node *node)
89 {
90 	RenameStmt *renameStmt = castNode(RenameStmt, node);
91 	Assert(renameStmt->renameType == OBJECT_STATISTIC_EXT);
92 
93 	List *nameList = (List *) renameStmt->object;
94 	if (list_length(nameList) == 1)
95 	{
96 		RangeVar *stat = makeRangeVarFromNameList(nameList);
97 		Oid schemaOid = RangeVarGetCreationNamespace(stat);
98 		stat->schemaname = get_namespace_name(schemaOid);
99 		renameStmt->object = (Node *) MakeNameListFromRangeVar(stat);
100 	}
101 }
102 
103 
104 /*
105  * QualifyAlterStatisticsSchemaStmt qualifies AlterObjectSchemaStmt's with schema name for
106  * ALTER STATISTICS RENAME statements.
107  */
108 void
QualifyAlterStatisticsSchemaStmt(Node * node)109 QualifyAlterStatisticsSchemaStmt(Node *node)
110 {
111 	AlterObjectSchemaStmt *stmt = castNode(AlterObjectSchemaStmt, node);
112 	Assert(stmt->objectType == OBJECT_STATISTIC_EXT);
113 
114 	List *nameList = (List *) stmt->object;
115 	if (list_length(nameList) == 1)
116 	{
117 		RangeVar *stat = makeRangeVarFromNameList(nameList);
118 		Oid schemaOid = RangeVarGetCreationNamespace(stat);
119 		stat->schemaname = get_namespace_name(schemaOid);
120 		stmt->object = (Node *) MakeNameListFromRangeVar(stat);
121 	}
122 }
123 
124 
125 #if PG_VERSION_NUM >= PG_VERSION_13
126 
127 /*
128  * QualifyAlterStatisticsStmt qualifies AlterStatsStmt's with schema name for
129  * ALTER STATISTICS .. SET STATISTICS statements.
130  */
131 void
QualifyAlterStatisticsStmt(Node * node)132 QualifyAlterStatisticsStmt(Node *node)
133 {
134 	AlterStatsStmt *stmt = castNode(AlterStatsStmt, node);
135 
136 	if (list_length(stmt->defnames) == 1)
137 	{
138 		RangeVar *stat = makeRangeVarFromNameList(stmt->defnames);
139 		Oid schemaOid = RangeVarGetCreationNamespace(stat);
140 		stat->schemaname = get_namespace_name(schemaOid);
141 		stmt->defnames = MakeNameListFromRangeVar(stat);
142 	}
143 }
144 
145 
146 #endif
147 
148 /*
149  * QualifyAlterStatisticsStmt qualifies AlterOwnerStmt's with schema name for
150  * ALTER STATISTICS .. OWNER TO statements.
151  */
152 void
QualifyAlterStatisticsOwnerStmt(Node * node)153 QualifyAlterStatisticsOwnerStmt(Node *node)
154 {
155 	AlterOwnerStmt *stmt = castNode(AlterOwnerStmt, node);
156 	Assert(stmt->objectType == OBJECT_STATISTIC_EXT);
157 
158 	List *nameList = (List *) stmt->object;
159 	if (list_length(nameList) == 1)
160 	{
161 		RangeVar *stat = makeRangeVarFromNameList(nameList);
162 		Oid schemaOid = RangeVarGetCreationNamespace(stat);
163 		stat->schemaname = get_namespace_name(schemaOid);
164 		stmt->object = (Node *) MakeNameListFromRangeVar(stat);
165 	}
166 }
167