static int CompareTSQ(TSQuery a, TSQuery b) { if (a->size != b->size) { return (a->size < b->size) ? -1 : 1; } else if (VARSIZE(a) != VARSIZE(b)) { return (VARSIZE(a) < VARSIZE(b)) ? -1 : 1; } else if (a->size != 0) { QTNode *an = QT2QTN(GETQUERY(a), GETOPERAND(a)); QTNode *bn = QT2QTN(GETQUERY(b), GETOPERAND(b)); int res = QTNodeCompare(an, bn); QTNFree(an); QTNFree(bn); return res; } return 0; }
bool QTNEq(QTNode *a, QTNode *b) { uint32 sign = a->sign & b->sign; if (!(sign == a->sign && sign == b->sign)) return 0; return (QTNodeCompare(a, b) == 0) ? true : false; }
/* * Sort comparator for QTNodes. * * The sort order is somewhat arbitrary. */ int QTNodeCompare(QTNode *an, QTNode *bn) { /* since this function recurses, it could be driven to stack overflow. */ check_stack_depth(); if (an->valnode->type != bn->valnode->type) return (an->valnode->type > bn->valnode->type) ? -1 : 1; if (an->valnode->type == QI_OPR) { QueryOperator *ao = &an->valnode->qoperator; QueryOperator *bo = &bn->valnode->qoperator; if (ao->oper != bo->oper) return (ao->oper > bo->oper) ? -1 : 1; if (an->nchild != bn->nchild) return (an->nchild > bn->nchild) ? -1 : 1; { int i, res; for (i = 0; i < an->nchild; i++) if ((res = QTNodeCompare(an->child[i], bn->child[i])) != 0) return res; } if (ao->oper == OP_PHRASE && ao->distance != bo->distance) return (ao->distance > bo->distance) ? -1 : 1; return 0; } else if (an->valnode->type == QI_VAL) { QueryOperand *ao = &an->valnode->qoperand; QueryOperand *bo = &bn->valnode->qoperand; if (ao->valcrc != bo->valcrc) { return (ao->valcrc > bo->valcrc) ? -1 : 1; } return tsCompareString(an->word, ao->length, bn->word, bo->length, false); } else { elog(ERROR, "unrecognized QueryItem type: %d", an->valnode->type); return 0; /* keep compiler quiet */ } }
int QTNodeCompare(QTNode *an, QTNode *bn) { /* since this function recurses, it could be driven to stack overflow. */ check_stack_depth(); if (an->valnode->type != bn->valnode->type) return (an->valnode->type > bn->valnode->type) ? -1 : 1; if (an->valnode->type == QI_OPR) { QueryOperator *ao = &an->valnode->qoperator; QueryOperator *bo = &bn->valnode->qoperator; if (ao->oper != bo->oper) return (ao->oper > bo->oper) ? -1 : 1; if (an->nchild != bn->nchild) return (an->nchild > bn->nchild) ? -1 : 1; { int i, res; for (i = 0; i < an->nchild; i++) if ((res = QTNodeCompare(an->child[i], bn->child[i])) != 0) return res; } return 0; } else { QueryOperand *ao = &an->valnode->qoperand; QueryOperand *bo = &bn->valnode->qoperand; Assert(an->valnode->type == QI_VAL); if (ao->valcrc != bo->valcrc) { return (ao->valcrc > bo->valcrc) ? -1 : 1; } return tsCompareString(an->word, ao->length, bn->word, bo->length, false); } }
static int cmpQTN(const void *a, const void *b) { return QTNodeCompare(*(QTNode *const *) a, *(QTNode *const *) b); }