1/*-------------------------------------------------------------------------
4 * Utilities for tsquery datatype
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
10 * src/backend/utils/adt/tsquery_util.c
12 *-------------------------------------------------------------------------
22 * Build QTNode tree for a tsquery given in QueryItem array format.
29 /* since this function recurses, it could be driven to stack overflow. */
60 * Referenced "word" and "valnode" items are freed if marked as transient
69 /* since this function recurses, it could be driven to stack overflow. */
92 * Sort comparator for QTNodes.
94 * The sort order is somewhat arbitrary.
99 /* since this function recurses, it could be driven to stack overflow. */
111 return (ao->
oper > bo->
oper) ? -1 : 1;
145 return 0;
/* keep compiler quiet */
150 * qsort comparator for QTNode pointers.
159 * Canonicalize a QTNode tree by sorting the children of AND/OR nodes
160 * into an arbitrary but well-defined order.
167 /* since this function recurses, it could be driven to stack overflow. */
180 * Are two QTNode trees equal according to QTNodeCompare?
194 * Remove unnecessary intermediate nodes. For example:
205 /* since this function recurses, it could be driven to stack overflow. */
214 /* Only AND and OR are associative, so don't flatten other node types */
226 int oldnchild = in->
nchild;
231 if (
i + 1 != oldnchild)
233 (oldnchild -
i - 1) *
sizeof(
QTNode *));
246 * Convert a tree to binary tree by inserting intermediate nodes.
247 * (Opposite of QTNTernary)
254 /* since this function recurses, it could be driven to stack overflow. */
287 * Count the total length of operand strings in tree (including '0円'-
288 * terminators) and the total number of nodes.
289 * Caller must initialize *sumlen and *nnode to zeroes.
294 /* since this function recurses, it could be driven to stack overflow. */
319 * Recursively convert a QTNode tree into flat tsquery format.
320 * Caller must have allocated arrays of the correct size.
325 /* since this function recurses, it could be driven to stack overflow. */
333 state->curitem->qoperand.distance =
state->curoperand -
state->operand;
360 * Build flat tsquery from a QTNode tree.
375 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
376 errmsg(
"tsquery is too large")));
391 * Copy a QTNode tree.
393 * Modifiable copies of the words and valnodes are made, too.
400 /* since this function recurses, it could be driven to stack overflow. */
431 * Clear the specified flag bit(s) in all nodes of a QTNode tree.
436 /* since this function recurses, it could be driven to stack overflow. */
#define COMPUTESIZE(size)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
Assert(PointerIsAligned(start, uint64))
void * repalloc(void *pointer, Size size)
void pfree(void *pointer)
void * palloc0(Size size)
#define qsort(a, b, c, d)
void check_stack_depth(void)
#define TSQUERY_TOO_BIG(size, lenofoperand)
void QTNClearFlags(QTNode *in, uint32 flags)
int QTNodeCompare(QTNode *an, QTNode *bn)
QTNode * QTNCopy(QTNode *in)
static void cntsize(QTNode *in, int *sumlen, int *nnode)
static int cmpQTN(const void *a, const void *b)
void QTNTernary(QTNode *in)
bool QTNEq(QTNode *a, QTNode *b)
static void fillQT(QTN2QTState *state, QTNode *in)
QTNode * QT2QTN(QueryItem *in, char *operand)
TSQuery QTN2QT(QTNode *in)
void QTNBinary(QTNode *in)
int32 tsCompareString(char *a, int lena, char *b, int lenb, bool prefix)
static void SET_VARSIZE(void *PTR, Size len)