From 454c62d9afdbe598c31eba04bb89bb0ce7202855 Mon Sep 17 00:00:00 2001 From: hjLiao Date: Tue, 12 May 2020 17:07:22 +0800 Subject: [PATCH] [TD-288]support alias name in from clause --- src/client/src/sql.c | 30 ++++++++++++++++++++++++++---- src/client/src/tscSQLParser.c | 24 ++++++++++++++++-------- src/inc/sql.y | 33 +++++++++++++++++++++++++++++---- 3 files changed, 71 insertions(+), 16 deletions(-) diff --git a/src/client/src/sql.c b/src/client/src/sql.c index 03510f4e9c..fff8747912 100644 --- a/src/client/src/sql.c +++ b/src/client/src/sql.c @@ -2404,19 +2404,41 @@ static void yy_reduce( {yymsp[-1].minor.yy30 = yymsp[0].minor.yy30;} break; case 137: /* tablelist ::= ids cpxName */ -{ toTSDBType(yymsp[-1].minor.yy0.type); yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; yylhsminor.yy30 = tVariantListAppendToken(NULL, &yymsp[-1].minor.yy0, -1); yylhsminor.yy30 = tVariantListAppendToken(yylhsminor.yy30, &yymsp[-1].minor.yy0, -1);} +{ + toTSDBType(yymsp[-1].minor.yy0.type); + yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; + yylhsminor.yy30 = tVariantListAppendToken(NULL, &yymsp[-1].minor.yy0, -1); + yylhsminor.yy30 = tVariantListAppendToken(yylhsminor.yy30, &yymsp[-1].minor.yy0, -1); +} yymsp[-1].minor.yy30 = yylhsminor.yy30; break; case 138: /* tablelist ::= ids cpxName ids */ -{ toTSDBType(yymsp[-2].minor.yy0.type); yymsp[-2].minor.yy0.n += yymsp[-1].minor.yy0.n; yylhsminor.yy30 = tVariantListAppendToken(NULL, &yymsp[-2].minor.yy0, -1); yylhsminor.yy30 = tVariantListAppendToken(yylhsminor.yy30, &yymsp[0].minor.yy0, -1);} +{ + toTSDBType(yymsp[-2].minor.yy0.type); + toTSDBType(yymsp[0].minor.yy0.type); + yymsp[-2].minor.yy0.n += yymsp[-1].minor.yy0.n; + yylhsminor.yy30 = tVariantListAppendToken(NULL, &yymsp[-2].minor.yy0, -1); + yylhsminor.yy30 = tVariantListAppendToken(yylhsminor.yy30, &yymsp[0].minor.yy0, -1); +} yymsp[-2].minor.yy30 = yylhsminor.yy30; break; case 139: /* tablelist ::= tablelist COMMA ids cpxName */ -{ toTSDBType(yymsp[-1].minor.yy0.type); yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; yylhsminor.yy30 = tVariantListAppendToken(yymsp[-3].minor.yy30, &yymsp[-1].minor.yy0, -1); yylhsminor.yy30 = tVariantListAppendToken(yylhsminor.yy30, &yymsp[-1].minor.yy0, -1); } +{ + toTSDBType(yymsp[-1].minor.yy0.type); + yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; + yylhsminor.yy30 = tVariantListAppendToken(yymsp[-3].minor.yy30, &yymsp[-1].minor.yy0, -1); + yylhsminor.yy30 = tVariantListAppendToken(yylhsminor.yy30, &yymsp[-1].minor.yy0, -1); +} yymsp[-3].minor.yy30 = yylhsminor.yy30; break; case 140: /* tablelist ::= tablelist COMMA ids cpxName ids */ -{ toTSDBType(yymsp[-2].minor.yy0.type); yymsp[-2].minor.yy0.n += yymsp[-1].minor.yy0.n; yylhsminor.yy30 = tVariantListAppendToken(yymsp[-4].minor.yy30, &yymsp[-2].minor.yy0, -1); yylhsminor.yy30 = tVariantListAppendToken(yylhsminor.yy30, &yymsp[0].minor.yy0, -1); } +{ + toTSDBType(yymsp[-2].minor.yy0.type); + toTSDBType(yymsp[0].minor.yy0.type); + yymsp[-2].minor.yy0.n += yymsp[-1].minor.yy0.n; + yylhsminor.yy30 = tVariantListAppendToken(yymsp[-4].minor.yy30, &yymsp[-2].minor.yy0, -1); + yylhsminor.yy30 = tVariantListAppendToken(yylhsminor.yy30, &yymsp[0].minor.yy0, -1); +} yymsp[-4].minor.yy30 = yylhsminor.yy30; break; case 141: /* tmvar ::= VARIABLE */ diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 6baae638c7..7af6e79cd2 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -2011,9 +2011,8 @@ int32_t getMeterIndex(SSQLToken* pTableToken, SQueryInfo* pQueryInfo, SColumnInd 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) { + if (strncasecmp(pMeterMetaInfo->aliasName, pTableToken->z, pTableToken->n) == 0 && + strlen(pMeterMetaInfo->aliasName) == pTableToken->n) { pIndex->tableIndex = i; break; } @@ -5603,7 +5602,8 @@ int32_t doCheckForQuery(SSqlObj* pSql, SQuerySQL* pQuerySql, int32_t index) { const char* msg7 = "illegal number of tables in from clause"; const char* msg8 = "too many columns in selection clause"; const char* msg9 = "TWA query requires both the start and end time"; - + const char* msg10 = "alias name too long"; + int32_t code = TSDB_CODE_SUCCESS; SSqlCmd* pCmd = &pSql->cmd; @@ -5632,8 +5632,8 @@ int32_t doCheckForQuery(SSqlObj* pSql, SQuerySQL* pQuerySql, int32_t index) { return doLocalQueryProcess(pQueryInfo, pQuerySql); } - if (pQuerySql->from->nExpr > 1) { - if (pQuerySql->from->nExpr > 2) { // not support more than 2 tables join query + if (pQuerySql->from->nExpr > 2) { + if (pQuerySql->from->nExpr > 4) { // not support more than 2 tables join query return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg7); } @@ -5644,7 +5644,7 @@ int32_t doCheckForQuery(SSqlObj* pSql, SQuerySQL* pQuerySql, int32_t index) { pQueryInfo->command = TSDB_SQL_SELECT; // set all query tables, which are maybe more than one. - for (int32_t i = 0; i < pQuerySql->from->nExpr; ++i) { + for (int32_t i = 0; i < pQuerySql->from->nExpr; i += 2) { tVariant* pTableItem = &pQuerySql->from->a[i].pVar; if (pTableItem->nType != TSDB_DATA_TYPE_BINARY) { @@ -5673,9 +5673,17 @@ int32_t doCheckForQuery(SSqlObj* pSql, SQuerySQL* pQuerySql, int32_t index) { if (code != TSDB_CODE_SUCCESS) { return code; } + + //set the alias name + tVariant* aliasName = &pQuerySql->from->a[i + 1].pVar; + if (aliasName->nLen > TSDB_METER_NAME_LEN) { + return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg10); + } + + tVariantDump(aliasName, pMeterInfo1->aliasName, TSDB_DATA_TYPE_BINARY); } - assert(pQueryInfo->numOfTables == pQuerySql->from->nExpr); + assert(pQueryInfo->numOfTables == pQuerySql->from->nExpr / 2); // parse the group by clause in the first place if (parseGroupbyClause(pQueryInfo, pQuerySql->pGroupby, pCmd) != TSDB_CODE_SUCCESS) { diff --git a/src/inc/sql.y b/src/inc/sql.y index db94ec62a8..eecf05b159 100644 --- a/src/inc/sql.y +++ b/src/inc/sql.y @@ -406,10 +406,35 @@ as(X) ::= . { X.n = 0; } from(A) ::= FROM tablelist(X). {A = X;} %type tablelist {tVariantList*} -tablelist(A) ::= ids(X) cpxName(Y). { toTSDBType(X.type); X.n += Y.n; A = tVariantListAppendToken(NULL, &X, -1); A = tVariantListAppendToken(A, &X, -1);} -tablelist(A) ::= ids(X) cpxName(Y) ids(Z). { toTSDBType(X.type); X.n += Y.n; A = tVariantListAppendToken(NULL, &X, -1); A = tVariantListAppendToken(A, &Z, -1);} -tablelist(A) ::= tablelist(Y) COMMA ids(X) cpxName(Z). { toTSDBType(X.type); X.n += Z.n; A = tVariantListAppendToken(Y, &X, -1); A = tVariantListAppendToken(A, &X, -1); } -tablelist(A) ::= tablelist(Y) COMMA ids(X) cpxName(Z) ids(F). { toTSDBType(X.type); X.n += Z.n; A = tVariantListAppendToken(Y, &X, -1); A = tVariantListAppendToken(A, &F, -1); } +tablelist(A) ::= ids(X) cpxName(Y). { + toTSDBType(X.type); + X.n += Y.n; + A = tVariantListAppendToken(NULL, &X, -1); + A = tVariantListAppendToken(A, &X, -1); +} + +tablelist(A) ::= ids(X) cpxName(Y) ids(Z). { + toTSDBType(X.type); + toTSDBType(Z.type); + X.n += Y.n; + A = tVariantListAppendToken(NULL, &X, -1); + A = tVariantListAppendToken(A, &Z, -1); +} + +tablelist(A) ::= tablelist(Y) COMMA ids(X) cpxName(Z). { + toTSDBType(X.type); + X.n += Z.n; + A = tVariantListAppendToken(Y, &X, -1); + A = tVariantListAppendToken(A, &X, -1); +} + +tablelist(A) ::= tablelist(Y) COMMA ids(X) cpxName(Z) ids(F). { + toTSDBType(X.type); + toTSDBType(F.type); + X.n += Z.n; + A = tVariantListAppendToken(Y, &X, -1); + A = tVariantListAppendToken(A, &F, -1); +} // The value of interval should be the form of "number+[a,s,m,h,d,n,y]" or "now" %type tmvar {SSQLToken} -- GitLab