diff --git a/src/client/inc/tscUtil.h b/src/client/inc/tscUtil.h index 3fde949947e6f277305ab2439e06757644ab34d6..30663df4746f3cb0be23ac4ade130ddba4d73c3d 100644 --- a/src/client/inc/tscUtil.h +++ b/src/client/inc/tscUtil.h @@ -29,9 +29,9 @@ extern "C" { #include "tsclient.h" #include "tsdb.h" -#define UTIL_METER_IS_METRIC(metaInfo) \ +#define UTIL_METER_IS_SUPERTABLE(metaInfo) \ (((metaInfo)->pMeterMeta != NULL) && ((metaInfo)->pMeterMeta->meterType == TSDB_METER_METRIC)) -#define UTIL_METER_IS_NOMRAL_METER(metaInfo) (!(UTIL_METER_IS_METRIC(metaInfo))) +#define UTIL_METER_IS_NOMRAL_METER(metaInfo) (!(UTIL_METER_IS_SUPERTABLE(metaInfo))) #define UTIL_METER_IS_CREATE_FROM_METRIC(metaInfo) \ (((metaInfo)->pMeterMeta != NULL) && ((metaInfo)->pMeterMeta->meterType == TSDB_METER_MTABLE)) @@ -95,23 +95,23 @@ SMeterSidExtInfo* tscGetMeterSidInfo(SVnodeSidList* pSidList, int32_t idx); * @param pSql sql object * @return */ -bool tscIsPointInterpQuery(SSqlCmd* pCmd); -bool tscIsTWAQuery(SSqlCmd* pCmd); -bool tscProjectionQueryOnMetric(SSqlCmd* pCmd); -bool tscProjectionQueryOnTable(SSqlCmd* pCmd); +bool tscIsPointInterpQuery(SQueryInfo* pQueryInfo); +bool tscIsTWAQuery(SQueryInfo* pQueryInfo); +bool tscProjectionQueryOnMetric(SSqlCmd* pCmd, int32_t subClauseIndex); +bool tscProjectionQueryOnTable(SQueryInfo* pQueryInfo); bool tscIsTwoStageMergeMetricQuery(SSqlCmd* pCmd); bool tscQueryOnMetric(SSqlCmd* pCmd); -bool tscQueryMetricTags(SSqlCmd* pCmd); +bool tscQueryMetricTags(SQueryInfo* pQueryInfo); bool tscIsSelectivityWithTagQuery(SSqlCmd* pCmd); -void tscAddSpecialColumnForSelect(SSqlCmd* pCmd, int32_t outputColIndex, int16_t functionId, SColumnIndex* pIndex, +void tscAddSpecialColumnForSelect(SQueryInfo* pQueryInfo, int32_t outputColIndex, int16_t functionId, SColumnIndex* pIndex, SSchema* pColSchema, int16_t isTag); -void addRequiredTagColumn(SSqlCmd* pCmd, int32_t tagColIndex, int32_t tableIndex); +void addRequiredTagColumn(SQueryInfo* pQueryInfo, int32_t tagColIndex, int32_t tableIndex); -int32_t setMeterID(SSqlObj* pSql, SSQLToken* pzTableName, int32_t tableIndex); -void tscClearInterpInfo(SSqlCmd* pCmd); +int32_t setMeterID(SSqlObj* pSql, int32_t subClauseIndex, SSQLToken* pzTableName, int32_t tableIndex); +void tscClearInterpInfo(SQueryInfo* pQueryInfo); bool tscIsInsertOrImportData(char* sqlstr); @@ -125,30 +125,30 @@ void tscFieldInfoSetValFromField(SFieldInfo* pFieldInfo, int32_t index, TAOS_FIE void tscFieldInfoSetValue(SFieldInfo* pFieldInfo, int32_t index, int8_t type, const char* name, int16_t bytes); void tscFieldInfoUpdateVisible(SFieldInfo* pFieldInfo, int32_t index, bool visible); -void tscFieldInfoCalOffset(SSqlCmd* pCmd); -void tscFieldInfoUpdateOffset(SSqlCmd* pCmd); +void tscFieldInfoCalOffset(SQueryInfo* pQueryInfo); +void tscFieldInfoUpdateOffset(SQueryInfo* pQueryInfo); void tscFieldInfoCopy(SFieldInfo* src, SFieldInfo* dst, const int32_t* indexList, int32_t size); void tscFieldInfoCopyAll(SFieldInfo* src, SFieldInfo* dst); -TAOS_FIELD* tscFieldInfoGetField(SSqlCmd* pCmd, int32_t index); -int16_t tscFieldInfoGetOffset(SSqlCmd* pCmd, int32_t index); -int32_t tscGetResRowLength(SSqlCmd* pCmd); +TAOS_FIELD* tscFieldInfoGetField(SQueryInfo* pQueryInfo, int32_t index); +int16_t tscFieldInfoGetOffset(SQueryInfo* pQueryInfo, int32_t index); +int32_t tscGetResRowLength(SQueryInfo* pQueryInfo); void tscClearFieldInfo(SFieldInfo* pFieldInfo); -int32_t tscNumOfFields(SSqlCmd* pCmd); +int32_t tscNumOfFields(SQueryInfo* pQueryInfo); void addExprParams(SSqlExpr* pExpr, char* argument, int32_t type, int32_t bytes, int16_t tableIndex); -SSqlExpr* tscSqlExprInsert(SSqlCmd* pCmd, int32_t index, int16_t functionId, SColumnIndex* pColIndex, int16_t type, +SSqlExpr* tscSqlExprInsert(SQueryInfo* pQueryInfo, int32_t index, int16_t functionId, SColumnIndex* pColIndex, int16_t type, int16_t size, int16_t interSize); -SSqlExpr* tscSqlExprInsertEmpty(SSqlCmd* pCmd, int32_t index, int16_t functionId); +SSqlExpr* tscSqlExprInsertEmpty(SQueryInfo* pQueryInfo, int32_t index, int16_t functionId); -SSqlExpr* tscSqlExprUpdate(SSqlCmd* pCmd, int32_t index, int16_t functionId, int16_t srcColumnIndex, int16_t type, +SSqlExpr* tscSqlExprUpdate(SQueryInfo* pQueryInfo, int32_t index, int16_t functionId, int16_t srcColumnIndex, int16_t type, int16_t size); -SSqlExpr* tscSqlExprGet(SSqlCmd* pCmd, int32_t index); +SSqlExpr* tscSqlExprGet(SQueryInfo* pQueryInfo, int32_t index); void tscSqlExprCopy(SSqlExprInfo* dst, const SSqlExprInfo* src, uint64_t uid); -SColumnBase* tscColumnBaseInfoInsert(SSqlCmd* pCmd, SColumnIndex* colIndex); +SColumnBase* tscColumnBaseInfoInsert(SQueryInfo* pQueryInfo, SColumnIndex* colIndex); void tscColumnFilterInfoCopy(SColumnFilterInfo* dst, const SColumnFilterInfo* src); void tscColumnBaseCopy(SColumnBase* dst, const SColumnBase* src); @@ -172,31 +172,36 @@ void tsSetMetricQueryCond(STagCond* pTagCond, uint64_t uid, const char* str); void tscTagCondCopy(STagCond* dest, const STagCond* src); void tscTagCondRelease(STagCond* pCond); -void tscGetSrcColumnInfo(SSrcColumnInfo* pColInfo, SSqlCmd* pCmd); +void tscGetSrcColumnInfo(SSrcColumnInfo* pColInfo, SQueryInfo* pQueryInfo); void tscSetFreeHeatBeat(STscObj* pObj); bool tscShouldFreeHeatBeat(SSqlObj* pHb); void tscCleanSqlCmd(SSqlCmd* pCmd); bool tscShouldFreeAsyncSqlObj(SSqlObj* pSql); -void tscRemoveAllMeterMetaInfo(SSqlCmd* pCmd, bool removeFromCache); -SMeterMetaInfo* tscGetMeterMetaInfo(SSqlCmd* pCmd, int32_t index); -SMeterMetaInfo* tscGetMeterMetaInfoByUid(SSqlCmd* pCmd, uint64_t uid, int32_t* index); +void tscRemoveAllMeterMetaInfo(SQueryInfo* pQueryInfo, const char* address, bool removeFromCache); +SMeterMetaInfo* tscGetMeterMetaInfo(SSqlCmd *pCmd, int32_t subClauseIndex, int32_t tableIndex); +SMeterMetaInfo* tscGetMeterMetaInfoFromQueryInfo(SQueryInfo *pQueryInfo, int32_t tableIndex); + +SQueryInfo *tscGetQueryInfoDetail(SSqlCmd* pCmd, int32_t subClauseIndex); + +SMeterMetaInfo* tscGetMeterMetaInfoByUid(SQueryInfo* pQueryInfo, int32_t subClauseIndex, uint64_t uid, int32_t* index); void tscClearMeterMetaInfo(SMeterMetaInfo* pMeterMetaInfo, bool removeFromCache); -SMeterMetaInfo* tscAddMeterMetaInfo(SSqlCmd* pCmd, const char* name, SMeterMeta* pMeterMeta, SMetricMeta* pMetricMeta, +SMeterMetaInfo* tscAddMeterMetaInfo(SSqlCmd* pCmd, int32_t subClauseIndex, const char* name, SMeterMeta* pMeterMeta, SMetricMeta* pMetricMeta, int16_t numOfTags, int16_t* tags); -SMeterMetaInfo* tscAddEmptyMeterMetaInfo(SSqlCmd* pCmd); -int32_t tscAddQueryInfo(SSqlCmd *pCmd); +SMeterMetaInfo* tscAddEmptyMeterMetaInfo(SSqlCmd* pCmd, int32_t subClauseIndex); +int32_t tscAddSubqueryInfo(SSqlCmd *pCmd); +void tscFreeSubqueryInfo(SSqlCmd* pCmd); -void tscGetMetricMetaCacheKey(SSqlCmd* pCmd, char* keyStr, uint64_t uid); +void tscGetMetricMetaCacheKey(SSqlCmd* pCmd, int32_t subClauseIndex, char* keyStr, uint64_t uid); int tscGetMetricMeta(SSqlObj* pSql); int tscGetMeterMeta(SSqlObj* pSql, char* meterId, int32_t tableIndex); int tscGetMeterMetaEx(SSqlObj* pSql, char* meterId, bool createIfNotExists); void tscResetForNextRetrieve(SSqlRes* pRes); -void tscAddTimestampColumn(SSqlCmd* pCmd, int16_t functionId, int16_t tableIndex); +void tscAddTimestampColumn(SQueryInfo* pQueryInfo, int16_t functionId, int16_t tableIndex); void tscDoQuery(SSqlObj* pSql); /** @@ -217,9 +222,9 @@ void tscDoQuery(SSqlObj* pSql); * @return */ SSqlObj* createSubqueryObj(SSqlObj* pSql, int16_t tableIndex, void (*fp)(), void* param, SSqlObj* pPrevSql); -void addGroupInfoForSubquery(SSqlObj* pParentObj, SSqlObj* pSql, int32_t tableIndex); +void addGroupInfoForSubquery(SSqlObj* pParentObj, SSqlObj* pSql, int32_t subClauseIndex, int32_t tableIndex); -void doAddGroupColumnForSubquery(SSqlCmd* pCmd, int32_t tagIndex); +void doAddGroupColumnForSubquery(SQueryInfo* pQueryInfo, int32_t tagIndex); int16_t tscGetJoinTagColIndexByUid(STagCond* pTagCond, uint64_t uid); @@ -228,7 +233,7 @@ TAOS* taos_connect_a(char* ip, char* user, char* pass, char* db, uint16_t port, void sortRemoveDuplicates(STableDataBlocks* dataBuf); -void tscPrintSelectClause(SSqlCmd* pCmd); +void tscPrintSelectClause(SSqlCmd* pCmd, int32_t subClauseIndex); #ifdef __cplusplus } diff --git a/src/client/inc/tsclient.h b/src/client/inc/tsclient.h index cd43e4528dc7e2e977e4a169150852891abf717a..a8e8ac47ef0ffd4942bce3556f65d3cd8056fa20 100644 --- a/src/client/inc/tsclient.h +++ b/src/client/inc/tsclient.h @@ -39,9 +39,9 @@ extern "C" { #include "tsqlfunction.h" #include "tutil.h" -#define TSC_GET_RESPTR_BASE(res, cmd, col, ord) \ - ((res->data + tscFieldInfoGetOffset(cmd, col) * res->numOfRows) + \ - (1 - ord.order) * (res->numOfRows - 1) * tscFieldInfoGetField(cmd, col)->bytes) +#define TSC_GET_RESPTR_BASE(res, _queryinfo, col, ord) \ + ((res->data + tscFieldInfoGetOffset(_queryinfo, col) * res->numOfRows) + \ + (1 - ord.order) * (res->numOfRows - 1) * tscFieldInfoGetField(_queryinfo, col)->bytes) // forward declaration struct SSqlInfo; @@ -208,6 +208,7 @@ typedef struct SDataBlockList { } SDataBlockList; typedef struct SQueryInfo { + uint16_t type; // query type char intervalTimeUnit; int64_t etime, stime; @@ -221,16 +222,18 @@ typedef struct SQueryInfo { SLimitVal limit; SLimitVal slimit; STagCond tagCond; + SOrderVal order; int16_t interpoType; // interpolate type int16_t numOfTables; SMeterMetaInfo **pMeterInfo; struct STSBuf * tsBuf; // todo use dynamic allocated memory for defaultVal int64_t defaultVal[TSDB_MAX_COLUMNS]; // default value for interpolation + char* msg; // pointer to the pCmd->payload to keep error message temporarily } SQueryInfo; typedef struct { - SOrderVal order; +// SOrderVal order; int command; int count; // TODO refactor @@ -241,7 +244,6 @@ typedef struct { int8_t isInsertFromFile; // load data from file or not uint8_t msgType; - uint16_t type; // query type /* * use to keep short request msg and error msg, in such case, SSqlCmd->payload == SSqlCmd->ext; @@ -255,8 +257,8 @@ typedef struct { short numOfCols; int64_t globalLimit; - SQueryInfo *pQueryInfo; - int32_t numOfQueries; + SQueryInfo **pQueryInfo; + int32_t numOfClause; // char intervalTimeUnit; // int64_t etime, stime; @@ -421,12 +423,12 @@ int taos_retrieve(TAOS_RES *res); * transfer function for metric query in stream computing, the function need to be change * before send query message to vnode */ -int32_t tscTansformSQLFunctionForMetricQuery(SSqlCmd *pCmd); -void tscRestoreSQLFunctionForMetricQuery(SSqlCmd *pCmd); +int32_t tscTansformSQLFunctionForMetricQuery(SQueryInfo* pQueryInfo); +void tscRestoreSQLFunctionForMetricQuery(SQueryInfo* pQueryInfo); void tscClearSqlMetaInfoForce(SSqlCmd *pCmd); -int32_t tscCreateResPointerInfo(SSqlCmd *pCmd, SSqlRes *pRes); +int32_t tscCreateResPointerInfo(SQueryInfo* pQueryInfo, SSqlRes *pRes); void tscDestroyResPointerInfo(SSqlRes *pRes); void tscFreeSqlCmdData(SSqlCmd *pCmd); @@ -454,6 +456,8 @@ void tscInitResObjForLocalQuery(SSqlObj *pObj, int32_t numOfRes, int32_t rowL bool tscIsUpdateQuery(STscObj *pObj); bool tscHasReachLimitation(SSqlObj* pSql); +char* tscGetErrorMsgPayload(SSqlCmd* pCmd); + int32_t tscInvalidSQLErrMsg(char *msg, const char *additionalInfo, const char *sql); // transfer SSqlInfo to SqlCmd struct diff --git a/src/client/src/sql.c b/src/client/src/sql.c index 73576a14fb4a63e4f92b61679bfed7a6cc70d269..331c27a474668644108f57ff4a38f43d1f15ff11 100644 --- a/src/client/src/sql.c +++ b/src/client/src/sql.c @@ -122,17 +122,17 @@ typedef union { #define ParseARG_FETCH SSqlInfo* pInfo = yypParser->pInfo #define ParseARG_STORE yypParser->pInfo = pInfo #define YYFALLBACK 1 -#define YYNSTATE 241 -#define YYNRULE 213 +#define YYNSTATE 246 +#define YYNRULE 215 #define YYNTOKEN 197 -#define YY_MAX_SHIFT 240 -#define YY_MIN_SHIFTREDUCE 391 -#define YY_MAX_SHIFTREDUCE 603 -#define YY_ERROR_ACTION 604 -#define YY_ACCEPT_ACTION 605 -#define YY_NO_ACTION 606 -#define YY_MIN_REDUCE 607 -#define YY_MAX_REDUCE 819 +#define YY_MAX_SHIFT 245 +#define YY_MIN_SHIFTREDUCE 398 +#define YY_MAX_SHIFTREDUCE 612 +#define YY_ERROR_ACTION 613 +#define YY_ACCEPT_ACTION 614 +#define YY_NO_ACTION 615 +#define YY_MIN_REDUCE 616 +#define YY_MAX_REDUCE 830 /************* End control #defines *******************************************/ /* Define the yytestcase() macro to be a no-op if is not already defined @@ -198,115 +198,116 @@ typedef union { ** yy_default[] Default action for each state. ** *********** Begin parsing tables **********************************************/ -#define YY_ACTTAB_COUNT (516) +#define YY_ACTTAB_COUNT (528) static const YYACTIONTYPE yy_action[] = { - /* 0 */ 738, 431, 129, 147, 238, 10, 605, 240, 129, 432, - /* 10 */ 129, 151, 807, 40, 42, 19, 34, 35, 806, 150, - /* 20 */ 807, 28, 128, 431, 194, 38, 36, 39, 37, 133, - /* 30 */ 490, 432, 93, 33, 32, 97, 803, 31, 30, 29, - /* 40 */ 40, 42, 729, 34, 35, 148, 802, 159, 28, 715, - /* 50 */ 735, 194, 38, 36, 39, 37, 181, 97, 219, 218, - /* 60 */ 33, 32, 158, 718, 31, 30, 29, 392, 393, 394, - /* 70 */ 395, 396, 397, 398, 399, 400, 401, 402, 403, 239, - /* 80 */ 718, 40, 42, 182, 34, 35, 209, 230, 191, 28, - /* 90 */ 56, 19, 194, 38, 36, 39, 37, 31, 30, 29, - /* 100 */ 54, 33, 32, 801, 718, 31, 30, 29, 42, 517, - /* 110 */ 34, 35, 762, 16, 189, 28, 19, 19, 194, 38, - /* 120 */ 36, 39, 37, 160, 559, 715, 221, 33, 32, 145, - /* 130 */ 170, 31, 30, 29, 232, 34, 35, 178, 7, 175, - /* 140 */ 28, 59, 107, 194, 38, 36, 39, 37, 217, 222, - /* 150 */ 715, 715, 33, 32, 146, 716, 31, 30, 29, 15, - /* 160 */ 208, 231, 207, 206, 205, 204, 203, 202, 201, 200, - /* 170 */ 700, 19, 689, 690, 691, 692, 693, 694, 695, 696, - /* 180 */ 697, 698, 699, 155, 572, 431, 703, 563, 702, 566, - /* 190 */ 565, 569, 568, 432, 38, 36, 39, 37, 237, 236, - /* 200 */ 414, 11, 33, 32, 232, 714, 31, 30, 29, 110, - /* 210 */ 111, 213, 62, 65, 134, 152, 153, 155, 572, 193, - /* 220 */ 142, 563, 97, 566, 58, 569, 84, 83, 136, 163, - /* 230 */ 506, 231, 97, 503, 141, 504, 26, 505, 71, 75, - /* 240 */ 233, 82, 74, 124, 122, 234, 86, 85, 77, 152, - /* 250 */ 153, 155, 572, 520, 564, 563, 567, 566, 709, 569, - /* 260 */ 25, 161, 162, 46, 33, 32, 73, 154, 31, 30, - /* 270 */ 29, 645, 230, 561, 120, 540, 541, 761, 41, 180, - /* 280 */ 135, 47, 654, 152, 153, 120, 144, 57, 571, 646, - /* 290 */ 164, 49, 120, 216, 215, 531, 532, 589, 573, 44, - /* 300 */ 13, 12, 12, 570, 2, 137, 496, 50, 138, 562, - /* 310 */ 44, 495, 41, 139, 198, 20, 717, 510, 20, 511, - /* 320 */ 140, 508, 571, 509, 81, 80, 131, 70, 69, 127, - /* 330 */ 507, 9, 8, 132, 816, 130, 772, 570, 771, 156, - /* 340 */ 768, 767, 157, 220, 737, 754, 41, 94, 753, 108, - /* 350 */ 109, 106, 656, 199, 125, 23, 571, 212, 25, 214, - /* 360 */ 815, 67, 814, 730, 88, 812, 112, 527, 674, 24, - /* 370 */ 183, 570, 21, 126, 643, 187, 76, 641, 78, 639, - /* 380 */ 51, 638, 165, 728, 121, 48, 98, 636, 635, 634, - /* 390 */ 632, 43, 625, 123, 629, 99, 627, 192, 741, 742, - /* 400 */ 190, 188, 186, 184, 27, 755, 211, 72, 223, 224, - /* 410 */ 225, 226, 227, 228, 100, 196, 229, 52, 235, 603, - /* 420 */ 166, 167, 143, 169, 60, 63, 168, 637, 602, 171, - /* 430 */ 87, 172, 173, 174, 115, 631, 114, 675, 113, 116, - /* 440 */ 117, 119, 118, 89, 1, 713, 103, 101, 22, 102, - /* 450 */ 104, 105, 601, 176, 177, 594, 179, 92, 55, 528, - /* 460 */ 95, 149, 185, 4, 533, 5, 96, 61, 3, 17, - /* 470 */ 574, 471, 18, 14, 195, 6, 197, 470, 469, 468, - /* 480 */ 467, 466, 465, 464, 462, 44, 435, 64, 437, 66, - /* 490 */ 20, 210, 492, 491, 489, 53, 456, 45, 454, 446, - /* 500 */ 68, 452, 448, 450, 444, 442, 463, 461, 79, 417, - /* 510 */ 433, 90, 407, 91, 405, 607, + /* 0 */ 749, 438, 131, 149, 243, 10, 614, 245, 131, 439, + /* 10 */ 131, 154, 818, 41, 43, 20, 35, 36, 817, 153, + /* 20 */ 818, 29, 738, 438, 199, 39, 37, 40, 38, 130, + /* 30 */ 497, 439, 95, 34, 33, 99, 150, 32, 31, 30, + /* 40 */ 41, 43, 738, 35, 36, 151, 135, 162, 29, 724, + /* 50 */ 746, 199, 39, 37, 40, 38, 184, 99, 224, 223, + /* 60 */ 34, 33, 161, 727, 32, 31, 30, 399, 400, 401, + /* 70 */ 402, 403, 404, 405, 406, 407, 408, 409, 410, 244, + /* 80 */ 727, 41, 43, 187, 35, 36, 214, 814, 196, 29, + /* 90 */ 58, 20, 199, 39, 37, 40, 38, 32, 31, 30, + /* 100 */ 56, 34, 33, 75, 727, 32, 31, 30, 43, 235, + /* 110 */ 35, 36, 773, 235, 194, 29, 20, 20, 199, 39, + /* 120 */ 37, 40, 38, 163, 568, 724, 226, 34, 33, 438, + /* 130 */ 166, 32, 31, 30, 237, 35, 36, 439, 7, 813, + /* 140 */ 29, 61, 109, 199, 39, 37, 40, 38, 222, 227, + /* 150 */ 724, 724, 34, 33, 50, 725, 32, 31, 30, 15, + /* 160 */ 213, 236, 212, 211, 210, 209, 208, 207, 206, 205, + /* 170 */ 709, 51, 698, 699, 700, 701, 702, 703, 704, 705, + /* 180 */ 706, 707, 708, 158, 581, 11, 812, 572, 99, 575, + /* 190 */ 99, 578, 167, 158, 581, 221, 220, 572, 16, 575, + /* 200 */ 20, 578, 34, 33, 144, 26, 32, 31, 30, 237, + /* 210 */ 86, 85, 138, 173, 654, 155, 156, 122, 143, 198, + /* 220 */ 181, 712, 178, 711, 147, 155, 156, 158, 581, 529, + /* 230 */ 60, 572, 148, 575, 723, 578, 236, 16, 39, 37, + /* 240 */ 40, 38, 27, 772, 26, 59, 34, 33, 549, 550, + /* 250 */ 32, 31, 30, 136, 112, 113, 218, 64, 67, 155, + /* 260 */ 156, 94, 513, 663, 183, 510, 122, 511, 26, 512, + /* 270 */ 521, 146, 126, 124, 239, 88, 87, 186, 42, 157, + /* 280 */ 73, 77, 238, 84, 76, 570, 526, 137, 42, 580, + /* 290 */ 79, 17, 655, 164, 165, 122, 242, 241, 421, 580, + /* 300 */ 47, 540, 541, 598, 579, 45, 13, 12, 582, 574, + /* 310 */ 783, 577, 12, 573, 579, 576, 2, 72, 71, 48, + /* 320 */ 503, 571, 42, 139, 45, 502, 203, 9, 8, 21, + /* 330 */ 21, 140, 517, 580, 518, 515, 141, 516, 83, 82, + /* 340 */ 142, 133, 129, 134, 726, 132, 827, 782, 579, 159, + /* 350 */ 108, 779, 778, 160, 748, 718, 225, 96, 740, 110, + /* 360 */ 765, 764, 514, 111, 665, 204, 26, 127, 24, 217, + /* 370 */ 185, 219, 826, 69, 90, 825, 823, 114, 683, 25, + /* 380 */ 22, 128, 652, 536, 78, 650, 188, 80, 648, 647, + /* 390 */ 168, 192, 52, 737, 123, 645, 644, 643, 641, 634, + /* 400 */ 49, 125, 44, 638, 636, 100, 101, 197, 195, 752, + /* 410 */ 193, 753, 766, 28, 191, 189, 216, 74, 228, 229, + /* 420 */ 230, 231, 232, 201, 233, 234, 53, 240, 612, 169, + /* 430 */ 170, 62, 145, 65, 171, 172, 611, 646, 174, 175, + /* 440 */ 177, 176, 610, 89, 117, 640, 116, 684, 115, 119, + /* 450 */ 118, 120, 722, 121, 91, 106, 102, 103, 23, 104, + /* 460 */ 105, 107, 1, 179, 180, 603, 182, 186, 523, 55, + /* 470 */ 537, 97, 152, 57, 190, 18, 63, 542, 4, 98, + /* 480 */ 5, 583, 19, 3, 14, 478, 6, 200, 477, 202, + /* 490 */ 476, 475, 474, 473, 472, 471, 469, 45, 442, 215, + /* 500 */ 66, 444, 21, 68, 46, 499, 498, 496, 54, 463, + /* 510 */ 461, 453, 70, 459, 455, 457, 451, 449, 470, 468, + /* 520 */ 81, 424, 440, 92, 93, 414, 412, 616, }; static const YYCODETYPE yy_lookahead[] = { /* 0 */ 201, 1, 250, 200, 201, 250, 198, 199, 250, 9, /* 10 */ 250, 259, 260, 13, 14, 201, 16, 17, 260, 259, - /* 20 */ 260, 21, 250, 1, 24, 25, 26, 27, 28, 250, - /* 30 */ 5, 9, 201, 33, 34, 201, 250, 37, 38, 39, + /* 20 */ 260, 21, 234, 1, 24, 25, 26, 27, 28, 250, + /* 30 */ 5, 9, 201, 33, 34, 201, 248, 37, 38, 39, /* 40 */ 13, 14, 234, 16, 17, 218, 250, 233, 21, 235, /* 50 */ 251, 24, 25, 26, 27, 28, 248, 201, 33, 34, /* 60 */ 33, 34, 218, 236, 37, 38, 39, 45, 46, 47, /* 70 */ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, - /* 80 */ 236, 13, 14, 252, 16, 17, 218, 78, 254, 21, + /* 80 */ 236, 13, 14, 252, 16, 17, 218, 250, 254, 21, /* 90 */ 256, 201, 24, 25, 26, 27, 28, 37, 38, 39, - /* 100 */ 100, 33, 34, 250, 236, 37, 38, 39, 14, 101, - /* 110 */ 16, 17, 256, 105, 258, 21, 201, 201, 24, 25, - /* 120 */ 26, 27, 28, 233, 97, 235, 201, 33, 34, 250, - /* 130 */ 123, 37, 38, 39, 60, 16, 17, 130, 96, 132, + /* 100 */ 100, 33, 34, 72, 236, 37, 38, 39, 14, 78, + /* 110 */ 16, 17, 256, 78, 258, 21, 201, 201, 24, 25, + /* 120 */ 26, 27, 28, 233, 97, 235, 201, 33, 34, 1, + /* 130 */ 63, 37, 38, 39, 60, 16, 17, 9, 96, 250, /* 140 */ 21, 99, 100, 24, 25, 26, 27, 28, 233, 233, - /* 150 */ 235, 235, 33, 34, 250, 230, 37, 38, 39, 85, + /* 150 */ 235, 235, 33, 34, 101, 230, 37, 38, 39, 85, /* 160 */ 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, - /* 170 */ 217, 201, 219, 220, 221, 222, 223, 224, 225, 226, - /* 180 */ 227, 228, 229, 1, 2, 1, 219, 5, 221, 7, - /* 190 */ 5, 9, 7, 9, 25, 26, 27, 28, 60, 61, - /* 200 */ 62, 44, 33, 34, 60, 235, 37, 38, 39, 64, - /* 210 */ 65, 66, 67, 68, 250, 33, 34, 1, 2, 37, - /* 220 */ 63, 5, 201, 7, 237, 9, 69, 70, 71, 63, - /* 230 */ 2, 87, 201, 5, 77, 7, 249, 9, 64, 65, - /* 240 */ 66, 67, 68, 64, 65, 66, 67, 68, 74, 33, - /* 250 */ 34, 1, 2, 37, 5, 5, 7, 7, 232, 9, - /* 260 */ 103, 33, 34, 101, 33, 34, 72, 59, 37, 38, - /* 270 */ 39, 205, 78, 1, 208, 112, 113, 256, 96, 122, - /* 280 */ 250, 119, 205, 33, 34, 208, 129, 256, 106, 205, - /* 290 */ 124, 101, 208, 127, 128, 97, 97, 97, 97, 101, - /* 300 */ 101, 101, 101, 121, 96, 250, 97, 117, 250, 37, - /* 310 */ 101, 97, 96, 250, 97, 101, 236, 5, 101, 7, - /* 320 */ 250, 5, 106, 7, 72, 73, 250, 125, 126, 250, - /* 330 */ 102, 125, 126, 250, 236, 250, 231, 121, 231, 231, - /* 340 */ 231, 231, 231, 231, 201, 257, 96, 201, 257, 201, - /* 350 */ 201, 238, 201, 201, 201, 201, 106, 201, 103, 201, - /* 360 */ 201, 201, 201, 234, 59, 201, 201, 106, 201, 201, - /* 370 */ 253, 121, 201, 201, 201, 253, 201, 201, 201, 201, - /* 380 */ 116, 201, 201, 247, 201, 118, 246, 201, 201, 201, - /* 390 */ 201, 115, 201, 201, 201, 245, 201, 110, 202, 202, - /* 400 */ 114, 109, 108, 107, 120, 202, 75, 84, 83, 49, - /* 410 */ 80, 82, 53, 81, 244, 202, 79, 202, 75, 5, - /* 420 */ 131, 5, 202, 58, 206, 206, 131, 202, 5, 131, - /* 430 */ 203, 5, 131, 58, 210, 202, 214, 216, 215, 213, - /* 440 */ 211, 209, 212, 203, 207, 234, 241, 243, 204, 242, - /* 450 */ 240, 239, 5, 131, 58, 86, 123, 104, 101, 97, - /* 460 */ 96, 1, 96, 111, 97, 111, 96, 72, 96, 101, - /* 470 */ 97, 9, 101, 96, 98, 96, 98, 5, 5, 5, - /* 480 */ 5, 1, 5, 5, 5, 101, 76, 72, 58, 126, - /* 490 */ 101, 15, 5, 5, 97, 96, 5, 16, 5, 5, - /* 500 */ 126, 5, 5, 5, 5, 5, 5, 5, 58, 58, - /* 510 */ 76, 21, 59, 21, 58, 0, 261, 261, 261, 261, - /* 520 */ 261, 261, 261, 261, 261, 261, 261, 261, 261, 261, + /* 170 */ 217, 118, 219, 220, 221, 222, 223, 224, 225, 226, + /* 180 */ 227, 228, 229, 1, 2, 44, 250, 5, 201, 7, + /* 190 */ 201, 9, 125, 1, 2, 128, 129, 5, 96, 7, + /* 200 */ 201, 9, 33, 34, 63, 103, 37, 38, 39, 60, + /* 210 */ 69, 70, 71, 124, 205, 33, 34, 208, 77, 37, + /* 220 */ 131, 219, 133, 221, 250, 33, 34, 1, 2, 37, + /* 230 */ 237, 5, 250, 7, 235, 9, 87, 96, 25, 26, + /* 240 */ 27, 28, 249, 256, 103, 256, 33, 34, 113, 114, + /* 250 */ 37, 38, 39, 250, 64, 65, 66, 67, 68, 33, + /* 260 */ 34, 96, 2, 205, 123, 5, 208, 7, 103, 9, + /* 270 */ 97, 130, 64, 65, 66, 67, 68, 104, 96, 59, + /* 280 */ 64, 65, 66, 67, 68, 1, 101, 250, 96, 107, + /* 290 */ 74, 106, 205, 33, 34, 208, 60, 61, 62, 107, + /* 300 */ 101, 97, 97, 97, 122, 101, 101, 101, 97, 5, + /* 310 */ 231, 7, 101, 5, 122, 7, 96, 126, 127, 120, + /* 320 */ 97, 37, 96, 250, 101, 97, 97, 126, 127, 101, + /* 330 */ 101, 250, 5, 107, 7, 5, 250, 7, 72, 73, + /* 340 */ 250, 250, 250, 250, 236, 250, 236, 231, 122, 231, + /* 350 */ 238, 231, 231, 231, 201, 232, 231, 201, 234, 201, + /* 360 */ 257, 257, 102, 201, 201, 201, 103, 201, 201, 201, + /* 370 */ 234, 201, 201, 201, 59, 201, 201, 201, 201, 201, + /* 380 */ 201, 201, 201, 107, 201, 201, 253, 201, 201, 201, + /* 390 */ 201, 253, 117, 247, 201, 201, 201, 201, 201, 201, + /* 400 */ 119, 201, 116, 201, 201, 246, 245, 111, 115, 202, + /* 410 */ 110, 202, 202, 121, 109, 108, 75, 84, 83, 49, + /* 420 */ 80, 82, 53, 202, 81, 79, 202, 75, 5, 132, + /* 430 */ 5, 206, 202, 206, 132, 58, 5, 202, 132, 5, + /* 440 */ 58, 132, 5, 203, 210, 202, 214, 216, 215, 211, + /* 450 */ 213, 212, 234, 209, 203, 240, 244, 243, 204, 242, + /* 460 */ 241, 239, 207, 132, 58, 86, 124, 104, 97, 105, + /* 470 */ 97, 96, 1, 101, 96, 101, 72, 97, 112, 96, + /* 480 */ 112, 97, 101, 96, 96, 9, 96, 98, 5, 98, + /* 490 */ 5, 5, 5, 1, 5, 5, 5, 101, 76, 15, + /* 500 */ 72, 58, 101, 127, 16, 5, 5, 97, 96, 5, + /* 510 */ 5, 5, 127, 5, 5, 5, 5, 5, 5, 5, + /* 520 */ 58, 58, 76, 21, 21, 59, 58, 0, 261, 261, /* 530 */ 261, 261, 261, 261, 261, 261, 261, 261, 261, 261, /* 540 */ 261, 261, 261, 261, 261, 261, 261, 261, 261, 261, /* 550 */ 261, 261, 261, 261, 261, 261, 261, 261, 261, 261, @@ -325,82 +326,83 @@ static const YYCODETYPE yy_lookahead[] = { /* 680 */ 261, 261, 261, 261, 261, 261, 261, 261, 261, 261, /* 690 */ 261, 261, 261, 261, 261, 261, 261, 261, 261, 261, /* 700 */ 261, 261, 261, 261, 261, 261, 261, 261, 261, 261, - /* 710 */ 261, 261, 261, + /* 710 */ 261, 261, 261, 261, 261, 261, 261, 261, 261, 261, + /* 720 */ 261, 261, 261, 261, 261, }; -#define YY_SHIFT_COUNT (240) +#define YY_SHIFT_COUNT (245) #define YY_SHIFT_MIN (0) -#define YY_SHIFT_MAX (515) +#define YY_SHIFT_MAX (527) static const unsigned short int yy_shift_ofst[] = { - /* 0 */ 157, 74, 182, 250, 184, 184, 184, 184, 184, 184, - /* 10 */ 0, 22, 250, 228, 228, 228, 184, 184, 184, 184, - /* 20 */ 184, 194, 144, 9, 9, 516, 216, 250, 250, 250, - /* 30 */ 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, - /* 40 */ 250, 250, 250, 250, 228, 228, 25, 25, 25, 25, - /* 50 */ 25, 25, 42, 25, 184, 184, 163, 163, 8, 184, - /* 60 */ 184, 184, 184, 184, 184, 184, 184, 184, 184, 184, - /* 70 */ 184, 184, 184, 184, 184, 184, 184, 184, 184, 184, - /* 80 */ 184, 184, 184, 184, 184, 184, 184, 184, 184, 184, - /* 90 */ 184, 184, 255, 305, 305, 261, 261, 305, 264, 267, - /* 100 */ 276, 287, 286, 292, 294, 296, 284, 255, 305, 305, - /* 110 */ 331, 331, 305, 323, 325, 360, 330, 329, 359, 332, - /* 120 */ 337, 305, 343, 305, 343, 516, 516, 27, 68, 68, - /* 130 */ 68, 94, 119, 169, 169, 169, 174, 231, 231, 231, - /* 140 */ 231, 145, 179, 166, 7, 60, 60, 138, 198, 199, - /* 150 */ 200, 201, 185, 249, 272, 208, 162, 190, 209, 214, - /* 160 */ 217, 312, 316, 202, 206, 252, 414, 289, 416, 295, - /* 170 */ 365, 423, 298, 426, 301, 375, 447, 322, 396, 369, - /* 180 */ 333, 353, 357, 362, 364, 460, 366, 367, 370, 368, - /* 190 */ 352, 371, 354, 373, 372, 377, 376, 379, 378, 395, - /* 200 */ 462, 472, 473, 474, 475, 480, 477, 478, 479, 384, - /* 210 */ 410, 476, 415, 430, 481, 363, 374, 389, 487, 488, - /* 220 */ 397, 399, 389, 491, 493, 494, 496, 497, 498, 499, - /* 230 */ 500, 501, 502, 450, 451, 434, 490, 492, 453, 456, - /* 240 */ 515, + /* 0 */ 141, 74, 182, 226, 128, 128, 128, 128, 128, 128, + /* 10 */ 0, 22, 226, 260, 260, 260, 102, 128, 128, 128, + /* 20 */ 128, 128, 31, 149, 35, 35, 528, 192, 226, 226, + /* 30 */ 226, 226, 226, 226, 226, 226, 226, 226, 226, 226, + /* 40 */ 226, 226, 226, 226, 226, 260, 260, 25, 25, 25, + /* 50 */ 25, 25, 25, 42, 25, 165, 128, 128, 135, 135, + /* 60 */ 185, 128, 128, 128, 128, 128, 128, 128, 128, 128, + /* 70 */ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + /* 80 */ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + /* 90 */ 128, 128, 128, 128, 263, 315, 315, 276, 276, 315, + /* 100 */ 275, 281, 286, 296, 293, 300, 305, 307, 292, 263, + /* 110 */ 315, 315, 341, 341, 315, 333, 335, 370, 340, 339, + /* 120 */ 369, 343, 346, 315, 352, 315, 352, 528, 528, 27, + /* 130 */ 68, 68, 68, 94, 119, 213, 213, 213, 216, 169, + /* 140 */ 169, 169, 169, 190, 208, 67, 89, 60, 60, 236, + /* 150 */ 173, 204, 205, 206, 211, 304, 308, 284, 220, 199, + /* 160 */ 53, 223, 228, 229, 327, 330, 191, 201, 266, 423, + /* 170 */ 297, 425, 302, 377, 431, 306, 434, 309, 382, 437, + /* 180 */ 331, 406, 379, 342, 363, 371, 364, 372, 373, 375, + /* 190 */ 471, 378, 380, 383, 374, 366, 381, 368, 384, 387, + /* 200 */ 388, 389, 390, 391, 404, 476, 483, 485, 486, 487, + /* 210 */ 492, 489, 490, 491, 396, 422, 484, 428, 443, 488, + /* 220 */ 376, 385, 401, 500, 501, 410, 412, 401, 504, 505, + /* 230 */ 506, 508, 509, 510, 511, 512, 513, 514, 462, 463, + /* 240 */ 446, 502, 503, 466, 468, 527, }; -#define YY_REDUCE_COUNT (126) +#define YY_REDUCE_COUNT (128) #define YY_REDUCE_MIN (-248) -#define YY_REDUCE_MAX (244) +#define YY_REDUCE_MAX (255) static const short yy_reduce_ofst[] = { /* 0 */ -192, -47, -248, -240, -144, -166, -186, -110, -85, -84, - /* 10 */ -201, -197, -242, -173, -156, -132, -169, 21, 31, -75, - /* 20 */ -30, 66, -33, 77, 84, -13, -245, -228, -221, -214, - /* 30 */ -204, -147, -121, -96, -36, 30, 55, 58, 63, 70, - /* 40 */ 76, 79, 83, 85, 80, 98, 105, 107, 108, 109, - /* 50 */ 110, 111, 26, 112, 143, 146, 88, 91, 113, 148, - /* 60 */ 149, 151, 152, 153, 154, 156, 158, 159, 160, 161, - /* 70 */ 164, 165, 167, 168, 171, 172, 173, 175, 176, 177, - /* 80 */ 178, 180, 181, 183, 186, 187, 188, 189, 191, 192, - /* 90 */ 193, 195, 129, 196, 197, 117, 122, 203, 136, 140, - /* 100 */ 150, 170, 204, 207, 205, 210, 212, 211, 213, 215, - /* 110 */ 218, 219, 220, 221, 223, 222, 224, 226, 229, 230, - /* 120 */ 232, 225, 227, 233, 240, 237, 244, + /* 10 */ -201, -197, -242, -173, -156, -132, -212, -169, -13, -11, + /* 20 */ -75, -1, 9, 2, 58, 87, -7, -245, -221, -204, + /* 30 */ -163, -111, -64, -26, -18, 3, 37, 73, 81, 86, + /* 40 */ 90, 91, 92, 93, 95, 108, 110, 79, 116, 118, + /* 50 */ 120, 121, 122, 123, 125, 124, 153, 156, 103, 104, + /* 60 */ 112, 158, 162, 163, 164, 166, 167, 168, 170, 171, + /* 70 */ 172, 174, 175, 176, 177, 178, 179, 180, 181, 183, + /* 80 */ 184, 186, 187, 188, 189, 193, 194, 195, 196, 197, + /* 90 */ 198, 200, 202, 203, 136, 207, 209, 133, 138, 210, + /* 100 */ 146, 159, 161, 212, 214, 217, 219, 215, 222, 218, + /* 110 */ 221, 224, 225, 227, 230, 231, 233, 232, 234, 237, + /* 120 */ 238, 239, 244, 235, 240, 243, 251, 255, 254, }; static const YYACTIONTYPE yy_default[] = { - /* 0 */ 604, 655, 809, 809, 604, 604, 604, 604, 604, 604, - /* 10 */ 739, 622, 809, 604, 604, 604, 604, 604, 604, 604, - /* 20 */ 604, 657, 644, 657, 657, 734, 604, 604, 604, 604, - /* 30 */ 604, 604, 604, 604, 604, 604, 604, 604, 604, 604, - /* 40 */ 604, 604, 604, 604, 604, 604, 604, 604, 604, 604, - /* 50 */ 604, 604, 604, 604, 604, 604, 758, 758, 732, 604, - /* 60 */ 604, 604, 604, 604, 604, 604, 604, 604, 604, 604, - /* 70 */ 604, 604, 604, 604, 604, 604, 642, 604, 640, 604, - /* 80 */ 604, 604, 604, 604, 604, 604, 604, 604, 604, 604, - /* 90 */ 604, 604, 604, 624, 624, 604, 604, 624, 765, 769, - /* 100 */ 763, 751, 759, 750, 746, 745, 773, 604, 624, 624, - /* 110 */ 652, 652, 624, 673, 671, 669, 661, 667, 663, 665, - /* 120 */ 659, 624, 650, 624, 650, 688, 701, 604, 774, 808, - /* 130 */ 764, 792, 791, 804, 798, 797, 604, 796, 795, 794, - /* 140 */ 793, 604, 604, 604, 604, 800, 799, 604, 604, 604, - /* 150 */ 604, 604, 604, 604, 604, 776, 770, 766, 604, 604, - /* 160 */ 604, 604, 604, 604, 604, 604, 604, 604, 604, 604, - /* 170 */ 604, 604, 604, 604, 604, 604, 604, 604, 604, 604, - /* 180 */ 604, 731, 740, 604, 604, 604, 604, 604, 604, 760, - /* 190 */ 604, 752, 604, 604, 604, 604, 604, 604, 710, 604, - /* 200 */ 604, 604, 604, 604, 604, 604, 604, 604, 604, 676, - /* 210 */ 604, 604, 604, 604, 604, 604, 604, 813, 604, 604, - /* 220 */ 604, 704, 811, 604, 604, 604, 604, 604, 604, 604, - /* 230 */ 604, 604, 604, 604, 604, 604, 628, 626, 604, 620, - /* 240 */ 604, + /* 0 */ 613, 664, 820, 820, 613, 613, 613, 613, 613, 613, + /* 10 */ 750, 631, 820, 613, 613, 613, 613, 613, 613, 613, + /* 20 */ 613, 613, 666, 653, 666, 666, 745, 613, 613, 613, + /* 30 */ 613, 613, 613, 613, 613, 613, 613, 613, 613, 613, + /* 40 */ 613, 613, 613, 613, 613, 613, 613, 613, 613, 613, + /* 50 */ 613, 613, 613, 613, 613, 613, 613, 613, 769, 769, + /* 60 */ 743, 613, 613, 613, 613, 613, 613, 613, 613, 613, + /* 70 */ 613, 613, 613, 613, 613, 613, 613, 613, 651, 613, + /* 80 */ 649, 613, 613, 613, 613, 613, 613, 613, 613, 613, + /* 90 */ 613, 613, 613, 613, 613, 633, 633, 613, 613, 633, + /* 100 */ 776, 780, 774, 762, 770, 761, 757, 756, 784, 613, + /* 110 */ 633, 633, 661, 661, 633, 682, 680, 678, 670, 676, + /* 120 */ 672, 674, 668, 633, 659, 633, 659, 697, 710, 613, + /* 130 */ 785, 819, 775, 803, 802, 815, 809, 808, 613, 807, + /* 140 */ 806, 805, 804, 613, 613, 613, 613, 811, 810, 613, + /* 150 */ 613, 613, 613, 613, 613, 613, 613, 613, 787, 781, + /* 160 */ 777, 613, 613, 613, 613, 613, 613, 613, 613, 613, + /* 170 */ 613, 613, 613, 613, 613, 613, 613, 613, 613, 613, + /* 180 */ 613, 613, 613, 613, 742, 613, 613, 751, 613, 613, + /* 190 */ 613, 613, 613, 613, 771, 613, 763, 613, 613, 613, + /* 200 */ 613, 613, 613, 719, 613, 613, 613, 613, 613, 613, + /* 210 */ 613, 613, 613, 613, 685, 613, 613, 613, 613, 613, + /* 220 */ 613, 613, 824, 613, 613, 613, 713, 822, 613, 613, + /* 230 */ 613, 613, 613, 613, 613, 613, 613, 613, 613, 613, + /* 240 */ 613, 637, 635, 613, 629, 613, }; /********** End of lemon-generated parsing tables *****************************/ @@ -525,6 +527,7 @@ static const YYCODETYPE yyFallback[] = { 1, /* NULL => ID */ 0, /* SELECT => nothing */ 0, /* UNION => nothing */ + 1, /* ALL => ID */ 0, /* FROM => nothing */ 0, /* VARIABLE => nothing */ 0, /* INTERVAL => nothing */ @@ -584,7 +587,6 @@ static const YYCODETYPE yyFallback[] = { 1, /* STATEMENT => ID */ 1, /* TRIGGER => ID */ 1, /* VIEW => ID */ - 1, /* ALL => ID */ 1, /* COUNT => ID */ 1, /* SUM => ID */ 1, /* AVG => ID */ @@ -808,66 +810,66 @@ static const char *const yyTokenName[] = { /* 102 */ "NULL", /* 103 */ "SELECT", /* 104 */ "UNION", - /* 105 */ "FROM", - /* 106 */ "VARIABLE", - /* 107 */ "INTERVAL", - /* 108 */ "FILL", - /* 109 */ "SLIDING", - /* 110 */ "ORDER", - /* 111 */ "BY", - /* 112 */ "ASC", - /* 113 */ "DESC", - /* 114 */ "GROUP", - /* 115 */ "HAVING", - /* 116 */ "LIMIT", - /* 117 */ "OFFSET", - /* 118 */ "SLIMIT", - /* 119 */ "SOFFSET", - /* 120 */ "WHERE", - /* 121 */ "NOW", - /* 122 */ "RESET", - /* 123 */ "QUERY", - /* 124 */ "ADD", - /* 125 */ "COLUMN", - /* 126 */ "TAG", - /* 127 */ "CHANGE", - /* 128 */ "SET", - /* 129 */ "KILL", - /* 130 */ "CONNECTION", - /* 131 */ "COLON", - /* 132 */ "STREAM", - /* 133 */ "ABORT", - /* 134 */ "AFTER", - /* 135 */ "ATTACH", - /* 136 */ "BEFORE", - /* 137 */ "BEGIN", - /* 138 */ "CASCADE", - /* 139 */ "CLUSTER", - /* 140 */ "CONFLICT", - /* 141 */ "COPY", - /* 142 */ "DEFERRED", - /* 143 */ "DELIMITERS", - /* 144 */ "DETACH", - /* 145 */ "EACH", - /* 146 */ "END", - /* 147 */ "EXPLAIN", - /* 148 */ "FAIL", - /* 149 */ "FOR", - /* 150 */ "IGNORE", - /* 151 */ "IMMEDIATE", - /* 152 */ "INITIALLY", - /* 153 */ "INSTEAD", - /* 154 */ "MATCH", - /* 155 */ "KEY", - /* 156 */ "OF", - /* 157 */ "RAISE", - /* 158 */ "REPLACE", - /* 159 */ "RESTRICT", - /* 160 */ "ROW", - /* 161 */ "STATEMENT", - /* 162 */ "TRIGGER", - /* 163 */ "VIEW", - /* 164 */ "ALL", + /* 105 */ "ALL", + /* 106 */ "FROM", + /* 107 */ "VARIABLE", + /* 108 */ "INTERVAL", + /* 109 */ "FILL", + /* 110 */ "SLIDING", + /* 111 */ "ORDER", + /* 112 */ "BY", + /* 113 */ "ASC", + /* 114 */ "DESC", + /* 115 */ "GROUP", + /* 116 */ "HAVING", + /* 117 */ "LIMIT", + /* 118 */ "OFFSET", + /* 119 */ "SLIMIT", + /* 120 */ "SOFFSET", + /* 121 */ "WHERE", + /* 122 */ "NOW", + /* 123 */ "RESET", + /* 124 */ "QUERY", + /* 125 */ "ADD", + /* 126 */ "COLUMN", + /* 127 */ "TAG", + /* 128 */ "CHANGE", + /* 129 */ "SET", + /* 130 */ "KILL", + /* 131 */ "CONNECTION", + /* 132 */ "COLON", + /* 133 */ "STREAM", + /* 134 */ "ABORT", + /* 135 */ "AFTER", + /* 136 */ "ATTACH", + /* 137 */ "BEFORE", + /* 138 */ "BEGIN", + /* 139 */ "CASCADE", + /* 140 */ "CLUSTER", + /* 141 */ "CONFLICT", + /* 142 */ "COPY", + /* 143 */ "DEFERRED", + /* 144 */ "DELIMITERS", + /* 145 */ "DETACH", + /* 146 */ "EACH", + /* 147 */ "END", + /* 148 */ "EXPLAIN", + /* 149 */ "FAIL", + /* 150 */ "FOR", + /* 151 */ "IGNORE", + /* 152 */ "IMMEDIATE", + /* 153 */ "INITIALLY", + /* 154 */ "INSTEAD", + /* 155 */ "MATCH", + /* 156 */ "KEY", + /* 157 */ "OF", + /* 158 */ "RAISE", + /* 159 */ "REPLACE", + /* 160 */ "RESTRICT", + /* 161 */ "ROW", + /* 162 */ "STATEMENT", + /* 163 */ "TRIGGER", + /* 164 */ "VIEW", /* 165 */ "COUNT", /* 166 */ "SUM", /* 167 */ "AVG", @@ -1094,96 +1096,98 @@ static const char *const yyRuleName[] = { /* 120 */ "tagitem ::= PLUS FLOAT", /* 121 */ "select ::= SELECT selcollist from where_opt interval_opt fill_opt sliding_opt groupby_opt orderby_opt having_opt slimit_opt limit_opt", /* 122 */ "union ::= select", - /* 123 */ "union ::= union UNION select", - /* 124 */ "cmd ::= union", - /* 125 */ "select ::= SELECT selcollist", - /* 126 */ "sclp ::= selcollist COMMA", - /* 127 */ "sclp ::=", - /* 128 */ "selcollist ::= sclp expr as", - /* 129 */ "selcollist ::= sclp STAR", - /* 130 */ "as ::= AS ids", - /* 131 */ "as ::= ids", - /* 132 */ "as ::=", - /* 133 */ "from ::= FROM tablelist", - /* 134 */ "tablelist ::= ids cpxName", - /* 135 */ "tablelist ::= tablelist COMMA ids cpxName", - /* 136 */ "tmvar ::= VARIABLE", - /* 137 */ "interval_opt ::= INTERVAL LP tmvar RP", - /* 138 */ "interval_opt ::=", - /* 139 */ "fill_opt ::=", - /* 140 */ "fill_opt ::= FILL LP ID COMMA tagitemlist RP", - /* 141 */ "fill_opt ::= FILL LP ID RP", - /* 142 */ "sliding_opt ::= SLIDING LP tmvar RP", - /* 143 */ "sliding_opt ::=", - /* 144 */ "orderby_opt ::=", - /* 145 */ "orderby_opt ::= ORDER BY sortlist", - /* 146 */ "sortlist ::= sortlist COMMA item sortorder", - /* 147 */ "sortlist ::= item sortorder", - /* 148 */ "item ::= ids cpxName", - /* 149 */ "sortorder ::= ASC", - /* 150 */ "sortorder ::= DESC", - /* 151 */ "sortorder ::=", - /* 152 */ "groupby_opt ::=", - /* 153 */ "groupby_opt ::= GROUP BY grouplist", - /* 154 */ "grouplist ::= grouplist COMMA item", - /* 155 */ "grouplist ::= item", - /* 156 */ "having_opt ::=", - /* 157 */ "having_opt ::= HAVING expr", - /* 158 */ "limit_opt ::=", - /* 159 */ "limit_opt ::= LIMIT signed", - /* 160 */ "limit_opt ::= LIMIT signed OFFSET signed", - /* 161 */ "limit_opt ::= LIMIT signed COMMA signed", - /* 162 */ "slimit_opt ::=", - /* 163 */ "slimit_opt ::= SLIMIT signed", - /* 164 */ "slimit_opt ::= SLIMIT signed SOFFSET signed", - /* 165 */ "slimit_opt ::= SLIMIT signed COMMA signed", - /* 166 */ "where_opt ::=", - /* 167 */ "where_opt ::= WHERE expr", - /* 168 */ "expr ::= LP expr RP", - /* 169 */ "expr ::= ID", - /* 170 */ "expr ::= ID DOT ID", - /* 171 */ "expr ::= ID DOT STAR", - /* 172 */ "expr ::= INTEGER", - /* 173 */ "expr ::= MINUS INTEGER", - /* 174 */ "expr ::= PLUS INTEGER", - /* 175 */ "expr ::= FLOAT", - /* 176 */ "expr ::= MINUS FLOAT", - /* 177 */ "expr ::= PLUS FLOAT", - /* 178 */ "expr ::= STRING", - /* 179 */ "expr ::= NOW", - /* 180 */ "expr ::= VARIABLE", - /* 181 */ "expr ::= BOOL", - /* 182 */ "expr ::= ID LP exprlist RP", - /* 183 */ "expr ::= ID LP STAR RP", - /* 184 */ "expr ::= expr AND expr", - /* 185 */ "expr ::= expr OR expr", - /* 186 */ "expr ::= expr LT expr", - /* 187 */ "expr ::= expr GT expr", - /* 188 */ "expr ::= expr LE expr", - /* 189 */ "expr ::= expr GE expr", - /* 190 */ "expr ::= expr NE expr", - /* 191 */ "expr ::= expr EQ expr", - /* 192 */ "expr ::= expr PLUS expr", - /* 193 */ "expr ::= expr MINUS expr", - /* 194 */ "expr ::= expr STAR expr", - /* 195 */ "expr ::= expr SLASH expr", - /* 196 */ "expr ::= expr REM expr", - /* 197 */ "expr ::= expr LIKE expr", - /* 198 */ "expr ::= expr IN LP exprlist RP", - /* 199 */ "exprlist ::= exprlist COMMA expritem", - /* 200 */ "exprlist ::= expritem", - /* 201 */ "expritem ::= expr", - /* 202 */ "expritem ::=", - /* 203 */ "cmd ::= RESET QUERY CACHE", - /* 204 */ "cmd ::= ALTER TABLE ids cpxName ADD COLUMN columnlist", - /* 205 */ "cmd ::= ALTER TABLE ids cpxName DROP COLUMN ids", - /* 206 */ "cmd ::= ALTER TABLE ids cpxName ADD TAG columnlist", - /* 207 */ "cmd ::= ALTER TABLE ids cpxName DROP TAG ids", - /* 208 */ "cmd ::= ALTER TABLE ids cpxName CHANGE TAG ids ids", - /* 209 */ "cmd ::= ALTER TABLE ids cpxName SET TAG ids EQ tagitem", - /* 210 */ "cmd ::= KILL CONNECTION IPTOKEN COLON INTEGER", - /* 211 */ "cmd ::= KILL STREAM IPTOKEN COLON INTEGER COLON INTEGER", - /* 212 */ "cmd ::= KILL QUERY IPTOKEN COLON INTEGER COLON INTEGER", + /* 123 */ "union ::= LP union RP", + /* 124 */ "union ::= union UNION ALL select", + /* 125 */ "union ::= union UNION ALL LP select RP", + /* 126 */ "cmd ::= union", + /* 127 */ "select ::= SELECT selcollist", + /* 128 */ "sclp ::= selcollist COMMA", + /* 129 */ "sclp ::=", + /* 130 */ "selcollist ::= sclp expr as", + /* 131 */ "selcollist ::= sclp STAR", + /* 132 */ "as ::= AS ids", + /* 133 */ "as ::= ids", + /* 134 */ "as ::=", + /* 135 */ "from ::= FROM tablelist", + /* 136 */ "tablelist ::= ids cpxName", + /* 137 */ "tablelist ::= tablelist COMMA ids cpxName", + /* 138 */ "tmvar ::= VARIABLE", + /* 139 */ "interval_opt ::= INTERVAL LP tmvar RP", + /* 140 */ "interval_opt ::=", + /* 141 */ "fill_opt ::=", + /* 142 */ "fill_opt ::= FILL LP ID COMMA tagitemlist RP", + /* 143 */ "fill_opt ::= FILL LP ID RP", + /* 144 */ "sliding_opt ::= SLIDING LP tmvar RP", + /* 145 */ "sliding_opt ::=", + /* 146 */ "orderby_opt ::=", + /* 147 */ "orderby_opt ::= ORDER BY sortlist", + /* 148 */ "sortlist ::= sortlist COMMA item sortorder", + /* 149 */ "sortlist ::= item sortorder", + /* 150 */ "item ::= ids cpxName", + /* 151 */ "sortorder ::= ASC", + /* 152 */ "sortorder ::= DESC", + /* 153 */ "sortorder ::=", + /* 154 */ "groupby_opt ::=", + /* 155 */ "groupby_opt ::= GROUP BY grouplist", + /* 156 */ "grouplist ::= grouplist COMMA item", + /* 157 */ "grouplist ::= item", + /* 158 */ "having_opt ::=", + /* 159 */ "having_opt ::= HAVING expr", + /* 160 */ "limit_opt ::=", + /* 161 */ "limit_opt ::= LIMIT signed", + /* 162 */ "limit_opt ::= LIMIT signed OFFSET signed", + /* 163 */ "limit_opt ::= LIMIT signed COMMA signed", + /* 164 */ "slimit_opt ::=", + /* 165 */ "slimit_opt ::= SLIMIT signed", + /* 166 */ "slimit_opt ::= SLIMIT signed SOFFSET signed", + /* 167 */ "slimit_opt ::= SLIMIT signed COMMA signed", + /* 168 */ "where_opt ::=", + /* 169 */ "where_opt ::= WHERE expr", + /* 170 */ "expr ::= LP expr RP", + /* 171 */ "expr ::= ID", + /* 172 */ "expr ::= ID DOT ID", + /* 173 */ "expr ::= ID DOT STAR", + /* 174 */ "expr ::= INTEGER", + /* 175 */ "expr ::= MINUS INTEGER", + /* 176 */ "expr ::= PLUS INTEGER", + /* 177 */ "expr ::= FLOAT", + /* 178 */ "expr ::= MINUS FLOAT", + /* 179 */ "expr ::= PLUS FLOAT", + /* 180 */ "expr ::= STRING", + /* 181 */ "expr ::= NOW", + /* 182 */ "expr ::= VARIABLE", + /* 183 */ "expr ::= BOOL", + /* 184 */ "expr ::= ID LP exprlist RP", + /* 185 */ "expr ::= ID LP STAR RP", + /* 186 */ "expr ::= expr AND expr", + /* 187 */ "expr ::= expr OR expr", + /* 188 */ "expr ::= expr LT expr", + /* 189 */ "expr ::= expr GT expr", + /* 190 */ "expr ::= expr LE expr", + /* 191 */ "expr ::= expr GE expr", + /* 192 */ "expr ::= expr NE expr", + /* 193 */ "expr ::= expr EQ expr", + /* 194 */ "expr ::= expr PLUS expr", + /* 195 */ "expr ::= expr MINUS expr", + /* 196 */ "expr ::= expr STAR expr", + /* 197 */ "expr ::= expr SLASH expr", + /* 198 */ "expr ::= expr REM expr", + /* 199 */ "expr ::= expr LIKE expr", + /* 200 */ "expr ::= expr IN LP exprlist RP", + /* 201 */ "exprlist ::= exprlist COMMA expritem", + /* 202 */ "exprlist ::= expritem", + /* 203 */ "expritem ::= expr", + /* 204 */ "expritem ::=", + /* 205 */ "cmd ::= RESET QUERY CACHE", + /* 206 */ "cmd ::= ALTER TABLE ids cpxName ADD COLUMN columnlist", + /* 207 */ "cmd ::= ALTER TABLE ids cpxName DROP COLUMN ids", + /* 208 */ "cmd ::= ALTER TABLE ids cpxName ADD TAG columnlist", + /* 209 */ "cmd ::= ALTER TABLE ids cpxName DROP TAG ids", + /* 210 */ "cmd ::= ALTER TABLE ids cpxName CHANGE TAG ids ids", + /* 211 */ "cmd ::= ALTER TABLE ids cpxName SET TAG ids EQ tagitem", + /* 212 */ "cmd ::= KILL CONNECTION IPTOKEN COLON INTEGER", + /* 213 */ "cmd ::= KILL STREAM IPTOKEN COLON INTEGER COLON INTEGER", + /* 214 */ "cmd ::= KILL QUERY IPTOKEN COLON INTEGER COLON INTEGER", }; #endif /* NDEBUG */ @@ -1764,96 +1768,98 @@ static const struct { { 236, -2 }, /* (120) tagitem ::= PLUS FLOAT */ { 234, -12 }, /* (121) select ::= SELECT selcollist from where_opt interval_opt fill_opt sliding_opt groupby_opt orderby_opt having_opt slimit_opt limit_opt */ { 248, -1 }, /* (122) union ::= select */ - { 248, -3 }, /* (123) union ::= union UNION select */ - { 199, -1 }, /* (124) cmd ::= union */ - { 234, -2 }, /* (125) select ::= SELECT selcollist */ - { 249, -2 }, /* (126) sclp ::= selcollist COMMA */ - { 249, 0 }, /* (127) sclp ::= */ - { 237, -3 }, /* (128) selcollist ::= sclp expr as */ - { 237, -2 }, /* (129) selcollist ::= sclp STAR */ - { 251, -2 }, /* (130) as ::= AS ids */ - { 251, -1 }, /* (131) as ::= ids */ - { 251, 0 }, /* (132) as ::= */ - { 238, -2 }, /* (133) from ::= FROM tablelist */ - { 252, -2 }, /* (134) tablelist ::= ids cpxName */ - { 252, -4 }, /* (135) tablelist ::= tablelist COMMA ids cpxName */ - { 253, -1 }, /* (136) tmvar ::= VARIABLE */ - { 240, -4 }, /* (137) interval_opt ::= INTERVAL LP tmvar RP */ - { 240, 0 }, /* (138) interval_opt ::= */ - { 241, 0 }, /* (139) fill_opt ::= */ - { 241, -6 }, /* (140) fill_opt ::= FILL LP ID COMMA tagitemlist RP */ - { 241, -4 }, /* (141) fill_opt ::= FILL LP ID RP */ - { 242, -4 }, /* (142) sliding_opt ::= SLIDING LP tmvar RP */ - { 242, 0 }, /* (143) sliding_opt ::= */ - { 244, 0 }, /* (144) orderby_opt ::= */ - { 244, -3 }, /* (145) orderby_opt ::= ORDER BY sortlist */ - { 254, -4 }, /* (146) sortlist ::= sortlist COMMA item sortorder */ - { 254, -2 }, /* (147) sortlist ::= item sortorder */ - { 256, -2 }, /* (148) item ::= ids cpxName */ - { 257, -1 }, /* (149) sortorder ::= ASC */ - { 257, -1 }, /* (150) sortorder ::= DESC */ - { 257, 0 }, /* (151) sortorder ::= */ - { 243, 0 }, /* (152) groupby_opt ::= */ - { 243, -3 }, /* (153) groupby_opt ::= GROUP BY grouplist */ - { 258, -3 }, /* (154) grouplist ::= grouplist COMMA item */ - { 258, -1 }, /* (155) grouplist ::= item */ - { 245, 0 }, /* (156) having_opt ::= */ - { 245, -2 }, /* (157) having_opt ::= HAVING expr */ - { 247, 0 }, /* (158) limit_opt ::= */ - { 247, -2 }, /* (159) limit_opt ::= LIMIT signed */ - { 247, -4 }, /* (160) limit_opt ::= LIMIT signed OFFSET signed */ - { 247, -4 }, /* (161) limit_opt ::= LIMIT signed COMMA signed */ - { 246, 0 }, /* (162) slimit_opt ::= */ - { 246, -2 }, /* (163) slimit_opt ::= SLIMIT signed */ - { 246, -4 }, /* (164) slimit_opt ::= SLIMIT signed SOFFSET signed */ - { 246, -4 }, /* (165) slimit_opt ::= SLIMIT signed COMMA signed */ - { 239, 0 }, /* (166) where_opt ::= */ - { 239, -2 }, /* (167) where_opt ::= WHERE expr */ - { 250, -3 }, /* (168) expr ::= LP expr RP */ - { 250, -1 }, /* (169) expr ::= ID */ - { 250, -3 }, /* (170) expr ::= ID DOT ID */ - { 250, -3 }, /* (171) expr ::= ID DOT STAR */ - { 250, -1 }, /* (172) expr ::= INTEGER */ - { 250, -2 }, /* (173) expr ::= MINUS INTEGER */ - { 250, -2 }, /* (174) expr ::= PLUS INTEGER */ - { 250, -1 }, /* (175) expr ::= FLOAT */ - { 250, -2 }, /* (176) expr ::= MINUS FLOAT */ - { 250, -2 }, /* (177) expr ::= PLUS FLOAT */ - { 250, -1 }, /* (178) expr ::= STRING */ - { 250, -1 }, /* (179) expr ::= NOW */ - { 250, -1 }, /* (180) expr ::= VARIABLE */ - { 250, -1 }, /* (181) expr ::= BOOL */ - { 250, -4 }, /* (182) expr ::= ID LP exprlist RP */ - { 250, -4 }, /* (183) expr ::= ID LP STAR RP */ - { 250, -3 }, /* (184) expr ::= expr AND expr */ - { 250, -3 }, /* (185) expr ::= expr OR expr */ - { 250, -3 }, /* (186) expr ::= expr LT expr */ - { 250, -3 }, /* (187) expr ::= expr GT expr */ - { 250, -3 }, /* (188) expr ::= expr LE expr */ - { 250, -3 }, /* (189) expr ::= expr GE expr */ - { 250, -3 }, /* (190) expr ::= expr NE expr */ - { 250, -3 }, /* (191) expr ::= expr EQ expr */ - { 250, -3 }, /* (192) expr ::= expr PLUS expr */ - { 250, -3 }, /* (193) expr ::= expr MINUS expr */ - { 250, -3 }, /* (194) expr ::= expr STAR expr */ - { 250, -3 }, /* (195) expr ::= expr SLASH expr */ - { 250, -3 }, /* (196) expr ::= expr REM expr */ - { 250, -3 }, /* (197) expr ::= expr LIKE expr */ - { 250, -5 }, /* (198) expr ::= expr IN LP exprlist RP */ - { 259, -3 }, /* (199) exprlist ::= exprlist COMMA expritem */ - { 259, -1 }, /* (200) exprlist ::= expritem */ - { 260, -1 }, /* (201) expritem ::= expr */ - { 260, 0 }, /* (202) expritem ::= */ - { 199, -3 }, /* (203) cmd ::= RESET QUERY CACHE */ - { 199, -7 }, /* (204) cmd ::= ALTER TABLE ids cpxName ADD COLUMN columnlist */ - { 199, -7 }, /* (205) cmd ::= ALTER TABLE ids cpxName DROP COLUMN ids */ - { 199, -7 }, /* (206) cmd ::= ALTER TABLE ids cpxName ADD TAG columnlist */ - { 199, -7 }, /* (207) cmd ::= ALTER TABLE ids cpxName DROP TAG ids */ - { 199, -8 }, /* (208) cmd ::= ALTER TABLE ids cpxName CHANGE TAG ids ids */ - { 199, -9 }, /* (209) cmd ::= ALTER TABLE ids cpxName SET TAG ids EQ tagitem */ - { 199, -5 }, /* (210) cmd ::= KILL CONNECTION IPTOKEN COLON INTEGER */ - { 199, -7 }, /* (211) cmd ::= KILL STREAM IPTOKEN COLON INTEGER COLON INTEGER */ - { 199, -7 }, /* (212) cmd ::= KILL QUERY IPTOKEN COLON INTEGER COLON INTEGER */ + { 248, -3 }, /* (123) union ::= LP union RP */ + { 248, -4 }, /* (124) union ::= union UNION ALL select */ + { 248, -6 }, /* (125) union ::= union UNION ALL LP select RP */ + { 199, -1 }, /* (126) cmd ::= union */ + { 234, -2 }, /* (127) select ::= SELECT selcollist */ + { 249, -2 }, /* (128) sclp ::= selcollist COMMA */ + { 249, 0 }, /* (129) sclp ::= */ + { 237, -3 }, /* (130) selcollist ::= sclp expr as */ + { 237, -2 }, /* (131) selcollist ::= sclp STAR */ + { 251, -2 }, /* (132) as ::= AS ids */ + { 251, -1 }, /* (133) as ::= ids */ + { 251, 0 }, /* (134) as ::= */ + { 238, -2 }, /* (135) from ::= FROM tablelist */ + { 252, -2 }, /* (136) tablelist ::= ids cpxName */ + { 252, -4 }, /* (137) tablelist ::= tablelist COMMA ids cpxName */ + { 253, -1 }, /* (138) tmvar ::= VARIABLE */ + { 240, -4 }, /* (139) interval_opt ::= INTERVAL LP tmvar RP */ + { 240, 0 }, /* (140) interval_opt ::= */ + { 241, 0 }, /* (141) fill_opt ::= */ + { 241, -6 }, /* (142) fill_opt ::= FILL LP ID COMMA tagitemlist RP */ + { 241, -4 }, /* (143) fill_opt ::= FILL LP ID RP */ + { 242, -4 }, /* (144) sliding_opt ::= SLIDING LP tmvar RP */ + { 242, 0 }, /* (145) sliding_opt ::= */ + { 244, 0 }, /* (146) orderby_opt ::= */ + { 244, -3 }, /* (147) orderby_opt ::= ORDER BY sortlist */ + { 254, -4 }, /* (148) sortlist ::= sortlist COMMA item sortorder */ + { 254, -2 }, /* (149) sortlist ::= item sortorder */ + { 256, -2 }, /* (150) item ::= ids cpxName */ + { 257, -1 }, /* (151) sortorder ::= ASC */ + { 257, -1 }, /* (152) sortorder ::= DESC */ + { 257, 0 }, /* (153) sortorder ::= */ + { 243, 0 }, /* (154) groupby_opt ::= */ + { 243, -3 }, /* (155) groupby_opt ::= GROUP BY grouplist */ + { 258, -3 }, /* (156) grouplist ::= grouplist COMMA item */ + { 258, -1 }, /* (157) grouplist ::= item */ + { 245, 0 }, /* (158) having_opt ::= */ + { 245, -2 }, /* (159) having_opt ::= HAVING expr */ + { 247, 0 }, /* (160) limit_opt ::= */ + { 247, -2 }, /* (161) limit_opt ::= LIMIT signed */ + { 247, -4 }, /* (162) limit_opt ::= LIMIT signed OFFSET signed */ + { 247, -4 }, /* (163) limit_opt ::= LIMIT signed COMMA signed */ + { 246, 0 }, /* (164) slimit_opt ::= */ + { 246, -2 }, /* (165) slimit_opt ::= SLIMIT signed */ + { 246, -4 }, /* (166) slimit_opt ::= SLIMIT signed SOFFSET signed */ + { 246, -4 }, /* (167) slimit_opt ::= SLIMIT signed COMMA signed */ + { 239, 0 }, /* (168) where_opt ::= */ + { 239, -2 }, /* (169) where_opt ::= WHERE expr */ + { 250, -3 }, /* (170) expr ::= LP expr RP */ + { 250, -1 }, /* (171) expr ::= ID */ + { 250, -3 }, /* (172) expr ::= ID DOT ID */ + { 250, -3 }, /* (173) expr ::= ID DOT STAR */ + { 250, -1 }, /* (174) expr ::= INTEGER */ + { 250, -2 }, /* (175) expr ::= MINUS INTEGER */ + { 250, -2 }, /* (176) expr ::= PLUS INTEGER */ + { 250, -1 }, /* (177) expr ::= FLOAT */ + { 250, -2 }, /* (178) expr ::= MINUS FLOAT */ + { 250, -2 }, /* (179) expr ::= PLUS FLOAT */ + { 250, -1 }, /* (180) expr ::= STRING */ + { 250, -1 }, /* (181) expr ::= NOW */ + { 250, -1 }, /* (182) expr ::= VARIABLE */ + { 250, -1 }, /* (183) expr ::= BOOL */ + { 250, -4 }, /* (184) expr ::= ID LP exprlist RP */ + { 250, -4 }, /* (185) expr ::= ID LP STAR RP */ + { 250, -3 }, /* (186) expr ::= expr AND expr */ + { 250, -3 }, /* (187) expr ::= expr OR expr */ + { 250, -3 }, /* (188) expr ::= expr LT expr */ + { 250, -3 }, /* (189) expr ::= expr GT expr */ + { 250, -3 }, /* (190) expr ::= expr LE expr */ + { 250, -3 }, /* (191) expr ::= expr GE expr */ + { 250, -3 }, /* (192) expr ::= expr NE expr */ + { 250, -3 }, /* (193) expr ::= expr EQ expr */ + { 250, -3 }, /* (194) expr ::= expr PLUS expr */ + { 250, -3 }, /* (195) expr ::= expr MINUS expr */ + { 250, -3 }, /* (196) expr ::= expr STAR expr */ + { 250, -3 }, /* (197) expr ::= expr SLASH expr */ + { 250, -3 }, /* (198) expr ::= expr REM expr */ + { 250, -3 }, /* (199) expr ::= expr LIKE expr */ + { 250, -5 }, /* (200) expr ::= expr IN LP exprlist RP */ + { 259, -3 }, /* (201) exprlist ::= exprlist COMMA expritem */ + { 259, -1 }, /* (202) exprlist ::= expritem */ + { 260, -1 }, /* (203) expritem ::= expr */ + { 260, 0 }, /* (204) expritem ::= */ + { 199, -3 }, /* (205) cmd ::= RESET QUERY CACHE */ + { 199, -7 }, /* (206) cmd ::= ALTER TABLE ids cpxName ADD COLUMN columnlist */ + { 199, -7 }, /* (207) cmd ::= ALTER TABLE ids cpxName DROP COLUMN ids */ + { 199, -7 }, /* (208) cmd ::= ALTER TABLE ids cpxName ADD TAG columnlist */ + { 199, -7 }, /* (209) cmd ::= ALTER TABLE ids cpxName DROP TAG ids */ + { 199, -8 }, /* (210) cmd ::= ALTER TABLE ids cpxName CHANGE TAG ids ids */ + { 199, -9 }, /* (211) cmd ::= ALTER TABLE ids cpxName SET TAG ids EQ tagitem */ + { 199, -5 }, /* (212) cmd ::= KILL CONNECTION IPTOKEN COLON INTEGER */ + { 199, -7 }, /* (213) cmd ::= KILL STREAM IPTOKEN COLON INTEGER COLON INTEGER */ + { 199, -7 }, /* (214) cmd ::= KILL QUERY IPTOKEN COLON INTEGER COLON INTEGER */ }; static void yy_accept(yyParser*); /* Forward Declaration */ @@ -2313,85 +2319,86 @@ static void yy_reduce( yymsp[-11].minor.yy138 = yylhsminor.yy138; break; case 122: /* union ::= select */ -{ - yylhsminor.yy117 = setSubclause(NULL, yymsp[0].minor.yy138); -} +{ yylhsminor.yy117 = setSubclause(NULL, yymsp[0].minor.yy138); } yymsp[0].minor.yy117 = yylhsminor.yy117; break; - case 123: /* union ::= union UNION select */ -{ - yylhsminor.yy117 = appendSelectClause(yymsp[-2].minor.yy117, yymsp[0].minor.yy138); -} - yymsp[-2].minor.yy117 = yylhsminor.yy117; + case 123: /* union ::= LP union RP */ +{ yymsp[-2].minor.yy117 = yymsp[-1].minor.yy117; } break; - case 124: /* cmd ::= union */ -{ - setSQLInfo(pInfo, yymsp[0].minor.yy117, NULL, TSDB_SQL_SELECT); -} + case 124: /* union ::= union UNION ALL select */ +{ yylhsminor.yy117 = appendSelectClause(yymsp[-3].minor.yy117, yymsp[0].minor.yy138); } + yymsp[-3].minor.yy117 = yylhsminor.yy117; + break; + case 125: /* union ::= union UNION ALL LP select RP */ +{ yylhsminor.yy117 = appendSelectClause(yymsp[-5].minor.yy117, yymsp[-1].minor.yy138); } + yymsp[-5].minor.yy117 = yylhsminor.yy117; + break; + case 126: /* cmd ::= union */ +{ setSQLInfo(pInfo, yymsp[0].minor.yy117, NULL, TSDB_SQL_SELECT); } break; - case 125: /* select ::= SELECT selcollist */ + case 127: /* select ::= SELECT selcollist */ { yylhsminor.yy138 = tSetQuerySQLElems(&yymsp[-1].minor.yy0, yymsp[0].minor.yy284, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); } yymsp[-1].minor.yy138 = yylhsminor.yy138; break; - case 126: /* sclp ::= selcollist COMMA */ + case 128: /* sclp ::= selcollist COMMA */ {yylhsminor.yy284 = yymsp[-1].minor.yy284;} yymsp[-1].minor.yy284 = yylhsminor.yy284; break; - case 127: /* sclp ::= */ + case 129: /* sclp ::= */ {yymsp[1].minor.yy284 = 0;} break; - case 128: /* selcollist ::= sclp expr as */ + case 130: /* selcollist ::= sclp expr as */ { yylhsminor.yy284 = tSQLExprListAppend(yymsp[-2].minor.yy284, yymsp[-1].minor.yy244, yymsp[0].minor.yy0.n?&yymsp[0].minor.yy0:0); } yymsp[-2].minor.yy284 = yylhsminor.yy284; break; - case 129: /* selcollist ::= sclp STAR */ + case 131: /* selcollist ::= sclp STAR */ { tSQLExpr *pNode = tSQLExprIdValueCreate(NULL, TK_ALL); yylhsminor.yy284 = tSQLExprListAppend(yymsp[-1].minor.yy284, pNode, 0); } yymsp[-1].minor.yy284 = yylhsminor.yy284; break; - case 130: /* as ::= AS ids */ + case 132: /* as ::= AS ids */ { yymsp[-1].minor.yy0 = yymsp[0].minor.yy0; } break; - case 131: /* as ::= ids */ + case 133: /* as ::= ids */ { yylhsminor.yy0 = yymsp[0].minor.yy0; } yymsp[0].minor.yy0 = yylhsminor.yy0; break; - case 132: /* as ::= */ + case 134: /* as ::= */ { yymsp[1].minor.yy0.n = 0; } break; - case 133: /* from ::= FROM tablelist */ + case 135: /* from ::= FROM tablelist */ {yymsp[-1].minor.yy480 = yymsp[0].minor.yy480;} break; - case 134: /* tablelist ::= ids cpxName */ + case 136: /* tablelist ::= ids cpxName */ { toTSDBType(yymsp[-1].minor.yy0.type); yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; yylhsminor.yy480 = tVariantListAppendToken(NULL, &yymsp[-1].minor.yy0, -1);} yymsp[-1].minor.yy480 = yylhsminor.yy480; break; - case 135: /* tablelist ::= tablelist COMMA ids cpxName */ + case 137: /* tablelist ::= tablelist COMMA ids cpxName */ { toTSDBType(yymsp[-1].minor.yy0.type); yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; yylhsminor.yy480 = tVariantListAppendToken(yymsp[-3].minor.yy480, &yymsp[-1].minor.yy0, -1); } yymsp[-3].minor.yy480 = yylhsminor.yy480; break; - case 136: /* tmvar ::= VARIABLE */ + case 138: /* tmvar ::= VARIABLE */ {yylhsminor.yy0 = yymsp[0].minor.yy0;} yymsp[0].minor.yy0 = yylhsminor.yy0; break; - case 137: /* interval_opt ::= INTERVAL LP tmvar RP */ - case 142: /* sliding_opt ::= SLIDING LP tmvar RP */ yytestcase(yyruleno==142); + case 139: /* interval_opt ::= INTERVAL LP tmvar RP */ + case 144: /* sliding_opt ::= SLIDING LP tmvar RP */ yytestcase(yyruleno==144); {yymsp[-3].minor.yy0 = yymsp[-1].minor.yy0; } break; - case 138: /* interval_opt ::= */ - case 143: /* sliding_opt ::= */ yytestcase(yyruleno==143); + case 140: /* interval_opt ::= */ + case 145: /* sliding_opt ::= */ yytestcase(yyruleno==145); {yymsp[1].minor.yy0.n = 0; yymsp[1].minor.yy0.z = NULL; yymsp[1].minor.yy0.type = 0; } break; - case 139: /* fill_opt ::= */ + case 141: /* fill_opt ::= */ {yymsp[1].minor.yy480 = 0; } break; - case 140: /* fill_opt ::= FILL LP ID COMMA tagitemlist RP */ + case 142: /* fill_opt ::= FILL LP ID COMMA tagitemlist RP */ { tVariant A = {0}; toTSDBType(yymsp[-3].minor.yy0.type); @@ -2401,33 +2408,33 @@ static void yy_reduce( yymsp[-5].minor.yy480 = yymsp[-1].minor.yy480; } break; - case 141: /* fill_opt ::= FILL LP ID RP */ + case 143: /* fill_opt ::= FILL LP ID RP */ { toTSDBType(yymsp[-1].minor.yy0.type); yymsp[-3].minor.yy480 = tVariantListAppendToken(NULL, &yymsp[-1].minor.yy0, -1); } break; - case 144: /* orderby_opt ::= */ - case 152: /* groupby_opt ::= */ yytestcase(yyruleno==152); + case 146: /* orderby_opt ::= */ + case 154: /* groupby_opt ::= */ yytestcase(yyruleno==154); {yymsp[1].minor.yy480 = 0;} break; - case 145: /* orderby_opt ::= ORDER BY sortlist */ - case 153: /* groupby_opt ::= GROUP BY grouplist */ yytestcase(yyruleno==153); + case 147: /* orderby_opt ::= ORDER BY sortlist */ + case 155: /* groupby_opt ::= GROUP BY grouplist */ yytestcase(yyruleno==155); {yymsp[-2].minor.yy480 = yymsp[0].minor.yy480;} break; - case 146: /* sortlist ::= sortlist COMMA item sortorder */ + case 148: /* sortlist ::= sortlist COMMA item sortorder */ { yylhsminor.yy480 = tVariantListAppend(yymsp[-3].minor.yy480, &yymsp[-1].minor.yy236, yymsp[0].minor.yy220); } yymsp[-3].minor.yy480 = yylhsminor.yy480; break; - case 147: /* sortlist ::= item sortorder */ + case 149: /* sortlist ::= item sortorder */ { yylhsminor.yy480 = tVariantListAppend(NULL, &yymsp[-1].minor.yy236, yymsp[0].minor.yy220); } yymsp[-1].minor.yy480 = yylhsminor.yy480; break; - case 148: /* item ::= ids cpxName */ + case 150: /* item ::= ids cpxName */ { toTSDBType(yymsp[-1].minor.yy0.type); yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; @@ -2436,196 +2443,196 @@ static void yy_reduce( } yymsp[-1].minor.yy236 = yylhsminor.yy236; break; - case 149: /* sortorder ::= ASC */ + case 151: /* sortorder ::= ASC */ {yymsp[0].minor.yy220 = TSQL_SO_ASC; } break; - case 150: /* sortorder ::= DESC */ + case 152: /* sortorder ::= DESC */ {yymsp[0].minor.yy220 = TSQL_SO_DESC;} break; - case 151: /* sortorder ::= */ + case 153: /* sortorder ::= */ {yymsp[1].minor.yy220 = TSQL_SO_ASC;} break; - case 154: /* grouplist ::= grouplist COMMA item */ + case 156: /* grouplist ::= grouplist COMMA item */ { yylhsminor.yy480 = tVariantListAppend(yymsp[-2].minor.yy480, &yymsp[0].minor.yy236, -1); } yymsp[-2].minor.yy480 = yylhsminor.yy480; break; - case 155: /* grouplist ::= item */ + case 157: /* grouplist ::= item */ { yylhsminor.yy480 = tVariantListAppend(NULL, &yymsp[0].minor.yy236, -1); } yymsp[0].minor.yy480 = yylhsminor.yy480; break; - case 156: /* having_opt ::= */ - case 166: /* where_opt ::= */ yytestcase(yyruleno==166); - case 202: /* expritem ::= */ yytestcase(yyruleno==202); + case 158: /* having_opt ::= */ + case 168: /* where_opt ::= */ yytestcase(yyruleno==168); + case 204: /* expritem ::= */ yytestcase(yyruleno==204); {yymsp[1].minor.yy244 = 0;} break; - case 157: /* having_opt ::= HAVING expr */ - case 167: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==167); + case 159: /* having_opt ::= HAVING expr */ + case 169: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==169); {yymsp[-1].minor.yy244 = yymsp[0].minor.yy244;} break; - case 158: /* limit_opt ::= */ - case 162: /* slimit_opt ::= */ yytestcase(yyruleno==162); + case 160: /* limit_opt ::= */ + case 164: /* slimit_opt ::= */ yytestcase(yyruleno==164); {yymsp[1].minor.yy162.limit = -1; yymsp[1].minor.yy162.offset = 0;} break; - case 159: /* limit_opt ::= LIMIT signed */ - case 163: /* slimit_opt ::= SLIMIT signed */ yytestcase(yyruleno==163); + case 161: /* limit_opt ::= LIMIT signed */ + case 165: /* slimit_opt ::= SLIMIT signed */ yytestcase(yyruleno==165); {yymsp[-1].minor.yy162.limit = yymsp[0].minor.yy369; yymsp[-1].minor.yy162.offset = 0;} break; - case 160: /* limit_opt ::= LIMIT signed OFFSET signed */ - case 164: /* slimit_opt ::= SLIMIT signed SOFFSET signed */ yytestcase(yyruleno==164); + case 162: /* limit_opt ::= LIMIT signed OFFSET signed */ + case 166: /* slimit_opt ::= SLIMIT signed SOFFSET signed */ yytestcase(yyruleno==166); {yymsp[-3].minor.yy162.limit = yymsp[-2].minor.yy369; yymsp[-3].minor.yy162.offset = yymsp[0].minor.yy369;} break; - case 161: /* limit_opt ::= LIMIT signed COMMA signed */ - case 165: /* slimit_opt ::= SLIMIT signed COMMA signed */ yytestcase(yyruleno==165); + case 163: /* limit_opt ::= LIMIT signed COMMA signed */ + case 167: /* slimit_opt ::= SLIMIT signed COMMA signed */ yytestcase(yyruleno==167); {yymsp[-3].minor.yy162.limit = yymsp[0].minor.yy369; yymsp[-3].minor.yy162.offset = yymsp[-2].minor.yy369;} break; - case 168: /* expr ::= LP expr RP */ + case 170: /* expr ::= LP expr RP */ {yymsp[-2].minor.yy244 = yymsp[-1].minor.yy244; } break; - case 169: /* expr ::= ID */ + case 171: /* expr ::= ID */ {yylhsminor.yy244 = tSQLExprIdValueCreate(&yymsp[0].minor.yy0, TK_ID);} yymsp[0].minor.yy244 = yylhsminor.yy244; break; - case 170: /* expr ::= ID DOT ID */ + case 172: /* expr ::= ID DOT ID */ {yymsp[-2].minor.yy0.n += (1+yymsp[0].minor.yy0.n); yylhsminor.yy244 = tSQLExprIdValueCreate(&yymsp[-2].minor.yy0, TK_ID);} yymsp[-2].minor.yy244 = yylhsminor.yy244; break; - case 171: /* expr ::= ID DOT STAR */ + case 173: /* expr ::= ID DOT STAR */ {yymsp[-2].minor.yy0.n += (1+yymsp[0].minor.yy0.n); yylhsminor.yy244 = tSQLExprIdValueCreate(&yymsp[-2].minor.yy0, TK_ALL);} yymsp[-2].minor.yy244 = yylhsminor.yy244; break; - case 172: /* expr ::= INTEGER */ + case 174: /* expr ::= INTEGER */ {yylhsminor.yy244 = tSQLExprIdValueCreate(&yymsp[0].minor.yy0, TK_INTEGER);} yymsp[0].minor.yy244 = yylhsminor.yy244; break; - case 173: /* expr ::= MINUS INTEGER */ - case 174: /* expr ::= PLUS INTEGER */ yytestcase(yyruleno==174); + case 175: /* expr ::= MINUS INTEGER */ + case 176: /* expr ::= PLUS INTEGER */ yytestcase(yyruleno==176); {yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; yymsp[-1].minor.yy0.type = TK_INTEGER; yylhsminor.yy244 = tSQLExprIdValueCreate(&yymsp[-1].minor.yy0, TK_INTEGER);} yymsp[-1].minor.yy244 = yylhsminor.yy244; break; - case 175: /* expr ::= FLOAT */ + case 177: /* expr ::= FLOAT */ {yylhsminor.yy244 = tSQLExprIdValueCreate(&yymsp[0].minor.yy0, TK_FLOAT);} yymsp[0].minor.yy244 = yylhsminor.yy244; break; - case 176: /* expr ::= MINUS FLOAT */ - case 177: /* expr ::= PLUS FLOAT */ yytestcase(yyruleno==177); + case 178: /* expr ::= MINUS FLOAT */ + case 179: /* expr ::= PLUS FLOAT */ yytestcase(yyruleno==179); {yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; yymsp[-1].minor.yy0.type = TK_FLOAT; yylhsminor.yy244 = tSQLExprIdValueCreate(&yymsp[-1].minor.yy0, TK_FLOAT);} yymsp[-1].minor.yy244 = yylhsminor.yy244; break; - case 178: /* expr ::= STRING */ + case 180: /* expr ::= STRING */ {yylhsminor.yy244 = tSQLExprIdValueCreate(&yymsp[0].minor.yy0, TK_STRING);} yymsp[0].minor.yy244 = yylhsminor.yy244; break; - case 179: /* expr ::= NOW */ + case 181: /* expr ::= NOW */ {yylhsminor.yy244 = tSQLExprIdValueCreate(&yymsp[0].minor.yy0, TK_NOW); } yymsp[0].minor.yy244 = yylhsminor.yy244; break; - case 180: /* expr ::= VARIABLE */ + case 182: /* expr ::= VARIABLE */ {yylhsminor.yy244 = tSQLExprIdValueCreate(&yymsp[0].minor.yy0, TK_VARIABLE);} yymsp[0].minor.yy244 = yylhsminor.yy244; break; - case 181: /* expr ::= BOOL */ + case 183: /* expr ::= BOOL */ {yylhsminor.yy244 = tSQLExprIdValueCreate(&yymsp[0].minor.yy0, TK_BOOL);} yymsp[0].minor.yy244 = yylhsminor.yy244; break; - case 182: /* expr ::= ID LP exprlist RP */ + case 184: /* expr ::= ID LP exprlist RP */ { yylhsminor.yy244 = tSQLExprCreateFunction(yymsp[-1].minor.yy284, &yymsp[-3].minor.yy0, &yymsp[0].minor.yy0, yymsp[-3].minor.yy0.type); } yymsp[-3].minor.yy244 = yylhsminor.yy244; break; - case 183: /* expr ::= ID LP STAR RP */ + case 185: /* expr ::= ID LP STAR RP */ { yylhsminor.yy244 = tSQLExprCreateFunction(NULL, &yymsp[-3].minor.yy0, &yymsp[0].minor.yy0, yymsp[-3].minor.yy0.type); } yymsp[-3].minor.yy244 = yylhsminor.yy244; break; - case 184: /* expr ::= expr AND expr */ + case 186: /* expr ::= expr AND expr */ {yylhsminor.yy244 = tSQLExprCreate(yymsp[-2].minor.yy244, yymsp[0].minor.yy244, TK_AND);} yymsp[-2].minor.yy244 = yylhsminor.yy244; break; - case 185: /* expr ::= expr OR expr */ + case 187: /* expr ::= expr OR expr */ {yylhsminor.yy244 = tSQLExprCreate(yymsp[-2].minor.yy244, yymsp[0].minor.yy244, TK_OR); } yymsp[-2].minor.yy244 = yylhsminor.yy244; break; - case 186: /* expr ::= expr LT expr */ + case 188: /* expr ::= expr LT expr */ {yylhsminor.yy244 = tSQLExprCreate(yymsp[-2].minor.yy244, yymsp[0].minor.yy244, TK_LT);} yymsp[-2].minor.yy244 = yylhsminor.yy244; break; - case 187: /* expr ::= expr GT expr */ + case 189: /* expr ::= expr GT expr */ {yylhsminor.yy244 = tSQLExprCreate(yymsp[-2].minor.yy244, yymsp[0].minor.yy244, TK_GT);} yymsp[-2].minor.yy244 = yylhsminor.yy244; break; - case 188: /* expr ::= expr LE expr */ + case 190: /* expr ::= expr LE expr */ {yylhsminor.yy244 = tSQLExprCreate(yymsp[-2].minor.yy244, yymsp[0].minor.yy244, TK_LE);} yymsp[-2].minor.yy244 = yylhsminor.yy244; break; - case 189: /* expr ::= expr GE expr */ + case 191: /* expr ::= expr GE expr */ {yylhsminor.yy244 = tSQLExprCreate(yymsp[-2].minor.yy244, yymsp[0].minor.yy244, TK_GE);} yymsp[-2].minor.yy244 = yylhsminor.yy244; break; - case 190: /* expr ::= expr NE expr */ + case 192: /* expr ::= expr NE expr */ {yylhsminor.yy244 = tSQLExprCreate(yymsp[-2].minor.yy244, yymsp[0].minor.yy244, TK_NE);} yymsp[-2].minor.yy244 = yylhsminor.yy244; break; - case 191: /* expr ::= expr EQ expr */ + case 193: /* expr ::= expr EQ expr */ {yylhsminor.yy244 = tSQLExprCreate(yymsp[-2].minor.yy244, yymsp[0].minor.yy244, TK_EQ);} yymsp[-2].minor.yy244 = yylhsminor.yy244; break; - case 192: /* expr ::= expr PLUS expr */ + case 194: /* expr ::= expr PLUS expr */ {yylhsminor.yy244 = tSQLExprCreate(yymsp[-2].minor.yy244, yymsp[0].minor.yy244, TK_PLUS); } yymsp[-2].minor.yy244 = yylhsminor.yy244; break; - case 193: /* expr ::= expr MINUS expr */ + case 195: /* expr ::= expr MINUS expr */ {yylhsminor.yy244 = tSQLExprCreate(yymsp[-2].minor.yy244, yymsp[0].minor.yy244, TK_MINUS); } yymsp[-2].minor.yy244 = yylhsminor.yy244; break; - case 194: /* expr ::= expr STAR expr */ + case 196: /* expr ::= expr STAR expr */ {yylhsminor.yy244 = tSQLExprCreate(yymsp[-2].minor.yy244, yymsp[0].minor.yy244, TK_STAR); } yymsp[-2].minor.yy244 = yylhsminor.yy244; break; - case 195: /* expr ::= expr SLASH expr */ + case 197: /* expr ::= expr SLASH expr */ {yylhsminor.yy244 = tSQLExprCreate(yymsp[-2].minor.yy244, yymsp[0].minor.yy244, TK_DIVIDE);} yymsp[-2].minor.yy244 = yylhsminor.yy244; break; - case 196: /* expr ::= expr REM expr */ + case 198: /* expr ::= expr REM expr */ {yylhsminor.yy244 = tSQLExprCreate(yymsp[-2].minor.yy244, yymsp[0].minor.yy244, TK_REM); } yymsp[-2].minor.yy244 = yylhsminor.yy244; break; - case 197: /* expr ::= expr LIKE expr */ + case 199: /* expr ::= expr LIKE expr */ {yylhsminor.yy244 = tSQLExprCreate(yymsp[-2].minor.yy244, yymsp[0].minor.yy244, TK_LIKE); } yymsp[-2].minor.yy244 = yylhsminor.yy244; break; - case 198: /* expr ::= expr IN LP exprlist RP */ + case 200: /* expr ::= expr IN LP exprlist RP */ {yylhsminor.yy244 = tSQLExprCreate(yymsp[-4].minor.yy244, (tSQLExpr*)yymsp[-1].minor.yy284, TK_IN); } yymsp[-4].minor.yy244 = yylhsminor.yy244; break; - case 199: /* exprlist ::= exprlist COMMA expritem */ + case 201: /* exprlist ::= exprlist COMMA expritem */ {yylhsminor.yy284 = tSQLExprListAppend(yymsp[-2].minor.yy284,yymsp[0].minor.yy244,0);} yymsp[-2].minor.yy284 = yylhsminor.yy284; break; - case 200: /* exprlist ::= expritem */ + case 202: /* exprlist ::= expritem */ {yylhsminor.yy284 = tSQLExprListAppend(0,yymsp[0].minor.yy244,0);} yymsp[0].minor.yy284 = yylhsminor.yy284; break; - case 201: /* expritem ::= expr */ + case 203: /* expritem ::= expr */ {yylhsminor.yy244 = yymsp[0].minor.yy244;} yymsp[0].minor.yy244 = yylhsminor.yy244; break; - case 203: /* cmd ::= RESET QUERY CACHE */ + case 205: /* cmd ::= RESET QUERY CACHE */ { setDCLSQLElems(pInfo, TSDB_SQL_RESET_CACHE, 0);} break; - case 204: /* cmd ::= ALTER TABLE ids cpxName ADD COLUMN columnlist */ + case 206: /* cmd ::= ALTER TABLE ids cpxName ADD COLUMN columnlist */ { yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; SAlterTableSQL* pAlterTable = tAlterTableSQLElems(&yymsp[-4].minor.yy0, yymsp[0].minor.yy421, NULL, TSDB_ALTER_TABLE_ADD_COLUMN); setSQLInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); } break; - case 205: /* cmd ::= ALTER TABLE ids cpxName DROP COLUMN ids */ + case 207: /* cmd ::= ALTER TABLE ids cpxName DROP COLUMN ids */ { yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; @@ -2636,14 +2643,14 @@ static void yy_reduce( setSQLInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); } break; - case 206: /* cmd ::= ALTER TABLE ids cpxName ADD TAG columnlist */ + case 208: /* cmd ::= ALTER TABLE ids cpxName ADD TAG columnlist */ { yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; SAlterTableSQL* pAlterTable = tAlterTableSQLElems(&yymsp[-4].minor.yy0, yymsp[0].minor.yy421, NULL, TSDB_ALTER_TABLE_ADD_TAG_COLUMN); setSQLInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); } break; - case 207: /* cmd ::= ALTER TABLE ids cpxName DROP TAG ids */ + case 209: /* cmd ::= ALTER TABLE ids cpxName DROP TAG ids */ { yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; @@ -2654,7 +2661,7 @@ static void yy_reduce( setSQLInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); } break; - case 208: /* cmd ::= ALTER TABLE ids cpxName CHANGE TAG ids ids */ + case 210: /* cmd ::= ALTER TABLE ids cpxName CHANGE TAG ids ids */ { yymsp[-5].minor.yy0.n += yymsp[-4].minor.yy0.n; @@ -2668,7 +2675,7 @@ static void yy_reduce( setSQLInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); } break; - case 209: /* cmd ::= ALTER TABLE ids cpxName SET TAG ids EQ tagitem */ + case 211: /* cmd ::= ALTER TABLE ids cpxName SET TAG ids EQ tagitem */ { yymsp[-6].minor.yy0.n += yymsp[-5].minor.yy0.n; @@ -2680,13 +2687,13 @@ static void yy_reduce( setSQLInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); } break; - case 210: /* cmd ::= KILL CONNECTION IPTOKEN COLON INTEGER */ + case 212: /* cmd ::= KILL CONNECTION IPTOKEN COLON INTEGER */ {yymsp[-2].minor.yy0.n += (yymsp[-1].minor.yy0.n + yymsp[0].minor.yy0.n); setKillSQL(pInfo, TSDB_SQL_KILL_CONNECTION, &yymsp[-2].minor.yy0);} break; - case 211: /* cmd ::= KILL STREAM IPTOKEN COLON INTEGER COLON INTEGER */ + case 213: /* cmd ::= KILL STREAM IPTOKEN COLON INTEGER COLON INTEGER */ {yymsp[-4].minor.yy0.n += (yymsp[-3].minor.yy0.n + yymsp[-2].minor.yy0.n + yymsp[-1].minor.yy0.n + yymsp[0].minor.yy0.n); setKillSQL(pInfo, TSDB_SQL_KILL_STREAM, &yymsp[-4].minor.yy0);} break; - case 212: /* cmd ::= KILL QUERY IPTOKEN COLON INTEGER COLON INTEGER */ + case 214: /* cmd ::= KILL QUERY IPTOKEN COLON INTEGER COLON INTEGER */ {yymsp[-4].minor.yy0.n += (yymsp[-3].minor.yy0.n + yymsp[-2].minor.yy0.n + yymsp[-1].minor.yy0.n + yymsp[0].minor.yy0.n); setKillSQL(pInfo, TSDB_SQL_KILL_QUERY, &yymsp[-4].minor.yy0);} break; default: diff --git a/src/client/src/tscAsync.c b/src/client/src/tscAsync.c index eab8545063b2e4a426bd11aa28f2c9ed12280a58..7dfaa82dbba633def31b6d886ced0dc172965ae9 100644 --- a/src/client/src/tscAsync.c +++ b/src/client/src/tscAsync.c @@ -118,10 +118,12 @@ static void tscProcessAsyncFetchRowsProxy(void *param, TAOS_RES *tres, int numOf SSqlRes *pRes = &pSql->res; SSqlCmd *pCmd = &pSql->cmd; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + // sequentially retrieve data from remain vnodes first, query vnode specified by vnodeIdx - if (numOfRows == 0 && tscProjectionQueryOnMetric(pCmd)) { + if (numOfRows == 0 && tscProjectionQueryOnMetric(pCmd, 0)) { // vnode is denoted by vnodeIdx, continue to query vnode specified by vnodeIdx - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); assert(pMeterMetaInfo->vnodeIndex >= 0); /* reach the maximum number of output rows, abort */ @@ -131,8 +133,9 @@ static void tscProcessAsyncFetchRowsProxy(void *param, TAOS_RES *tres, int numOf } /* update the limit value according to current retrieval results */ - pCmd->pQueryInfo->limit.limit = pCmd->globalLimit - pRes->numOfTotal; - pCmd->pQueryInfo->limit.offset = pRes->offset; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + pQueryInfo->limit.limit = pCmd->globalLimit - pRes->numOfTotal; + pQueryInfo->limit.offset = pRes->offset; if ((++(pMeterMetaInfo->vnodeIndex)) < pMeterMetaInfo->pMetricMeta->numOfVnodes) { tscTrace("%p retrieve data from next vnode:%d", pSql, pMeterMetaInfo->vnodeIndex); @@ -266,13 +269,14 @@ void tscProcessAsyncRetrieve(void *param, TAOS_RES *tres, int numOfRows) { SSqlRes *pRes = &pSql->res; SSqlCmd *pCmd = &pSql->cmd; + SQueryInfo *pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); if (numOfRows == 0) { // sequentially retrieve data from remain vnodes. - if (tscProjectionQueryOnMetric(pCmd)) { + if (tscProjectionQueryOnMetric(pCmd, 0)) { /* * vnode is denoted by vnodeIdx, continue to query vnode specified by vnodeIdx till all vnode have been retrieved */ - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0); assert(pMeterMetaInfo->vnodeIndex >= 0); /* reach the maximum number of output rows, abort */ @@ -282,7 +286,8 @@ void tscProcessAsyncRetrieve(void *param, TAOS_RES *tres, int numOfRows) { } /* update the limit value according to current retrieval results */ - pCmd->pQueryInfo->limit.limit = pCmd->globalLimit - pRes->numOfTotal; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + pQueryInfo->limit.limit = pCmd->globalLimit - pRes->numOfTotal; if ((++pMeterMetaInfo->vnodeIndex) <= pMeterMetaInfo->pMetricMeta->numOfVnodes) { pSql->cmd.command = TSDB_SQL_SELECT; // reset flag to launch query first. @@ -297,7 +302,7 @@ void tscProcessAsyncRetrieve(void *param, TAOS_RES *tres, int numOfRows) { } } else { for (int i = 0; i < pCmd->numOfCols; ++i) - pRes->tsrow[i] = TSC_GET_RESPTR_BASE(pRes, pCmd, i, pCmd->order) + pRes->bytes[i] * pRes->row; + pRes->tsrow[i] = TSC_GET_RESPTR_BASE(pRes, pQueryInfo, i, pQueryInfo->order) + pRes->bytes[i] * pRes->row; pRes->row++; (*pSql->fetchFp)(pSql->param, pSql, pSql->res.tsrow); @@ -308,9 +313,13 @@ void tscProcessFetchRow(SSchedMsg *pMsg) { SSqlObj *pSql = (SSqlObj *)pMsg->ahandle; SSqlRes *pRes = &pSql->res; SSqlCmd *pCmd = &pSql->cmd; + assert(0); + + SQueryInfo *pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + assert(pCmd->numOfCols == pQueryInfo->fieldsInfo.numOfOutputCols); for (int i = 0; i < pCmd->numOfCols; ++i) - pRes->tsrow[i] = TSC_GET_RESPTR_BASE(pRes, pCmd, i, pCmd->order) + pRes->bytes[i] * pRes->row; + pRes->tsrow[i] = TSC_GET_RESPTR_BASE(pRes, pQueryInfo, i, pQueryInfo->order) + pRes->bytes[i] * pRes->row; pRes->row++; (*pSql->fetchFp)(pSql->param, pSql, pRes->tsrow); @@ -406,8 +415,11 @@ void tscAsyncInsertMultiVnodesProxy(void *param, TAOS_RES *tres, int numOfRows) assert(!pCmd->isInsertFromFile && pSql->signature == pSql); - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); - assert(pCmd->pQueryInfo->numOfTables == 1); + int32_t index = 0; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, index); + + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, index, 0); + assert(pQueryInfo->numOfTables == 1); SDataBlockList *pDataBlocks = pCmd->pDataBlocks; if (pDataBlocks == NULL || pMeterMetaInfo->vnodeIndex >= pDataBlocks->nSize) { @@ -464,10 +476,11 @@ void tscMeterMetaCallBack(void *param, TAOS_RES *res, int code) { } else { tscTrace("%p renew meterMeta successfully, command:%d, code:%d, thandle:%p, retry:%d", pSql, pSql->cmd.command, pSql->res.code, pSql->thandle, pSql->retry); - - assert(tscGetMeterMetaInfo(&pSql->cmd, 0)->pMeterMeta == NULL); - tscGetMeterMeta(pSql, tscGetMeterMetaInfo(&pSql->cmd, 0)->name, 0); - + + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0, 0); + assert(pMeterMetaInfo->pMeterMeta == NULL); + + tscGetMeterMeta(pSql, pMeterMetaInfo->name, 0); code = tscSendMsgToServer(pSql); if (code != 0) { pRes->code = code; @@ -486,18 +499,21 @@ void tscMeterMetaCallBack(void *param, TAOS_RES *res, int code) { if (pSql->pStream == NULL) { // check if it is a sub-query of metric query first, if true, enter another routine - if ((pSql->cmd.type & TSDB_QUERY_TYPE_STABLE_SUBQUERY) == TSDB_QUERY_TYPE_STABLE_SUBQUERY) { - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + + if ((pQueryInfo->type & TSDB_QUERY_TYPE_STABLE_SUBQUERY) == TSDB_QUERY_TYPE_STABLE_SUBQUERY) { + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); assert(pMeterMetaInfo->pMeterMeta->numOfTags != 0 && pMeterMetaInfo->vnodeIndex >= 0 && pSql->param != NULL); SRetrieveSupport *trs = (SRetrieveSupport *)pSql->param; SSqlObj * pParObj = trs->pParentSqlObj; + assert(pParObj->signature == pParObj && trs->subqueryIndex == pMeterMetaInfo->vnodeIndex && pMeterMetaInfo->pMeterMeta->numOfTags != 0); tscTrace("%p get metricMeta during metric query successfully", pSql); - - code = tscGetMeterMeta(pSql, tscGetMeterMetaInfo(&pSql->cmd, 0)->name, 0); + + code = tscGetMeterMeta(pSql, pMeterMetaInfo->name, 0); pRes->code = code; if (code == TSDB_CODE_ACTION_IN_PROGRESS) return; @@ -512,14 +528,14 @@ void tscMeterMetaCallBack(void *param, TAOS_RES *res, int code) { } } else { // stream computing - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0); code = tscGetMeterMeta(pSql, pMeterMetaInfo->name, 0); pRes->code = code; if (code == TSDB_CODE_ACTION_IN_PROGRESS) return; - pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); - if (code == TSDB_CODE_SUCCESS && UTIL_METER_IS_METRIC(pMeterMetaInfo)) { + pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0); + if (code == TSDB_CODE_SUCCESS && UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo)) { code = tscGetMetricMeta(pSql); pRes->code = code; @@ -539,7 +555,9 @@ void tscMeterMetaCallBack(void *param, TAOS_RES *res, int code) { * transfer the sql function for metric query before get meter/metric meta, * since in callback functions, only tscProcessSql(pStream->pSql) is executed! */ - tscTansformSQLFunctionForMetricQuery(&pSql->cmd); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + + tscTansformSQLFunctionForMetricQuery(pQueryInfo); tscIncStreamExecutionCount(pSql->pStream); } else { tscTrace("%p get meterMeta/metricMeta successfully", pSql); diff --git a/src/client/src/tscJoinProcess.c b/src/client/src/tscJoinProcess.c index bb6b14352d17755f7a302a8d4b3bc33be883437e..4ea958309dd41af08666b21b8752875930cd205d 100644 --- a/src/client/src/tscJoinProcess.c +++ b/src/client/src/tscJoinProcess.c @@ -53,11 +53,16 @@ static int64_t doTSBlockIntersect(SSqlObj* pSql, SJoinSubquerySupporter* pSuppor *st = INT64_MAX; *et = INT64_MIN; - SLimitVal* pLimit = &pSql->cmd.pQueryInfo->limit; - int32_t order = pSql->cmd.order.order; - - pSql->pSubs[0]->cmd.pQueryInfo->tsBuf = output1; - pSql->pSubs[1]->cmd.pQueryInfo->tsBuf = output2; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + + SLimitVal* pLimit = &pQueryInfo->limit; + int32_t order = pQueryInfo->order.order; + + SQueryInfo* pSubQueryInfo1 = tscGetQueryInfoDetail(&pSql->pSubs[0]->cmd, 0); + SQueryInfo* pSubQueryInfo2 = tscGetQueryInfoDetail(&pSql->pSubs[1]->cmd, 0); + + pSubQueryInfo1->tsBuf = output1; + pSubQueryInfo2->tsBuf = output2; tsBufResetPos(pSupporter1->pTSBuf); tsBufResetPos(pSupporter2->pTSBuf); @@ -113,7 +118,9 @@ static int64_t doTSBlockIntersect(SSqlObj* pSql, SJoinSubquerySupporter* pSuppor } // in case of stable query, limit/offset is not applied here - if (pLimit->offset == 0 || pSql->cmd.pQueryInfo->nAggTimeInterval > 0 || QUERY_IS_STABLE_QUERY(pSql->cmd.type)) { + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + + if (pLimit->offset == 0 || pQueryInfo->nAggTimeInterval > 0 || QUERY_IS_STABLE_QUERY(pQueryInfo->type)) { tsBufAppend(output1, elem1.vnode, elem1.tag, (const char*)&elem1.ts, sizeof(elem1.ts)); tsBufAppend(output2, elem2.vnode, elem2.tag, (const char*)&elem2.ts, sizeof(elem2.ts)); } else { @@ -168,10 +175,12 @@ SJoinSubquerySupporter* tscCreateJoinSupporter(SSqlObj* pSql, SSubqueryState* pS pSupporter->pState = pState; pSupporter->subqueryIndex = index; - pSupporter->interval = pSql->cmd.pQueryInfo->nAggTimeInterval; - pSupporter->limit = pSql->cmd.pQueryInfo->limit; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + + pSupporter->interval = pQueryInfo->nAggTimeInterval; + pSupporter->limit = pQueryInfo->limit; - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, index); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0, index); pSupporter->uid = pMeterMetaInfo->pMeterMeta->uid; getTmpfilePath("join-", pSupporter->path); @@ -209,10 +218,9 @@ void tscDestroyJoinSupporter(SJoinSubquerySupporter* pSupporter) { * primary timestamp column , the secondary query is not necessary * */ -bool needSecondaryQuery(SSqlObj* pSql) { - SSqlCmd* pCmd = &pSql->cmd; - for (int32_t i = 0; i < pCmd->pQueryInfo->colList.numOfCols; ++i) { - SColumnBase* pBase = tscColumnBaseInfoGet(&pCmd->pQueryInfo->colList, i); +bool needSecondaryQuery(SQueryInfo* pQueryInfo) { + for (int32_t i = 0; i < pQueryInfo->colList.numOfCols; ++i) { + SColumnBase* pBase = tscColumnBaseInfoGet(&pQueryInfo->colList, i); if (pBase->colIndex.columnIndex != PRIMARYKEY_TIMESTAMP_COL_INDEX) { return true; } @@ -272,53 +280,58 @@ int32_t tscLaunchSecondSubquery(SSqlObj* pSql) { tscFreeSqlCmdData(&pNew->cmd); pSql->pSubs[j++] = pNew; - pNew->cmd.pQueryInfo->tsBuf = pSub->cmd.pQueryInfo->tsBuf; - pSub->cmd.pQueryInfo->tsBuf = NULL; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pNew->cmd, 0); + SQueryInfo* pSubQueryInfo = tscGetQueryInfoDetail(&pSub->cmd, 0); + + pQueryInfo->tsBuf = pSubQueryInfo->tsBuf; + pSubQueryInfo->tsBuf = NULL; taos_free_result(pSub); // set the second stage sub query for join process - pNew->cmd.type |= TSDB_QUERY_TYPE_JOIN_SEC_STAGE; + pQueryInfo->type |= TSDB_QUERY_TYPE_JOIN_SEC_STAGE; - pNew->cmd.pQueryInfo->nAggTimeInterval = pSupporter->interval; - pNew->cmd.pQueryInfo->groupbyExpr = pSupporter->groupbyExpr; + pQueryInfo->nAggTimeInterval = pSupporter->interval; + pQueryInfo->groupbyExpr = pSupporter->groupbyExpr; - tscColumnBaseInfoCopy(&pNew->cmd.pQueryInfo->colList, &pSupporter->colList, 0); - tscTagCondCopy(&pNew->cmd.pQueryInfo->tagCond, &pSupporter->tagCond); + tscColumnBaseInfoCopy(&pQueryInfo->colList, &pSupporter->colList, 0); + tscTagCondCopy(&pQueryInfo->tagCond, &pSupporter->tagCond); - tscSqlExprCopy(&pNew->cmd.pQueryInfo->exprsInfo, &pSupporter->exprsInfo, pSupporter->uid); - tscFieldInfoCopyAll(&pSupporter->fieldsInfo, &pNew->cmd.pQueryInfo->fieldsInfo); + tscSqlExprCopy(&pQueryInfo->exprsInfo, &pSupporter->exprsInfo, pSupporter->uid); + tscFieldInfoCopyAll(&pSupporter->fieldsInfo, &pQueryInfo->fieldsInfo); // add the ts function for interval query if it is missing - if (pSupporter->exprsInfo.pExprs[0].functionId != TSDB_FUNC_TS && pNew->cmd.pQueryInfo->nAggTimeInterval > 0) { - tscAddTimestampColumn(&pNew->cmd, TSDB_FUNC_TS, 0); + if (pSupporter->exprsInfo.pExprs[0].functionId != TSDB_FUNC_TS && pQueryInfo->nAggTimeInterval > 0) { + tscAddTimestampColumn(pQueryInfo, TSDB_FUNC_TS, 0); } // todo refactor function name - tscAddTimestampColumn(&pNew->cmd, TSDB_FUNC_TS, 0); - tscFieldInfoCalOffset(&pNew->cmd); + SQueryInfo* pNewQueryInfo = tscGetQueryInfoDetail(&pNew->cmd, 0); + + tscAddTimestampColumn(pQueryInfo, TSDB_FUNC_TS, 0); + tscFieldInfoCalOffset(pNewQueryInfo); - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(&pNew->cmd, 0); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pNewQueryInfo, 0); /* * When handling the projection query, the offset value will be modified for table-table join, which is changed * during the timestamp intersection. */ - pSupporter->limit = pSql->cmd.pQueryInfo->limit; - pNew->cmd.pQueryInfo->limit = pSupporter->limit; + pSupporter->limit = pQueryInfo->limit; + pNewQueryInfo->limit = pSupporter->limit; // fetch the join tag column - if (UTIL_METER_IS_METRIC(pMeterMetaInfo)) { - SSqlExpr* pExpr = tscSqlExprGet(&pNew->cmd, 0); - assert(pNew->cmd.pQueryInfo->tagCond.joinInfo.hasJoin); + if (UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo)) { + SSqlExpr* pExpr = tscSqlExprGet(pNewQueryInfo, 0); + assert(pQueryInfo->tagCond.joinInfo.hasJoin); - int16_t tagColIndex = tscGetJoinTagColIndexByUid(&pNew->cmd.pQueryInfo->tagCond, pMeterMetaInfo->pMeterMeta->uid); + int16_t tagColIndex = tscGetJoinTagColIndexByUid(&pQueryInfo->tagCond, pMeterMetaInfo->pMeterMeta->uid); pExpr->param[0].i64Key = tagColIndex; pExpr->numOfParams = 1; } #ifdef _DEBUG_VIEW - tscPrintSelectClause(&pNew->cmd); + tscPrintSelectClause(&pNew->cmd, 0); #endif tscProcessSql(pNew); @@ -370,10 +383,12 @@ static void quitAllSubquery(SSqlObj* pSqlObj, SJoinSubquerySupporter* pSupporter // update the query time range according to the join results on timestamp static void updateQueryTimeRange(SSqlObj* pSql, int64_t st, int64_t et) { - assert(pSql->cmd.pQueryInfo->stime <= st && pSql->cmd.pQueryInfo->etime >= et); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + + assert(pQueryInfo->stime <= st && pQueryInfo->etime >= et); - pSql->cmd.pQueryInfo->stime = st; - pSql->cmd.pQueryInfo->etime = et; + pQueryInfo->stime = st; + pQueryInfo->etime = et; } static void joinRetrieveCallback(void* param, TAOS_RES* tres, int numOfRows) { @@ -381,8 +396,9 @@ static void joinRetrieveCallback(void* param, TAOS_RES* tres, int numOfRows) { SSqlObj* pParentSql = pSupporter->pObj; SSqlObj* pSql = (SSqlObj*)tres; - - if ((pSql->cmd.type & TSDB_QUERY_TYPE_JOIN_SEC_STAGE) == 0) { + SQueryInfo *pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + + if ((pQueryInfo->type & TSDB_QUERY_TYPE_JOIN_SEC_STAGE) == 0) { if (pSupporter->pState->code != TSDB_CODE_SUCCESS) { tscError("%p abort query due to other subquery failure. code:%d, global code:%d", pSql, numOfRows, pSupporter->pState->code); @@ -408,8 +424,8 @@ static void joinRetrieveCallback(void* param, TAOS_RES* tres, int numOfRows) { tscTrace("%p create tmp file for ts block:%s", pSql, pBuf->path); pSupporter->pTSBuf = pBuf; } else { - assert(pSql->cmd.pQueryInfo->numOfTables == 1); // for subquery, only one metermetaInfo - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0); + assert(pQueryInfo->numOfTables == 1); // for subquery, only one metermetaInfo + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); tsBufMerge(pSupporter->pTSBuf, pBuf, pMeterMetaInfo->vnodeIndex); tsBufDestory(pBuf); @@ -422,9 +438,11 @@ static void joinRetrieveCallback(void* param, TAOS_RES* tres, int numOfRows) { taos_fetch_rows_a(tres, joinRetrieveCallback, param); } else if (numOfRows == 0) { // no data from this vnode anymore - if (tscProjectionQueryOnMetric(&pParentSql->cmd)) { - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0); - assert(pSql->cmd.pQueryInfo->numOfTables == 1); + if (tscProjectionQueryOnMetric(&pSql->cmd, 0)) { + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); + assert(pQueryInfo->numOfTables == 1); // for projection query, need to try next vnode if ((++pMeterMetaInfo->vnodeIndex) < pMeterMetaInfo->pMetricMeta->numOfVnodes) { @@ -477,10 +495,12 @@ static void joinRetrieveCallback(void* param, TAOS_RES* tres, int numOfRows) { if (numOfRows >= 0) { pSql->res.numOfTotal += pSql->res.numOfRows; } - - if (tscProjectionQueryOnMetric(&pSql->cmd) && numOfRows == 0) { - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0); - assert(pSql->cmd.pQueryInfo->numOfTables == 1); + + SSqlCmd* pCmd = &pSql->cmd; + + if (tscProjectionQueryOnMetric(pCmd, 0) && numOfRows == 0) { + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); + assert(pQueryInfo->numOfTables == 1); // for projection query, need to try next vnode if current vnode is exhausted if ((++pMeterMetaInfo->vnodeIndex) < pMeterMetaInfo->pMetricMeta->numOfVnodes) { @@ -516,13 +536,14 @@ void tscFetchDatablockFromSubquery(SSqlObj* pSql) { assert(pSql->numOfSubs >= 1); + for (int32_t i = 0; i < pSql->numOfSubs; ++i) { SSqlRes *pRes = &pSql->pSubs[i]->res; - SSqlCmd *pCmd = &pSql->pSubs[i]->cmd; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->pSubs[i]->cmd, 0); - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); - if (tscProjectionQueryOnMetric(pCmd)) { + if (tscProjectionQueryOnMetric(&pSql->cmd, 0)) { if (pRes->row >= pRes->numOfRows && pMeterMetaInfo->vnodeIndex < pMeterMetaInfo->pMetricMeta->numOfVnodes && (!tscHasReachLimitation(pSql->pSubs[i]))) { numOfFetch++; @@ -555,9 +576,11 @@ void tscFetchDatablockFromSubquery(SSqlObj* pSql) { // wait for all subqueries completed pSupporter->pState->numOfTotal = numOfFetch; - assert(pRes1->numOfRows >= 0 && pCmd1->pQueryInfo->numOfTables == 1); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd1, 0); + + assert(pRes1->numOfRows >= 0 && pQueryInfo->numOfTables == 1); - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd1, 0); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); if (pRes1->row >= pRes1->numOfRows) { tscTrace("%p subquery:%p retrieve data from vnode, subquery:%d, vnodeIndex:%d", pSql, pSql1, @@ -589,16 +612,17 @@ void tscSetupOutputColumnIndex(SSqlObj* pSql) { return; // the column transfer support struct has been built } - pRes->pColumnIndex = calloc(1, sizeof(SColumnIndex) * pCmd->pQueryInfo->fieldsInfo.numOfOutputCols); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + pRes->pColumnIndex = calloc(1, sizeof(SColumnIndex) * pQueryInfo->fieldsInfo.numOfOutputCols); - for (int32_t i = 0; i < pCmd->pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { - SSqlExpr* pExpr = tscSqlExprGet(pCmd, i); + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i); int32_t tableIndexOfSub = -1; - for (int32_t j = 0; j < pCmd->pQueryInfo->numOfTables; ++j) { + for (int32_t j = 0; j < pQueryInfo->numOfTables; ++j) { SSqlObj* pSub = pSql->pSubs[j]; - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(&pSub->cmd, 0); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(&pSub->cmd, 0, 0); if (pMeterMetaInfo->pMeterMeta->uid == pExpr->uid) { tableIndexOfSub = j; break; @@ -606,9 +630,10 @@ void tscSetupOutputColumnIndex(SSqlObj* pSql) { } SSqlCmd* pSubCmd = &pSql->pSubs[tableIndexOfSub]->cmd; - - for (int32_t k = 0; k < pSubCmd->pQueryInfo->exprsInfo.numOfExprs; ++k) { - SSqlExpr* pSubExpr = tscSqlExprGet(pSubCmd, k); + SQueryInfo* pSubQueryInfo = tscGetQueryInfoDetail(pSubCmd, 0); + + for (int32_t k = 0; k < pSubQueryInfo->exprsInfo.numOfExprs; ++k) { + SSqlExpr* pSubExpr = tscSqlExprGet(pSubQueryInfo, k); if (pExpr->functionId == pSubExpr->functionId && pExpr->colInfo.colId == pSubExpr->colInfo.colId) { pRes->pColumnIndex[i] = (SColumnIndex){.tableIndex = tableIndexOfSub, .columnIndex = k}; break; @@ -619,7 +644,7 @@ void tscSetupOutputColumnIndex(SSqlObj* pSql) { void tscJoinQueryCallback(void* param, TAOS_RES* tres, int code) { SSqlObj* pSql = (SSqlObj*)tres; - // SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0); + // SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0, 0); // int32_t idx = pSql->cmd.vnodeIdx; @@ -648,7 +673,8 @@ void tscJoinQueryCallback(void* param, TAOS_RES* tres, int code) { // } // } else { - if ((pSql->cmd.type & TSDB_QUERY_TYPE_JOIN_SEC_STAGE) != TSDB_QUERY_TYPE_JOIN_SEC_STAGE) { + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + if ((pQueryInfo->type & TSDB_QUERY_TYPE_JOIN_SEC_STAGE) != TSDB_QUERY_TYPE_JOIN_SEC_STAGE) { if (code != TSDB_CODE_SUCCESS) { // direct call joinRetrieveCallback and set the error code joinRetrieveCallback(param, pSql, code); } else { // first stage query, continue to retrieve data @@ -678,13 +704,14 @@ void tscJoinQueryCallback(void* param, TAOS_RES* tres, int code) { if (atomic_add_fetch_32(&pSupporter->pState->numOfCompleted, 1) >= pSupporter->pState->numOfTotal) { tscSetupOutputColumnIndex(pParentSql); - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); /** * if the query is a continue query (vnodeIndex > 0 for projection query) for next vnode, do the retrieval of * data instead of returning to its invoker */ - if (pMeterMetaInfo->vnodeIndex > 0 && tscProjectionQueryOnMetric(&pSql->cmd)) { + if (pMeterMetaInfo->vnodeIndex > 0 && tscProjectionQueryOnMetric(&pSql->cmd, 0)) { assert(pMeterMetaInfo->vnodeIndex < pMeterMetaInfo->pMetricMeta->numOfVnodes); pSupporter->pState->numOfCompleted = 0; // reset the record value diff --git a/src/client/src/tscLocal.c b/src/client/src/tscLocal.c index ec39faf87d76c43390c1b8c5c8adba01d17343d2..4d5a5421235067334d2891aa9b440d365e979773 100644 --- a/src/client/src/tscLocal.c +++ b/src/client/src/tscLocal.c @@ -85,7 +85,7 @@ static int32_t getToStringLength(const char *pData, int32_t length, int32_t type * length((uint64_t) 123456789011) > 12, greater than sizsof(uint64_t) */ static int32_t tscMaxLengthOfTagsFields(SSqlObj *pSql) { - SMeterMeta *pMeta = tscGetMeterMetaInfo(&pSql->cmd, 0)->pMeterMeta; + SMeterMeta *pMeta = tscGetMeterMetaInfo(&pSql->cmd, 0, 0)->pMeterMeta; if (pMeta->meterType == TSDB_METER_METRIC || pMeta->meterType == TSDB_METER_OTABLE || pMeta->meterType == TSDB_METER_STABLE) { @@ -114,8 +114,9 @@ static int32_t tscSetValueToResObj(SSqlObj *pSql, int32_t rowLen) { SSqlRes *pRes = &pSql->res; // one column for each row - SSqlCmd * pCmd = &pSql->cmd; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); SMeterMeta * pMeta = pMeterMetaInfo->pMeterMeta; /* @@ -127,7 +128,7 @@ static int32_t tscSetValueToResObj(SSqlObj *pSql, int32_t rowLen) { int32_t numOfRows = pMeta->numOfColumns; int32_t totalNumOfRows = numOfRows + pMeta->numOfTags; - if (UTIL_METER_IS_METRIC(pMeterMetaInfo)) { + if (UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo)) { numOfRows = pMeta->numOfColumns + pMeta->numOfTags; } @@ -135,31 +136,31 @@ static int32_t tscSetValueToResObj(SSqlObj *pSql, int32_t rowLen) { SSchema *pSchema = tsGetSchema(pMeta); for (int32_t i = 0; i < numOfRows; ++i) { - TAOS_FIELD *pField = tscFieldInfoGetField(pCmd, 0); - strncpy(pRes->data + tscFieldInfoGetOffset(pCmd, 0) * totalNumOfRows + pField->bytes * i, pSchema[i].name, + TAOS_FIELD *pField = tscFieldInfoGetField(pQueryInfo, 0); + strncpy(pRes->data + tscFieldInfoGetOffset(pQueryInfo, 0) * totalNumOfRows + pField->bytes * i, pSchema[i].name, TSDB_COL_NAME_LEN); char *type = tDataTypeDesc[pSchema[i].type].aName; - pField = tscFieldInfoGetField(pCmd, 1); - strncpy(pRes->data + tscFieldInfoGetOffset(pCmd, 1) * totalNumOfRows + pField->bytes * i, type, pField->bytes); + pField = tscFieldInfoGetField(pQueryInfo, 1); + strncpy(pRes->data + tscFieldInfoGetOffset(pQueryInfo, 1) * totalNumOfRows + pField->bytes * i, type, pField->bytes); int32_t bytes = pSchema[i].bytes; if (pSchema[i].type == TSDB_DATA_TYPE_NCHAR) { bytes = bytes / TSDB_NCHAR_SIZE; } - pField = tscFieldInfoGetField(pCmd, 2); - *(int32_t *)(pRes->data + tscFieldInfoGetOffset(pCmd, 2) * totalNumOfRows + pField->bytes * i) = bytes; + pField = tscFieldInfoGetField(pQueryInfo, 2); + *(int32_t *)(pRes->data + tscFieldInfoGetOffset(pQueryInfo, 2) * totalNumOfRows + pField->bytes * i) = bytes; - pField = tscFieldInfoGetField(pCmd, 3); + pField = tscFieldInfoGetField(pQueryInfo, 3); if (i >= pMeta->numOfColumns && pMeta->numOfTags != 0) { - strncpy(pRes->data + tscFieldInfoGetOffset(pCmd, 3) * totalNumOfRows + pField->bytes * i, "tag", + strncpy(pRes->data + tscFieldInfoGetOffset(pQueryInfo, 3) * totalNumOfRows + pField->bytes * i, "tag", strlen("tag") + 1); } } - if (UTIL_METER_IS_METRIC(pMeterMetaInfo)) { + if (UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo)) { return 0; } @@ -167,27 +168,27 @@ static int32_t tscSetValueToResObj(SSqlObj *pSql, int32_t rowLen) { char *pTagValue = tsGetTagsValue(pMeta); for (int32_t i = numOfRows; i < totalNumOfRows; ++i) { // field name - TAOS_FIELD *pField = tscFieldInfoGetField(pCmd, 0); - strncpy(pRes->data + tscFieldInfoGetOffset(pCmd, 0) * totalNumOfRows + pField->bytes * i, pSchema[i].name, + TAOS_FIELD *pField = tscFieldInfoGetField(pQueryInfo, 0); + strncpy(pRes->data + tscFieldInfoGetOffset(pQueryInfo, 0) * totalNumOfRows + pField->bytes * i, pSchema[i].name, TSDB_COL_NAME_LEN); // type name - pField = tscFieldInfoGetField(pCmd, 1); + pField = tscFieldInfoGetField(pQueryInfo, 1); char *type = tDataTypeDesc[pSchema[i].type].aName; - strncpy(pRes->data + tscFieldInfoGetOffset(pCmd, 1) * totalNumOfRows + pField->bytes * i, type, pField->bytes); + strncpy(pRes->data + tscFieldInfoGetOffset(pQueryInfo, 1) * totalNumOfRows + pField->bytes * i, type, pField->bytes); // type length int32_t bytes = pSchema[i].bytes; - pField = tscFieldInfoGetField(pCmd, 2); + pField = tscFieldInfoGetField(pQueryInfo, 2); if (pSchema[i].type == TSDB_DATA_TYPE_NCHAR) { bytes = bytes / TSDB_NCHAR_SIZE; } - *(int32_t *)(pRes->data + tscFieldInfoGetOffset(pCmd, 2) * totalNumOfRows + pField->bytes * i) = bytes; + *(int32_t *)(pRes->data + tscFieldInfoGetOffset(pQueryInfo, 2) * totalNumOfRows + pField->bytes * i) = bytes; // tag value - pField = tscFieldInfoGetField(pCmd, 3); - char *target = pRes->data + tscFieldInfoGetOffset(pCmd, 3) * totalNumOfRows + pField->bytes * i; + pField = tscFieldInfoGetField(pQueryInfo, 3); + char *target = pRes->data + tscFieldInfoGetOffset(pQueryInfo, 3) * totalNumOfRows + pField->bytes * i; if (isNull(pTagValue, pSchema[i].type)) { sprintf(target, "%s", TSDB_DATA_NULL_STR); @@ -252,25 +253,28 @@ static int32_t tscBuildMeterSchemaResultFields(SSqlObj *pSql, int32_t numOfCols, SSqlCmd *pCmd = &pSql->cmd; pCmd->numOfCols = numOfCols; - pCmd->order.order = TSQL_SO_ASC; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + pQueryInfo->order.order = TSQL_SO_ASC; - tscFieldInfoSetValue(&pCmd->pQueryInfo->fieldsInfo, 0, TSDB_DATA_TYPE_BINARY, "Field", TSDB_COL_NAME_LEN); + tscFieldInfoSetValue(&pQueryInfo->fieldsInfo, 0, TSDB_DATA_TYPE_BINARY, "Field", TSDB_COL_NAME_LEN); rowLen += TSDB_COL_NAME_LEN; - tscFieldInfoSetValue(&pCmd->pQueryInfo->fieldsInfo, 1, TSDB_DATA_TYPE_BINARY, "Type", typeColLength); + tscFieldInfoSetValue(&pQueryInfo->fieldsInfo, 1, TSDB_DATA_TYPE_BINARY, "Type", typeColLength); rowLen += typeColLength; - tscFieldInfoSetValue(&pCmd->pQueryInfo->fieldsInfo, 2, TSDB_DATA_TYPE_INT, "Length", sizeof(int32_t)); + tscFieldInfoSetValue(&pQueryInfo->fieldsInfo, 2, TSDB_DATA_TYPE_INT, "Length", sizeof(int32_t)); rowLen += sizeof(int32_t); - tscFieldInfoSetValue(&pCmd->pQueryInfo->fieldsInfo, 3, TSDB_DATA_TYPE_BINARY, "Note", noteColLength); + tscFieldInfoSetValue(&pQueryInfo->fieldsInfo, 3, TSDB_DATA_TYPE_BINARY, "Note", noteColLength); rowLen += noteColLength; return rowLen; } static int32_t tscProcessDescribeTable(SSqlObj *pSql) { - assert(tscGetMeterMetaInfo(&pSql->cmd, 0)->pMeterMeta != NULL); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + + assert(tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0)->pMeterMeta != NULL); const int32_t NUM_OF_DESCRIBE_TABLE_COLUMNS = 4; const int32_t TYPE_COLUMN_LENGTH = 16; @@ -283,7 +287,7 @@ static int32_t tscProcessDescribeTable(SSqlObj *pSql) { int32_t rowLen = tscBuildMeterSchemaResultFields(pSql, NUM_OF_DESCRIBE_TABLE_COLUMNS, TYPE_COLUMN_LENGTH, note_field_length); - tscFieldInfoCalOffset(&pSql->cmd); + tscFieldInfoCalOffset(pQueryInfo); return tscSetValueToResObj(pSql, rowLen); } @@ -293,7 +297,9 @@ static int tscBuildMetricTagProjectionResult(SSqlObj *pSql) { // only need to reorganize the results in the column format SSqlCmd * pCmd = &pSql->cmd; SSqlRes * pRes = &pSql->res; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SQueryInfo *pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); SMetricMeta *pMetricMeta = pMeterMetaInfo->pMetricMeta; SSchema * pSchema = tsGetTagSchema(pMeterMetaInfo->pMeterMeta); @@ -310,7 +316,7 @@ static int tscBuildMetricTagProjectionResult(SSqlObj *pSql) { } int32_t totalNumOfResults = pMetricMeta->numOfMeters; - int32_t rowLen = tscGetResRowLength(pCmd); + int32_t rowLen = tscGetResRowLength(pQueryInfo); tscInitResObjForLocalQuery(pSql, totalNumOfResults, rowLen); @@ -320,17 +326,17 @@ static int tscBuildMetricTagProjectionResult(SSqlObj *pSql) { for (int32_t j = 0; j < pSidList->numOfSids; ++j) { SMeterSidExtInfo *pSidExt = tscGetMeterSidInfo(pSidList, j); - - for (int32_t k = 0; k < pCmd->pQueryInfo->fieldsInfo.numOfOutputCols; ++k) { - SColIndexEx *pColIndex = &tscSqlExprGet(pCmd, k)->colInfo; + + for (int32_t k = 0; k < pQueryInfo->fieldsInfo.numOfOutputCols; ++k) { + SColIndexEx *pColIndex = &tscSqlExprGet(pQueryInfo, k)->colInfo; int16_t offsetId = pColIndex->colIdx; assert((pColIndex->flag & TSDB_COL_TAG) != 0); char * val = pSidExt->tags + vOffset[offsetId]; - TAOS_FIELD *pField = tscFieldInfoGetField(pCmd, k); + TAOS_FIELD *pField = tscFieldInfoGetField(pQueryInfo, k); - memcpy(pRes->data + tscFieldInfoGetOffset(pCmd, k) * totalNumOfResults + pField->bytes * rowIdx, val, + memcpy(pRes->data + tscFieldInfoGetOffset(pQueryInfo, k) * totalNumOfResults + pField->bytes * rowIdx, val, (size_t)pField->bytes); } rowIdx++; @@ -344,21 +350,23 @@ static int tscBuildMetricTagSqlFunctionResult(SSqlObj *pSql) { SSqlCmd *pCmd = &pSql->cmd; SSqlRes *pRes = &pSql->res; - SMetricMeta *pMetricMeta = tscGetMeterMetaInfo(pCmd, 0)->pMetricMeta; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + + SMetricMeta *pMetricMeta = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0)->pMetricMeta; int32_t totalNumOfResults = 1; // count function only produce one result - int32_t rowLen = tscGetResRowLength(pCmd); + int32_t rowLen = tscGetResRowLength(pQueryInfo); tscInitResObjForLocalQuery(pSql, totalNumOfResults, rowLen); int32_t rowIdx = 0; for (int32_t i = 0; i < totalNumOfResults; ++i) { - for (int32_t k = 0; k < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++k) { - SSqlExpr *pExpr = tscSqlExprGet(pCmd, i); + for (int32_t k = 0; k < pQueryInfo->fieldsInfo.numOfOutputCols; ++k) { + SSqlExpr *pExpr = tscSqlExprGet(pQueryInfo, i); if (pExpr->colInfo.colIdx == -1 && pExpr->functionId == TSDB_FUNC_COUNT) { - TAOS_FIELD *pField = tscFieldInfoGetField(pCmd, k); + TAOS_FIELD *pField = tscFieldInfoGetField(pQueryInfo, k); - memcpy(pRes->data + tscFieldInfoGetOffset(pCmd, i) * totalNumOfResults + pField->bytes * rowIdx, + memcpy(pRes->data + tscFieldInfoGetOffset(pQueryInfo, i) * totalNumOfResults + pField->bytes * rowIdx, &pMetricMeta->numOfMeters, sizeof(pMetricMeta->numOfMeters)); } else { tscError("not support operations"); @@ -373,15 +381,17 @@ static int tscBuildMetricTagSqlFunctionResult(SSqlObj *pSql) { static int tscProcessQueryTags(SSqlObj *pSql) { SSqlCmd *pCmd = &pSql->cmd; - - SMeterMeta *pMeterMeta = tscGetMeterMetaInfo(pCmd, 0)->pMeterMeta; + + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + + SMeterMeta *pMeterMeta = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0)->pMeterMeta; if (pMeterMeta == NULL || pMeterMeta->numOfTags == 0 || pMeterMeta->numOfColumns == 0) { strcpy(pCmd->payload, "invalid table"); pSql->res.code = TSDB_CODE_INVALID_TABLE; return pSql->res.code; } - SSqlExpr *pExpr = tscSqlExprGet(pCmd, 0); + SSqlExpr *pExpr = tscSqlExprGet(pQueryInfo, 0); if (pExpr->functionId == TSDB_FUNC_COUNT) { return tscBuildMetricTagSqlFunctionResult(pSql); } else { @@ -390,7 +400,9 @@ static int tscProcessQueryTags(SSqlObj *pSql) { } static void tscProcessCurrentUser(SSqlObj *pSql) { - SSqlExpr* pExpr = tscSqlExprGet(&pSql->cmd, 0); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, 0); tscSetLocalQueryResult(pSql, pSql->pTscObj->user, pExpr->aliasName, TSDB_USER_LEN); } @@ -403,19 +415,24 @@ static void tscProcessCurrentDB(SSqlObj *pSql) { setNull(db, TSDB_DATA_TYPE_BINARY, TSDB_DB_NAME_LEN); } - SSqlExpr* pExpr = tscSqlExprGet(&pSql->cmd, 0); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, 0); tscSetLocalQueryResult(pSql, db, pExpr->aliasName, TSDB_DB_NAME_LEN); } static void tscProcessServerVer(SSqlObj *pSql) { const char* v = pSql->pTscObj->sversion; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); - SSqlExpr* pExpr = tscSqlExprGet(&pSql->cmd, 0); + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, 0); tscSetLocalQueryResult(pSql, v, pExpr->aliasName, tListLen(pSql->pTscObj->sversion)); } static void tscProcessClientVer(SSqlObj *pSql) { - SSqlExpr* pExpr = tscSqlExprGet(&pSql->cmd, 0); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, 0); tscSetLocalQueryResult(pSql, version, pExpr->aliasName, strlen(version)); } @@ -433,7 +450,9 @@ static void tscProcessServStatus(SSqlObj *pSql) { } } - SSqlExpr* pExpr = tscSqlExprGet(&pSql->cmd, 0); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, 0); tscSetLocalQueryResult(pSql, "1", pExpr->aliasName, 2); } @@ -442,12 +461,13 @@ void tscSetLocalQueryResult(SSqlObj *pSql, const char *val, const char *columnNa SSqlRes *pRes = &pSql->res; pCmd->numOfCols = 1; - pCmd->order.order = TSQL_SO_ASC; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + pQueryInfo->order.order = TSQL_SO_ASC; - tscFieldInfoSetValue(&pCmd->pQueryInfo[0].fieldsInfo, 0, TSDB_DATA_TYPE_BINARY, columnName, valueLength); + tscFieldInfoSetValue(&pQueryInfo->fieldsInfo, 0, TSDB_DATA_TYPE_BINARY, columnName, valueLength); tscInitResObjForLocalQuery(pSql, 1, valueLength); - TAOS_FIELD *pField = tscFieldInfoGetField(pCmd, 0); + TAOS_FIELD *pField = tscFieldInfoGetField(pQueryInfo, 0); strncpy(pRes->data, val, pField->bytes); } diff --git a/src/client/src/tscParseInsert.c b/src/client/src/tscParseInsert.c index 2f740faba35aef1a9ace02f4dd3e2e4cbdbf7e64..c67dc811e1aac294e55699ec4a42a79d1fa4dc4a 100644 --- a/src/client/src/tscParseInsert.c +++ b/src/client/src/tscParseInsert.c @@ -651,7 +651,7 @@ void sortRemoveDuplicates(STableDataBlocks *dataBuf) { static int32_t doParseInsertStatement(SSqlObj *pSql, void *pTableHashList, char **str, SParsedDataColInfo *spd, int32_t *totalNum) { SSqlCmd * pCmd = &pSql->cmd; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0); SMeterMeta * pMeterMeta = pMeterMetaInfo->pMeterMeta; STableDataBlocks *dataBuf = NULL; @@ -707,7 +707,7 @@ static int32_t tscParseSqlForCreateTableOnDemand(char **sqlstr, SSqlObj *pSql) { int32_t code = TSDB_CODE_SUCCESS; SSqlCmd * pCmd = &pSql->cmd; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0); char *sql = *sqlstr; // get the token of specified table @@ -754,7 +754,7 @@ static int32_t tscParseSqlForCreateTableOnDemand(char **sqlstr, SSqlObj *pSql) { STagData *pTag = (STagData *)pCmd->payload; memset(pTag, 0, sizeof(STagData)); - setMeterID(pSql, &sToken, 0); + setMeterID(pSql, 0, &sToken, 0); strncpy(pTag->name, pMeterMetaInfo->name, TSDB_METER_ID_LEN); code = tscGetMeterMeta(pSql, pTag->name, 0); @@ -762,7 +762,7 @@ static int32_t tscParseSqlForCreateTableOnDemand(char **sqlstr, SSqlObj *pSql) { return code; } - if (!UTIL_METER_IS_METRIC(pMeterMetaInfo)) { + if (!UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo)) { return tscInvalidSQLErrMsg(pCmd->payload, "create table only from super table is allowed", sToken.z); } @@ -894,7 +894,7 @@ static int32_t tscParseSqlForCreateTableOnDemand(char **sqlstr, SSqlObj *pSql) { return tscInvalidSQLErrMsg(pCmd->payload, "invalid table name", *sqlstr); } - int32_t ret = setMeterID(pSql, &tableToken, 0); + int32_t ret = setMeterID(pSql, 0, &tableToken, 0); if (ret != TSDB_CODE_SUCCESS) { return ret; } @@ -941,13 +941,13 @@ int validateTableName(char *tblName, int len) { * @param pSql * @return */ -int doParserInsertSql(SSqlObj *pSql, char *str) { +int doParseInsertSql(SSqlObj *pSql, char *str) { SSqlCmd *pCmd = &pSql->cmd; int32_t code = TSDB_CODE_INVALID_SQL; int32_t totalNum = 0; - SMeterMetaInfo *pMeterMetaInfo = tscAddEmptyMeterMetaInfo(pCmd); + SMeterMetaInfo *pMeterMetaInfo = tscAddEmptyMeterMetaInfo(pCmd, 0); if ((code = tscAllocPayload(pCmd, TSDB_PAYLOAD_SIZE)) != TSDB_CODE_SUCCESS) { return code; @@ -992,7 +992,7 @@ int doParserInsertSql(SSqlObj *pSql, char *str) { } //TODO refactor - if ((code = setMeterID(pSql, &sToken, 0)) != TSDB_CODE_SUCCESS) { + if ((code = setMeterID(pSql, 0, &sToken, 0)) != TSDB_CODE_SUCCESS) { goto _error_clean; } @@ -1011,7 +1011,7 @@ int doParserInsertSql(SSqlObj *pSql, char *str) { } } - if (UTIL_METER_IS_METRIC(pMeterMetaInfo)) { + if (UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo)) { code = tscInvalidSQLErrMsg(pCmd->payload, "insert data into super table is not supported", NULL); goto _error_clean; } @@ -1088,7 +1088,7 @@ int doParserInsertSql(SSqlObj *pSql, char *str) { strcpy(pDataBlock->filename, fname); } else if (sToken.type == TK_LP) { /* insert into tablename(col1, col2,..., coln) values(v1, v2,... vn); */ - SMeterMeta *pMeterMeta = tscGetMeterMetaInfo(pCmd, 0)->pMeterMeta; + SMeterMeta *pMeterMeta = tscGetMeterMetaInfo(pCmd, 0, 0)->pMeterMeta; SSchema * pSchema = tsGetSchema(pMeterMeta); if (pCmd->isInsertFromFile == -1) { @@ -1188,7 +1188,7 @@ int doParserInsertSql(SSqlObj *pSql, char *str) { goto _error_clean; } - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0, 0); // set the next sent data vnode index in data block arraylist pMeterMetaInfo->vnodeIndex = 1; @@ -1203,7 +1203,7 @@ _error_clean: pCmd->pDataBlocks = tscDestroyBlockArrayList(pCmd->pDataBlocks); _clean: - taosCleanUpIntHash(pSql->pTableHashList); + taosCleanUpHashTable(pSql->pTableHashList); pSql->pTableHashList = NULL; return code; } @@ -1231,14 +1231,14 @@ int tsParseInsertSql(SSqlObj *pSql, char *sql, char *acct, char *db) { pCmd->isInsertFromFile = -1; pSql->res.numOfRows = 0; - return doParserInsertSql(pSql, sql + index); + return doParseInsertSql(pSql, sql + index); } int tsParseSql(SSqlObj *pSql, char *acct, char *db, bool multiVnodeInsertion) { int32_t ret = TSDB_CODE_SUCCESS; // must before clean the sqlcmd object - tscRemoveAllMeterMetaInfo(&pSql->cmd, false); +// tscRemoveMeterMetaInfo(&pSql->cmd, false); if (NULL == pSql->asyncTblPos) { tscCleanSqlCmd(&pSql->cmd); @@ -1287,7 +1287,7 @@ static int doPackSendDataBlock(SSqlObj *pSql, int32_t numOfRows, STableDataBlock int32_t code = TSDB_CODE_SUCCESS; SSqlCmd *pCmd = &pSql->cmd; - SMeterMeta *pMeterMeta = tscGetMeterMetaInfo(pCmd, 0)->pMeterMeta; + SMeterMeta *pMeterMeta = tscGetMeterMetaInfo(pCmd, 0, 0)->pMeterMeta; SShellSubmitBlock *pBlocks = (SShellSubmitBlock *)(pTableDataBlocks->pData); tsSetBlockInfo(pBlocks, pMeterMeta, numOfRows); @@ -1319,7 +1319,7 @@ static int tscInsertDataFromFile(SSqlObj *pSql, FILE *fp, char *tmpTokenBuf) { int numOfRows = 0; int32_t code = 0; int nrows = 0; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0); SMeterMeta * pMeterMeta = pMeterMetaInfo->pMeterMeta; int32_t rowSize = pMeterMeta->rowSize; @@ -1415,7 +1415,7 @@ void tscProcessMultiVnodesInsert(SSqlObj *pSql) { } STableDataBlocks *pDataBlock = NULL; - SMeterMetaInfo * pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo * pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0); int32_t code = TSDB_CODE_SUCCESS; /* the first block has been sent to server in processSQL function */ @@ -1450,7 +1450,7 @@ void tscProcessMultiVnodesInsertForFile(SSqlObj *pSql) { return; } - SMeterMetaInfo * pInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo * pInfo = tscGetMeterMetaInfo(pCmd, 0, 0); STableDataBlocks *pDataBlock = NULL; int32_t affected_rows = 0; diff --git a/src/client/src/tscPrepare.c b/src/client/src/tscPrepare.c index ef8ddfd211acbcf76ae2a9471d6aca8cc4a40c97..8bc416b14020d0a3ed82d85d2018955731d0067d 100644 --- a/src/client/src/tscPrepare.c +++ b/src/client/src/tscPrepare.c @@ -409,7 +409,7 @@ static int insertStmtReset(STscStmt* pStmt) { } pCmd->batchSize = 0; - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0); pMeterMetaInfo->vnodeIndex = 0; return TSDB_CODE_SUCCESS; } @@ -423,7 +423,7 @@ static int insertStmtExecute(STscStmt* stmt) { ++pCmd->batchSize; } - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0); if (pCmd->pDataBlocks->nSize > 0) { // merge according to vgid diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index aa7e40a79bbb8d16e2e331a05257df5e179855bc..c6006e3e0cf3f03b26b531a47270f827af6dc644 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -48,7 +48,7 @@ typedef struct SColumnIdListRes { SColumnList list; } SColumnIdListRes; -static SSqlExpr* doAddProjectCol(SSqlCmd* pCmd, int32_t outputIndex, int32_t colIdx, int32_t tableIndex); +static SSqlExpr* doAddProjectCol(SQueryInfo* pQueryInfo, int32_t outputIndex, int32_t colIdx, int32_t tableIndex); static int32_t setShowInfo(SSqlObj* pSql, SSqlInfo* pInfo); static char* getAccountId(SSqlObj* pSql); @@ -59,38 +59,38 @@ static bool hasSpecifyDB(SSQLToken* pTableName); static bool validateTableColumnInfo(tFieldList* pFieldList, SSqlCmd* pCmd); static bool validateTagParams(tFieldList* pTagsList, tFieldList* pFieldList, SSqlCmd* pCmd); -static int32_t setObjFullName(char* fullName, char* account, SSQLToken* pDB, SSQLToken* tableName, int32_t* len); +static int32_t setObjFullName(char* fullName, const char* account, SSQLToken* pDB, SSQLToken* tableName, int32_t* len); static void getColumnName(tSQLExprItem* pItem, char* resultFieldName, int32_t nameLength); static void getRevisedName(char* resultFieldName, int32_t functionId, int32_t maxLen, char* columnName); -static int32_t addExprAndResultField(SSqlCmd* pCmd, int32_t colIdx, tSQLExprItem* pItem); -static int32_t insertResultField(SSqlCmd* pCmd, int32_t outputIndex, SColumnList* pIdList, int16_t bytes, int8_t type, +static int32_t addExprAndResultField(SQueryInfo* pQueryInfo, int32_t colIdx, tSQLExprItem* pItem); +static int32_t insertResultField(SQueryInfo* pQueryInfo, int32_t outputIndex, SColumnList* pIdList, int16_t bytes, int8_t type, char* fieldName); static int32_t changeFunctionID(int32_t optr, int16_t* functionId); static int32_t parseSelectClause(SSqlCmd* pCmd, tSQLExprList* pSelection, bool isMetric); static bool validateIpAddress(const char* ip, size_t size); -static bool hasUnsupportFunctionsForMetricQuery(SSqlCmd* pCmd); -static bool functionCompatibleCheck(SSqlCmd* pCmd); -static void setColumnOffsetValueInResultset(SSqlCmd* pCmd); +static bool hasUnsupportFunctionsForMetricQuery(SQueryInfo* pQueryInfo); +static bool functionCompatibleCheck(SQueryInfo* pQueryInfo); +static void setColumnOffsetValueInResultset(SQueryInfo* pQueryInfo); -static int32_t parseGroupbyClause(SSqlCmd* pCmd, tVariantList* pList); +static int32_t parseGroupbyClause(SQueryInfo* pQueryInfo, tVariantList* pList, SSqlCmd* pCmd); -static int32_t parseIntervalClause(SSqlCmd* pCmd, SQuerySQL* pQuerySql); -static int32_t setSlidingClause(SSqlCmd* pCmd, SQuerySQL* pQuerySql); +static int32_t parseIntervalClause(SQueryInfo* pQueryInfo, SQuerySQL* pQuerySql); +static int32_t setSlidingClause(SQueryInfo* pQueryInfo, SQuerySQL* pQuerySql); -static int32_t addProjectionExprAndResultField(SSqlCmd* pCmd, tSQLExprItem* pItem); +static int32_t addProjectionExprAndResultField(SQueryInfo* pQueryInfo, tSQLExprItem* pItem); static int32_t parseWhereClause(SSqlObj* pSql, tSQLExpr** pExpr); -static int32_t parseFillClause(SSqlCmd* pCmd, SQuerySQL* pQuerySQL); -static int32_t parseOrderbyClause(SSqlCmd* pCmd, SQuerySQL* pQuerySql, SSchema* pSchema, int32_t numOfCols); +static int32_t parseFillClause(SQueryInfo* pQueryInfo, SQuerySQL* pQuerySQL); +static int32_t parseOrderbyClause(SQueryInfo* pQueryInfo, SQuerySQL* pQuerySql, SSchema* pSchema, int32_t numOfCols); -static int32_t tsRewriteFieldNameIfNecessary(SSqlCmd* pCmd); +static int32_t tsRewriteFieldNameIfNecessary(SQueryInfo* pQueryInfo); static int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo); -static int32_t validateSqlFunctionInStreamSql(SSqlCmd* pCmd); +static int32_t validateSqlFunctionInStreamSql(SQueryInfo* pQueryInfo); static int32_t buildArithmeticExprString(tSQLExpr* pExpr, char** exprString); -static int32_t validateFunctionsInIntervalOrGroupbyQuery(SSqlCmd* pCmd); +static int32_t validateFunctionsInIntervalOrGroupbyQuery(SQueryInfo* pQueryInfo); static int32_t validateArithmeticSQLExpr(tSQLExpr* pExpr, SSchema* pSchema, int32_t numOfCols, SColumnIdListRes* pList); static int32_t validateDNodeConfig(tDCLSQL* pOptions); static int32_t validateLocalConfig(tDCLSQL* pOptions); @@ -98,23 +98,23 @@ static int32_t validateColumnName(char* name); static int32_t setKillInfo(SSqlObj* pSql, struct SSqlInfo* pInfo); static bool validateOneTags(SSqlCmd* pCmd, TAOS_FIELD* pTagField); -static bool hasTimestampForPointInterpQuery(SSqlCmd* pCmd); -static void updateTagColumnIndex(SSqlCmd* pCmd, int32_t tableIndex); +static bool hasTimestampForPointInterpQuery(SQueryInfo* pQueryInfo); +static void updateTagColumnIndex(SQueryInfo* pQueryInfo, int32_t tableIndex); -static int32_t parseLimitClause(SSqlObj* pSql, SQuerySQL* pQuerySql); +static int32_t parseLimitClause(SSqlObj* pSql, int32_t subClauseIndex, SQuerySQL* pQuerySql); static int32_t parseCreateDBOptions(SSqlCmd* pCmd, SCreateDBInfo* pCreateDbSql); -static int32_t getColumnIndexByNameEx(SSQLToken* pToken, SSqlCmd* pCmd, SColumnIndex* pIndex); -static int32_t getTableIndexByName(SSQLToken* pToken, SSqlCmd* pCmd, SColumnIndex* pIndex); +static int32_t getColumnIndexByNameEx(SSQLToken* pToken, SQueryInfo* pQueryInfo, SColumnIndex* pIndex); +static int32_t getTableIndexByName(SSQLToken* pToken, SQueryInfo *pQueryInfo, SColumnIndex* pIndex); static int32_t optrToString(tSQLExpr* pExpr, char** exprString); -static int32_t getMeterIndex(SSQLToken* pTableToken, SSqlCmd* pCmd, SColumnIndex* pIndex); -static int32_t doFunctionsCompatibleCheck(SSqlObj* pSql); -static int32_t doLocalQueryProcess(SQuerySQL* pQuerySql, SSqlCmd* pCmd); +static int32_t getMeterIndex(SSQLToken* pTableToken, SQueryInfo* pQueryInfo, SColumnIndex* pIndex); +static int32_t doFunctionsCompatibleCheck(SSqlCmd* pCmd, SQueryInfo* pQueryInfo); +static int32_t doLocalQueryProcess(SQuerySQL* pQuerySql, SSqlCmd* pCmd, int32_t subClauseIndex); static int32_t tscCheckCreateDbParams(SSqlCmd* pCmd, SCreateDbMsg* pCreate); static SColumnList getColumnList(int32_t num, int16_t tableIndex, int32_t columnIndex); -static int32_t doCheckForCreateTable(SSqlObj* pSql, SSqlInfo* pInfo); +static int32_t doCheckForCreateTable(SSqlObj* pSql, int32_t subClauseIndex, SSqlInfo* pInfo); static int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo); static int32_t doCheckForStream(SSqlObj* pSql, SSqlInfo* pInfo); static int32_t doCheckForQuery(SSqlObj* pSql, SQuerySQL* pQuerySql, int32_t index); @@ -123,16 +123,16 @@ static int32_t doCheckForQuery(SSqlObj* pSql, SQuerySQL* pQuerySql, int32_t inde * Used during parsing query sql. Since the query sql usually small in length, error position * is not needed in the final error message. */ -static int32_t invalidSqlErrMsg(SSqlCmd* pCmd, const char* errMsg) { - return tscInvalidSQLErrMsg(pCmd->payload, errMsg, NULL); +static int32_t invalidSqlErrMsg(char* dstBuffer, const char* errMsg) { + return tscInvalidSQLErrMsg(dstBuffer, errMsg, NULL); } -static int32_t tscQueryOnlyMetricTags(SSqlCmd* pCmd, bool* queryOnMetricTags) { - assert(QUERY_IS_STABLE_QUERY(pCmd->type)); +static int32_t tscQueryOnlyMetricTags(SQueryInfo *pQueryInfo, bool* queryOnMetricTags) { + assert(QUERY_IS_STABLE_QUERY(pQueryInfo->type)); *queryOnMetricTags = true; - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - SSqlExpr* pExpr = tscSqlExprGet(pCmd, i); + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i); if (pExpr->functionId != TSDB_FUNC_TAGPRJ && !(pExpr->functionId == TSDB_FUNC_COUNT && pExpr->colInfo.colIdx == TSDB_TBNAME_COLUMN_INDEX)) { @@ -144,21 +144,21 @@ static int32_t tscQueryOnlyMetricTags(SSqlCmd* pCmd, bool* queryOnMetricTags) { return TSDB_CODE_SUCCESS; } -static int setColumnFilterInfoForTimestamp(SSqlCmd* pCmd, tVariant* pVar) { +static int setColumnFilterInfoForTimestamp(SQueryInfo* pQueryInfo, tVariant* pVar) { int64_t time = 0; const char* msg = "invalid timestamp"; strdequote(pVar->pz); char* seg = strnchr(pVar->pz, '-', pVar->nLen, false); - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); if (seg != NULL) { if (taosParseTime(pVar->pz, &time, pVar->nLen, pMeterMetaInfo->pMeterMeta->precision) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg); + return invalidSqlErrMsg(pQueryInfo->msg, msg); } } else { if (tVariantDump(pVar, (char*)&time, TSDB_DATA_TYPE_BIGINT)) { - return invalidSqlErrMsg(pCmd, msg); + return invalidSqlErrMsg(pQueryInfo->msg, msg); } } @@ -174,7 +174,7 @@ static int32_t handlePassword(SSqlCmd* pCmd, SSQLToken* pPwd) { const char* msg3 = "password needs single quote marks enclosed"; if (pPwd->type != TK_STRING) { - return invalidSqlErrMsg(pCmd, msg3); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3); } strdequote(pPwd->z); @@ -182,11 +182,11 @@ static int32_t handlePassword(SSqlCmd* pCmd, SSQLToken* pPwd) { pPwd->n = strlen(pPwd->z); if (pPwd->n <= 0) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); } if (pPwd->n > TSDB_PASSWORD_LEN) { - return invalidSqlErrMsg(pCmd, msg2); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } return TSDB_CODE_SUCCESS; @@ -201,10 +201,10 @@ int32_t tscToSQLCmd(SSqlObj* pSql, struct SSqlInfo* pInfo) { SSqlCmd* pCmd = &(pSql->cmd); if (!pInfo->valid) { - return invalidSqlErrMsg(pCmd, pInfo->pzErrMsg); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), pInfo->pzErrMsg); } - SMeterMetaInfo* pMeterMetaInfo = tscAddEmptyMeterMetaInfo(pCmd); + SMeterMetaInfo* pMeterMetaInfo = tscAddEmptyMeterMetaInfo(pCmd, 0); pCmd->command = pInfo->type; int32_t code = 0; @@ -220,7 +220,7 @@ int32_t tscToSQLCmd(SSqlObj* pSql, struct SSqlInfo* pInfo) { SSQLToken* pzName = &pInfo->pDCLInfo->a[0]; if ((pInfo->type != TSDB_SQL_DROP_DNODE) && (tscValidateName(pzName) != TSDB_CODE_SUCCESS)) { - return invalidSqlErrMsg(pCmd, msg2); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } if (pInfo->type == TSDB_SQL_DROP_DB) { @@ -228,24 +228,24 @@ int32_t tscToSQLCmd(SSqlObj* pSql, struct SSqlInfo* pInfo) { int32_t code = setObjFullName(pMeterMetaInfo->name, getAccountId(pSql), pzName, NULL, NULL); if (code != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg2); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } } else if (pInfo->type == TSDB_SQL_DROP_TABLE) { assert(pInfo->pDCLInfo->nTokens == 1); - if (setMeterID(pSql, pzName, 0) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg3); + if (setMeterID(pSql, 0, pzName, 0) != TSDB_CODE_SUCCESS) { + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3); } } else if (pInfo->type == TSDB_SQL_DROP_DNODE) { if (!validateIpAddress(pzName->z, pzName->n)) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); } strncpy(pMeterMetaInfo->name, pzName->z, pzName->n); } else { // drop user if (pzName->n > TSDB_USER_LEN) { - return invalidSqlErrMsg(pCmd, msg3); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3); } strncpy(pMeterMetaInfo->name, pzName->z, pzName->n); @@ -259,12 +259,12 @@ int32_t tscToSQLCmd(SSqlObj* pSql, struct SSqlInfo* pInfo) { SSQLToken* pToken = &pInfo->pDCLInfo->a[0]; if (tscValidateName(pToken) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg); } int32_t ret = setObjFullName(pMeterMetaInfo->name, getAccountId(pSql), pToken, NULL, NULL); if (ret != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg); } break; @@ -289,12 +289,12 @@ int32_t tscToSQLCmd(SSqlObj* pSql, struct SSqlInfo* pInfo) { SCreateDBInfo* pCreateDB = &(pInfo->pDCLInfo->dbOpt); if (tscValidateName(&pCreateDB->dbname) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); } int32_t ret = setObjFullName(pMeterMetaInfo->name, getAccountId(pSql), &(pCreateDB->dbname), NULL, NULL); if (ret != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg2); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } if (parseCreateDBOptions(pCmd, pCreateDB) != TSDB_CODE_SUCCESS) { @@ -308,12 +308,12 @@ int32_t tscToSQLCmd(SSqlObj* pSql, struct SSqlInfo* pInfo) { const char* msg = "invalid ip address"; if (pInfo->pDCLInfo->nTokens > 1) { - return invalidSqlErrMsg(pCmd, msg); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg); } SSQLToken* pIpAddr = &pInfo->pDCLInfo->a[0]; if (!validateIpAddress(pIpAddr->z, pIpAddr->n)) { - return invalidSqlErrMsg(pCmd, msg); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg); } break; @@ -333,11 +333,11 @@ int32_t tscToSQLCmd(SSqlObj* pSql, struct SSqlInfo* pInfo) { } if (pName->n > TSDB_USER_LEN) { - return invalidSqlErrMsg(pCmd, msg3); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3); } if (tscValidateName(pName) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg2); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } SCreateAcctSQL* pAcctOpt = &pInfo->pDCLInfo->acctOpt; @@ -347,7 +347,7 @@ int32_t tscToSQLCmd(SSqlObj* pSql, struct SSqlInfo* pInfo) { } else if (strncmp(pAcctOpt->stat.z, "all", 3) == 0 && pAcctOpt->stat.n == 3) { } else if (strncmp(pAcctOpt->stat.z, "no", 2) == 0 && pAcctOpt->stat.n == 2) { } else { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); } } @@ -360,15 +360,15 @@ int32_t tscToSQLCmd(SSqlObj* pSql, struct SSqlInfo* pInfo) { const char* msg1 = "invalid table name"; if (tscValidateName(pToken) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); } if (pToken->n > TSDB_METER_NAME_LEN) { - return invalidSqlErrMsg(pCmd, msg2); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } - if (setMeterID(pSql, pToken, 0) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg2); + if (setMeterID(pSql, 0, pToken, 0) != TSDB_CODE_SUCCESS) { + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } return tscGetMeterMeta(pSql, pMeterMetaInfo->name, 0); @@ -381,12 +381,12 @@ int32_t tscToSQLCmd(SSqlObj* pSql, struct SSqlInfo* pInfo) { /* validate the ip address */ tDCLSQL* pDCL = pInfo->pDCLInfo; if (!validateIpAddress(pDCL->a[0].z, pDCL->a[0].n)) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); } /* validate the parameter names and options */ if (validateDNodeConfig(pDCL) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg2); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } char* pMsg = pCmd->payload + tsRpcHeadSize; @@ -420,11 +420,11 @@ int32_t tscToSQLCmd(SSqlObj* pSql, struct SSqlInfo* pInfo) { SSQLToken* pPwd = &pUser->passwd; if (pName->n > TSDB_USER_LEN) { - return invalidSqlErrMsg(pCmd, msg3); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3); } if (tscValidateName(pName) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg2); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } if (pCmd->command == TSDB_SQL_CREATE_USER) { @@ -446,10 +446,10 @@ int32_t tscToSQLCmd(SSqlObj* pSql, struct SSqlInfo* pInfo) { } else if (strncasecmp(pDCL->a[1].z, "write", 5) == 0 && pDCL->a[1].n == 5) { pCmd->count = 3; } else { - return invalidSqlErrMsg(pCmd, msg5); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg5); } } else { - return invalidSqlErrMsg(pCmd, msg7); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg7); } } @@ -462,7 +462,7 @@ int32_t tscToSQLCmd(SSqlObj* pSql, struct SSqlInfo* pInfo) { // validate the parameter names and options if (validateLocalConfig(pDCL) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg); } strncpy(pCmd->payload, pDCL->a[0].z, pDCL->a[0].n); @@ -478,7 +478,7 @@ int32_t tscToSQLCmd(SSqlObj* pSql, struct SSqlInfo* pInfo) { SCreateTableSQL* pCreateTable = pInfo->pCreateTableInfo; if (pCreateTable->type == TSQL_CREATE_TABLE || pCreateTable->type == TSQL_CREATE_STABLE) { - if ((code = doCheckForCreateTable(pSql, pInfo)) != TSDB_CODE_SUCCESS) { + if ((code = doCheckForCreateTable(pSql, 0, pInfo)) != TSDB_CODE_SUCCESS) { return code; } @@ -497,15 +497,22 @@ int32_t tscToSQLCmd(SSqlObj* pSql, struct SSqlInfo* pInfo) { } case TSDB_SQL_SELECT: { - for(int32_t i = 0; i < pInfo->subclauseInfo.numOfClause; ++i) { + assert(pCmd->numOfClause == 1); + + for (int32_t i = pCmd->numOfClause; i < pInfo->subclauseInfo.numOfClause; ++i) { + tscAddEmptyMeterMetaInfo(pCmd, i); + } + + assert(pCmd->numOfClause == pInfo->subclauseInfo.numOfClause); + for (int32_t i = 0; i < pInfo->subclauseInfo.numOfClause; ++i) { SQuerySQL* pQuerySql = pInfo->subclauseInfo.pClause[i]; - + if ((code = doCheckForQuery(pSql, pQuerySql, i)) != TSDB_CODE_SUCCESS) { return code; } } - - return TSDB_CODE_SUCCESS;// do not build query message here + + return TSDB_CODE_SUCCESS; // do not build query message here } case TSDB_SQL_ALTER_TABLE: { @@ -527,7 +534,7 @@ int32_t tscToSQLCmd(SSqlObj* pSql, struct SSqlInfo* pInfo) { } default: - return invalidSqlErrMsg(pCmd, "not support sql expression"); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), "not support sql expression"); } return tscBuildMsg[pCmd->command](pSql, pInfo); @@ -537,9 +544,9 @@ int32_t tscToSQLCmd(SSqlObj* pSql, struct SSqlInfo* pInfo) { * if the top/bottom exists, only tags columns, tbname column, and primary timestamp column * are available. */ -static bool isTopBottomQuery(SSqlCmd* pCmd) { - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - int32_t functionId = tscSqlExprGet(pCmd, i)->functionId; +static bool isTopBottomQuery(SQueryInfo* pQueryInfo) { + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + int32_t functionId = tscSqlExprGet(pQueryInfo, i)->functionId; if (functionId == TSDB_FUNC_TOP || functionId == TSDB_FUNC_BOTTOM) { return true; @@ -549,11 +556,11 @@ static bool isTopBottomQuery(SSqlCmd* pCmd) { return false; } -int32_t parseIntervalClause(SSqlCmd* pCmd, SQuerySQL* pQuerySql) { +int32_t parseIntervalClause(SQueryInfo* pQueryInfo, SQuerySQL* pQuerySql) { const char* msg1 = "invalid query expression"; const char* msg2 = "interval cannot be less than 10 ms"; - - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); if (pQuerySql->interval.type == 0 || pQuerySql->interval.n == 0) { return TSDB_CODE_SUCCESS; @@ -561,42 +568,42 @@ int32_t parseIntervalClause(SSqlCmd* pCmd, SQuerySQL* pQuerySql) { // interval is not null SSQLToken* t = &pQuerySql->interval; - if (getTimestampInUsFromStr(t->z, t->n, &pCmd->pQueryInfo[0].nAggTimeInterval) != TSDB_CODE_SUCCESS) { + if (getTimestampInUsFromStr(t->z, t->n, &pQueryInfo->nAggTimeInterval) != TSDB_CODE_SUCCESS) { return TSDB_CODE_INVALID_SQL; } /* revised the time precision according to the flag */ if (pMeterMetaInfo->pMeterMeta->precision == TSDB_TIME_PRECISION_MILLI) { - pCmd->pQueryInfo[0].nAggTimeInterval = pCmd->pQueryInfo[0].nAggTimeInterval / 1000; + pQueryInfo->nAggTimeInterval = pQueryInfo->nAggTimeInterval / 1000; } /* parser has filter the illegal type, no need to check here */ - pCmd->pQueryInfo[0].intervalTimeUnit = pQuerySql->interval.z[pQuerySql->interval.n - 1]; + pQueryInfo->intervalTimeUnit = pQuerySql->interval.z[pQuerySql->interval.n - 1]; // interval cannot be less than 10 milliseconds - if (pCmd->pQueryInfo[0].nAggTimeInterval < tsMinIntervalTime) { - return invalidSqlErrMsg(pCmd, msg2); + if (pQueryInfo->nAggTimeInterval < tsMinIntervalTime) { + return invalidSqlErrMsg(pQueryInfo->msg, msg2); } // for top/bottom + interval query, we do not add additional timestamp column in the front - if (isTopBottomQuery(pCmd)) { + if (isTopBottomQuery(pQueryInfo)) { return TSDB_CODE_SUCCESS; } // check the invalid sql expresssion: select count(tbname)/count(tag1)/count(tag2) from super_table interval(1d); - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - SSqlExpr* pExpr = tscSqlExprGet(pCmd, i); + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i); if (pExpr->functionId == TSDB_FUNC_COUNT && TSDB_COL_IS_TAG(pExpr->colInfo.flag)) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(pQueryInfo->msg, msg1); } } // need to add timestamp column in result set, if interval is existed - uint64_t uid = tscSqlExprGet(pCmd, 0)->uid; + uint64_t uid = tscSqlExprGet(pQueryInfo, 0)->uid; int32_t tableIndex = COLUMN_INDEX_INITIAL_VAL; - for (int32_t i = 0; i < pCmd->pQueryInfo->numOfTables; ++i) { - pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, i); + for (int32_t i = 0; i < pQueryInfo->numOfTables; ++i) { + pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, i); if (pMeterMetaInfo->pMeterMeta->uid == uid) { tableIndex = i; break; @@ -608,51 +615,47 @@ int32_t parseIntervalClause(SSqlCmd* pCmd, SQuerySQL* pQuerySql) { } SColumnIndex index = {tableIndex, PRIMARYKEY_TIMESTAMP_COL_INDEX}; - tscSqlExprInsert(pCmd, 0, TSDB_FUNC_TS, &index, TSDB_DATA_TYPE_TIMESTAMP, TSDB_KEYSIZE, TSDB_KEYSIZE); + tscSqlExprInsert(pQueryInfo, 0, TSDB_FUNC_TS, &index, TSDB_DATA_TYPE_TIMESTAMP, TSDB_KEYSIZE, TSDB_KEYSIZE); SColumnList ids = getColumnList(1, 0, PRIMARYKEY_TIMESTAMP_COL_INDEX); - int32_t ret = insertResultField(pCmd, 0, &ids, TSDB_KEYSIZE, TSDB_DATA_TYPE_TIMESTAMP, aAggs[TSDB_FUNC_TS].aName); - - return ret; + + return insertResultField(pQueryInfo, 0, &ids, TSDB_KEYSIZE, TSDB_DATA_TYPE_TIMESTAMP, aAggs[TSDB_FUNC_TS].aName); } -int32_t setSlidingClause(SSqlCmd* pCmd, SQuerySQL* pQuerySql) { +int32_t setSlidingClause(SQueryInfo *pQueryInfo, SQuerySQL* pQuerySql) { const char* msg0 = "sliding value too small"; const char* msg1 = "sliding value no larger than the interval value"; - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); SSQLToken* pSliding = &pQuerySql->sliding; if (pSliding->n != 0) { - getTimestampInUsFromStr(pSliding->z, pSliding->n, &pCmd->pQueryInfo[0].nSlidingTime); + getTimestampInUsFromStr(pSliding->z, pSliding->n, &pQueryInfo->nSlidingTime); if (pMeterMetaInfo->pMeterMeta->precision == TSDB_TIME_PRECISION_MILLI) { - pCmd->pQueryInfo[0].nSlidingTime /= 1000; + pQueryInfo->nSlidingTime /= 1000; } - if (pCmd->pQueryInfo[0].nSlidingTime < tsMinSlidingTime) { - return invalidSqlErrMsg(pCmd, msg0); + if (pQueryInfo->nSlidingTime < tsMinSlidingTime) { + return invalidSqlErrMsg(pQueryInfo->msg, msg0); } - if (pCmd->pQueryInfo[0].nSlidingTime > pCmd->pQueryInfo[0].nAggTimeInterval) { - return invalidSqlErrMsg(pCmd, msg1); + if (pQueryInfo->nSlidingTime > pQueryInfo->nAggTimeInterval) { + return invalidSqlErrMsg(pQueryInfo->msg, msg1); } } return TSDB_CODE_SUCCESS; } -int32_t setMeterID(SSqlObj* pSql, SSQLToken* pzTableName, int32_t tableIndex) { +int32_t setMeterID(SSqlObj* pSql, int32_t subClauseIndex, SSQLToken* pzTableName, int32_t tableIndex) { const char* msg = "name too long"; SSqlCmd* pCmd = &pSql->cmd; - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, tableIndex); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, subClauseIndex, tableIndex); int32_t code = TSDB_CODE_SUCCESS; if (hasSpecifyDB(pzTableName)) { - /* - * db has been specified in sql string - * so we ignore current db path - */ + // db has been specified in sql string so we ignore current db path code = setObjFullName(pMeterMetaInfo->name, getAccountId(pSql), NULL, pzTableName, NULL); } else { // get current DB name first, then set it into path SSQLToken t = {0}; @@ -662,7 +665,7 @@ int32_t setMeterID(SSqlObj* pSql, SSQLToken* pzTableName, int32_t tableIndex) { } if (code != TSDB_CODE_SUCCESS) { - invalidSqlErrMsg(pCmd, msg); + invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg); } return code; @@ -681,13 +684,13 @@ static bool validateTableColumnInfo(tFieldList* pFieldList, SSqlCmd* pCmd) { // number of fields no less than 2 if (pFieldList->nField <= 1 || pFieldList->nField > TSDB_MAX_COLUMNS) { - invalidSqlErrMsg(pCmd, msg); + invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg); return false; } // first column must be timestamp if (pFieldList->p[0].type != TSDB_DATA_TYPE_TIMESTAMP) { - invalidSqlErrMsg(pCmd, msg1); + invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); return false; } @@ -698,7 +701,7 @@ static bool validateTableColumnInfo(tFieldList* pFieldList, SSqlCmd* pCmd) { // max row length must be less than TSDB_MAX_BYTES_PER_ROW if (nLen > TSDB_MAX_BYTES_PER_ROW) { - invalidSqlErrMsg(pCmd, msg2); + invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); return false; } @@ -706,23 +709,23 @@ static bool validateTableColumnInfo(tFieldList* pFieldList, SSqlCmd* pCmd) { for (int32_t i = 0; i < pFieldList->nField; ++i) { TAOS_FIELD* pField = &pFieldList->p[i]; if (pField->type < TSDB_DATA_TYPE_BOOL || pField->type > TSDB_DATA_TYPE_NCHAR) { - invalidSqlErrMsg(pCmd, msg4); + invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg4); return false; } if ((pField->type == TSDB_DATA_TYPE_BINARY && (pField->bytes <= 0 || pField->bytes > TSDB_MAX_BINARY_LEN)) || (pField->type == TSDB_DATA_TYPE_NCHAR && (pField->bytes <= 0 || pField->bytes > TSDB_MAX_NCHAR_LEN))) { - invalidSqlErrMsg(pCmd, msg5); + invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg5); return false; } if (validateColumnName(pField->name) != TSDB_CODE_SUCCESS) { - invalidSqlErrMsg(pCmd, msg6); + invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg6); return false; } if (has(pFieldList, i + 1, pFieldList->p[i].name) == true) { - invalidSqlErrMsg(pCmd, msg3); + invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3); return false; } } @@ -743,7 +746,7 @@ static bool validateTagParams(tFieldList* pTagsList, tFieldList* pFieldList, SSq // number of fields at least 1 if (pTagsList->nField < 1 || pTagsList->nField > TSDB_MAX_TAGS) { - invalidSqlErrMsg(pCmd, msg1); + invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); return false; } @@ -754,14 +757,14 @@ static bool validateTagParams(tFieldList* pTagsList, tFieldList* pFieldList, SSq // max tag row length must be less than TSDB_MAX_TAGS_LEN if (nLen > TSDB_MAX_TAGS_LEN) { - invalidSqlErrMsg(pCmd, msg2); + invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); return false; } // field name must be unique for (int32_t i = 0; i < pTagsList->nField; ++i) { if (has(pFieldList, 0, pTagsList->p[i].name) == true) { - invalidSqlErrMsg(pCmd, msg3); + invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3); return false; } } @@ -769,28 +772,28 @@ static bool validateTagParams(tFieldList* pTagsList, tFieldList* pFieldList, SSq /* timestamp in tag is not allowed */ for (int32_t i = 0; i < pTagsList->nField; ++i) { if (pTagsList->p[i].type == TSDB_DATA_TYPE_TIMESTAMP) { - invalidSqlErrMsg(pCmd, msg4); + invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg4); return false; } if (pTagsList->p[i].type < TSDB_DATA_TYPE_BOOL || pTagsList->p[i].type > TSDB_DATA_TYPE_NCHAR) { - invalidSqlErrMsg(pCmd, msg5); + invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg5); return false; } if ((pTagsList->p[i].type == TSDB_DATA_TYPE_BINARY && pTagsList->p[i].bytes <= 0) || (pTagsList->p[i].type == TSDB_DATA_TYPE_NCHAR && pTagsList->p[i].bytes <= 0)) { - invalidSqlErrMsg(pCmd, msg7); + invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg7); return false; } if (validateColumnName(pTagsList->p[i].name) != TSDB_CODE_SUCCESS) { - invalidSqlErrMsg(pCmd, msg6); + invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg6); return false; } if (has(pTagsList, i + 1, pTagsList->p[i].name) == true) { - invalidSqlErrMsg(pCmd, msg3); + invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3); return false; } } @@ -809,7 +812,7 @@ bool validateOneTags(SSqlCmd* pCmd, TAOS_FIELD* pTagField) { const char* msg5 = "invalid binary/nchar tag length"; const char* msg6 = "invalid data type in tags"; - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0); SMeterMeta* pMeterMeta = pMeterMetaInfo->pMeterMeta; // no more than 6 tags @@ -817,18 +820,18 @@ bool validateOneTags(SSqlCmd* pCmd, TAOS_FIELD* pTagField) { char msg[128] = {0}; sprintf(msg, "tags no more than %d", TSDB_MAX_TAGS); - invalidSqlErrMsg(pCmd, msg); + invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg); return false; } // no timestamp allowable if (pTagField->type == TSDB_DATA_TYPE_TIMESTAMP) { - invalidSqlErrMsg(pCmd, msg1); + invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); return false; } if (pTagField->type < TSDB_DATA_TYPE_BOOL && pTagField->type > TSDB_DATA_TYPE_NCHAR) { - invalidSqlErrMsg(pCmd, msg6); + invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg6); return false; } @@ -841,19 +844,19 @@ bool validateOneTags(SSqlCmd* pCmd, TAOS_FIELD* pTagField) { // length less than TSDB_MAX_TASG_LEN if (nLen + pTagField->bytes > TSDB_MAX_TAGS_LEN) { - invalidSqlErrMsg(pCmd, msg3); + invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3); return false; } // tags name can not be a keyword if (validateColumnName(pTagField->name) != TSDB_CODE_SUCCESS) { - invalidSqlErrMsg(pCmd, msg4); + invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg4); return false; } // binary(val), val can not be equalled to or less than 0 if ((pTagField->type == TSDB_DATA_TYPE_BINARY || pTagField->type == TSDB_DATA_TYPE_NCHAR) && pTagField->bytes <= 0) { - invalidSqlErrMsg(pCmd, msg5); + invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg5); return false; } @@ -862,7 +865,7 @@ bool validateOneTags(SSqlCmd* pCmd, TAOS_FIELD* pTagField) { for (int32_t i = 0; i < pMeterMeta->numOfTags + pMeterMeta->numOfColumns; ++i) { if (strncasecmp(pTagField->name, pSchema[i].name, TSDB_COL_NAME_LEN) == 0) { - invalidSqlErrMsg(pCmd, msg2); + invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); return false; } } @@ -878,23 +881,23 @@ bool validateOneColumn(SSqlCmd* pCmd, TAOS_FIELD* pColField) { const char* msg5 = "invalid column name"; const char* msg6 = "invalid column length"; - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0); SMeterMeta* pMeterMeta = pMeterMetaInfo->pMeterMeta; // no more max columns if (pMeterMeta->numOfColumns >= TSDB_MAX_COLUMNS || pMeterMeta->numOfTags + pMeterMeta->numOfColumns >= TSDB_MAX_COLUMNS) { - invalidSqlErrMsg(pCmd, msg1); + invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); return false; } if (pColField->type < TSDB_DATA_TYPE_BOOL || pColField->type > TSDB_DATA_TYPE_NCHAR) { - invalidSqlErrMsg(pCmd, msg4); + invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg4); return false; } if (validateColumnName(pColField->name) != TSDB_CODE_SUCCESS) { - invalidSqlErrMsg(pCmd, msg5); + invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg5); return false; } @@ -906,20 +909,20 @@ bool validateOneColumn(SSqlCmd* pCmd, TAOS_FIELD* pColField) { } if (pColField->bytes <= 0) { - invalidSqlErrMsg(pCmd, msg6); + invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg6); return false; } // length less than TSDB_MAX_BYTES_PER_ROW if (nLen + pColField->bytes > TSDB_MAX_BYTES_PER_ROW) { - invalidSqlErrMsg(pCmd, msg3); + invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3); return false; } // field name must be unique for (int32_t i = 0; i < pMeterMeta->numOfTags + pMeterMeta->numOfColumns; ++i) { if (strncasecmp(pColField->name, pSchema[i].name, TSDB_COL_NAME_LEN) == 0) { - invalidSqlErrMsg(pCmd, msg2); + invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); return false; } } @@ -954,7 +957,7 @@ static bool hasSpecifyDB(SSQLToken* pTableName) { return false; } -static int32_t setObjFullName(char* fullName, char* account, SSQLToken* pDB, SSQLToken* tableName, int32_t* xlen) { +int32_t setObjFullName(char* fullName, const char* account, SSQLToken* pDB, SSQLToken* tableName, int32_t* xlen) { int32_t totalLen = 0; if (account != NULL) { @@ -1017,15 +1020,16 @@ static void extractColumnNameFromString(tSQLExprItem* pItem) { } } -int32_t parseSelectClause(SSqlCmd* pCmd, tSQLExprList* pSelection, bool isMetric) { +int32_t parseSelectClause(SSqlCmd* pCmd, tSQLExprList* pSelection, bool isSTable) { assert(pSelection != NULL && pCmd != NULL); const char* msg1 = "invalid column name/illegal column type in arithmetic expression"; const char* msg2 = "functions can not be mixed up"; const char* msg3 = "not support query expression"; - + SQueryInfo *pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + for (int32_t i = 0; i < pSelection->nExpr; ++i) { - int32_t outputIndex = pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; + int32_t outputIndex = pQueryInfo->fieldsInfo.numOfOutputCols; tSQLExprItem* pItem = &pSelection->a[i]; // project on all fields @@ -1037,22 +1041,22 @@ int32_t parseSelectClause(SSqlCmd* pCmd, tSQLExprList* pSelection, bool isMetric // if the name of column is quoted, remove it and set the right information for later process extractColumnNameFromString(pItem); - - pCmd->type |= TSDB_QUERY_TYPE_PROJECTION_QUERY; + + pQueryInfo->type |= TSDB_QUERY_TYPE_PROJECTION_QUERY; // select table_name1.field_name1, table_name2.field_name2 from table_name1, table_name2 - if (addProjectionExprAndResultField(pCmd, pItem) != TSDB_CODE_SUCCESS) { + if (addProjectionExprAndResultField(pQueryInfo, pItem) != TSDB_CODE_SUCCESS) { return TSDB_CODE_INVALID_SQL; } } else if (pItem->pNode->nSQLOptr >= TK_COUNT && pItem->pNode->nSQLOptr <= TK_LAST_ROW) { // sql function in selection clause, append sql function info in pSqlCmd structure sequentially - if (addExprAndResultField(pCmd, outputIndex, pItem) != TSDB_CODE_SUCCESS) { + if (addExprAndResultField(pQueryInfo, outputIndex, pItem) != TSDB_CODE_SUCCESS) { return TSDB_CODE_INVALID_SQL; } } else if (pItem->pNode->nSQLOptr >= TK_PLUS && pItem->pNode->nSQLOptr <= TK_REM) { // arithmetic function in select - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); SSchema* pSchema = tsGetSchema(pMeterMetaInfo->pMeterMeta); SColumnIdListRes columnList = {.pSchema = pSchema, .numOfCols = pMeterMetaInfo->pMeterMeta->numOfColumns}; @@ -1060,7 +1064,7 @@ int32_t parseSelectClause(SSqlCmd* pCmd, tSQLExprList* pSelection, bool isMetric int32_t ret = validateArithmeticSQLExpr(pItem->pNode, pSchema, pMeterMetaInfo->pMeterMeta->numOfColumns, &columnList); if (ret != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(pQueryInfo->msg, msg1); } char arithmeticExprStr[1024] = {0}; @@ -1072,7 +1076,7 @@ int32_t parseSelectClause(SSqlCmd* pCmd, tSQLExprList* pSelection, bool isMetric // expr string is set as the parameter of function SColumnIndex index = {0}; - SSqlExpr* pExpr = tscSqlExprInsert(pCmd, outputIndex, TSDB_FUNC_ARITHM, &index, TSDB_DATA_TYPE_DOUBLE, + SSqlExpr* pExpr = tscSqlExprInsert(pQueryInfo, outputIndex, TSDB_FUNC_ARITHM, &index, TSDB_DATA_TYPE_DOUBLE, sizeof(double), sizeof(double)); addExprParams(pExpr, arithmeticExprStr, TSDB_DATA_TYPE_BINARY, strlen(arithmeticExprStr), 0); @@ -1083,29 +1087,29 @@ int32_t parseSelectClause(SSqlCmd* pCmd, tSQLExprList* pSelection, bool isMetric strncpy(pExpr->aliasName, arithmeticExprStr, TSDB_COL_NAME_LEN); } - insertResultField(pCmd, i, &columnList.list, sizeof(double), TSDB_DATA_TYPE_DOUBLE, pExpr->aliasName); + insertResultField(pQueryInfo, i, &columnList.list, sizeof(double), TSDB_DATA_TYPE_DOUBLE, pExpr->aliasName); } else { /* * not support such expression * e.g., select 12+5 from table_name */ - return invalidSqlErrMsg(pCmd, msg3); + return invalidSqlErrMsg(pQueryInfo->msg, msg3); } - if (pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols > TSDB_MAX_COLUMNS) { + if (pQueryInfo->fieldsInfo.numOfOutputCols > TSDB_MAX_COLUMNS) { return TSDB_CODE_INVALID_SQL; } } - if (!functionCompatibleCheck(pCmd)) { - return invalidSqlErrMsg(pCmd, msg2); + if (!functionCompatibleCheck(pQueryInfo)) { + return invalidSqlErrMsg(pQueryInfo->msg, msg2); } - if (isMetric) { - pCmd->type |= TSDB_QUERY_TYPE_STABLE_QUERY; - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + if (isSTable) { + pQueryInfo->type |= TSDB_QUERY_TYPE_STABLE_QUERY; + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); - if (tscQueryMetricTags(pCmd)) { // local handle the metric tag query + if (tscQueryMetricTags(pQueryInfo)) { // local handle the metric tag query pCmd->command = TSDB_SQL_RETRIEVE_TAGS; pCmd->count = pMeterMetaInfo->pMeterMeta->numOfColumns; // the number of meter schema, tricky. } @@ -1114,9 +1118,9 @@ int32_t parseSelectClause(SSqlCmd* pCmd, tSQLExprList* pSelection, bool isMetric * transfer sql functions that need secondary merge into another format * in dealing with metric queries such as: count/first/last */ - tscTansformSQLFunctionForMetricQuery(pCmd); + tscTansformSQLFunctionForMetricQuery(pQueryInfo); - if (hasUnsupportFunctionsForMetricQuery(pCmd)) { + if (hasUnsupportFunctionsForMetricQuery(pQueryInfo)) { return TSDB_CODE_INVALID_SQL; } } @@ -1124,18 +1128,19 @@ int32_t parseSelectClause(SSqlCmd* pCmd, tSQLExprList* pSelection, bool isMetric return TSDB_CODE_SUCCESS; } -int32_t insertResultField(SSqlCmd* pCmd, int32_t outputIndex, SColumnList* pIdList, int16_t bytes, int8_t type, +int32_t insertResultField(SQueryInfo* pQueryInfo, int32_t outputIndex, SColumnList* pIdList, int16_t bytes, int8_t type, char* fieldName) { for (int32_t i = 0; i < pIdList->num; ++i) { - tscColumnBaseInfoInsert(pCmd, &(pIdList->ids[i])); + tscColumnBaseInfoInsert(pQueryInfo, &(pIdList->ids[i])); } - tscFieldInfoSetValue(&pCmd->pQueryInfo[0].fieldsInfo, outputIndex, type, fieldName, bytes); + tscFieldInfoSetValue(&pQueryInfo->fieldsInfo, outputIndex, type, fieldName, bytes); return TSDB_CODE_SUCCESS; } -SSqlExpr* doAddProjectCol(SSqlCmd* pCmd, int32_t outputIndex, int32_t colIdx, int32_t tableIndex) { - SMeterMeta* pMeterMeta = tscGetMeterMetaInfo(pCmd, tableIndex)->pMeterMeta; +SSqlExpr* doAddProjectCol(SQueryInfo* pQueryInfo, int32_t outputIndex, int32_t colIdx, int32_t tableIndex) { + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, tableIndex); + SMeterMeta* pMeterMeta = pMeterMetaInfo->pMeterMeta; SSchema* pSchema = tsGetColumnSchema(pMeterMeta, colIdx); int32_t numOfCols = pMeterMeta->numOfColumns; @@ -1143,21 +1148,21 @@ SSqlExpr* doAddProjectCol(SSqlCmd* pCmd, int32_t outputIndex, int32_t colIdx, in int16_t functionId = (int16_t)((colIdx >= numOfCols) ? TSDB_FUNC_TAGPRJ : TSDB_FUNC_PRJ); if (functionId == TSDB_FUNC_TAGPRJ) { - addRequiredTagColumn(pCmd, colIdx - numOfCols, tableIndex); - pCmd->type = TSDB_QUERY_TYPE_STABLE_QUERY; + addRequiredTagColumn(pQueryInfo, colIdx - numOfCols, tableIndex); + pQueryInfo->type = TSDB_QUERY_TYPE_STABLE_QUERY; } else { - pCmd->type = TSDB_QUERY_TYPE_PROJECTION_QUERY; + pQueryInfo->type = TSDB_QUERY_TYPE_PROJECTION_QUERY; } SColumnIndex index = {tableIndex, colIdx}; SSqlExpr* pExpr = - tscSqlExprInsert(pCmd, outputIndex, functionId, &index, pSchema->type, pSchema->bytes, pSchema->bytes); + tscSqlExprInsert(pQueryInfo, outputIndex, functionId, &index, pSchema->type, pSchema->bytes, pSchema->bytes); return pExpr; } -void addRequiredTagColumn(SSqlCmd* pCmd, int32_t tagColIndex, int32_t tableIndex) { - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, tableIndex); +void addRequiredTagColumn(SQueryInfo* pQueryInfo, int32_t tagColIndex, int32_t tableIndex) { + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, tableIndex); if (pMeterMetaInfo->numOfTags == 0 || pMeterMetaInfo->tagColumnIndex[pMeterMetaInfo->numOfTags - 1] < tagColIndex) { pMeterMetaInfo->tagColumnIndex[pMeterMetaInfo->numOfTags++] = tagColIndex; @@ -1183,10 +1188,11 @@ void addRequiredTagColumn(SSqlCmd* pCmd, int32_t tagColIndex, int32_t tableIndex assert(tagColIndex >= -1 && tagColIndex < TSDB_MAX_TAGS && pMeterMetaInfo->numOfTags <= TSDB_MAX_TAGS + 1); } -static void addProjectQueryCol(SSqlCmd* pCmd, int32_t startPos, SColumnIndex* pIndex, tSQLExprItem* pItem) { - SSqlExpr* pExpr = doAddProjectCol(pCmd, startPos, pIndex->columnIndex, pIndex->tableIndex); +static void addProjectQueryCol(SQueryInfo* pQueryInfo, int32_t startPos, SColumnIndex* pIndex, tSQLExprItem* pItem) { + SSqlExpr* pExpr = doAddProjectCol(pQueryInfo, startPos, pIndex->columnIndex, pIndex->tableIndex); - SMeterMeta* pMeterMeta = tscGetMeterMetaInfo(pCmd, pIndex->tableIndex)->pMeterMeta; + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, pIndex->tableIndex); + SMeterMeta* pMeterMeta = pMeterMetaInfo->pMeterMeta; SSchema* pSchema = tsGetColumnSchema(pMeterMeta, pIndex->columnIndex); @@ -1200,12 +1206,12 @@ static void addProjectQueryCol(SSqlCmd* pCmd, int32_t startPos, SColumnIndex* pI ids.num = 0; } - insertResultField(pCmd, startPos, &ids, pExpr->resBytes, pExpr->resType, colName); + insertResultField(pQueryInfo, startPos, &ids, pExpr->resBytes, pExpr->resType, colName); } -void tscAddSpecialColumnForSelect(SSqlCmd* pCmd, int32_t outputColIndex, int16_t functionId, SColumnIndex* pIndex, +void tscAddSpecialColumnForSelect(SQueryInfo* pQueryInfo, int32_t outputColIndex, int16_t functionId, SColumnIndex* pIndex, SSchema* pColSchema, int16_t flag) { - SSqlExpr* pExpr = tscSqlExprInsert(pCmd, outputColIndex, functionId, pIndex, pColSchema->type, pColSchema->bytes, + SSqlExpr* pExpr = tscSqlExprInsert(pQueryInfo, outputColIndex, functionId, pIndex, pColSchema->type, pColSchema->bytes, pColSchema->bytes); SColumnList ids = getColumnList(1, pIndex->tableIndex, pIndex->columnIndex); @@ -1213,29 +1219,29 @@ void tscAddSpecialColumnForSelect(SSqlCmd* pCmd, int32_t outputColIndex, int16_t ids.num = 0; } - insertResultField(pCmd, outputColIndex, &ids, pColSchema->bytes, pColSchema->type, pColSchema->name); + insertResultField(pQueryInfo, outputColIndex, &ids, pColSchema->bytes, pColSchema->type, pColSchema->name); pExpr->colInfo.flag = flag; if (TSDB_COL_IS_TAG(flag)) { - addRequiredTagColumn(pCmd, pIndex->columnIndex, pIndex->tableIndex); + addRequiredTagColumn(pQueryInfo, pIndex->columnIndex, pIndex->tableIndex); } } -static int32_t doAddProjectionExprAndResultFields(SSqlCmd* pCmd, SColumnIndex* pIndex, int32_t startPos) { - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, pIndex->tableIndex); +static int32_t doAddProjectionExprAndResultFields(SQueryInfo* pQueryInfo, SColumnIndex* pIndex, int32_t startPos) { + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, pIndex->tableIndex); int32_t numOfTotalColumns = 0; SMeterMeta* pMeterMeta = pMeterMetaInfo->pMeterMeta; SSchema* pSchema = tsGetSchema(pMeterMeta); - if (UTIL_METER_IS_METRIC(pMeterMetaInfo)) { + if (UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo)) { numOfTotalColumns = pMeterMeta->numOfColumns + pMeterMeta->numOfTags; } else { numOfTotalColumns = pMeterMeta->numOfColumns; } for (int32_t j = 0; j < numOfTotalColumns; ++j) { - doAddProjectCol(pCmd, startPos + j, j, pIndex->tableIndex); + doAddProjectCol(pQueryInfo, startPos + j, j, pIndex->tableIndex); pIndex->columnIndex = j; SColumnList ids = {0}; @@ -1244,56 +1250,56 @@ static int32_t doAddProjectionExprAndResultFields(SSqlCmd* pCmd, SColumnIndex* p // tag columns do not add to source list ids.num = (j >= pMeterMeta->numOfColumns) ? 0 : 1; - insertResultField(pCmd, startPos + j, &ids, pSchema[j].bytes, pSchema[j].type, pSchema[j].name); + insertResultField(pQueryInfo, startPos + j, &ids, pSchema[j].bytes, pSchema[j].type, pSchema[j].name); } return numOfTotalColumns; } -int32_t addProjectionExprAndResultField(SSqlCmd* pCmd, tSQLExprItem* pItem) { +int32_t addProjectionExprAndResultField(SQueryInfo* pQueryInfo, tSQLExprItem* pItem) { const char* msg0 = "invalid column name"; const char* msg1 = "tag for table query is not allowed"; - int32_t startPos = pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; + int32_t startPos = pQueryInfo->fieldsInfo.numOfOutputCols; if (pItem->pNode->nSQLOptr == TK_ALL) { // project on all fields SColumnIndex index = COLUMN_INDEX_INITIALIZER; - if (getTableIndexByName(&pItem->pNode->colInfo, pCmd, &index) != TSDB_CODE_SUCCESS) { + if (getTableIndexByName(&pItem->pNode->colInfo, pQueryInfo, &index) != TSDB_CODE_SUCCESS) { return TSDB_CODE_INVALID_SQL; } // all meters columns are required if (index.tableIndex == COLUMN_INDEX_INITIAL_VAL) { // all table columns are required. - for (int32_t i = 0; i < pCmd->pQueryInfo->numOfTables; ++i) { + for (int32_t i = 0; i < pQueryInfo->numOfTables; ++i) { index.tableIndex = i; - int32_t inc = doAddProjectionExprAndResultFields(pCmd, &index, startPos); + int32_t inc = doAddProjectionExprAndResultFields(pQueryInfo, &index, startPos); startPos += inc; } } else { - doAddProjectionExprAndResultFields(pCmd, &index, startPos); + doAddProjectionExprAndResultFields(pQueryInfo, &index, startPos); } } else if (pItem->pNode->nSQLOptr == TK_ID) { // simple column projection query SColumnIndex index = COLUMN_INDEX_INITIALIZER; - if (getColumnIndexByNameEx(&pItem->pNode->colInfo, pCmd, &index) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg0); + if (getColumnIndexByNameEx(&pItem->pNode->colInfo, pQueryInfo, &index) != TSDB_CODE_SUCCESS) { + return invalidSqlErrMsg(pQueryInfo->msg, msg0); } if (index.columnIndex == TSDB_TBNAME_COLUMN_INDEX) { SSchema colSchema = {.type = TSDB_DATA_TYPE_BINARY, .bytes = TSDB_METER_NAME_LEN}; strcpy(colSchema.name, TSQL_TBNAME_L); - - pCmd->type = TSDB_QUERY_TYPE_STABLE_QUERY; - tscAddSpecialColumnForSelect(pCmd, startPos, TSDB_FUNC_TAGPRJ, &index, &colSchema, true); + + pQueryInfo->type = TSDB_QUERY_TYPE_STABLE_QUERY; + tscAddSpecialColumnForSelect(pQueryInfo, startPos, TSDB_FUNC_TAGPRJ, &index, &colSchema, true); } else { - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, index.tableIndex); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, index.tableIndex); SMeterMeta* pMeterMeta = pMeterMetaInfo->pMeterMeta; if (index.columnIndex >= pMeterMeta->numOfColumns && UTIL_METER_IS_NOMRAL_METER(pMeterMetaInfo)) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(pQueryInfo->msg, msg1); } - addProjectQueryCol(pCmd, startPos, &index, pItem); + addProjectQueryCol(pQueryInfo, startPos, &index, pItem); } } else { return TSDB_CODE_INVALID_SQL; @@ -1302,7 +1308,7 @@ int32_t addProjectionExprAndResultField(SSqlCmd* pCmd, tSQLExprItem* pItem) { return TSDB_CODE_SUCCESS; } -static int32_t setExprInfoForFunctions(SSqlCmd* pCmd, SSchema* pSchema, int32_t functionID, char* aliasName, +static int32_t setExprInfoForFunctions(SQueryInfo* pQueryInfo, SSchema* pSchema, int32_t functionID, char* aliasName, int32_t resColIdx, SColumnIndex* pColIndex) { int16_t type = 0; int16_t bytes = 0; @@ -1314,7 +1320,7 @@ static int32_t setExprInfoForFunctions(SSqlCmd* pCmd, SSchema* pSchema, int32_t if (pSchema[pColIndex->columnIndex].type == TSDB_DATA_TYPE_BINARY || pSchema[pColIndex->columnIndex].type == TSDB_DATA_TYPE_NCHAR || pSchema[pColIndex->columnIndex].type == TSDB_DATA_TYPE_BOOL) { - invalidSqlErrMsg(pCmd, msg1); + invalidSqlErrMsg(pQueryInfo->msg, msg1); return -1; } else { type = TSDB_DATA_TYPE_DOUBLE; @@ -1331,21 +1337,21 @@ static int32_t setExprInfoForFunctions(SSqlCmd* pCmd, SSchema* pSchema, int32_t getRevisedName(columnName, functionID, TSDB_COL_NAME_LEN, pSchema[pColIndex->columnIndex].name); } - tscSqlExprInsert(pCmd, resColIdx, functionID, pColIndex, type, bytes, bytes); + tscSqlExprInsert(pQueryInfo, resColIdx, functionID, pColIndex, type, bytes, bytes); // for point interpolation/last_row query, we need the timestamp column to be loaded SColumnIndex index = {.tableIndex = pColIndex->tableIndex, .columnIndex = PRIMARYKEY_TIMESTAMP_COL_INDEX}; if (functionID == TSDB_FUNC_INTERP || functionID == TSDB_FUNC_LAST_ROW) { - tscColumnBaseInfoInsert(pCmd, &index); + tscColumnBaseInfoInsert(pQueryInfo, &index); } SColumnList ids = getColumnList(1, pColIndex->tableIndex, pColIndex->columnIndex); - insertResultField(pCmd, resColIdx, &ids, bytes, type, columnName); + insertResultField(pQueryInfo, resColIdx, &ids, bytes, type, columnName); return TSDB_CODE_SUCCESS; } -int32_t addExprAndResultField(SSqlCmd* pCmd, int32_t colIdx, tSQLExprItem* pItem) { +int32_t addExprAndResultField(SQueryInfo* pQueryInfo, int32_t colIdx, tSQLExprItem* pItem) { SMeterMetaInfo* pMeterMetaInfo = NULL; int32_t optr = pItem->pNode->nSQLOptr; @@ -1360,7 +1366,7 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, int32_t colIdx, tSQLExprItem* pItem case TK_COUNT: { if (pItem->pNode->pParam != NULL && pItem->pNode->pParam->nExpr != 1) { /* more than one parameter for count() function */ - return invalidSqlErrMsg(pCmd, msg2); + return invalidSqlErrMsg(pQueryInfo->msg, msg2); } int16_t functionID = 0; @@ -1373,7 +1379,7 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, int32_t colIdx, tSQLExprItem* pItem if (pItem->pNode->pParam != NULL) { SSQLToken* pToken = &pItem->pNode->pParam->a[0].pNode->colInfo; if (pToken->z == NULL || pToken->n == 0) { - return invalidSqlErrMsg(pCmd, msg3); + return invalidSqlErrMsg(pQueryInfo->msg, msg3); } tSQLExprItem* pParamElem = &pItem->pNode->pParam->a[0]; @@ -1382,20 +1388,20 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, int32_t colIdx, tSQLExprItem* pItem // check if the table name is valid or not SSQLToken tmpToken = pParamElem->pNode->colInfo; - if (getTableIndexByName(&tmpToken, pCmd, &index) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg4); + if (getTableIndexByName(&tmpToken, pQueryInfo, &index) != TSDB_CODE_SUCCESS) { + return invalidSqlErrMsg(pQueryInfo->msg, msg4); } index = (SColumnIndex){0, PRIMARYKEY_TIMESTAMP_COL_INDEX}; int32_t size = tDataTypeDesc[TSDB_DATA_TYPE_BIGINT].nSize; - tscSqlExprInsert(pCmd, colIdx, functionID, &index, TSDB_DATA_TYPE_BIGINT, size, size); + tscSqlExprInsert(pQueryInfo, colIdx, functionID, &index, TSDB_DATA_TYPE_BIGINT, size, size); } else { // count the number of meters created according to the metric - if (getColumnIndexByNameEx(pToken, pCmd, &index) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg3); + if (getColumnIndexByNameEx(pToken, pQueryInfo, &index) != TSDB_CODE_SUCCESS) { + return invalidSqlErrMsg(pQueryInfo->msg, msg3); } - pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, index.tableIndex); + pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, index.tableIndex); // count tag is equalled to count(tbname) if (index.columnIndex >= pMeterMetaInfo->pMeterMeta->numOfColumns) { @@ -1403,13 +1409,13 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, int32_t colIdx, tSQLExprItem* pItem } int32_t size = tDataTypeDesc[TSDB_DATA_TYPE_BIGINT].nSize; - tscSqlExprInsert(pCmd, colIdx, functionID, &index, TSDB_DATA_TYPE_BIGINT, size, size); + tscSqlExprInsert(pQueryInfo, colIdx, functionID, &index, TSDB_DATA_TYPE_BIGINT, size, size); } } else { // count(*) is equalled to count(primary_timestamp_key) index = (SColumnIndex){0, PRIMARYKEY_TIMESTAMP_COL_INDEX}; int32_t size = tDataTypeDesc[TSDB_DATA_TYPE_BIGINT].nSize; - tscSqlExprInsert(pCmd, colIdx, functionID, &index, TSDB_DATA_TYPE_BIGINT, size, size); + tscSqlExprInsert(pQueryInfo, colIdx, functionID, &index, TSDB_DATA_TYPE_BIGINT, size, size); } char columnName[TSDB_COL_NAME_LEN] = {0}; @@ -1418,7 +1424,7 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, int32_t colIdx, tSQLExprItem* pItem // count always use the primary timestamp key column, which is 0. SColumnList ids = getColumnList(1, index.tableIndex, index.columnIndex); - insertResultField(pCmd, colIdx, &ids, sizeof(int64_t), TSDB_DATA_TYPE_BIGINT, columnName); + insertResultField(pQueryInfo, colIdx, &ids, sizeof(int64_t), TSDB_DATA_TYPE_BIGINT, columnName); return TSDB_CODE_SUCCESS; } case TK_SUM: @@ -1433,27 +1439,27 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, int32_t colIdx, tSQLExprItem* pItem if (pItem->pNode->pParam == NULL || (optr != TK_LEASTSQUARES && pItem->pNode->pParam->nExpr != 1) || (optr == TK_LEASTSQUARES && pItem->pNode->pParam->nExpr != 3)) { /* no parameters or more than one parameter for function */ - return invalidSqlErrMsg(pCmd, msg2); + return invalidSqlErrMsg(pQueryInfo->msg, msg2); } tSQLExprItem* pParamElem = &(pItem->pNode->pParam->a[0]); if (pParamElem->pNode->nSQLOptr != TK_ALL && pParamElem->pNode->nSQLOptr != TK_ID) { - return invalidSqlErrMsg(pCmd, msg2); + return invalidSqlErrMsg(pQueryInfo->msg, msg2); } SColumnIndex index = COLUMN_INDEX_INITIALIZER; - if ((getColumnIndexByNameEx(&pParamElem->pNode->colInfo, pCmd, &index) != TSDB_CODE_SUCCESS) || + if ((getColumnIndexByNameEx(&pParamElem->pNode->colInfo, pQueryInfo, &index) != TSDB_CODE_SUCCESS) || index.columnIndex == TSDB_TBNAME_COLUMN_INDEX) { - return invalidSqlErrMsg(pCmd, msg3); + return invalidSqlErrMsg(pQueryInfo->msg, msg3); } // 2. check if sql function can be applied on this column data type - pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, index.tableIndex); + pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, index.tableIndex); SSchema* pSchema = tsGetColumnSchema(pMeterMetaInfo->pMeterMeta, index.columnIndex); int16_t colType = pSchema->type; if (colType <= TSDB_DATA_TYPE_BOOL || colType >= TSDB_DATA_TYPE_BINARY) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(pQueryInfo->msg, msg1); } char columnName[TSDB_COL_NAME_LEN] = {0}; @@ -1477,18 +1483,18 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, int32_t colIdx, tSQLExprItem* pItem if (optr == TK_DIFF) { colIdx += 1; SColumnIndex indexTS = {.tableIndex = index.tableIndex, .columnIndex = 0}; - tscSqlExprInsert(pCmd, 0, TSDB_FUNC_TS_DUMMY, &indexTS, TSDB_DATA_TYPE_TIMESTAMP, TSDB_KEYSIZE, TSDB_KEYSIZE); + tscSqlExprInsert(pQueryInfo, 0, TSDB_FUNC_TS_DUMMY, &indexTS, TSDB_DATA_TYPE_TIMESTAMP, TSDB_KEYSIZE, TSDB_KEYSIZE); SColumnList ids = getColumnList(1, 0, 0); - insertResultField(pCmd, 0, &ids, TSDB_KEYSIZE, TSDB_DATA_TYPE_TIMESTAMP, aAggs[TSDB_FUNC_TS_DUMMY].aName); + insertResultField(pQueryInfo, 0, &ids, TSDB_KEYSIZE, TSDB_DATA_TYPE_TIMESTAMP, aAggs[TSDB_FUNC_TS_DUMMY].aName); } // functions can not be applied to tags if (index.columnIndex >= pMeterMetaInfo->pMeterMeta->numOfColumns) { - return invalidSqlErrMsg(pCmd, msg6); + return invalidSqlErrMsg(pQueryInfo->msg, msg6); } - SSqlExpr* pExpr = tscSqlExprInsert(pCmd, colIdx, functionID, &index, resultType, resultSize, resultSize); + SSqlExpr* pExpr = tscSqlExprInsert(pQueryInfo, colIdx, functionID, &index, resultType, resultSize, resultSize); if (optr == TK_LEASTSQUARES) { /* set the leastsquares parameters */ @@ -1511,7 +1517,7 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, int32_t colIdx, tSQLExprItem* pItem ids.num = 1; ids.ids[0] = index; - insertResultField(pCmd, colIdx, &ids, pExpr->resBytes, pExpr->resType, columnName); + insertResultField(pQueryInfo, colIdx, &ids, pExpr->resBytes, pExpr->resType, columnName); return TSDB_CODE_SUCCESS; } @@ -1527,7 +1533,7 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, int32_t colIdx, tSQLExprItem* pItem if (!requireAllFields) { if (pItem->pNode->pParam->nExpr < 1) { - return invalidSqlErrMsg(pCmd, msg3); + return invalidSqlErrMsg(pQueryInfo->msg, msg3); } /* in first/last function, multiple columns can be add to resultset */ @@ -1535,7 +1541,7 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, int32_t colIdx, tSQLExprItem* pItem for (int32_t i = 0; i < pItem->pNode->pParam->nExpr; ++i) { tSQLExprItem* pParamElem = &(pItem->pNode->pParam->a[i]); if (pParamElem->pNode->nSQLOptr != TK_ALL && pParamElem->pNode->nSQLOptr != TK_ID) { - return invalidSqlErrMsg(pCmd, msg3); + return invalidSqlErrMsg(pQueryInfo->msg, msg3); } SColumnIndex index = COLUMN_INDEX_INITIALIZER; @@ -1544,34 +1550,34 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, int32_t colIdx, tSQLExprItem* pItem // select table.* SSQLToken tmpToken = pParamElem->pNode->colInfo; - if (getTableIndexByName(&tmpToken, pCmd, &index) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg4); + if (getTableIndexByName(&tmpToken, pQueryInfo, &index) != TSDB_CODE_SUCCESS) { + return invalidSqlErrMsg(pQueryInfo->msg, msg4); } - pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, index.tableIndex); + pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, index.tableIndex); SSchema* pSchema = tsGetSchema(pMeterMetaInfo->pMeterMeta); for (int32_t j = 0; j < pMeterMetaInfo->pMeterMeta->numOfColumns; ++j) { index.columnIndex = j; - if (setExprInfoForFunctions(pCmd, pSchema, functionID, pItem->aliasName, colIdx++, &index) != 0) { + if (setExprInfoForFunctions(pQueryInfo, pSchema, functionID, pItem->aliasName, colIdx++, &index) != 0) { return TSDB_CODE_INVALID_SQL; } } } else { - if (getColumnIndexByNameEx(&pParamElem->pNode->colInfo, pCmd, &index) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg3); + if (getColumnIndexByNameEx(&pParamElem->pNode->colInfo, pQueryInfo, &index) != TSDB_CODE_SUCCESS) { + return invalidSqlErrMsg(pQueryInfo->msg, msg3); } - pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, index.tableIndex); + pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, index.tableIndex); SSchema* pSchema = tsGetSchema(pMeterMetaInfo->pMeterMeta); // functions can not be applied to tags if (index.columnIndex >= pMeterMetaInfo->pMeterMeta->numOfColumns) { - return invalidSqlErrMsg(pCmd, msg6); + return invalidSqlErrMsg(pQueryInfo->msg, msg6); } - if (setExprInfoForFunctions(pCmd, pSchema, functionID, pItem->aliasName, colIdx + i, &index) != 0) { + if (setExprInfoForFunctions(pQueryInfo, pSchema, functionID, pItem->aliasName, colIdx + i, &index) != 0) { return TSDB_CODE_INVALID_SQL; } } @@ -1581,13 +1587,13 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, int32_t colIdx, tSQLExprItem* pItem } else { // select * from xxx int32_t numOfFields = 0; - for (int32_t j = 0; j < pCmd->pQueryInfo->numOfTables; ++j) { - pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, j); + for (int32_t j = 0; j < pQueryInfo->numOfTables; ++j) { + pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, j); SSchema* pSchema = tsGetSchema(pMeterMetaInfo->pMeterMeta); for (int32_t i = 0; i < pMeterMetaInfo->pMeterMeta->numOfColumns; ++i) { SColumnIndex index = {.tableIndex = j, .columnIndex = i}; - if (setExprInfoForFunctions(pCmd, pSchema, functionID, pItem->aliasName, colIdx + i + j, &index) != 0) { + if (setExprInfoForFunctions(pQueryInfo, pSchema, functionID, pItem->aliasName, colIdx + i + j, &index) != 0) { return TSDB_CODE_INVALID_SQL; } } @@ -1605,39 +1611,39 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, int32_t colIdx, tSQLExprItem* pItem // 1. valid the number of parameters if (pItem->pNode->pParam == NULL || pItem->pNode->pParam->nExpr != 2) { /* no parameters or more than one parameter for function */ - return invalidSqlErrMsg(pCmd, msg2); + return invalidSqlErrMsg(pQueryInfo->msg, msg2); } tSQLExprItem* pParamElem = &(pItem->pNode->pParam->a[0]); if (pParamElem->pNode->nSQLOptr != TK_ID) { - return invalidSqlErrMsg(pCmd, msg2); + return invalidSqlErrMsg(pQueryInfo->msg, msg2); } char columnName[TSDB_COL_NAME_LEN] = {0}; getColumnName(pItem, columnName, TSDB_COL_NAME_LEN); SColumnIndex index = COLUMN_INDEX_INITIALIZER; - if (getColumnIndexByNameEx(&pParamElem->pNode->colInfo, pCmd, &index) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg3); + if (getColumnIndexByNameEx(&pParamElem->pNode->colInfo, pQueryInfo, &index) != TSDB_CODE_SUCCESS) { + return invalidSqlErrMsg(pQueryInfo->msg, msg3); } - pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, index.tableIndex); + pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, index.tableIndex); SSchema* pSchema = tsGetSchema(pMeterMetaInfo->pMeterMeta); // functions can not be applied to tags if (index.columnIndex >= pMeterMetaInfo->pMeterMeta->numOfColumns) { - return invalidSqlErrMsg(pCmd, msg6); + return invalidSqlErrMsg(pQueryInfo->msg, msg6); } // 2. valid the column type int16_t colType = pSchema[index.columnIndex].type; if (colType == TSDB_DATA_TYPE_BOOL || colType >= TSDB_DATA_TYPE_BINARY) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(pQueryInfo->msg, msg1); } // 3. valid the parameters if (pParamElem[1].pNode->nSQLOptr == TK_ID) { - return invalidSqlErrMsg(pCmd, msg2); + return invalidSqlErrMsg(pQueryInfo->msg, msg2); } tVariant* pVariant = &pParamElem[1].pNode->val; @@ -1652,7 +1658,7 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, int32_t colIdx, tSQLExprItem* pItem double dp = *((double*)val); if (dp < 0 || dp > TOP_BOTTOM_QUERY_LIMIT) { - return invalidSqlErrMsg(pCmd, msg5); + return invalidSqlErrMsg(pQueryInfo->msg, msg5); } resultSize = sizeof(double); @@ -1668,14 +1674,14 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, int32_t colIdx, tSQLExprItem* pItem return TSDB_CODE_INVALID_SQL; } - SSqlExpr* pExpr = tscSqlExprInsert(pCmd, colIdx, functionId, &index, resultType, resultSize, resultSize); + SSqlExpr* pExpr = tscSqlExprInsert(pQueryInfo, colIdx, functionId, &index, resultType, resultSize, resultSize); addExprParams(pExpr, val, TSDB_DATA_TYPE_DOUBLE, sizeof(double), 0); } else { tVariantDump(pVariant, val, TSDB_DATA_TYPE_BIGINT); int64_t nTop = *((int32_t*)val); if (nTop <= 0 || nTop > 100) { // todo use macro - return invalidSqlErrMsg(pCmd, msg5); + return invalidSqlErrMsg(pQueryInfo->msg, msg5); } int16_t functionId = 0; @@ -1685,22 +1691,22 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, int32_t colIdx, tSQLExprItem* pItem // set the first column ts for top/bottom query SColumnIndex index1 = {0, PRIMARYKEY_TIMESTAMP_COL_INDEX}; - tscSqlExprInsert(pCmd, 0, TSDB_FUNC_TS, &index1, TSDB_DATA_TYPE_TIMESTAMP, TSDB_KEYSIZE, TSDB_KEYSIZE); + tscSqlExprInsert(pQueryInfo, 0, TSDB_FUNC_TS, &index1, TSDB_DATA_TYPE_TIMESTAMP, TSDB_KEYSIZE, TSDB_KEYSIZE); const int32_t TS_COLUMN_INDEX = 0; SColumnList ids = getColumnList(1, 0, TS_COLUMN_INDEX); - insertResultField(pCmd, TS_COLUMN_INDEX, &ids, TSDB_KEYSIZE, TSDB_DATA_TYPE_TIMESTAMP, + insertResultField(pQueryInfo, TS_COLUMN_INDEX, &ids, TSDB_KEYSIZE, TSDB_DATA_TYPE_TIMESTAMP, aAggs[TSDB_FUNC_TS].aName); colIdx += 1; // the first column is ts numOfAddedColumn += 1; - SSqlExpr* pExpr = tscSqlExprInsert(pCmd, colIdx, functionId, &index, resultType, resultSize, resultSize); + SSqlExpr* pExpr = tscSqlExprInsert(pQueryInfo, colIdx, functionId, &index, resultType, resultSize, resultSize); addExprParams(pExpr, val, TSDB_DATA_TYPE_BIGINT, sizeof(int64_t), 0); } SColumnList ids = getColumnList(1, 0, index.columnIndex); - insertResultField(pCmd, colIdx, &ids, resultSize, resultType, columnName); + insertResultField(pQueryInfo, colIdx, &ids, resultSize, resultType, columnName); return TSDB_CODE_SUCCESS; } @@ -1745,8 +1751,8 @@ static bool isTablenameToken(SSQLToken* token) { return (strncasecmp(TSQL_TBNAME_L, tmpToken.z, tmpToken.n) == 0 && tmpToken.n == strlen(TSQL_TBNAME_L)); } -static int16_t doGetColumnIndex(SSqlCmd* pCmd, int32_t index, SSQLToken* pToken) { - SMeterMeta* pMeterMeta = tscGetMeterMetaInfo(pCmd, index)->pMeterMeta; +static int16_t doGetColumnIndex(SQueryInfo* pQueryInfo, int32_t index, SSQLToken* pToken) { + SMeterMeta* pMeterMeta = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, index)->pMeterMeta; int32_t numOfCols = pMeterMeta->numOfColumns + pMeterMeta->numOfTags; SSchema* pSchema = tsGetSchema(pMeterMeta); @@ -1766,7 +1772,7 @@ static int16_t doGetColumnIndex(SSqlCmd* pCmd, int32_t index, SSQLToken* pToken) return columnIndex; } -int32_t doGetColumnIndexByName(SSQLToken* pToken, SSqlCmd* pCmd, SColumnIndex* pIndex) { +int32_t doGetColumnIndexByName(SSQLToken* pToken, SQueryInfo *pQueryInfo, SColumnIndex* pIndex) { const char* msg0 = "ambiguous column name"; const char* msg1 = "invalid column name"; @@ -1777,12 +1783,12 @@ int32_t doGetColumnIndexByName(SSQLToken* pToken, SSqlCmd* pCmd, SColumnIndex* p } else { // not specify the table name, try to locate the table index by column name if (pIndex->tableIndex == COLUMN_INDEX_INITIAL_VAL) { - for (int16_t i = 0; i < pCmd->pQueryInfo->numOfTables; ++i) { - int16_t colIndex = doGetColumnIndex(pCmd, i, pToken); + for (int16_t i = 0; i < pQueryInfo->numOfTables; ++i) { + int16_t colIndex = doGetColumnIndex(pQueryInfo, i, pToken); if (colIndex != COLUMN_INDEX_INITIAL_VAL) { if (pIndex->columnIndex != COLUMN_INDEX_INITIAL_VAL) { - return invalidSqlErrMsg(pCmd, msg0); + return invalidSqlErrMsg(pQueryInfo->msg, msg0); } else { pIndex->tableIndex = i; pIndex->columnIndex = colIndex; @@ -1790,14 +1796,14 @@ int32_t doGetColumnIndexByName(SSQLToken* pToken, SSqlCmd* pCmd, SColumnIndex* p } } } else { // table index is valid, get the column index - int16_t colIndex = doGetColumnIndex(pCmd, pIndex->tableIndex, pToken); + int16_t colIndex = doGetColumnIndex(pQueryInfo, pIndex->tableIndex, pToken); if (colIndex != COLUMN_INDEX_INITIAL_VAL) { pIndex->columnIndex = colIndex; } } if (pIndex->columnIndex == COLUMN_INDEX_INITIAL_VAL) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(pQueryInfo->msg, msg1); } } @@ -1808,9 +1814,9 @@ int32_t doGetColumnIndexByName(SSQLToken* pToken, SSqlCmd* pCmd, SColumnIndex* p } } -static int32_t getMeterIndex(SSQLToken* pTableToken, SSqlCmd* pCmd, SColumnIndex* pIndex) { +int32_t getMeterIndex(SSQLToken* pTableToken, SQueryInfo* pQueryInfo, SColumnIndex* pIndex) { if (pTableToken->n == 0) { // only one table and no table name prefix in column name - if (pCmd->pQueryInfo->numOfTables == 1) { + if (pQueryInfo->numOfTables == 1) { pIndex->tableIndex = 0; } @@ -1820,8 +1826,8 @@ static int32_t getMeterIndex(SSQLToken* pTableToken, SSqlCmd* pCmd, SColumnIndex pIndex->tableIndex = COLUMN_INDEX_INITIAL_VAL; char tableName[TSDB_METER_ID_LEN + 1] = {0}; - for (int32_t i = 0; i < pCmd->pQueryInfo->numOfTables; ++i) { - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, i); + for (int32_t i = 0; i < pQueryInfo->numOfTables; ++i) { + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, i); extractTableName(pMeterMetaInfo->name, tableName); if (strncasecmp(tableName, pTableToken->z, pTableToken->n) == 0 && strlen(tableName) == pTableToken->n) { @@ -1837,29 +1843,29 @@ static int32_t getMeterIndex(SSQLToken* pTableToken, SSqlCmd* pCmd, SColumnIndex return TSDB_CODE_SUCCESS; } -int32_t getTableIndexByName(SSQLToken* pToken, SSqlCmd* pCmd, SColumnIndex* pIndex) { +int32_t getTableIndexByName(SSQLToken* pToken, SQueryInfo *pQueryInfo, SColumnIndex* pIndex) { SSQLToken tableToken = {0}; extractTableNameFromToken(pToken, &tableToken); - if (getMeterIndex(&tableToken, pCmd, pIndex) != TSDB_CODE_SUCCESS) { + if (getMeterIndex(&tableToken, pQueryInfo, pIndex) != TSDB_CODE_SUCCESS) { return TSDB_CODE_INVALID_SQL; } return TSDB_CODE_SUCCESS; } -int32_t getColumnIndexByNameEx(SSQLToken* pToken, SSqlCmd* pCmd, SColumnIndex* pIndex) { - if (pCmd->pQueryInfo[0].pMeterInfo == NULL || pCmd->pQueryInfo->numOfTables == 0) { +int32_t getColumnIndexByNameEx(SSQLToken* pToken, SQueryInfo *pQueryInfo, SColumnIndex* pIndex) { + if (pQueryInfo->pMeterInfo == NULL || pQueryInfo->numOfTables == 0) { return TSDB_CODE_INVALID_SQL; } SSQLToken tmpToken = *pToken; - if (getTableIndexByName(&tmpToken, pCmd, pIndex) != TSDB_CODE_SUCCESS) { + if (getTableIndexByName(&tmpToken, pQueryInfo, pIndex) != TSDB_CODE_SUCCESS) { return TSDB_CODE_INVALID_SQL; } - return doGetColumnIndexByName(&tmpToken, pCmd, pIndex); + return doGetColumnIndexByName(&tmpToken, pQueryInfo, pIndex); } int32_t changeFunctionID(int32_t optr, int16_t* functionId) { @@ -1927,7 +1933,7 @@ int32_t changeFunctionID(int32_t optr, int16_t* functionId) { int32_t setShowInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { SSqlCmd* pCmd = &pSql->cmd; - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0); pCmd->command = TSDB_SQL_SHOW; @@ -1951,20 +1957,20 @@ int32_t setShowInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { assert(pDbPrefixToken->n >= 0); if (pDbPrefixToken->n > TSDB_DB_NAME_LEN) { // db name is too long - return invalidSqlErrMsg(pCmd, msg3); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3); } if (pDbPrefixToken->n <= 0) { - return invalidSqlErrMsg(pCmd, msg5); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg5); } if (tscValidateName(pDbPrefixToken) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); } int32_t ret = setObjFullName(pMeterMetaInfo->name, getAccountId(pSql), pDbPrefixToken, NULL, NULL); if (ret != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); } } @@ -1974,26 +1980,26 @@ int32_t setShowInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { pPattern->n = strdequote(pPattern->z); if (pPattern->n <= 0) { - return invalidSqlErrMsg(pCmd, msg6); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg6); } if (pCmd->payloadLen > TSDB_METER_NAME_LEN) { - return invalidSqlErrMsg(pCmd, msg2); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } } } else if (showType == TSDB_MGMT_TABLE_VNODES) { if (pShowInfo->prefix.type == 0) { - return invalidSqlErrMsg(pCmd, "No specified ip of dnode"); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), "No specified ip of dnode"); } // show vnodes may be ip addr of dnode in payload SSQLToken* pDnodeIp = &pShowInfo->prefix; if (pDnodeIp->n > TSDB_IPv4ADDR_LEN) { // ip addr is too long - return invalidSqlErrMsg(pCmd, msg3); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3); } if (!validateIpAddress(pDnodeIp->z, pDnodeIp->n)) { - return invalidSqlErrMsg(pCmd, msg4); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg4); } } @@ -2022,13 +2028,13 @@ int32_t setKillInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { if (!validateIpAddress(ipStr, strlen(ipStr))) { memset(pCmd->payload, 0, tListLen(pCmd->payload)); - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); } uint16_t port = (uint16_t)strtol(portStr, NULL, 10); if (port <= 0 || port > 65535) { memset(pCmd->payload, 0, tListLen(pCmd->payload)); - return invalidSqlErrMsg(pCmd, msg2); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } return TSDB_CODE_SUCCESS; @@ -2045,10 +2051,10 @@ bool validateIpAddress(const char* ip, size_t size) { return ipAddr != INADDR_NONE; } -int32_t tscTansformSQLFunctionForMetricQuery(SSqlCmd* pCmd) { - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); +int32_t tscTansformSQLFunctionForMetricQuery(SQueryInfo* pQueryInfo) { + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); - if (pMeterMetaInfo->pMeterMeta == NULL || !UTIL_METER_IS_METRIC(pMeterMetaInfo)) { + if (pMeterMetaInfo->pMeterMeta == NULL || !UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo)) { return TSDB_CODE_INVALID_SQL; } @@ -2058,9 +2064,9 @@ int32_t tscTansformSQLFunctionForMetricQuery(SSqlCmd* pCmd) { int16_t type = 0; int16_t intermediateBytes = 0; - for (int32_t k = 0; k < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++k) { - SSqlExpr* pExpr = tscSqlExprGet(pCmd, k); - TAOS_FIELD* pField = tscFieldInfoGetField(pCmd, k); + for (int32_t k = 0; k < pQueryInfo->fieldsInfo.numOfOutputCols; ++k) { + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, k); + TAOS_FIELD* pField = tscFieldInfoGetField(pQueryInfo, k); int16_t functionId = aAggs[pExpr->functionId].stableFuncId; @@ -2071,26 +2077,26 @@ int32_t tscTansformSQLFunctionForMetricQuery(SSqlCmd* pCmd) { return TSDB_CODE_INVALID_SQL; } - tscSqlExprUpdate(pCmd, k, functionId, pExpr->colInfo.colIdx, TSDB_DATA_TYPE_BINARY, bytes); + tscSqlExprUpdate(pQueryInfo, k, functionId, pExpr->colInfo.colIdx, TSDB_DATA_TYPE_BINARY, bytes); // todo refactor pExpr->interResBytes = intermediateBytes; } } - tscFieldInfoUpdateOffset(pCmd); + tscFieldInfoUpdateOffset(pQueryInfo); return TSDB_CODE_SUCCESS; } /* transfer the field-info back to original input format */ -void tscRestoreSQLFunctionForMetricQuery(SSqlCmd* pCmd) { - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); - if (!UTIL_METER_IS_METRIC(pMeterMetaInfo)) { +void tscRestoreSQLFunctionForMetricQuery(SQueryInfo* pQueryInfo) { + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); + if (!UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo)) { return; } - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - SSqlExpr* pExpr = tscSqlExprGet(pCmd, i); - TAOS_FIELD* pField = tscFieldInfoGetField(pCmd, i); + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i); + TAOS_FIELD* pField = tscFieldInfoGetField(pQueryInfo, i); if ((pExpr->functionId >= TSDB_FUNC_FIRST_DST && pExpr->functionId <= TSDB_FUNC_LAST_DST) || (pExpr->functionId >= TSDB_FUNC_SUM && pExpr->functionId <= TSDB_FUNC_MAX)) { @@ -2100,26 +2106,27 @@ void tscRestoreSQLFunctionForMetricQuery(SSqlCmd* pCmd) { } } -bool hasUnsupportFunctionsForMetricQuery(SSqlCmd* pCmd) { +bool hasUnsupportFunctionsForMetricQuery(SQueryInfo* pQueryInfo) { const char* msg1 = "TWA not allowed to apply to super table directly"; const char* msg2 = "TWA only support group by tbname for super table query"; // filter sql function not supported by metric query yet. - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - int32_t functionId = tscSqlExprGet(pCmd, i)->functionId; + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + int32_t functionId = tscSqlExprGet(pQueryInfo, i)->functionId; if ((aAggs[functionId].nStatus & TSDB_FUNCSTATE_METRIC) == 0) { return true; } } - if (tscIsTWAQuery(pCmd)) { - if (pCmd->pQueryInfo[0].groupbyExpr.numOfGroupCols == 0) { - invalidSqlErrMsg(pCmd, msg1); + if (tscIsTWAQuery(pQueryInfo)) { + if (pQueryInfo->groupbyExpr.numOfGroupCols == 0) { + invalidSqlErrMsg(pQueryInfo->msg, msg1); return true; } - if (pCmd->pQueryInfo[0].groupbyExpr.numOfGroupCols != 1 || pCmd->pQueryInfo[0].groupbyExpr.columnInfo[0].colIdx != TSDB_TBNAME_COLUMN_INDEX) { - invalidSqlErrMsg(pCmd, msg2); + if (pQueryInfo->groupbyExpr.numOfGroupCols != 1 || + pQueryInfo->groupbyExpr.columnInfo[0].colIdx != TSDB_TBNAME_COLUMN_INDEX) { + invalidSqlErrMsg(pQueryInfo->msg, msg2); return true; } } @@ -2127,21 +2134,21 @@ bool hasUnsupportFunctionsForMetricQuery(SSqlCmd* pCmd) { return false; } -static bool functionCompatibleCheck(SSqlCmd* pCmd) { +static bool functionCompatibleCheck(SQueryInfo* pQueryInfo) { int32_t startIdx = 0; - int32_t functionID = tscSqlExprGet(pCmd, startIdx)->functionId; + int32_t functionID = tscSqlExprGet(pQueryInfo, startIdx)->functionId; // ts function can be simultaneously used with any other functions. if (functionID == TSDB_FUNC_TS || functionID == TSDB_FUNC_TS_DUMMY) { startIdx++; } - int32_t factor = funcCompatDefList[tscSqlExprGet(pCmd, startIdx)->functionId]; + int32_t factor = funcCompatDefList[tscSqlExprGet(pQueryInfo, startIdx)->functionId]; // diff function cannot be executed with other function // arithmetic function can be executed with other arithmetic functions - for (int32_t i = startIdx + 1; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - SSqlExpr* pExpr = tscSqlExprGet(pCmd, i); + for (int32_t i = startIdx + 1; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i); int16_t functionId = pExpr->functionId; if (functionId == TSDB_FUNC_TAGPRJ || functionId == TSDB_FUNC_TAG || functionId == TSDB_FUNC_TS) { @@ -2160,21 +2167,21 @@ static bool functionCompatibleCheck(SSqlCmd* pCmd) { return true; } -void updateTagColumnIndex(SSqlCmd* pCmd, int32_t tableIndex) { - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, tableIndex); +void updateTagColumnIndex(SQueryInfo* pQueryInfo, int32_t tableIndex) { + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, tableIndex); /* * update tags column index for group by tags * group by columns belong to this table */ - if (pCmd->pQueryInfo[0].groupbyExpr.numOfGroupCols > 0 && pCmd->pQueryInfo[0].groupbyExpr.tableIndex == tableIndex) { - for (int32_t i = 0; i < pCmd->pQueryInfo[0].groupbyExpr.numOfGroupCols; ++i) { - int32_t index = pCmd->pQueryInfo[0].groupbyExpr.columnInfo[i].colIdx; + if (pQueryInfo->groupbyExpr.numOfGroupCols > 0 && pQueryInfo->groupbyExpr.tableIndex == tableIndex) { + for (int32_t i = 0; i < pQueryInfo->groupbyExpr.numOfGroupCols; ++i) { + int32_t index = pQueryInfo->groupbyExpr.columnInfo[i].colIdx; for (int32_t j = 0; j < pMeterMetaInfo->numOfTags; ++j) { int32_t tagColIndex = pMeterMetaInfo->tagColumnIndex[j]; if (tagColIndex == index) { - pCmd->pQueryInfo[0].groupbyExpr.columnInfo[i].colIdx = j; + pQueryInfo->groupbyExpr.columnInfo[i].colIdx = j; break; } } @@ -2182,8 +2189,8 @@ void updateTagColumnIndex(SSqlCmd* pCmd, int32_t tableIndex) { } // update tags column index for expression - for (int32_t i = 0; i < pCmd->pQueryInfo[0].exprsInfo.numOfExprs; ++i) { - SSqlExpr* pExpr = tscSqlExprGet(pCmd, i); + for (int32_t i = 0; i < pQueryInfo->exprsInfo.numOfExprs; ++i) { + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i); if (!TSDB_COL_IS_TAG(pExpr->colInfo.flag)) { // not tags, continue continue; @@ -2203,7 +2210,7 @@ void updateTagColumnIndex(SSqlCmd* pCmd, int32_t tableIndex) { } // update join condition tag column index - SJoinInfo* pJoinInfo = &pCmd->pQueryInfo[0].tagCond.joinInfo; + SJoinInfo* pJoinInfo = &pQueryInfo->tagCond.joinInfo; if (!pJoinInfo->hasJoin) { // not join query return; } @@ -2228,7 +2235,7 @@ void updateTagColumnIndex(SSqlCmd* pCmd, int32_t tableIndex) { } } -int32_t parseGroupbyClause(SSqlCmd* pCmd, tVariantList* pList) { +int32_t parseGroupbyClause(SQueryInfo* pQueryInfo, tVariantList* pList, SSqlCmd* pCmd) { const char* msg1 = "too many columns in group by clause"; const char* msg2 = "invalid column name in group by clause"; const char* msg3 = "group by columns must belong to one table"; @@ -2243,9 +2250,9 @@ int32_t parseGroupbyClause(SSqlCmd* pCmd, tVariantList* pList) { return TSDB_CODE_SUCCESS; } - pCmd->pQueryInfo[0].groupbyExpr.numOfGroupCols = pList->nExpr; + pQueryInfo->groupbyExpr.numOfGroupCols = pList->nExpr; if (pList->nExpr > TSDB_MAX_TAGS) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(pQueryInfo->msg, msg1); } SMeterMeta* pMeterMeta = NULL; @@ -2260,17 +2267,17 @@ int32_t parseGroupbyClause(SSqlCmd* pCmd, tVariantList* pList) { SColumnIndex index = COLUMN_INDEX_INITIALIZER; - if (getColumnIndexByNameEx(&token, pCmd, &index) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg2); + if (getColumnIndexByNameEx(&token, pQueryInfo, &index) != TSDB_CODE_SUCCESS) { + return invalidSqlErrMsg(pQueryInfo->msg, msg2); } if (tableIndex != index.tableIndex && tableIndex >= 0) { - return invalidSqlErrMsg(pCmd, msg3); + return invalidSqlErrMsg(pQueryInfo->msg, msg3); } tableIndex = index.tableIndex; - pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, index.tableIndex); + pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, index.tableIndex); pMeterMeta = pMeterMetaInfo->pMeterMeta; if (index.columnIndex == TSDB_TBNAME_COLUMN_INDEX) { @@ -2285,8 +2292,8 @@ int32_t parseGroupbyClause(SSqlCmd* pCmd, tVariantList* pList) { } if (groupTag) { - if (!UTIL_METER_IS_METRIC(pMeterMetaInfo)) { - return invalidSqlErrMsg(pCmd, msg9); + if (!UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo)) { + return invalidSqlErrMsg(pQueryInfo->msg, msg9); } int32_t relIndex = index.columnIndex; @@ -2294,36 +2301,36 @@ int32_t parseGroupbyClause(SSqlCmd* pCmd, tVariantList* pList) { relIndex -= pMeterMeta->numOfColumns; } - pCmd->pQueryInfo[0].groupbyExpr.columnInfo[i] = + pQueryInfo->groupbyExpr.columnInfo[i] = (SColIndexEx){.colIdx = relIndex, .flag = TSDB_COL_TAG, .colId = pSchema->colId}; // relIndex; - addRequiredTagColumn(pCmd, pCmd->pQueryInfo[0].groupbyExpr.columnInfo[i].colIdx, index.tableIndex); + addRequiredTagColumn(pQueryInfo, pQueryInfo->groupbyExpr.columnInfo[i].colIdx, index.tableIndex); } else { // check if the column type is valid, here only support the bool/tinyint/smallint/bigint group by if (pSchema->type > TSDB_DATA_TYPE_BINARY) { - return invalidSqlErrMsg(pCmd, msg8); + return invalidSqlErrMsg(pQueryInfo->msg, msg8); } - tscColumnBaseInfoInsert(pCmd, &index); - pCmd->pQueryInfo[0].groupbyExpr.columnInfo[i] = + tscColumnBaseInfoInsert(pQueryInfo, &index); + pQueryInfo->groupbyExpr.columnInfo[i] = (SColIndexEx){.colIdx = index.columnIndex, .flag = TSDB_COL_NORMAL, .colId = pSchema->colId}; // relIndex; - pCmd->pQueryInfo[0].groupbyExpr.orderType = TSQL_SO_ASC; + pQueryInfo->groupbyExpr.orderType = TSQL_SO_ASC; if (i == 0 && pList->nExpr > 1) { - return invalidSqlErrMsg(pCmd, msg7); + return invalidSqlErrMsg(pQueryInfo->msg, msg7); } } } - pCmd->pQueryInfo[0].groupbyExpr.tableIndex = tableIndex; + pQueryInfo->groupbyExpr.tableIndex = tableIndex; return TSDB_CODE_SUCCESS; } -void setColumnOffsetValueInResultset(SSqlCmd* pCmd) { - if (QUERY_IS_STABLE_QUERY(pCmd->type)) { - tscFieldInfoUpdateOffset(pCmd); +void setColumnOffsetValueInResultset(SQueryInfo* pQueryInfo) { + if (QUERY_IS_STABLE_QUERY(pQueryInfo->type)) { + tscFieldInfoUpdateOffset(pQueryInfo); } else { - tscFieldInfoCalOffset(pCmd); + tscFieldInfoCalOffset(pQueryInfo); } } @@ -2346,12 +2353,12 @@ static SColumnFilterInfo* addColumnFilterInfo(SColumnBase* pColumn) { return pColFilterInfo; } -static int32_t doExtractColumnFilterInfo(SSqlCmd* pCmd, SColumnFilterInfo* pColumnFilter, SColumnIndex* columnIndex, +static int32_t doExtractColumnFilterInfo(SQueryInfo* pQueryInfo, SColumnFilterInfo* pColumnFilter, SColumnIndex* columnIndex, tSQLExpr* pExpr) { const char* msg = "not supported filter condition"; tSQLExpr* pRight = pExpr->pRight; - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, columnIndex->tableIndex); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, columnIndex->tableIndex); SSchema* pSchema = tsGetColumnSchema(pMeterMetaInfo->pMeterMeta, columnIndex->columnIndex); @@ -2361,7 +2368,7 @@ static int32_t doExtractColumnFilterInfo(SSqlCmd* pCmd, SColumnFilterInfo* pColu } else if (colType == TSDB_DATA_TYPE_FLOAT || colType == TSDB_DATA_TYPE_DOUBLE) { colType = TSDB_DATA_TYPE_DOUBLE; } else if ((colType == TSDB_DATA_TYPE_TIMESTAMP) && (TSDB_DATA_TYPE_BINARY == pRight->val.nType)) { - int retVal = setColumnFilterInfoForTimestamp(pCmd, &pRight->val); + int retVal = setColumnFilterInfoForTimestamp(pQueryInfo, &pRight->val); if (TSDB_CODE_SUCCESS != retVal) { return retVal; } @@ -2411,7 +2418,7 @@ static int32_t doExtractColumnFilterInfo(SSqlCmd* pCmd, SColumnFilterInfo* pColu pColumnFilter->lowerRelOptr = TSDB_RELATION_LIKE; break; default: - return invalidSqlErrMsg(pCmd, msg); + return invalidSqlErrMsg(pQueryInfo->msg, msg); } return TSDB_CODE_SUCCESS; @@ -2589,8 +2596,8 @@ enum { TSQL_EXPR_TBNAME = 3, }; -static int32_t extractColumnFilterInfo(SSqlCmd* pCmd, SColumnIndex* pIndex, tSQLExpr* pExpr, int32_t sqlOptr) { - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, pIndex->tableIndex); +static int32_t extractColumnFilterInfo(SQueryInfo* pQueryInfo, SColumnIndex* pIndex, tSQLExpr* pExpr, int32_t sqlOptr) { + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, pIndex->tableIndex); SMeterMeta* pMeterMeta = pMeterMetaInfo->pMeterMeta; SSchema* pSchema = tsGetColumnSchema(pMeterMeta, pIndex->columnIndex); @@ -2598,7 +2605,7 @@ static int32_t extractColumnFilterInfo(SSqlCmd* pCmd, SColumnIndex* pIndex, tSQL const char* msg1 = "non binary column not support like operator"; const char* msg2 = "binary column not support this operator"; - SColumnBase* pColumn = tscColumnBaseInfoInsert(pCmd, pIndex); + SColumnBase* pColumn = tscColumnBaseInfoInsert(pQueryInfo, pIndex); SColumnFilterInfo* pColFilter = NULL; /* @@ -2624,25 +2631,25 @@ static int32_t extractColumnFilterInfo(SSqlCmd* pCmd, SColumnIndex* pIndex, tSQL if (pColFilter->filterOnBinary) { if (pExpr->nSQLOptr != TK_EQ && pExpr->nSQLOptr != TK_NE && pExpr->nSQLOptr != TK_LIKE) { - return invalidSqlErrMsg(pCmd, msg2); + return invalidSqlErrMsg(pQueryInfo->msg, msg2); } } else { if (pExpr->nSQLOptr == TK_LIKE) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(pQueryInfo->msg, msg1); } } pColumn->colIndex = *pIndex; - return doExtractColumnFilterInfo(pCmd, pColFilter, pIndex, pExpr); + return doExtractColumnFilterInfo(pQueryInfo, pColFilter, pIndex, pExpr); } -static void relToString(SSqlCmd* pCmd, tSQLExpr* pExpr, char** str) { +static void relToString(tSQLExpr* pExpr, char** str) { assert(pExpr->nSQLOptr == TK_AND || pExpr->nSQLOptr == TK_OR); const char* or = "OR"; const char*and = "AND"; - // if (pCmd->pQueryInfo[0].tagCond.relType == TSQL_STABLE_QTYPE_COND) { + // if (pQueryInfo->tagCond.relType == TSQL_STABLE_QTYPE_COND) { if (pExpr->nSQLOptr == TK_AND) { strcpy(*str, and); *str += strlen(and); @@ -2650,10 +2657,9 @@ static void relToString(SSqlCmd* pCmd, tSQLExpr* pExpr, char** str) { strcpy(*str, or); *str += strlen(or); } - // } } -static int32_t getTagCondString(SSqlCmd* pCmd, tSQLExpr* pExpr, char** str) { +static int32_t getTagCondString(tSQLExpr* pExpr, char** str) { if (pExpr == NULL) { return TSDB_CODE_SUCCESS; } @@ -2662,14 +2668,14 @@ static int32_t getTagCondString(SSqlCmd* pCmd, tSQLExpr* pExpr, char** str) { *(*str) = '('; *str += 1; - int32_t ret = getTagCondString(pCmd, pExpr->pLeft, str); + int32_t ret = getTagCondString(pExpr->pLeft, str); if (ret != TSDB_CODE_SUCCESS) { return ret; } - relToString(pCmd, pExpr, str); + relToString(pExpr, str); - ret = getTagCondString(pCmd, pExpr->pRight, str); + ret = getTagCondString(pExpr->pRight, str); *(*str) = ')'; *str += 1; @@ -2680,7 +2686,7 @@ static int32_t getTagCondString(SSqlCmd* pCmd, tSQLExpr* pExpr, char** str) { return tSQLExprLeafToString(pExpr, true, str); } -static int32_t getTablenameCond(SSqlCmd* pCmd, tSQLExpr* pTableCond, /*char* str*/ SStringBuilder* sb) { +static int32_t getTablenameCond(SQueryInfo* pQueryInfo, tSQLExpr* pTableCond, SStringBuilder* sb) { const char* msg0 = "invalid table name list"; if (pTableCond == NULL) { @@ -2703,57 +2709,55 @@ static int32_t getTablenameCond(SSqlCmd* pCmd, tSQLExpr* pTableCond, /*char* str } if (ret != TSDB_CODE_SUCCESS) { - invalidSqlErrMsg(pCmd, msg0); + invalidSqlErrMsg(pQueryInfo->msg, msg0); } return ret; } -static int32_t getColumnQueryCondInfo(SSqlCmd* pCmd, tSQLExpr* pExpr, int32_t relOptr) { +static int32_t getColumnQueryCondInfo(SQueryInfo* pQueryInfo, tSQLExpr* pExpr, int32_t relOptr) { if (pExpr == NULL) { return TSDB_CODE_SUCCESS; } if (!isExprDirectParentOfLeaftNode(pExpr)) { // internal node - int32_t ret = getColumnQueryCondInfo(pCmd, pExpr->pLeft, pExpr->nSQLOptr); + int32_t ret = getColumnQueryCondInfo(pQueryInfo, pExpr->pLeft, pExpr->nSQLOptr); if (ret != TSDB_CODE_SUCCESS) { return ret; } - return getColumnQueryCondInfo(pCmd, pExpr->pRight, pExpr->nSQLOptr); + return getColumnQueryCondInfo(pQueryInfo, pExpr->pRight, pExpr->nSQLOptr); } else { // handle leaf node SColumnIndex index = COLUMN_INDEX_INITIALIZER; - if (getColumnIndexByNameEx(&pExpr->pLeft->colInfo, pCmd, &index) != TSDB_CODE_SUCCESS) { + if (getColumnIndexByNameEx(&pExpr->pLeft->colInfo, pQueryInfo, &index) != TSDB_CODE_SUCCESS) { return TSDB_CODE_INVALID_SQL; } - return extractColumnFilterInfo(pCmd, &index, pExpr, relOptr); + return extractColumnFilterInfo(pQueryInfo, &index, pExpr, relOptr); } } -static int32_t getJoinCondInfo(SSqlObj* pSql, tSQLExpr* pExpr) { +static int32_t getJoinCondInfo(SQueryInfo* pQueryInfo, tSQLExpr* pExpr) { const char* msg = "invalid join query condition"; if (pExpr == NULL) { return TSDB_CODE_SUCCESS; } - SSqlCmd* pCmd = &pSql->cmd; - if (!isExprDirectParentOfLeaftNode(pExpr)) { - return invalidSqlErrMsg(pCmd, msg); + return invalidSqlErrMsg(pQueryInfo->msg, msg); } - STagCond* pTagCond = &pCmd->pQueryInfo[0].tagCond; + STagCond* pTagCond = &pQueryInfo->tagCond; SJoinNode* pLeft = &pTagCond->joinInfo.left; SJoinNode* pRight = &pTagCond->joinInfo.right; SColumnIndex index = COLUMN_INDEX_INITIALIZER; - if (getColumnIndexByNameEx(&pExpr->pLeft->colInfo, pCmd, &index) != TSDB_CODE_SUCCESS) { + if (getColumnIndexByNameEx(&pExpr->pLeft->colInfo, pQueryInfo, &index) != TSDB_CODE_SUCCESS) { return TSDB_CODE_INVALID_SQL; } - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, index.tableIndex); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, index.tableIndex); int16_t tagColIndex = index.columnIndex - pMeterMetaInfo->pMeterMeta->numOfColumns; pLeft->uid = pMeterMetaInfo->pMeterMeta->uid; @@ -2761,11 +2765,11 @@ static int32_t getJoinCondInfo(SSqlObj* pSql, tSQLExpr* pExpr) { strcpy(pLeft->meterId, pMeterMetaInfo->name); index = (SColumnIndex)COLUMN_INDEX_INITIALIZER; - if (getColumnIndexByNameEx(&pExpr->pRight->colInfo, pCmd, &index) != TSDB_CODE_SUCCESS) { + if (getColumnIndexByNameEx(&pExpr->pRight->colInfo, pQueryInfo, &index) != TSDB_CODE_SUCCESS) { return TSDB_CODE_INVALID_SQL; } - pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, index.tableIndex); + pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, index.tableIndex); tagColIndex = index.columnIndex - pMeterMetaInfo->pMeterMeta->numOfColumns; pRight->uid = pMeterMetaInfo->pMeterMeta->uid; @@ -2937,7 +2941,7 @@ static void exchangeExpr(tSQLExpr* pExpr) { } } -static bool validateJoinExprNode(SSqlCmd* pCmd, tSQLExpr* pExpr, SColumnIndex* pLeftIndex) { +static bool validateJoinExprNode(SQueryInfo* pQueryInfo, tSQLExpr* pExpr, SColumnIndex* pLeftIndex) { const char* msg1 = "illegal column name"; const char* msg2 = "= is expected in join expression"; const char* msg3 = "join column must have same type"; @@ -2952,40 +2956,40 @@ static bool validateJoinExprNode(SSqlCmd* pCmd, tSQLExpr* pExpr, SColumnIndex* p } if (pExpr->nSQLOptr != TK_EQ) { - invalidSqlErrMsg(pCmd, msg2); + invalidSqlErrMsg(pQueryInfo->msg, msg2); return false; } SColumnIndex rightIndex = COLUMN_INDEX_INITIALIZER; - if (getColumnIndexByNameEx(&pRight->colInfo, pCmd, &rightIndex) != TSDB_CODE_SUCCESS) { - invalidSqlErrMsg(pCmd, msg1); + if (getColumnIndexByNameEx(&pRight->colInfo, pQueryInfo, &rightIndex) != TSDB_CODE_SUCCESS) { + invalidSqlErrMsg(pQueryInfo->msg, msg1); return false; } // todo extract function - SMeterMetaInfo* pLeftMeterMeta = tscGetMeterMetaInfo(pCmd, pLeftIndex->tableIndex); + SMeterMetaInfo* pLeftMeterMeta = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, pLeftIndex->tableIndex); SSchema* pLeftSchema = tsGetSchema(pLeftMeterMeta->pMeterMeta); int16_t leftType = pLeftSchema[pLeftIndex->columnIndex].type; - SMeterMetaInfo* pRightMeterMeta = tscGetMeterMetaInfo(pCmd, rightIndex.tableIndex); + SMeterMetaInfo* pRightMeterMeta = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, rightIndex.tableIndex); SSchema* pRightSchema = tsGetSchema(pRightMeterMeta->pMeterMeta); int16_t rightType = pRightSchema[rightIndex.columnIndex].type; if (leftType != rightType) { - invalidSqlErrMsg(pCmd, msg3); + invalidSqlErrMsg(pQueryInfo->msg, msg3); return false; } else if (pLeftIndex->tableIndex == rightIndex.tableIndex) { - invalidSqlErrMsg(pCmd, msg4); + invalidSqlErrMsg(pQueryInfo->msg, msg4); return false; } else if (leftType == TSDB_DATA_TYPE_BINARY || leftType == TSDB_DATA_TYPE_NCHAR) { - invalidSqlErrMsg(pCmd, msg6); + invalidSqlErrMsg(pQueryInfo->msg, msg6); return false; } // table to table/ super table to super table are allowed - if (UTIL_METER_IS_METRIC(pLeftMeterMeta) != UTIL_METER_IS_METRIC(pRightMeterMeta)) { - invalidSqlErrMsg(pCmd, msg5); + if (UTIL_METER_IS_SUPERTABLE(pLeftMeterMeta) != UTIL_METER_IS_SUPERTABLE(pRightMeterMeta)) { + invalidSqlErrMsg(pQueryInfo->msg, msg5); return false; } @@ -3004,10 +3008,10 @@ static bool validTableNameOptr(tSQLExpr* pExpr) { return false; } -static int32_t setExprToCond(SSqlCmd* pCmd, tSQLExpr** parent, tSQLExpr* pExpr, const char* msg, int32_t parentOptr) { +static int32_t setExprToCond(tSQLExpr** parent, tSQLExpr* pExpr, const char* msg, int32_t parentOptr, char* msgBuf) { if (*parent != NULL) { if (parentOptr == TK_OR && msg != NULL) { - return invalidSqlErrMsg(pCmd, msg); + return invalidSqlErrMsg(msgBuf, msg); } *parent = tSQLExprCreate((*parent), pExpr, parentOptr); @@ -3018,7 +3022,7 @@ static int32_t setExprToCond(SSqlCmd* pCmd, tSQLExpr** parent, tSQLExpr* pExpr, return TSDB_CODE_SUCCESS; } -static int32_t handleExprInQueryCond(SSqlCmd* pCmd, tSQLExpr** pExpr, SCondExpr* pCondExpr, int32_t* type, +static int32_t handleExprInQueryCond(SQueryInfo* pQueryInfo, tSQLExpr** pExpr, SCondExpr* pCondExpr, int32_t* type, int32_t parentOptr) { const char* msg1 = "meter query cannot use tags filter"; const char* msg2 = "illegal column name"; @@ -3034,23 +3038,23 @@ static int32_t handleExprInQueryCond(SSqlCmd* pCmd, tSQLExpr** pExpr, SCondExpr* int32_t ret = TSDB_CODE_SUCCESS; SColumnIndex index = COLUMN_INDEX_INITIALIZER; - if (getColumnIndexByNameEx(&pLeft->colInfo, pCmd, &index) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg2); + if (getColumnIndexByNameEx(&pLeft->colInfo, pQueryInfo, &index) != TSDB_CODE_SUCCESS) { + return invalidSqlErrMsg(pQueryInfo->msg, msg2); } assert(isExprDirectParentOfLeaftNode(*pExpr)); - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, index.tableIndex); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, index.tableIndex); SMeterMeta* pMeterMeta = pMeterMetaInfo->pMeterMeta; if (index.columnIndex == PRIMARYKEY_TIMESTAMP_COL_INDEX) { // query on time range - if (!validateJoinExprNode(pCmd, *pExpr, &index)) { + if (!validateJoinExprNode(pQueryInfo, *pExpr, &index)) { return TSDB_CODE_INVALID_SQL; } // set join query condition if (pRight->nSQLOptr == TK_ID) { // no need to keep the timestamp join condition - pCmd->type |= TSDB_QUERY_TYPE_JOIN_QUERY; + pQueryInfo->type |= TSDB_QUERY_TYPE_JOIN_QUERY; pCondExpr->tsJoin = true; /* @@ -3059,7 +3063,7 @@ static int32_t handleExprInQueryCond(SSqlCmd* pCmd, tSQLExpr** pExpr, SCondExpr* */ tSQLExprDestroy(*pExpr); } else { - ret = setExprToCond(pCmd, &pCondExpr->pTimewindow, *pExpr, msg3, parentOptr); + ret = setExprToCond(&pCondExpr->pTimewindow, *pExpr, msg3, parentOptr, pQueryInfo->msg); } *pExpr = NULL; // remove this expression @@ -3068,7 +3072,7 @@ static int32_t handleExprInQueryCond(SSqlCmd* pCmd, tSQLExpr** pExpr, SCondExpr* index.columnIndex == TSDB_TBNAME_COLUMN_INDEX) { // query on tags // check for tag query condition if (UTIL_METER_IS_NOMRAL_METER(pMeterMetaInfo)) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(pQueryInfo->msg, msg1); } // check for like expression @@ -3081,14 +3085,14 @@ static int32_t handleExprInQueryCond(SSqlCmd* pCmd, tSQLExpr** pExpr, SCondExpr* if ((!isTablenameToken(&pLeft->colInfo)) && pSchema[index.columnIndex].type != TSDB_DATA_TYPE_BINARY && pSchema[index.columnIndex].type != TSDB_DATA_TYPE_NCHAR) { - return invalidSqlErrMsg(pCmd, msg2); + return invalidSqlErrMsg(pQueryInfo->msg, msg2); } } // in case of in operator, keep it in a seperate attribute if (index.columnIndex == TSDB_TBNAME_COLUMN_INDEX) { if (!validTableNameOptr(*pExpr)) { - return invalidSqlErrMsg(pCmd, msg7); + return invalidSqlErrMsg(pQueryInfo->msg, msg7); } if (pCondExpr->pTableCond == NULL) { @@ -3096,23 +3100,23 @@ static int32_t handleExprInQueryCond(SSqlCmd* pCmd, tSQLExpr** pExpr, SCondExpr* pCondExpr->relType = parentOptr; pCondExpr->tableCondIndex = index.tableIndex; } else { - return invalidSqlErrMsg(pCmd, msg6); + return invalidSqlErrMsg(pQueryInfo->msg, msg6); } *type = TSQL_EXPR_TBNAME; *pExpr = NULL; } else { if (pRight->nSQLOptr == TK_ID) { // join on tag columns for stable query - if (!validateJoinExprNode(pCmd, *pExpr, &index)) { + if (!validateJoinExprNode(pQueryInfo, *pExpr, &index)) { return TSDB_CODE_INVALID_SQL; } if (pCondExpr->pJoinExpr != NULL) { - return invalidSqlErrMsg(pCmd, msg4); + return invalidSqlErrMsg(pQueryInfo->msg, msg4); } - pCmd->type |= TSDB_QUERY_TYPE_JOIN_QUERY; - ret = setExprToCond(pCmd, &pCondExpr->pJoinExpr, *pExpr, NULL, parentOptr); + pQueryInfo->type |= TSDB_QUERY_TYPE_JOIN_QUERY; + ret = setExprToCond(&pCondExpr->pJoinExpr, *pExpr, NULL, parentOptr, pQueryInfo->msg); *pExpr = NULL; } else { // do nothing @@ -3127,17 +3131,17 @@ static int32_t handleExprInQueryCond(SSqlCmd* pCmd, tSQLExpr** pExpr, SCondExpr* *type = TSQL_EXPR_COLUMN; if (pRight->nSQLOptr == TK_ID) { // other column cannot be served as the join column - return invalidSqlErrMsg(pCmd, msg5); + return invalidSqlErrMsg(pQueryInfo->msg, msg5); } - ret = setExprToCond(pCmd, &pCondExpr->pColumnCond, *pExpr, NULL, parentOptr); + ret = setExprToCond(&pCondExpr->pColumnCond, *pExpr, NULL, parentOptr, pQueryInfo->msg); *pExpr = NULL; // remove it from expr tree } return ret; } -int32_t getQueryCondExpr(SSqlCmd* pCmd, tSQLExpr** pExpr, SCondExpr* pCondExpr, int32_t* type, int32_t parentOptr) { +int32_t getQueryCondExpr(SQueryInfo* pQueryInfo, tSQLExpr** pExpr, SCondExpr* pCondExpr, int32_t* type, int32_t parentOptr) { if (pExpr == NULL) { return TSDB_CODE_SUCCESS; } @@ -3155,12 +3159,12 @@ int32_t getQueryCondExpr(SSqlCmd* pCmd, tSQLExpr** pExpr, SCondExpr* pCondExpr, int32_t rightType = -1; if (!isExprDirectParentOfLeaftNode(*pExpr)) { - int32_t ret = getQueryCondExpr(pCmd, &(*pExpr)->pLeft, pCondExpr, &leftType, (*pExpr)->nSQLOptr); + int32_t ret = getQueryCondExpr(pQueryInfo, &(*pExpr)->pLeft, pCondExpr, &leftType, (*pExpr)->nSQLOptr); if (ret != TSDB_CODE_SUCCESS) { return ret; } - ret = getQueryCondExpr(pCmd, &(*pExpr)->pRight, pCondExpr, &rightType, (*pExpr)->nSQLOptr); + ret = getQueryCondExpr(pQueryInfo, &(*pExpr)->pRight, pCondExpr, &rightType, (*pExpr)->nSQLOptr); if (ret != TSDB_CODE_SUCCESS) { return ret; } @@ -3171,7 +3175,7 @@ int32_t getQueryCondExpr(SSqlCmd* pCmd, tSQLExpr** pExpr, SCondExpr* pCondExpr, */ if (leftType != rightType) { if ((*pExpr)->nSQLOptr == TK_OR && (leftType + rightType != TSQL_EXPR_TBNAME + TSQL_EXPR_TAG)) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(pQueryInfo->msg, msg1); } } @@ -3181,7 +3185,7 @@ int32_t getQueryCondExpr(SSqlCmd* pCmd, tSQLExpr** pExpr, SCondExpr* pCondExpr, exchangeExpr(*pExpr); - return handleExprInQueryCond(pCmd, pExpr, pCondExpr, type, parentOptr); + return handleExprInQueryCond(pQueryInfo, pExpr, pCondExpr, type, parentOptr); } static void doCompactQueryExpr(tSQLExpr** pExpr) { @@ -3215,12 +3219,12 @@ static void doCompactQueryExpr(tSQLExpr** pExpr) { } } -static void doExtractExprForSTable(tSQLExpr** pExpr, SSqlCmd* pCmd, tSQLExpr** pOut, int32_t tableIndex) { +static void doExtractExprForSTable(tSQLExpr** pExpr, SQueryInfo* pQueryInfo, tSQLExpr** pOut, int32_t tableIndex) { if (isExprDirectParentOfLeaftNode(*pExpr)) { tSQLExpr* pLeft = (*pExpr)->pLeft; SColumnIndex index = COLUMN_INDEX_INITIALIZER; - if (getColumnIndexByNameEx(&pLeft->colInfo, pCmd, &index) != TSDB_CODE_SUCCESS) { + if (getColumnIndexByNameEx(&pLeft->colInfo, pQueryInfo, &index) != TSDB_CODE_SUCCESS) { return; } @@ -3237,16 +3241,16 @@ static void doExtractExprForSTable(tSQLExpr** pExpr, SSqlCmd* pCmd, tSQLExpr** p } else { *pOut = tSQLExprCreate(NULL, NULL, (*pExpr)->nSQLOptr); - doExtractExprForSTable(&(*pExpr)->pLeft, pCmd, &((*pOut)->pLeft), tableIndex); - doExtractExprForSTable(&(*pExpr)->pRight, pCmd, &((*pOut)->pRight), tableIndex); + doExtractExprForSTable(&(*pExpr)->pLeft, pQueryInfo, &((*pOut)->pLeft), tableIndex); + doExtractExprForSTable(&(*pExpr)->pRight, pQueryInfo, &((*pOut)->pRight), tableIndex); } } -static tSQLExpr* extractExprForSTable(tSQLExpr** pExpr, SSqlCmd* pCmd, int32_t tableIndex) { +static tSQLExpr* extractExprForSTable(tSQLExpr** pExpr, SQueryInfo* pQueryInfo, int32_t tableIndex) { tSQLExpr* pResExpr = NULL; if (*pExpr != NULL) { - doExtractExprForSTable(pExpr, pCmd, &pResExpr, tableIndex); + doExtractExprForSTable(pExpr, pQueryInfo, &pResExpr, tableIndex); doCompactQueryExpr(&pResExpr); } @@ -3266,24 +3270,23 @@ int tableNameCompar(const void* lhs, const void* rhs) { return ret > 0 ? 1 : -1; } -static int32_t setTableCondForMetricQuery(SSqlObj* pSql, tSQLExpr* pExpr, int16_t tableCondIndex, SStringBuilder* sb) { - SSqlCmd* pCmd = &pSql->cmd; - const char* msg = "meter name too long"; +static int32_t setTableCondForMetricQuery(SQueryInfo *pQueryInfo, const char* account, tSQLExpr* pExpr, int16_t tableCondIndex, SStringBuilder* sb) { + const char* msg = "table name too long"; if (pExpr == NULL) { return TSDB_CODE_SUCCESS; } - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, tableCondIndex); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, tableCondIndex); - STagCond* pTagCond = &pSql->cmd.pQueryInfo[0].tagCond; + STagCond* pTagCond = &pQueryInfo->tagCond; pTagCond->tbnameCond.uid = pMeterMetaInfo->pMeterMeta->uid; assert(pExpr->nSQLOptr == TK_LIKE || pExpr->nSQLOptr == TK_IN); if (pExpr->nSQLOptr == TK_LIKE) { char* str = taosStringBuilderGetResult(sb, NULL); - pCmd->pQueryInfo[0].tagCond.tbnameCond.cond = strdup(str); + pQueryInfo->tagCond.tbnameCond.cond = strdup(str); return TSDB_CODE_SUCCESS; } @@ -3308,7 +3311,6 @@ static int32_t setTableCondForMetricQuery(SSqlObj* pSql, tSQLExpr* pExpr, int16_ num = j; SSQLToken dbToken = extractDBName(pMeterMetaInfo->name, db); - char* acc = getAccountId(pSql); for (int32_t i = 0; i < num; ++i) { if (i >= 1) { @@ -3319,12 +3321,12 @@ static int32_t setTableCondForMetricQuery(SSqlObj* pSql, tSQLExpr* pExpr, int16_ int32_t xlen = strlen(segments[i]); SSQLToken t = {.z = segments[i], .n = xlen, .type = TK_STRING}; - int32_t ret = setObjFullName(idBuf, acc, &dbToken, &t, &xlen); + int32_t ret = setObjFullName(idBuf, account, &dbToken, &t, &xlen); if (ret != TSDB_CODE_SUCCESS) { taosStringBuilderDestroy(&sb1); tfree(segments); - invalidSqlErrMsg(pCmd, msg); + invalidSqlErrMsg(pQueryInfo->msg, msg); return ret; } @@ -3332,16 +3334,16 @@ static int32_t setTableCondForMetricQuery(SSqlObj* pSql, tSQLExpr* pExpr, int16_ } char* str = taosStringBuilderGetResult(&sb1, NULL); - pCmd->pQueryInfo[0].tagCond.tbnameCond.cond = strdup(str); + pQueryInfo->tagCond.tbnameCond.cond = strdup(str); taosStringBuilderDestroy(&sb1); tfree(segments); return TSDB_CODE_SUCCESS; } -static bool validateFilterExpr(SSqlCmd* pCmd) { - for (int32_t i = 0; i < pCmd->pQueryInfo[0].colList.numOfCols; ++i) { - SColumnBase* pColBase = &pCmd->pQueryInfo[0].colList.pColList[i]; +static bool validateFilterExpr(SQueryInfo* pQueryInfo) { + for (int32_t i = 0; i < pQueryInfo->colList.numOfCols; ++i) { + SColumnBase* pColBase = &pQueryInfo->colList.pColList[i]; for (int32_t j = 0; j < pColBase->numOfFilters; ++j) { SColumnFilterInfo* pColFilter = &pColBase->filterInfo[j]; @@ -3363,7 +3365,7 @@ static bool validateFilterExpr(SSqlCmd* pCmd) { return true; } -static int32_t getTimeRangeFromExpr(SSqlCmd* pCmd, tSQLExpr* pExpr) { +static int32_t getTimeRangeFromExpr(SQueryInfo* pQueryInfo, tSQLExpr* pExpr) { const char* msg0 = "invalid timestamp"; const char* msg1 = "only one time stamp window allowed"; @@ -3373,19 +3375,19 @@ static int32_t getTimeRangeFromExpr(SSqlCmd* pCmd, tSQLExpr* pExpr) { if (!isExprDirectParentOfLeaftNode(pExpr)) { if (pExpr->nSQLOptr == TK_OR) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(pQueryInfo->msg, msg1); } - getTimeRangeFromExpr(pCmd, pExpr->pLeft); + getTimeRangeFromExpr(pQueryInfo, pExpr->pLeft); - return getTimeRangeFromExpr(pCmd, pExpr->pRight); + return getTimeRangeFromExpr(pQueryInfo, pExpr->pRight); } else { SColumnIndex index = COLUMN_INDEX_INITIALIZER; - if (getColumnIndexByNameEx(&pExpr->pLeft->colInfo, pCmd, &index) != TSDB_CODE_SUCCESS) { + if (getColumnIndexByNameEx(&pExpr->pLeft->colInfo, pQueryInfo, &index) != TSDB_CODE_SUCCESS) { return TSDB_CODE_INVALID_SQL; } - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, index.tableIndex); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, index.tableIndex); SMeterMeta* pMeterMeta = pMeterMetaInfo->pMeterMeta; tSQLExpr* pRight = pExpr->pRight; @@ -3394,45 +3396,45 @@ static int32_t getTimeRangeFromExpr(SSqlCmd* pCmd, tSQLExpr* pExpr) { TSKEY etime = INT64_MAX; if (getTimeRange(&stime, &etime, pRight, pExpr->nSQLOptr, pMeterMeta->precision) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg0); + return invalidSqlErrMsg(pQueryInfo->msg, msg0); } // update the timestamp query range - if (pCmd->pQueryInfo[0].stime < stime) { - pCmd->pQueryInfo[0].stime = stime; + if (pQueryInfo->stime < stime) { + pQueryInfo->stime = stime; } - if (pCmd->pQueryInfo[0].etime > etime) { - pCmd->pQueryInfo[0].etime = etime; + if (pQueryInfo->etime > etime) { + pQueryInfo->etime = etime; } } return TSDB_CODE_SUCCESS; } -static int32_t validateJoinExpr(SSqlCmd* pCmd, SCondExpr* pCondExpr) { +static int32_t validateJoinExpr(SQueryInfo* pQueryInfo, SCondExpr* pCondExpr) { const char* msg1 = "super table join requires tags column"; const char* msg2 = "timestamp join condition missing"; const char* msg3 = "condition missing for join query"; - if (!QUERY_IS_JOIN_QUERY(pCmd->type)) { - if (pCmd->pQueryInfo->numOfTables == 1) { + if (!QUERY_IS_JOIN_QUERY(pQueryInfo->type)) { + if (pQueryInfo->numOfTables == 1) { return TSDB_CODE_SUCCESS; } else { - return invalidSqlErrMsg(pCmd, msg3); + return invalidSqlErrMsg(pQueryInfo->msg, msg3); } } - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); - if (UTIL_METER_IS_METRIC(pMeterMetaInfo)) { // for stable join, tag columns + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); + if (UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo)) { // for stable join, tag columns // must be present for join if (pCondExpr->pJoinExpr == NULL) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(pQueryInfo->msg, msg1); } } if (!pCondExpr->tsJoin) { - return invalidSqlErrMsg(pCmd, msg2); + return invalidSqlErrMsg(pQueryInfo->msg, msg2); } return TSDB_CODE_SUCCESS; @@ -3460,42 +3462,42 @@ static void cleanQueryExpr(SCondExpr* pCondExpr) { } } -static void doAddJoinTagsColumnsIntoTagList(SSqlCmd* pCmd, SCondExpr* pCondExpr) { - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); - if (QUERY_IS_JOIN_QUERY(pCmd->type) && UTIL_METER_IS_METRIC(pMeterMetaInfo)) { +static void doAddJoinTagsColumnsIntoTagList(SQueryInfo* pQueryInfo, SCondExpr* pCondExpr) { + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); + if (QUERY_IS_JOIN_QUERY(pQueryInfo->type) && UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo)) { SColumnIndex index = {0}; - getColumnIndexByNameEx(&pCondExpr->pJoinExpr->pLeft->colInfo, pCmd, &index); - pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, index.tableIndex); + getColumnIndexByNameEx(&pCondExpr->pJoinExpr->pLeft->colInfo, pQueryInfo, &index); + pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, index.tableIndex); int32_t columnInfo = index.columnIndex - pMeterMetaInfo->pMeterMeta->numOfColumns; - addRequiredTagColumn(pCmd, columnInfo, index.tableIndex); + addRequiredTagColumn(pQueryInfo, columnInfo, index.tableIndex); - getColumnIndexByNameEx(&pCondExpr->pJoinExpr->pRight->colInfo, pCmd, &index); - pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, index.tableIndex); + getColumnIndexByNameEx(&pCondExpr->pJoinExpr->pRight->colInfo, pQueryInfo, &index); + pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, index.tableIndex); columnInfo = index.columnIndex - pMeterMetaInfo->pMeterMeta->numOfColumns; - addRequiredTagColumn(pCmd, columnInfo, index.tableIndex); + addRequiredTagColumn(pQueryInfo, columnInfo, index.tableIndex); } } -static int32_t getTagQueryCondExpr(SSqlCmd* pCmd, SCondExpr* pCondExpr, tSQLExpr** pExpr) { +static int32_t getTagQueryCondExpr(SQueryInfo* pQueryInfo, SCondExpr* pCondExpr, tSQLExpr** pExpr) { int32_t ret = TSDB_CODE_SUCCESS; if (pCondExpr->pTagCond != NULL) { - for (int32_t i = 0; i < pCmd->pQueryInfo->numOfTables; ++i) { - tSQLExpr* p1 = extractExprForSTable(pExpr, pCmd, i); + for (int32_t i = 0; i < pQueryInfo->numOfTables; ++i) { + tSQLExpr* p1 = extractExprForSTable(pExpr, pQueryInfo, i); - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, i); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, i); char c[TSDB_MAX_TAGS_LEN] = {0}; char* str = c; - if ((ret = getTagCondString(pCmd, p1, &str)) != TSDB_CODE_SUCCESS) { + if ((ret = getTagCondString(p1, &str)) != TSDB_CODE_SUCCESS) { return ret; } - tsSetMetricQueryCond(&pCmd->pQueryInfo[0].tagCond, pMeterMetaInfo->pMeterMeta->uid, c); + tsSetMetricQueryCond(&pQueryInfo->tagCond, pMeterMetaInfo->pMeterMeta->uid, c); doCompactQueryExpr(pExpr); tSQLExprDestroy(p1); @@ -3517,19 +3519,20 @@ int32_t parseWhereClause(SSqlObj* pSql, tSQLExpr** pExpr) { int32_t ret = TSDB_CODE_SUCCESS; SSqlCmd* pCmd = &pSql->cmd; - pCmd->pQueryInfo[0].stime = 0; - pCmd->pQueryInfo[0].etime = INT64_MAX; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + pQueryInfo->stime = 0; + pQueryInfo->etime = INT64_MAX; // tags query condition may be larger than 512bytes, therefore, we need to prepare enough large space SStringBuilder sb = {0}; SCondExpr condExpr = {0}; if ((*pExpr)->pLeft == NULL || (*pExpr)->pRight == NULL) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(pQueryInfo->msg, msg1); } int32_t type = 0; - if ((ret = getQueryCondExpr(pCmd, pExpr, &condExpr, &type, (*pExpr)->nSQLOptr)) != TSDB_CODE_SUCCESS) { + if ((ret = getQueryCondExpr(pQueryInfo, pExpr, &condExpr, &type, (*pExpr)->nSQLOptr)) != TSDB_CODE_SUCCESS) { return ret; } @@ -3539,46 +3542,46 @@ int32_t parseWhereClause(SSqlObj* pSql, tSQLExpr** pExpr) { condExpr.pTagCond = (*pExpr); // 1. check if it is a join query - if ((ret = validateJoinExpr(pCmd, &condExpr)) != TSDB_CODE_SUCCESS) { + if ((ret = validateJoinExpr(pQueryInfo, &condExpr)) != TSDB_CODE_SUCCESS) { return ret; } // 2. get the query time range - if ((ret = getTimeRangeFromExpr(pCmd, condExpr.pTimewindow)) != TSDB_CODE_SUCCESS) { + if ((ret = getTimeRangeFromExpr(pQueryInfo, condExpr.pTimewindow)) != TSDB_CODE_SUCCESS) { return ret; } // 3. get the tag query condition - if ((ret = getTagQueryCondExpr(pCmd, &condExpr, pExpr)) != TSDB_CODE_SUCCESS) { + if ((ret = getTagQueryCondExpr(pQueryInfo, &condExpr, pExpr)) != TSDB_CODE_SUCCESS) { return ret; } // 4. get the table name query condition - if ((ret = getTablenameCond(pCmd, condExpr.pTableCond, &sb)) != TSDB_CODE_SUCCESS) { + if ((ret = getTablenameCond(pQueryInfo, condExpr.pTableCond, &sb)) != TSDB_CODE_SUCCESS) { return ret; } // 5. other column query condition - if ((ret = getColumnQueryCondInfo(pCmd, condExpr.pColumnCond, TK_AND)) != TSDB_CODE_SUCCESS) { + if ((ret = getColumnQueryCondInfo(pQueryInfo, condExpr.pColumnCond, TK_AND)) != TSDB_CODE_SUCCESS) { return ret; } // 6. join condition - if ((ret = getJoinCondInfo(pSql, condExpr.pJoinExpr)) != TSDB_CODE_SUCCESS) { + if ((ret = getJoinCondInfo(pQueryInfo, condExpr.pJoinExpr)) != TSDB_CODE_SUCCESS) { return ret; } // 7. query condition for table name - pCmd->pQueryInfo[0].tagCond.relType = (condExpr.relType == TK_AND) ? TSDB_RELATION_AND : TSDB_RELATION_OR; + pQueryInfo->tagCond.relType = (condExpr.relType == TK_AND) ? TSDB_RELATION_AND : TSDB_RELATION_OR; - ret = setTableCondForMetricQuery(pSql, condExpr.pTableCond, condExpr.tableCondIndex, &sb); + ret = setTableCondForMetricQuery(pQueryInfo, getAccountId(pSql), condExpr.pTableCond, condExpr.tableCondIndex, &sb); taosStringBuilderDestroy(&sb); - if (!validateFilterExpr(pCmd)) { - return invalidSqlErrMsg(pCmd, msg); + if (!validateFilterExpr(pQueryInfo)) { + return invalidSqlErrMsg(pQueryInfo->msg, msg); } - doAddJoinTagsColumnsIntoTagList(pCmd, &condExpr); + doAddJoinTagsColumnsIntoTagList(pQueryInfo, &condExpr); cleanQueryExpr(&condExpr); return ret; @@ -3673,11 +3676,11 @@ int32_t getTimeRange(int64_t* stime, int64_t* etime, tSQLExpr* pRight, int32_t o return TSDB_CODE_SUCCESS; } -int32_t tsRewriteFieldNameIfNecessary(SSqlCmd* pCmd) { +int32_t tsRewriteFieldNameIfNecessary(SQueryInfo* pQueryInfo) { const char rep[] = {'(', ')', '*', ',', '.', '/', '\\', '+', '-', '%', ' '}; - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - char* fieldName = tscFieldInfoGetField(pCmd, i)->name; + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + char* fieldName = tscFieldInfoGetField(pQueryInfo, i)->name; for (int32_t j = 0; j < TSDB_COL_NAME_LEN && fieldName[j] != 0; ++j) { for (int32_t k = 0; k < tListLen(rep); ++k) { if (fieldName[j] == rep[k]) { @@ -3691,12 +3694,12 @@ int32_t tsRewriteFieldNameIfNecessary(SSqlCmd* pCmd) { } // the column name may be identical, here check again - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - char* fieldName = tscFieldInfoGetField(pCmd, i)->name; - for (int32_t j = i + 1; j < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++j) { - if (strncasecmp(fieldName, tscFieldInfoGetField(pCmd, j)->name, TSDB_COL_NAME_LEN) == 0) { + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + char* fieldName = tscFieldInfoGetField(pQueryInfo, i)->name; + for (int32_t j = i + 1; j < pQueryInfo->fieldsInfo.numOfOutputCols; ++j) { + if (strncasecmp(fieldName, tscFieldInfoGetField(pQueryInfo, j)->name, TSDB_COL_NAME_LEN) == 0) { const char* msg = "duplicated column name in new table"; - return invalidSqlErrMsg(pCmd, msg); + return invalidSqlErrMsg(pQueryInfo->msg, msg); } } } @@ -3704,7 +3707,7 @@ int32_t tsRewriteFieldNameIfNecessary(SSqlCmd* pCmd) { return TSDB_CODE_SUCCESS; } -int32_t parseFillClause(SSqlCmd* pCmd, SQuerySQL* pQuerySQL) { +int32_t parseFillClause(SQueryInfo* pQueryInfo, SQuerySQL* pQuerySQL) { tVariantList* pFillToken = pQuerySQL->fillType; tVariantListItem* pItem = &pFillToken->a[0]; @@ -3714,105 +3717,106 @@ int32_t parseFillClause(SSqlCmd* pCmd, SQuerySQL* pQuerySQL) { const char* msg2 = "invalid fill option"; if (pItem->pVar.nType != TSDB_DATA_TYPE_BINARY) { - return invalidSqlErrMsg(pCmd, msg2); + return invalidSqlErrMsg(pQueryInfo->msg, msg2); } if (strncasecmp(pItem->pVar.pz, "none", 4) == 0 && pItem->pVar.nLen == 4) { - pCmd->pQueryInfo[0].interpoType = TSDB_INTERPO_NONE; + pQueryInfo->interpoType = TSDB_INTERPO_NONE; } else if (strncasecmp(pItem->pVar.pz, "null", 4) == 0 && pItem->pVar.nLen == 4) { - pCmd->pQueryInfo[0].interpoType = TSDB_INTERPO_NULL; - for (int32_t i = START_INTERPO_COL_IDX; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - TAOS_FIELD* pFields = tscFieldInfoGetField(pCmd, i); - setNull((char*)&pCmd->pQueryInfo[0].defaultVal[i], pFields->type, pFields->bytes); + pQueryInfo->interpoType = TSDB_INTERPO_NULL; + for (int32_t i = START_INTERPO_COL_IDX; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + TAOS_FIELD* pFields = tscFieldInfoGetField(pQueryInfo, i); + setNull((char*)&pQueryInfo->defaultVal[i], pFields->type, pFields->bytes); } } else if (strncasecmp(pItem->pVar.pz, "prev", 4) == 0 && pItem->pVar.nLen == 4) { - pCmd->pQueryInfo[0].interpoType = TSDB_INTERPO_PREV; + pQueryInfo->interpoType = TSDB_INTERPO_PREV; } else if (strncasecmp(pItem->pVar.pz, "linear", 6) == 0 && pItem->pVar.nLen == 6) { // not support yet - pCmd->pQueryInfo[0].interpoType = TSDB_INTERPO_LINEAR; + pQueryInfo->interpoType = TSDB_INTERPO_LINEAR; } else if (strncasecmp(pItem->pVar.pz, "value", 5) == 0 && pItem->pVar.nLen == 5) { - pCmd->pQueryInfo[0].interpoType = TSDB_INTERPO_SET_VALUE; + pQueryInfo->interpoType = TSDB_INTERPO_SET_VALUE; if (pFillToken->nExpr == 1) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(pQueryInfo->msg, msg1); } int32_t startPos = 1; int32_t numOfFillVal = pFillToken->nExpr - 1; /* for point interpolation query, we do not have the timestamp column */ - if (tscIsPointInterpQuery(pCmd)) { + if (tscIsPointInterpQuery(pQueryInfo)) { startPos = 0; - if (numOfFillVal > pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols) { - numOfFillVal = pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; + if (numOfFillVal > pQueryInfo->fieldsInfo.numOfOutputCols) { + numOfFillVal = pQueryInfo->fieldsInfo.numOfOutputCols; } } else { - numOfFillVal = - (pFillToken->nExpr > pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols) ? pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols : pFillToken->nExpr; + numOfFillVal = (pFillToken->nExpr > pQueryInfo->fieldsInfo.numOfOutputCols) + ? pQueryInfo->fieldsInfo.numOfOutputCols + : pFillToken->nExpr; } int32_t j = 1; for (int32_t i = startPos; i < numOfFillVal; ++i, ++j) { - TAOS_FIELD* pFields = tscFieldInfoGetField(pCmd, i); + TAOS_FIELD* pFields = tscFieldInfoGetField(pQueryInfo, i); - int32_t ret = tVariantDump(&pFillToken->a[j].pVar, (char*)&pCmd->pQueryInfo[0].defaultVal[i], pFields->type); + int32_t ret = tVariantDump(&pFillToken->a[j].pVar, (char*)&pQueryInfo->defaultVal[i], pFields->type); if (ret != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg); + return invalidSqlErrMsg(pQueryInfo->msg, msg); } if (pFields->type == TSDB_DATA_TYPE_BINARY || pFields->type == TSDB_DATA_TYPE_NCHAR) { - setNull((char*)(&pCmd->pQueryInfo[0].defaultVal[i]), pFields->type, pFields->bytes); + setNull((char*)(&pQueryInfo->defaultVal[i]), pFields->type, pFields->bytes); } } - if ((pFillToken->nExpr < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols) || - ((pFillToken->nExpr - 1 < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols) && (tscIsPointInterpQuery(pCmd)))) { + if ((pFillToken->nExpr < pQueryInfo->fieldsInfo.numOfOutputCols) || + ((pFillToken->nExpr - 1 < pQueryInfo->fieldsInfo.numOfOutputCols) && (tscIsPointInterpQuery(pQueryInfo)))) { tVariantListItem* lastItem = &pFillToken->a[pFillToken->nExpr - 1]; - for (int32_t i = numOfFillVal; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - TAOS_FIELD* pFields = tscFieldInfoGetField(pCmd, i); - tVariantDump(&lastItem->pVar, (char*)&pCmd->pQueryInfo[0].defaultVal[i], pFields->type); + for (int32_t i = numOfFillVal; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + TAOS_FIELD* pFields = tscFieldInfoGetField(pQueryInfo, i); + tVariantDump(&lastItem->pVar, (char*)&pQueryInfo->defaultVal[i], pFields->type); if (pFields->type == TSDB_DATA_TYPE_BINARY || pFields->type == TSDB_DATA_TYPE_NCHAR) { - setNull((char*)(&pCmd->pQueryInfo[0].defaultVal[i]), pFields->type, pFields->bytes); + setNull((char*)(&pQueryInfo->defaultVal[i]), pFields->type, pFields->bytes); } } } } else { - return invalidSqlErrMsg(pCmd, msg2); + return invalidSqlErrMsg(pQueryInfo->msg, msg2); } return TSDB_CODE_SUCCESS; } -static void setDefaultOrderInfo(SSqlCmd* pCmd) { +static void setDefaultOrderInfo(SQueryInfo* pQueryInfo) { /* set default timestamp order information for all queries */ - pCmd->order.order = TSQL_SO_ASC; - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + pQueryInfo->order.order = TSQL_SO_ASC; + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); - if (isTopBottomQuery(pCmd)) { - pCmd->order.order = TSQL_SO_ASC; - pCmd->order.orderColId = PRIMARYKEY_TIMESTAMP_COL_INDEX; + if (isTopBottomQuery(pQueryInfo)) { + pQueryInfo->order.order = TSQL_SO_ASC; + pQueryInfo->order.orderColId = PRIMARYKEY_TIMESTAMP_COL_INDEX; } else { - pCmd->order.orderColId = -1; + pQueryInfo->order.orderColId = -1; } /* for metric query, set default ascending order for group output */ - if (UTIL_METER_IS_METRIC(pMeterMetaInfo)) { - pCmd->pQueryInfo[0].groupbyExpr.orderType = TSQL_SO_ASC; + if (UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo)) { + pQueryInfo->groupbyExpr.orderType = TSQL_SO_ASC; } } -int32_t parseOrderbyClause(SSqlCmd* pCmd, SQuerySQL* pQuerySql, SSchema* pSchema, int32_t numOfCols) { +int32_t parseOrderbyClause(SQueryInfo* pQueryInfo, SQuerySQL* pQuerySql, SSchema* pSchema, int32_t numOfCols) { const char* msg0 = "only support order by primary timestamp"; const char* msg1 = "invalid column name"; const char* msg2 = "only support order by primary timestamp and queried column"; const char* msg3 = "only support order by primary timestamp and first tag in groupby clause"; - setDefaultOrderInfo(pCmd); - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + setDefaultOrderInfo(pQueryInfo); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); if (pQuerySql->pSortOrder == NULL) { return TSDB_CODE_SUCCESS; @@ -3828,11 +3832,11 @@ int32_t parseOrderbyClause(SSqlCmd* pCmd, SQuerySQL* pQuerySql, SSchema* pSchema */ if (UTIL_METER_IS_NOMRAL_METER(pMeterMetaInfo)) { if (pSortorder->nExpr > 1) { - return invalidSqlErrMsg(pCmd, msg0); + return invalidSqlErrMsg(pQueryInfo->msg, msg0); } } else { if (pSortorder->nExpr > 2) { - return invalidSqlErrMsg(pCmd, msg3); + return invalidSqlErrMsg(pQueryInfo->msg, msg3); } } @@ -3847,9 +3851,9 @@ int32_t parseOrderbyClause(SSqlCmd* pCmd, SQuerySQL* pQuerySql, SSchema* pSchema SSQLToken columnName = {pVar->nLen, pVar->nType, pVar->pz}; SColumnIndex index = {0}; - if (UTIL_METER_IS_METRIC(pMeterMetaInfo)) { // metric query - if (getColumnIndexByNameEx(&columnName, pCmd, &index) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg1); + if (UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo)) { // metric query + if (getColumnIndexByNameEx(&columnName, pQueryInfo, &index) != TSDB_CODE_SUCCESS) { + return invalidSqlErrMsg(pQueryInfo->msg, msg1); } bool orderByTags = false; @@ -3857,7 +3861,7 @@ int32_t parseOrderbyClause(SSqlCmd* pCmd, SQuerySQL* pQuerySql, SSchema* pSchema if (index.columnIndex >= pMeterMetaInfo->pMeterMeta->numOfColumns) { int32_t relTagIndex = index.columnIndex - pMeterMetaInfo->pMeterMeta->numOfColumns; - if (relTagIndex == pCmd->pQueryInfo[0].groupbyExpr.columnInfo[0].colIdx) { + if (relTagIndex == pQueryInfo->groupbyExpr.columnInfo[0].colIdx) { orderByTags = true; } } else if (index.columnIndex == TSDB_TBNAME_COLUMN_INDEX) { @@ -3868,83 +3872,83 @@ int32_t parseOrderbyClause(SSqlCmd* pCmd, SQuerySQL* pQuerySql, SSchema* pSchema orderByTS = true; } - if (!(orderByTags || orderByTS) && !isTopBottomQuery(pCmd)) { - return invalidSqlErrMsg(pCmd, msg3); + if (!(orderByTags || orderByTS) && !isTopBottomQuery(pQueryInfo)) { + return invalidSqlErrMsg(pQueryInfo->msg, msg3); } else { assert(!(orderByTags && orderByTS)); } if (pSortorder->nExpr == 1) { if (orderByTags) { - pCmd->pQueryInfo[0].groupbyExpr.orderIndex = index.columnIndex - pMeterMetaInfo->pMeterMeta->numOfColumns; - pCmd->pQueryInfo[0].groupbyExpr.orderType = pQuerySql->pSortOrder->a[0].sortOrder; - } else if (isTopBottomQuery(pCmd)) { + pQueryInfo->groupbyExpr.orderIndex = index.columnIndex - pMeterMetaInfo->pMeterMeta->numOfColumns; + pQueryInfo->groupbyExpr.orderType = pQuerySql->pSortOrder->a[0].sortOrder; + } else if (isTopBottomQuery(pQueryInfo)) { /* order of top/bottom query in interval is not valid */ - SSqlExpr* pExpr = tscSqlExprGet(pCmd, 0); + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, 0); assert(pExpr->functionId == TSDB_FUNC_TS); - pExpr = tscSqlExprGet(pCmd, 1); + pExpr = tscSqlExprGet(pQueryInfo, 1); if (pExpr->colInfo.colIdx != index.columnIndex && index.columnIndex != PRIMARYKEY_TIMESTAMP_COL_INDEX) { - return invalidSqlErrMsg(pCmd, msg2); + return invalidSqlErrMsg(pQueryInfo->msg, msg2); } - - pCmd->order.order = pQuerySql->pSortOrder->a[0].sortOrder; - pCmd->order.orderColId = pSchema[index.columnIndex].colId; + + pQueryInfo->order.order = pQuerySql->pSortOrder->a[0].sortOrder; + pQueryInfo->order.orderColId = pSchema[index.columnIndex].colId; return TSDB_CODE_SUCCESS; } else { - pCmd->order.order = pSortorder->a[0].sortOrder; - pCmd->order.orderColId = PRIMARYKEY_TIMESTAMP_COL_INDEX; + pQueryInfo->order.order = pSortorder->a[0].sortOrder; + pQueryInfo->order.orderColId = PRIMARYKEY_TIMESTAMP_COL_INDEX; } } if (pSortorder->nExpr == 2) { if (orderByTags) { - pCmd->pQueryInfo[0].groupbyExpr.orderIndex = index.columnIndex - pMeterMetaInfo->pMeterMeta->numOfColumns; - pCmd->pQueryInfo[0].groupbyExpr.orderType = pQuerySql->pSortOrder->a[0].sortOrder; + pQueryInfo->groupbyExpr.orderIndex = index.columnIndex - pMeterMetaInfo->pMeterMeta->numOfColumns; + pQueryInfo->groupbyExpr.orderType = pQuerySql->pSortOrder->a[0].sortOrder; } else { - pCmd->order.order = pSortorder->a[0].sortOrder; - pCmd->order.orderColId = PRIMARYKEY_TIMESTAMP_COL_INDEX; + pQueryInfo->order.order = pSortorder->a[0].sortOrder; + pQueryInfo->order.orderColId = PRIMARYKEY_TIMESTAMP_COL_INDEX; } tVariant* pVar2 = &pSortorder->a[1].pVar; SSQLToken cname = {pVar2->nLen, pVar2->nType, pVar2->pz}; - if (getColumnIndexByNameEx(&cname, pCmd, &index) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg1); + if (getColumnIndexByNameEx(&cname, pQueryInfo, &index) != TSDB_CODE_SUCCESS) { + return invalidSqlErrMsg(pQueryInfo->msg, msg1); } if (index.columnIndex != PRIMARYKEY_TIMESTAMP_COL_INDEX) { - return invalidSqlErrMsg(pCmd, msg2); + return invalidSqlErrMsg(pQueryInfo->msg, msg2); } else { - pCmd->order.order = pSortorder->a[1].sortOrder; - pCmd->order.orderColId = PRIMARYKEY_TIMESTAMP_COL_INDEX; + pQueryInfo->order.order = pSortorder->a[1].sortOrder; + pQueryInfo->order.orderColId = PRIMARYKEY_TIMESTAMP_COL_INDEX; } } } else { // meter query - if (getColumnIndexByNameEx(&columnName, pCmd, &index) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg1); + if (getColumnIndexByNameEx(&columnName, pQueryInfo, &index) != TSDB_CODE_SUCCESS) { + return invalidSqlErrMsg(pQueryInfo->msg, msg1); } - if (index.columnIndex != PRIMARYKEY_TIMESTAMP_COL_INDEX && !isTopBottomQuery(pCmd)) { - return invalidSqlErrMsg(pCmd, msg2); + if (index.columnIndex != PRIMARYKEY_TIMESTAMP_COL_INDEX && !isTopBottomQuery(pQueryInfo)) { + return invalidSqlErrMsg(pQueryInfo->msg, msg2); } - if (isTopBottomQuery(pCmd)) { + if (isTopBottomQuery(pQueryInfo)) { /* order of top/bottom query in interval is not valid */ - SSqlExpr* pExpr = tscSqlExprGet(pCmd, 0); + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, 0); assert(pExpr->functionId == TSDB_FUNC_TS); - pExpr = tscSqlExprGet(pCmd, 1); + pExpr = tscSqlExprGet(pQueryInfo, 1); if (pExpr->colInfo.colIdx != index.columnIndex && index.columnIndex != PRIMARYKEY_TIMESTAMP_COL_INDEX) { - return invalidSqlErrMsg(pCmd, msg2); + return invalidSqlErrMsg(pQueryInfo->msg, msg2); } - - pCmd->order.order = pQuerySql->pSortOrder->a[0].sortOrder; - pCmd->order.orderColId = pSchema[index.columnIndex].colId; + + pQueryInfo->order.order = pQuerySql->pSortOrder->a[0].sortOrder; + pQueryInfo->order.orderColId = pSchema[index.columnIndex].colId; return TSDB_CODE_SUCCESS; } - - pCmd->order.order = pQuerySql->pSortOrder->a[0].sortOrder; + + pQueryInfo->order.order = pQuerySql->pSortOrder->a[0].sortOrder; } return TSDB_CODE_SUCCESS; @@ -3952,7 +3956,7 @@ int32_t parseOrderbyClause(SSqlCmd* pCmd, SQuerySQL* pQuerySql, SSchema* pSchema int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { const int32_t DEFAULT_TABLE_INDEX = 0; - + const char* msg1 = "invalid table name"; const char* msg2 = "table name too long"; const char* msg3 = "manipulation of tag available for super table"; @@ -3962,15 +3966,16 @@ int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { SSqlCmd* pCmd = &pSql->cmd; SAlterTableSQL* pAlterSQL = pInfo->pAlterInfo; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, DEFAULT_TABLE_INDEX); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, DEFAULT_TABLE_INDEX); if (tscValidateName(&(pAlterSQL->name)) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(pQueryInfo->msg, msg1); } - if (setMeterID(pSql, &(pAlterSQL->name), 0) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg2); + if (setMeterID(pSql, 0, &(pAlterSQL->name), 0) != TSDB_CODE_SUCCESS) { + return invalidSqlErrMsg(pQueryInfo->msg, msg2); } int32_t ret = tscGetMeterMeta(pSql, pMeterMetaInfo->name, DEFAULT_TABLE_INDEX); @@ -3983,28 +3988,27 @@ int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { if (pAlterSQL->type == TSDB_ALTER_TABLE_ADD_TAG_COLUMN || pAlterSQL->type == TSDB_ALTER_TABLE_DROP_TAG_COLUMN || pAlterSQL->type == TSDB_ALTER_TABLE_CHANGE_TAG_COLUMN) { if (UTIL_METER_IS_NOMRAL_METER(pMeterMetaInfo)) { - return invalidSqlErrMsg(pCmd, msg3); + return invalidSqlErrMsg(pQueryInfo->msg, msg3); } - } else if ((pAlterSQL->type == TSDB_ALTER_TABLE_UPDATE_TAG_VAL) && (UTIL_METER_IS_METRIC(pMeterMetaInfo))) { - return invalidSqlErrMsg(pCmd, msg4); + } else if ((pAlterSQL->type == TSDB_ALTER_TABLE_UPDATE_TAG_VAL) && (UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo))) { + return invalidSqlErrMsg(pQueryInfo->msg, msg4); } else if ((pAlterSQL->type == TSDB_ALTER_TABLE_ADD_COLUMN || pAlterSQL->type == TSDB_ALTER_TABLE_DROP_COLUMN) && UTIL_METER_IS_CREATE_FROM_METRIC(pMeterMetaInfo)) { - return invalidSqlErrMsg(pCmd, msg6); + return invalidSqlErrMsg(pQueryInfo->msg, msg6); } if (pAlterSQL->type == TSDB_ALTER_TABLE_ADD_TAG_COLUMN) { tFieldList* pFieldList = pAlterSQL->pAddColumns; if (pFieldList->nField > 1) { - return invalidSqlErrMsg(pCmd, msg5); + return invalidSqlErrMsg(pQueryInfo->msg, msg5); } if (!validateOneTags(pCmd, &pFieldList->p[0])) { return TSDB_CODE_INVALID_SQL; } - tscFieldInfoSetValFromField(&pCmd->pQueryInfo[0].fieldsInfo, 0, &pFieldList->p[0]); + tscFieldInfoSetValFromField(&pQueryInfo->fieldsInfo, 0, &pFieldList->p[0]); } else if (pAlterSQL->type == TSDB_ALTER_TABLE_DROP_TAG_COLUMN) { - const char* msg1 = "no tags can be dropped"; const char* msg2 = "only support one tag"; const char* msg3 = "tag name too long"; @@ -4012,36 +4016,36 @@ int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { const char* msg5 = "primary tag cannot be dropped"; if (pMeterMeta->numOfTags == 1) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(pQueryInfo->msg, msg1); } // numOfTags == 1 if (pAlterSQL->varList->nExpr > 1) { - return invalidSqlErrMsg(pCmd, msg2); + return invalidSqlErrMsg(pQueryInfo->msg, msg2); } tVariantListItem* pItem = &pAlterSQL->varList->a[0]; if (pItem->pVar.nLen > TSDB_COL_NAME_LEN) { - return invalidSqlErrMsg(pCmd, msg3); + return invalidSqlErrMsg(pQueryInfo->msg, msg3); } - + SColumnIndex index = COLUMN_INDEX_INITIALIZER; - SSQLToken name = {.z = pItem->pVar.pz, .n = pItem->pVar.nLen, .type = TK_STRING}; - - if (getColumnIndexByNameEx(&name, pCmd, &index) != TSDB_CODE_SUCCESS) { + SSQLToken name = {.z = pItem->pVar.pz, .n = pItem->pVar.nLen, .type = TK_STRING}; + + if (getColumnIndexByNameEx(&name, pQueryInfo, &index) != TSDB_CODE_SUCCESS) { return TSDB_CODE_INVALID_SQL; } if (index.columnIndex < pMeterMeta->numOfColumns) { - return invalidSqlErrMsg(pCmd, msg4); + return invalidSqlErrMsg(pQueryInfo->msg, msg4); } else if (index.columnIndex == 0) { - return invalidSqlErrMsg(pCmd, msg5); + return invalidSqlErrMsg(pQueryInfo->msg, msg5); } char name1[128] = {0}; strncpy(name1, pItem->pVar.pz, pItem->pVar.nLen); - tscFieldInfoSetValue(&pCmd->pQueryInfo[0].fieldsInfo, 0, TSDB_DATA_TYPE_INT, name1, - tDataTypeDesc[TSDB_DATA_TYPE_INT].nSize); + tscFieldInfoSetValue(&pQueryInfo->fieldsInfo, 0, TSDB_DATA_TYPE_INT, name1, + tDataTypeDesc[TSDB_DATA_TYPE_INT].nSize); } else if (pAlterSQL->type == TSDB_ALTER_TABLE_CHANGE_TAG_COLUMN) { const char* msg1 = "tag name too long"; const char* msg2 = "invalid tag name"; @@ -4055,33 +4059,35 @@ int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { tVariantListItem* pDstItem = &pAlterSQL->varList->a[1]; if (pSrcItem->pVar.nLen >= TSDB_COL_NAME_LEN || pDstItem->pVar.nLen >= TSDB_COL_NAME_LEN) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(pQueryInfo->msg, msg1); } if (pSrcItem->pVar.nType != TSDB_DATA_TYPE_BINARY || pDstItem->pVar.nType != TSDB_DATA_TYPE_BINARY) { - return invalidSqlErrMsg(pCmd, msg2); + return invalidSqlErrMsg(pQueryInfo->msg, msg2); } SColumnIndex srcIndex = COLUMN_INDEX_INITIALIZER; SColumnIndex destIndex = COLUMN_INDEX_INITIALIZER; SSQLToken srcToken = {.z = pSrcItem->pVar.pz, .n = pSrcItem->pVar.nLen, .type = TK_STRING}; - if (getColumnIndexByNameEx(&srcToken, pCmd, &srcIndex) != TSDB_CODE_SUCCESS) { + if (getColumnIndexByNameEx(&srcToken, pQueryInfo, &srcIndex) != TSDB_CODE_SUCCESS) { return TSDB_CODE_INVALID_SQL; } SSQLToken destToken = {.z = pDstItem->pVar.pz, .n = pDstItem->pVar.nLen, .type = TK_STRING}; - if (getColumnIndexByNameEx(&destToken, pCmd, &destIndex) == TSDB_CODE_SUCCESS) { + if (getColumnIndexByNameEx(&destToken, pQueryInfo, &destIndex) == TSDB_CODE_SUCCESS) { return TSDB_CODE_INVALID_SQL; } char name[128] = {0}; strncpy(name, pVarList->a[0].pVar.pz, pVarList->a[0].pVar.nLen); - tscFieldInfoSetValue(&pCmd->pQueryInfo[0].fieldsInfo, 0, TSDB_DATA_TYPE_INT, name, tDataTypeDesc[TSDB_DATA_TYPE_INT].nSize); + tscFieldInfoSetValue(&pQueryInfo->fieldsInfo, 0, TSDB_DATA_TYPE_INT, name, + tDataTypeDesc[TSDB_DATA_TYPE_INT].nSize); memset(name, 0, tListLen(name)); strncpy(name, pVarList->a[1].pVar.pz, pVarList->a[1].pVar.nLen); - tscFieldInfoSetValue(&pCmd->pQueryInfo[0].fieldsInfo, 1, TSDB_DATA_TYPE_INT, name, tDataTypeDesc[TSDB_DATA_TYPE_INT].nSize); + tscFieldInfoSetValue(&pQueryInfo->fieldsInfo, 1, TSDB_DATA_TYPE_INT, name, + tDataTypeDesc[TSDB_DATA_TYPE_INT].nSize); } else if (pAlterSQL->type == TSDB_ALTER_TABLE_UPDATE_TAG_VAL) { const char* msg1 = "invalid tag value"; const char* msg2 = "update normal column not supported"; @@ -4094,41 +4100,43 @@ int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { SColumnIndex columnIndex = COLUMN_INDEX_INITIALIZER; SSQLToken name = {.type = TK_STRING, .z = pTagName->pz, .n = pTagName->nLen}; - if (getColumnIndexByNameEx(&name, pCmd, &columnIndex) != TSDB_CODE_SUCCESS) { + if (getColumnIndexByNameEx(&name, pQueryInfo, &columnIndex) != TSDB_CODE_SUCCESS) { return TSDB_CODE_INVALID_SQL; } if (columnIndex.columnIndex < pMeterMeta->numOfColumns) { - return invalidSqlErrMsg(pCmd, msg2); + return invalidSqlErrMsg(pQueryInfo->msg, msg2); } SSchema* pTagsSchema = tsGetColumnSchema(pMeterMetaInfo->pMeterMeta, columnIndex.columnIndex); - if (tVariantDump(&pVarList->a[1].pVar, pAlterSQL->tagData.data/*pCmd->payload*/, pTagsSchema->type) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg1); + if (tVariantDump(&pVarList->a[1].pVar, pAlterSQL->tagData.data /*pCmd->payload*/, pTagsSchema->type) != + TSDB_CODE_SUCCESS) { + return invalidSqlErrMsg(pQueryInfo->msg, msg1); } // validate the length of binary if ((pTagsSchema->type == TSDB_DATA_TYPE_BINARY || pTagsSchema->type == TSDB_DATA_TYPE_NCHAR) && pVarList->a[1].pVar.nLen > pTagsSchema->bytes) { - return invalidSqlErrMsg(pCmd, msg3); + return invalidSqlErrMsg(pQueryInfo->msg, msg3); } char name1[128] = {0}; strncpy(name1, pTagName->pz, pTagName->nLen); - tscFieldInfoSetValue(&pCmd->pQueryInfo[0].fieldsInfo, 0, TSDB_DATA_TYPE_INT, name1, tDataTypeDesc[TSDB_DATA_TYPE_INT].nSize); + tscFieldInfoSetValue(&pQueryInfo->fieldsInfo, 0, TSDB_DATA_TYPE_INT, name1, + tDataTypeDesc[TSDB_DATA_TYPE_INT].nSize); } else if (pAlterSQL->type == TSDB_ALTER_TABLE_ADD_COLUMN) { tFieldList* pFieldList = pAlterSQL->pAddColumns; if (pFieldList->nField > 1) { const char* msg = "only support add one column"; - return invalidSqlErrMsg(pCmd, msg); + return invalidSqlErrMsg(pQueryInfo->msg, msg); } if (!validateOneColumn(pCmd, &pFieldList->p[0])) { return TSDB_CODE_INVALID_SQL; } - tscFieldInfoSetValFromField(&pCmd->pQueryInfo[0].fieldsInfo, 0, &pFieldList->p[0]); + tscFieldInfoSetValFromField(&pQueryInfo->fieldsInfo, 0, &pFieldList->p[0]); } else if (pAlterSQL->type == TSDB_ALTER_TABLE_DROP_COLUMN) { const char* msg1 = "no columns can be dropped"; const char* msg2 = "only support one column"; @@ -4136,64 +4144,65 @@ int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { const char* msg3 = "primary timestamp column cannot be dropped"; if (pMeterMeta->numOfColumns == TSDB_MIN_COLUMNS) { // - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); } if (pAlterSQL->varList->nExpr > 1) { - return invalidSqlErrMsg(pCmd, msg2); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } tVariantListItem* pItem = &pAlterSQL->varList->a[0]; SColumnIndex columnIndex = COLUMN_INDEX_INITIALIZER; SSQLToken name = {.type = TK_STRING, .z = pItem->pVar.pz, .n = pItem->pVar.nLen}; - if (getColumnIndexByNameEx(&name, pCmd, &columnIndex) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg4); + if (getColumnIndexByNameEx(&name, pQueryInfo, &columnIndex) != TSDB_CODE_SUCCESS) { + return invalidSqlErrMsg(pQueryInfo->msg, msg4); } if (columnIndex.columnIndex == PRIMARYKEY_TIMESTAMP_COL_INDEX) { - return invalidSqlErrMsg(pCmd, msg3); + return invalidSqlErrMsg(pQueryInfo->msg, msg3); } char name1[128] = {0}; strncpy(name1, pItem->pVar.pz, pItem->pVar.nLen); - tscFieldInfoSetValue(&pCmd->pQueryInfo[0].fieldsInfo, 0, TSDB_DATA_TYPE_INT, name1, tDataTypeDesc[TSDB_DATA_TYPE_INT].nSize); + tscFieldInfoSetValue(&pQueryInfo->fieldsInfo, 0, TSDB_DATA_TYPE_INT, name1, + tDataTypeDesc[TSDB_DATA_TYPE_INT].nSize); } return TSDB_CODE_SUCCESS; } -int32_t validateSqlFunctionInStreamSql(SSqlCmd* pCmd) { +int32_t validateSqlFunctionInStreamSql(SQueryInfo* pQueryInfo) { const char* msg0 = "sample interval can not be less than 10ms."; const char* msg1 = "functions not allowed in select clause"; - if (pCmd->pQueryInfo[0].nAggTimeInterval != 0 && pCmd->pQueryInfo[0].nAggTimeInterval < 10) { - return invalidSqlErrMsg(pCmd, msg0); + if (pQueryInfo->nAggTimeInterval != 0 && pQueryInfo->nAggTimeInterval < 10) { + return invalidSqlErrMsg(pQueryInfo->msg, msg0); } - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - int32_t functId = tscSqlExprGet(pCmd, i)->functionId; + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + int32_t functId = tscSqlExprGet(pQueryInfo, i)->functionId; if (!IS_STREAM_QUERY_VALID(aAggs[functId].nStatus)) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(pQueryInfo->msg, msg1); } } return TSDB_CODE_SUCCESS; } -int32_t validateFunctionsInIntervalOrGroupbyQuery(SSqlCmd* pCmd) { +int32_t validateFunctionsInIntervalOrGroupbyQuery(SQueryInfo* pQueryInfo) { bool isProjectionFunction = false; const char* msg1 = "column projection is not compatible with interval"; // multi-output set/ todo refactor - for (int32_t k = 0; k < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++k) { - SSqlExpr* pExpr = tscSqlExprGet(pCmd, k); + for (int32_t k = 0; k < pQueryInfo->fieldsInfo.numOfOutputCols; ++k) { + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, k); // projection query on primary timestamp, the selectivity function needs to be present. if (pExpr->functionId == TSDB_FUNC_PRJ && pExpr->colInfo.colId == PRIMARYKEY_TIMESTAMP_COL_INDEX) { bool hasSelectivity = false; - for (int32_t j = 0; j < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++j) { - SSqlExpr* pEx = tscSqlExprGet(pCmd, j); + for (int32_t j = 0; j < pQueryInfo->fieldsInfo.numOfOutputCols; ++j) { + SSqlExpr* pEx = tscSqlExprGet(pQueryInfo, j); if ((aAggs[pEx->functionId].nStatus & TSDB_FUNCSTATE_SELECTIVITY) == TSDB_FUNCSTATE_SELECTIVITY) { hasSelectivity = true; break; @@ -4212,7 +4221,7 @@ int32_t validateFunctionsInIntervalOrGroupbyQuery(SSqlCmd* pCmd) { } if (isProjectionFunction) { - invalidSqlErrMsg(pCmd, msg1); + invalidSqlErrMsg(pQueryInfo->msg, msg1); } return isProjectionFunction == true ? TSDB_CODE_INVALID_SQL : TSDB_CODE_SUCCESS; @@ -4345,17 +4354,18 @@ int32_t validateColumnName(char* name) { return TSDB_CODE_SUCCESS; } -bool hasTimestampForPointInterpQuery(SSqlCmd* pCmd) { - if (!tscIsPointInterpQuery(pCmd)) { +bool hasTimestampForPointInterpQuery(SQueryInfo* pQueryInfo) { + if (!tscIsPointInterpQuery(pQueryInfo)) { return true; } - return (pCmd->pQueryInfo[0].stime == pCmd->pQueryInfo[0].etime) && (pCmd->pQueryInfo[0].stime != 0); + return (pQueryInfo->stime == pQueryInfo->etime) && (pQueryInfo->stime != 0); } -int32_t parseLimitClause(SSqlObj* pSql, SQuerySQL* pQuerySql) { +int32_t parseLimitClause(SSqlObj* pSql, int32_t subClauseIndex, SQuerySQL* pQuerySql) { SSqlCmd* pCmd = &pSql->cmd; - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, subClauseIndex); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); const char* msg0 = "soffset/offset can not be less than 0"; const char* msg1 = "slimit/soffset only available for STable query"; @@ -4363,35 +4373,36 @@ int32_t parseLimitClause(SSqlObj* pSql, SQuerySQL* pQuerySql) { const char* msg3 = "slimit/soffset can not apply to projection query"; // handle the limit offset value, validate the limit - pCmd->pQueryInfo->limit = pQuerySql->limit; - pCmd->globalLimit = pCmd->pQueryInfo->limit.limit; + pQueryInfo->limit = pQuerySql->limit; + pCmd->globalLimit = pQueryInfo->limit.limit; - pCmd->pQueryInfo[0].slimit = pQuerySql->slimit; + pQueryInfo->slimit = pQuerySql->slimit; - if (pCmd->pQueryInfo[0].slimit.offset < 0 || pCmd->pQueryInfo->limit.offset < 0) { - return invalidSqlErrMsg(pCmd, msg0); + if (pQueryInfo->slimit.offset < 0 || pQueryInfo->limit.offset < 0) { + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg0); } - if (pCmd->pQueryInfo->limit.limit == 0) { + if (pQueryInfo->limit.limit == 0) { tscTrace("%p limit 0, no output result", pSql); pCmd->command = TSDB_SQL_RETRIEVE_EMPTY_RESULT; } - if (UTIL_METER_IS_METRIC(pMeterMetaInfo)) { + if (UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo)) { bool queryOnTags = false; - if (tscQueryOnlyMetricTags(pCmd, &queryOnTags) != TSDB_CODE_SUCCESS) { + if (tscQueryOnlyMetricTags(pQueryInfo, &queryOnTags) != TSDB_CODE_SUCCESS) { return TSDB_CODE_INVALID_SQL; } if (queryOnTags == true) { // local handle the metric tag query pCmd->command = TSDB_SQL_RETRIEVE_TAGS; } else { - if (tscProjectionQueryOnMetric(pCmd) && (pCmd->pQueryInfo[0].slimit.limit > 0 || pCmd->pQueryInfo[0].slimit.offset > 0)) { - return invalidSqlErrMsg(pCmd, msg3); + if (tscProjectionQueryOnMetric(&pSql->cmd, 0) && + (pQueryInfo->slimit.limit > 0 || pQueryInfo->slimit.offset > 0)) { + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3); } } - if (pCmd->pQueryInfo[0].slimit.limit == 0) { + if (pQueryInfo->slimit.limit == 0) { tscTrace("%p limit 0, no output result", pSql); pCmd->command = TSDB_SQL_RETRIEVE_EMPTY_RESULT; return TSDB_CODE_SUCCESS; @@ -4415,17 +4426,17 @@ int32_t parseLimitClause(SSqlObj* pSql, SQuerySQL* pQuerySql) { } // keep original limitation value in globalLimit - pCmd->globalLimit = pCmd->pQueryInfo->limit.limit; + pCmd->globalLimit = pQueryInfo->limit.limit; } else { - if (pCmd->pQueryInfo[0].slimit.limit != -1 || pCmd->pQueryInfo[0].slimit.offset != 0) { - return invalidSqlErrMsg(pCmd, msg1); + if (pQueryInfo->slimit.limit != -1 || pQueryInfo->slimit.offset != 0) { + return invalidSqlErrMsg(pQueryInfo->msg, msg1); } // filter the query functions operating on "tbname" column that are not supported by normal columns. - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - SSqlExpr* pExpr = tscSqlExprGet(pCmd, i); + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i); if (pExpr->colInfo.colIdx == TSDB_TBNAME_COLUMN_INDEX) { - return invalidSqlErrMsg(pCmd, msg2); + return invalidSqlErrMsg(pQueryInfo->msg, msg2); } } } @@ -4457,7 +4468,7 @@ static int32_t setKeepOption(SSqlCmd* pCmd, SCreateDbMsg* pMsg, SCreateDBInfo* p pMsg->daysToKeep2 = htonl(pKeep->a[2].pVar.i64Key); break; } - default: { return invalidSqlErrMsg(pCmd, msg); } + default: { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg); } } } @@ -4481,7 +4492,7 @@ static int32_t setTimePrecisionOption(SSqlCmd* pCmd, SCreateDbMsg* pMsg, SCreate strlen(TSDB_TIME_PRECISION_MICRO_STR) == pToken->n) { pMsg->precision = TSDB_TIME_PRECISION_MICRO; } else { - return invalidSqlErrMsg(pCmd, msg); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg); } } @@ -4521,69 +4532,73 @@ int32_t parseCreateDBOptions(SSqlCmd* pCmd, SCreateDBInfo* pCreateDbSql) { return TSDB_CODE_SUCCESS; } -void tscAddTimestampColumn(SSqlCmd* pCmd, int16_t functionId, int16_t tableIndex) { +void tscAddTimestampColumn(SQueryInfo* pQueryInfo, int16_t functionId, int16_t tableIndex) { // the first column not timestamp column, add it SSqlExpr* pExpr = NULL; - if (pCmd->pQueryInfo[0].exprsInfo.numOfExprs > 0) { - pExpr = tscSqlExprGet(pCmd, 0); + if (pQueryInfo->exprsInfo.numOfExprs > 0) { + pExpr = tscSqlExprGet(pQueryInfo, 0); } if (pExpr == NULL || pExpr->colInfo.colId != PRIMARYKEY_TIMESTAMP_COL_INDEX || pExpr->functionId != functionId) { SColumnIndex index = {tableIndex, PRIMARYKEY_TIMESTAMP_COL_INDEX}; - pExpr = tscSqlExprInsert(pCmd, 0, functionId, &index, TSDB_DATA_TYPE_TIMESTAMP, TSDB_KEYSIZE, TSDB_KEYSIZE); + pExpr = tscSqlExprInsert(pQueryInfo, 0, functionId, &index, TSDB_DATA_TYPE_TIMESTAMP, TSDB_KEYSIZE, TSDB_KEYSIZE); pExpr->colInfo.flag = TSDB_COL_NORMAL; // NOTE: tag column does not add to source column list SColumnList ids = getColumnList(1, tableIndex, PRIMARYKEY_TIMESTAMP_COL_INDEX); - insertResultField(pCmd, 0, &ids, TSDB_KEYSIZE, TSDB_DATA_TYPE_TIMESTAMP, "ts"); + insertResultField(pQueryInfo, 0, &ids, TSDB_KEYSIZE, TSDB_DATA_TYPE_TIMESTAMP, "ts"); } } -void addGroupInfoForSubquery(SSqlObj* pParentObj, SSqlObj* pSql, int32_t tableIndex) { - if (pParentObj->cmd.pQueryInfo[0].groupbyExpr.numOfGroupCols > 0) { - int32_t num = pSql->cmd.pQueryInfo[0].exprsInfo.numOfExprs; - SSqlExpr* pExpr = tscSqlExprGet(&pSql->cmd, num - 1); - SSqlCmd* pCmd = &pSql->cmd; - +void addGroupInfoForSubquery(SSqlObj* pParentObj, SSqlObj* pSql, int32_t subClauseIndex, int32_t tableIndex) { + SQueryInfo* pParentQueryInfo = tscGetQueryInfoDetail(&pParentObj->cmd, subClauseIndex); + + if (pParentQueryInfo->groupbyExpr.numOfGroupCols > 0) { + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, subClauseIndex); + int32_t num = pQueryInfo->exprsInfo.numOfExprs; + + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, num - 1); + if (pExpr->functionId != TSDB_FUNC_TAG) { - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0); - int16_t columnInfo = tscGetJoinTagColIndexByUid(&pCmd->pQueryInfo[0].tagCond, pMeterMetaInfo->pMeterMeta->uid); - SColumnIndex index = {.tableIndex = 0, .columnIndex = columnInfo}; - SSchema* pSchema = tsGetTagSchema(pMeterMetaInfo->pMeterMeta); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, tableIndex); + int16_t columnInfo = tscGetJoinTagColIndexByUid(&pQueryInfo->tagCond, pMeterMetaInfo->pMeterMeta->uid); + SColumnIndex index = {.tableIndex = 0, .columnIndex = columnInfo}; + SSchema* pSchema = tsGetTagSchema(pMeterMetaInfo->pMeterMeta); int16_t type = pSchema[index.columnIndex].type; int16_t bytes = pSchema[index.columnIndex].bytes; char* name = pSchema[index.columnIndex].name; - pExpr = tscSqlExprInsert(pCmd, pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols, TSDB_FUNC_TAG, &index, type, bytes, bytes); + pExpr = tscSqlExprInsert(pQueryInfo, pQueryInfo->fieldsInfo.numOfOutputCols, TSDB_FUNC_TAG, &index, type, bytes, + bytes); pExpr->colInfo.flag = TSDB_COL_TAG; // NOTE: tag column does not add to source column list SColumnList ids = {0}; - insertResultField(pCmd, pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols, &ids, bytes, type, name); + insertResultField(pQueryInfo, pQueryInfo->fieldsInfo.numOfOutputCols, &ids, bytes, type, name); int32_t relIndex = index.columnIndex; pExpr->colInfo.colIdx = relIndex; - pCmd->pQueryInfo[0].groupbyExpr.columnInfo[0].colIdx = relIndex; + pQueryInfo->groupbyExpr.columnInfo[0].colIdx = relIndex; - addRequiredTagColumn(pCmd, pCmd->pQueryInfo[0].groupbyExpr.columnInfo[0].colIdx, 0); + addRequiredTagColumn(pQueryInfo, pQueryInfo->groupbyExpr.columnInfo[0].colIdx, 0); } } } -void doAddGroupColumnForSubquery(SSqlCmd* pCmd, int32_t tagIndex) { - int32_t index = pCmd->pQueryInfo[0].groupbyExpr.columnInfo[tagIndex].colIdx; +void doAddGroupColumnForSubquery(SQueryInfo* pQueryInfo, int32_t tagIndex) { + int32_t index = pQueryInfo->groupbyExpr.columnInfo[tagIndex].colIdx; - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); SSchema* pSchema = tsGetColumnSchema(pMeterMetaInfo->pMeterMeta, index); SColumnIndex colIndex = {.tableIndex = 0, .columnIndex = index}; - SSqlExpr* pExpr = tscSqlExprInsert(pCmd, pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols, TSDB_FUNC_PRJ, &colIndex, pSchema->type, - pSchema->bytes, pSchema->bytes); + SSqlExpr* pExpr = tscSqlExprInsert(pQueryInfo, pQueryInfo->fieldsInfo.numOfOutputCols, TSDB_FUNC_PRJ, &colIndex, + pSchema->type, pSchema->bytes, pSchema->bytes); pExpr->colInfo.flag = TSDB_COL_NORMAL; pExpr->param[0].i64Key = 1; @@ -4594,14 +4609,15 @@ void doAddGroupColumnForSubquery(SSqlCmd* pCmd, int32_t tagIndex) { list.num = 1; list.ids[0] = colIndex; - insertResultField(pCmd, pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols, &list, pSchema->bytes, pSchema->type, pSchema->name); - tscFieldInfoUpdateVisible(&pCmd->pQueryInfo[0].fieldsInfo, pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols - 1, false); + insertResultField(pQueryInfo, pQueryInfo->fieldsInfo.numOfOutputCols, &list, pSchema->bytes, pSchema->type, + pSchema->name); + tscFieldInfoUpdateVisible(&pQueryInfo->fieldsInfo, pQueryInfo->fieldsInfo.numOfOutputCols - 1, false); } -static void doUpdateSqlFunctionForTagPrj(SSqlCmd* pCmd) { +static void doUpdateSqlFunctionForTagPrj(SQueryInfo* pQueryInfo) { int32_t tagLength = 0; - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - SSqlExpr* pExpr = tscSqlExprGet(pCmd, i); + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i); if (pExpr->functionId == TSDB_FUNC_TAGPRJ || pExpr->functionId == TSDB_FUNC_TAG) { pExpr->functionId = TSDB_FUNC_TAG_DUMMY; tagLength += pExpr->resBytes; @@ -4611,11 +4627,11 @@ static void doUpdateSqlFunctionForTagPrj(SSqlCmd* pCmd) { } } - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); SSchema* pSchema = tsGetSchema(pMeterMetaInfo->pMeterMeta); - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - SSqlExpr* pExpr = tscSqlExprGet(pCmd, i); + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i); if (pExpr->functionId != TSDB_FUNC_TAG_DUMMY && pExpr->functionId != TSDB_FUNC_TS_DUMMY) { SSchema* pColSchema = &pSchema[pExpr->colInfo.colIdx]; getResultDataInfo(pColSchema->type, pColSchema->bytes, pExpr->functionId, pExpr->param[0].i64Key, &pExpr->resType, @@ -4624,13 +4640,13 @@ static void doUpdateSqlFunctionForTagPrj(SSqlCmd* pCmd) { } } -static void doUpdateSqlFunctionForColPrj(SSqlCmd* pCmd) { - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - SSqlExpr* pExpr = tscSqlExprGet(pCmd, i); +static void doUpdateSqlFunctionForColPrj(SQueryInfo* pQueryInfo) { + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i); if (pExpr->functionId == TSDB_FUNC_PRJ) { bool qualifiedCol = false; - for (int32_t j = 0; j < pCmd->pQueryInfo[0].groupbyExpr.numOfGroupCols; ++j) { - if (pExpr->colInfo.colId == pCmd->pQueryInfo[0].groupbyExpr.columnInfo[j].colId) { + for (int32_t j = 0; j < pQueryInfo->groupbyExpr.numOfGroupCols; ++j) { + if (pExpr->colInfo.colId == pQueryInfo->groupbyExpr.columnInfo[j].colId) { qualifiedCol = true; pExpr->param[0].i64Key = 1; // limit the output to be 1 for each state value @@ -4654,12 +4670,12 @@ static bool tagColumnInGroupby(SSqlGroupbyExpr* pGroupbyExpr, int16_t columnId) return false; } -static bool onlyTagPrjFunction(SSqlCmd* pCmd) { +static bool onlyTagPrjFunction(SQueryInfo* pQueryInfo) { bool hasTagPrj = false; bool hasColumnPrj = false; - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - SSqlExpr* pExpr = tscSqlExprGet(pCmd, i); + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i); if (pExpr->functionId == TSDB_FUNC_PRJ) { hasColumnPrj = true; } else if (pExpr->functionId == TSDB_FUNC_TAGPRJ) { @@ -4671,16 +4687,16 @@ static bool onlyTagPrjFunction(SSqlCmd* pCmd) { } // check if all the tags prj columns belongs to the group by columns -static bool allTagPrjInGroupby(SSqlCmd* pCmd) { +static bool allTagPrjInGroupby(SQueryInfo* pQueryInfo) { bool allInGroupby = true; - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - SSqlExpr* pExpr = tscSqlExprGet(pCmd, i); + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i); if (pExpr->functionId != TSDB_FUNC_TAGPRJ) { continue; } - if (!tagColumnInGroupby(&pCmd->pQueryInfo[0].groupbyExpr, pExpr->colInfo.colId)) { + if (!tagColumnInGroupby(&pQueryInfo->groupbyExpr, pExpr->colInfo.colId)) { allInGroupby = false; break; } @@ -4690,9 +4706,9 @@ static bool allTagPrjInGroupby(SSqlCmd* pCmd) { return allInGroupby; } -static void updateTagPrjFunction(SSqlCmd* pCmd) { - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - SSqlExpr* pExpr = tscSqlExprGet(pCmd, i); +static void updateTagPrjFunction(SQueryInfo* pQueryInfo) { + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i); if (pExpr->functionId == TSDB_FUNC_TAGPRJ) { pExpr->functionId = TSDB_FUNC_TAG; } @@ -4705,7 +4721,7 @@ static void updateTagPrjFunction(SSqlCmd* pCmd) { * 2. if selectivity function and tagprj function both exist, there should be only * one selectivity function exists. */ -static int32_t checkUpdateTagPrjFunctions(SSqlCmd* pCmd) { +static int32_t checkUpdateTagPrjFunctions(SQueryInfo* pQueryInfo) { const char* msg1 = "only one selectivity function allowed in presence of tags function"; const char* msg3 = "aggregation function should not be mixed up with projection"; @@ -4713,8 +4729,8 @@ static int32_t checkUpdateTagPrjFunctions(SSqlCmd* pCmd) { int16_t numOfSelectivity = 0; int16_t numOfAggregation = 0; - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - SSqlExpr* pExpr = tscSqlExprGet(pCmd, i); + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i); if (pExpr->functionId == TSDB_FUNC_TAGPRJ || (pExpr->functionId == TSDB_FUNC_PRJ && pExpr->colInfo.colId == PRIMARYKEY_TIMESTAMP_COL_INDEX)) { tagColExists = true; @@ -4722,8 +4738,8 @@ static int32_t checkUpdateTagPrjFunctions(SSqlCmd* pCmd) { } } - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - int16_t functionId = tscSqlExprGet(pCmd, i)->functionId; + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + int16_t functionId = tscSqlExprGet(pQueryInfo, i)->functionId; if (functionId == TSDB_FUNC_TAGPRJ || functionId == TSDB_FUNC_PRJ || functionId == TSDB_FUNC_TS || functionId == TSDB_FUNC_ARITHM) { continue; @@ -4740,44 +4756,44 @@ static int32_t checkUpdateTagPrjFunctions(SSqlCmd* pCmd) { // When the tag projection function on tag column that is not in the group by clause, aggregation function and // selectivity function exist in select clause is not allowed. if (numOfAggregation > 0) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(pQueryInfo->msg, msg1); } /* * if numOfSelectivity equals to 0, it is a super table projection query */ if (numOfSelectivity == 1) { - doUpdateSqlFunctionForTagPrj(pCmd); - doUpdateSqlFunctionForColPrj(pCmd); + doUpdateSqlFunctionForTagPrj(pQueryInfo); + doUpdateSqlFunctionForColPrj(pQueryInfo); } else if (numOfSelectivity > 1) { /* * If more than one selectivity functions exist, all the selectivity functions must be last_row. * Otherwise, return with error code. */ - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - int16_t functionId = tscSqlExprGet(pCmd, i)->functionId; + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + int16_t functionId = tscSqlExprGet(pQueryInfo, i)->functionId; if (functionId == TSDB_FUNC_TAGPRJ) { continue; } if (((aAggs[functionId].nStatus & TSDB_FUNCSTATE_SELECTIVITY) != 0) && (functionId != TSDB_FUNC_LAST_ROW)) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(pQueryInfo->msg, msg1); } } - doUpdateSqlFunctionForTagPrj(pCmd); - doUpdateSqlFunctionForColPrj(pCmd); + doUpdateSqlFunctionForTagPrj(pQueryInfo); + doUpdateSqlFunctionForColPrj(pQueryInfo); } } else { - if ((pCmd->type & TSDB_QUERY_TYPE_PROJECTION_QUERY) == TSDB_QUERY_TYPE_PROJECTION_QUERY) { - if (numOfAggregation > 0 && pCmd->pQueryInfo[0].groupbyExpr.numOfGroupCols == 0) { - return invalidSqlErrMsg(pCmd, msg3); + if ((pQueryInfo->type & TSDB_QUERY_TYPE_PROJECTION_QUERY) == TSDB_QUERY_TYPE_PROJECTION_QUERY) { + if (numOfAggregation > 0 && pQueryInfo->groupbyExpr.numOfGroupCols == 0) { + return invalidSqlErrMsg(pQueryInfo->msg, msg3); } if (numOfAggregation > 0 || numOfSelectivity > 0) { // clear the projection type flag - pCmd->type &= (~TSDB_QUERY_TYPE_PROJECTION_QUERY); - doUpdateSqlFunctionForColPrj(pCmd); + pQueryInfo->type &= (~TSDB_QUERY_TYPE_PROJECTION_QUERY); + doUpdateSqlFunctionForColPrj(pQueryInfo); } } } @@ -4785,18 +4801,18 @@ static int32_t checkUpdateTagPrjFunctions(SSqlCmd* pCmd) { return TSDB_CODE_SUCCESS; } -static int32_t doAddGroupbyColumnsOnDemand(SSqlCmd* pCmd) { +static int32_t doAddGroupbyColumnsOnDemand(SQueryInfo* pQueryInfo) { const char* msg2 = "interval not allowed in group by normal column"; - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); SSchema* pSchema = tsGetSchema(pMeterMetaInfo->pMeterMeta); int16_t bytes = 0; int16_t type = 0; char* name = NULL; - for (int32_t i = 0; i < pCmd->pQueryInfo[0].groupbyExpr.numOfGroupCols; ++i) { - SColIndexEx* pColIndex = &pCmd->pQueryInfo[0].groupbyExpr.columnInfo[i]; + for (int32_t i = 0; i < pQueryInfo->groupbyExpr.numOfGroupCols; ++i) { + SColIndexEx* pColIndex = &pQueryInfo->groupbyExpr.columnInfo[i]; int16_t colIndex = pColIndex->colIdx; if (pColIndex->colIdx == TSDB_TBNAME_COLUMN_INDEX) { @@ -4813,25 +4829,25 @@ static int32_t doAddGroupbyColumnsOnDemand(SSqlCmd* pCmd) { } if (TSDB_COL_IS_TAG(pColIndex->flag)) { - SColumnIndex index = {.tableIndex = pCmd->pQueryInfo[0].groupbyExpr.tableIndex, .columnIndex = colIndex}; + SColumnIndex index = {.tableIndex = pQueryInfo->groupbyExpr.tableIndex, .columnIndex = colIndex}; - SSqlExpr* pExpr = - tscSqlExprInsert(pCmd, pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols, TSDB_FUNC_TAG, &index, type, bytes, bytes); + SSqlExpr* pExpr = tscSqlExprInsert(pQueryInfo, pQueryInfo->fieldsInfo.numOfOutputCols, TSDB_FUNC_TAG, &index, + type, bytes, bytes); pExpr->colInfo.flag = TSDB_COL_TAG; // NOTE: tag column does not add to source column list SColumnList ids = {0}; - insertResultField(pCmd, pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols, &ids, bytes, type, name); + insertResultField(pQueryInfo, pQueryInfo->fieldsInfo.numOfOutputCols, &ids, bytes, type, name); } else { // if this query is "group by" normal column, interval is not allowed - if (pCmd->pQueryInfo[0].nAggTimeInterval > 0) { - return invalidSqlErrMsg(pCmd, msg2); + if (pQueryInfo->nAggTimeInterval > 0) { + return invalidSqlErrMsg(pQueryInfo->msg, msg2); } bool hasGroupColumn = false; - for (int32_t j = 0; j < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++j) { - SSqlExpr* pExpr = tscSqlExprGet(pCmd, j); + for (int32_t j = 0; j < pQueryInfo->fieldsInfo.numOfOutputCols; ++j) { + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, j); if (pExpr->colInfo.colId == pColIndex->colId) { break; } @@ -4842,7 +4858,7 @@ static int32_t doAddGroupbyColumnsOnDemand(SSqlCmd* pCmd) { * but invisible to user */ if (!hasGroupColumn) { - doAddGroupColumnForSubquery(pCmd, i); + doAddGroupColumnForSubquery(pQueryInfo, i); } } } @@ -4850,33 +4866,31 @@ static int32_t doAddGroupbyColumnsOnDemand(SSqlCmd* pCmd) { return TSDB_CODE_SUCCESS; } -int32_t doFunctionsCompatibleCheck(SSqlObj* pSql) { +int32_t doFunctionsCompatibleCheck(SSqlCmd* pCmd, SQueryInfo* pQueryInfo) { const char* msg1 = "functions/columns not allowed in group by query"; const char* msg2 = "projection query on columns not allowed"; const char* msg3 = "group by not allowed on projection query"; const char* msg4 = "retrieve tags not compatible with group by or interval query"; - SSqlCmd* pCmd = &pSql->cmd; - // only retrieve tags, group by is not supportted if (pCmd->command == TSDB_SQL_RETRIEVE_TAGS) { - if (pCmd->pQueryInfo[0].groupbyExpr.numOfGroupCols > 0 || pCmd->pQueryInfo[0].nAggTimeInterval > 0) { - return invalidSqlErrMsg(pCmd, msg4); + if (pQueryInfo->groupbyExpr.numOfGroupCols > 0 || pQueryInfo->nAggTimeInterval > 0) { + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg4); } else { return TSDB_CODE_SUCCESS; } } - if (pCmd->pQueryInfo[0].groupbyExpr.numOfGroupCols > 0) { + if (pQueryInfo->groupbyExpr.numOfGroupCols > 0) { // check if all the tags prj columns belongs to the group by columns - if (onlyTagPrjFunction(pCmd) && allTagPrjInGroupby(pCmd)) { - updateTagPrjFunction(pCmd); - return doAddGroupbyColumnsOnDemand(pCmd); + if (onlyTagPrjFunction(pQueryInfo) && allTagPrjInGroupby(pQueryInfo)) { + updateTagPrjFunction(pQueryInfo); + return doAddGroupbyColumnsOnDemand(pQueryInfo); } // check all query functions in selection clause, multi-output functions are not allowed - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - SSqlExpr* pExpr = tscSqlExprGet(pCmd, i); + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i); int32_t functId = pExpr->functionId; /* @@ -4885,8 +4899,8 @@ int32_t doFunctionsCompatibleCheck(SSqlObj* pSql) { */ if (functId == TSDB_FUNC_PRJ && pExpr->colInfo.colId != PRIMARYKEY_TIMESTAMP_COL_INDEX) { bool qualified = false; - for (int32_t j = 0; j < pCmd->pQueryInfo[0].groupbyExpr.numOfGroupCols; ++j) { - SColIndexEx* pColIndex = &pCmd->pQueryInfo[0].groupbyExpr.columnInfo[j]; + for (int32_t j = 0; j < pQueryInfo->groupbyExpr.numOfGroupCols; ++j) { + SColIndexEx* pColIndex = &pQueryInfo->groupbyExpr.columnInfo[j]; if (pColIndex->colId == pExpr->colInfo.colId) { qualified = true; break; @@ -4894,21 +4908,21 @@ int32_t doFunctionsCompatibleCheck(SSqlObj* pSql) { } if (!qualified) { - return invalidSqlErrMsg(pCmd, msg2); + return invalidSqlErrMsg(pQueryInfo->msg, msg2); } } if (IS_MULTIOUTPUT(aAggs[functId].nStatus) && functId != TSDB_FUNC_TOP && functId != TSDB_FUNC_BOTTOM && functId != TSDB_FUNC_TAGPRJ && functId != TSDB_FUNC_PRJ) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(pQueryInfo->msg, msg1); } if (functId == TSDB_FUNC_COUNT && pExpr->colInfo.colIdx == TSDB_TBNAME_COLUMN_INDEX) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(pQueryInfo->msg, msg1); } } - if (checkUpdateTagPrjFunctions(pCmd) != TSDB_CODE_SUCCESS) { + if (checkUpdateTagPrjFunctions(pQueryInfo) != TSDB_CODE_SUCCESS) { return TSDB_CODE_INVALID_SQL; } @@ -4916,34 +4930,35 @@ int32_t doFunctionsCompatibleCheck(SSqlObj* pSql) { * group by tag function must be not changed the function name, otherwise, the group operation may fail to * divide the subset of final result. */ - if (doAddGroupbyColumnsOnDemand(pCmd) != TSDB_CODE_SUCCESS) { + if (doAddGroupbyColumnsOnDemand(pQueryInfo) != TSDB_CODE_SUCCESS) { return TSDB_CODE_INVALID_SQL; } // projection query on metric does not compatible with "group by" syntax - if (tscProjectionQueryOnMetric(pCmd)) { - return invalidSqlErrMsg(pCmd, msg3); + if (tscProjectionQueryOnMetric(pCmd, 0)) { + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3); } return TSDB_CODE_SUCCESS; } else { - return checkUpdateTagPrjFunctions(pCmd); + return checkUpdateTagPrjFunctions(pQueryInfo); } } -int32_t doLocalQueryProcess(SQuerySQL* pQuerySql, SSqlCmd* pCmd) { +int32_t doLocalQueryProcess(SQuerySQL* pQuerySql, SSqlCmd* pCmd, int32_t subClauseIndex) { const char* msg1 = "only one expression allowed"; const char* msg2 = "invalid expression in select clause"; const char* msg3 = "invalid function"; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, subClauseIndex); tSQLExprList* pExprList = pQuerySql->pSelection; if (pExprList->nExpr != 1) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(pQueryInfo->msg, msg1); } tSQLExpr* pExpr = pExprList->a[0].pNode; if (pExpr->operand.z == NULL) { - return invalidSqlErrMsg(pCmd, msg2); + return invalidSqlErrMsg(pQueryInfo->msg, msg2); } // TODO redefine the function @@ -4962,7 +4977,7 @@ int32_t doLocalQueryProcess(SQuerySQL* pQuerySql, SSqlCmd* pCmd) { } } - SSqlExpr* pExpr1 = tscSqlExprInsertEmpty(pCmd, 0, TSDB_FUNC_TAG_DUMMY); + SSqlExpr* pExpr1 = tscSqlExprInsertEmpty(pQueryInfo, 0, TSDB_FUNC_TAG_DUMMY); if (pExprList->a[0].aliasName != NULL) { strncpy(pExpr1->aliasName, pExprList->a[0].aliasName, tListLen(pExpr1->aliasName)); } else { @@ -4985,7 +5000,7 @@ int32_t doLocalQueryProcess(SQuerySQL* pQuerySql, SSqlCmd* pCmd) { case 4: pCmd->command = TSDB_SQL_CURRENT_USER; return TSDB_CODE_SUCCESS; - default: { return invalidSqlErrMsg(pCmd, msg3); } + default: { return invalidSqlErrMsg(pQueryInfo->msg, msg3); } } } @@ -4995,91 +5010,93 @@ int32_t tscCheckCreateDbParams(SSqlCmd* pCmd, SCreateDbMsg* pCreate) { if (pCreate->commitLog != -1 && (pCreate->commitLog < 0 || pCreate->commitLog > 1)) { snprintf(msg, tListLen(msg), "invalid db option commitLog: %d, only 0 or 1 allowed", pCreate->commitLog); - return invalidSqlErrMsg(pCmd, msg); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg); } if (pCreate->replications != -1 && (pCreate->replications < TSDB_REPLICA_MIN_NUM || pCreate->replications > TSDB_REPLICA_MAX_NUM)) { snprintf(msg, tListLen(msg), "invalid db option replications: %d valid range: [%d, %d]", pCreate->replications, TSDB_REPLICA_MIN_NUM, TSDB_REPLICA_MAX_NUM); - return invalidSqlErrMsg(pCmd, msg); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg); } int32_t val = htonl(pCreate->daysPerFile); if (val != -1 && (val < TSDB_FILE_MIN_PARTITION_RANGE || val > TSDB_FILE_MAX_PARTITION_RANGE)) { snprintf(msg, tListLen(msg), "invalid db option daysPerFile: %d valid range: [%d, %d]", val, TSDB_FILE_MIN_PARTITION_RANGE, TSDB_FILE_MAX_PARTITION_RANGE); - return invalidSqlErrMsg(pCmd, msg); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg); } val = htonl(pCreate->rowsInFileBlock); if (val != -1 && (val < TSDB_MIN_ROWS_IN_FILEBLOCK || val > TSDB_MAX_ROWS_IN_FILEBLOCK)) { snprintf(msg, tListLen(msg), "invalid db option rowsInFileBlock: %d valid range: [%d, %d]", val, TSDB_MIN_ROWS_IN_FILEBLOCK, TSDB_MAX_ROWS_IN_FILEBLOCK); - return invalidSqlErrMsg(pCmd, msg); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg); } val = htonl(pCreate->cacheBlockSize); if (val != -1 && (val < TSDB_MIN_CACHE_BLOCK_SIZE || val > TSDB_MAX_CACHE_BLOCK_SIZE)) { snprintf(msg, tListLen(msg), "invalid db option cacheBlockSize: %d valid range: [%d, %d]", val, TSDB_MIN_CACHE_BLOCK_SIZE, TSDB_MAX_CACHE_BLOCK_SIZE); - return invalidSqlErrMsg(pCmd, msg); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg); } val = htonl(pCreate->maxSessions); if (val != -1 && (val < TSDB_MIN_TABLES_PER_VNODE || val > TSDB_MAX_TABLES_PER_VNODE)) { snprintf(msg, tListLen(msg), "invalid db option maxSessions: %d valid range: [%d, %d]", val, TSDB_MIN_TABLES_PER_VNODE, TSDB_MAX_TABLES_PER_VNODE); - return invalidSqlErrMsg(pCmd, msg); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg); } if (pCreate->precision != TSDB_TIME_PRECISION_MILLI && pCreate->precision != TSDB_TIME_PRECISION_MICRO) { snprintf(msg, tListLen(msg), "invalid db option timePrecision: %d valid value: [%d, %d]", pCreate->precision, TSDB_TIME_PRECISION_MILLI, TSDB_TIME_PRECISION_MICRO); - return invalidSqlErrMsg(pCmd, msg); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg); } if (pCreate->cacheNumOfBlocks.fraction != -1 && (pCreate->cacheNumOfBlocks.fraction < TSDB_MIN_AVG_BLOCKS || pCreate->cacheNumOfBlocks.fraction > TSDB_MAX_AVG_BLOCKS)) { snprintf(msg, tListLen(msg), "invalid db option ablocks: %f valid value: [%d, %d]", pCreate->cacheNumOfBlocks.fraction, TSDB_MIN_AVG_BLOCKS, TSDB_MAX_AVG_BLOCKS); - return invalidSqlErrMsg(pCmd, msg); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg); } val = htonl(pCreate->commitTime); if (val != -1 && (val < TSDB_MIN_COMMIT_TIME_INTERVAL || val > TSDB_MAX_COMMIT_TIME_INTERVAL)) { snprintf(msg, tListLen(msg), "invalid db option commitTime: %d valid range: [%d, %d]", val, TSDB_MIN_COMMIT_TIME_INTERVAL, TSDB_MAX_COMMIT_TIME_INTERVAL); - return invalidSqlErrMsg(pCmd, msg); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg); } if (pCreate->compression != -1 && (pCreate->compression < TSDB_MIN_COMPRESSION_LEVEL || pCreate->compression > TSDB_MAX_COMPRESSION_LEVEL)) { snprintf(msg, tListLen(msg), "invalid db option compression: %d valid range: [%d, %d]", pCreate->compression, TSDB_MIN_COMPRESSION_LEVEL, TSDB_MAX_COMPRESSION_LEVEL); - return invalidSqlErrMsg(pCmd, msg); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg); } return TSDB_CODE_SUCCESS; } // for debug purpose -void tscPrintSelectClause(SSqlCmd* pCmd) { - if (pCmd == NULL || pCmd->pQueryInfo[0].exprsInfo.numOfExprs == 0) { +void tscPrintSelectClause(SSqlCmd* pCmd, int32_t subClauseIndex) { + SQueryInfo *pQueryInfo = tscGetQueryInfoDetail(pCmd, subClauseIndex); + + if (pCmd == NULL || pQueryInfo->exprsInfo.numOfExprs == 0) { return; } char* str = calloc(1, 10240); int32_t offset = 0; - offset += sprintf(str, "%d [", pCmd->pQueryInfo[0].exprsInfo.numOfExprs); - for (int32_t i = 0; i < pCmd->pQueryInfo[0].exprsInfo.numOfExprs; ++i) { - SSqlExpr* pExpr = tscSqlExprGet(pCmd, i); + offset += sprintf(str, "%d [", pQueryInfo->exprsInfo.numOfExprs); + for (int32_t i = 0; i < pQueryInfo->exprsInfo.numOfExprs; ++i) { + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i); int32_t size = sprintf(str + offset, "%s(%d)", aAggs[pExpr->functionId].aName, pExpr->colInfo.colId); offset += size; - if (i < pCmd->pQueryInfo[0].exprsInfo.numOfExprs - 1) { + if (i < pQueryInfo->exprsInfo.numOfExprs - 1) { str[offset++] = ','; } } @@ -5090,11 +5107,12 @@ void tscPrintSelectClause(SSqlCmd* pCmd) { free(str); } -int32_t doCheckForCreateTable(SSqlObj* pSql, SSqlInfo* pInfo) { +int32_t doCheckForCreateTable(SSqlObj* pSql, int32_t subClauseIndex, SSqlInfo* pInfo) { const char* msg1 = "invalid table name"; const char* msg2 = "table name too long"; SSqlCmd* pCmd = &pSql->cmd; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, subClauseIndex); SCreateTableSQL* pCreateTable = pInfo->pCreateTableInfo; @@ -5107,11 +5125,11 @@ int32_t doCheckForCreateTable(SSqlObj* pSql, SSqlInfo* pInfo) { SSQLToken* pzTableName = &(pCreateTable->name); if (tscValidateName(pzTableName) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); } - if (setMeterID(pSql, pzTableName, 0) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg2); + if (setMeterID(pSql, 0, pzTableName, 0) != TSDB_CODE_SUCCESS) { + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } if (!validateTableColumnInfo(pFieldList, pCmd) || @@ -5121,14 +5139,14 @@ int32_t doCheckForCreateTable(SSqlObj* pSql, SSqlInfo* pInfo) { int32_t col = 0; for (; col < pFieldList->nField; ++col) { - tscFieldInfoSetValFromField(&pCmd->pQueryInfo[0].fieldsInfo, col, &pFieldList->p[col]); + tscFieldInfoSetValFromField(&pQueryInfo->fieldsInfo, col, &pFieldList->p[col]); } pCmd->numOfCols = (int16_t)pFieldList->nField; if (pTagList != NULL) { // create metric[optional] for (int32_t i = 0; i < pTagList->nField; ++i) { - tscFieldInfoSetValFromField(&pCmd->pQueryInfo[0].fieldsInfo, col++, &pTagList->p[i]); + tscFieldInfoSetValFromField(&pQueryInfo->fieldsInfo, col++, &pTagList->p[i]); } pCmd->count = pTagList->nField; @@ -5146,17 +5164,17 @@ int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo) { SSqlCmd* pCmd = &pSql->cmd; SCreateTableSQL* pCreateTable = pInfo->pCreateTableInfo; - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0); // super table name, create table by using dst SSQLToken* pToken = &(pCreateTable->usingInfo.stableName); if (tscValidateName(pToken) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); } - if (setMeterID(pSql, pToken, 0) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg1); + if (setMeterID(pSql, 0, pToken, 0) != TSDB_CODE_SUCCESS) { + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); } // get meter meta from mnode @@ -5169,7 +5187,7 @@ int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo) { } if (pMeterMetaInfo->pMeterMeta->numOfTags != pList->nExpr) { - return invalidSqlErrMsg(pCmd, msg5); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg5); } // too long tag values will return invalid sql, not be truncated automatically @@ -5179,13 +5197,13 @@ int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo) { for (int32_t i = 0; i < pList->nExpr; ++i) { int32_t ret = tVariantDump(&(pList->a[i].pVar), tagVal, pTagSchema[i].type); if (ret != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg4); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg4); } // validate the length of binary if ((pTagSchema[i].type == TSDB_DATA_TYPE_BINARY || pTagSchema[i].type == TSDB_DATA_TYPE_NCHAR) && pList->a[i].pVar.nLen > pTagSchema[i].bytes) { - return invalidSqlErrMsg(pCmd, msg3); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3); } tagVal += pTagSchema[i].bytes; @@ -5193,10 +5211,10 @@ int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo) { // table name if (tscValidateName(&pInfo->pCreateTableInfo->name) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); } - int32_t ret = setMeterID(pSql, &pInfo->pCreateTableInfo->name, 0); + int32_t ret = setMeterID(pSql, 0, &pInfo->pCreateTableInfo->name, 0); if (ret != TSDB_CODE_SUCCESS) { return ret; } @@ -5212,16 +5230,17 @@ int32_t doCheckForStream(SSqlObj* pSql, SSqlInfo* pInfo) { const char* msg5 = "sql too long"; // todo ADD support SSqlCmd* pCmd = &pSql->cmd; - + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + SCreateTableSQL* pCreateTable = pInfo->pCreateTableInfo; - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); // if sql specifies db, use it, otherwise use default db SSQLToken* pzTableName = &(pCreateTable->name); SQuerySQL* pQuerySql = pCreateTable->pSelect; if (tscValidateName(pzTableName) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); } tVariantList* pSrcMeterName = pInfo->pCreateTableInfo->pSelect->from; @@ -5229,11 +5248,11 @@ int32_t doCheckForStream(SSqlObj* pSql, SSqlInfo* pInfo) { SSQLToken srcToken = {.z = pVar->pz, .n = pVar->nLen, .type = TK_STRING}; if (tscValidateName(&srcToken) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg1); + return invalidSqlErrMsg(pQueryInfo->msg, msg1); } - if (setMeterID(pSql, &srcToken, 0) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg2); + if (setMeterID(pSql, 0, &srcToken, 0) != TSDB_CODE_SUCCESS) { + return invalidSqlErrMsg(pQueryInfo->msg, msg2); } int32_t code = tscGetMeterMeta(pSql, pMeterMetaInfo->name, 0); @@ -5241,8 +5260,8 @@ int32_t doCheckForStream(SSqlObj* pSql, SSqlInfo* pInfo) { return code; } - bool isMetric = UTIL_METER_IS_METRIC(pMeterMetaInfo); - if (parseSelectClause(pCmd, pQuerySql->pSelection, isMetric) != TSDB_CODE_SUCCESS) { + bool isSTable = UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo); + if (parseSelectClause(&pSql->cmd, pQuerySql->pSelection, isSTable) != TSDB_CODE_SUCCESS) { return TSDB_CODE_INVALID_SQL; } @@ -5253,34 +5272,35 @@ int32_t doCheckForStream(SSqlObj* pSql, SSqlInfo* pInfo) { } // set interval value - if (parseIntervalClause(pCmd, pQuerySql) != TSDB_CODE_SUCCESS) { + if (parseIntervalClause(pQueryInfo, pQuerySql) != TSDB_CODE_SUCCESS) { return TSDB_CODE_INVALID_SQL; } else { - if ((pCmd->pQueryInfo[0].nAggTimeInterval > 0) && (validateFunctionsInIntervalOrGroupbyQuery(pCmd) != TSDB_CODE_SUCCESS)) { + if ((pQueryInfo->nAggTimeInterval > 0) && + (validateFunctionsInIntervalOrGroupbyQuery(pQueryInfo) != TSDB_CODE_SUCCESS)) { return TSDB_CODE_INVALID_SQL; } } - if (setSlidingClause(pCmd, pQuerySql) != TSDB_CODE_SUCCESS) { + if (setSlidingClause(pQueryInfo, pQuerySql) != TSDB_CODE_SUCCESS) { return TSDB_CODE_INVALID_SQL; } // set the created table[stream] name - if (setMeterID(pSql, pzTableName, 0) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg1); + if (setMeterID(pSql, 0, pzTableName, 0) != TSDB_CODE_SUCCESS) { + return invalidSqlErrMsg(pQueryInfo->msg, msg1); } if (pQuerySql->selectToken.n > TSDB_MAX_SAVED_SQL_LEN) { - return invalidSqlErrMsg(pCmd, msg5); + return invalidSqlErrMsg(pQueryInfo->msg, msg5); } - if (tsRewriteFieldNameIfNecessary(pCmd) != TSDB_CODE_SUCCESS) { + if (tsRewriteFieldNameIfNecessary(pQueryInfo) != TSDB_CODE_SUCCESS) { return TSDB_CODE_INVALID_SQL; } - pCmd->numOfCols = pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; + pCmd->numOfCols = pQueryInfo->fieldsInfo.numOfOutputCols; - if (validateSqlFunctionInStreamSql(pCmd) != TSDB_CODE_SUCCESS) { + if (validateSqlFunctionInStreamSql(pQueryInfo) != TSDB_CODE_SUCCESS) { return TSDB_CODE_INVALID_SQL; } @@ -5289,21 +5309,21 @@ int32_t doCheckForStream(SSqlObj* pSql, SSqlInfo* pInfo) { * not here. */ if (pQuerySql->fillType != NULL) { - if (pCmd->pQueryInfo[0].nAggTimeInterval == 0) { - return invalidSqlErrMsg(pCmd, msg3); + if (pQueryInfo->nAggTimeInterval == 0) { + return invalidSqlErrMsg(pQueryInfo->msg, msg3); } tVariantListItem* pItem = &pQuerySql->fillType->a[0]; if (pItem->pVar.nType == TSDB_DATA_TYPE_BINARY) { if (!((strncmp(pItem->pVar.pz, "none", 4) == 0 && pItem->pVar.nLen == 4) || (strncmp(pItem->pVar.pz, "null", 4) == 0 && pItem->pVar.nLen == 4))) { - return invalidSqlErrMsg(pCmd, msg4); + return invalidSqlErrMsg(pQueryInfo->msg, msg4); } } } // set the number of stream table columns - pCmd->numOfCols = pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; + pCmd->numOfCols = pQueryInfo->fieldsInfo.numOfOutputCols; return TSDB_CODE_SUCCESS; } @@ -5322,14 +5342,14 @@ int32_t doCheckForQuery(SSqlObj* pSql, SQuerySQL* pQuerySql, int32_t index) { const char* msg9 = "TWA query requires both the start and end time"; int32_t code = TSDB_CODE_SUCCESS; - + SSqlCmd* pCmd = &pSql->cmd; - - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); - + + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, index, 0); + // too many result columns not support order by in query if (pQuerySql->pSelection->nExpr > TSDB_MAX_COLUMNS) { - return invalidSqlErrMsg(pCmd, msg8); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg8); } /* @@ -5342,11 +5362,11 @@ int32_t doCheckForQuery(SSqlObj* pSql, SQuerySQL* pQuerySql, int32_t index) { if (pQuerySql->from == NULL) { assert(pQuerySql->fillType == NULL && pQuerySql->pGroupby == NULL && pQuerySql->pWhere == NULL && pQuerySql->pSortOrder == NULL); - return doLocalQueryProcess(pQuerySql, pCmd); + return doLocalQueryProcess(pQuerySql, pCmd, 0); } if (pQuerySql->from->nExpr > TSDB_MAX_JOIN_TABLE_NUM) { - return invalidSqlErrMsg(pCmd, msg7); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg7); } // set all query tables, which are maybe more than one. @@ -5354,26 +5374,27 @@ int32_t doCheckForQuery(SSqlObj* pSql, SQuerySQL* pQuerySql, int32_t index) { tVariant* pTableItem = &pQuerySql->from->a[i].pVar; if (pTableItem->nType != TSDB_DATA_TYPE_BINARY) { - return invalidSqlErrMsg(pCmd, msg0); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg0); } pTableItem->nLen = strdequote(pTableItem->pz); SSQLToken tableName = {.z = pTableItem->pz, .n = pTableItem->nLen, .type = TK_STRING}; if (tscValidateName(&tableName) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg0); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg0); } - if (pCmd->pQueryInfo->numOfTables <= i) { - tscAddEmptyMeterMetaInfo(pCmd); + SQueryInfo* pQueryInfo = pCmd->pQueryInfo[index]; + if (pQueryInfo->numOfTables <= i) { // more than one table + tscAddEmptyMeterMetaInfo(pCmd, 0); } SSQLToken t = {.type = TSDB_DATA_TYPE_BINARY, .n = pTableItem->nLen, .z = pTableItem->pz}; - if (setMeterID(pSql, &t, i) != TSDB_CODE_SUCCESS) { - return invalidSqlErrMsg(pCmd, msg1); + if (setMeterID(pSql, index, &t, i) != TSDB_CODE_SUCCESS) { + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); } - SMeterMetaInfo* pMeterInfo1 = tscGetMeterMetaInfo(pCmd, i); + SMeterMetaInfo* pMeterInfo1 = tscGetMeterMetaInfo(pCmd, index, i); code = tscGetMeterMeta(pSql, pMeterInfo1->name, i); if (code != TSDB_CODE_SUCCESS) { return code; @@ -5381,20 +5402,22 @@ int32_t doCheckForQuery(SSqlObj* pSql, SQuerySQL* pQuerySql, int32_t index) { } // parse the group by clause in the first place - if (parseGroupbyClause(pCmd, pQuerySql->pGroupby) != TSDB_CODE_SUCCESS) { + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, index); + if (parseGroupbyClause(pQueryInfo, pQuerySql->pGroupby, pCmd) != TSDB_CODE_SUCCESS) { return TSDB_CODE_INVALID_SQL; } - bool isMetric = UTIL_METER_IS_METRIC(pMeterMetaInfo); - if (parseSelectClause(pCmd, pQuerySql->pSelection, isMetric) != TSDB_CODE_SUCCESS) { + bool isSTable = UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo); + if (parseSelectClause(pCmd, pQuerySql->pSelection, isSTable) != TSDB_CODE_SUCCESS) { return TSDB_CODE_INVALID_SQL; } // set interval value - if (parseIntervalClause(pCmd, pQuerySql) != TSDB_CODE_SUCCESS) { + if (parseIntervalClause(pQueryInfo, pQuerySql) != TSDB_CODE_SUCCESS) { return TSDB_CODE_INVALID_SQL; } else { - if ((pCmd->pQueryInfo[0].nAggTimeInterval > 0) && (validateFunctionsInIntervalOrGroupbyQuery(pCmd) != TSDB_CODE_SUCCESS)) { + if ((pQueryInfo->nAggTimeInterval > 0) && + (validateFunctionsInIntervalOrGroupbyQuery(pQueryInfo) != TSDB_CODE_SUCCESS)) { return TSDB_CODE_INVALID_SQL; } } @@ -5405,25 +5428,25 @@ int32_t doCheckForQuery(SSqlObj* pSql, SQuerySQL* pQuerySql, int32_t index) { // TODO refactor pCmd->count == 1 means sql in stream function if (!tscEmbedded && pCmd->count == 0) { const char* msg = "not support sliding in query"; - return invalidSqlErrMsg(pCmd, msg); + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg); } - getTimestampInUsFromStr(pSliding->z, pSliding->n, &pCmd->pQueryInfo[0].nSlidingTime); + getTimestampInUsFromStr(pSliding->z, pSliding->n, &pQueryInfo->nSlidingTime); if (pMeterMetaInfo->pMeterMeta->precision == TSDB_TIME_PRECISION_MILLI) { - pCmd->pQueryInfo[0].nSlidingTime /= 1000; + pQueryInfo->nSlidingTime /= 1000; } - if (pCmd->pQueryInfo[0].nSlidingTime < tsMinSlidingTime) { - return invalidSqlErrMsg(pCmd, msg3); + if (pQueryInfo->nSlidingTime < tsMinSlidingTime) { + return invalidSqlErrMsg(pQueryInfo->msg, msg3); } - if (pCmd->pQueryInfo[0].nSlidingTime > pCmd->pQueryInfo[0].nAggTimeInterval) { - return invalidSqlErrMsg(pCmd, msg4); + if (pQueryInfo->nSlidingTime > pQueryInfo->nAggTimeInterval) { + return invalidSqlErrMsg(pQueryInfo->msg, msg4); } } // set order by info - if (parseOrderbyClause(pCmd, pQuerySql, tsGetSchema(pMeterMetaInfo->pMeterMeta), + if (parseOrderbyClause(pQueryInfo, pQuerySql, tsGetSchema(pMeterMetaInfo->pMeterMeta), pMeterMetaInfo->pMeterMeta->numOfColumns) != TSDB_CODE_SUCCESS) { return TSDB_CODE_INVALID_SQL; } @@ -5437,75 +5460,77 @@ int32_t doCheckForQuery(SSqlObj* pSql, SQuerySQL* pQuerySql, int32_t index) { pQuerySql->pWhere = NULL; if (pMeterMetaInfo->pMeterMeta->precision == TSDB_TIME_PRECISION_MILLI) { - pCmd->pQueryInfo[0].stime = pCmd->pQueryInfo[0].stime / 1000; - pCmd->pQueryInfo[0].etime = pCmd->pQueryInfo[0].etime / 1000; + pQueryInfo->stime = pQueryInfo->stime / 1000; + pQueryInfo->etime = pQueryInfo->etime / 1000; } } else { // set the time rang - pCmd->pQueryInfo[0].stime = 0; - pCmd->pQueryInfo[0].etime = INT64_MAX; + pQueryInfo->stime = 0; + pQueryInfo->etime = INT64_MAX; } // user does not specified the query time window, twa is not allowed in such case. - if ((pCmd->pQueryInfo[0].stime == 0 || pCmd->pQueryInfo[0].etime == INT64_MAX || - (pCmd->pQueryInfo[0].etime == INT64_MAX / 1000 && pMeterMetaInfo->pMeterMeta->precision == TSDB_TIME_PRECISION_MILLI)) && - tscIsTWAQuery(pCmd)) { - return invalidSqlErrMsg(pCmd, msg9); + if ((pQueryInfo->stime == 0 || pQueryInfo->etime == INT64_MAX || + (pQueryInfo->etime == INT64_MAX / 1000 && + pMeterMetaInfo->pMeterMeta->precision == TSDB_TIME_PRECISION_MILLI)) && + tscIsTWAQuery(pQueryInfo)) { + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg9); } // no result due to invalid query time range - if (pCmd->pQueryInfo[0].stime > pCmd->pQueryInfo[0].etime) { + if (pQueryInfo->stime > pQueryInfo->etime) { pCmd->command = TSDB_SQL_RETRIEVE_EMPTY_RESULT; return TSDB_CODE_SUCCESS; } - if (!hasTimestampForPointInterpQuery(pCmd)) { - return invalidSqlErrMsg(pCmd, msg2); + if (!hasTimestampForPointInterpQuery(pQueryInfo)) { + return invalidSqlErrMsg(pQueryInfo->msg, msg2); } if (pQuerySql->fillType != NULL) { - if (pCmd->pQueryInfo[0].nAggTimeInterval == 0 && (!tscIsPointInterpQuery(pCmd))) { - return invalidSqlErrMsg(pCmd, msg5); + if (pQueryInfo->nAggTimeInterval == 0 && (!tscIsPointInterpQuery(pQueryInfo))) { + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg5); } - if (pCmd->pQueryInfo[0].nAggTimeInterval > 0) { - int64_t timeRange = labs(pCmd->pQueryInfo[0].stime - pCmd->pQueryInfo[0].etime); + if (pQueryInfo->nAggTimeInterval > 0) { + int64_t timeRange = labs(pQueryInfo->stime - pQueryInfo->etime); // number of result is not greater than 10,000,000 - if ((timeRange == 0) || (timeRange / pCmd->pQueryInfo[0].nAggTimeInterval) > MAX_RETRIEVE_ROWS_IN_INTERVAL_QUERY) { - return invalidSqlErrMsg(pCmd, msg6); + if ((timeRange == 0) || + (timeRange / pQueryInfo->nAggTimeInterval) > MAX_RETRIEVE_ROWS_IN_INTERVAL_QUERY) { + return invalidSqlErrMsg(pQueryInfo->msg, msg6); } } - int32_t ret = parseFillClause(pCmd, pQuerySql); + int32_t ret = parseFillClause(pQueryInfo, pQuerySql); if (ret != TSDB_CODE_SUCCESS) { return ret; } } // in case of join query, time range is required. - if (QUERY_IS_JOIN_QUERY(pCmd->type)) { - int64_t timeRange = labs(pCmd->pQueryInfo[0].stime - pCmd->pQueryInfo[0].etime); + if (QUERY_IS_JOIN_QUERY(pQueryInfo->type)) { + int64_t timeRange = labs(pQueryInfo->stime - pQueryInfo->etime); - if (timeRange == 0 && pCmd->pQueryInfo[0].stime == 0) { - return invalidSqlErrMsg(pCmd, msg6); + if (timeRange == 0 && pQueryInfo->stime == 0) { + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg6); } } // handle the limit offset value, validate the limit - pCmd->pQueryInfo->limit = pQuerySql->limit; + pQueryInfo->limit = pQuerySql->limit; // temporarily save the original limitation value - if ((code = parseLimitClause(pSql, pQuerySql)) != TSDB_CODE_SUCCESS) { + if ((code = parseLimitClause(pSql, 0, pQuerySql)) != TSDB_CODE_SUCCESS) { return code; } - if ((code = doFunctionsCompatibleCheck(pSql)) != TSDB_CODE_SUCCESS) { + if ((code = doFunctionsCompatibleCheck(pCmd, pQueryInfo)) != TSDB_CODE_SUCCESS) { return code; } - setColumnOffsetValueInResultset(pCmd); + setColumnOffsetValueInResultset(pQueryInfo); - for (int32_t i = 0; i < pCmd->pQueryInfo->numOfTables; ++i) { - updateTagColumnIndex(pCmd, i); + for (int32_t i = 0; i < pQueryInfo->numOfTables; ++i) { + updateTagColumnIndex(pQueryInfo, i); } return TSDB_CODE_SUCCESS; // Does not build query message here diff --git a/src/client/src/tscSecondaryMerge.c b/src/client/src/tscSecondaryMerge.c index e499486e624507e4cffb534a5a86d872736f5c37..ebf1a7a8b7d5742b3719f7fcd2a55b76deafc835 100644 --- a/src/client/src/tscSecondaryMerge.c +++ b/src/client/src/tscSecondaryMerge.c @@ -58,12 +58,13 @@ static void tscInitSqlContext(SSqlCmd *pCmd, SSqlRes *pRes, SLocalReducer *pRedu * the fields and offset attributes in pCmd and pModel may be different due to * merge requirement. So, the final result in pRes structure is formatted in accordance with the pCmd object. */ - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { SQLFunctionCtx *pCtx = &pReducer->pCtx[i]; - pCtx->aOutputBuf = pReducer->pResultBuf->data + tscFieldInfoGetOffset(pCmd, i) * pReducer->resColModel->maxCapacity; - pCtx->order = pCmd->order.order; - pCtx->functionId = pCmd->pQueryInfo[0].exprsInfo.pExprs[i].functionId; + pCtx->aOutputBuf = pReducer->pResultBuf->data + tscFieldInfoGetOffset(pQueryInfo, i) * pReducer->resColModel->maxCapacity; + pCtx->order = pQueryInfo->order.order; + pCtx->functionId = pQueryInfo->exprsInfo.pExprs[i].functionId; // input buffer hold only one point data pCtx->aInputElemBuf = pReducer->pTempBuffer->data + pDesc->pSchema->colOffset[i]; @@ -72,7 +73,7 @@ static void tscInitSqlContext(SSqlCmd *pCmd, SSqlRes *pRes, SLocalReducer *pRedu pCtx->inputType = pDesc->pSchema->pFields[i].type; pCtx->inputBytes = pDesc->pSchema->pFields[i].bytes; - TAOS_FIELD *pField = tscFieldInfoGetField(pCmd, i); + TAOS_FIELD *pField = tscFieldInfoGetField(pQueryInfo, i); // output data format yet comes from pCmd. pCtx->outputBytes = pField->bytes; pCtx->outputType = pField->type; @@ -84,15 +85,15 @@ static void tscInitSqlContext(SSqlCmd *pCmd, SSqlRes *pRes, SLocalReducer *pRedu pRes->bytes[i] = pField->bytes; - SSqlExpr *pExpr = tscSqlExprGet(pCmd, i); + SSqlExpr *pExpr = tscSqlExprGet(pQueryInfo, i); // for top/bottom function, the output of timestamp is the first column int32_t functionId = pExpr->functionId; if (functionId == TSDB_FUNC_TOP || functionId == TSDB_FUNC_BOTTOM) { pCtx->ptsOutputBuf = pReducer->pCtx[0].aOutputBuf; - pCtx->param[2].i64Key = pCmd->order.order; + pCtx->param[2].i64Key = pQueryInfo->order.order; pCtx->param[2].nType = TSDB_DATA_TYPE_BIGINT; - pCtx->param[1].i64Key = pCmd->order.orderColId; + pCtx->param[1].i64Key = pQueryInfo->order.orderColId; } SResultInfo *pResInfo = &pReducer->pResInfo[i]; @@ -105,11 +106,11 @@ static void tscInitSqlContext(SSqlCmd *pCmd, SSqlRes *pRes, SLocalReducer *pRedu int16_t n = 0; int16_t tagLen = 0; - SQLFunctionCtx** pTagCtx = calloc(pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols, POINTER_BYTES); + SQLFunctionCtx** pTagCtx = calloc(pQueryInfo->fieldsInfo.numOfOutputCols, POINTER_BYTES); SQLFunctionCtx* pCtx = NULL; - for(int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - SSqlExpr *pExpr = tscSqlExprGet(pCmd, i); + for(int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + SSqlExpr *pExpr = tscSqlExprGet(pQueryInfo, i); if (pExpr->functionId == TSDB_FUNC_TAG_DUMMY || pExpr->functionId == TSDB_FUNC_TS_DUMMY) { tagLen += pExpr->resBytes; pTagCtx[n++] = &pReducer->pCtx[i]; @@ -214,7 +215,9 @@ void tscCreateLocalReducer(tExtMemBuffer **pMemBuffer, int32_t numOfBuffer, tOrd #ifdef _DEBUG_VIEW printf("load data page into mem for build loser tree: %ld rows\n", pDS->filePage.numOfElems); SSrcColumnInfo colInfo[256] = {0}; - tscGetSrcColumnInfo(colInfo, pCmd); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + + tscGetSrcColumnInfo(colInfo, pQueryInfo); tColModelDisplayEx(pDesc->pSchema, pDS->filePage.data, pDS->filePage.numOfElems, pMemBuffer[0]->numOfElemsPerPage, colInfo); @@ -238,7 +241,9 @@ void tscCreateLocalReducer(tExtMemBuffer **pMemBuffer, int32_t numOfBuffer, tOrd param->pLocalData = pReducer->pLocalDataSrc; param->pDesc = pReducer->pDesc; param->numOfElems = pReducer->pLocalDataSrc[0]->pMemBuffer->numOfElemsPerPage; - param->groupOrderType = pCmd->pQueryInfo[0].groupbyExpr.orderType; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + + param->groupOrderType = pQueryInfo->groupbyExpr.orderType; pRes->code = tLoserTreeCreate(&pReducer->pLoserTree, pReducer->numOfBuffer, param, treeComparator); if (pReducer->pLoserTree == NULL || pRes->code != 0) { @@ -247,12 +252,12 @@ void tscCreateLocalReducer(tExtMemBuffer **pMemBuffer, int32_t numOfBuffer, tOrd // the input data format follows the old format, but output in a new format. // so, all the input must be parsed as old format - pReducer->pCtx = (SQLFunctionCtx *)calloc(pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols, sizeof(SQLFunctionCtx)); + pReducer->pCtx = (SQLFunctionCtx *)calloc(pQueryInfo->fieldsInfo.numOfOutputCols, sizeof(SQLFunctionCtx)); pReducer->rowSize = pMemBuffer[0]->nElemSize; - tscRestoreSQLFunctionForMetricQuery(pCmd); - tscFieldInfoCalOffset(pCmd); + tscRestoreSQLFunctionForMetricQuery(pQueryInfo); + tscFieldInfoCalOffset(pQueryInfo); if (pReducer->rowSize > pMemBuffer[0]->nPageSize) { assert(false); // todo fixed row size is larger than the minimum page size; @@ -272,7 +277,7 @@ void tscCreateLocalReducer(tExtMemBuffer **pMemBuffer, int32_t numOfBuffer, tOrd pReducer->nResultBufSize = pMemBuffer[0]->nPageSize * 16; pReducer->pResultBuf = (tFilePage *)calloc(1, pReducer->nResultBufSize + sizeof(tFilePage)); - int32_t finalRowLength = tscGetResRowLength(pCmd); + int32_t finalRowLength = tscGetResRowLength(pQueryInfo); pReducer->resColModel = finalmodel; pReducer->resColModel->maxCapacity = pReducer->nResultBufSize / finalRowLength; assert(finalRowLength <= pReducer->rowSize); @@ -294,33 +299,33 @@ void tscCreateLocalReducer(tExtMemBuffer **pMemBuffer, int32_t numOfBuffer, tOrd } pReducer->pTempBuffer->numOfElems = 0; - pReducer->pResInfo = calloc((size_t)pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols, sizeof(SResultInfo)); + pReducer->pResInfo = calloc((size_t)pQueryInfo->fieldsInfo.numOfOutputCols, sizeof(SResultInfo)); - tscCreateResPointerInfo(pCmd, pRes); + tscCreateResPointerInfo(pQueryInfo, pRes); tscInitSqlContext(pCmd, pRes, pReducer, pDesc); // we change the maxCapacity of schema to denote that there is only one row in temp buffer pReducer->pDesc->pSchema->maxCapacity = 1; - pReducer->offset = pCmd->pQueryInfo->limit.offset; + pReducer->offset = pQueryInfo->limit.offset; pRes->pLocalReducer = pReducer; pRes->numOfGroups = 0; - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0); int16_t prec = pMeterMetaInfo->pMeterMeta->precision; - int64_t stime = (pCmd->pQueryInfo[0].stime < pCmd->pQueryInfo[0].etime) ? pCmd->pQueryInfo[0].stime : pCmd->pQueryInfo[0].etime; - int64_t revisedSTime = taosGetIntervalStartTimestamp(stime, pCmd->pQueryInfo[0].nAggTimeInterval, pCmd->pQueryInfo[0].intervalTimeUnit, prec); + int64_t stime = (pQueryInfo->stime < pQueryInfo->etime) ? pQueryInfo->stime : pQueryInfo->etime; + int64_t revisedSTime = taosGetIntervalStartTimestamp(stime, pQueryInfo->nAggTimeInterval, pQueryInfo->intervalTimeUnit, prec); SInterpolationInfo *pInterpoInfo = &pReducer->interpolationInfo; - taosInitInterpoInfo(pInterpoInfo, pCmd->order.order, revisedSTime, pCmd->pQueryInfo[0].groupbyExpr.numOfGroupCols, + taosInitInterpoInfo(pInterpoInfo, pQueryInfo->order.order, revisedSTime, pQueryInfo->groupbyExpr.numOfGroupCols, pReducer->rowSize); - int32_t startIndex = pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols - pCmd->pQueryInfo[0].groupbyExpr.numOfGroupCols; + int32_t startIndex = pQueryInfo->fieldsInfo.numOfOutputCols - pQueryInfo->groupbyExpr.numOfGroupCols; - if (pCmd->pQueryInfo[0].groupbyExpr.numOfGroupCols > 0) { - pInterpoInfo->pTags[0] = (char *)pInterpoInfo->pTags + POINTER_BYTES * pCmd->pQueryInfo[0].groupbyExpr.numOfGroupCols; - for (int32_t i = 1; i < pCmd->pQueryInfo[0].groupbyExpr.numOfGroupCols; ++i) { + if (pQueryInfo->groupbyExpr.numOfGroupCols > 0) { + pInterpoInfo->pTags[0] = (char *)pInterpoInfo->pTags + POINTER_BYTES * pQueryInfo->groupbyExpr.numOfGroupCols; + for (int32_t i = 1; i < pQueryInfo->groupbyExpr.numOfGroupCols; ++i) { pInterpoInfo->pTags[i] = pReducer->resColModel->pFields[startIndex + i - 1].bytes + pInterpoInfo->pTags[i - 1]; } } else { @@ -429,7 +434,8 @@ void tscDestroyLocalReducer(SSqlObj *pSql) { } SSqlCmd *pCmd = &pSql->cmd; - + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + // there is no more result, so we release all allocated resource SLocalReducer *pLocalReducer = (SLocalReducer*)atomic_exchange_ptr(&pRes->pLocalReducer, NULL); if (pLocalReducer != NULL) { @@ -444,7 +450,7 @@ void tscDestroyLocalReducer(SSqlObj *pSql) { tfree(pLocalReducer->interpolationInfo.pTags); if (pLocalReducer->pCtx != NULL) { - for(int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { + for(int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { SQLFunctionCtx *pCtx = &pLocalReducer->pCtx[i]; tVariantDestroy(&pCtx->tag); } @@ -459,7 +465,7 @@ void tscDestroyLocalReducer(SSqlObj *pSql) { tfree(pLocalReducer->pResultBuf); if (pLocalReducer->pResInfo != NULL) { - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { tfree(pLocalReducer->pResInfo[i].interResultBuf); } @@ -494,12 +500,14 @@ void tscDestroyLocalReducer(SSqlObj *pSql) { static int32_t createOrderDescriptor(tOrderDescriptor **pOrderDesc, SSqlCmd *pCmd, tColModel *pModel) { int32_t numOfGroupByCols = 0; - if (pCmd->pQueryInfo[0].groupbyExpr.numOfGroupCols > 0) { - numOfGroupByCols = pCmd->pQueryInfo[0].groupbyExpr.numOfGroupCols; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + + if (pQueryInfo->groupbyExpr.numOfGroupCols > 0) { + numOfGroupByCols = pQueryInfo->groupbyExpr.numOfGroupCols; } // primary timestamp column is involved in final result - if (pCmd->pQueryInfo[0].nAggTimeInterval != 0) { + if (pQueryInfo->nAggTimeInterval != 0) { numOfGroupByCols++; } @@ -509,20 +517,20 @@ static int32_t createOrderDescriptor(tOrderDescriptor **pOrderDesc, SSqlCmd *pCm } if (numOfGroupByCols > 0) { - int32_t startCols = pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols - pCmd->pQueryInfo[0].groupbyExpr.numOfGroupCols; + int32_t startCols = pQueryInfo->fieldsInfo.numOfOutputCols - pQueryInfo->groupbyExpr.numOfGroupCols; // tags value locate at the last columns - for (int32_t i = 0; i < pCmd->pQueryInfo[0].groupbyExpr.numOfGroupCols; ++i) { + for (int32_t i = 0; i < pQueryInfo->groupbyExpr.numOfGroupCols; ++i) { orderIdx[i] = startCols++; } - if (pCmd->pQueryInfo[0].nAggTimeInterval != 0) { + if (pQueryInfo->nAggTimeInterval != 0) { // the first column is the timestamp, handles queries like "interval(10m) group by tags" orderIdx[numOfGroupByCols - 1] = PRIMARYKEY_TIMESTAMP_COL_INDEX; } } - *pOrderDesc = tOrderDesCreate(orderIdx, numOfGroupByCols, pModel, pCmd->order.order); + *pOrderDesc = tOrderDesCreate(orderIdx, numOfGroupByCols, pModel, pQueryInfo->order.order); tfree(orderIdx); if (*pOrderDesc == NULL) { @@ -533,7 +541,9 @@ static int32_t createOrderDescriptor(tOrderDescriptor **pOrderDesc, SSqlCmd *pCm } bool isSameGroup(SSqlCmd *pCmd, SLocalReducer *pReducer, char *pPrev, tFilePage *tmpBuffer) { - int16_t functionId = tscSqlExprGet(pCmd, 0)->functionId; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + + int16_t functionId = tscSqlExprGet(pQueryInfo, 0)->functionId; // disable merge procedure for column projection query if (functionId == TSDB_FUNC_PRJ || functionId == TSDB_FUNC_ARITHM) { @@ -550,10 +560,10 @@ bool isSameGroup(SSqlCmd *pCmd, SLocalReducer *pReducer, char *pPrev, tFilePage if (pOrderDesc->orderIdx.pData[numOfCols - 1] == PRIMARYKEY_TIMESTAMP_COL_INDEX) { //<= 0 // super table interval query - assert(pCmd->pQueryInfo[0].nAggTimeInterval > 0); + assert(pQueryInfo->nAggTimeInterval > 0); pOrderDesc->orderIdx.numOfOrderedCols -= 1; } else { // simple group by query - assert(pCmd->pQueryInfo[0].nAggTimeInterval == 0); + assert(pQueryInfo->nAggTimeInterval == 0); } // only one row exists @@ -572,7 +582,8 @@ int32_t tscLocalReducerEnvCreate(SSqlObj *pSql, tExtMemBuffer ***pMemBuffer, tOr tColModel *pModel = NULL; *pFinalModel = NULL; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); (*pMemBuffer) = (tExtMemBuffer **)malloc(POINTER_BYTES * pMeterMetaInfo->pMetricMeta->numOfVnodes); if (*pMemBuffer == NULL) { @@ -581,7 +592,7 @@ int32_t tscLocalReducerEnvCreate(SSqlObj *pSql, tExtMemBuffer ***pMemBuffer, tOr return pRes->code; } - pSchema = (SSchema *)calloc(1, sizeof(SSchema) * pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols); + pSchema = (SSchema *)calloc(1, sizeof(SSchema) * pQueryInfo->fieldsInfo.numOfOutputCols); if (pSchema == NULL) { tscError("%p failed to allocate memory", pSql); pRes->code = TSDB_CODE_CLI_OUT_OF_MEMORY; @@ -589,8 +600,8 @@ int32_t tscLocalReducerEnvCreate(SSqlObj *pSql, tExtMemBuffer ***pMemBuffer, tOr } int32_t rlen = 0; - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - SSqlExpr *pExpr = tscSqlExprGet(pCmd, i); + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + SSqlExpr *pExpr = tscSqlExprGet(pQueryInfo, i); pSchema[i].bytes = pExpr->resBytes; pSchema[i].type = pExpr->resType; @@ -599,7 +610,7 @@ int32_t tscLocalReducerEnvCreate(SSqlObj *pSql, tExtMemBuffer ***pMemBuffer, tOr } int32_t capacity = nBufferSizes / rlen; - pModel = tColModelCreate(pSchema, pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols, capacity); + pModel = tColModelCreate(pSchema, pQueryInfo->fieldsInfo.numOfOutputCols, capacity); for (int32_t i = 0; i < pMeterMetaInfo->pMetricMeta->numOfVnodes; ++i) { char tmpPath[512] = {0}; @@ -615,16 +626,16 @@ int32_t tscLocalReducerEnvCreate(SSqlObj *pSql, tExtMemBuffer ***pMemBuffer, tOr return pRes->code; } - memset(pSchema, 0, sizeof(SSchema) * pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols); - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - TAOS_FIELD *pField = tscFieldInfoGetField(pCmd, i); + memset(pSchema, 0, sizeof(SSchema) * pQueryInfo->fieldsInfo.numOfOutputCols); + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + TAOS_FIELD *pField = tscFieldInfoGetField(pQueryInfo, i); pSchema[i].type = pField->type; pSchema[i].bytes = pField->bytes; strcpy(pSchema[i].name, pField->name); } - *pFinalModel = tColModelCreate(pSchema, pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols, capacity); + *pFinalModel = tColModelCreate(pSchema, pQueryInfo->fieldsInfo.numOfOutputCols, capacity); tfree(pSchema); return TSDB_CODE_SUCCESS; @@ -714,15 +725,15 @@ void adjustLoserTreeFromNewData(SLocalReducer *pLocalReducer, SLocalDataSource * } } -void savePrevRecordAndSetupInterpoInfo(SLocalReducer *pLocalReducer, SSqlCmd *pCmd, SInterpolationInfo *pInterpoInfo) { +void savePrevRecordAndSetupInterpoInfo(SLocalReducer *pLocalReducer, SQueryInfo* pQueryInfo, SInterpolationInfo *pInterpoInfo) { // discard following dataset in the same group and reset the interpolation information - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); int16_t prec = pMeterMetaInfo->pMeterMeta->precision; - int64_t stime = (pCmd->pQueryInfo[0].stime < pCmd->pQueryInfo[0].etime) ? pCmd->pQueryInfo[0].stime : pCmd->pQueryInfo[0].etime; - int64_t revisedSTime = taosGetIntervalStartTimestamp(stime, pCmd->pQueryInfo[0].nAggTimeInterval, pCmd->pQueryInfo[0].intervalTimeUnit, prec); + int64_t stime = (pQueryInfo->stime < pQueryInfo->etime) ? pQueryInfo->stime : pQueryInfo->etime; + int64_t revisedSTime = taosGetIntervalStartTimestamp(stime, pQueryInfo->nAggTimeInterval, pQueryInfo->intervalTimeUnit, prec); - taosInitInterpoInfo(pInterpoInfo, pCmd->order.order, revisedSTime, pCmd->pQueryInfo[0].groupbyExpr.numOfGroupCols, + taosInitInterpoInfo(pInterpoInfo, pQueryInfo->order.order, revisedSTime, pQueryInfo->groupbyExpr.numOfGroupCols, pLocalReducer->rowSize); pLocalReducer->discard = true; @@ -733,11 +744,12 @@ void savePrevRecordAndSetupInterpoInfo(SLocalReducer *pLocalReducer, SSqlCmd *pC } // todo merge with following function -static void reversedCopyResultToDstBuf(SSqlCmd *pCmd, SSqlRes *pRes, tFilePage *pFinalDataPage) { - for (int32_t i = 0; i < pCmd->pQueryInfo[0].exprsInfo.numOfExprs; ++i) { - TAOS_FIELD *pField = tscFieldInfoGetField(pCmd, i); +static void reversedCopyResultToDstBuf(SQueryInfo* pQueryInfo, SSqlRes *pRes, tFilePage *pFinalDataPage) { + + for (int32_t i = 0; i < pQueryInfo->exprsInfo.numOfExprs; ++i) { + TAOS_FIELD *pField = tscFieldInfoGetField(pQueryInfo, i); - int32_t offset = tscFieldInfoGetOffset(pCmd, i); + int32_t offset = tscFieldInfoGetOffset(pQueryInfo, i); char * src = pFinalDataPage->data + (pRes->numOfRows - 1) * pField->bytes + pRes->numOfRows * offset; char * dst = pRes->data + pRes->numOfRows * offset; @@ -749,12 +761,11 @@ static void reversedCopyResultToDstBuf(SSqlCmd *pCmd, SSqlRes *pRes, tFilePage * } } -static void reversedCopyFromInterpolationToDstBuf(SSqlCmd *pCmd, SSqlRes *pRes, tFilePage **pResPages, - SLocalReducer *pLocalReducer) { - for (int32_t i = 0; i < pCmd->pQueryInfo[0].exprsInfo.numOfExprs; ++i) { - TAOS_FIELD *pField = tscFieldInfoGetField(pCmd, i); +static void reversedCopyFromInterpolationToDstBuf(SQueryInfo* pQueryInfo, SSqlRes *pRes, tFilePage **pResPages, SLocalReducer *pLocalReducer) { + for (int32_t i = 0; i < pQueryInfo->exprsInfo.numOfExprs; ++i) { + TAOS_FIELD *pField = tscFieldInfoGetField(pQueryInfo, i); - int32_t offset = tscFieldInfoGetOffset(pCmd, i); + int32_t offset = tscFieldInfoGetOffset(pQueryInfo, i); assert(offset == pLocalReducer->resColModel->colOffset[i]); char *src = pResPages[i]->data + (pRes->numOfRows - 1) * pField->bytes; @@ -776,7 +787,8 @@ static void doInterpolateResult(SSqlObj *pSql, SLocalReducer *pLocalReducer, boo SSqlCmd * pCmd = &pSql->cmd; SSqlRes * pRes = &pSql->res; tFilePage *pFinalDataPage = pLocalReducer->pResultBuf; - + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + if (pRes->pLocalReducer != pLocalReducer) { /* * Release the SSqlObj is called, and it is int destroying function invoked by other thread. @@ -786,100 +798,101 @@ static void doInterpolateResult(SSqlObj *pSql, SLocalReducer *pLocalReducer, boo assert(pRes->pLocalReducer == NULL); } - if (pCmd->pQueryInfo[0].nAggTimeInterval == 0 || pCmd->pQueryInfo[0].interpoType == TSDB_INTERPO_NONE) { + if (pQueryInfo->nAggTimeInterval == 0 || pQueryInfo->interpoType == TSDB_INTERPO_NONE) { // no interval query, no interpolation pRes->data = pLocalReducer->pFinalRes; pRes->numOfRows = pFinalDataPage->numOfElems; pRes->numOfTotal += pRes->numOfRows; - if (pCmd->pQueryInfo->limit.offset > 0) { - if (pCmd->pQueryInfo->limit.offset < pRes->numOfRows) { + if (pQueryInfo->limit.offset > 0) { + if (pQueryInfo->limit.offset < pRes->numOfRows) { int32_t prevSize = pFinalDataPage->numOfElems; - tColModelErase(pLocalReducer->resColModel, pFinalDataPage, prevSize, 0, pCmd->pQueryInfo->limit.offset - 1); + tColModelErase(pLocalReducer->resColModel, pFinalDataPage, prevSize, 0, pQueryInfo->limit.offset - 1); /* remove the hole in column model */ tColModelCompact(pLocalReducer->resColModel, pFinalDataPage, prevSize); - pRes->numOfRows -= pCmd->pQueryInfo->limit.offset; - pRes->numOfTotal -= pCmd->pQueryInfo->limit.offset; - pCmd->pQueryInfo->limit.offset = 0; + pRes->numOfRows -= pQueryInfo->limit.offset; + pRes->numOfTotal -= pQueryInfo->limit.offset; + pQueryInfo->limit.offset = 0; } else { - pCmd->pQueryInfo->limit.offset -= pRes->numOfRows; + pQueryInfo->limit.offset -= pRes->numOfRows; pRes->numOfRows = 0; pRes->numOfTotal = 0; } } - if (pCmd->pQueryInfo->limit.limit >= 0 && pRes->numOfTotal > pCmd->pQueryInfo->limit.limit) { + if (pQueryInfo->limit.limit >= 0 && pRes->numOfTotal > pQueryInfo->limit.limit) { /* impose the limitation of output rows on the final result */ int32_t prevSize = pFinalDataPage->numOfElems; - int32_t overFlow = pRes->numOfTotal - pCmd->pQueryInfo->limit.limit; + int32_t overFlow = pRes->numOfTotal - pQueryInfo->limit.limit; assert(overFlow < pRes->numOfRows); - pRes->numOfTotal = pCmd->pQueryInfo->limit.limit; + pRes->numOfTotal = pQueryInfo->limit.limit; pRes->numOfRows -= overFlow; pFinalDataPage->numOfElems -= overFlow; tColModelCompact(pLocalReducer->resColModel, pFinalDataPage, prevSize); /* set remain data to be discarded, and reset the interpolation information */ - savePrevRecordAndSetupInterpoInfo(pLocalReducer, pCmd, &pLocalReducer->interpolationInfo); + savePrevRecordAndSetupInterpoInfo(pLocalReducer, pQueryInfo, &pLocalReducer->interpolationInfo); } - int32_t rowSize = tscGetResRowLength(pCmd); + int32_t rowSize = tscGetResRowLength(pQueryInfo); // handle the descend order output - if (pCmd->order.order == TSQL_SO_ASC) { + if (pQueryInfo->order.order == TSQL_SO_ASC) { memcpy(pRes->data, pFinalDataPage->data, pRes->numOfRows * rowSize); } else { - reversedCopyResultToDstBuf(pCmd, pRes, pFinalDataPage); + reversedCopyResultToDstBuf(pQueryInfo, pRes, pFinalDataPage); } pFinalDataPage->numOfElems = 0; return; } - int64_t * pPrimaryKeys = (int64_t *)pLocalReducer->pBufForInterpo; + int64_t *pPrimaryKeys = (int64_t *)pLocalReducer->pBufForInterpo; + SInterpolationInfo *pInterpoInfo = &pLocalReducer->interpolationInfo; - int64_t actualETime = (pCmd->pQueryInfo[0].stime < pCmd->pQueryInfo[0].etime) ? pCmd->pQueryInfo[0].etime : pCmd->pQueryInfo[0].stime; + int64_t actualETime = (pQueryInfo->stime < pQueryInfo->etime) ? pQueryInfo->etime : pQueryInfo->stime; - tFilePage **pResPages = malloc(POINTER_BYTES * pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols); - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - TAOS_FIELD *pField = tscFieldInfoGetField(pCmd, i); + tFilePage **pResPages = malloc(POINTER_BYTES * pQueryInfo->fieldsInfo.numOfOutputCols); + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + TAOS_FIELD *pField = tscFieldInfoGetField(pQueryInfo, i); pResPages[i] = calloc(1, sizeof(tFilePage) + pField->bytes * pLocalReducer->resColModel->maxCapacity); } - char ** srcData = (char **)malloc((POINTER_BYTES + sizeof(int32_t)) * pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols); - int32_t *functions = (int32_t *)((char *)srcData + pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols * sizeof(void *)); + char ** srcData = (char **)malloc((POINTER_BYTES + sizeof(int32_t)) * pQueryInfo->fieldsInfo.numOfOutputCols); + int32_t *functions = (int32_t *)((char *)srcData + pQueryInfo->fieldsInfo.numOfOutputCols * sizeof(void *)); - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - srcData[i] = pLocalReducer->pBufForInterpo + tscFieldInfoGetOffset(pCmd, i) * pInterpoInfo->numOfRawDataInRows; - functions[i] = tscSqlExprGet(pCmd, i)->functionId; + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + srcData[i] = pLocalReducer->pBufForInterpo + tscFieldInfoGetOffset(pQueryInfo, i) * pInterpoInfo->numOfRawDataInRows; + functions[i] = tscSqlExprGet(pQueryInfo, i)->functionId; } - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0); int8_t precision = pMeterMetaInfo->pMeterMeta->precision; while (1) { int32_t remains = taosNumOfRemainPoints(pInterpoInfo); - TSKEY etime = taosGetRevisedEndKey(actualETime, pCmd->order.order, pCmd->pQueryInfo[0].nAggTimeInterval, pCmd->pQueryInfo[0].intervalTimeUnit, + TSKEY etime = taosGetRevisedEndKey(actualETime, pQueryInfo->order.order, pQueryInfo->nAggTimeInterval, pQueryInfo->intervalTimeUnit, precision); - int32_t nrows = taosGetNumOfResultWithInterpo(pInterpoInfo, pPrimaryKeys, remains, pCmd->pQueryInfo[0].nAggTimeInterval, etime, + int32_t nrows = taosGetNumOfResultWithInterpo(pInterpoInfo, pPrimaryKeys, remains, pQueryInfo->nAggTimeInterval, etime, pLocalReducer->resColModel->maxCapacity); - int32_t newRows = taosDoInterpoResult(pInterpoInfo, pCmd->pQueryInfo[0].interpoType, pResPages, remains, nrows, - pCmd->pQueryInfo[0].nAggTimeInterval, pPrimaryKeys, pLocalReducer->resColModel, srcData, - pCmd->pQueryInfo[0].defaultVal, functions, pLocalReducer->resColModel->maxCapacity); + int32_t newRows = taosDoInterpoResult(pInterpoInfo, pQueryInfo->interpoType, pResPages, remains, nrows, + pQueryInfo->nAggTimeInterval, pPrimaryKeys, pLocalReducer->resColModel, srcData, + pQueryInfo->defaultVal, functions, pLocalReducer->resColModel->maxCapacity); assert(newRows <= nrows); - if (pCmd->pQueryInfo->limit.offset < newRows) { - newRows -= pCmd->pQueryInfo->limit.offset; + if (pQueryInfo->limit.offset < newRows) { + newRows -= pQueryInfo->limit.offset; - if (pCmd->pQueryInfo->limit.offset > 0) { - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - TAOS_FIELD *pField = tscFieldInfoGetField(pCmd, i); - memmove(pResPages[i]->data, pResPages[i]->data + pField->bytes * pCmd->pQueryInfo->limit.offset, newRows * pField->bytes); + if (pQueryInfo->limit.offset > 0) { + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + TAOS_FIELD *pField = tscFieldInfoGetField(pQueryInfo, i); + memmove(pResPages[i]->data, pResPages[i]->data + pField->bytes * pQueryInfo->limit.offset, newRows * pField->bytes); } } @@ -887,10 +900,10 @@ static void doInterpolateResult(SSqlObj *pSql, SLocalReducer *pLocalReducer, boo pRes->numOfRows = newRows; pRes->numOfTotal += newRows; - pCmd->pQueryInfo->limit.offset = 0; + pQueryInfo->limit.offset = 0; break; } else { - pCmd->pQueryInfo->limit.offset -= newRows; + pQueryInfo->limit.offset -= newRows; pRes->numOfRows = 0; int32_t rpoints = taosNumOfRemainPoints(pInterpoInfo); @@ -902,7 +915,7 @@ static void doInterpolateResult(SSqlObj *pSql, SLocalReducer *pLocalReducer, boo /* all output for current group are completed */ int32_t totalRemainRows = - taosGetNumOfResWithoutLimit(pInterpoInfo, pPrimaryKeys, rpoints, pCmd->pQueryInfo[0].nAggTimeInterval, actualETime); + taosGetNumOfResWithoutLimit(pInterpoInfo, pPrimaryKeys, rpoints, pQueryInfo->nAggTimeInterval, actualETime); if (totalRemainRows <= 0) { break; } @@ -911,33 +924,33 @@ static void doInterpolateResult(SSqlObj *pSql, SLocalReducer *pLocalReducer, boo } if (pRes->numOfRows > 0) { - if (pCmd->pQueryInfo->limit.limit >= 0 && pRes->numOfTotal > pCmd->pQueryInfo->limit.limit) { - int32_t overFlow = pRes->numOfTotal - pCmd->pQueryInfo->limit.limit; + if (pQueryInfo->limit.limit >= 0 && pRes->numOfTotal > pQueryInfo->limit.limit) { + int32_t overFlow = pRes->numOfTotal - pQueryInfo->limit.limit; pRes->numOfRows -= overFlow; assert(pRes->numOfRows >= 0); - pRes->numOfTotal = pCmd->pQueryInfo->limit.limit; + pRes->numOfTotal = pQueryInfo->limit.limit; pFinalDataPage->numOfElems -= overFlow; /* set remain data to be discarded, and reset the interpolation information */ - savePrevRecordAndSetupInterpoInfo(pLocalReducer, pCmd, pInterpoInfo); + savePrevRecordAndSetupInterpoInfo(pLocalReducer, pQueryInfo, pInterpoInfo); } - if (pCmd->order.order == TSQL_SO_ASC) { - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - TAOS_FIELD *pField = tscFieldInfoGetField(pCmd, i); + if (pQueryInfo->order.order == TSQL_SO_ASC) { + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + TAOS_FIELD *pField = tscFieldInfoGetField(pQueryInfo, i); memcpy(pRes->data + pLocalReducer->resColModel->colOffset[i] * pRes->numOfRows, pResPages[i]->data, pField->bytes * pRes->numOfRows); } } else { - reversedCopyFromInterpolationToDstBuf(pCmd, pRes, pResPages, pLocalReducer); + reversedCopyFromInterpolationToDstBuf(pQueryInfo, pRes, pResPages, pLocalReducer); } } pFinalDataPage->numOfElems = 0; - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { tfree(pResPages[i]); } tfree(pResPages); @@ -961,8 +974,10 @@ static void savePreviousRow(SLocalReducer *pLocalReducer, tFilePage *tmpBuffer) static void doExecuteSecondaryMerge(SSqlCmd* pCmd, SLocalReducer *pLocalReducer, bool needInit) { // the tag columns need to be set before all functions execution - for(int32_t j = 0; j < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++j) { - SSqlExpr * pExpr = tscSqlExprGet(pCmd, j); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + + for(int32_t j = 0; j < pQueryInfo->fieldsInfo.numOfOutputCols; ++j) { + SSqlExpr * pExpr = tscSqlExprGet(pQueryInfo, j); SQLFunctionCtx *pCtx = &pLocalReducer->pCtx[j]; tVariantAssign(&pCtx->param[0], &pExpr->param[0]); @@ -981,8 +996,8 @@ static void doExecuteSecondaryMerge(SSqlCmd* pCmd, SLocalReducer *pLocalReducer, } } - for (int32_t j = 0; j < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++j) { - int32_t functionId = tscSqlExprGet(pCmd, j)->functionId; + for (int32_t j = 0; j < pQueryInfo->fieldsInfo.numOfOutputCols; ++j) { + int32_t functionId = tscSqlExprGet(pQueryInfo, j)->functionId; if (functionId == TSDB_FUNC_TAG_DUMMY || functionId == TSDB_FUNC_TS_DUMMY) { continue; } @@ -999,11 +1014,11 @@ static void handleUnprocessedRow(SSqlCmd* pCmd, SLocalReducer *pLocalReducer, tF } } -static int64_t getNumOfResultLocal(SSqlCmd *pCmd, SQLFunctionCtx *pCtx) { +static int64_t getNumOfResultLocal(SQueryInfo *pQueryInfo, SQLFunctionCtx *pCtx) { int64_t maxOutput = 0; - - for (int32_t j = 0; j < pCmd->pQueryInfo[0].exprsInfo.numOfExprs; ++j) { - int32_t functionId = tscSqlExprGet(pCmd, j)->functionId; + + for (int32_t j = 0; j < pQueryInfo->exprsInfo.numOfExprs; ++j) { + int32_t functionId = tscSqlExprGet(pQueryInfo, j)->functionId; /* * ts, tag, tagprj function can not decide the output number of current query @@ -1026,10 +1041,10 @@ static int64_t getNumOfResultLocal(SSqlCmd *pCmd, SQLFunctionCtx *pCtx) { * filled with the same result, which is the tags, specified in group by clause * */ -static void fillMultiRowsOfTagsVal(SSqlCmd *pCmd, int32_t numOfRes, SLocalReducer *pLocalReducer) { +static void fillMultiRowsOfTagsVal(SQueryInfo* pQueryInfo, int32_t numOfRes, SLocalReducer *pLocalReducer) { int32_t maxBufSize = 0; // find the max tags column length to prepare the buffer - for (int32_t k = 0; k < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++k) { - SSqlExpr *pExpr = tscSqlExprGet(pCmd, k); + for (int32_t k = 0; k < pQueryInfo->fieldsInfo.numOfOutputCols; ++k) { + SSqlExpr *pExpr = tscSqlExprGet(pQueryInfo, k); if (maxBufSize < pExpr->resBytes && pExpr->functionId == TSDB_FUNC_TAG) { maxBufSize = pExpr->resBytes; } @@ -1038,8 +1053,8 @@ static void fillMultiRowsOfTagsVal(SSqlCmd *pCmd, int32_t numOfRes, SLocalReduce assert(maxBufSize >= 0); char *buf = malloc((size_t) maxBufSize); - for (int32_t k = 0; k < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++k) { - SSqlExpr *pExpr = tscSqlExprGet(pCmd, k); + for (int32_t k = 0; k < pQueryInfo->fieldsInfo.numOfOutputCols; ++k) { + SSqlExpr *pExpr = tscSqlExprGet(pQueryInfo, k); if (pExpr->functionId != TSDB_FUNC_TAG) { continue; } @@ -1059,9 +1074,9 @@ static void fillMultiRowsOfTagsVal(SSqlCmd *pCmd, int32_t numOfRes, SLocalReduce free(buf); } -int32_t finalizeRes(SSqlCmd *pCmd, SLocalReducer *pLocalReducer) { - for (int32_t k = 0; k < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++k) { - SSqlExpr *pExpr = tscSqlExprGet(pCmd, k); +int32_t finalizeRes(SQueryInfo* pQueryInfo, SLocalReducer *pLocalReducer) { + for (int32_t k = 0; k < pQueryInfo->fieldsInfo.numOfOutputCols; ++k) { + SSqlExpr *pExpr = tscSqlExprGet(pQueryInfo, k); aAggs[pExpr->functionId].xFinalize(&pLocalReducer->pCtx[k]); // allow to re-initialize for the next round @@ -1070,10 +1085,10 @@ int32_t finalizeRes(SSqlCmd *pCmd, SLocalReducer *pLocalReducer) { pLocalReducer->hasPrevRow = false; - int32_t numOfRes = (int32_t)getNumOfResultLocal(pCmd, pLocalReducer->pCtx); + int32_t numOfRes = (int32_t)getNumOfResultLocal(pQueryInfo, pLocalReducer->pCtx); pLocalReducer->pResultBuf->numOfElems += numOfRes; - fillMultiRowsOfTagsVal(pCmd, numOfRes, pLocalReducer); + fillMultiRowsOfTagsVal(pQueryInfo, numOfRes, pLocalReducer); return numOfRes; } @@ -1084,9 +1099,9 @@ int32_t finalizeRes(SSqlCmd *pCmd, SLocalReducer *pLocalReducer) { * results generated by simple aggregation function, we merge them all into one points * *Exception*: column projection query, required no merge procedure */ -bool needToMerge(SSqlCmd *pCmd, SLocalReducer *pLocalReducer, tFilePage *tmpBuffer) { +bool needToMerge(SQueryInfo* pQueryInfo, SLocalReducer *pLocalReducer, tFilePage *tmpBuffer) { int32_t ret = 0; // merge all result by default - int16_t functionId = tscSqlExprGet(pCmd, 0)->functionId; + int16_t functionId = tscSqlExprGet(pQueryInfo, 0)->functionId; if (functionId == TSDB_FUNC_PRJ || functionId == TSDB_FUNC_ARITHM) { // column projection query ret = 1; // disable merge procedure @@ -1106,18 +1121,19 @@ bool needToMerge(SSqlCmd *pCmd, SLocalReducer *pLocalReducer, tFilePage *tmpBuff return (ret == 0); } -static bool reachGroupResultLimit(SSqlCmd *pCmd, SSqlRes *pRes) { - return (pRes->numOfGroups >= pCmd->pQueryInfo[0].slimit.limit && pCmd->pQueryInfo[0].slimit.limit >= 0); +static bool reachGroupResultLimit(SQueryInfo* pQueryInfo, SSqlRes *pRes) { + return (pRes->numOfGroups >= pQueryInfo->slimit.limit && pQueryInfo->slimit.limit >= 0); } static bool saveGroupResultInfo(SSqlObj *pSql) { SSqlCmd *pCmd = &pSql->cmd; SSqlRes *pRes = &pSql->res; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); pRes->numOfGroups += 1; // the output group is limited by the slimit clause - if (reachGroupResultLimit(pCmd, pRes)) { + if (reachGroupResultLimit(pQueryInfo, pRes)) { return true; } @@ -1138,6 +1154,8 @@ static bool saveGroupResultInfo(SSqlObj *pSql) { bool doGenerateFinalResults(SSqlObj *pSql, SLocalReducer *pLocalReducer, bool noMoreCurrentGroupRes) { SSqlCmd * pCmd = &pSql->cmd; SSqlRes * pRes = &pSql->res; + + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); tFilePage *pResBuf = pLocalReducer->pResultBuf; tColModel *pModel = pLocalReducer->resColModel; @@ -1147,9 +1165,9 @@ bool doGenerateFinalResults(SSqlObj *pSql, SLocalReducer *pLocalReducer, bool no * ignore the output of the current group since this group is skipped by user * We set the numOfRows to be 0 and discard the possible remain results. */ - if (pCmd->pQueryInfo[0].slimit.offset > 0) { + if (pQueryInfo->slimit.offset > 0) { pRes->numOfRows = 0; - pCmd->pQueryInfo[0].slimit.offset -= 1; + pQueryInfo->slimit.offset -= 1; pLocalReducer->discard = !noMoreCurrentGroupRes; return false; } @@ -1163,24 +1181,24 @@ bool doGenerateFinalResults(SSqlObj *pSql, SLocalReducer *pLocalReducer, bool no #endif SInterpolationInfo *pInterpoInfo = &pLocalReducer->interpolationInfo; - int32_t startIndex = pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols - pCmd->pQueryInfo[0].groupbyExpr.numOfGroupCols; + int32_t startIndex = pQueryInfo->fieldsInfo.numOfOutputCols - pQueryInfo->groupbyExpr.numOfGroupCols; - for (int32_t i = 0; i < pCmd->pQueryInfo[0].groupbyExpr.numOfGroupCols; ++i) { + for (int32_t i = 0; i < pQueryInfo->groupbyExpr.numOfGroupCols; ++i) { memcpy(pInterpoInfo->pTags[i], pLocalReducer->pBufForInterpo + pModel->colOffset[startIndex + i] * pResBuf->numOfElems, pModel->pFields[startIndex + i].bytes); } - taosInterpoSetStartInfo(&pLocalReducer->interpolationInfo, pResBuf->numOfElems, pCmd->pQueryInfo[0].interpoType); + taosInterpoSetStartInfo(&pLocalReducer->interpolationInfo, pResBuf->numOfElems, pQueryInfo->interpoType); doInterpolateResult(pSql, pLocalReducer, noMoreCurrentGroupRes); return true; } -void resetOutputBuf(SSqlCmd *pCmd, SLocalReducer *pLocalReducer) { // reset output buffer to the beginning - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { +void resetOutputBuf(SQueryInfo* pQueryInfo, SLocalReducer *pLocalReducer) { // reset output buffer to the beginning + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { pLocalReducer->pCtx[i].aOutputBuf = - pLocalReducer->pResultBuf->data + tscFieldInfoGetOffset(pCmd, i) * pLocalReducer->resColModel->maxCapacity; + pLocalReducer->pResultBuf->data + tscFieldInfoGetOffset(pQueryInfo, i) * pLocalReducer->resColModel->maxCapacity; } memset(pLocalReducer->pResultBuf, 0, pLocalReducer->nResultBufSize + sizeof(tFilePage)); @@ -1190,17 +1208,19 @@ static void resetEnvForNewResultset(SSqlRes *pRes, SSqlCmd *pCmd, SLocalReducer // In handling data in other groups, we need to reset the interpolation information for a new group data pRes->numOfRows = 0; pRes->numOfTotal = 0; - pCmd->pQueryInfo->limit.offset = pLocalReducer->offset; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + + pQueryInfo->limit.offset = pLocalReducer->offset; - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0); int16_t precision = pMeterMetaInfo->pMeterMeta->precision; // for group result interpolation, do not return if not data is generated - if (pCmd->pQueryInfo[0].interpoType != TSDB_INTERPO_NONE) { - int64_t stime = (pCmd->pQueryInfo[0].stime < pCmd->pQueryInfo[0].etime) ? pCmd->pQueryInfo[0].stime : pCmd->pQueryInfo[0].etime; - int64_t newTime = taosGetIntervalStartTimestamp(stime, pCmd->pQueryInfo[0].nAggTimeInterval, pCmd->pQueryInfo[0].intervalTimeUnit, precision); + if (pQueryInfo->interpoType != TSDB_INTERPO_NONE) { + int64_t stime = (pQueryInfo->stime < pQueryInfo->etime) ? pQueryInfo->stime : pQueryInfo->etime; + int64_t newTime = taosGetIntervalStartTimestamp(stime, pQueryInfo->nAggTimeInterval, pQueryInfo->intervalTimeUnit, precision); - taosInitInterpoInfo(&pLocalReducer->interpolationInfo, pCmd->order.order, newTime, pCmd->pQueryInfo[0].groupbyExpr.numOfGroupCols, + taosInitInterpoInfo(&pLocalReducer->interpolationInfo, pQueryInfo->order.order, newTime, pQueryInfo->groupbyExpr.numOfGroupCols, pLocalReducer->rowSize); } } @@ -1213,22 +1233,23 @@ static bool doInterpolationForCurrentGroup(SSqlObj *pSql) { SSqlCmd *pCmd = &pSql->cmd; SSqlRes *pRes = &pSql->res; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); SLocalReducer * pLocalReducer = pRes->pLocalReducer; SInterpolationInfo *pInterpoInfo = &pLocalReducer->interpolationInfo; - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); int8_t p = pMeterMetaInfo->pMeterMeta->precision; if (taosHasRemainsDataForInterpolation(pInterpoInfo)) { - assert(pCmd->pQueryInfo[0].interpoType != TSDB_INTERPO_NONE); + assert(pQueryInfo->interpoType != TSDB_INTERPO_NONE); tFilePage *pFinalDataBuf = pLocalReducer->pResultBuf; int64_t etime = *(int64_t *)(pFinalDataBuf->data + TSDB_KEYSIZE * (pInterpoInfo->numOfRawDataInRows - 1)); int32_t remain = taosNumOfRemainPoints(pInterpoInfo); - TSKEY ekey = taosGetRevisedEndKey(etime, pCmd->order.order, pCmd->pQueryInfo[0].nAggTimeInterval, pCmd->pQueryInfo[0].intervalTimeUnit, p); + TSKEY ekey = taosGetRevisedEndKey(etime, pQueryInfo->order.order, pQueryInfo->nAggTimeInterval, pQueryInfo->intervalTimeUnit, p); int32_t rows = taosGetNumOfResultWithInterpo(pInterpoInfo, (TSKEY *)pLocalReducer->pBufForInterpo, remain, - pCmd->pQueryInfo[0].nAggTimeInterval, ekey, pLocalReducer->resColModel->maxCapacity); + pQueryInfo->nAggTimeInterval, ekey, pLocalReducer->resColModel->maxCapacity); if (rows > 0) { // do interpo doInterpolateResult(pSql, pLocalReducer, false); } @@ -1248,17 +1269,18 @@ static bool doHandleLastRemainData(SSqlObj *pSql) { bool prevGroupCompleted = (!pLocalReducer->discard) && pLocalReducer->hasUnprocessedRow; - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); int8_t precision = pMeterMetaInfo->pMeterMeta->precision; if ((isAllSourcesCompleted(pLocalReducer) && !pLocalReducer->hasPrevRow) || pLocalReducer->pLocalDataSrc[0] == NULL || prevGroupCompleted) { // if interpoType == TSDB_INTERPO_NONE, return directly - if (pCmd->pQueryInfo[0].interpoType != TSDB_INTERPO_NONE) { - int64_t etime = (pCmd->pQueryInfo[0].stime < pCmd->pQueryInfo[0].etime) ? pCmd->pQueryInfo[0].etime : pCmd->pQueryInfo[0].stime; + if (pQueryInfo->interpoType != TSDB_INTERPO_NONE) { + int64_t etime = (pQueryInfo->stime < pQueryInfo->etime) ? pQueryInfo->etime : pQueryInfo->stime; - etime = taosGetRevisedEndKey(etime, pCmd->order.order, pCmd->pQueryInfo[0].nAggTimeInterval, pCmd->pQueryInfo[0].intervalTimeUnit, precision); - int32_t rows = taosGetNumOfResultWithInterpo(pInterpoInfo, NULL, 0, pCmd->pQueryInfo[0].nAggTimeInterval, etime, + etime = taosGetRevisedEndKey(etime, pQueryInfo->order.order, pQueryInfo->nAggTimeInterval, pQueryInfo->intervalTimeUnit, precision); + int32_t rows = taosGetNumOfResultWithInterpo(pInterpoInfo, NULL, 0, pQueryInfo->nAggTimeInterval, etime, pLocalReducer->resColModel->maxCapacity); if (rows > 0) { // do interpo doInterpolateResult(pSql, pLocalReducer, true); @@ -1289,10 +1311,12 @@ static bool doHandleLastRemainData(SSqlObj *pSql) { static void doMergeWithPrevRows(SSqlObj *pSql, int32_t numOfRes) { SSqlCmd * pCmd = &pSql->cmd; SSqlRes * pRes = &pSql->res; + SLocalReducer *pLocalReducer = pRes->pLocalReducer; - - for (int32_t k = 0; k < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++k) { - SSqlExpr *pExpr = tscSqlExprGet(pCmd, k); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + + for (int32_t k = 0; k < pQueryInfo->fieldsInfo.numOfOutputCols; ++k) { + SSqlExpr *pExpr = tscSqlExprGet(pQueryInfo, k); SQLFunctionCtx *pCtx = &pLocalReducer->pCtx[k]; pCtx->aOutputBuf += pCtx->outputBytes * numOfRes; @@ -1325,7 +1349,8 @@ int32_t tscLocalDoReduce(SSqlObj *pSql) { pRes->numOfRows = 0; SLocalReducer *pLocalReducer = pRes->pLocalReducer; - + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + // set the data merge in progress int32_t prevStatus = atomic_val_compare_exchange_32(&pLocalReducer->status, TSC_LOCALREDUCE_READY, TSC_LOCALREDUCE_IN_PROGRESS); @@ -1372,7 +1397,9 @@ int32_t tscLocalDoReduce(SSqlObj *pSql) { #if defined(_DEBUG_VIEW) printf("chosen row:\t"); SSrcColumnInfo colInfo[256] = {0}; - tscGetSrcColumnInfo(colInfo, pCmd); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + + tscGetSrcColumnInfo(colInfo, pQueryInfo); tColModelDisplayEx(pModel, tmpBuffer->data, tmpBuffer->numOfElems, pModel->maxCapacity, colInfo); #endif @@ -1408,7 +1435,7 @@ int32_t tscLocalDoReduce(SSqlObj *pSql) { } if (pLocalReducer->hasPrevRow) { - if (needToMerge(pCmd, pLocalReducer, tmpBuffer)) { + if (needToMerge(pQueryInfo, pLocalReducer, tmpBuffer)) { // belong to the group of the previous row, continue process it doExecuteSecondaryMerge(pCmd, pLocalReducer, false); @@ -1419,7 +1446,7 @@ int32_t tscLocalDoReduce(SSqlObj *pSql) { * current row does not belong to the group of previous row. * so the processing of previous group is completed. */ - int32_t numOfRes = finalizeRes(pCmd, pLocalReducer); + int32_t numOfRes = finalizeRes(pQueryInfo, pLocalReducer); bool sameGroup = isSameGroup(pCmd, pLocalReducer, pLocalReducer->prevRowOfInput, tmpBuffer); tFilePage *pResBuf = pLocalReducer->pResultBuf; @@ -1442,7 +1469,7 @@ int32_t tscLocalDoReduce(SSqlObj *pSql) { pLocalReducer->hasUnprocessedRow = true; } - resetOutputBuf(pCmd, pLocalReducer); + resetOutputBuf(pQueryInfo, pLocalReducer); pOneDataSrc->rowIdx += 1; // here we do not check the return value @@ -1496,7 +1523,7 @@ int32_t tscLocalDoReduce(SSqlObj *pSql) { } if (pLocalReducer->hasPrevRow) { - finalizeRes(pCmd, pLocalReducer); + finalizeRes(pQueryInfo, pLocalReducer); } if (pLocalReducer->pResultBuf->numOfElems) { diff --git a/src/client/src/tscServer.c b/src/client/src/tscServer.c index 2a5a14a3f517e059bfee600297a0914d73b06b13..38a33498d2fd712c6e9f773775b7d1ca443182ef 100644 --- a/src/client/src/tscServer.c +++ b/src/client/src/tscServer.c @@ -138,6 +138,8 @@ void tscProcessActivityTimer(void *handle, void *tmrId) { pSql->pTscObj = pObj; pSql->signature = pSql; pObj->pHb = pSql; + tscAddSubqueryInfo(&pObj->pHb->cmd); + tscTrace("%p pHb is allocated, pObj:%p", pObj->pHb, pObj); } @@ -221,9 +223,9 @@ void tscGetConnToVnode(SSqlObj *pSql, uint8_t *pCode) { pSql->thandle = NULL; SSqlCmd * pCmd = &pSql->cmd; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0); - if (UTIL_METER_IS_METRIC(pMeterMetaInfo)) { // multiple vnode query + if (UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo)) { // multiple vnode query SVnodeSidList *vnodeList = tscGetVnodeSidList(pMeterMetaInfo->pMetricMeta, pMeterMetaInfo->vnodeIndex); if (vnodeList != NULL) { pVPeersDesc = vnodeList->vpeerDesc; @@ -421,7 +423,7 @@ void *tscProcessMsgFromServer(char *msg, void *ahandle, void *thandle) { return ahandle; } - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0); if (msg == NULL) { tscTrace("%p no response from ip:0x%x", pSql, pSql->ip); @@ -639,7 +641,8 @@ static int tscLaunchMetricSubQueries(SSqlObj *pSql); // todo merge with callback int32_t tscLaunchJoinSubquery(SSqlObj *pSql, int16_t tableIndex, SJoinSubquerySupporter *pSupporter) { SSqlCmd *pCmd = &pSql->cmd; - + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + pSql->res.qhandle = 0x1; pSql->res.numOfRows = 0; @@ -658,36 +661,38 @@ int32_t tscLaunchJoinSubquery(SSqlObj *pSql, int16_t tableIndex, SJoinSubquerySu pSql->pSubs[pSql->numOfSubs++] = pNew; assert(pSql->numOfSubs <= pSupporter->pState->numOfTotal); - if (QUERY_IS_JOIN_QUERY(pCmd->type)) { - addGroupInfoForSubquery(pSql, pNew, tableIndex); + if (QUERY_IS_JOIN_QUERY(pQueryInfo->type)) { + addGroupInfoForSubquery(pSql, pNew, 0, tableIndex); // refactor as one method - tscColumnBaseInfoUpdateTableIndex(&pNew->cmd.pQueryInfo[0].colList, 0); - tscColumnBaseInfoCopy(&pSupporter->colList, &pNew->cmd.pQueryInfo[0].colList, 0); + SQueryInfo* pNewQueryInfo = tscGetQueryInfoDetail(&pNew->cmd, 0); + tscColumnBaseInfoUpdateTableIndex(&pNewQueryInfo->colList, 0); + tscColumnBaseInfoCopy(&pSupporter->colList, &pNewQueryInfo->colList, 0); - tscSqlExprCopy(&pSupporter->exprsInfo, &pNew->cmd.pQueryInfo[0].exprsInfo, pSupporter->uid); + tscSqlExprCopy(&pSupporter->exprsInfo, &pNewQueryInfo->exprsInfo, pSupporter->uid); - tscFieldInfoCopyAll(&pNew->cmd.pQueryInfo[0].fieldsInfo, &pSupporter->fieldsInfo); - tscTagCondCopy(&pSupporter->tagCond, &pNew->cmd.pQueryInfo[0].tagCond); - pSupporter->groupbyExpr = pNew->cmd.pQueryInfo[0].groupbyExpr; + tscFieldInfoCopyAll(&pNewQueryInfo->fieldsInfo, &pSupporter->fieldsInfo); + tscTagCondCopy(&pSupporter->tagCond, &pNewQueryInfo->tagCond); + pSupporter->groupbyExpr = pNewQueryInfo->groupbyExpr; pNew->cmd.numOfCols = 0; - pNew->cmd.pQueryInfo[0].nAggTimeInterval = 0; - memset(&pNew->cmd.pQueryInfo[0].limit, 0, sizeof(SLimitVal)); - memset(&pNew->cmd.pQueryInfo[0].groupbyExpr, 0, sizeof(SSqlGroupbyExpr)); + pNewQueryInfo->nAggTimeInterval = 0; + memset(&pNewQueryInfo->limit, 0, sizeof(SLimitVal)); + memset(&pNewQueryInfo->groupbyExpr, 0, sizeof(SSqlGroupbyExpr)); // set the ts,tags that involved in join, as the output column of intermediate result - tscFreeSqlCmdData(&pNew->cmd); - + pCmd->pDataBlocks = tscDestroyBlockArrayList(pNew->cmd.pDataBlocks); + tscFreeSubqueryInfo(&pNew->cmd); + SSchema colSchema = {.type = TSDB_DATA_TYPE_BINARY, .bytes = 1}; SColumnIndex index = {0, PRIMARYKEY_TIMESTAMP_COL_INDEX}; - tscAddSpecialColumnForSelect(&pNew->cmd, 0, TSDB_FUNC_TS_COMP, &index, &colSchema, TSDB_COL_NORMAL); + tscAddSpecialColumnForSelect(pNewQueryInfo, 0, TSDB_FUNC_TS_COMP, &index, &colSchema, TSDB_COL_NORMAL); // set the tags value for ts_comp function - SSqlExpr *pExpr = tscSqlExprGet(&pNew->cmd, 0); + SSqlExpr *pExpr = tscSqlExprGet(pNewQueryInfo, 0); - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(&pNew->cmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pNewQueryInfo, 0); int16_t tagColIndex = tscGetJoinTagColIndexByUid(&pSupporter->tagCond, pMeterMetaInfo->pMeterMeta->uid); pExpr->param->i64Key = tagColIndex; @@ -697,16 +702,17 @@ int32_t tscLaunchJoinSubquery(SSqlObj *pSql, int16_t tableIndex, SJoinSubquerySu for (int32_t i = 0; i < pSupporter->colList.numOfCols; ++i) { SColumnBase *pColBase = &pSupporter->colList.pColList[i]; if (pColBase->numOfFilters > 0) { // copy to the pNew->cmd.colList if it is filtered. - tscColumnBaseCopy(&pNew->cmd.pQueryInfo[0].colList.pColList[pNew->cmd.pQueryInfo[0].colList.numOfCols], pColBase); - pNew->cmd.pQueryInfo[0].colList.numOfCols++; + tscColumnBaseCopy(&pNewQueryInfo->colList.pColList[pNewQueryInfo->colList.numOfCols], pColBase); + pNewQueryInfo->colList.numOfCols++; } } } else { - pNew->cmd.type |= TSDB_QUERY_TYPE_SUBQUERY; + SQueryInfo* pNewQueryInfo = tscGetQueryInfoDetail(&pNew->cmd, 0); + pNewQueryInfo->type |= TSDB_QUERY_TYPE_SUBQUERY; } #ifdef _DEBUG_VIEW - tscPrintSelectClause(&pNew->cmd); + tscPrintSelectClause(&pNew->cmd, 0); #endif return tscProcessSql(pNew); @@ -752,13 +758,20 @@ int tscProcessSql(SSqlObj *pSql) { SSqlRes * pRes = &pSql->res; SSqlCmd * pCmd = &pSql->cmd; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); - - if (pMeterMetaInfo != NULL) { - name = pMeterMetaInfo->name; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + SMeterMetaInfo* pMeterMetaInfo = NULL; + int16_t type = 0; + + if (pQueryInfo != NULL) { + pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); + if (pMeterMetaInfo != NULL) { + name = pMeterMetaInfo->name; + } + + type = pQueryInfo->type; } - tscTrace("%p SQL cmd:%d will be processed, name:%s, type:%d", pSql, pSql->cmd.command, name, pSql->cmd.type); + tscTrace("%p SQL cmd:%d will be processed, name:%s, type:%d", pSql, pCmd->command, name, type); pSql->retry = 0; if (pSql->cmd.command < TSDB_SQL_MGMT) { #ifdef CLUSTER @@ -767,8 +780,7 @@ int tscProcessSql(SSqlObj *pSql) { pSql->maxRetry = 2; #endif - // the pMeterMetaInfo cannot be NULL - if (pMeterMetaInfo == NULL) { + if (pMeterMetaInfo == NULL) { // the pMeterMetaInfo cannot be NULL pSql->res.code = TSDB_CODE_OTHERS; return pSql->res.code; } @@ -776,7 +788,7 @@ int tscProcessSql(SSqlObj *pSql) { if (UTIL_METER_IS_NOMRAL_METER(pMeterMetaInfo)) { pSql->index = pMeterMetaInfo->pMeterMeta->index; } else { // it must be the parent SSqlObj for super table query - if ((pSql->cmd.type & TSDB_QUERY_TYPE_SUBQUERY) != 0) { + if ((pQueryInfo->type & TSDB_QUERY_TYPE_SUBQUERY) != 0) { int32_t idx = pMeterMetaInfo->vnodeIndex; SVnodeSidList *pSidList = tscGetVnodeSidList(pMeterMetaInfo->pMetricMeta, idx); @@ -790,17 +802,18 @@ int tscProcessSql(SSqlObj *pSql) { } // todo handle async situation - if (QUERY_IS_JOIN_QUERY(pSql->cmd.type)) { - if ((pSql->cmd.type & TSDB_QUERY_TYPE_SUBQUERY) == 0) { + if (QUERY_IS_JOIN_QUERY(type)) { + if ((pQueryInfo->type & TSDB_QUERY_TYPE_SUBQUERY) == 0) { SSubqueryState *pState = calloc(1, sizeof(SSubqueryState)); - pState->numOfTotal = pSql->cmd.pQueryInfo[0].numOfTables; + + pState->numOfTotal = pQueryInfo->numOfTables; - for (int32_t i = 0; i < pSql->cmd.pQueryInfo[0].numOfTables; ++i) { + for (int32_t i = 0; i < pQueryInfo->numOfTables; ++i) { SJoinSubquerySupporter *pSupporter = tscCreateJoinSupporter(pSql, pState, i); if (pSupporter == NULL) { // failed to create support struct, abort current query tscError("%p tableIndex:%d, failed to allocate join support object, abort further query", pSql, i); - pState->numOfCompleted = pSql->cmd.pQueryInfo[0].numOfTables - i - 1; + pState->numOfCompleted = pQueryInfo->numOfTables - i - 1; pSql->res.code = TSDB_CODE_CLI_OUT_OF_MEMORY; return pSql->res.code; @@ -829,7 +842,7 @@ int tscProcessSql(SSqlObj *pSql) { return TSDB_CODE_SUCCESS; } else { // for first stage sub query, iterate all vnodes to get all timestamp - if ((pSql->cmd.type & TSDB_QUERY_TYPE_JOIN_SEC_STAGE) != TSDB_QUERY_TYPE_JOIN_SEC_STAGE) { + if ((pQueryInfo->type & TSDB_QUERY_TYPE_JOIN_SEC_STAGE) != TSDB_QUERY_TYPE_JOIN_SEC_STAGE) { return doProcessSql(pSql); } } @@ -891,11 +904,12 @@ static void doCleanupSubqueries(SSqlObj *pSql, int32_t vnodeIndex, int32_t numOf int tscLaunchMetricSubQueries(SSqlObj *pSql) { SSqlRes *pRes = &pSql->res; - + SSqlCmd *pCmd = &pSql->cmd; + // pRes->code check only serves in launching metric sub-queries if (pRes->code == TSDB_CODE_QUERY_CANCELLED) { - pSql->cmd.command = TSDB_SQL_RETRIEVE_METRIC; // enable the abort of kill metric function. - return pSql->res.code; + pCmd->command = TSDB_SQL_RETRIEVE_METRIC; // enable the abort of kill metric function. + return pRes->code; } tExtMemBuffer ** pMemoryBuf = NULL; @@ -903,9 +917,11 @@ int tscLaunchMetricSubQueries(SSqlObj *pSql) { tColModel * pModel = NULL; pRes->qhandle = 1; // hack the qhandle check - + const uint32_t nBufferSize = (1 << 16); // 64KB - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0); + + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); int32_t numOfVnodes = pMeterMetaInfo->pMetricMeta->numOfVnodes; assert(numOfVnodes > 0); @@ -920,7 +936,7 @@ int tscLaunchMetricSubQueries(SSqlObj *pSql) { pSql->pSubs = malloc(POINTER_BYTES * numOfVnodes); pSql->numOfSubs = numOfVnodes; - + tscTrace("%p retrieved query data from %d vnode(s)", pSql, numOfVnodes); SSubqueryState *pState = calloc(1, sizeof(SSubqueryState)); pState->numOfTotal = numOfVnodes; @@ -964,15 +980,17 @@ int tscLaunchMetricSubQueries(SSqlObj *pSql) { doCleanupSubqueries(pSql, i, numOfVnodes, trs, pDesc, pModel, pMemoryBuf, pState); if (i == 0) { - return pSql->res.code; + return pRes->code; } break; } // todo handle multi-vnode situation - if (pSql->cmd.pQueryInfo[0].tsBuf) { - pNew->cmd.pQueryInfo[0].tsBuf = tsBufClone(pSql->cmd.pQueryInfo[0].tsBuf); + if (pQueryInfo->tsBuf) { + + SQueryInfo* pNewQueryInfo = tscGetQueryInfoDetail(&pNew->cmd, 0); + pNewQueryInfo->tsBuf = tsBufClone(pQueryInfo->tsBuf); } tscTrace("%p sub:%p launch subquery.orderOfSub:%d", pSql, pNew, trs->subqueryIndex); @@ -1107,7 +1125,9 @@ static void tscHandleSubRetrievalError(SRetrieveSupport *trsupport, SSqlObj *pSq pPObj->cmd.command = TSDB_SQL_RETRIEVE_METRIC; } else { // in case of second stage join subquery, invoke its callback function instead of regular QueueAsyncRes - if ((pPObj->cmd.type & TSDB_QUERY_TYPE_JOIN_SEC_STAGE) == TSDB_QUERY_TYPE_JOIN_SEC_STAGE) { + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pPObj->cmd, 0); + + if ((pQueryInfo->type & TSDB_QUERY_TYPE_JOIN_SEC_STAGE) == TSDB_QUERY_TYPE_JOIN_SEC_STAGE) { (*pPObj->fp)(pPObj->param, pPObj, pPObj->res.code); } else { // regular super table query if (pPObj->res.code != TSDB_CODE_SUCCESS) { @@ -1137,8 +1157,9 @@ void tscRetrieveFromVnodeCallBack(void *param, TAOS_RES *tres, int numOfRows) { } SSqlRes * pRes = &pSql->res; - SSqlCmd * pCmd = &pSql->cmd; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); SVnodeSidList *vnodeInfo = tscGetVnodeSidList(pMeterMetaInfo->pMetricMeta, idx); SVPeerDesc * pSvd = &vnodeInfo->vpeerDesc[vnodeInfo->index]; @@ -1153,7 +1174,9 @@ void tscRetrieveFromVnodeCallBack(void *param, TAOS_RES *tres, int numOfRows) { #ifdef _DEBUG_VIEW printf("received data from vnode: %d rows\n", pRes->numOfRows); SSrcColumnInfo colInfo[256] = {0}; - tscGetSrcColumnInfo(colInfo, &pPObj->cmd); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + + tscGetSrcColumnInfo(colInfo, pQueryInfo); tColModelDisplayEx(pDesc->pSchema, pRes->data, pRes->numOfRows, pRes->numOfRows, colInfo); #endif if (tsTotalTmpDirGB != 0 && tsAvailTmpDirGB < tsMinimalTmpDirGB) { @@ -1163,7 +1186,7 @@ void tscRetrieveFromVnodeCallBack(void *param, TAOS_RES *tres, int numOfRows) { return; } int32_t ret = saveToBuffer(trsupport->pExtMemBuffer[idx], pDesc, trsupport->localBuffer, pRes->data, - pRes->numOfRows, pCmd->pQueryInfo[0].groupbyExpr.orderType); + pRes->numOfRows, pQueryInfo->groupbyExpr.orderType); if (ret < 0) { // set no disk space error info, and abort retry tscAbortFurtherRetryRetrieval(trsupport, tres, TSDB_CODE_CLI_NO_DISKSPACE); @@ -1183,7 +1206,9 @@ void tscRetrieveFromVnodeCallBack(void *param, TAOS_RES *tres, int numOfRows) { #ifdef _DEBUG_VIEW printf("%ld rows data flushed to disk:\n", trsupport->localBuffer->numOfElems); SSrcColumnInfo colInfo[256] = {0}; - tscGetSrcColumnInfo(colInfo, &pPObj->cmd); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pPObj->cmd, 0); + + tscGetSrcColumnInfo(colInfo, pQueryInfo); tColModelDisplayEx(pDesc->pSchema, trsupport->localBuffer->data, trsupport->localBuffer->numOfElems, trsupport->localBuffer->numOfElems, colInfo); #endif @@ -1197,7 +1222,7 @@ void tscRetrieveFromVnodeCallBack(void *param, TAOS_RES *tres, int numOfRows) { // each result for a vnode is ordered as an independant list, // then used as an input of loser tree for disk-based merge routine int32_t ret = - tscFlushTmpBuffer(trsupport->pExtMemBuffer[idx], pDesc, trsupport->localBuffer, pCmd->pQueryInfo[0].groupbyExpr.orderType); + tscFlushTmpBuffer(trsupport->pExtMemBuffer[idx], pDesc, trsupport->localBuffer, pQueryInfo->groupbyExpr.orderType); if (ret != 0) { /* set no disk space error info, and abort retry */ return tscAbortFurtherRetryRetrieval(trsupport, tres, TSDB_CODE_CLI_NO_DISKSPACE); @@ -1213,7 +1238,9 @@ void tscRetrieveFromVnodeCallBack(void *param, TAOS_RES *tres, int numOfRows) { tscTrace("%p retrieve from %d vnodes completed.final NumOfRows:%d,start to build loser tree", pPObj, trsupport->pState->numOfTotal, trsupport->pState->numOfCompleted); - tscClearInterpInfo(&pPObj->cmd); + SQueryInfo* pPQueryInfo = tscGetQueryInfoDetail(&pPObj->cmd, 0); + tscClearInterpInfo(pPQueryInfo); + tscCreateLocalReducer(trsupport->pExtMemBuffer, trsupport->pState->numOfTotal, pDesc, trsupport->pFinalColModel, &pPObj->cmd, &pPObj->res); tscTrace("%p build loser tree completed", pPObj); @@ -1289,11 +1316,12 @@ static void tscRetrieveDataRes(void *param, TAOS_RES *tres, int retCode); static SSqlObj *tscCreateSqlObjForSubquery(SSqlObj *pSql, SRetrieveSupport *trsupport, SSqlObj *prevSqlObj) { SSqlObj *pNew = createSubqueryObj(pSql, 0, tscRetrieveDataRes, trsupport, prevSqlObj); if (pNew != NULL) { // the sub query of two-stage super table query - pNew->cmd.type |= TSDB_QUERY_TYPE_STABLE_SUBQUERY; - assert(pNew->cmd.pQueryInfo[0].numOfTables == 1); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pNew->cmd, 0); + pQueryInfo->type |= TSDB_QUERY_TYPE_STABLE_SUBQUERY; + assert(pQueryInfo->numOfTables == 1); // launch subquery for each vnode, so the subquery index equals to the vnodeIndex. - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(&pNew->cmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); pMeterMetaInfo->vnodeIndex = trsupport->subqueryIndex; pSql->pSubs[trsupport->subqueryIndex] = pNew; @@ -1306,7 +1334,7 @@ void tscRetrieveDataRes(void *param, TAOS_RES *tres, int code) { SRetrieveSupport *trsupport = (SRetrieveSupport *)param; SSqlObj * pSql = (SSqlObj *)tres; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0, 0); int32_t idx = pMeterMetaInfo->vnodeIndex; SVnodeSidList *vnodeInfo = NULL; @@ -1350,7 +1378,9 @@ void tscRetrieveDataRes(void *param, TAOS_RES *tres, int code) { trsupport->pState->code = -TSDB_CODE_CLI_OUT_OF_MEMORY; trsupport->numOfRetry = MAX_NUM_OF_SUBQUERY_RETRY; } else { - assert(pNew->cmd.pQueryInfo[0].pMeterInfo[0]->pMeterMeta != NULL && pNew->cmd.pQueryInfo[0].pMeterInfo[0]->pMetricMeta != NULL); + + SQueryInfo* pNewQueryInfo = tscGetQueryInfoDetail(&pNew->cmd, 0); + assert(pNewQueryInfo->pMeterInfo[0]->pMeterMeta != NULL && pNewQueryInfo->pMeterInfo[0]->pMetricMeta != NULL); tscProcessSql(pNew); return; } @@ -1387,8 +1417,9 @@ int tscBuildRetrieveMsg(SSqlObj *pSql, SSqlInfo *pInfo) { *((uint64_t *)pMsg) = pSql->res.qhandle; pMsg += sizeof(pSql->res.qhandle); - *((uint16_t *)pMsg) = htons(pSql->cmd.type); - pMsg += sizeof(pSql->cmd.type); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + *((uint16_t *)pMsg) = htons(pQueryInfo->type); + pMsg += sizeof(pQueryInfo->type); msgLen = pMsg - pStart; pSql->cmd.payloadLen = msgLen; @@ -1400,7 +1431,7 @@ int tscBuildRetrieveMsg(SSqlObj *pSql, SSqlInfo *pInfo) { void tscUpdateVnodeInSubmitMsg(SSqlObj *pSql, char *buf) { SShellSubmitMsg *pShellMsg; char * pMsg; - SMeterMetaInfo * pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0); + SMeterMetaInfo * pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0, 0); SMeterMeta *pMeterMeta = pMeterMetaInfo->pMeterMeta; @@ -1417,7 +1448,7 @@ int tscBuildSubmitMsg(SSqlObj *pSql, SSqlInfo *pInfo) { char * pMsg, *pStart; int msgLen = 0; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0, 0); SMeterMeta * pMeterMeta = pMeterMetaInfo->pMeterMeta; pStart = pSql->cmd.payload + tsRpcHeadSize; @@ -1438,7 +1469,7 @@ int tscBuildSubmitMsg(SSqlObj *pSql, SSqlInfo *pInfo) { void tscUpdateVnodeInQueryMsg(SSqlObj *pSql, char *buf) { SSqlCmd * pCmd = &pSql->cmd; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0); char * pStart = buf + tsRpcHeadSize; SQueryMeterMsg *pQueryMsg = (SQueryMeterMsg *)pStart; @@ -1459,14 +1490,15 @@ void tscUpdateVnodeInQueryMsg(SSqlObj *pSql, char *buf) { */ static int32_t tscEstimateQueryMsgSize(SSqlCmd *pCmd) { const static int32_t MIN_QUERY_MSG_PKT_SIZE = TSDB_MAX_BYTES_PER_ROW * 5; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); - int32_t srcColListSize = pCmd->pQueryInfo[0].colList.numOfCols * sizeof(SColumnInfo); + int32_t srcColListSize = pQueryInfo->colList.numOfCols * sizeof(SColumnInfo); - int32_t exprSize = sizeof(SSqlFuncExprMsg) * pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + int32_t exprSize = sizeof(SSqlFuncExprMsg) * pQueryInfo->fieldsInfo.numOfOutputCols; + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0); // meter query without tags values - if (!UTIL_METER_IS_METRIC(pMeterMetaInfo)) { + if (!UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo)) { return MIN_QUERY_MSG_PKT_SIZE + minMsgSize() + sizeof(SQueryMeterMsg) + srcColListSize + exprSize; } @@ -1475,18 +1507,18 @@ static int32_t tscEstimateQueryMsgSize(SSqlCmd *pCmd) { SVnodeSidList *pVnodeSidList = tscGetVnodeSidList(pMetricMeta, pMeterMetaInfo->vnodeIndex); int32_t meterInfoSize = (pMetricMeta->tagLen + sizeof(SMeterSidExtInfo)) * pVnodeSidList->numOfSids; - int32_t outputColumnSize = pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols * sizeof(SSqlFuncExprMsg); + int32_t outputColumnSize = pQueryInfo->fieldsInfo.numOfOutputCols * sizeof(SSqlFuncExprMsg); int32_t size = meterInfoSize + outputColumnSize + srcColListSize + exprSize + MIN_QUERY_MSG_PKT_SIZE; - if (pCmd->pQueryInfo[0].tsBuf != NULL) { - size += pCmd->pQueryInfo[0].tsBuf->fileSize; + if (pQueryInfo->tsBuf != NULL) { + size += pQueryInfo->tsBuf->fileSize; } return size; } static char *doSerializeTableInfo(SSqlObj *pSql, int32_t numOfMeters, int32_t vnodeId, char *pMsg) { - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0, 0); SMeterMeta * pMeterMeta = pMeterMetaInfo->pMeterMeta; SMetricMeta *pMetricMeta = pMeterMetaInfo->pMetricMeta; @@ -1535,7 +1567,9 @@ int tscBuildQueryMsg(SSqlObj *pSql, SSqlInfo *pInfo) { return -1; } - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); char * pStart = pCmd->payload + tsRpcHeadSize; SMeterMeta * pMeterMeta = pMeterMetaInfo->pMeterMeta; @@ -1577,26 +1611,26 @@ int tscBuildQueryMsg(SSqlObj *pSql, SSqlInfo *pInfo) { pQueryMsg->numOfSids = htonl(numOfMeters); pQueryMsg->numOfTagsCols = htons(pMeterMetaInfo->numOfTags); - if (pCmd->order.order == TSQL_SO_ASC) { - pQueryMsg->skey = htobe64(pCmd->pQueryInfo[0].stime); - pQueryMsg->ekey = htobe64(pCmd->pQueryInfo[0].etime); + if (pQueryInfo->order.order == TSQL_SO_ASC) { + pQueryMsg->skey = htobe64(pQueryInfo->stime); + pQueryMsg->ekey = htobe64(pQueryInfo->etime); } else { - pQueryMsg->skey = htobe64(pCmd->pQueryInfo[0].etime); - pQueryMsg->ekey = htobe64(pCmd->pQueryInfo[0].stime); + pQueryMsg->skey = htobe64(pQueryInfo->etime); + pQueryMsg->ekey = htobe64(pQueryInfo->stime); } pQueryMsg->num = htonl(0); - pQueryMsg->order = htons(pCmd->order.order); - pQueryMsg->orderColId = htons(pCmd->order.orderColId); + pQueryMsg->order = htons(pQueryInfo->order.order); + pQueryMsg->orderColId = htons(pQueryInfo->order.orderColId); - pQueryMsg->interpoType = htons(pCmd->pQueryInfo[0].interpoType); + pQueryMsg->interpoType = htons(pQueryInfo->interpoType); - pQueryMsg->limit = htobe64(pCmd->pQueryInfo->limit.limit); - pQueryMsg->offset = htobe64(pCmd->pQueryInfo->limit.offset); + pQueryMsg->limit = htobe64(pQueryInfo->limit.limit); + pQueryMsg->offset = htobe64(pQueryInfo->limit.offset); - pQueryMsg->numOfCols = htons(pCmd->pQueryInfo[0].colList.numOfCols); + pQueryMsg->numOfCols = htons(pQueryInfo->colList.numOfCols); - if (pCmd->pQueryInfo[0].colList.numOfCols <= 0) { + if (pQueryInfo->colList.numOfCols <= 0) { tscError("%p illegal value of numOfCols in query msg: %d", pSql, pMeterMeta->numOfColumns); return -1; } @@ -1606,19 +1640,19 @@ int tscBuildQueryMsg(SSqlObj *pSql, SSqlInfo *pInfo) { return -1; } - pQueryMsg->nAggTimeInterval = htobe64(pCmd->pQueryInfo[0].nAggTimeInterval); - pQueryMsg->intervalTimeUnit = pCmd->pQueryInfo[0].intervalTimeUnit; - if (pCmd->pQueryInfo[0].nAggTimeInterval < 0) { - tscError("%p illegal value of aggregation time interval in query msg: %ld", pSql, pCmd->pQueryInfo[0].nAggTimeInterval); + pQueryMsg->nAggTimeInterval = htobe64(pQueryInfo->nAggTimeInterval); + pQueryMsg->intervalTimeUnit = pQueryInfo->intervalTimeUnit; + if (pQueryInfo->nAggTimeInterval < 0) { + tscError("%p illegal value of aggregation time interval in query msg: %ld", pSql, pQueryInfo->nAggTimeInterval); return -1; } - if (pCmd->pQueryInfo[0].groupbyExpr.numOfGroupCols < 0) { - tscError("%p illegal value of numOfGroupCols in query msg: %d", pSql, pCmd->pQueryInfo[0].groupbyExpr.numOfGroupCols); + if (pQueryInfo->groupbyExpr.numOfGroupCols < 0) { + tscError("%p illegal value of numOfGroupCols in query msg: %d", pSql, pQueryInfo->groupbyExpr.numOfGroupCols); return -1; } - pQueryMsg->numOfGroupCols = htons(pCmd->pQueryInfo[0].groupbyExpr.numOfGroupCols); + pQueryMsg->numOfGroupCols = htons(pQueryInfo->groupbyExpr.numOfGroupCols); if (UTIL_METER_IS_NOMRAL_METER(pMeterMetaInfo)) { // query on meter pQueryMsg->tagLength = 0; @@ -1626,20 +1660,20 @@ int tscBuildQueryMsg(SSqlObj *pSql, SSqlInfo *pInfo) { pQueryMsg->tagLength = htons(pMetricMeta->tagLen); } - pQueryMsg->queryType = htons(pCmd->type); - pQueryMsg->numOfOutputCols = htons(pCmd->pQueryInfo[0].exprsInfo.numOfExprs); + pQueryMsg->queryType = htons(pQueryInfo->type); + pQueryMsg->numOfOutputCols = htons(pQueryInfo->exprsInfo.numOfExprs); - if (pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols < 0) { - tscError("%p illegal value of number of output columns in query msg: %d", pSql, pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols); + if (pQueryInfo->fieldsInfo.numOfOutputCols < 0) { + tscError("%p illegal value of number of output columns in query msg: %d", pSql, pQueryInfo->fieldsInfo.numOfOutputCols); return -1; } // set column list ids - char * pMsg = (char *)(pQueryMsg->colList) + pCmd->pQueryInfo[0].colList.numOfCols * sizeof(SColumnInfo); + char * pMsg = (char *)(pQueryMsg->colList) + pQueryInfo->colList.numOfCols * sizeof(SColumnInfo); SSchema *pSchema = tsGetSchema(pMeterMeta); - for (int32_t i = 0; i < pCmd->pQueryInfo[0].colList.numOfCols; ++i) { - SColumnBase *pCol = tscColumnBaseInfoGet(&pCmd->pQueryInfo[0].colList, i); + for (int32_t i = 0; i < pQueryInfo->colList.numOfCols; ++i) { + SColumnBase *pCol = tscColumnBaseInfoGet(&pQueryInfo->colList, i); SSchema * pColSchema = &pSchema[pCol->colIndex.columnIndex]; if (pCol->colIndex.columnIndex >= pMeterMeta->numOfColumns || pColSchema->type < TSDB_DATA_TYPE_BOOL || @@ -1688,8 +1722,8 @@ int tscBuildQueryMsg(SSqlObj *pSql, SSqlInfo *pInfo) { SSqlFuncExprMsg *pSqlFuncExpr = (SSqlFuncExprMsg *)pMsg; - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - SSqlExpr *pExpr = tscSqlExprGet(pCmd, i); + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + SSqlExpr *pExpr = tscSqlExprGet(pQueryInfo, i); if (pExpr->functionId == TSDB_FUNC_ARITHM) { hasArithmeticFunction = true; @@ -1728,8 +1762,8 @@ int tscBuildQueryMsg(SSqlObj *pSql, SSqlInfo *pInfo) { int32_t len = 0; if (hasArithmeticFunction) { - SColumnBase *pColBase = pCmd->pQueryInfo[0].colList.pColList; - for (int32_t i = 0; i < pCmd->pQueryInfo[0].colList.numOfCols; ++i) { + SColumnBase *pColBase = pQueryInfo->colList.pColList; + for (int32_t i = 0; i < pQueryInfo->colList.numOfCols; ++i) { char * name = pSchema[pColBase[i].colIndex.columnIndex].name; int32_t lenx = strlen(name); memcpy(pMsg, name, lenx); @@ -1763,7 +1797,7 @@ int tscBuildQueryMsg(SSqlObj *pSql, SSqlInfo *pInfo) { } } - SSqlGroupbyExpr *pGroupbyExpr = &pCmd->pQueryInfo[0].groupbyExpr; + SSqlGroupbyExpr *pGroupbyExpr = &pQueryInfo->groupbyExpr; if (pGroupbyExpr->numOfGroupCols != 0) { pQueryMsg->orderByIdx = htons(pGroupbyExpr->orderIndex); pQueryMsg->orderType = htons(pGroupbyExpr->orderType); @@ -1785,10 +1819,10 @@ int tscBuildQueryMsg(SSqlObj *pSql, SSqlInfo *pInfo) { } } - if (pCmd->pQueryInfo[0].interpoType != TSDB_INTERPO_NONE) { - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - *((int64_t *)pMsg) = htobe64(pCmd->pQueryInfo[0].defaultVal[i]); - pMsg += sizeof(pCmd->pQueryInfo[0].defaultVal[0]); + if (pQueryInfo->interpoType != TSDB_INTERPO_NONE) { + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + *((int64_t *)pMsg) = htobe64(pQueryInfo->defaultVal[i]); + pMsg += sizeof(pQueryInfo->defaultVal[0]); } } @@ -1797,13 +1831,13 @@ int tscBuildQueryMsg(SSqlObj *pSql, SSqlInfo *pInfo) { int32_t tsLen = 0; int32_t numOfBlocks = 0; - if (pCmd->pQueryInfo[0].tsBuf != NULL) { - STSVnodeBlockInfo *pBlockInfo = tsBufGetVnodeBlockInfo(pCmd->pQueryInfo[0].tsBuf, pMeterMetaInfo->vnodeIndex); - assert(QUERY_IS_JOIN_QUERY(pCmd->type) && pBlockInfo != NULL); // this query should not be sent + if (pQueryInfo->tsBuf != NULL) { + STSVnodeBlockInfo *pBlockInfo = tsBufGetVnodeBlockInfo(pQueryInfo->tsBuf, pMeterMetaInfo->vnodeIndex); + assert(QUERY_IS_JOIN_QUERY(pQueryInfo->type) && pBlockInfo != NULL); // this query should not be sent // todo refactor - fseek(pCmd->pQueryInfo[0].tsBuf->f, pBlockInfo->offset, SEEK_SET); - fread(pMsg, pBlockInfo->compLen, 1, pCmd->pQueryInfo[0].tsBuf->f); + fseek(pQueryInfo->tsBuf->f, pBlockInfo->offset, SEEK_SET); + fread(pMsg, pBlockInfo->compLen, 1, pQueryInfo->tsBuf->f); pMsg += pBlockInfo->compLen; tsLen = pBlockInfo->compLen; @@ -1812,8 +1846,8 @@ int tscBuildQueryMsg(SSqlObj *pSql, SSqlInfo *pInfo) { pQueryMsg->tsLen = htonl(tsLen); pQueryMsg->tsNumOfBlocks = htonl(numOfBlocks); - if (pCmd->pQueryInfo[0].tsBuf != NULL) { - pQueryMsg->tsOrder = htonl(pCmd->pQueryInfo[0].tsBuf->tsOrder); + if (pQueryInfo->tsBuf != NULL) { + pQueryMsg->tsOrder = htonl(pQueryInfo->tsBuf->tsOrder); } msgLen = pMsg - pStart; @@ -1836,7 +1870,7 @@ int32_t tscBuildCreateDbMsg(SSqlObj *pSql, SSqlInfo *pInfo) { pMsg = doBuildMsgHeader(pSql, &pStart); pCreateDbMsg = (SCreateDbMsg *)pMsg; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0); strncpy(pCreateDbMsg->db, pMeterMetaInfo->name, tListLen(pCreateDbMsg->db)); pMsg += sizeof(SCreateDbMsg); @@ -1986,7 +2020,7 @@ int32_t tscBuildDropDbMsg(SSqlObj *pSql, SSqlInfo *pInfo) { pMsg = doBuildMsgHeader(pSql, &pStart); pDropDbMsg = (SDropDbMsg *)pMsg; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0); strncpy(pDropDbMsg->db, pMeterMetaInfo->name, tListLen(pDropDbMsg->db)); pDropDbMsg->ignoreNotExists = pInfo->pDCLInfo->existsCheck ? 1 : 0; @@ -2008,7 +2042,7 @@ int32_t tscBuildDropTableMsg(SSqlObj *pSql, SSqlInfo *pInfo) { pMsg = doBuildMsgHeader(pSql, &pStart); pDropTableMsg = (SDropTableMsg *)pMsg; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0); strcpy(pDropTableMsg->meterId, pMeterMetaInfo->name); pDropTableMsg->igNotExists = pInfo->pDCLInfo->existsCheck ? 1 : 0; @@ -2026,7 +2060,7 @@ int32_t tscBuildDropDnodeMsg(SSqlObj *pSql, SSqlInfo *pInfo) { char * pMsg, *pStart; SSqlCmd * pCmd = &pSql->cmd; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0); pMsg = doBuildMsgHeader(pSql, &pStart); pDrop = (SDropDnodeMsg *)pMsg; @@ -2051,7 +2085,7 @@ int32_t tscBuildDropAcctMsg(SSqlObj *pSql, SSqlInfo *pInfo) { pMsg = doBuildMsgHeader(pSql, &pStart); pDropMsg = (SDropUserMsg *)pMsg; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0); strcpy(pDropMsg->user, pMeterMetaInfo->name); pMsg += sizeof(SDropUserMsg); @@ -2071,7 +2105,7 @@ int32_t tscBuildUseDbMsg(SSqlObj *pSql, SSqlInfo *pInfo) { pMsg = doBuildMsgHeader(pSql, &pStart); pUseDbMsg = (SUseDbMsg *)pMsg; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0); strcpy(pUseDbMsg->db, pMeterMetaInfo->name); pMsg += sizeof(SUseDbMsg); @@ -2101,7 +2135,7 @@ int32_t tscBuildShowMsg(SSqlObj *pSql, SSqlInfo *pInfo) { SMgmtHead *pMgmt = (SMgmtHead *)pMsg; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0); size_t nameLen = strlen(pMeterMetaInfo->name); if (nameLen > 0) { @@ -2199,7 +2233,9 @@ int tscBuildCreateTableMsg(SSqlObj *pSql, SSqlInfo *pInfo) { int size = 0; SSqlCmd * pCmd = &pSql->cmd; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); // Reallocate the payload size size = tscEstimateCreateTableMsgLength(pSql, pInfo); @@ -2239,7 +2275,7 @@ int tscBuildCreateTableMsg(SSqlObj *pSql, SSqlInfo *pInfo) { pSchema = pCreateTableMsg->schema; for (int i = 0; i < pCmd->numOfCols + pCmd->count; ++i) { - TAOS_FIELD *pField = tscFieldInfoGetField(pCmd, i); + TAOS_FIELD *pField = tscFieldInfoGetField(pQueryInfo, i); pSchema->type = pField->type; strcpy(pSchema->name, pField->name); @@ -2258,7 +2294,7 @@ int tscBuildCreateTableMsg(SSqlObj *pSql, SSqlInfo *pInfo) { } } - tscClearFieldInfo(&pCmd->pQueryInfo[0].fieldsInfo); + tscClearFieldInfo(&pQueryInfo->fieldsInfo); msgLen = pMsg - pStart; pCmd->payloadLen = msgLen; @@ -2269,7 +2305,8 @@ int tscBuildCreateTableMsg(SSqlObj *pSql, SSqlInfo *pInfo) { } int tscEstimateAlterTableMsgLength(SSqlCmd *pCmd) { - return minMsgSize() + sizeof(SMgmtHead) + sizeof(SAlterTableMsg) + sizeof(SSchema) * tscNumOfFields(pCmd) + + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + return minMsgSize() + sizeof(SMgmtHead) + sizeof(SAlterTableMsg) + sizeof(SSchema) * tscNumOfFields(pQueryInfo) + TSDB_EXTRA_PAYLOAD_SIZE; } @@ -2280,7 +2317,9 @@ int tscBuildAlterTableMsg(SSqlObj *pSql, SSqlInfo *pInfo) { int size = 0; SSqlCmd * pCmd = &pSql->cmd; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); size = tscEstimateAlterTableMsgLength(pCmd); if (TSDB_CODE_SUCCESS != tscAllocPayload(pCmd, size)) { @@ -2301,12 +2340,12 @@ int tscBuildAlterTableMsg(SSqlObj *pSql, SSqlInfo *pInfo) { strcpy(pAlterTableMsg->meterId, pMeterMetaInfo->name); pAlterTableMsg->type = htons(pAlterInfo->type); - pAlterTableMsg->numOfCols = htons(tscNumOfFields(pCmd)); + pAlterTableMsg->numOfCols = htons(tscNumOfFields(pQueryInfo)); memcpy(pAlterTableMsg->tagVal, pAlterInfo->tagData.data, TSDB_MAX_TAGS_LEN); SSchema *pSchema = pAlterTableMsg->schema; - for (int i = 0; i < tscNumOfFields(pCmd); ++i) { - TAOS_FIELD *pField = tscFieldInfoGetField(pCmd, i); + for (int i = 0; i < tscNumOfFields(pQueryInfo); ++i) { + TAOS_FIELD *pField = tscFieldInfoGetField(pQueryInfo, i); pSchema->type = pField->type; strcpy(pSchema->name, pField->name); @@ -2332,7 +2371,7 @@ int tscAlterDbMsg(SSqlObj *pSql, SSqlInfo *pInfo) { SSqlCmd * pCmd = &pSql->cmd; STscObj * pObj = pSql->pTscObj; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0); pStart = pCmd->payload + tsRpcHeadSize; pMsg = pStart; @@ -2364,7 +2403,8 @@ int tscBuildRetrieveFromMgmtMsg(SSqlObj *pSql, SSqlInfo *pInfo) { SMgmtHead *pMgmt = (SMgmtHead *)pMsg; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); size_t nameLen = strlen(pMeterMetaInfo->name); if (nameLen > 0) { @@ -2378,8 +2418,8 @@ int tscBuildRetrieveFromMgmtMsg(SSqlObj *pSql, SSqlInfo *pInfo) { *((uint64_t *)pMsg) = pSql->res.qhandle; pMsg += sizeof(pSql->res.qhandle); - *((uint16_t *)pMsg) = htons(pCmd->type); - pMsg += sizeof(pCmd->type); + *((uint16_t *)pMsg) = htons(pQueryInfo->type); + pMsg += sizeof(pQueryInfo->type); msgLen = pMsg - pStart; pCmd->payloadLen = msgLen; @@ -2388,17 +2428,17 @@ int tscBuildRetrieveFromMgmtMsg(SSqlObj *pSql, SSqlInfo *pInfo) { return TSDB_CODE_SUCCESS; } -static int tscSetResultPointer(SSqlCmd *pCmd, SSqlRes *pRes) { - if (tscCreateResPointerInfo(pCmd, pRes) != TSDB_CODE_SUCCESS) { +static int tscSetResultPointer(SQueryInfo* pQueryInfo, SSqlRes *pRes) { + if (tscCreateResPointerInfo(pQueryInfo, pRes) != TSDB_CODE_SUCCESS) { return pRes->code; } - for (int i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - TAOS_FIELD *pField = tscFieldInfoGetField(pCmd, i); - int16_t offset = tscFieldInfoGetOffset(pCmd, i); + for (int i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + TAOS_FIELD *pField = tscFieldInfoGetField(pQueryInfo, i); + int16_t offset = tscFieldInfoGetOffset(pQueryInfo, i); pRes->bytes[i] = pField->bytes; - if (pCmd->order.order == TSQL_SO_DESC) { + if (pQueryInfo->order.order == TSQL_SO_DESC) { pRes->bytes[i] = -pRes->bytes[i]; pRes->tsrow[i] = ((pRes->data + offset * pRes->numOfRows) + (pRes->numOfRows - 1) * pField->bytes); } else { @@ -2418,7 +2458,9 @@ static int tscSetResultPointer(SSqlCmd *pCmd, SSqlRes *pRes) { static int tscLocalResultCommonBuilder(SSqlObj *pSql, int32_t numOfRes) { SSqlRes *pRes = &pSql->res; SSqlCmd *pCmd = &pSql->cmd; - + + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + pRes->code = TSDB_CODE_SUCCESS; if (pRes->rspType == 0) { @@ -2426,7 +2468,7 @@ static int tscLocalResultCommonBuilder(SSqlObj *pSql, int32_t numOfRes) { pRes->row = 0; pRes->rspType = 1; - tscSetResultPointer(pCmd, pRes); + tscSetResultPointer(pQueryInfo, pRes); pRes->row = 0; } else { @@ -2447,7 +2489,7 @@ static int tscLocalResultCommonBuilder(SSqlObj *pSql, int32_t numOfRes) { int tscProcessDescribeTableRsp(SSqlObj *pSql) { SSqlCmd * pCmd = &pSql->cmd; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0); int32_t numOfRes = pMeterMetaInfo->pMeterMeta->numOfColumns + pMeterMetaInfo->pMeterMeta->numOfTags; @@ -2456,10 +2498,12 @@ int tscProcessDescribeTableRsp(SSqlObj *pSql) { int tscProcessTagRetrieveRsp(SSqlObj *pSql) { SSqlCmd * pCmd = &pSql->cmd; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + + SQueryInfo *pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); int32_t numOfRes = 0; - if (tscSqlExprGet(pCmd, 0)->functionId == TSDB_FUNC_TAGPRJ) { + if (tscSqlExprGet(pQueryInfo, 0)->functionId == TSDB_FUNC_TAGPRJ) { numOfRes = pMeterMetaInfo->pMetricMeta->numOfMeters; } else { numOfRes = 1; // for count function, there is only one output. @@ -2472,17 +2516,18 @@ int tscProcessRetrieveMetricRsp(SSqlObj *pSql) { SSqlCmd *pCmd = &pSql->cmd; pRes->code = tscLocalDoReduce(pSql); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); if (pRes->code == TSDB_CODE_SUCCESS && pRes->numOfRows > 0) { - tscSetResultPointer(pCmd, pRes); + tscSetResultPointer(pQueryInfo, pRes); } pRes->row = 0; - uint8_t code = pSql->res.code; + uint8_t code = pRes->code; if (pSql->fp) { // async retrieve metric data - if (pSql->res.code == TSDB_CODE_SUCCESS) { - (*pSql->fp)(pSql->param, pSql, pSql->res.numOfRows); + if (pRes->code == TSDB_CODE_SUCCESS) { + (*pSql->fp)(pSql->param, pSql, pRes->numOfRows); } else { tscQueueAsyncRes(pSql); } @@ -2535,7 +2580,9 @@ int tscBuildMeterMetaMsg(SSqlObj *pSql, SSqlInfo *pInfo) { } SSqlCmd * pCmd = &pSql->cmd; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); pMsg = pCmd->payload + tsRpcHeadSize; pStart = pMsg; @@ -2547,10 +2594,10 @@ int tscBuildMeterMetaMsg(SSqlObj *pSql, SSqlInfo *pInfo) { pInfoMsg = (SMeterInfoMsg *)pMsg; strcpy(pInfoMsg->meterId, pMeterMetaInfo->name); - pInfoMsg->createFlag = htons((uint16_t)pCmd->pQueryInfo[0].defaultVal[0]); + pInfoMsg->createFlag = htons((uint16_t)pQueryInfo->defaultVal[0]); pMsg += sizeof(SMeterInfoMsg); - if (pCmd->pQueryInfo[0].defaultVal[0] != 0) { + if (pQueryInfo->defaultVal[0] != 0) { memcpy(pInfoMsg->tags, tmpData, sizeof(STagData)); pMsg += sizeof(STagData); } @@ -2608,19 +2655,20 @@ int tscBuildMultiMeterMetaMsg(SSqlObj *pSql, SSqlInfo *pInfo) { static int32_t tscEstimateMetricMetaMsgSize(SSqlCmd *pCmd) { const int32_t defaultSize = minMsgSize() + sizeof(SMetricMetaMsg) + sizeof(SMgmtHead) + sizeof(int16_t) * TSDB_MAX_TAGS; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); int32_t n = 0; - for (int32_t i = 0; i < pCmd->pQueryInfo[0].tagCond.numOfTagCond; ++i) { - n += strlen(pCmd->pQueryInfo[0].tagCond.cond[i].cond); + for (int32_t i = 0; i < pQueryInfo->tagCond.numOfTagCond; ++i) { + n += strlen(pQueryInfo->tagCond.cond[i].cond); } int32_t tagLen = n * TSDB_NCHAR_SIZE; - if (pCmd->pQueryInfo[0].tagCond.tbnameCond.cond != NULL) { - tagLen += strlen(pCmd->pQueryInfo[0].tagCond.tbnameCond.cond) * TSDB_NCHAR_SIZE; + if (pQueryInfo->tagCond.tbnameCond.cond != NULL) { + tagLen += strlen(pQueryInfo->tagCond.tbnameCond.cond) * TSDB_NCHAR_SIZE; } int32_t joinCondLen = (TSDB_METER_ID_LEN + sizeof(int16_t)) * 2; - int32_t elemSize = sizeof(SMetricMetaElemMsg) * pCmd->pQueryInfo->numOfTables; + int32_t elemSize = sizeof(SMetricMetaElemMsg) * pQueryInfo->numOfTables; int32_t len = tagLen + joinCondLen + elemSize + defaultSize; @@ -2634,9 +2682,11 @@ int tscBuildMetricMetaMsg(SSqlObj *pSql, SSqlInfo *pInfo) { int tableIndex = 0; SSqlCmd * pCmd = &pSql->cmd; - STagCond *pTagCond = &pCmd->pQueryInfo[0].tagCond; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + + STagCond *pTagCond = &pQueryInfo->tagCond; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, tableIndex); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, tableIndex); int32_t size = tscEstimateMetricMetaMsgSize(pCmd); if (TSDB_CODE_SUCCESS != tscAllocPayload(pCmd, size)) { @@ -2653,7 +2703,7 @@ int tscBuildMetricMetaMsg(SSqlObj *pSql, SSqlInfo *pInfo) { pMsg += sizeof(SMgmtHead); pMetaMsg = (SMetricMetaMsg *)pMsg; - pMetaMsg->numOfMeters = htonl(pCmd->pQueryInfo->numOfTables); + pMetaMsg->numOfMeters = htonl(pQueryInfo->numOfTables); pMsg += sizeof(SMetricMetaMsg); @@ -2675,8 +2725,8 @@ int tscBuildMetricMetaMsg(SSqlObj *pSql, SSqlInfo *pInfo) { *(int16_t *)pMsg = pTagCond->joinInfo.right.tagCol; pMsg += sizeof(int16_t); - for (int32_t i = 0; i < pCmd->pQueryInfo->numOfTables; ++i) { - pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, i); + for (int32_t i = 0; i < pQueryInfo->numOfTables; ++i) { + pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, i); uint64_t uid = pMeterMetaInfo->pMeterMeta->uid; offset = pMsg - (char *)pMetaMsg; @@ -2719,7 +2769,7 @@ int tscBuildMetricMetaMsg(SSqlObj *pSql, SSqlInfo *pInfo) { pMsg += len; } - SSqlGroupbyExpr *pGroupby = &pCmd->pQueryInfo[0].groupbyExpr; + SSqlGroupbyExpr *pGroupby = &pQueryInfo->groupbyExpr; if (pGroupby->tableIndex != i && pGroupby->numOfGroupCols > 0) { pElem->orderType = 0; @@ -2737,8 +2787,8 @@ int tscBuildMetricMetaMsg(SSqlObj *pSql, SSqlInfo *pInfo) { offset = pMsg - (char *)pMetaMsg; pElem->groupbyTagColumnList = htonl(offset); - for (int32_t j = 0; j < pCmd->pQueryInfo[0].groupbyExpr.numOfGroupCols; ++j) { - SColIndexEx *pCol = &pCmd->pQueryInfo[0].groupbyExpr.columnInfo[j]; + for (int32_t j = 0; j < pQueryInfo->groupbyExpr.numOfGroupCols; ++j) { + SColIndexEx *pCol = &pQueryInfo->groupbyExpr.columnInfo[j]; SColIndexEx *pDestCol = (SColIndexEx *)pMsg; pDestCol->colIdxInBuf = 0; @@ -2833,7 +2883,8 @@ int tscProcessRetrieveRspFromMgmt(SSqlObj *pSql) { pRes->data = pRetrieve->data; - tscSetResultPointer(pCmd, pRes); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + tscSetResultPointer(pQueryInfo, pRes); if (pRes->numOfRows == 0) { taosAddConnIntoCache(tscConnCache, pSql->thandle, pSql->ip, pSql->vnode, pObj->user); @@ -2925,7 +2976,7 @@ int tscProcessMeterMetaRsp(SSqlObj *pSql) { pMeta->index = 0; // todo add one more function: taosAddDataIfNotExists(); - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0, 0); taosRemoveDataFromCache(tscCacheHandle, (void **)&(pMeterMetaInfo->pMeterMeta), false); pMeterMetaInfo->pMeterMeta = (SMeterMeta *)taosAddDataIntoCache(tscCacheHandle, pMeterMetaInfo->name, (char *)pMeta, @@ -3131,11 +3182,12 @@ int tscProcessMetricMetaRsp(SSqlObj *pSql) { sizes[k] = pBuf - (char *)pNewMetricMeta; } + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); for (int32_t i = 0; i < num; ++i) { char name[TSDB_MAX_TAGS_LEN + 1] = {0}; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, i); - tscGetMetricMetaCacheKey(&pSql->cmd, name, pMeterMetaInfo->pMeterMeta->uid); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, i); + tscGetMetricMetaCacheKey(&pSql->cmd, 0, name, pMeterMetaInfo->pMeterMeta->uid); #ifdef _DEBUG_VIEW printf("generate the metric key:%s, index:%d\n", name, i); @@ -3178,7 +3230,10 @@ int tscProcessShowRsp(SSqlObj *pSql) { SSqlRes * pRes = &pSql->res; SSqlCmd * pCmd = &pSql->cmd; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); //? + + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); pShow = (SShowRspMsg *)pRes->pRsp; pRes->qhandle = pShow->qhandle; @@ -3203,19 +3258,19 @@ int tscProcessShowRsp(SSqlObj *pSql) { int32_t size = pMeta->numOfColumns * sizeof(SSchema) + sizeof(SMeterMeta); pMeterMetaInfo->pMeterMeta = (SMeterMeta *)taosAddDataIntoCache(tscCacheHandle, key, (char *)pMeta, size, tsMeterMetaKeepTimer); - pCmd->numOfCols = pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; + pCmd->numOfCols = pQueryInfo->fieldsInfo.numOfOutputCols; SSchema *pMeterSchema = tsGetSchema(pMeterMetaInfo->pMeterMeta); - tscColumnBaseInfoReserve(&pCmd->pQueryInfo[0].colList, pMeta->numOfColumns); + tscColumnBaseInfoReserve(&pQueryInfo->colList, pMeta->numOfColumns); SColumnIndex index = {0}; for (int16_t i = 0; i < pMeta->numOfColumns; ++i) { index.columnIndex = i; - tscColumnBaseInfoInsert(pCmd, &index); - tscFieldInfoSetValFromSchema(&pCmd->pQueryInfo[0].fieldsInfo, i, &pMeterSchema[i]); + tscColumnBaseInfoInsert(pQueryInfo, &index); + tscFieldInfoSetValFromSchema(&pQueryInfo->fieldsInfo, i, &pMeterSchema[i]); } - tscFieldInfoCalOffset(pCmd); + tscFieldInfoCalOffset(pQueryInfo); return 0; } @@ -3257,7 +3312,7 @@ int tscProcessConnectRsp(SSqlObj *pSql) { int tscProcessUseDbRsp(SSqlObj *pSql) { STscObj * pObj = pSql->pTscObj; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0, 0); strcpy(pObj->db, pMeterMetaInfo->name); return 0; @@ -3269,7 +3324,7 @@ int tscProcessDropDbRsp(SSqlObj *UNUSED_PARAM(pSql)) { } int tscProcessDropTableRsp(SSqlObj *pSql) { - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0, 0); SMeterMeta *pMeterMeta = taosGetDataFromCache(tscCacheHandle, pMeterMetaInfo->name); if (pMeterMeta == NULL) { @@ -3296,7 +3351,7 @@ int tscProcessDropTableRsp(SSqlObj *pSql) { } int tscProcessAlterTableMsgRsp(SSqlObj *pSql) { - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0, 0); SMeterMeta *pMeterMeta = taosGetDataFromCache(tscCacheHandle, pMeterMetaInfo->name); if (pMeterMeta == NULL) { /* not in cache, abort */ @@ -3307,7 +3362,7 @@ int tscProcessAlterTableMsgRsp(SSqlObj *pSql) { taosRemoveDataFromCache(tscCacheHandle, (void **)&pMeterMeta, true); if (pMeterMetaInfo->pMeterMeta) { - bool isSuperTable = UTIL_METER_IS_METRIC(pMeterMetaInfo); + bool isSuperTable = UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo); taosRemoveDataFromCache(tscCacheHandle, (void **)&(pMeterMetaInfo->pMeterMeta), true); taosRemoveDataFromCache(tscCacheHandle, (void **)&(pMeterMetaInfo->pMetricMeta), true); @@ -3340,6 +3395,8 @@ int tscProcessRetrieveRspFromVnode(SSqlObj *pSql) { SSqlCmd *pCmd = &pSql->cmd; STscObj *pObj = pSql->pTscObj; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + SRetrieveMeterRsp *pRetrieve = (SRetrieveMeterRsp *)pRes->pRsp; pRes->numOfRows = htonl(pRetrieve->numOfRows); @@ -3349,15 +3406,15 @@ int tscProcessRetrieveRspFromVnode(SSqlObj *pSql) { pRes->useconds = htobe64(pRetrieve->useconds); pRes->data = pRetrieve->data; - tscSetResultPointer(pCmd, pRes); + tscSetResultPointer(tscGetQueryInfoDetail(pCmd, 0), pRes); pRes->row = 0; /** * If the query result is exhausted, or current query is to free resource at server side, * the connection will be recycled. */ - if ((pRes->numOfRows == 0 && !(tscProjectionQueryOnMetric(pCmd) && pRes->offset > 0)) || - ((pCmd->type & TSDB_QUERY_TYPE_FREE_RESOURCE) == TSDB_QUERY_TYPE_FREE_RESOURCE)) { + if ((pRes->numOfRows == 0 && !(tscProjectionQueryOnMetric(pCmd, 0) && pRes->offset > 0)) || + ((pQueryInfo->type & TSDB_QUERY_TYPE_FREE_RESOURCE) == TSDB_QUERY_TYPE_FREE_RESOURCE)) { tscTrace("%p no result or free resource, recycle connection", pSql); taosAddConnIntoCache(tscConnCache, pSql->thandle, pSql->ip, pSql->vnode, pObj->user); pSql->thandle = NULL; @@ -3371,12 +3428,14 @@ int tscProcessRetrieveRspFromVnode(SSqlObj *pSql) { int tscProcessRetrieveRspFromLocal(SSqlObj *pSql) { SSqlRes * pRes = &pSql->res; SSqlCmd * pCmd = &pSql->cmd; + SQueryInfo *pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + SRetrieveMeterRsp *pRetrieve = (SRetrieveMeterRsp *)pRes->pRsp; pRes->numOfRows = htonl(pRetrieve->numOfRows); pRes->data = pRetrieve->data; - - tscSetResultPointer(pCmd, pRes); + + tscSetResultPointer(pQueryInfo, pRes); pRes->row = 0; return 0; } @@ -3385,7 +3444,8 @@ void tscMeterMetaCallBack(void *param, TAOS_RES *res, int code); static int32_t tscDoGetMeterMeta(SSqlObj *pSql, char *meterId, int32_t index) { int32_t code = TSDB_CODE_SUCCESS; - + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + SSqlObj *pNew = calloc(1, sizeof(SSqlObj)); if (NULL == pNew) { tscError("%p malloc failed for new sqlobj to get meter meta", pSql); @@ -3397,10 +3457,11 @@ static int32_t tscDoGetMeterMeta(SSqlObj *pSql, char *meterId, int32_t index) { pNew->cmd.payload = NULL; pNew->cmd.allocSize = 0; - pNew->cmd.pQueryInfo = calloc(1, sizeof(SQueryInfo)); - pNew->cmd.numOfQueries = 1; + tscAddSubqueryInfo(&pNew->cmd); + pNew->cmd.numOfClause = 1; - pNew->cmd.pQueryInfo[0].defaultVal[0] = pSql->cmd.pQueryInfo[0].defaultVal[0]; // flag of create table if not exists + SQueryInfo* pNewQueryInfo = tscGetQueryInfoDetail(&pNew->cmd, 0); + pNewQueryInfo->defaultVal[0] = pQueryInfo->defaultVal[0]; // flag of create table if not exists if (TSDB_CODE_SUCCESS != tscAllocPayload(&pNew->cmd, TSDB_DEFAULT_PAYLOAD_SIZE)) { tscError("%p malloc failed for payload to get meter meta", pSql); free(pNew); @@ -3408,7 +3469,7 @@ static int32_t tscDoGetMeterMeta(SSqlObj *pSql, char *meterId, int32_t index) { return TSDB_CODE_CLI_OUT_OF_MEMORY; } - SMeterMetaInfo *pMeterMetaInfo = tscAddEmptyMeterMetaInfo(&pNew->cmd); + SMeterMetaInfo *pMeterMetaInfo = tscAddEmptyMeterMetaInfo(&pNew->cmd, 0); strcpy(pMeterMetaInfo->name, meterId); memcpy(pNew->cmd.payload, pSql->cmd.payload, TSDB_DEFAULT_PAYLOAD_SIZE); @@ -3419,7 +3480,7 @@ static int32_t tscDoGetMeterMeta(SSqlObj *pSql, char *meterId, int32_t index) { tsem_init(&pNew->emptyRspSem, 0, 1); code = tscProcessSql(pNew); - SMeterMetaInfo *pInfo = tscGetMeterMetaInfo(&pSql->cmd, index); + SMeterMetaInfo *pInfo = tscGetMeterMetaInfo(&pSql->cmd, 0, index); // update cache only on success get metermeta if (code == TSDB_CODE_SUCCESS) { @@ -3446,7 +3507,7 @@ static int32_t tscDoGetMeterMeta(SSqlObj *pSql, char *meterId, int32_t index) { int tscGetMeterMeta(SSqlObj *pSql, char *meterId, int32_t index) { SSqlCmd *pCmd = &pSql->cmd; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, index); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, index); // if the SSqlCmd owns a metermeta, release it first taosRemoveDataFromCache(tscCacheHandle, (void **)&(pMeterMetaInfo->pMeterMeta), false); @@ -3473,7 +3534,9 @@ int tscGetMeterMeta(SSqlObj *pSql, char *meterId, int32_t index) { } int tscGetMeterMetaEx(SSqlObj *pSql, char *meterId, bool createIfNotExists) { - pSql->cmd.pQueryInfo[0].defaultVal[0] = createIfNotExists ? 1 : 0; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + + pQueryInfo->defaultVal[0] = createIfNotExists ? 1 : 0; return tscGetMeterMeta(pSql, meterId, 0); } @@ -3498,10 +3561,12 @@ static void tscWaitingForCreateTable(SSqlCmd *pCmd) { */ int tscRenewMeterMeta(SSqlObj *pSql, char *meterId) { int code = 0; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0); - + // handle metric meta renew process SSqlCmd *pCmd = &pSql->cmd; + + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); // enforce the renew metermeta operation in async model if (pSql->fp == NULL) pSql->fp = (void *)0x1; @@ -3515,7 +3580,7 @@ int tscRenewMeterMeta(SSqlObj *pSql, char *meterId) { tscTrace("%p update meter meta, old: numOfTags:%d, numOfCols:%d, uid:%" PRId64 ", addr:%p", pSql, pMeterMetaInfo->numOfTags, pCmd->numOfCols, pMeterMetaInfo->pMeterMeta->uid, pMeterMetaInfo->pMeterMeta); } - tscWaitingForCreateTable(&pSql->cmd); + tscWaitingForCreateTable(pCmd); taosRemoveDataFromCache(tscCacheHandle, (void **)&(pMeterMetaInfo->pMeterMeta), true); code = tscDoGetMeterMeta(pSql, meterId, 0); // todo ?? @@ -3542,11 +3607,14 @@ int tscGetMetricMeta(SSqlObj *pSql) { * the vnode query condition is serialized into pCmd->payload, we need to rebuild key for metricmeta info in cache. */ bool reqMetricMeta = false; - for (int32_t i = 0; i < pSql->cmd.pQueryInfo[0].numOfTables; ++i) { + int32_t subClauseIndex = 0; + + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, subClauseIndex); + for (int32_t i = 0; i < pQueryInfo->numOfTables; ++i) { char tagstr[TSDB_MAX_TAGS_LEN + 1] = {0}; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, i); - tscGetMetricMetaCacheKey(pCmd, tagstr, pMeterMetaInfo->pMeterMeta->uid); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, i); + tscGetMetricMetaCacheKey(pCmd, subClauseIndex, tagstr, pMeterMetaInfo->pMeterMeta->uid); taosRemoveDataFromCache(tscCacheHandle, (void **)&(pMeterMetaInfo->pMetricMeta), false); @@ -3570,11 +3638,11 @@ int tscGetMetricMeta(SSqlObj *pSql) { pNew->cmd.command = TSDB_SQL_METRIC; - for (int32_t i = 0; i < pSql->cmd.pQueryInfo[0].numOfTables; ++i) { - SMeterMetaInfo *pMMInfo = tscGetMeterMetaInfo(&pSql->cmd, i); + for (int32_t i = 0; i < pQueryInfo->numOfTables; ++i) { + SMeterMetaInfo *pMMInfo = tscGetMeterMetaInfo(&pSql->cmd, 0, i); SMeterMeta *pMeterMeta = taosGetDataFromCache(tscCacheHandle, pMMInfo->name); - tscAddMeterMetaInfo(&pNew->cmd, pMMInfo->name, pMeterMeta, NULL, pMMInfo->numOfTags, pMMInfo->tagColumnIndex); + tscAddMeterMetaInfo(&pNew->cmd, 0, pMMInfo->name, pMeterMeta, NULL, pMMInfo->numOfTags, pMMInfo->tagColumnIndex); } if ((code = tscAllocPayload(&pNew->cmd, TSDB_DEFAULT_PAYLOAD_SIZE)) != TSDB_CODE_SUCCESS) { @@ -3583,16 +3651,18 @@ int tscGetMetricMeta(SSqlObj *pSql) { } // the query condition on meter is serialized into payload - tscTagCondCopy(&pNew->cmd.pQueryInfo[0].tagCond, &pSql->cmd.pQueryInfo[0].tagCond); + SQueryInfo* pNewQueryInfo = tscGetQueryInfoDetail(&pNew->cmd, 0); + tscTagCondCopy(&pNewQueryInfo->tagCond, &pQueryInfo->tagCond); - pNew->cmd.pQueryInfo[0].groupbyExpr = pSql->cmd.pQueryInfo[0].groupbyExpr; - pNew->cmd.pQueryInfo[0].numOfTables = pSql->cmd.pQueryInfo[0].numOfTables; + pNewQueryInfo->groupbyExpr = pQueryInfo->groupbyExpr; + pNewQueryInfo->numOfTables = pQueryInfo->numOfTables; - pNew->cmd.pQueryInfo[0].slimit = pSql->cmd.pQueryInfo[0].slimit; - pNew->cmd.order = pSql->cmd.order; + pNewQueryInfo->slimit = pQueryInfo->slimit; + pNewQueryInfo->order = pQueryInfo->order; if (pSql->fp != NULL && pSql->pStream == NULL) { - tscFreeSqlCmdData(&pSql->cmd); + pCmd->pDataBlocks = tscDestroyBlockArrayList(pCmd->pDataBlocks); + tscFreeSubqueryInfo(pCmd); } tscTrace("%p allocate new pSqlObj:%p to get metricMeta", pSql, pNew); @@ -3602,11 +3672,11 @@ int tscGetMetricMeta(SSqlObj *pSql) { code = tscProcessSql(pNew); - for (int32_t i = 0; i < pCmd->pQueryInfo->numOfTables; ++i) { + for (int32_t i = 0; i < pQueryInfo->numOfTables; ++i) { char tagstr[TSDB_MAX_TAGS_LEN] = {0}; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, i); - tscGetMetricMetaCacheKey(pCmd, tagstr, pMeterMetaInfo->pMeterMeta->uid); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, i); + tscGetMetricMetaCacheKey(pCmd, 0, tagstr, pMeterMetaInfo->pMeterMeta->uid); #ifdef _DEBUG_VIEW printf("create metric key:%s, index:%d\n", tagstr, i); diff --git a/src/client/src/tscSql.c b/src/client/src/tscSql.c index 4cd442996372529d299ed5489fa1d9172bd20e25..291090cf6a1543efcaf5e943405ec7d0ca5b86d8 100644 --- a/src/client/src/tscSql.c +++ b/src/client/src/tscSql.c @@ -128,8 +128,7 @@ TAOS *taos_connect_imp(const char *ip, const char *user, const char *pass, const } pSql->cmd.command = TSDB_SQL_CONNECT; - int ret = tscAllocPayload(&pSql->cmd, TSDB_DEFAULT_PAYLOAD_SIZE); - if (TSDB_CODE_SUCCESS != ret) { + if (TSDB_CODE_SUCCESS != tscAllocPayload(&pSql->cmd, TSDB_DEFAULT_PAYLOAD_SIZE)) { globalCode = TSDB_CODE_CLI_OUT_OF_MEMORY; free(pSql); free(pObj); @@ -298,7 +297,8 @@ int taos_num_fields(TAOS_RES *res) { SSqlObj *pSql = (SSqlObj *)res; if (pSql == NULL || pSql->signature != pSql) return 0; - SFieldInfo *pFieldsInfo = &pSql->cmd.pQueryInfo[0].fieldsInfo; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + SFieldInfo *pFieldsInfo = &pQueryInfo->fieldsInfo; return (pFieldsInfo->numOfOutputCols - pFieldsInfo->numOfHiddenCols); } @@ -320,8 +320,9 @@ int taos_affected_rows(TAOS *taos) { TAOS_FIELD *taos_fetch_fields(TAOS_RES *res) { SSqlObj *pSql = (SSqlObj *)res; if (pSql == NULL || pSql->signature != pSql) return 0; - - return pSql->cmd.pQueryInfo[0].fieldsInfo.pFields; + + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + return pQueryInfo->fieldsInfo.pFields; } int taos_retrieve(TAOS_RES *res) { @@ -370,31 +371,33 @@ int taos_fetch_block_impl(TAOS_RES *res, TAOS_ROW *rows) { pRes->numOfTotal += pRes->numOfRows; } - for (int i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - pRes->tsrow[i] = TSC_GET_RESPTR_BASE(pRes, pCmd, i, pCmd->order) + - pRes->bytes[i] * (1 - pCmd->order.order) * (pRes->numOfRows - 1); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + for (int i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + pRes->tsrow[i] = TSC_GET_RESPTR_BASE(pRes, pQueryInfo, i, pQueryInfo->order) + + pRes->bytes[i] * (1 - pQueryInfo->order.order) * (pRes->numOfRows - 1); } *rows = pRes->tsrow; - return (pCmd->order.order == TSQL_SO_DESC) ? pRes->numOfRows : -pRes->numOfRows; + return (pQueryInfo->order.order == TSQL_SO_DESC) ? pRes->numOfRows : -pRes->numOfRows; } static void **doSetResultRowData(SSqlObj *pSql) { SSqlCmd *pCmd = &pSql->cmd; SSqlRes *pRes = &pSql->res; - + SQueryInfo *pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + int32_t num = 0; - for (int i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - pRes->tsrow[i] = TSC_GET_RESPTR_BASE(pRes, pCmd, i, pCmd->order) + pRes->bytes[i] * pRes->row; + for (int i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + pRes->tsrow[i] = TSC_GET_RESPTR_BASE(pRes, pQueryInfo, i, pQueryInfo->order) + pRes->bytes[i] * pRes->row; // primary key column cannot be null in interval query, no need to check - if (i == 0 && pCmd->pQueryInfo[0].nAggTimeInterval > 0) { + if (i == 0 && pQueryInfo->nAggTimeInterval > 0) { continue; } - TAOS_FIELD *pField = tscFieldInfoGetField(pCmd, i); + TAOS_FIELD *pField = tscFieldInfoGetField(pQueryInfo, i); if (isNull(pRes->tsrow[i], pField->type)) { pRes->tsrow[i] = NULL; @@ -419,7 +422,7 @@ static void **doSetResultRowData(SSqlObj *pSql) { } } - assert(num <= pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols); + assert(num <= pQueryInfo->fieldsInfo.numOfOutputCols); return pRes->tsrow; } @@ -437,15 +440,17 @@ static bool tscHashRemainDataInSubqueryResultSet(SSqlObj *pSql) { bool hasData = true; SSqlCmd *pCmd = &pSql->cmd; - if (tscProjectionQueryOnMetric(pCmd)) { + if (tscProjectionQueryOnMetric(pCmd, 0)) { bool allSubqueryExhausted = true; for (int32_t i = 0; i < pSql->numOfSubs; ++i) { SSqlRes *pRes1 = &pSql->pSubs[i]->res; SSqlCmd *pCmd1 = &pSql->pSubs[i]->cmd; - SMeterMetaInfo *pMetaInfo = tscGetMeterMetaInfo(pCmd1, 0); - assert(pCmd1->pQueryInfo[0].numOfTables == 1); + SMeterMetaInfo *pMetaInfo = tscGetMeterMetaInfo(pCmd1, 0, 0); + + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + assert(pQueryInfo->numOfTables == 1); /* * if the global limitation is not reached, and current result has not exhausted, or next more vnodes are @@ -462,10 +467,10 @@ static bool tscHashRemainDataInSubqueryResultSet(SSqlObj *pSql) { } else { // otherwise, in case inner join, if any subquery exhausted, query completed. for (int32_t i = 0; i < pSql->numOfSubs; ++i) { SSqlRes *pRes1 = &pSql->pSubs[i]->res; - + SQueryInfo *pQueryInfo = tscGetQueryInfoDetail(&pSql->pSubs[i]->cmd, 0); + if ((pRes1->row >= pRes1->numOfRows && tscHasReachLimitation(pSql->pSubs[i]) && - tscProjectionQueryOnTable(&pSql->pSubs[i]->cmd)) || - (pRes1->numOfRows == 0)) { + tscProjectionQueryOnTable(pQueryInfo)) || (pRes1->numOfRows == 0)) { hasData = false; break; @@ -477,7 +482,6 @@ static bool tscHashRemainDataInSubqueryResultSet(SSqlObj *pSql) { } static void **tscJoinResultsetFromBuf(SSqlObj *pSql) { - SSqlCmd *pCmd = &pSql->cmd; SSqlRes *pRes = &pSql->res; while (1) { @@ -498,9 +502,11 @@ static void **tscJoinResultsetFromBuf(SSqlObj *pSql) { free(pState); return NULL; } - + + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + if (pRes->tsrow == NULL) { - pRes->tsrow = malloc(POINTER_BYTES * pCmd->pQueryInfo[0].exprsInfo.numOfExprs); + pRes->tsrow = malloc(POINTER_BYTES * pQueryInfo->exprsInfo.numOfExprs); } bool success = false; @@ -526,7 +532,7 @@ static void **tscJoinResultsetFromBuf(SSqlObj *pSql) { } if (success) { // current row of final output has been built, return to app - for (int32_t i = 0; i < pCmd->pQueryInfo[0].exprsInfo.numOfExprs; ++i) { + for (int32_t i = 0; i < pQueryInfo->exprsInfo.numOfExprs; ++i) { int32_t tableIndex = pRes->pColumnIndex[i].tableIndex; int32_t columnIndex = pRes->pColumnIndex[i].columnIndex; @@ -599,8 +605,11 @@ TAOS_ROW taos_fetch_row(TAOS_RES *res) { // projection query on metric, pipeline retrieve data from vnode list, instead of two-stage merge TAOS_ROW rows = taos_fetch_row_impl(res); - while (rows == NULL && tscProjectionQueryOnMetric(pCmd)) { - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + + while (rows == NULL && tscProjectionQueryOnMetric(pCmd, 0)) { + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); // reach the maximum number of output rows, abort if (tscHasReachLimitation(pSql)) { @@ -611,8 +620,8 @@ TAOS_ROW taos_fetch_row(TAOS_RES *res) { * update the limit and offset value according to current retrieval results * Note: if pRes->offset > 0, pRes->numOfRows = 0, pRes->numOfTotal = 0; */ - pCmd->pQueryInfo->limit.limit = pCmd->globalLimit - pRes->numOfTotal; - pCmd->pQueryInfo->limit.offset = pRes->offset; + pQueryInfo->limit.limit = pCmd->globalLimit - pRes->numOfTotal; + pQueryInfo->limit.offset = pRes->offset; assert((pRes->offset >= 0 && pRes->numOfRows == 0) || (pRes->offset == 0 && pRes->numOfRows >= 0)); @@ -656,17 +665,19 @@ int taos_fetch_block(TAOS_RES *res, TAOS_ROW *rows) { // projection query on metric, pipeline retrieve data from vnode list, // instead of two-stage mergevnodeProcessMsgFromShell free qhandle nRows = taos_fetch_block_impl(res, rows); - while (*rows == NULL && tscProjectionQueryOnMetric(pCmd)) { + + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + while (*rows == NULL && tscProjectionQueryOnMetric(pCmd, 0)) { /* reach the maximum number of output rows, abort */ if (tscHasReachLimitation(pSql)) { return 0; } - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); /* update the limit value according to current retrieval results */ - pCmd->pQueryInfo->limit.limit = pSql->cmd.globalLimit - pRes->numOfTotal; - pCmd->pQueryInfo->limit.offset = pRes->offset; + pQueryInfo->limit.limit = pSql->cmd.globalLimit - pRes->numOfTotal; + pQueryInfo->limit.offset = pRes->offset; if ((++pMeterMetaInfo->vnodeIndex) < pMeterMetaInfo->pMetricMeta->numOfVnodes) { pSql->cmd.command = TSDB_SQL_SELECT; @@ -723,9 +734,10 @@ void taos_free_result(TAOS_RES *res) { } // set freeFlag to 1 in retrieve message if there are un-retrieved results - pCmd->type = TSDB_QUERY_TYPE_FREE_RESOURCE; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + pQueryInfo->type = TSDB_QUERY_TYPE_FREE_RESOURCE; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); /* * case 1. Partial data have been retrieved from vnodes, but not all data has been retrieved yet. @@ -971,7 +983,6 @@ int taos_validate_sql(TAOS *taos, const char *sql) { static int tscParseTblNameList(SSqlObj *pSql, const char *tblNameList, int32_t tblListLen) { // must before clean the sqlcmd object - tscRemoveAllMeterMetaInfo(&pSql->cmd, false); tscCleanSqlCmd(&pSql->cmd); SSqlCmd *pCmd = &pSql->cmd; @@ -982,7 +993,7 @@ static int tscParseTblNameList(SSqlObj *pSql, const char *tblNameList, int32_t t int code = TSDB_CODE_INVALID_METER_ID; char *str = (char *)tblNameList; - SMeterMetaInfo *pMeterMetaInfo = tscAddEmptyMeterMetaInfo(pCmd); + SMeterMetaInfo *pMeterMetaInfo = tscAddEmptyMeterMetaInfo(pCmd, 0); if ((code = tscAllocPayload(pCmd, tblListLen + 16)) != TSDB_CODE_SUCCESS) { return code; @@ -1017,7 +1028,7 @@ static int tscParseTblNameList(SSqlObj *pSql, const char *tblNameList, int32_t t return code; } - if ((code = setMeterID(pSql, &sToken, 0)) != TSDB_CODE_SUCCESS) { + if ((code = setMeterID(pSql, 0, &sToken, 0)) != TSDB_CODE_SUCCESS) { return code; } diff --git a/src/client/src/tscStream.c b/src/client/src/tscStream.c index 95c5e2fbc6a44d13dfdb6f2a25f1684d79c81395..e5fdd65accac433de19b294b7008551737713cbf 100644 --- a/src/client/src/tscStream.c +++ b/src/client/src/tscStream.c @@ -31,9 +31,9 @@ static void tscProcessStreamRetrieveResult(void *param, TAOS_RES *res, int numOf static void tscSetNextLaunchTimer(SSqlStream *pStream, SSqlObj *pSql); static void tscSetRetryTimer(SSqlStream *pStream, SSqlObj *pSql, int64_t timer); -static bool isProjectStream(SSqlCmd *pCmd) { - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - SSqlExpr *pExpr = tscSqlExprGet(pCmd, i); +static bool isProjectStream(SQueryInfo* pQueryInfo) { + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + SSqlExpr *pExpr = tscSqlExprGet(pQueryInfo, i); if (pExpr->functionId != TSDB_FUNC_PRJ) { return false; } @@ -66,21 +66,23 @@ static void tscProcessStreamLaunchQuery(SSchedMsg *pMsg) { pSql->fp = tscProcessStreamQueryCallback; pSql->param = pStream; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0); + + SQueryInfo *pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); int code = tscGetMeterMeta(pSql, pMeterMetaInfo->name, 0); pSql->res.code = code; if (code == TSDB_CODE_ACTION_IN_PROGRESS) return; - if (code == 0 && UTIL_METER_IS_METRIC(pMeterMetaInfo)) { + if (code == 0 && UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo)) { code = tscGetMetricMeta(pSql); pSql->res.code = code; if (code == TSDB_CODE_ACTION_IN_PROGRESS) return; } - tscTansformSQLFunctionForMetricQuery(&pSql->cmd); + tscTansformSQLFunctionForMetricQuery(pQueryInfo); // failed to get meter/metric meta, retry in 10sec. if (code != TSDB_CODE_SUCCESS) { @@ -105,22 +107,23 @@ static void tscProcessStreamTimer(void *handle, void *tmrId) { pStream->numOfRes = 0; // reset the numOfRes. SSqlObj *pSql = pStream->pSql; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); tscTrace("%p add into timer", pSql); - if (isProjectStream(&pSql->cmd)) { + if (isProjectStream(pQueryInfo)) { /* - * pSql->cmd.pQueryInfo[0].etime, which is the start time, does not change in case of + * pQueryInfo->etime, which is the start time, does not change in case of * repeat first execution, once the first execution failed. */ - pSql->cmd.pQueryInfo[0].stime = pStream->stime; // start time + pQueryInfo->stime = pStream->stime; // start time - pSql->cmd.pQueryInfo[0].etime = taosGetTimestamp(pStream->precision); // end time - if (pSql->cmd.pQueryInfo[0].etime > pStream->etime) { - pSql->cmd.pQueryInfo[0].etime = pStream->etime; + pQueryInfo->etime = taosGetTimestamp(pStream->precision); // end time + if (pQueryInfo->etime > pStream->etime) { + pQueryInfo->etime = pStream->etime; } } else { - pSql->cmd.pQueryInfo[0].stime = pStream->stime - pStream->interval; - pSql->cmd.pQueryInfo[0].etime = pStream->stime - 1; + pQueryInfo->stime = pStream->stime - pStream->interval; + pQueryInfo->etime = pStream->stime - 1; } // launch stream computing in a new thread @@ -139,7 +142,7 @@ static void tscProcessStreamQueryCallback(void *param, TAOS_RES *tres, int numOf tscError("%p stream:%p, query data failed, code:%d, retry in %" PRId64 "ms", pStream->pSql, pStream, numOfRows, retryDelay); - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(&pStream->pSql->cmd, 0); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(&pStream->pSql->cmd, 0, 0); tscClearMeterMetaInfo(pMeterMetaInfo, true); tscSetRetryTimer(pStream, pStream->pSql, retryDelay); @@ -165,7 +168,7 @@ static void tscSetTimestampForRes(SSqlStream *pStream, SSqlObj *pSql) { static void tscProcessStreamRetrieveResult(void *param, TAOS_RES *res, int numOfRows) { SSqlStream * pStream = (SSqlStream *)param; SSqlObj * pSql = (SSqlObj *)res; - SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0); + SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0, 0); if (pSql == NULL || numOfRows < 0) { int64_t retryDelayTime = tscGetRetryDelayTime(pStream->slidingTime, pStream->precision); @@ -178,11 +181,12 @@ static void tscProcessStreamRetrieveResult(void *param, TAOS_RES *res, int numOf if (numOfRows > 0) { // when reaching here the first execution of stream computing is successful. pStream->numOfRes += numOfRows; - + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + for(int32_t i = 0; i < numOfRows; ++i) { TAOS_ROW row = taos_fetch_row(res); tscTrace("%p stream:%p fetch result", pSql, pStream); - if (isProjectStream(&pSql->cmd)) { + if (isProjectStream(pQueryInfo)) { pStream->stime = *(TSKEY *)row[0]; } else { tscSetTimestampForRes(pStream, pSql); @@ -197,9 +201,10 @@ static void tscProcessStreamRetrieveResult(void *param, TAOS_RES *res, int numOf } else { // numOfRows == 0, all data has been retrieved pStream->useconds += pSql->res.useconds; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + if (pStream->numOfRes == 0) { - if (pSql->cmd.pQueryInfo[0].interpoType == TSDB_INTERPO_SET_VALUE || pSql->cmd.pQueryInfo[0].interpoType == TSDB_INTERPO_NULL) { - SSqlCmd *pCmd = &pSql->cmd; + if (pQueryInfo->interpoType == TSDB_INTERPO_SET_VALUE || pQueryInfo->interpoType == TSDB_INTERPO_NULL) { SSqlRes *pRes = &pSql->res; /* failed to retrieve any result in this retrieve */ @@ -209,12 +214,12 @@ static void tscProcessStreamRetrieveResult(void *param, TAOS_RES *res, int numOf void *oldPtr = pSql->res.data; pSql->res.data = tmpRes; + + for (int32_t i = 1; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + int16_t offset = tscFieldInfoGetOffset(pQueryInfo, i); + TAOS_FIELD *pField = tscFieldInfoGetField(pQueryInfo, i); - for (int32_t i = 1; i < pSql->cmd.pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - int16_t offset = tscFieldInfoGetOffset(pCmd, i); - TAOS_FIELD *pField = tscFieldInfoGetField(pCmd, i); - - assignVal(pSql->res.data + offset, (char *)(&pCmd->pQueryInfo[0].defaultVal[i]), pField->bytes, pField->type); + assignVal(pSql->res.data + offset, (char *)(&pQueryInfo->defaultVal[i]), pField->bytes, pField->type); row[i] = pSql->res.data + offset; } @@ -222,7 +227,7 @@ static void tscProcessStreamRetrieveResult(void *param, TAOS_RES *res, int numOf row[0] = pRes->data; // char result[512] = {0}; - // taos_print_row(result, row, pSql->cmd.pQueryInfo[0].fieldsInfo.pFields, pSql->cmd.pQueryInfo[0].fieldsInfo.numOfOutputCols); + // taos_print_row(result, row, pQueryInfo->fieldsInfo.pFields, pQueryInfo->fieldsInfo.numOfOutputCols); // tscPrint("%p stream:%p query result: %s", pSql, pStream, result); tscTrace("%p stream:%p fetch result", pSql, pStream); @@ -231,7 +236,7 @@ static void tscProcessStreamRetrieveResult(void *param, TAOS_RES *res, int numOf pRes->numOfRows = 0; pRes->data = oldPtr; - } else if (isProjectStream(&pSql->cmd)) { + } else if (isProjectStream(pQueryInfo)) { /* no resuls in the query range, retry */ // todo set retry dynamic time int32_t retry = tsProjectExecInterval; @@ -242,7 +247,7 @@ static void tscProcessStreamRetrieveResult(void *param, TAOS_RES *res, int numOf return; } } else { - if (isProjectStream(&pSql->cmd)) { + if (isProjectStream(pQueryInfo)) { pStream->stime += 1; } } @@ -257,7 +262,9 @@ static void tscProcessStreamRetrieveResult(void *param, TAOS_RES *res, int numOf } static void tscSetRetryTimer(SSqlStream *pStream, SSqlObj *pSql, int64_t timer) { - if (isProjectStream(&pSql->cmd)) { + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + + if (isProjectStream(pQueryInfo)) { int64_t now = taosGetTimestamp(pStream->precision); int64_t etime = now > pStream->etime ? pStream->etime : now; @@ -291,8 +298,9 @@ static void tscSetRetryTimer(SSqlStream *pStream, SSqlObj *pSql, int64_t timer) static void tscSetNextLaunchTimer(SSqlStream *pStream, SSqlObj *pSql) { int64_t timer = 0; - - if (isProjectStream(&pSql->cmd)) { + + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + if (isProjectStream(pQueryInfo)) { /* * for project query, no mater fetch data successfully or not, next launch will issue * more than the sliding time window @@ -348,55 +356,56 @@ static void tscSetNextLaunchTimer(SSqlStream *pStream, SSqlObj *pSql) { } static void tscSetSlidingWindowInfo(SSqlObj *pSql, SSqlStream *pStream) { - SSqlCmd *pCmd = &pSql->cmd; - int64_t minIntervalTime = (pStream->precision == TSDB_TIME_PRECISION_MICRO) ? tsMinIntervalTime * 1000L : tsMinIntervalTime; - if (pCmd->pQueryInfo[0].nAggTimeInterval < minIntervalTime) { + + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + + if (pQueryInfo->nAggTimeInterval < minIntervalTime) { tscWarn("%p stream:%p, original sample interval:%ld too small, reset to:%" PRId64 "", pSql, pStream, - pCmd->pQueryInfo[0].nAggTimeInterval, minIntervalTime); - pCmd->pQueryInfo[0].nAggTimeInterval = minIntervalTime; + pQueryInfo->nAggTimeInterval, minIntervalTime); + pQueryInfo->nAggTimeInterval = minIntervalTime; } - pStream->interval = pCmd->pQueryInfo[0].nAggTimeInterval; // it shall be derived from sql string + pStream->interval = pQueryInfo->nAggTimeInterval; // it shall be derived from sql string - if (pCmd->pQueryInfo[0].nSlidingTime == 0) { - pCmd->pQueryInfo[0].nSlidingTime = pCmd->pQueryInfo[0].nAggTimeInterval; + if (pQueryInfo->nSlidingTime == 0) { + pQueryInfo->nSlidingTime = pQueryInfo->nAggTimeInterval; } int64_t minSlidingTime = (pStream->precision == TSDB_TIME_PRECISION_MICRO) ? tsMinSlidingTime * 1000L : tsMinSlidingTime; - if (pCmd->pQueryInfo[0].nSlidingTime < minSlidingTime) { + if (pQueryInfo->nSlidingTime < minSlidingTime) { tscWarn("%p stream:%p, original sliding value:%" PRId64 " too small, reset to:%" PRId64 "", pSql, pStream, - pCmd->pQueryInfo[0].nSlidingTime, minSlidingTime); + pQueryInfo->nSlidingTime, minSlidingTime); - pCmd->pQueryInfo[0].nSlidingTime = minSlidingTime; + pQueryInfo->nSlidingTime = minSlidingTime; } - if (pCmd->pQueryInfo[0].nSlidingTime > pCmd->pQueryInfo[0].nAggTimeInterval) { + if (pQueryInfo->nSlidingTime > pQueryInfo->nAggTimeInterval) { tscWarn("%p stream:%p, sliding value:%" PRId64 " can not be larger than interval range, reset to:%" PRId64 "", pSql, pStream, - pCmd->pQueryInfo[0].nSlidingTime, pCmd->pQueryInfo[0].nAggTimeInterval); + pQueryInfo->nSlidingTime, pQueryInfo->nAggTimeInterval); - pCmd->pQueryInfo[0].nSlidingTime = pCmd->pQueryInfo[0].nAggTimeInterval; + pQueryInfo->nSlidingTime = pQueryInfo->nAggTimeInterval; } - pStream->slidingTime = pCmd->pQueryInfo[0].nSlidingTime; + pStream->slidingTime = pQueryInfo->nSlidingTime; } static int64_t tscGetStreamStartTimestamp(SSqlObj *pSql, SSqlStream *pStream, int64_t stime) { - SSqlCmd *pCmd = &pSql->cmd; - - if (isProjectStream(pCmd)) { + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); + + if (isProjectStream(pQueryInfo)) { // no data in table, flush all data till now to destination meter, 10sec delay pStream->interval = tsProjectExecInterval; pStream->slidingTime = tsProjectExecInterval; if (stime != 0) { // first projection start from the latest event timestamp - assert(stime >= pCmd->pQueryInfo[0].stime); + assert(stime >= pQueryInfo->stime); stime += 1; // exclude the last records from table } else { - stime = pCmd->pQueryInfo[0].stime; + stime = pQueryInfo->stime; } } else { // timewindow based aggregation stream if (stime == 0) { // no data in meter till now @@ -520,7 +529,8 @@ TAOS_STREAM *taos_open_stream(TAOS *taos, const char *sqlstr, void (*fp)(void *p return NULL; } - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); pStream->fp = fp; pStream->callback = callback; @@ -529,7 +539,7 @@ TAOS_STREAM *taos_open_stream(TAOS *taos, const char *sqlstr, void (*fp)(void *p pStream->precision = pMeterMetaInfo->pMeterMeta->precision; pStream->ctime = taosGetTimestamp(pStream->precision); - pStream->etime = pCmd->pQueryInfo[0].etime; + pStream->etime = pQueryInfo->etime; pSql->pStream = pStream; tscAddIntoStreamList(pStream); diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index 400171eaa237e84ed15732c7248555dfbf44c878..2409978ebefc0dd7825bc21b63954cf64731713f 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -37,9 +37,12 @@ * fullmetername + '.' + '(nil)' + '.' + '(nil)' + relation + '.' + [tagId1, * tagId2,...] + '.' + group_orderType */ -void tscGetMetricMetaCacheKey(SSqlCmd* pCmd, char* str, uint64_t uid) { - int32_t index = -1; - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoByUid(pCmd, uid, &index); +void tscGetMetricMetaCacheKey(SSqlCmd* pCmd, int32_t subClauseIndex, char* str, uint64_t uid) { + int32_t index = -1; + + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoByUid(pQueryInfo, subClauseIndex, uid, &index); int32_t len = 0; char tagIdBuf[128] = {0}; @@ -47,7 +50,7 @@ void tscGetMetricMetaCacheKey(SSqlCmd* pCmd, char* str, uint64_t uid) { len += sprintf(&tagIdBuf[len], "%d,", pMeterMetaInfo->tagColumnIndex[i]); } - STagCond* pTagCond = &pCmd->pQueryInfo[0].tagCond; + STagCond* pTagCond = &pQueryInfo->tagCond; assert(len < tListLen(tagIdBuf)); const int32_t maxKeySize = TSDB_MAX_TAGS_LEN; // allowed max key size @@ -73,7 +76,7 @@ void tscGetMetricMetaCacheKey(SSqlCmd* pCmd, char* str, uint64_t uid) { int32_t keyLen = snprintf(tmp, bufSize, "%s,%s,%s,%d,%s,[%s],%d", pMeterMetaInfo->name, (cond != NULL ? cond->cond : NULL), (tbnameCondLen > 0 ? pTagCond->tbnameCond.cond : NULL), - pTagCond->relType, join, tagIdBuf, pCmd->pQueryInfo[0].groupbyExpr.orderType); + pTagCond->relType, join, tagIdBuf, pQueryInfo->groupbyExpr.orderType); assert(keyLen <= bufSize); @@ -115,13 +118,15 @@ void tsSetMetricQueryCond(STagCond* pTagCond, uint64_t uid, const char* str) { } bool tscQueryOnMetric(SSqlCmd* pCmd) { - return ((pCmd->type & TSDB_QUERY_TYPE_STABLE_QUERY) == TSDB_QUERY_TYPE_STABLE_QUERY) && + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + + return ((pQueryInfo->type & TSDB_QUERY_TYPE_STABLE_QUERY) == TSDB_QUERY_TYPE_STABLE_QUERY) && (pCmd->msgType == TSDB_MSG_TYPE_QUERY); } -bool tscQueryMetricTags(SSqlCmd* pCmd) { - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - if (tscSqlExprGet(pCmd, i)->functionId != TSDB_FUNC_TAGPRJ) { +bool tscQueryMetricTags(SQueryInfo* pQueryInfo) { + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + if (tscSqlExprGet(pQueryInfo, i)->functionId != TSDB_FUNC_TAGPRJ) { return false; } } @@ -133,8 +138,10 @@ bool tscIsSelectivityWithTagQuery(SSqlCmd* pCmd) { bool hasTags = false; int32_t numOfSelectivity = 0; - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - int32_t functId = tscSqlExprGet(pCmd, i)->functionId; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + int32_t functId = tscSqlExprGet(pQueryInfo, i)->functionId; if (functId == TSDB_FUNC_TAG_DUMMY) { hasTags = true; continue; @@ -204,46 +211,53 @@ SMeterSidExtInfo* tscGetMeterSidInfo(SVnodeSidList* pSidList, int32_t idx) { bool tscIsTwoStageMergeMetricQuery(SSqlCmd* pCmd) { assert(pCmd != NULL); - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + int32_t subClauseIndex = 0; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, subClauseIndex); + if (pQueryInfo == NULL) { + return false; + } + + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); if (pMeterMetaInfo == NULL || pMeterMetaInfo->pMetricMeta == NULL) { return false; } // for projection query, iterate all qualified vnodes sequentially - if (tscProjectionQueryOnMetric(pCmd)) { + if (tscProjectionQueryOnMetric(pCmd, subClauseIndex)) { return false; } - if (((pCmd->type & TSDB_QUERY_TYPE_STABLE_SUBQUERY) != TSDB_QUERY_TYPE_STABLE_SUBQUERY) && + if (((pQueryInfo->type & TSDB_QUERY_TYPE_STABLE_SUBQUERY) != TSDB_QUERY_TYPE_STABLE_SUBQUERY) && pCmd->command == TSDB_SQL_SELECT) { - return UTIL_METER_IS_METRIC(pMeterMetaInfo); + return UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo); } return false; } -bool tscProjectionQueryOnMetric(SSqlCmd* pCmd) { +bool tscProjectionQueryOnMetric(SSqlCmd* pCmd, int32_t subClauseIndex) { assert(pCmd != NULL); - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, subClauseIndex); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); /* * In following cases, return false for project query on metric * 1. failed to get metermeta from server; 2. not a metric; 3. limit 0; 4. show query, instead of a select query */ - if (pMeterMetaInfo == NULL || !UTIL_METER_IS_METRIC(pMeterMetaInfo) || - pCmd->command == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pCmd->pQueryInfo[0].exprsInfo.numOfExprs == 0) { + if (pMeterMetaInfo == NULL || !UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo) || + pCmd->command == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pQueryInfo->exprsInfo.numOfExprs == 0) { return false; } // only query on tag, not a projection query - if (tscQueryMetricTags(pCmd)) { + if (tscQueryMetricTags(pQueryInfo)) { return false; } // for project query, only the following two function is allowed - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - int32_t functionId = tscSqlExprGet(pCmd, i)->functionId; + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + int32_t functionId = tscSqlExprGet(pQueryInfo, i)->functionId; if (functionId != TSDB_FUNC_PRJ && functionId != TSDB_FUNC_TAGPRJ && functionId != TSDB_FUNC_TAG && functionId != TSDB_FUNC_TS && functionId != TSDB_FUNC_ARITHM) { return false; @@ -253,9 +267,9 @@ bool tscProjectionQueryOnMetric(SSqlCmd* pCmd) { return true; } -bool tscProjectionQueryOnTable(SSqlCmd* pCmd) { - for (int32_t i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; ++i) { - int32_t functionId = tscSqlExprGet(pCmd, i)->functionId; +bool tscProjectionQueryOnTable(SQueryInfo* pQueryInfo) { + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { + int32_t functionId = tscSqlExprGet(pQueryInfo, i)->functionId; if (functionId != TSDB_FUNC_PRJ && functionId != TSDB_FUNC_TS) { return false; } @@ -264,9 +278,9 @@ bool tscProjectionQueryOnTable(SSqlCmd* pCmd) { return true; } -bool tscIsPointInterpQuery(SSqlCmd* pCmd) { - for (int32_t i = 0; i < pCmd->pQueryInfo[0].exprsInfo.numOfExprs; ++i) { - SSqlExpr* pExpr = tscSqlExprGet(pCmd, i); +bool tscIsPointInterpQuery(SQueryInfo* pQueryInfo) { + for (int32_t i = 0; i < pQueryInfo->exprsInfo.numOfExprs; ++i) { + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i); if (pExpr == NULL) { return false; } @@ -283,9 +297,9 @@ bool tscIsPointInterpQuery(SSqlCmd* pCmd) { return true; } -bool tscIsTWAQuery(SSqlCmd* pCmd) { - for (int32_t i = 0; i < pCmd->pQueryInfo[0].exprsInfo.numOfExprs; ++i) { - SSqlExpr* pExpr = tscSqlExprGet(pCmd, i); +bool tscIsTWAQuery(SQueryInfo* pQueryInfo) { + for (int32_t i = 0; i < pQueryInfo->exprsInfo.numOfExprs; ++i) { + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i); if (pExpr == NULL) { continue; } @@ -299,30 +313,28 @@ bool tscIsTWAQuery(SSqlCmd* pCmd) { return false; } -void tscClearInterpInfo(SSqlCmd* pCmd) { - if (!tscIsPointInterpQuery(pCmd)) { +void tscClearInterpInfo(SQueryInfo* pQueryInfo) { + if (!tscIsPointInterpQuery(pQueryInfo)) { return; } - pCmd->pQueryInfo[0].interpoType = TSDB_INTERPO_NONE; - memset(pCmd->pQueryInfo[0].defaultVal, 0, sizeof(pCmd->pQueryInfo[0].defaultVal)); + pQueryInfo->interpoType = TSDB_INTERPO_NONE; + memset(pQueryInfo->defaultVal, 0, sizeof(pQueryInfo->defaultVal)); } void tscClearSqlMetaInfoForce(SSqlCmd* pCmd) { /* remove the metermeta/metricmeta in cache */ - // taosRemoveDataFromCache(tscCacheHandle, (void**)&(pCmd->pMeterMeta), - // true); - // taosRemoveDataFromCache(tscCacheHandle, (void**)&(pCmd->pMetricMeta), - // true); + // taosRemoveDataFromCache(tscCacheHandle, (void**)&(pCmd->pMeterMeta), true); + // taosRemoveDataFromCache(tscCacheHandle, (void**)&(pCmd->pMetricMeta), true); } -int32_t tscCreateResPointerInfo(SSqlCmd* pCmd, SSqlRes* pRes) { +int32_t tscCreateResPointerInfo(SQueryInfo* pQueryInfo, SSqlRes* pRes) { if (pRes->tsrow == NULL) { pRes->numOfnchar = 0; - int32_t numOfOutputCols = pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; + int32_t numOfOutputCols = pQueryInfo->fieldsInfo.numOfOutputCols; for (int32_t i = 0; i < numOfOutputCols; ++i) { - TAOS_FIELD* pField = tscFieldInfoGetField(pCmd, i); + TAOS_FIELD* pField = tscFieldInfoGetField(pQueryInfo, i); if (pField->type == TSDB_DATA_TYPE_NCHAR) { pRes->numOfnchar++; } @@ -360,24 +372,7 @@ void tscDestroyResPointerInfo(SSqlRes* pRes) { void tscFreeSqlCmdData(SSqlCmd* pCmd) { pCmd->pDataBlocks = tscDestroyBlockArrayList(pCmd->pDataBlocks); - - if (pCmd->pQueryInfo == NULL) { - return; - } - - tscTagCondRelease(&pCmd->pQueryInfo[0].tagCond); - tscClearFieldInfo(&pCmd->pQueryInfo[0].fieldsInfo); - - tfree(pCmd->pQueryInfo[0].exprsInfo.pExprs); - memset(&pCmd->pQueryInfo[0].exprsInfo, 0, sizeof(pCmd->pQueryInfo[0].exprsInfo)); - - tscColumnBaseInfoDestroy(&pCmd->pQueryInfo[0].colList); - memset(&pCmd->pQueryInfo[0].colList, 0, sizeof(pCmd->pQueryInfo[0].colList)); - - if (pCmd->pQueryInfo[0].tsBuf != NULL) { - tsBufDestory(pCmd->pQueryInfo[0].tsBuf); - pCmd->pQueryInfo[0].tsBuf = NULL; - } + tscFreeSubqueryInfo(pCmd); } void tscFreeSqlObjPartial(SSqlObj* pSql) { @@ -419,7 +414,6 @@ void tscFreeSqlObjPartial(SSqlObj* pSql) { tfree(pSql->res.pColumnIndex); tscFreeSqlCmdData(pCmd); - tscRemoveAllMeterMetaInfo(pCmd, false); } void tscFreeSqlObj(SSqlObj* pSql) { @@ -438,7 +432,9 @@ void tscFreeSqlObj(SSqlObj* pSql) { pCmd->allocSize = 0; if (pSql->res.buffer != NULL) { - for (int i = 0; i < pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; i++) { + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + + for (int i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; i++) { if (pSql->res.buffer[i] != NULL) { tfree(pSql->res.buffer[i]); } @@ -540,7 +536,7 @@ int32_t tscCopyDataBlockToPayload(SSqlObj* pSql, STableDataBlocks* pDataBlock) { assert(pDataBlock->pMeterMeta != NULL); pCmd->count = pDataBlock->numOfMeters; - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0); // set the correct metermeta object, the metermeta has been locked in pDataBlocks, so it must be in the cache if (pMeterMetaInfo->pMeterMeta != pDataBlock->pMeterMeta) { @@ -856,8 +852,8 @@ void tscFieldInfoSetValue(SFieldInfo* pFieldInfo, int32_t index, int8_t type, co pFieldInfo->numOfOutputCols++; } -void tscFieldInfoCalOffset(SSqlCmd* pCmd) { - SFieldInfo* pFieldInfo = &pCmd->pQueryInfo[0].fieldsInfo; +void tscFieldInfoCalOffset(SQueryInfo* pQueryInfo) { + SFieldInfo* pFieldInfo = &pQueryInfo->fieldsInfo; pFieldInfo->pOffset[0] = 0; for (int32_t i = 1; i < pFieldInfo->numOfOutputCols; ++i) { @@ -865,8 +861,8 @@ void tscFieldInfoCalOffset(SSqlCmd* pCmd) { } } -void tscFieldInfoUpdateOffset(SSqlCmd* pCmd) { - SFieldInfo* pFieldInfo = &pCmd->pQueryInfo[0].fieldsInfo; +void tscFieldInfoUpdateOffset(SQueryInfo* pQueryInfo) { + SFieldInfo* pFieldInfo = &pQueryInfo->fieldsInfo; if (pFieldInfo->numOfOutputCols == 0) { return; } @@ -878,7 +874,7 @@ void tscFieldInfoUpdateOffset(SSqlCmd* pCmd) { * for potential secondary merge exists */ for (int32_t i = 1; i < pFieldInfo->numOfOutputCols; ++i) { - pFieldInfo->pOffset[i] = pFieldInfo->pOffset[i - 1] + tscSqlExprGet(pCmd, i - 1)->resBytes; + pFieldInfo->pOffset[i] = pFieldInfo->pOffset[i - 1] + tscSqlExprGet(pQueryInfo, i - 1)->resBytes; } } @@ -910,28 +906,26 @@ void tscFieldInfoCopyAll(SFieldInfo* src, SFieldInfo* dst) { memcpy(dst->pVisibleCols, src->pVisibleCols, sizeof(bool) * dst->numOfOutputCols); } -TAOS_FIELD* tscFieldInfoGetField(SSqlCmd* pCmd, int32_t index) { - if (index >= pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols) { +TAOS_FIELD* tscFieldInfoGetField(SQueryInfo* pQueryInfo, int32_t index) { + if (index >= pQueryInfo->fieldsInfo.numOfOutputCols) { return NULL; } - return &pCmd->pQueryInfo[0].fieldsInfo.pFields[index]; + return &pQueryInfo->fieldsInfo.pFields[index]; } -int32_t tscNumOfFields(SSqlCmd* pCmd) { - return pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols; -} +int32_t tscNumOfFields(SQueryInfo* pQueryInfo) { return pQueryInfo->fieldsInfo.numOfOutputCols; } -int16_t tscFieldInfoGetOffset(SSqlCmd* pCmd, int32_t index) { - if (index >= pCmd->pQueryInfo[0].fieldsInfo.numOfOutputCols) { +int16_t tscFieldInfoGetOffset(SQueryInfo* pQueryInfo, int32_t index) { + if (index >= pQueryInfo->fieldsInfo.numOfOutputCols) { return 0; } - return pCmd->pQueryInfo[0].fieldsInfo.pOffset[index]; + return pQueryInfo->fieldsInfo.pOffset[index]; } -int32_t tscGetResRowLength(SSqlCmd* pCmd) { - SFieldInfo* pFieldInfo = &pCmd->pQueryInfo[0].fieldsInfo; +int32_t tscGetResRowLength(SQueryInfo* pQueryInfo) { + SFieldInfo* pFieldInfo = &pQueryInfo->fieldsInfo; if (pFieldInfo->numOfOutputCols <= 0) { return 0; } @@ -981,8 +975,8 @@ static void _exprEvic(SSqlExprInfo* pExprInfo, int32_t index) { } } -SSqlExpr* tscSqlExprInsertEmpty(SSqlCmd* pCmd, int32_t index, int16_t functionId) { - SSqlExprInfo* pExprInfo = &pCmd->pQueryInfo[0].exprsInfo; +SSqlExpr* tscSqlExprInsertEmpty(SQueryInfo* pQueryInfo, int32_t index, int16_t functionId) { + SSqlExprInfo* pExprInfo = &pQueryInfo->exprsInfo; _exprCheckSpace(pExprInfo, pExprInfo->numOfExprs + 1); _exprEvic(pExprInfo, index); @@ -994,11 +988,11 @@ SSqlExpr* tscSqlExprInsertEmpty(SSqlCmd* pCmd, int32_t index, int16_t functionId return pExpr; } -SSqlExpr* tscSqlExprInsert(SSqlCmd* pCmd, int32_t index, int16_t functionId, SColumnIndex* pColIndex, int16_t type, - int16_t size, int16_t interSize) { - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, pColIndex->tableIndex); +SSqlExpr* tscSqlExprInsert(SQueryInfo* pQueryInfo, int32_t index, int16_t functionId, SColumnIndex* pColIndex, + int16_t type, int16_t size, int16_t interSize) { + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, pColIndex->tableIndex); - SSqlExprInfo* pExprInfo = &pCmd->pQueryInfo[0].exprsInfo; + SSqlExprInfo* pExprInfo = &pQueryInfo->exprsInfo; _exprCheckSpace(pExprInfo, pExprInfo->numOfExprs + 1); _exprEvic(pExprInfo, index); @@ -1038,10 +1032,10 @@ SSqlExpr* tscSqlExprInsert(SSqlCmd* pCmd, int32_t index, int16_t functionId, SCo return pExpr; } -SSqlExpr* tscSqlExprUpdate(SSqlCmd* pCmd, int32_t index, int16_t functionId, int16_t srcColumnIndex, int16_t type, - int16_t size) { - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); - SSqlExprInfo* pExprInfo = &pCmd->pQueryInfo[0].exprsInfo; +SSqlExpr* tscSqlExprUpdate(SQueryInfo* pQueryInfo, int32_t index, int16_t functionId, int16_t srcColumnIndex, + int16_t type, int16_t size) { + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); + SSqlExprInfo* pExprInfo = &pQueryInfo->exprsInfo; if (index > pExprInfo->numOfExprs) { return NULL; } @@ -1072,12 +1066,12 @@ void addExprParams(SSqlExpr* pExpr, char* argument, int32_t type, int32_t bytes, assert(pExpr->numOfParams <= 3); } -SSqlExpr* tscSqlExprGet(SSqlCmd* pCmd, int32_t index) { - if (pCmd->pQueryInfo[0].exprsInfo.numOfExprs <= index) { +SSqlExpr* tscSqlExprGet(SQueryInfo* pQueryInfo, int32_t index) { + if (pQueryInfo->exprsInfo.numOfExprs <= index) { return NULL; } - return &pCmd->pQueryInfo[0].exprsInfo.pExprs[index]; + return &pQueryInfo->exprsInfo.pExprs[index]; } void tscSqlExprCopy(SSqlExprInfo* dst, const SSqlExprInfo* src, uint64_t tableuid) { @@ -1156,8 +1150,8 @@ void tscColumnBaseInfoUpdateTableIndex(SColumnBaseInfo* pColList, int16_t tableI } // todo refactor -SColumnBase* tscColumnBaseInfoInsert(SSqlCmd* pCmd, SColumnIndex* pColIndex) { - SColumnBaseInfo* pcolList = &pCmd->pQueryInfo[0].colList; +SColumnBase* tscColumnBaseInfoInsert(SQueryInfo* pQueryInfo, SColumnIndex* pColIndex) { + SColumnBaseInfo* pcolList = &pQueryInfo->colList; // ignore the tbname column to be inserted into source list if (pColIndex->columnIndex < 0) { @@ -1185,7 +1179,6 @@ SColumnBase* tscColumnBaseInfoInsert(SSqlCmd* pCmd, SColumnIndex* pColIndex) { pcolList->pColList[i].colIndex = *pColIndex; pcolList->numOfCols++; - pCmd->numOfCols++; } return &pcolList->pColList[i]; @@ -1404,12 +1397,12 @@ void tscIncStreamExecutionCount(void* pStream) { } bool tscValidateColumnId(SSqlCmd* pCmd, int32_t colId) { - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0); if (pMeterMetaInfo->pMeterMeta == NULL) { return false; } - if (colId == -1 && UTIL_METER_IS_METRIC(pMeterMetaInfo)) { + if (colId == -1 && UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo)) { return true; } @@ -1457,12 +1450,12 @@ void tscTagCondRelease(STagCond* pCond) { memset(pCond, 0, sizeof(STagCond)); } -void tscGetSrcColumnInfo(SSrcColumnInfo* pColInfo, SSqlCmd* pCmd) { - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); +void tscGetSrcColumnInfo(SSrcColumnInfo* pColInfo, SQueryInfo* pQueryInfo) { + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); SSchema* pSchema = tsGetSchema(pMeterMetaInfo->pMeterMeta); - for (int32_t i = 0; i < pCmd->pQueryInfo[0].exprsInfo.numOfExprs; ++i) { - SSqlExpr* pExpr = tscSqlExprGet(pCmd, i); + for (int32_t i = 0; i < pQueryInfo->exprsInfo.numOfExprs; ++i) { + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i); pColInfo[i].functionId = pExpr->functionId; if (TSDB_COL_IS_TAG(pExpr->colInfo.flag)) { @@ -1485,20 +1478,20 @@ void tscSetFreeHeatBeat(STscObj* pObj) { assert(pHeatBeat == pHeatBeat->signature); // to denote the heart-beat timer close connection and free all allocated resources - pHeatBeat->cmd.type = TSDB_QUERY_TYPE_FREE_RESOURCE; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pHeatBeat->cmd, 0); + pQueryInfo->type = TSDB_QUERY_TYPE_FREE_RESOURCE; } bool tscShouldFreeHeatBeat(SSqlObj* pHb) { assert(pHb == pHb->signature); - return pHb->cmd.type == TSDB_QUERY_TYPE_FREE_RESOURCE; + + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pHb->cmd, 0); + return pQueryInfo->type == TSDB_QUERY_TYPE_FREE_RESOURCE; } void tscCleanSqlCmd(SSqlCmd* pCmd) { - tscFreeSqlCmdData(pCmd); - - if (pCmd->pQueryInfo != NULL) { - assert(pCmd->pQueryInfo[0].pMeterInfo == NULL); - } + pCmd->pDataBlocks = tscDestroyBlockArrayList(pCmd->pDataBlocks); + tscFreeSubqueryInfo(pCmd); uint32_t allocSize = pCmd->allocSize; char* allocPtr = pCmd->payload; @@ -1552,9 +1545,10 @@ bool tscShouldFreeAsyncSqlObj(SSqlObj* pSql) { * data blocks have been submit to vnode. */ SDataBlockList* pDataBlocks = pCmd->pDataBlocks; + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0); - assert(pSql->cmd.pQueryInfo[0].numOfTables == 1); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0); + assert(pQueryInfo->numOfTables == 1); if (pDataBlocks == NULL || pMeterMetaInfo->vnodeIndex >= pDataBlocks->nSize) { tscTrace("%p object should be release since all data blocks have been submit", pSql); @@ -1568,19 +1562,50 @@ bool tscShouldFreeAsyncSqlObj(SSqlObj* pSql) { } } -SMeterMetaInfo* tscGetMeterMetaInfo(SSqlCmd* pCmd, int32_t index) { - if (pCmd == NULL || pCmd->numOfQueries == 0 || pCmd->pQueryInfo[0].numOfTables == 0) { +/** + * + * @param pCmd + * @param unionSubClause denote the index of the union sub clause, usually are 0, if no union query exists. + * @param tableIndex denote the table index for join query, where more than one table exists + * @return + */ +SMeterMetaInfo* tscGetMeterMetaInfo(SSqlCmd* pCmd, int32_t unionClauseIndex, int32_t tableIndex) { + if (pCmd == NULL || pCmd->numOfClause == 0) { return NULL; } - assert(index >= 0 && index <= pCmd->pQueryInfo[0].numOfTables && pCmd->pQueryInfo[0].pMeterInfo != NULL); - return pCmd->pQueryInfo[0].pMeterInfo[index]; + assert(unionClauseIndex >= 0 && unionClauseIndex < pCmd->numOfClause); + + SQueryInfo* pQueryInfo = pCmd->pQueryInfo[unionClauseIndex]; + return tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, tableIndex); } -SMeterMetaInfo* tscGetMeterMetaInfoByUid(SSqlCmd* pCmd, uint64_t uid, int32_t* index) { +SMeterMetaInfo* tscGetMeterMetaInfoFromQueryInfo(SQueryInfo* pQueryInfo, int32_t tableIndex) { + if (pQueryInfo->pMeterInfo == NULL) { + assert(pQueryInfo->numOfTables == 0); + return NULL; + } + + assert(pQueryInfo != NULL && tableIndex >= 0 && tableIndex <= pQueryInfo->numOfTables && + pQueryInfo->pMeterInfo != NULL); + + return pQueryInfo->pMeterInfo[tableIndex]; +} + +SQueryInfo* tscGetQueryInfoDetail(SSqlCmd* pCmd, int32_t subClauseIndex) { + if (pCmd->pQueryInfo == NULL) { + return NULL; + } + + assert(pCmd != NULL && subClauseIndex >= 0 && subClauseIndex < pCmd->numOfClause); + return pCmd->pQueryInfo[subClauseIndex]; +} + +SMeterMetaInfo* tscGetMeterMetaInfoByUid(SQueryInfo* pQueryInfo, int32_t subClauseIndex, uint64_t uid, int32_t* index) { int32_t k = -1; - for (int32_t i = 0; i < pCmd->pQueryInfo->numOfTables; ++i) { - if (pCmd->pQueryInfo[0].pMeterInfo[i]->pMeterMeta->uid == uid) { + + for (int32_t i = 0; i < pQueryInfo->numOfTables; ++i) { + if (pQueryInfo->pMeterInfo[i]->pMeterMeta->uid == uid) { k = i; break; } @@ -1590,40 +1615,80 @@ SMeterMetaInfo* tscGetMeterMetaInfoByUid(SSqlCmd* pCmd, uint64_t uid, int32_t* i *index = k; } - return tscGetMeterMetaInfo(pCmd, k); + return tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, k); } -int32_t tscAddQueryInfo(SSqlCmd* pCmd) { +int32_t tscAddSubqueryInfo(SSqlCmd* pCmd) { assert(pCmd != NULL); - - size_t s = pCmd->numOfQueries + 1; - char* tmp = realloc(pCmd->pQueryInfo, s * sizeof(SQueryInfo)); + + size_t s = pCmd->numOfClause + 1; + char* tmp = realloc(pCmd->pQueryInfo, s * POINTER_BYTES); if (tmp == NULL) { return TSDB_CODE_CLI_OUT_OF_MEMORY; } - - pCmd->pQueryInfo = (SQueryInfo*) tmp; - memset(&pCmd->pQueryInfo[pCmd->numOfQueries], 0, sizeof(SQueryInfo)); - - pCmd->numOfQueries++; + + pCmd->pQueryInfo = (SQueryInfo**)tmp; + + SQueryInfo* pQueryInfo = calloc(1, sizeof(SQueryInfo)); + pQueryInfo->msg = pCmd->payload; // pointer to the parent error message buffer + + pCmd->pQueryInfo[pCmd->numOfClause++] = pQueryInfo; return TSDB_CODE_SUCCESS; } -SMeterMetaInfo* tscAddMeterMetaInfo(SSqlCmd* pCmd, const char* name, SMeterMeta* pMeterMeta, SMetricMeta* pMetricMeta, - int16_t numOfTags, int16_t* tags) { - if (pCmd->pQueryInfo == NULL) { - tscAddQueryInfo(pCmd); +static void doFreeSubqueryInfo(SQueryInfo* pQueryInfo, int64_t address) { + tscTagCondRelease(&pQueryInfo->tagCond); + tscClearFieldInfo(&pQueryInfo->fieldsInfo); + + tfree(pQueryInfo->exprsInfo.pExprs); + memset(&pQueryInfo->exprsInfo, 0, sizeof(pQueryInfo->exprsInfo)); + + tscColumnBaseInfoDestroy(&pQueryInfo->colList); + memset(&pQueryInfo->colList, 0, sizeof(pQueryInfo->colList)); + + if (pQueryInfo->tsBuf != NULL) { + tsBufDestory(pQueryInfo->tsBuf); + pQueryInfo->tsBuf = NULL; } + + tscRemoveAllMeterMetaInfo(pQueryInfo, (const char*) address, false); + tfree(pQueryInfo); +} + +void tscFreeSubqueryInfo(SSqlCmd* pCmd) { + if (pCmd == NULL || pCmd->numOfClause == 0) { + return; + } + + for (int32_t i = 0; i < pCmd->numOfClause; ++i) { + int64_t offset = offsetof(SSqlObj, cmd); + int64_t addr = (char*) pCmd - offset; + + doFreeSubqueryInfo(tscGetQueryInfoDetail(pCmd, i), addr); + } + + pCmd->numOfClause = 0; + tfree(pCmd->pQueryInfo); +} + +SMeterMetaInfo* tscAddMeterMetaInfo(SSqlCmd* pCmd, int32_t subClauseIndex, const char* name, SMeterMeta* pMeterMeta, + SMetricMeta* pMetricMeta, int16_t numOfTags, int16_t* tags) { + assert(subClauseIndex >= 0 && subClauseIndex < TSDB_MAX_UNION_CLAUSE); + while (pCmd->numOfClause <= subClauseIndex) { + tscAddSubqueryInfo(pCmd); + } + + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); - void* pAlloc = realloc(pCmd->pQueryInfo[0].pMeterInfo, (pCmd->pQueryInfo->numOfTables + 1) * POINTER_BYTES); + void* pAlloc = realloc(pQueryInfo->pMeterInfo, (pQueryInfo->numOfTables + 1) * POINTER_BYTES); if (pAlloc == NULL) { return NULL; } - pCmd->pQueryInfo[0].pMeterInfo = pAlloc; - pCmd->pQueryInfo[0].pMeterInfo[pCmd->pQueryInfo->numOfTables] = calloc(1, sizeof(SMeterMetaInfo)); + pQueryInfo->pMeterInfo = pAlloc; + pQueryInfo->pMeterInfo[pQueryInfo->numOfTables] = calloc(1, sizeof(SMeterMetaInfo)); - SMeterMetaInfo* pMeterMetaInfo = pCmd->pQueryInfo[0].pMeterInfo[pCmd->pQueryInfo->numOfTables]; + SMeterMetaInfo* pMeterMetaInfo = pQueryInfo->pMeterInfo[pQueryInfo->numOfTables]; assert(pMeterMetaInfo != NULL); if (name != NULL) { @@ -1639,46 +1704,41 @@ SMeterMetaInfo* tscAddMeterMetaInfo(SSqlCmd* pCmd, const char* name, SMeterMeta* memcpy(pMeterMetaInfo->tagColumnIndex, tags, sizeof(pMeterMetaInfo->tagColumnIndex[0]) * numOfTags); } - pCmd->pQueryInfo->numOfTables += 1; - + pQueryInfo->numOfTables += 1; return pMeterMetaInfo; } -SMeterMetaInfo* tscAddEmptyMeterMetaInfo(SSqlCmd* pCmd) { return tscAddMeterMetaInfo(pCmd, NULL, NULL, NULL, 0, NULL); } +SMeterMetaInfo* tscAddEmptyMeterMetaInfo(SSqlCmd* pCmd, int32_t subClauseIndex) { + return tscAddMeterMetaInfo(pCmd, subClauseIndex, NULL, NULL, NULL, 0, NULL); +} -void tscRemoveMeterMetaInfo(SSqlCmd* pCmd, int32_t index, bool removeFromCache) { - if (index < 0 || index >= pCmd->pQueryInfo->numOfTables) { +void doRemoveMeterMetaInfo(SQueryInfo* pQueryInfo, int32_t index, bool removeFromCache) { + if (index < 0 || index >= pQueryInfo->numOfTables) { return; } - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, index); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, index); tscClearMeterMetaInfo(pMeterMetaInfo, removeFromCache); free(pMeterMetaInfo); - int32_t after = pCmd->pQueryInfo->numOfTables - index - 1; + int32_t after = pQueryInfo->numOfTables - index - 1; if (after > 0) { - memmove(&pCmd->pQueryInfo[0].pMeterInfo[index], &pCmd->pQueryInfo[0].pMeterInfo[index + 1], after * sizeof(void*)); + memmove(&pQueryInfo->pMeterInfo[index], &pQueryInfo->pMeterInfo[index + 1], after * sizeof(void*)); } - pCmd->pQueryInfo->numOfTables -= 1; + pQueryInfo->numOfTables -= 1; } -void tscRemoveAllMeterMetaInfo(SSqlCmd* pCmd, bool removeFromCache) { - int64_t addr = offsetof(SSqlObj, cmd); - - if (pCmd->numOfQueries == 0) { - return; - } - - tscTrace("%p deref the metric/meter meta in cache, numOfTables:%d", ((char*)pCmd - addr), - pCmd->pQueryInfo[0].numOfTables); +void tscRemoveAllMeterMetaInfo(SQueryInfo* pQueryInfo, const char* address, bool removeFromCache) { + tscTrace("%p deref the metric/meter meta in cache, numOfTables:%d", address, pQueryInfo->numOfTables); - while (pCmd->pQueryInfo[0].numOfTables > 0) { - tscRemoveMeterMetaInfo(pCmd, pCmd->pQueryInfo->numOfTables - 1, removeFromCache); + int32_t index = pQueryInfo->numOfTables; + while (index >= 0) { + doRemoveMeterMetaInfo(pQueryInfo, --index, removeFromCache); } - tfree(pCmd->pQueryInfo[0].pMeterInfo); + tfree(pQueryInfo->pMeterInfo); } void tscClearMeterMetaInfo(SMeterMetaInfo* pMeterMetaInfo, bool removeFromCache) { @@ -1697,7 +1757,7 @@ void tscResetForNextRetrieve(SSqlRes* pRes) { SSqlObj* createSubqueryObj(SSqlObj* pSql, int16_t tableIndex, void (*fp)(), void* param, SSqlObj* pPrevSql) { SSqlCmd* pCmd = &pSql->cmd; - SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, tableIndex); + SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, tableIndex); SSqlObj* pNew = (SSqlObj*)calloc(1, sizeof(SSqlObj)); if (pNew == NULL) { @@ -1723,26 +1783,26 @@ SSqlObj* createSubqueryObj(SSqlObj* pSql, int16_t tableIndex, void (*fp)(), void pNew->cmd.allocSize = 0; pNew->cmd.pQueryInfo = NULL; - pNew->cmd.numOfQueries = 0; - - if (tscAddQueryInfo(&pNew->cmd) != TSDB_CODE_SUCCESS) { + pNew->cmd.numOfClause = 0; + + if (tscAddSubqueryInfo(&pNew->cmd) != TSDB_CODE_SUCCESS) { tscFreeSqlObj(pNew); return NULL; } - - memcpy(&pNew->cmd.pQueryInfo[0], &pCmd->pQueryInfo[0], sizeof(SQueryInfo)); - pNew->cmd.pQueryInfo[0].pMeterInfo = NULL; - - pNew->cmd.pQueryInfo[0].colList.pColList = NULL; - pNew->cmd.pQueryInfo[0].colList.numOfAlloc = 0; - pNew->cmd.pQueryInfo[0].colList.numOfCols = 0; - - pNew->cmd.pQueryInfo[0].numOfTables = 0; - pNew->cmd.pQueryInfo[0].tsBuf = NULL; - - memset(&pNew->cmd.pQueryInfo[0].fieldsInfo, 0, sizeof(SFieldInfo)); - - tscTagCondCopy(&pNew->cmd.pQueryInfo[0].tagCond, &pCmd->pQueryInfo[0].tagCond); + + SQueryInfo* pNewQueryInfo = tscGetQueryInfoDetail(&pNew->cmd, 0); + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + + memcpy(pNewQueryInfo, pQueryInfo, sizeof(SQueryInfo)); + pNewQueryInfo->pMeterInfo = NULL; + + memset(&pNewQueryInfo->colList, 0, sizeof(pNewQueryInfo->colList)); + memset(&pNewQueryInfo->fieldsInfo, 0, sizeof(SFieldInfo)); + + pNewQueryInfo->numOfTables = 0; + pNewQueryInfo->tsBuf = NULL; + + tscTagCondCopy(&pNewQueryInfo->tagCond, &pQueryInfo->tagCond); if (tscAllocPayload(&pNew->cmd, TSDB_DEFAULT_PAYLOAD_SIZE) != TSDB_CODE_SUCCESS) { tscError("%p new subquery failed, tableIndex:%d, vnodeIndex:%d", pSql, tableIndex, pMeterMetaInfo->vnodeIndex); @@ -1750,40 +1810,41 @@ SSqlObj* createSubqueryObj(SSqlObj* pSql, int16_t tableIndex, void (*fp)(), void return NULL; } - tscColumnBaseInfoCopy(&pNew->cmd.pQueryInfo[0].colList, &pCmd->pQueryInfo[0].colList, (int16_t)tableIndex); - + tscColumnBaseInfoCopy(&pNewQueryInfo->colList, &pQueryInfo->colList, (int16_t)tableIndex); + // set the correct query type if (pPrevSql != NULL) { - pNew->cmd.type = pPrevSql->cmd.type; + SQueryInfo* pPrevQueryInfo = tscGetQueryInfoDetail(&pPrevSql->cmd, 0); + pNewQueryInfo->type = pPrevQueryInfo->type; } else { - pNew->cmd.type |= TSDB_QUERY_TYPE_SUBQUERY; // it must be the subquery + pNewQueryInfo->type |= TSDB_QUERY_TYPE_SUBQUERY; // it must be the subquery } uint64_t uid = pMeterMetaInfo->pMeterMeta->uid; - tscSqlExprCopy(&pNew->cmd.pQueryInfo[0].exprsInfo, &pCmd->pQueryInfo[0].exprsInfo, uid); + tscSqlExprCopy(&pNewQueryInfo->exprsInfo, &pQueryInfo->exprsInfo, uid); - int32_t numOfOutputCols = pNew->cmd.pQueryInfo[0].exprsInfo.numOfExprs; + int32_t numOfOutputCols = pNewQueryInfo->exprsInfo.numOfExprs; if (numOfOutputCols > 0) { int32_t* indexList = calloc(1, numOfOutputCols * sizeof(int32_t)); - for (int32_t i = 0, j = 0; i < pCmd->pQueryInfo[0].exprsInfo.numOfExprs; ++i) { - SSqlExpr* pExpr = tscSqlExprGet(pCmd, i); + for (int32_t i = 0, j = 0; i < pQueryInfo->exprsInfo.numOfExprs; ++i) { + SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i); if (pExpr->uid == uid) { indexList[j++] = i; } } - tscFieldInfoCopy(&pCmd->pQueryInfo[0].fieldsInfo, &pNew->cmd.pQueryInfo[0].fieldsInfo, indexList, numOfOutputCols); + tscFieldInfoCopy(&pQueryInfo->fieldsInfo, &pNewQueryInfo->fieldsInfo, indexList, numOfOutputCols); free(indexList); - tscFieldInfoUpdateOffset(&pNew->cmd); + tscFieldInfoUpdateOffset(pNewQueryInfo); } pNew->fp = fp; pNew->param = param; char key[TSDB_MAX_TAGS_LEN + 1] = {0}; - tscGetMetricMetaCacheKey(pCmd, key, uid); + tscGetMetricMetaCacheKey(pCmd, 0, key, uid); #ifdef _DEBUG_VIEW printf("the metricmeta key is:%s\n", key); @@ -1797,11 +1858,11 @@ SSqlObj* createSubqueryObj(SSqlObj* pSql, int16_t tableIndex, void (*fp)(), void SMeterMeta* pMeterMeta = taosGetDataFromCache(tscCacheHandle, name); SMetricMeta* pMetricMeta = taosGetDataFromCache(tscCacheHandle, key); - pFinalInfo = tscAddMeterMetaInfo(&pNew->cmd, name, pMeterMeta, pMetricMeta, pMeterMetaInfo->numOfTags, + pFinalInfo = tscAddMeterMetaInfo(&pNew->cmd, 0, name, pMeterMeta, pMetricMeta, pMeterMetaInfo->numOfTags, pMeterMetaInfo->tagColumnIndex); } else { - SMeterMetaInfo* pPrevInfo = tscGetMeterMetaInfo(&pPrevSql->cmd, 0); - pFinalInfo = tscAddMeterMetaInfo(&pNew->cmd, name, pPrevInfo->pMeterMeta, pPrevInfo->pMetricMeta, + SMeterMetaInfo* pPrevInfo = tscGetMeterMetaInfo(&pPrevSql->cmd, 0, 0); + pFinalInfo = tscAddMeterMetaInfo(&pNew->cmd, 0, name, pPrevInfo->pMeterMeta, pPrevInfo->pMetricMeta, pMeterMetaInfo->numOfTags, pMeterMetaInfo->tagColumnIndex); pPrevInfo->pMeterMeta = NULL; @@ -1809,12 +1870,12 @@ SSqlObj* createSubqueryObj(SSqlObj* pSql, int16_t tableIndex, void (*fp)(), void } assert(pFinalInfo->pMeterMeta != NULL); - if (UTIL_METER_IS_METRIC(pMeterMetaInfo)) { + if (UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo)) { assert(pFinalInfo->pMetricMeta != NULL); } tscTrace("%p new subquery %p, tableIndex:%d, vnodeIdx:%d, type:%d", pSql, pNew, tableIndex, - pMeterMetaInfo->vnodeIndex, pNew->cmd.type); + pMeterMetaInfo->vnodeIndex, pNewQueryInfo->type); return pNew; } @@ -1893,3 +1954,5 @@ bool tscHasReachLimitation(SSqlObj* pSql) { return (pCmd->globalLimit > 0 && pRes->numOfTotal >= pCmd->globalLimit); } + +char* tscGetErrorMsgPayload(SSqlCmd* pCmd) { return pCmd->payload; } diff --git a/src/inc/sql.y b/src/inc/sql.y index cb660333a9ccac60f97822cbc81af1703866365e..c113e21dfbde6311cb44ff0d908ba2d2864ab732 100644 --- a/src/inc/sql.y +++ b/src/inc/sql.y @@ -354,7 +354,8 @@ select(A) ::= SELECT(T) selcollist(W) from(X) where_opt(Y) interval_opt(K) fill_ union(Y) ::= select(X). { Y = setSubclause(NULL, X); } union(Y) ::= LP union(X) RP. { Y = X; } -union(Y) ::= union(Z) UNION select(X). { Y = appendSelectClause(Z, X); } +union(Y) ::= union(Z) UNION ALL select(X). { Y = appendSelectClause(Z, X); } +union(Y) ::= union(Z) UNION ALL LP select(X) RP. { Y = appendSelectClause(Z, X); } cmd ::= union(X). { setSQLInfo(pInfo, X, NULL, TSDB_SQL_SELECT); } diff --git a/src/inc/tsdb.h b/src/inc/tsdb.h index bc3caf8fa65afee52dd3b299c6f810c43e7be872..5171ca596ffedcf61cf1f46984323d7919b050cc 100644 --- a/src/inc/tsdb.h +++ b/src/inc/tsdb.h @@ -186,6 +186,7 @@ extern "C" { #define TSDB_MAX_TABLES_PER_VNODE 220000 #define TSDB_MAX_JOIN_TABLE_NUM 5 +#define TSDB_MAX_UNION_CLAUSE 5 #define TSDB_MAX_BINARY_LEN (TSDB_MAX_BYTES_PER_ROW-TSDB_KEYSIZE) #define TSDB_MAX_NCHAR_LEN (TSDB_MAX_BYTES_PER_ROW-TSDB_KEYSIZE) diff --git a/src/inc/tsqldef.h b/src/inc/tsqldef.h index c4d863c2916b51e5823d08c0f3f87771f6a0be52..78c290de5bdedfb6344958b245466000eab0bbea 100644 --- a/src/inc/tsqldef.h +++ b/src/inc/tsqldef.h @@ -120,66 +120,66 @@ #define TK_NULL 102 #define TK_SELECT 103 #define TK_UNION 104 -#define TK_FROM 105 -#define TK_VARIABLE 106 -#define TK_INTERVAL 107 -#define TK_FILL 108 -#define TK_SLIDING 109 -#define TK_ORDER 110 -#define TK_BY 111 -#define TK_ASC 112 -#define TK_DESC 113 -#define TK_GROUP 114 -#define TK_HAVING 115 -#define TK_LIMIT 116 -#define TK_OFFSET 117 -#define TK_SLIMIT 118 -#define TK_SOFFSET 119 -#define TK_WHERE 120 -#define TK_NOW 121 -#define TK_RESET 122 -#define TK_QUERY 123 -#define TK_ADD 124 -#define TK_COLUMN 125 -#define TK_TAG 126 -#define TK_CHANGE 127 -#define TK_SET 128 -#define TK_KILL 129 -#define TK_CONNECTION 130 -#define TK_COLON 131 -#define TK_STREAM 132 -#define TK_ABORT 133 -#define TK_AFTER 134 -#define TK_ATTACH 135 -#define TK_BEFORE 136 -#define TK_BEGIN 137 -#define TK_CASCADE 138 -#define TK_CLUSTER 139 -#define TK_CONFLICT 140 -#define TK_COPY 141 -#define TK_DEFERRED 142 -#define TK_DELIMITERS 143 -#define TK_DETACH 144 -#define TK_EACH 145 -#define TK_END 146 -#define TK_EXPLAIN 147 -#define TK_FAIL 148 -#define TK_FOR 149 -#define TK_IGNORE 150 -#define TK_IMMEDIATE 151 -#define TK_INITIALLY 152 -#define TK_INSTEAD 153 -#define TK_MATCH 154 -#define TK_KEY 155 -#define TK_OF 156 -#define TK_RAISE 157 -#define TK_REPLACE 158 -#define TK_RESTRICT 159 -#define TK_ROW 160 -#define TK_STATEMENT 161 -#define TK_TRIGGER 162 -#define TK_VIEW 163 -#define TK_ALL 164 +#define TK_ALL 105 +#define TK_FROM 106 +#define TK_VARIABLE 107 +#define TK_INTERVAL 108 +#define TK_FILL 109 +#define TK_SLIDING 110 +#define TK_ORDER 111 +#define TK_BY 112 +#define TK_ASC 113 +#define TK_DESC 114 +#define TK_GROUP 115 +#define TK_HAVING 116 +#define TK_LIMIT 117 +#define TK_OFFSET 118 +#define TK_SLIMIT 119 +#define TK_SOFFSET 120 +#define TK_WHERE 121 +#define TK_NOW 122 +#define TK_RESET 123 +#define TK_QUERY 124 +#define TK_ADD 125 +#define TK_COLUMN 126 +#define TK_TAG 127 +#define TK_CHANGE 128 +#define TK_SET 129 +#define TK_KILL 130 +#define TK_CONNECTION 131 +#define TK_COLON 132 +#define TK_STREAM 133 +#define TK_ABORT 134 +#define TK_AFTER 135 +#define TK_ATTACH 136 +#define TK_BEFORE 137 +#define TK_BEGIN 138 +#define TK_CASCADE 139 +#define TK_CLUSTER 140 +#define TK_CONFLICT 141 +#define TK_COPY 142 +#define TK_DEFERRED 143 +#define TK_DELIMITERS 144 +#define TK_DETACH 145 +#define TK_EACH 146 +#define TK_END 147 +#define TK_EXPLAIN 148 +#define TK_FAIL 149 +#define TK_FOR 150 +#define TK_IGNORE 151 +#define TK_IMMEDIATE 152 +#define TK_INITIALLY 153 +#define TK_INSTEAD 154 +#define TK_MATCH 155 +#define TK_KEY 156 +#define TK_OF 157 +#define TK_RAISE 158 +#define TK_REPLACE 159 +#define TK_RESTRICT 160 +#define TK_ROW 161 +#define TK_STATEMENT 162 +#define TK_TRIGGER 163 +#define TK_VIEW 164 #define TK_COUNT 165 #define TK_SUM 166 #define TK_AVG 167 diff --git a/src/inc/tutil.h b/src/inc/tutil.h index fcc3fde13f0c29832683292facbfe8e6dcdc6288..d3c8429edcdb6f729f227f44dfbe2fd4a50b29a5 100644 --- a/src/inc/tutil.h +++ b/src/inc/tutil.h @@ -102,8 +102,8 @@ extern "C" { #define GET_FLOAT_VAL(x) taos_align_get_float(x) #define GET_DOUBLE_VAL(x) taos_align_get_double(x) - float taos_align_get_float(char* pBuf); - double taos_align_get_double(char* pBuf); + float taos_align_get_float(const char* pBuf); + double taos_align_get_double(const char* pBuf); //#define __float_align_declear() float __underlyFloat = 0.0; //#define __float_align_declear() diff --git a/src/util/src/hash.c b/src/util/src/hash.c index a0ca4a96bbf5e8ca07e6cae063d8423af16d2a9c..90aa8cfaa5267a241aed91f0fa1531a332d60125 100644 --- a/src/util/src/hash.c +++ b/src/util/src/hash.c @@ -480,6 +480,8 @@ void taosCleanUpHashTable(void *handle) { free(pNode); pNode = pNext; } + + tfree(pEntry); } free(pObj->hashList); diff --git a/src/util/src/tutil.c b/src/util/src/tutil.c index cdd017fb564d1f11703b4608cfb85c0865f060c6..ef4bcb95cbe4944938cee447898da96f7480927f 100644 --- a/src/util/src/tutil.c +++ b/src/util/src/tutil.c @@ -528,13 +528,13 @@ char *taosIpStr(uint32_t ipInt) { void taosCleanupTier() {} #endif -FORCE_INLINE float taos_align_get_float(char* pBuf) { +FORCE_INLINE float taos_align_get_float(const char* pBuf) { float fv = 0; *(int32_t*)(&fv) = *(int32_t*)pBuf; return fv; } -FORCE_INLINE double taos_align_get_double(char* pBuf) { +FORCE_INLINE double taos_align_get_double(const char* pBuf) { double dv = 0; *(int64_t*)(&dv) = *(int64_t*)pBuf; return dv;