MCPcopy Create free account
hub / github.com/apache/cloudberry / transformInsertRow

Function transformInsertRow

src/backend/parser/analyze.c:1084–1177  ·  view source on GitHub ↗

* Prepare an INSERT row for assignment to the target table. * * exprlist: transformed expressions for source values; these might come from * a VALUES row, or be Vars referencing a sub-SELECT or VALUES RTE output. * stmtcols: original target-columns spec for INSERT (we just test for NIL) * icolumns: effective target-columns spec (list of ResTarget) * attrnos: integer column numbers (must be s

Source from the content-addressed store, hash-verified

1082 * strip_indirection: if true, remove any field/array assignment nodes
1083 */
1084static List *
1085transformInsertRow(ParseState *pstate, List *exprlist,
1086 List *stmtcols, List *icolumns, List *attrnos,
1087 bool strip_indirection)
1088{
1089 List *result;
1090 ListCell *lc;
1091 ListCell *icols;
1092 ListCell *attnos;
1093
1094 /*
1095 * Check length of expr list. It must not have more expressions than
1096 * there are target columns. We allow fewer, but only if no explicit
1097 * columns list was given (the remaining columns are implicitly
1098 * defaulted). Note we must check this *after* transformation because
1099 * that could expand '*' into multiple items.
1100 */
1101 if (list_length(exprlist) > list_length(icolumns))
1102 ereport(ERROR,
1103 (errcode(ERRCODE_SYNTAX_ERROR),
1104 errmsg("INSERT has more expressions than target columns"),
1105 parser_errposition(pstate,
1106 exprLocation(list_nth(exprlist,
1107 list_length(icolumns))))));
1108 if (stmtcols != NIL &&
1109 list_length(exprlist) < list_length(icolumns))
1110 {
1111 /*
1112 * We can get here for cases like INSERT ... SELECT (a,b,c) FROM ...
1113 * where the user accidentally created a RowExpr instead of separate
1114 * columns. Add a suitable hint if that seems to be the problem,
1115 * because the main error message is quite misleading for this case.
1116 * (If there's no stmtcols, you'll get something about data type
1117 * mismatch, which is less misleading so we don't worry about giving a
1118 * hint in that case.)
1119 */
1120 ereport(ERROR,
1121 (errcode(ERRCODE_SYNTAX_ERROR),
1122 errmsg("INSERT has more target columns than expressions"),
1123 ((list_length(exprlist) == 1 &&
1124 count_rowexpr_columns(pstate, linitial(exprlist)) ==
1125 list_length(icolumns)) ?
1126 errhint("The insertion source is a row expression containing the same number of columns expected by the INSERT. Did you accidentally use extra parentheses?") : 0),
1127 parser_errposition(pstate,
1128 exprLocation(list_nth(icolumns,
1129 list_length(exprlist))))));
1130 }
1131
1132 /*
1133 * Prepare columns for assignment to target table.
1134 */
1135 result = NIL;
1136 forthree(lc, exprlist, icols, icolumns, attnos, attrnos)
1137 {
1138 Expr *expr = (Expr *) lfirst(lc);
1139 ResTarget *col = lfirst_node(ResTarget, icols);
1140 int attno = lfirst_int(attnos);
1141

Callers 1

transformInsertStmtFunction · 0.85

Calls 10

list_lengthFunction · 0.85
parser_errpositionFunction · 0.85
exprLocationFunction · 0.85
list_nthFunction · 0.85
count_rowexpr_columnsFunction · 0.85
transformAssignedExprFunction · 0.85
lappendFunction · 0.85
errcodeFunction · 0.50
errmsgFunction · 0.50
errhintFunction · 0.50

Tested by

no test coverage detected