/third_party/libpg_query/grammar/statements/alter_table.y

https://github.com/cwida/duckdb · Happy · 458 lines · 435 code · 23 blank · 0 comment · 0 complexity · 01768ef5e12e7d719de39a02b3bb8eb1 MD5 · raw file

  1. /*****************************************************************************
  2. *
  3. * ALTER [ TABLE | INDEX | SEQUENCE | VIEW | MATERIALIZED VIEW ] variations
  4. *
  5. * Note: we accept all subcommands for each of the five variants, and sort
  6. * out what's really legal at execution time.
  7. *****************************************************************************/
  8. AlterTableStmt:
  9. ALTER TABLE relation_expr alter_table_cmds
  10. {
  11. PGAlterTableStmt *n = makeNode(PGAlterTableStmt);
  12. n->relation = $3;
  13. n->cmds = $4;
  14. n->relkind = PG_OBJECT_TABLE;
  15. n->missing_ok = false;
  16. $$ = (PGNode *)n;
  17. }
  18. | ALTER TABLE IF_P EXISTS relation_expr alter_table_cmds
  19. {
  20. PGAlterTableStmt *n = makeNode(PGAlterTableStmt);
  21. n->relation = $5;
  22. n->cmds = $6;
  23. n->relkind = PG_OBJECT_TABLE;
  24. n->missing_ok = true;
  25. $$ = (PGNode *)n;
  26. }
  27. | ALTER INDEX qualified_name alter_table_cmds
  28. {
  29. PGAlterTableStmt *n = makeNode(PGAlterTableStmt);
  30. n->relation = $3;
  31. n->cmds = $4;
  32. n->relkind = PG_OBJECT_INDEX;
  33. n->missing_ok = false;
  34. $$ = (PGNode *)n;
  35. }
  36. | ALTER INDEX IF_P EXISTS qualified_name alter_table_cmds
  37. {
  38. PGAlterTableStmt *n = makeNode(PGAlterTableStmt);
  39. n->relation = $5;
  40. n->cmds = $6;
  41. n->relkind = PG_OBJECT_INDEX;
  42. n->missing_ok = true;
  43. $$ = (PGNode *)n;
  44. }
  45. | ALTER SEQUENCE qualified_name alter_table_cmds
  46. {
  47. PGAlterTableStmt *n = makeNode(PGAlterTableStmt);
  48. n->relation = $3;
  49. n->cmds = $4;
  50. n->relkind = PG_OBJECT_SEQUENCE;
  51. n->missing_ok = false;
  52. $$ = (PGNode *)n;
  53. }
  54. | ALTER SEQUENCE IF_P EXISTS qualified_name alter_table_cmds
  55. {
  56. PGAlterTableStmt *n = makeNode(PGAlterTableStmt);
  57. n->relation = $5;
  58. n->cmds = $6;
  59. n->relkind = PG_OBJECT_SEQUENCE;
  60. n->missing_ok = true;
  61. $$ = (PGNode *)n;
  62. }
  63. | ALTER VIEW qualified_name alter_table_cmds
  64. {
  65. PGAlterTableStmt *n = makeNode(PGAlterTableStmt);
  66. n->relation = $3;
  67. n->cmds = $4;
  68. n->relkind = PG_OBJECT_VIEW;
  69. n->missing_ok = false;
  70. $$ = (PGNode *)n;
  71. }
  72. | ALTER VIEW IF_P EXISTS qualified_name alter_table_cmds
  73. {
  74. PGAlterTableStmt *n = makeNode(PGAlterTableStmt);
  75. n->relation = $5;
  76. n->cmds = $6;
  77. n->relkind = PG_OBJECT_VIEW;
  78. n->missing_ok = true;
  79. $$ = (PGNode *)n;
  80. }
  81. ;
  82. alter_identity_column_option_list:
  83. alter_identity_column_option
  84. { $$ = list_make1($1); }
  85. | alter_identity_column_option_list alter_identity_column_option
  86. { $$ = lappend($1, $2); }
  87. ;
  88. alter_column_default:
  89. SET DEFAULT a_expr { $$ = $3; }
  90. | DROP DEFAULT { $$ = NULL; }
  91. ;
  92. alter_identity_column_option:
  93. RESTART
  94. {
  95. $$ = makeDefElem("restart", NULL, @1);
  96. }
  97. | RESTART opt_with NumericOnly
  98. {
  99. $$ = makeDefElem("restart", (PGNode *)$3, @1);
  100. }
  101. | SET SeqOptElem
  102. {
  103. if (strcmp($2->defname, "as") == 0 ||
  104. strcmp($2->defname, "restart") == 0 ||
  105. strcmp($2->defname, "owned_by") == 0)
  106. ereport(ERROR,
  107. (errcode(PG_ERRCODE_SYNTAX_ERROR),
  108. errmsg("sequence option \"%s\" not supported here", $2->defname),
  109. parser_errposition(@2)));
  110. $$ = $2;
  111. }
  112. | SET GENERATED generated_when
  113. {
  114. $$ = makeDefElem("generated", (PGNode *) makeInteger($3), @1);
  115. }
  116. ;
  117. alter_generic_option_list:
  118. alter_generic_option_elem
  119. {
  120. $$ = list_make1($1);
  121. }
  122. | alter_generic_option_list ',' alter_generic_option_elem
  123. {
  124. $$ = lappend($1, $3);
  125. }
  126. ;
  127. alter_table_cmd:
  128. /* ALTER TABLE <name> ADD <coldef> */
  129. ADD_P columnDef
  130. {
  131. PGAlterTableCmd *n = makeNode(PGAlterTableCmd);
  132. n->subtype = PG_AT_AddColumn;
  133. n->def = $2;
  134. n->missing_ok = false;
  135. $$ = (PGNode *)n;
  136. }
  137. /* ALTER TABLE <name> ADD IF NOT EXISTS <coldef> */
  138. | ADD_P IF_P NOT EXISTS columnDef
  139. {
  140. PGAlterTableCmd *n = makeNode(PGAlterTableCmd);
  141. n->subtype = PG_AT_AddColumn;
  142. n->def = $5;
  143. n->missing_ok = true;
  144. $$ = (PGNode *)n;
  145. }
  146. /* ALTER TABLE <name> ADD COLUMN <coldef> */
  147. | ADD_P COLUMN columnDef
  148. {
  149. PGAlterTableCmd *n = makeNode(PGAlterTableCmd);
  150. n->subtype = PG_AT_AddColumn;
  151. n->def = $3;
  152. n->missing_ok = false;
  153. $$ = (PGNode *)n;
  154. }
  155. /* ALTER TABLE <name> ADD COLUMN IF NOT EXISTS <coldef> */
  156. | ADD_P COLUMN IF_P NOT EXISTS columnDef
  157. {
  158. PGAlterTableCmd *n = makeNode(PGAlterTableCmd);
  159. n->subtype = PG_AT_AddColumn;
  160. n->def = $6;
  161. n->missing_ok = true;
  162. $$ = (PGNode *)n;
  163. }
  164. /* ALTER TABLE <name> ALTER [COLUMN] <colname> {SET DEFAULT <expr>|DROP DEFAULT} */
  165. | ALTER opt_column ColId alter_column_default
  166. {
  167. PGAlterTableCmd *n = makeNode(PGAlterTableCmd);
  168. n->subtype = PG_AT_ColumnDefault;
  169. n->name = $3;
  170. n->def = $4;
  171. $$ = (PGNode *)n;
  172. }
  173. /* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP NOT NULL */
  174. | ALTER opt_column ColId DROP NOT NULL_P
  175. {
  176. PGAlterTableCmd *n = makeNode(PGAlterTableCmd);
  177. n->subtype = PG_AT_DropNotNull;
  178. n->name = $3;
  179. $$ = (PGNode *)n;
  180. }
  181. /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET NOT NULL */
  182. | ALTER opt_column ColId SET NOT NULL_P
  183. {
  184. PGAlterTableCmd *n = makeNode(PGAlterTableCmd);
  185. n->subtype = PG_AT_SetNotNull;
  186. n->name = $3;
  187. $$ = (PGNode *)n;
  188. }
  189. /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STATISTICS <SignedIconst> */
  190. | ALTER opt_column ColId SET STATISTICS SignedIconst
  191. {
  192. PGAlterTableCmd *n = makeNode(PGAlterTableCmd);
  193. n->subtype = PG_AT_SetStatistics;
  194. n->name = $3;
  195. n->def = (PGNode *) makeInteger($6);
  196. $$ = (PGNode *)n;
  197. }
  198. /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET ( column_parameter = value [, ... ] ) */
  199. | ALTER opt_column ColId SET reloptions
  200. {
  201. PGAlterTableCmd *n = makeNode(PGAlterTableCmd);
  202. n->subtype = PG_AT_SetOptions;
  203. n->name = $3;
  204. n->def = (PGNode *) $5;
  205. $$ = (PGNode *)n;
  206. }
  207. /* ALTER TABLE <name> ALTER [COLUMN] <colname> RESET ( column_parameter = value [, ... ] ) */
  208. | ALTER opt_column ColId RESET reloptions
  209. {
  210. PGAlterTableCmd *n = makeNode(PGAlterTableCmd);
  211. n->subtype = PG_AT_ResetOptions;
  212. n->name = $3;
  213. n->def = (PGNode *) $5;
  214. $$ = (PGNode *)n;
  215. }
  216. /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STORAGE <storagemode> */
  217. | ALTER opt_column ColId SET STORAGE ColId
  218. {
  219. PGAlterTableCmd *n = makeNode(PGAlterTableCmd);
  220. n->subtype = PG_AT_SetStorage;
  221. n->name = $3;
  222. n->def = (PGNode *) makeString($6);
  223. $$ = (PGNode *)n;
  224. }
  225. /* ALTER TABLE <name> ALTER [COLUMN] <colname> ADD GENERATED ... AS IDENTITY ... */
  226. | ALTER opt_column ColId ADD_P GENERATED generated_when AS IDENTITY_P OptParenthesizedSeqOptList
  227. {
  228. PGAlterTableCmd *n = makeNode(PGAlterTableCmd);
  229. PGConstraint *c = makeNode(PGConstraint);
  230. c->contype = PG_CONSTR_IDENTITY;
  231. c->generated_when = $6;
  232. c->options = $9;
  233. c->location = @5;
  234. n->subtype = PG_AT_AddIdentity;
  235. n->name = $3;
  236. n->def = (PGNode *) c;
  237. $$ = (PGNode *)n;
  238. }
  239. /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET <sequence options>/RESET */
  240. | ALTER opt_column ColId alter_identity_column_option_list
  241. {
  242. PGAlterTableCmd *n = makeNode(PGAlterTableCmd);
  243. n->subtype = PG_AT_SetIdentity;
  244. n->name = $3;
  245. n->def = (PGNode *) $4;
  246. $$ = (PGNode *)n;
  247. }
  248. /* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP IDENTITY */
  249. | ALTER opt_column ColId DROP IDENTITY_P
  250. {
  251. PGAlterTableCmd *n = makeNode(PGAlterTableCmd);
  252. n->subtype = AT_DropIdentity;
  253. n->name = $3;
  254. n->missing_ok = false;
  255. $$ = (PGNode *)n;
  256. }
  257. /* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP IDENTITY IF EXISTS */
  258. | ALTER opt_column ColId DROP IDENTITY_P IF_P EXISTS
  259. {
  260. PGAlterTableCmd *n = makeNode(PGAlterTableCmd);
  261. n->subtype = AT_DropIdentity;
  262. n->name = $3;
  263. n->missing_ok = true;
  264. $$ = (PGNode *)n;
  265. }
  266. /* ALTER TABLE <name> DROP [COLUMN] IF EXISTS <colname> [RESTRICT|CASCADE] */
  267. | DROP opt_column IF_P EXISTS ColId opt_drop_behavior
  268. {
  269. PGAlterTableCmd *n = makeNode(PGAlterTableCmd);
  270. n->subtype = PG_AT_DropColumn;
  271. n->name = $5;
  272. n->behavior = $6;
  273. n->missing_ok = true;
  274. $$ = (PGNode *)n;
  275. }
  276. /* ALTER TABLE <name> DROP [COLUMN] <colname> [RESTRICT|CASCADE] */
  277. | DROP opt_column ColId opt_drop_behavior
  278. {
  279. PGAlterTableCmd *n = makeNode(PGAlterTableCmd);
  280. n->subtype = PG_AT_DropColumn;
  281. n->name = $3;
  282. n->behavior = $4;
  283. n->missing_ok = false;
  284. $$ = (PGNode *)n;
  285. }
  286. /*
  287. * ALTER TABLE <name> ALTER [COLUMN] <colname> [SET DATA] TYPE <typename>
  288. * [ USING <expression> ]
  289. */
  290. | ALTER opt_column ColId opt_set_data TYPE_P Typename opt_collate_clause alter_using
  291. {
  292. PGAlterTableCmd *n = makeNode(PGAlterTableCmd);
  293. PGColumnDef *def = makeNode(PGColumnDef);
  294. n->subtype = PG_AT_AlterColumnType;
  295. n->name = $3;
  296. n->def = (PGNode *) def;
  297. /* We only use these fields of the PGColumnDef node */
  298. def->typeName = $6;
  299. def->collClause = (PGCollateClause *) $7;
  300. def->raw_default = $8;
  301. def->location = @3;
  302. $$ = (PGNode *)n;
  303. }
  304. /* ALTER FOREIGN TABLE <name> ALTER [COLUMN] <colname> OPTIONS */
  305. | ALTER opt_column ColId alter_generic_options
  306. {
  307. PGAlterTableCmd *n = makeNode(PGAlterTableCmd);
  308. n->subtype = PG_AT_AlterColumnGenericOptions;
  309. n->name = $3;
  310. n->def = (PGNode *) $4;
  311. $$ = (PGNode *)n;
  312. }
  313. /* ALTER TABLE <name> ADD CONSTRAINT ... */
  314. | ADD_P TableConstraint
  315. {
  316. PGAlterTableCmd *n = makeNode(PGAlterTableCmd);
  317. n->subtype = PG_AT_AddConstraint;
  318. n->def = $2;
  319. $$ = (PGNode *)n;
  320. }
  321. /* ALTER TABLE <name> ALTER CONSTRAINT ... */
  322. | ALTER CONSTRAINT name ConstraintAttributeSpec
  323. {
  324. PGAlterTableCmd *n = makeNode(PGAlterTableCmd);
  325. PGConstraint *c = makeNode(PGConstraint);
  326. n->subtype = PG_AT_AlterConstraint;
  327. n->def = (PGNode *) c;
  328. c->contype = PG_CONSTR_FOREIGN; /* others not supported, yet */
  329. c->conname = $3;
  330. processCASbits($4, @4, "ALTER CONSTRAINT statement",
  331. &c->deferrable,
  332. &c->initdeferred,
  333. NULL, NULL, yyscanner);
  334. $$ = (PGNode *)n;
  335. }
  336. /* ALTER TABLE <name> VALIDATE CONSTRAINT ... */
  337. | VALIDATE CONSTRAINT name
  338. {
  339. PGAlterTableCmd *n = makeNode(PGAlterTableCmd);
  340. n->subtype = PG_AT_ValidateConstraint;
  341. n->name = $3;
  342. $$ = (PGNode *)n;
  343. }
  344. /* ALTER TABLE <name> DROP CONSTRAINT IF EXISTS <name> [RESTRICT|CASCADE] */
  345. | DROP CONSTRAINT IF_P EXISTS name opt_drop_behavior
  346. {
  347. PGAlterTableCmd *n = makeNode(PGAlterTableCmd);
  348. n->subtype = PG_AT_DropConstraint;
  349. n->name = $5;
  350. n->behavior = $6;
  351. n->missing_ok = true;
  352. $$ = (PGNode *)n;
  353. }
  354. /* ALTER TABLE <name> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
  355. | DROP CONSTRAINT name opt_drop_behavior
  356. {
  357. PGAlterTableCmd *n = makeNode(PGAlterTableCmd);
  358. n->subtype = PG_AT_DropConstraint;
  359. n->name = $3;
  360. n->behavior = $4;
  361. n->missing_ok = false;
  362. $$ = (PGNode *)n;
  363. }
  364. /* ALTER TABLE <name> SET LOGGED */
  365. | SET LOGGED
  366. {
  367. PGAlterTableCmd *n = makeNode(PGAlterTableCmd);
  368. n->subtype = PG_AT_SetLogged;
  369. $$ = (PGNode *)n;
  370. }
  371. /* ALTER TABLE <name> SET UNLOGGED */
  372. | SET UNLOGGED
  373. {
  374. PGAlterTableCmd *n = makeNode(PGAlterTableCmd);
  375. n->subtype = PG_AT_SetUnLogged;
  376. $$ = (PGNode *)n;
  377. }
  378. /* ALTER TABLE <name> SET (...) */
  379. | SET reloptions
  380. {
  381. PGAlterTableCmd *n = makeNode(PGAlterTableCmd);
  382. n->subtype = PG_AT_SetRelOptions;
  383. n->def = (PGNode *)$2;
  384. $$ = (PGNode *)n;
  385. }
  386. /* ALTER TABLE <name> RESET (...) */
  387. | RESET reloptions
  388. {
  389. PGAlterTableCmd *n = makeNode(PGAlterTableCmd);
  390. n->subtype = PG_AT_ResetRelOptions;
  391. n->def = (PGNode *)$2;
  392. $$ = (PGNode *)n;
  393. }
  394. | alter_generic_options
  395. {
  396. PGAlterTableCmd *n = makeNode(PGAlterTableCmd);
  397. n->subtype = PG_AT_GenericOptions;
  398. n->def = (PGNode *)$1;
  399. $$ = (PGNode *) n;
  400. }
  401. ;
  402. alter_using:
  403. USING a_expr { $$ = $2; }
  404. | /* EMPTY */ { $$ = NULL; }
  405. ;
  406. alter_generic_option_elem:
  407. generic_option_elem
  408. {
  409. $$ = $1;
  410. }
  411. | SET generic_option_elem
  412. {
  413. $$ = $2;
  414. $$->defaction = PG_DEFELEM_SET;
  415. }
  416. | ADD_P generic_option_elem
  417. {
  418. $$ = $2;
  419. $$->defaction = PG_DEFELEM_ADD;
  420. }
  421. | DROP generic_option_name
  422. {
  423. $$ = makeDefElemExtended(NULL, $2, NULL, DEFELEM_DROP, @2);
  424. }
  425. ;
  426. alter_table_cmds:
  427. alter_table_cmd { $$ = list_make1($1); }
  428. | alter_table_cmds ',' alter_table_cmd { $$ = lappend($1, $3); }
  429. ;
  430. alter_generic_options:
  431. OPTIONS '(' alter_generic_option_list ')' { $$ = $3; }
  432. ;
  433. opt_set_data: SET DATA_P { $$ = 1; }
  434. | /*EMPTY*/ { $$ = 0; }
  435. ;