Datum texteq(PG_FUNCTION_ARGS) { Datum arg1 = PG_GETARG_DATUM(0); Datum arg2 = PG_GETARG_DATUM(1); bool result; Size len1, len2; /* * Since we only care about equality or not-equality, we can avoid all the * expense of strcoll() here, and just do bitwise comparison. In fact, we * don't even have to do a bitwise comparison if we can show the lengths * of the strings are unequal; which might save us from having to detoast * one or both values. */ len1 = toast_raw_datum_size(arg1); len2 = toast_raw_datum_size(arg2); if (len1 != len2) result = false; else { text *targ1 = DatumGetTextPP(arg1); text *targ2 = DatumGetTextPP(arg2); result = (memcmp(VARDATA_ANY(targ1), VARDATA_ANY(targ2), len1 - VARHDRSZ) == 0); PG_FREE_IF_COPY(targ1, 0); PG_FREE_IF_COPY(targ2, 1); } PG_RETURN_BOOL(result); }
Datum textne(PG_FUNCTION_ARGS) { Datum arg1 = PG_GETARG_DATUM(0); Datum arg2 = PG_GETARG_DATUM(1); bool result; Size len1, len2; /* See comment in texteq() */ len1 = toast_raw_datum_size(arg1); len2 = toast_raw_datum_size(arg2); if (len1 != len2) result = true; else { text *targ1 = DatumGetTextPP(arg1); text *targ2 = DatumGetTextPP(arg2); result = (memcmp(VARDATA_ANY(targ1), VARDATA_ANY(targ2), len1 - VARHDRSZ) != 0); PG_FREE_IF_COPY(targ1, 0); PG_FREE_IF_COPY(targ2, 1); } PG_RETURN_BOOL(result); }
/* * len < 0 means "length is not specified". */ static text * ora_substr(Datum str, int start, int len) { if (start == 0) start = 1; /* 0 is interpreted as 1 */ else if (start < 0) { text *t; int32 n; t = DatumGetTextPP(str); n = pg_mbstrlen_with_len(VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); start = n + start + 1; if (start <= 0) return cstring_to_text(""); str = PointerGetDatum(t); /* save detoasted text */ } if (len < 0) return DatumGetTextP(DirectFunctionCall2(text_substr_no_len, str, Int32GetDatum(start))); else return DatumGetTextP(DirectFunctionCall3(text_substr, str, Int32GetDatum(start), Int32GetDatum(len))); }
Datum create_empty_extension(PG_FUNCTION_ARGS) { text *extName = PG_GETARG_TEXT_PP(0); text *schemaName = PG_GETARG_TEXT_PP(1); bool relocatable = PG_GETARG_BOOL(2); text *extVersion = PG_GETARG_TEXT_PP(3); Datum extConfig; Datum extCondition; List *requiredExtensions; if (PG_ARGISNULL(4)) extConfig = PointerGetDatum(NULL); else extConfig = PG_GETARG_DATUM(4); if (PG_ARGISNULL(5)) extCondition = PointerGetDatum(NULL); else extCondition = PG_GETARG_DATUM(5); requiredExtensions = NIL; if (!PG_ARGISNULL(6)) { ArrayType *textArray = PG_GETARG_ARRAYTYPE_P(6); Datum *textDatums; int ndatums; int i; deconstruct_array(textArray, TEXTOID, -1, false, 'i', &textDatums, NULL, &ndatums); for (i = 0; i < ndatums; i++) { text *txtname = DatumGetTextPP(textDatums[i]); char *extName = text_to_cstring(txtname); Oid extOid = get_extension_oid(extName, false); requiredExtensions = lappend_oid(requiredExtensions, extOid); } } InsertExtensionTuple(text_to_cstring(extName), GetUserId(), get_namespace_oid(text_to_cstring(schemaName), false), relocatable, text_to_cstring(extVersion), extConfig, extCondition, requiredExtensions); PG_RETURN_VOID(); }
Datum fillCommentWithLen(PG_FUNCTION_ARGS) { if ( PG_NARGS() < 2 || PG_ARGISNULL(0) || PG_ARGISNULL(1)) { elog(WARNING, "fillCommentWithLen has incorrect args"); PG_RETURN_NULL(); } Datum d = PG_GETARG_DATUM(0); text *comment = DatumGetTextPP(d); int oldCommentLen = VARSIZE_ANY_EXHDR(comment); char *oldCommentStr = VARDATA_ANY(comment); int newCommentLen = PG_GETARG_INT32(1); Comment *newComment = (Comment *) palloc(newCommentLen + VARHDRSZ); int fillTimes; int fileTail; if (oldCommentLen == 0) { fillTimes = 0; fileTail = newCommentLen; } else { fillTimes = newCommentLen / oldCommentLen; fileTail = newCommentLen % oldCommentLen; } int fillCursor = 0; int i = 0; for (i = 0; i < fillTimes; i++) { memcpy(VARDATA(newComment) + fillCursor, oldCommentStr, oldCommentLen); fillCursor += oldCommentLen; } memcpy(VARDATA(newComment) + fillCursor, oldCommentStr, fileTail); SET_VARSIZE(newComment, newCommentLen + VARHDRSZ); PG_RETURN_COMMENT_P(newComment); }
Datum binary_upgrade_create_empty_extension(PG_FUNCTION_ARGS) { text *extName; text *schemaName; bool relocatable; text *extVersion; Datum extConfig; Datum extCondition; List *requiredExtensions; CHECK_IS_BINARY_UPGRADE; /* We must check these things before dereferencing the arguments */ if (PG_ARGISNULL(0) || PG_ARGISNULL(1) || PG_ARGISNULL(2) || PG_ARGISNULL(3)) elog(ERROR, "null argument to binary_upgrade_create_empty_extension is not allowed"); extName = PG_GETARG_TEXT_PP(0); schemaName = PG_GETARG_TEXT_PP(1); relocatable = PG_GETARG_BOOL(2); extVersion = PG_GETARG_TEXT_PP(3); if (PG_ARGISNULL(4)) extConfig = PointerGetDatum(NULL); else extConfig = PG_GETARG_DATUM(4); if (PG_ARGISNULL(5)) extCondition = PointerGetDatum(NULL); else extCondition = PG_GETARG_DATUM(5); requiredExtensions = NIL; if (!PG_ARGISNULL(6)) { ArrayType *textArray = PG_GETARG_ARRAYTYPE_P(6); Datum *textDatums; int ndatums; int i; deconstruct_array(textArray, TEXTOID, -1, false, 'i', &textDatums, NULL, &ndatums); for (i = 0; i < ndatums; i++) { text *txtname = DatumGetTextPP(textDatums[i]); char *extName = text_to_cstring(txtname); Oid extOid = get_extension_oid(extName, false); requiredExtensions = lappend_oid(requiredExtensions, extOid); } } InsertExtensionTuple(text_to_cstring(extName), GetUserId(), get_namespace_oid(text_to_cstring(schemaName), false), relocatable, text_to_cstring(extVersion), extConfig, extCondition, requiredExtensions); PG_RETURN_VOID(); }
Datum spg_text_leaf_consistent(PG_FUNCTION_ARGS) { spgLeafConsistentIn *in = (spgLeafConsistentIn *) PG_GETARG_POINTER(0); spgLeafConsistentOut *out = (spgLeafConsistentOut *) PG_GETARG_POINTER(1); StrategyNumber strategy = in->strategy; text *query = DatumGetTextPP(in->query); int level = in->level; text *leafValue, *reconstrValue = NULL; char *fullValue; int fullLen; int queryLen; int r; bool res; /* all tests are exact */ out->recheck = false; leafValue = DatumGetTextPP(in->leafDatum); if (DatumGetPointer(in->reconstructedValue)) reconstrValue = DatumGetTextP(in->reconstructedValue); Assert(level == 0 ? reconstrValue == NULL : VARSIZE_ANY_EXHDR(reconstrValue) == level); fullLen = level + VARSIZE_ANY_EXHDR(leafValue); queryLen = VARSIZE_ANY_EXHDR(query); /* * For an equality check, we needn't reconstruct fullValue if not same * length; it can't match */ if (strategy == BTEqualStrategyNumber && queryLen != fullLen) PG_RETURN_BOOL(false); /* Else, reconstruct the full string represented by this leaf tuple */ if (VARSIZE_ANY_EXHDR(leafValue) == 0 && level > 0) { fullValue = VARDATA(reconstrValue); out->leafValue = PointerGetDatum(reconstrValue); } else { text *fullText = palloc(VARHDRSZ + fullLen); SET_VARSIZE(fullText, VARHDRSZ + fullLen); fullValue = VARDATA(fullText); if (level) memcpy(fullValue, VARDATA(reconstrValue), level); if (VARSIZE_ANY_EXHDR(leafValue) > 0) memcpy(fullValue + level, VARDATA_ANY(leafValue), VARSIZE_ANY_EXHDR(leafValue)); out->leafValue = PointerGetDatum(fullText); } /* Run the appropriate type of comparison */ if (strategy > 10) { /* Collation-aware comparison */ strategy -= 10; /* If asserts are enabled, verify encoding of reconstructed string */ Assert(pg_verifymbstr(fullValue, fullLen, false)); r = varstr_cmp(fullValue, Min(queryLen, fullLen), VARDATA_ANY(query), Min(queryLen, fullLen), PG_GET_COLLATION()); } else { /* Non-collation-aware comparison */ r = memcmp(fullValue, VARDATA_ANY(query), Min(queryLen, fullLen)); } if (r == 0) { if (queryLen > fullLen) r = -1; else if (queryLen < fullLen) r = 1; } switch (strategy) { case BTLessStrategyNumber: res = (r < 0); break; case BTLessEqualStrategyNumber: res = (r <= 0); break; case BTEqualStrategyNumber: res = (r == 0); break; case BTGreaterEqualStrategyNumber: res = (r >= 0); break; case BTGreaterStrategyNumber: res = (r > 0); break; default: elog(ERROR, "unrecognized strategy number: %d", in->strategy); res = false; break; } PG_RETURN_BOOL(res); }
Datum spg_text_inner_consistent(PG_FUNCTION_ARGS) { spgInnerConsistentIn *in = (spgInnerConsistentIn *) PG_GETARG_POINTER(0); spgInnerConsistentOut *out = (spgInnerConsistentOut *) PG_GETARG_POINTER(1); StrategyNumber strategy = in->strategy; text *inText; int inSize; int i; text *reconstrText = NULL; int maxReconstrLen = 0; text *prefixText = NULL; int prefixSize = 0; /* * If it's a collation-aware operator, but the collation is C, we can * treat it as non-collation-aware. */ if (strategy > 10 && lc_collate_is_c(PG_GET_COLLATION())) strategy -= 10; inText = DatumGetTextPP(in->query); inSize = VARSIZE_ANY_EXHDR(inText); /* * Reconstruct values represented at this tuple, including parent data, * prefix of this tuple if any, and the node label if any. in->level * should be the length of the previously reconstructed value, and the * number of bytes added here is prefixSize or prefixSize + 1. * * Note: we assume that in->reconstructedValue isn't toasted and doesn't * have a short varlena header. This is okay because it must have been * created by a previous invocation of this routine, and we always emit * long-format reconstructed values. */ Assert(in->level == 0 ? DatumGetPointer(in->reconstructedValue) == NULL : VARSIZE_ANY_EXHDR(DatumGetPointer(in->reconstructedValue)) == in->level); maxReconstrLen = in->level + 1; if (in->hasPrefix) { prefixText = DatumGetTextPP(in->prefixDatum); prefixSize = VARSIZE_ANY_EXHDR(prefixText); maxReconstrLen += prefixSize; } reconstrText = palloc(VARHDRSZ + maxReconstrLen); SET_VARSIZE(reconstrText, VARHDRSZ + maxReconstrLen); if (in->level) memcpy(VARDATA(reconstrText), VARDATA(DatumGetPointer(in->reconstructedValue)), in->level); if (prefixSize) memcpy(((char *) VARDATA(reconstrText)) + in->level, VARDATA_ANY(prefixText), prefixSize); /* last byte of reconstrText will be filled in below */ /* * Scan the child nodes. For each one, complete the reconstructed value * and see if it's consistent with the query. If so, emit an entry into * the output arrays. */ out->nodeNumbers = (int *) palloc(sizeof(int) * in->nNodes); out->levelAdds = (int *) palloc(sizeof(int) * in->nNodes); out->reconstructedValues = (Datum *) palloc(sizeof(Datum) * in->nNodes); out->nNodes = 0; for (i = 0; i < in->nNodes; i++) { uint8 nodeChar = DatumGetUInt8(in->nodeLabels[i]); int thisLen; int r; bool res = false; /* If nodeChar is zero, don't include it in data */ if (nodeChar == '\0') thisLen = maxReconstrLen - 1; else { ((char *) VARDATA(reconstrText))[maxReconstrLen - 1] = nodeChar; thisLen = maxReconstrLen; } r = memcmp(VARDATA(reconstrText), VARDATA_ANY(inText), Min(inSize, thisLen)); switch (strategy) { case BTLessStrategyNumber: case BTLessEqualStrategyNumber: if (r <= 0) res = true; break; case BTEqualStrategyNumber: if (r == 0 && inSize >= thisLen) res = true; break; case BTGreaterEqualStrategyNumber: case BTGreaterStrategyNumber: if (r >= 0) res = true; break; case BTLessStrategyNumber + 10: case BTLessEqualStrategyNumber + 10: case BTGreaterEqualStrategyNumber + 10: case BTGreaterStrategyNumber + 10: /* * with non-C collation we need to traverse whole tree :-( */ res = true; break; default: elog(ERROR, "unrecognized strategy number: %d", in->strategy); break; } if (res) { out->nodeNumbers[out->nNodes] = i; out->levelAdds[out->nNodes] = thisLen - in->level; SET_VARSIZE(reconstrText, VARHDRSZ + thisLen); out->reconstructedValues[out->nNodes] = datumCopy(PointerGetDatum(reconstrText), false, -1); out->nNodes++; } } PG_RETURN_VOID(); }
Datum spg_text_picksplit(PG_FUNCTION_ARGS) { spgPickSplitIn *in = (spgPickSplitIn *) PG_GETARG_POINTER(0); spgPickSplitOut *out = (spgPickSplitOut *) PG_GETARG_POINTER(1); text *text0 = DatumGetTextPP(in->datums[0]); int i, commonLen; spgNodePtr *nodes; /* Identify longest common prefix, if any */ commonLen = VARSIZE_ANY_EXHDR(text0); for (i = 1; i < in->nTuples && commonLen > 0; i++) { text *texti = DatumGetTextPP(in->datums[i]); int tmp = commonPrefix(VARDATA_ANY(text0), VARDATA_ANY(texti), VARSIZE_ANY_EXHDR(text0), VARSIZE_ANY_EXHDR(texti)); if (tmp < commonLen) commonLen = tmp; } /* * Limit the prefix length, if necessary, to ensure that the resulting * inner tuple will fit on a page. */ commonLen = Min(commonLen, SPGIST_MAX_PREFIX_LENGTH); /* Set node prefix to be that string, if it's not empty */ if (commonLen == 0) { out->hasPrefix = false; } else { out->hasPrefix = true; out->prefixDatum = formTextDatum(VARDATA_ANY(text0), commonLen); } /* Extract the node label (first non-common byte) from each value */ nodes = (spgNodePtr *) palloc(sizeof(spgNodePtr) * in->nTuples); for (i = 0; i < in->nTuples; i++) { text *texti = DatumGetTextPP(in->datums[i]); if (commonLen < VARSIZE_ANY_EXHDR(texti)) nodes[i].c = *(uint8 *) (VARDATA_ANY(texti) + commonLen); else nodes[i].c = '\0'; /* use \0 if string is all common */ nodes[i].i = i; nodes[i].d = in->datums[i]; } /* * Sort by label bytes so that we can group the values into nodes. This * also ensures that the nodes are ordered by label value, allowing the * use of binary search in searchChar. */ qsort(nodes, in->nTuples, sizeof(*nodes), cmpNodePtr); /* And emit results */ out->nNodes = 0; out->nodeLabels = (Datum *) palloc(sizeof(Datum) * in->nTuples); out->mapTuplesToNodes = (int *) palloc(sizeof(int) * in->nTuples); out->leafTupleDatums = (Datum *) palloc(sizeof(Datum) * in->nTuples); for (i = 0; i < in->nTuples; i++) { text *texti = DatumGetTextPP(nodes[i].d); Datum leafD; if (i == 0 || nodes[i].c != nodes[i - 1].c) { out->nodeLabels[out->nNodes] = UInt8GetDatum(nodes[i].c); out->nNodes++; } if (commonLen < VARSIZE_ANY_EXHDR(texti)) leafD = formTextDatum(VARDATA_ANY(texti) + commonLen + 1, VARSIZE_ANY_EXHDR(texti) - commonLen - 1); else leafD = formTextDatum(NULL, 0); out->leafTupleDatums[nodes[i].i] = leafD; out->mapTuplesToNodes[nodes[i].i] = out->nNodes - 1; } PG_RETURN_VOID(); }
Datum spg_text_choose(PG_FUNCTION_ARGS) { spgChooseIn *in = (spgChooseIn *) PG_GETARG_POINTER(0); spgChooseOut *out = (spgChooseOut *) PG_GETARG_POINTER(1); text *inText = DatumGetTextPP(in->datum); char *inStr = VARDATA_ANY(inText); int inSize = VARSIZE_ANY_EXHDR(inText); uint8 nodeChar = '\0'; int i = 0; int commonLen = 0; /* Check for prefix match, set nodeChar to first byte after prefix */ if (in->hasPrefix) { text *prefixText = DatumGetTextPP(in->prefixDatum); char *prefixStr = VARDATA_ANY(prefixText); int prefixSize = VARSIZE_ANY_EXHDR(prefixText); commonLen = commonPrefix(inStr + in->level, prefixStr, inSize - in->level, prefixSize); if (commonLen == prefixSize) { if (inSize - in->level > commonLen) nodeChar = *(uint8 *) (inStr + in->level + commonLen); else nodeChar = '\0'; } else { /* Must split tuple because incoming value doesn't match prefix */ out->resultType = spgSplitTuple; if (commonLen == 0) { out->result.splitTuple.prefixHasPrefix = false; } else { out->result.splitTuple.prefixHasPrefix = true; out->result.splitTuple.prefixPrefixDatum = formTextDatum(prefixStr, commonLen); } out->result.splitTuple.nodeLabel = UInt8GetDatum(*(prefixStr + commonLen)); if (prefixSize - commonLen == 1) { out->result.splitTuple.postfixHasPrefix = false; } else { out->result.splitTuple.postfixHasPrefix = true; out->result.splitTuple.postfixPrefixDatum = formTextDatum(prefixStr + commonLen + 1, prefixSize - commonLen - 1); } PG_RETURN_VOID(); } } else if (inSize > in->level) { nodeChar = *(uint8 *) (inStr + in->level); } else { nodeChar = '\0'; } /* Look up nodeChar in the node label array */ if (searchChar(in->nodeLabels, in->nNodes, nodeChar, &i)) { /* * Descend to existing node. (If in->allTheSame, the core code will * ignore our nodeN specification here, but that's OK. We still * have to provide the correct levelAdd and restDatum values, and * those are the same regardless of which node gets chosen by core.) */ out->resultType = spgMatchNode; out->result.matchNode.nodeN = i; out->result.matchNode.levelAdd = commonLen + 1; if (inSize - in->level - commonLen - 1 > 0) out->result.matchNode.restDatum = formTextDatum(inStr + in->level + commonLen + 1, inSize - in->level - commonLen - 1); else out->result.matchNode.restDatum = formTextDatum(NULL, 0); } else if (in->allTheSame) { /* * Can't use AddNode action, so split the tuple. The upper tuple * has the same prefix as before and uses an empty node label for * the lower tuple. The lower tuple has no prefix and the same * node labels as the original tuple. */ out->resultType = spgSplitTuple; out->result.splitTuple.prefixHasPrefix = in->hasPrefix; out->result.splitTuple.prefixPrefixDatum = in->prefixDatum; out->result.splitTuple.nodeLabel = UInt8GetDatum('\0'); out->result.splitTuple.postfixHasPrefix = false; } else { /* Add a node for the not-previously-seen nodeChar value */ out->resultType = spgAddNode; out->result.addNode.nodeLabel = UInt8GetDatum(nodeChar); out->result.addNode.nodeN = i; } PG_RETURN_VOID(); }
Datum spg_text_leaf_consistent(PG_FUNCTION_ARGS) { spgLeafConsistentIn *in = (spgLeafConsistentIn *) PG_GETARG_POINTER(0); spgLeafConsistentOut *out = (spgLeafConsistentOut *) PG_GETARG_POINTER(1); int level = in->level; text *leafValue, *reconstrValue = NULL; char *fullValue; int fullLen; bool res; int j; /* all tests are exact */ out->recheck = false; leafValue = DatumGetTextPP(in->leafDatum); if (DatumGetPointer(in->reconstructedValue)) reconstrValue = DatumGetTextP(in->reconstructedValue); Assert(level == 0 ? reconstrValue == NULL : VARSIZE_ANY_EXHDR(reconstrValue) == level); /* Reconstruct the full string represented by this leaf tuple */ fullLen = level + VARSIZE_ANY_EXHDR(leafValue); if (VARSIZE_ANY_EXHDR(leafValue) == 0 && level > 0) { fullValue = VARDATA(reconstrValue); out->leafValue = PointerGetDatum(reconstrValue); } else { text *fullText = palloc(VARHDRSZ + fullLen); SET_VARSIZE(fullText, VARHDRSZ + fullLen); fullValue = VARDATA(fullText); if (level) memcpy(fullValue, VARDATA(reconstrValue), level); if (VARSIZE_ANY_EXHDR(leafValue) > 0) memcpy(fullValue + level, VARDATA_ANY(leafValue), VARSIZE_ANY_EXHDR(leafValue)); out->leafValue = PointerGetDatum(fullText); } /* Perform the required comparison(s) */ res = true; for (j = 0; j < in->nkeys; j++) { StrategyNumber strategy = in->scankeys[j].sk_strategy; text *query = DatumGetTextPP(in->scankeys[j].sk_argument); int queryLen = VARSIZE_ANY_EXHDR(query); int r; if (strategy > 10) { /* Collation-aware comparison */ strategy -= 10; /* If asserts enabled, verify encoding of reconstructed string */ Assert(pg_verifymbstr(fullValue, fullLen, false)); r = varstr_cmp(fullValue, Min(queryLen, fullLen), VARDATA_ANY(query), Min(queryLen, fullLen), PG_GET_COLLATION()); } else { /* Non-collation-aware comparison */ r = memcmp(fullValue, VARDATA_ANY(query), Min(queryLen, fullLen)); } if (r == 0) { if (queryLen > fullLen) r = -1; else if (queryLen < fullLen) r = 1; } switch (strategy) { case BTLessStrategyNumber: res = (r < 0); break; case BTLessEqualStrategyNumber: res = (r <= 0); break; case BTEqualStrategyNumber: res = (r == 0); break; case BTGreaterEqualStrategyNumber: res = (r >= 0); break; case BTGreaterStrategyNumber: res = (r > 0); break; default: elog(ERROR, "unrecognized strategy number: %d", in->scankeys[j].sk_strategy); res = false; break; } if (!res) break; /* no need to consider remaining conditions */ } PG_RETURN_BOOL(res); }
Datum spg_text_inner_consistent(PG_FUNCTION_ARGS) { spgInnerConsistentIn *in = (spgInnerConsistentIn *) PG_GETARG_POINTER(0); spgInnerConsistentOut *out = (spgInnerConsistentOut *) PG_GETARG_POINTER(1); bool collate_is_c = lc_collate_is_c(PG_GET_COLLATION()); text *reconstrText = NULL; int maxReconstrLen = 0; text *prefixText = NULL; int prefixSize = 0; int i; /* * Reconstruct values represented at this tuple, including parent data, * prefix of this tuple if any, and the node label if it's non-dummy. * in->level should be the length of the previously reconstructed value, * and the number of bytes added here is prefixSize or prefixSize + 1. * * Note: we assume that in->reconstructedValue isn't toasted and doesn't * have a short varlena header. This is okay because it must have been * created by a previous invocation of this routine, and we always emit * long-format reconstructed values. */ Assert(in->level == 0 ? DatumGetPointer(in->reconstructedValue) == NULL : VARSIZE_ANY_EXHDR(DatumGetPointer(in->reconstructedValue)) == in->level); maxReconstrLen = in->level + 1; if (in->hasPrefix) { prefixText = DatumGetTextPP(in->prefixDatum); prefixSize = VARSIZE_ANY_EXHDR(prefixText); maxReconstrLen += prefixSize; } reconstrText = palloc(VARHDRSZ + maxReconstrLen); SET_VARSIZE(reconstrText, VARHDRSZ + maxReconstrLen); if (in->level) memcpy(VARDATA(reconstrText), VARDATA(DatumGetPointer(in->reconstructedValue)), in->level); if (prefixSize) memcpy(((char *) VARDATA(reconstrText)) + in->level, VARDATA_ANY(prefixText), prefixSize); /* last byte of reconstrText will be filled in below */ /* * Scan the child nodes. For each one, complete the reconstructed value * and see if it's consistent with the query. If so, emit an entry into * the output arrays. */ out->nodeNumbers = (int *) palloc(sizeof(int) * in->nNodes); out->levelAdds = (int *) palloc(sizeof(int) * in->nNodes); out->reconstructedValues = (Datum *) palloc(sizeof(Datum) * in->nNodes); out->nNodes = 0; for (i = 0; i < in->nNodes; i++) { int16 nodeChar = DatumGetInt16(in->nodeLabels[i]); int thisLen; bool res = true; int j; /* If nodeChar is a dummy value, don't include it in data */ if (nodeChar <= 0) thisLen = maxReconstrLen - 1; else { ((unsigned char *) VARDATA(reconstrText))[maxReconstrLen - 1] = nodeChar; thisLen = maxReconstrLen; } for (j = 0; j < in->nkeys; j++) { StrategyNumber strategy = in->scankeys[j].sk_strategy; text *inText; int inSize; int r; /* * If it's a collation-aware operator, but the collation is C, we * can treat it as non-collation-aware. With non-C collation we * need to traverse whole tree :-( so there's no point in making * any check here. (Note also that our reconstructed value may * well end with a partial multibyte character, so that applying * any encoding-sensitive test to it would be risky anyhow.) */ if (strategy > 10) { if (collate_is_c) strategy -= 10; else continue; } inText = DatumGetTextPP(in->scankeys[j].sk_argument); inSize = VARSIZE_ANY_EXHDR(inText); r = memcmp(VARDATA(reconstrText), VARDATA_ANY(inText), Min(inSize, thisLen)); switch (strategy) { case BTLessStrategyNumber: case BTLessEqualStrategyNumber: if (r > 0) res = false; break; case BTEqualStrategyNumber: if (r != 0 || inSize < thisLen) res = false; break; case BTGreaterEqualStrategyNumber: case BTGreaterStrategyNumber: if (r < 0) res = false; break; default: elog(ERROR, "unrecognized strategy number: %d", in->scankeys[j].sk_strategy); break; } if (!res) break; /* no need to consider remaining conditions */ } if (res) { out->nodeNumbers[out->nNodes] = i; out->levelAdds[out->nNodes] = thisLen - in->level; SET_VARSIZE(reconstrText, VARHDRSZ + thisLen); out->reconstructedValues[out->nNodes] = datumCopy(PointerGetDatum(reconstrText), false, -1); out->nNodes++; } } PG_RETURN_VOID(); }
Datum spg_text_choose(PG_FUNCTION_ARGS) { spgChooseIn *in = (spgChooseIn *) PG_GETARG_POINTER(0); spgChooseOut *out = (spgChooseOut *) PG_GETARG_POINTER(1); text *inText = DatumGetTextPP(in->datum); char *inStr = VARDATA_ANY(inText); int inSize = VARSIZE_ANY_EXHDR(inText); char *prefixStr = NULL; int prefixSize = 0; int commonLen = 0; int16 nodeChar = 0; int i = 0; /* Check for prefix match, set nodeChar to first byte after prefix */ if (in->hasPrefix) { text *prefixText = DatumGetTextPP(in->prefixDatum); prefixStr = VARDATA_ANY(prefixText); prefixSize = VARSIZE_ANY_EXHDR(prefixText); commonLen = commonPrefix(inStr + in->level, prefixStr, inSize - in->level, prefixSize); if (commonLen == prefixSize) { if (inSize - in->level > commonLen) nodeChar = *(unsigned char *) (inStr + in->level + commonLen); else nodeChar = -1; } else { /* Must split tuple because incoming value doesn't match prefix */ out->resultType = spgSplitTuple; if (commonLen == 0) { out->result.splitTuple.prefixHasPrefix = false; } else { out->result.splitTuple.prefixHasPrefix = true; out->result.splitTuple.prefixPrefixDatum = formTextDatum(prefixStr, commonLen); } out->result.splitTuple.nodeLabel = Int16GetDatum(*(unsigned char *) (prefixStr + commonLen)); if (prefixSize - commonLen == 1) { out->result.splitTuple.postfixHasPrefix = false; } else { out->result.splitTuple.postfixHasPrefix = true; out->result.splitTuple.postfixPrefixDatum = formTextDatum(prefixStr + commonLen + 1, prefixSize - commonLen - 1); } PG_RETURN_VOID(); } } else if (inSize > in->level) { nodeChar = *(unsigned char *) (inStr + in->level); } else { nodeChar = -1; } /* Look up nodeChar in the node label array */ if (searchChar(in->nodeLabels, in->nNodes, nodeChar, &i)) { /* * Descend to existing node. (If in->allTheSame, the core code will * ignore our nodeN specification here, but that's OK. We still have * to provide the correct levelAdd and restDatum values, and those are * the same regardless of which node gets chosen by core.) */ int levelAdd; out->resultType = spgMatchNode; out->result.matchNode.nodeN = i; levelAdd = commonLen; if (nodeChar >= 0) levelAdd++; out->result.matchNode.levelAdd = levelAdd; if (inSize - in->level - levelAdd > 0) out->result.matchNode.restDatum = formTextDatum(inStr + in->level + levelAdd, inSize - in->level - levelAdd); else out->result.matchNode.restDatum = formTextDatum(NULL, 0); } else if (in->allTheSame) { /* * Can't use AddNode action, so split the tuple. The upper tuple has * the same prefix as before and uses a dummy node label -2 for the * lower tuple. The lower tuple has no prefix and the same node * labels as the original tuple. * * Note: it might seem tempting to shorten the upper tuple's prefix, * if it has one, then use its last byte as label for the lower tuple. * But that doesn't win since we know the incoming value matches the * whole prefix: we'd just end up splitting the lower tuple again. */ out->resultType = spgSplitTuple; out->result.splitTuple.prefixHasPrefix = in->hasPrefix; out->result.splitTuple.prefixPrefixDatum = in->prefixDatum; out->result.splitTuple.nodeLabel = Int16GetDatum(-2); out->result.splitTuple.postfixHasPrefix = false; } else { /* Add a node for the not-previously-seen nodeChar value */ out->resultType = spgAddNode; out->result.addNode.nodeLabel = Int16GetDatum(nodeChar); out->result.addNode.nodeN = i; } PG_RETURN_VOID(); }
Datum autoinc(PG_FUNCTION_ARGS) { TriggerData *trigdata = (TriggerData *) fcinfo->context; Trigger *trigger; /* to get trigger name */ int nargs; /* # of arguments */ int *chattrs; /* attnums of attributes to change */ int chnattrs = 0; /* # of above */ Datum *newvals; /* vals of above */ bool *newnulls; /* null flags for above */ char **args; /* arguments */ char *relname; /* triggered relation name */ Relation rel; /* triggered relation */ HeapTuple rettuple = NULL; TupleDesc tupdesc; /* tuple description */ bool isnull; int i; if (!CALLED_AS_TRIGGER(fcinfo)) /* internal error */ elog(ERROR, "not fired by trigger manager"); if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event)) /* internal error */ elog(ERROR, "must be fired for row"); if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event)) /* internal error */ elog(ERROR, "must be fired before event"); if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event)) rettuple = trigdata->tg_trigtuple; else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event)) rettuple = trigdata->tg_newtuple; else /* internal error */ elog(ERROR, "cannot process DELETE events"); rel = trigdata->tg_relation; relname = SPI_getrelname(rel); trigger = trigdata->tg_trigger; nargs = trigger->tgnargs; if (nargs <= 0 || nargs % 2 != 0) /* internal error */ elog(ERROR, "autoinc (%s): even number gt 0 of arguments was expected", relname); args = trigger->tgargs; tupdesc = rel->rd_att; chattrs = (int *) palloc(nargs / 2 * sizeof(int)); newvals = (Datum *) palloc(nargs / 2 * sizeof(Datum)); newnulls = (bool *) palloc(nargs / 2 * sizeof(bool)); for (i = 0; i < nargs;) { int attnum = SPI_fnumber(tupdesc, args[i]); int32 val; Datum seqname; if (attnum <= 0) ereport(ERROR, (errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION), errmsg("\"%s\" has no attribute \"%s\"", relname, args[i]))); if (SPI_gettypeid(tupdesc, attnum) != INT4OID) ereport(ERROR, (errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION), errmsg("attribute \"%s\" of \"%s\" must be type INT4", args[i], relname))); val = DatumGetInt32(SPI_getbinval(rettuple, tupdesc, attnum, &isnull)); if (!isnull && val != 0) { i += 2; continue; } i++; chattrs[chnattrs] = attnum; seqname = CStringGetTextDatum(args[i]); newvals[chnattrs] = DirectFunctionCall1(nextval, seqname); /* nextval now returns int64; coerce down to int32 */ newvals[chnattrs] = Int32GetDatum((int32) DatumGetInt64(newvals[chnattrs])); if (DatumGetInt32(newvals[chnattrs]) == 0) { newvals[chnattrs] = DirectFunctionCall1(nextval, seqname); newvals[chnattrs] = Int32GetDatum((int32) DatumGetInt64(newvals[chnattrs])); } newnulls[chnattrs] = false; pfree(DatumGetTextPP(seqname)); chnattrs++; i++; } if (chnattrs > 0) { rettuple = heap_modify_tuple_by_cols(rettuple, tupdesc, chnattrs, chattrs, newvals, newnulls); } pfree(relname); pfree(chattrs); pfree(newvals); pfree(newnulls); return PointerGetDatum(rettuple); }