qFilter.h 11.9 KB
Newer Older
D
dapan1121 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef TDENGINE_QFILTER_H
#define TDENGINE_QFILTER_H

#ifdef __cplusplus
extern "C" {
#endif

#include "texpr.h"
W
wpan 已提交
24
#include "hash.h"
W
wpan 已提交
25
#include "tname.h"
D
dapan1121 已提交
26

W
wpan 已提交
27
#define FILTER_DEFAULT_GROUP_SIZE 4
D
dapan1121 已提交
28 29
#define FILTER_DEFAULT_UNIT_SIZE 4
#define FILTER_DEFAULT_FIELD_SIZE 4
W
wpan 已提交
30
#define FILTER_DEFAULT_VALUE_SIZE 4
D
dapan1121 已提交
31 32
#define FILTER_DEFAULT_GROUP_UNIT_SIZE 2

W
wpan 已提交
33 34
#define FILTER_DUMMY_EMPTY_OPTR  127

W
fix bug  
wpan 已提交
35 36
#define MAX_NUM_STR_SIZE 40

W
wpan 已提交
37
#define FILTER_RM_UNIT_MIN_ROWS 100
W
wpan 已提交
38

D
dapan1121 已提交
39
enum {
W
wpan 已提交
40 41 42 43 44
  FLD_TYPE_COLUMN = 1,
  FLD_TYPE_VALUE = 2,  
  FLD_TYPE_MAX = 3,
  FLD_DESC_NO_FREE = 4,
  FLD_DATA_NO_FREE = 8,
W
wpan 已提交
45
  FLD_DATA_IS_HASH = 16,
D
dapan1121 已提交
46 47
};

W
wpan 已提交
48 49 50
enum {
  MR_ST_START = 1,
  MR_ST_FIN = 2,
W
wpan 已提交
51 52
  MR_ST_ALL = 4,
  MR_ST_EMPTY = 8,
W
wpan 已提交
53 54 55
};

enum {
W
wpan 已提交
56 57 58
  RANGE_FLG_EXCLUDE = 1,
  RANGE_FLG_INCLUDE = 2,
  RANGE_FLG_NULL    = 4,
W
wpan 已提交
59 60
};

W
wpan 已提交
61
enum {
W
wpan 已提交
62 63 64
  FI_OPTION_NO_REWRITE = 1,
  FI_OPTION_TIMESTAMP = 2,
  FI_OPTION_NEED_UNIQE = 4,
W
wpan 已提交
65 66
};

W
wpan 已提交
67
enum {
W
wpan 已提交
68 69 70
  FI_STATUS_ALL = 1,
  FI_STATUS_EMPTY = 2,
  FI_STATUS_REWRITE = 4,
W
wpan 已提交
71
  FI_STATUS_CLONED = 8,
W
wpan 已提交
72 73
};

W
wpan 已提交
74 75 76 77 78 79
enum {
  FI_STATUS_BLK_ALL = 1,
  FI_STATUS_BLK_EMPTY = 2,
  FI_STATUS_BLK_ACTIVE = 4,
};

W
wpan 已提交
80 81
enum {
  RANGE_TYPE_UNIT = 1,
W
wpan 已提交
82
  RANGE_TYPE_VAR_HASH = 2,
W
wpan 已提交
83
  RANGE_TYPE_MR_CTX = 3,
W
wpan 已提交
84 85
};

D
dapan1121 已提交
86 87 88 89 90
typedef struct OptrStr {
  uint16_t optr;
  char    *str;
} OptrStr;

W
wpan 已提交
91 92 93
typedef struct SFilterRange {
  int64_t s;
  int64_t e;
W
wpan 已提交
94 95
  char sflag;
  char eflag;
W
wpan 已提交
96 97
} SFilterRange;

W
wpan 已提交
98 99 100 101
typedef struct SFilterColRange {  
  uint16_t idx;  //column field idx
  bool isNull;
  bool notNull;
W
wpan 已提交
102
  bool isRange;
W
wpan 已提交
103 104 105
  SFilterRange ra;
} SFilterColRange;

W
wpan 已提交
106
typedef bool (*rangeCompFunc) (const void *, const void *, const void *, const void *, __compar_fn_t);
W
wpan 已提交
107
typedef int32_t(*filter_desc_compare_func)(const void *, const void *);
W
wpan 已提交
108
typedef bool(*filter_exec_func)(void *, int32_t, int8_t**, SDataStatis *, int16_t);
W
wpan 已提交
109

W
wpan 已提交
110 111 112 113 114 115
typedef struct SFilterRangeCompare {
  int64_t s;
  int64_t e;
  rangeCompFunc func;
} SFilterRangeCompare;

W
wpan 已提交
116 117 118
typedef struct SFilterRangeNode {
  struct SFilterRangeNode*   prev;
  struct SFilterRangeNode*   next;
W
wpan 已提交
119 120 121 122
  union {
    SFilterRange ra;
    SFilterRangeCompare rc;
  };
W
wpan 已提交
123 124
} SFilterRangeNode;

W
wpan 已提交
125
typedef struct SFilterRangeCtx {
W
wpan 已提交
126 127
  int32_t type;
  int32_t options;
W
wpan 已提交
128 129 130 131
  int8_t  status;  
  bool isnull;
  bool notnull;
  bool isrange;
W
wpan 已提交
132
  int16_t colId;
W
wpan 已提交
133
  __compar_fn_t pCompareFunc;
W
wpan 已提交
134 135
  SFilterRangeNode *rf;        //freed
  SFilterRangeNode *rs;
W
wpan 已提交
136 137 138 139 140 141 142 143 144 145 146 147
} SFilterRangeCtx ;

typedef struct SFilterVarCtx {
  int32_t type;
  int32_t options;
  int8_t  status;  
  bool isnull;
  bool notnull;
  bool isrange;
  SHashObj *wild;
  SHashObj *value;
} SFilterVarCtx;
D
dapan1121 已提交
148 149

typedef struct SFilterField {
W
wpan 已提交
150
  uint16_t flag;
D
dapan1121 已提交
151 152 153 154 155
  void*    desc;
  void*    data;
} SFilterField;

typedef struct SFilterFields {
D
dapan1121 已提交
156
  uint16_t size;
D
dapan1121 已提交
157 158 159 160
  uint16_t num;
  SFilterField *fields;
} SFilterFields;

D
dapan1121 已提交
161 162 163 164 165
typedef struct SFilterFieldId {
  uint16_t type;
  uint16_t idx;
} SFilterFieldId;

D
dapan1121 已提交
166
typedef struct SFilterGroup {
W
wpan 已提交
167
  uint16_t  unitSize;
D
dapan1121 已提交
168 169 170 171 172
  uint16_t  unitNum;
  uint16_t *unitIdxs;
  uint8_t  *unitFlags;  // !unit result
} SFilterGroup;

W
wpan 已提交
173 174
typedef struct SFilterColInfo {
  uint8_t type;
W
wpan 已提交
175
  int32_t dataType;
W
wpan 已提交
176 177 178
  void   *info;
} SFilterColInfo;

W
wpan 已提交
179
typedef struct SFilterGroupCtx {
W
wpan 已提交
180 181 182
  uint16_t         colNum;
  uint16_t        *colIdx;
  SFilterColInfo  *colInfo;
W
wpan 已提交
183 184
} SFilterGroupCtx;

W
wpan 已提交
185 186 187 188
typedef struct SFilterColCtx {
  uint16_t  colIdx;
  void*     ctx;
} SFilterColCtx;
W
wpan 已提交
189

D
dapan1121 已提交
190
typedef struct SFilterCompare {
W
wpan 已提交
191
  uint8_t       type;
D
dapan1121 已提交
192
  uint8_t       optr;
W
wpan 已提交
193
  uint8_t       optr2;
D
dapan1121 已提交
194 195 196
} SFilterCompare;

typedef struct SFilterUnit {
D
dapan1121 已提交
197 198 199
  SFilterCompare  compare;
  SFilterFieldId  left;
  SFilterFieldId  right;
W
wpan 已提交
200
  SFilterFieldId  right2;
D
dapan1121 已提交
201 202
} SFilterUnit;

W
wpan 已提交
203 204 205 206
typedef struct SFilterComUnit {
  void *colData;
  void *valData;
  void *valData2;
W
wpan 已提交
207
  uint16_t colId;
W
wpan 已提交
208
  uint16_t dataSize;
W
wpan 已提交
209 210 211 212
  uint8_t dataType;
  uint8_t optr;
  int8_t func;
  int8_t rfunc;
W
wpan 已提交
213 214
} SFilterComUnit;

W
wpan 已提交
215 216 217 218 219
typedef struct SFilterPCtx {
  SHashObj *valHash;
  SHashObj *unitHash;
} SFilterPCtx;

D
dapan1121 已提交
220
typedef struct SFilterInfo {
W
wpan 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233
  uint32_t          options;
  uint32_t          status;  
  uint16_t          unitSize;
  uint16_t          unitNum;
  uint16_t          groupNum;
  uint16_t          colRangeNum;
  SFilterFields     fields[FLD_TYPE_MAX];
  SFilterGroup     *groups;
  uint16_t         *cgroups;
  SFilterUnit      *units;
  SFilterComUnit   *cunits;
  uint8_t          *unitRes;    // result
  uint8_t          *unitFlags;  // got result
W
wpan 已提交
234
  SFilterRangeCtx **colRange;
W
wpan 已提交
235 236 237 238 239
  filter_exec_func  func;          
  uint8_t           blkFlag;
  uint16_t          blkGroupNum;
  uint16_t         *blkUnits;
  int8_t           *blkUnitRes;
W
wpan 已提交
240 241
  
  SFilterPCtx       pctx;
D
dapan1121 已提交
242 243
} SFilterInfo;

W
wpan 已提交
244 245 246
#define COL_FIELD_SIZE (sizeof(SFilterField) + 2 * sizeof(int64_t))

#define FILTER_NO_MERGE_DATA_TYPE(t) ((t) == TSDB_DATA_TYPE_BINARY || (t) == TSDB_DATA_TYPE_NCHAR)
W
wpan 已提交
247
#define FILTER_NO_MERGE_OPTR(o) ((o) == TSDB_RELATION_ISNULL || (o) == TSDB_RELATION_NOTNULL || (o) == FILTER_DUMMY_EMPTY_OPTR)
W
wpan 已提交
248 249 250

#define MR_EMPTY_RES(ctx) (ctx->rs == NULL)

W
wpan 已提交
251 252
#define SET_AND_OPTR(ctx, o) do {if (o == TSDB_RELATION_ISNULL) { (ctx)->isnull = true; } else if (o == TSDB_RELATION_NOTNULL) { if (!(ctx)->isrange) { (ctx)->notnull = true; } } else if (o != FILTER_DUMMY_EMPTY_OPTR) { (ctx)->isrange = true; (ctx)->notnull = false; }  } while (0)
#define SET_OR_OPTR(ctx,o) do {if (o == TSDB_RELATION_ISNULL) { (ctx)->isnull = true; } else if (o == TSDB_RELATION_NOTNULL) { (ctx)->notnull = true; (ctx)->isrange = false; } else if (o != FILTER_DUMMY_EMPTY_OPTR) { if (!(ctx)->notnull) { (ctx)->isrange = true; } } } while (0)
W
wpan 已提交
253 254
#define CHK_OR_OPTR(ctx)  ((ctx)->isnull == true && (ctx)->notnull == true)
#define CHK_AND_OPTR(ctx)  ((ctx)->isnull == true && (((ctx)->notnull == true) || ((ctx)->isrange == true)))
W
wpan 已提交
255 256 257 258 259


#define FILTER_GET_FLAG(st, f) (st & f)
#define FILTER_SET_FLAG(st, f) st |= (f)
#define FILTER_CLR_FLAG(st, f) st &= (~f)
W
wpan 已提交
260

W
wpan 已提交
261
#define SIMPLE_COPY_VALUES(dst, src) *((int64_t *)dst) = *((int64_t *)src)
W
wpan 已提交
262
#define FILTER_PACKAGE_UNIT_HASH_KEY(v, optr, idx1, idx2) do { char *_t = (char *)v; _t[0] = optr; *(uint16_t *)(_t + 1) = idx1; *(uint16_t *)(_t + 3) = idx2; } while (0)
W
wpan 已提交
263
#define FILTER_GREATER(cr,sflag,eflag) ((cr > 0) || ((cr == 0) && (FILTER_GET_FLAG(sflag,RANGE_FLG_EXCLUDE) || FILTER_GET_FLAG(eflag,RANGE_FLG_EXCLUDE))))
W
wpan 已提交
264 265 266 267
#define FILTER_COPY_RA(dst, src) do { (dst)->sflag = (src)->sflag; (dst)->eflag = (src)->eflag; (dst)->s = (src)->s; (dst)->e = (src)->e; } while (0)

#define RESET_RANGE(ctx, r) do { (r)->next = (ctx)->rf; (ctx)->rf = r; } while (0)
#define FREE_RANGE(ctx, r) do { if ((r)->prev) { (r)->prev->next = (r)->next; } else { (ctx)->rs = (r)->next;} if ((r)->next) { (r)->next->prev = (r)->prev; } RESET_RANGE(ctx, r); } while (0)
W
wpan 已提交
268
#define FREE_FROM_RANGE(ctx, r) do { SFilterRangeNode *_r = r; if ((_r)->prev) { (_r)->prev->next = NULL; } else { (ctx)->rs = NULL;} while (_r) {SFilterRangeNode *n = (_r)->next; RESET_RANGE(ctx, _r); _r = n; } } while (0)
W
wpan 已提交
269 270
#define INSERT_RANGE(ctx, r, ra) do { SFilterRangeNode *n = filterNewRange(ctx, ra); n->prev = (r)->prev; if ((r)->prev) { (r)->prev->next = n; } else { (ctx)->rs = n; } (r)->prev = n; n->next = r; } while (0)
#define APPEND_RANGE(ctx, r, ra) do { SFilterRangeNode *n = filterNewRange(ctx, ra); n->prev = (r); if (r) { (r)->next = n; } else { (ctx)->rs = n; } } while (0)
W
wpan 已提交
271

D
dapan1121 已提交
272 273
#define ERR_RET(c) do { int32_t _code = c; if (_code != TSDB_CODE_SUCCESS) { return _code; } } while (0)
#define ERR_LRET(c,...) do { int32_t _code = c; if (_code != TSDB_CODE_SUCCESS) { qError(__VA_ARGS__); return _code; } } while (0)
W
wpan 已提交
274
#define ERR_JRET(c) do { code = c; if (code != TSDB_CODE_SUCCESS) { goto _return; } } while (0)
D
dapan1121 已提交
275 276 277

#define CHK_RETV(c) do { if (c) { return; } } while (0)
#define CHK_RET(c, r) do { if (c) { return r; } } while (0)
W
wpan 已提交
278
#define CHK_JMP(c) do { if (c) { goto _return; } } while (0)
D
dapan1121 已提交
279
#define CHK_LRETV(c,...) do { if (c) { qError(__VA_ARGS__); return; } } while (0)
W
wpan 已提交
280
#define CHK_LRET(c, r,...) do { if (c) { if (r) {qError(__VA_ARGS__); } else { qDebug(__VA_ARGS__); } return r; } } while (0)
D
dapan1121 已提交
281

D
dapan1121 已提交
282
#define FILTER_GET_FIELD(i, id) (&((i)->fields[(id).type].fields[(id).idx]))
W
wpan 已提交
283
#define FILTER_GET_COL_FIELD(i, idx) (&((i)->fields[FLD_TYPE_COLUMN].fields[idx]))
D
dapan1121 已提交
284
#define FILTER_GET_COL_FIELD_TYPE(fi) (((SSchema *)((fi)->desc))->type)
W
wpan 已提交
285
#define FILTER_GET_COL_FIELD_SIZE(fi) (((SSchema *)((fi)->desc))->bytes)
W
wpan 已提交
286
#define FILTER_GET_COL_FIELD_ID(fi) (((SSchema *)((fi)->desc))->colId)
W
wpan 已提交
287
#define FILTER_GET_COL_FIELD_DESC(fi) ((SSchema *)((fi)->desc))
W
wpan 已提交
288
#define FILTER_GET_COL_FIELD_DATA(fi, ri) ((char *)(fi)->data + ((SSchema *)((fi)->desc))->bytes * (ri))
D
dapan1121 已提交
289
#define FILTER_GET_VAL_FIELD_TYPE(fi) (((tVariant *)((fi)->desc))->nType)
W
wpan 已提交
290
#define FILTER_GET_VAL_FIELD_DATA(fi) ((char *)(fi)->data)
W
wpan 已提交
291
#define FILTER_GET_TYPE(fl) ((fl) & FLD_TYPE_MAX)
D
dapan1121 已提交
292

W
wpan 已提交
293
#define FILTER_GROUP_UNIT(i, g, uid) ((i)->units + (g)->unitIdxs[uid])
W
wpan 已提交
294 295
#define FILTER_UNIT_LEFT_FIELD(i, u) FILTER_GET_FIELD(i, (u)->left)
#define FILTER_UNIT_RIGHT_FIELD(i, u) FILTER_GET_FIELD(i, (u)->right)
W
wpan 已提交
296
#define FILTER_UNIT_RIGHT2_FIELD(i, u) FILTER_GET_FIELD(i, (u)->right2)
W
wpan 已提交
297 298 299
#define FILTER_UNIT_DATA_TYPE(u) ((u)->compare.type)
#define FILTER_UNIT_COL_DESC(i, u) FILTER_GET_COL_FIELD_DESC(FILTER_UNIT_LEFT_FIELD(i, u))
#define FILTER_UNIT_COL_DATA(i, u, ri) FILTER_GET_COL_FIELD_DATA(FILTER_UNIT_LEFT_FIELD(i, u), ri)
W
wpan 已提交
300
#define FILTER_UNIT_COL_SIZE(i, u) FILTER_GET_COL_FIELD_SIZE(FILTER_UNIT_LEFT_FIELD(i, u))
W
wpan 已提交
301
#define FILTER_UNIT_COL_ID(i, u) FILTER_GET_COL_FIELD_ID(FILTER_UNIT_LEFT_FIELD(i, u))
W
wpan 已提交
302
#define FILTER_UNIT_VAL_DATA(i, u) FILTER_GET_VAL_FIELD_DATA(FILTER_UNIT_RIGHT_FIELD(i, u))
W
wpan 已提交
303 304
#define FILTER_UNIT_COL_IDX(u) ((u)->left.idx)
#define FILTER_UNIT_OPTR(u) ((u)->compare.optr)
W
wpan 已提交
305
#define FILTER_UNIT_COMP_FUNC(u) ((u)->compare.func)
D
dapan1121 已提交
306

D
dapan1121 已提交
307 308 309 310 311 312
#define FILTER_UNIT_CLR_F(i) memset((i)->unitFlags, 0, (i)->unitNum * sizeof(*info->unitFlags)) 
#define FILTER_UNIT_SET_F(i, idx) (i)->unitFlags[idx] = 1
#define FILTER_UNIT_GET_F(i, idx) ((i)->unitFlags[idx])
#define FILTER_UNIT_GET_R(i, idx) ((i)->unitRes[idx])
#define FILTER_UNIT_SET_R(i, idx, v) (i)->unitRes[idx] = (v)

W
wpan 已提交
313
#define FILTER_PUSH_UNIT(colInfo, u) do { (colInfo).type = RANGE_TYPE_UNIT; (colInfo).dataType = FILTER_UNIT_DATA_TYPE(u);taosArrayPush((SArray *)((colInfo).info), &u);} while (0)
W
wpan 已提交
314
#define FILTER_PUSH_VAR_HASH(colInfo, ha) do { (colInfo).type = RANGE_TYPE_VAR_HASH; (colInfo).info = ha;} while (0)
W
wpan 已提交
315 316 317 318 319 320
#define FILTER_PUSH_CTX(colInfo, ctx) do { (colInfo).type = RANGE_TYPE_MR_CTX; (colInfo).info = ctx;} while (0)

#define FILTER_COPY_IDX(dst, src, n) do { *(dst) = malloc(sizeof(uint16_t) * n); memcpy(*(dst), src, sizeof(uint16_t) * n);} while (0)

#define FILTER_ADD_CTX_TO_GRES(gres, idx, ctx) do { if ((gres)->colCtxs == NULL) { (gres)->colCtxs = taosArrayInit(gres->colNum, sizeof(SFilterColCtx)); } SFilterColCtx cCtx = {idx, ctx}; taosArrayPush((gres)->colCtxs, &cCtx); } while (0) 

W
wpan 已提交
321 322 323

#define FILTER_ALL_RES(i) FILTER_GET_FLAG((i)->status, FI_STATUS_ALL)
#define FILTER_EMPTY_RES(i) FILTER_GET_FLAG((i)->status, FI_STATUS_EMPTY)
D
dapan1121 已提交
324 325


W
wpan 已提交
326
extern int32_t filterInitFromTree(tExprNode* tree, SFilterInfo **pinfo, uint32_t options);
W
wpan 已提交
327 328
extern bool filterExecute(SFilterInfo *info, int32_t numOfRows, int8_t** p, SDataStatis *statis, int16_t numOfCols);
extern int32_t filterSetColFieldData(SFilterInfo *info, int32_t numOfCols, SArray* pDataBlock);
W
wpan 已提交
329
extern int32_t filterGetTimeRange(SFilterInfo *info, STimeWindow *win);
330 331 332
extern int32_t filterConverNcharColumns(SFilterInfo* pFilterInfo, int32_t rows, bool *gotNchar);
extern int32_t filterFreeNcharColumns(SFilterInfo* pFilterInfo);
extern void filterFreeInfo(SFilterInfo *info);
W
wpan 已提交
333
extern bool filterRangeExecute(SFilterInfo *info, SDataStatis *pDataStatis, int32_t numOfCols, int32_t numOfRows);
334

D
dapan1121 已提交
335 336 337 338 339
#ifdef __cplusplus
}
#endif

#endif  // TDENGINE_QFILTER_H