sql.y 28.2 KB
Newer Older
H
hzcheng 已提交
1 2 3 4
//lemon parser file to generate sql parse by using finite-state-machine code used to parse sql
//usage: lemon sql.y
%token_prefix TK_

H
Haojun Liao 已提交
5 6
%token_type {SStrToken}
%default_type {SStrToken}
H
hzcheng 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
%extra_argument {SSqlInfo* pInfo}

%fallback ID BOOL TINYINT SMALLINT INTEGER BIGINT FLOAT DOUBLE STRING TIMESTAMP BINARY NCHAR.

%left OR.
%left AND.
%right NOT.
%left EQ NE ISNULL NOTNULL IS LIKE GLOB BETWEEN IN.
%left GT GE LT LE.
%left BITAND BITOR LSHIFT RSHIFT.
%left PLUS MINUS.
%left DIVIDE TIMES.
%left STAR SLASH REM.
%left CONCAT.
%right UMINUS UPLUS BITNOT.

%include {
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdbool.h>
H
Haojun Liao 已提交
29 30 31 32
#include "qSqlparser.h"
#include "tcmdtype.h"
#include "tstoken.h"
#include "ttokendef.h"
H
hzcheng 已提交
33
#include "tutil.h"
H
Haojun Liao 已提交
34
#include "tvariant.h"
H
hzcheng 已提交
35 36 37
}

%syntax_error {
38
  pInfo->valid = false;
H
hzcheng 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  int32_t outputBufLen = tListLen(pInfo->pzErrMsg);
  int32_t len = 0;

  if(TOKEN.z) {
    char msg[] = "syntax error near \"%s\"";
    int32_t sqlLen = strlen(&TOKEN.z[0]);

    if (sqlLen + sizeof(msg)/sizeof(msg[0]) + 1 > outputBufLen) {
        char tmpstr[128] = {0};
        memcpy(tmpstr, &TOKEN.z[0], sizeof(tmpstr)/sizeof(tmpstr[0]) - 1);
        len = sprintf(pInfo->pzErrMsg, msg, tmpstr);
    } else {
        len = sprintf(pInfo->pzErrMsg, msg, &TOKEN.z[0]);
    }

  } else {
    len = sprintf(pInfo->pzErrMsg, "Incomplete SQL statement");
  }

  assert(len <= outputBufLen);
}

%parse_accept       {}

program ::= cmd.    {}

//////////////////////////////////THE SHOW STATEMENT///////////////////////////////////////////
66 67 68 69 70 71 72 73 74 75
cmd ::= SHOW DATABASES.  { setShowOptions(pInfo, TSDB_MGMT_TABLE_DB, 0, 0);}
cmd ::= SHOW MNODES.     { setShowOptions(pInfo, TSDB_MGMT_TABLE_MNODE, 0, 0);}
cmd ::= SHOW DNODES.     { setShowOptions(pInfo, TSDB_MGMT_TABLE_DNODE, 0, 0);}
cmd ::= SHOW ACCOUNTS.   { setShowOptions(pInfo, TSDB_MGMT_TABLE_ACCT, 0, 0);}
cmd ::= SHOW USERS.      { setShowOptions(pInfo, TSDB_MGMT_TABLE_USER, 0, 0);}

cmd ::= SHOW MODULES.    { setShowOptions(pInfo, TSDB_MGMT_TABLE_MODULE, 0, 0);  }
cmd ::= SHOW QUERIES.    { setShowOptions(pInfo, TSDB_MGMT_TABLE_QUERIES, 0, 0);  }
cmd ::= SHOW CONNECTIONS.{ setShowOptions(pInfo, TSDB_MGMT_TABLE_CONNS, 0, 0);}
cmd ::= SHOW STREAMS.    { setShowOptions(pInfo, TSDB_MGMT_TABLE_STREAMS, 0, 0);  }
H
Haojun Liao 已提交
76
cmd ::= SHOW VARIABLES.  { setShowOptions(pInfo, TSDB_MGMT_TABLE_VARIABLES, 0, 0);  }
77 78 79 80 81
cmd ::= SHOW SCORES.     { setShowOptions(pInfo, TSDB_MGMT_TABLE_SCORES, 0, 0);   }
cmd ::= SHOW GRANTS.     { setShowOptions(pInfo, TSDB_MGMT_TABLE_GRANTS, 0, 0);   }

cmd ::= SHOW VNODES.                { setShowOptions(pInfo, TSDB_MGMT_TABLE_VNODES, 0, 0); }
cmd ::= SHOW VNODES IPTOKEN(X).     { setShowOptions(pInfo, TSDB_MGMT_TABLE_VNODES, &X, 0); }
L
lihui 已提交
82

H
Haojun Liao 已提交
83
%type dbPrefix {SStrToken}
84
dbPrefix(A) ::=.                   {A.n = 0; A.type = 0;}
H
hzcheng 已提交
85 86
dbPrefix(A) ::= ids(X) DOT.        {A = X;  }

H
Haojun Liao 已提交
87
%type cpxName {SStrToken}
H
hzcheng 已提交
88 89 90 91
cpxName(A) ::= .             {A.n = 0;  }
cpxName(A) ::= DOT ids(Y).   {A = Y; A.n += 1;    }

cmd ::= SHOW dbPrefix(X) TABLES.         {
92
    setShowOptions(pInfo, TSDB_MGMT_TABLE_TABLE, &X, 0);
H
hzcheng 已提交
93 94 95
}

cmd ::= SHOW dbPrefix(X) TABLES LIKE ids(Y).         {
96
    setShowOptions(pInfo, TSDB_MGMT_TABLE_TABLE, &X, &Y);
H
hzcheng 已提交
97 98 99
}

cmd ::= SHOW dbPrefix(X) STABLES.      {
100
    setShowOptions(pInfo, TSDB_MGMT_TABLE_METRIC, &X, 0);
H
hzcheng 已提交
101 102 103
}

cmd ::= SHOW dbPrefix(X) STABLES LIKE ids(Y).      {
H
Haojun Liao 已提交
104
    SStrToken token;
H
hzcheng 已提交
105
    setDBName(&token, &X);
106
    setShowOptions(pInfo, TSDB_MGMT_TABLE_METRIC, &token, &Y);
H
hzcheng 已提交
107 108 109
}

cmd ::= SHOW dbPrefix(X) VGROUPS.    {
H
Haojun Liao 已提交
110
    SStrToken token;
H
hzcheng 已提交
111
    setDBName(&token, &X);
112
    setShowOptions(pInfo, TSDB_MGMT_TABLE_VGROUP, &token, 0);
H
hzcheng 已提交
113 114
}

L
[1361]  
lihui 已提交
115
cmd ::= SHOW dbPrefix(X) VGROUPS ids(Y).    {
H
Haojun Liao 已提交
116
    SStrToken token;
L
[1361]  
lihui 已提交
117
    setDBName(&token, &X);    
H
hjxilinx 已提交
118
    setShowOptions(pInfo, TSDB_MGMT_TABLE_VGROUP, &token, &Y);
L
[1361]  
lihui 已提交
119 120
}

H
hzcheng 已提交
121 122 123
//drop configure for tables
cmd ::= DROP TABLE ifexists(Y) ids(X) cpxName(Z).   {
    X.n += Z.n;
124
    setDropDBTableInfo(pInfo, TSDB_SQL_DROP_TABLE, &X, &Y);
H
hzcheng 已提交
125 126
}

127
cmd ::= DROP DATABASE ifexists(Y) ids(X).    { setDropDBTableInfo(pInfo, TSDB_SQL_DROP_DB, &X, &Y); }
H
hjxilinx 已提交
128
cmd ::= DROP DNODE ids(X).       { setDCLSQLElems(pInfo, TSDB_SQL_DROP_DNODE, 1, &X);    }
129 130
cmd ::= DROP USER ids(X).        { setDCLSQLElems(pInfo, TSDB_SQL_DROP_USER, 1, &X);     }
cmd ::= DROP ACCOUNT ids(X).     { setDCLSQLElems(pInfo, TSDB_SQL_DROP_ACCT, 1, &X);  }
H
hzcheng 已提交
131 132

/////////////////////////////////THE USE STATEMENT//////////////////////////////////////////
133
cmd ::= USE ids(X).              { setDCLSQLElems(pInfo, TSDB_SQL_USE_DB, 1, &X);}
H
hzcheng 已提交
134 135 136 137

/////////////////////////////////THE DESCRIBE STATEMENT/////////////////////////////////////
cmd ::= DESCRIBE ids(X) cpxName(Y). {
    X.n += Y.n;
138
    setDCLSQLElems(pInfo, TSDB_SQL_DESCRIBE_TABLE, 1, &X);
H
hzcheng 已提交
139 140 141
}

/////////////////////////////////THE ALTER STATEMENT////////////////////////////////////////
142 143
cmd ::= ALTER USER ids(X) PASS ids(Y).          { setAlterUserSQL(pInfo, TSDB_ALTER_USER_PASSWD, &X, &Y, NULL);    }
cmd ::= ALTER USER ids(X) PRIVILEGE ids(Y).     { setAlterUserSQL(pInfo, TSDB_ALTER_USER_PRIVILEGES, &X, NULL, &Y);}
H
hjxilinx 已提交
144 145
cmd ::= ALTER DNODE ids(X) ids(Y).              { setDCLSQLElems(pInfo, TSDB_SQL_CFG_DNODE, 2, &X, &Y);          }
cmd ::= ALTER DNODE ids(X) ids(Y) ids(Z).       { setDCLSQLElems(pInfo, TSDB_SQL_CFG_DNODE, 3, &X, &Y, &Z);      }
146 147
cmd ::= ALTER LOCAL ids(X).                     { setDCLSQLElems(pInfo, TSDB_SQL_CFG_LOCAL, 1, &X);              }
cmd ::= ALTER LOCAL ids(X) ids(Y).              { setDCLSQLElems(pInfo, TSDB_SQL_CFG_LOCAL, 2, &X, &Y);          }
H
Haojun Liao 已提交
148
cmd ::= ALTER DATABASE ids(X) alter_db_optr(Y). { SStrToken t = {0};  setCreateDBSQL(pInfo, TSDB_SQL_ALTER_DB, &X, &Y, &t);}
H
hzcheng 已提交
149

150 151
cmd ::= ALTER ACCOUNT ids(X) acct_optr(Z).      { setCreateAcctSQL(pInfo, TSDB_SQL_ALTER_ACCT, &X, NULL, &Z);}
cmd ::= ALTER ACCOUNT ids(X) PASS ids(Y) acct_optr(Z).      { setCreateAcctSQL(pInfo, TSDB_SQL_ALTER_ACCT, &X, &Y, &Z);}
S
slguan 已提交
152

H
hzcheng 已提交
153 154 155
// An IDENTIFIER can be a generic identifier, or one of several keywords.
// Any non-standard keyword can also be an identifier.
// And "ids" is an identifer-or-string.
H
Haojun Liao 已提交
156
%type ids {SStrToken}
H
hzcheng 已提交
157 158 159
ids(A) ::= ID(X).        {A = X; }
ids(A) ::= STRING(X).    {A = X; }

H
Haojun Liao 已提交
160
%type ifexists {SStrToken}
H
hzcheng 已提交
161 162 163
ifexists(X) ::= IF EXISTS.          {X.n = 1;}
ifexists(X) ::= .                   {X.n = 0;}

H
Haojun Liao 已提交
164
%type ifnotexists {SStrToken}
H
hzcheng 已提交
165 166 167 168
ifnotexists(X) ::= IF NOT EXISTS.   {X.n = 1;}
ifnotexists(X) ::= .                {X.n = 0;}

/////////////////////////////////THE CREATE STATEMENT///////////////////////////////////////
S
slguan 已提交
169
//create option for dnode/db/user/account
170
cmd ::= CREATE DNODE   ids(X).     { setDCLSQLElems(pInfo, TSDB_SQL_CREATE_DNODE, 1, &X);}
S
slguan 已提交
171
cmd ::= CREATE ACCOUNT ids(X) PASS ids(Y) acct_optr(Z).
172 173 174
                                { setCreateAcctSQL(pInfo, TSDB_SQL_CREATE_ACCT, &X, &Y, &Z);}
cmd ::= CREATE DATABASE ifnotexists(Z) ids(X) db_optr(Y).  { setCreateDBSQL(pInfo, TSDB_SQL_CREATE_DB, &X, &Y, &Z);}
cmd ::= CREATE USER ids(X) PASS ids(Y).     { setCreateUserSQL(pInfo, &X, &Y);}
H
hzcheng 已提交
175

S
slguan 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
pps(Y) ::= .                                {Y.n = 0;   }
pps(Y) ::= PPS INTEGER(X).                  {Y = X;     }

tseries(Y) ::= .                            {Y.n = 0;   }
tseries(Y) ::= TSERIES INTEGER(X).          {Y = X;     }

dbs(Y) ::= .                                {Y.n = 0;   }
dbs(Y) ::= DBS INTEGER(X).                  {Y = X;     }

streams(Y) ::= .                            {Y.n = 0;   }
streams(Y) ::= STREAMS INTEGER(X).          {Y = X;     }

storage(Y) ::= .                            {Y.n = 0;   }
storage(Y) ::= STORAGE INTEGER(X).          {Y = X;     }

qtime(Y) ::= .                              {Y.n = 0;   }
qtime(Y) ::= QTIME INTEGER(X).              {Y = X;     }

users(Y) ::= .                              {Y.n = 0;   }
users(Y) ::= USERS INTEGER(X).              {Y = X;     }

conns(Y) ::= .                              {Y.n = 0;   }
conns(Y) ::= CONNS INTEGER(X).              {Y = X;     }

state(Y) ::= .                              {Y.n = 0;   }
state(Y) ::= STATE ids(X).                  {Y = X;     }

%type acct_optr {SCreateAcctSQL}
acct_optr(Y) ::= pps(C) tseries(D) storage(P) streams(F) qtime(Q) dbs(E) users(K) conns(L) state(M). {
205 206 207 208 209 210 211 212
    Y.maxUsers   = (K.n>0)?atoi(K.z):-1;
    Y.maxDbs     = (E.n>0)?atoi(E.z):-1;
    Y.maxTimeSeries = (D.n>0)?atoi(D.z):-1;
    Y.maxStreams = (F.n>0)?atoi(F.z):-1;
    Y.maxPointsPerSecond     = (C.n>0)?atoi(C.z):-1;
    Y.maxStorage = (P.n>0)?strtoll(P.z, NULL, 10):-1;
    Y.maxQueryTime   = (Q.n>0)?strtoll(Q.z, NULL, 10):-1;
    Y.maxConnections   = (L.n>0)?atoi(L.z):-1;
S
slguan 已提交
213 214 215
    Y.stat    = M;
}

H
hzcheng 已提交
216 217
%type keep {tVariantList*}
%destructor keep {tVariantListDestroy($$);}
218 219 220 221
keep(Y)    ::= KEEP tagitemlist(X).           { Y = X; }

cache(Y)   ::= CACHE INTEGER(X).              { Y = X; }
replica(Y) ::= REPLICA INTEGER(X).            { Y = X; }
222
quorum(Y)  ::= QUORUM INTEGER(X).             { Y = X; }
223
days(Y)    ::= DAYS INTEGER(X).               { Y = X; }
H
hjxilinx 已提交
224 225 226
minrows(Y) ::= MINROWS INTEGER(X).            { Y = X; }
maxrows(Y) ::= MAXROWS INTEGER(X).            { Y = X; }
blocks(Y)  ::= BLOCKS INTEGER(X).             { Y = X; }
227
ctime(Y)   ::= CTIME INTEGER(X).              { Y = X; }
H
hjxilinx 已提交
228
wal(Y)     ::= WAL INTEGER(X).                { Y = X; }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
229
fsync(Y)   ::= FSYNC INTEGER(X).              { Y = X; }
230 231
comp(Y)    ::= COMP INTEGER(X).               { Y = X; }
prec(Y)    ::= PRECISION STRING(X).           { Y = X; }
Y
yihaoDeng 已提交
232
update(Y)  ::= UPDATE INTEGER(X).             { Y = X; }     
233 234

%type db_optr {SCreateDBInfo}
H
hjxilinx 已提交
235 236
db_optr(Y) ::= . {setDefaultCreateDbOption(&Y);}

237 238
db_optr(Y) ::= db_optr(Z) cache(X).          { Y = Z; Y.cacheBlockSize = strtol(X.z, NULL, 10); }
db_optr(Y) ::= db_optr(Z) replica(X).        { Y = Z; Y.replica = strtol(X.z, NULL, 10); }
239
db_optr(Y) ::= db_optr(Z) quorum(X).         { Y = Z; Y.quorum = strtol(X.z, NULL, 10); }
240
db_optr(Y) ::= db_optr(Z) days(X).           { Y = Z; Y.daysPerFile = strtol(X.z, NULL, 10); }
H
hjxilinx 已提交
241 242 243
db_optr(Y) ::= db_optr(Z) minrows(X).        { Y = Z; Y.minRowsPerBlock = strtod(X.z, NULL); }
db_optr(Y) ::= db_optr(Z) maxrows(X).        { Y = Z; Y.maxRowsPerBlock = strtod(X.z, NULL); }
db_optr(Y) ::= db_optr(Z) blocks(X).         { Y = Z; Y.numOfBlocks = strtol(X.z, NULL, 10); }
244
db_optr(Y) ::= db_optr(Z) ctime(X).          { Y = Z; Y.commitTime = strtol(X.z, NULL, 10); }
H
hjxilinx 已提交
245
db_optr(Y) ::= db_optr(Z) wal(X).            { Y = Z; Y.walLevel = strtol(X.z, NULL, 10); }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
246
db_optr(Y) ::= db_optr(Z) fsync(X).          { Y = Z; Y.fsyncPeriod = strtol(X.z, NULL, 10); }
247 248 249
db_optr(Y) ::= db_optr(Z) comp(X).           { Y = Z; Y.compressionLevel = strtol(X.z, NULL, 10); }
db_optr(Y) ::= db_optr(Z) prec(X).           { Y = Z; Y.precision = X; }
db_optr(Y) ::= db_optr(Z) keep(X).           { Y = Z; Y.keep = X; }
Y
yihaoDeng 已提交
250
db_optr(Y) ::= db_optr(Z) update(X).         { Y = Z; Y.update = strtol(X.z, NULL, 10); }
251 252

%type alter_db_optr {SCreateDBInfo}
H
hjxilinx 已提交
253
alter_db_optr(Y) ::= . { setDefaultCreateDbOption(&Y);}
S
slguan 已提交
254 255

alter_db_optr(Y) ::= alter_db_optr(Z) replica(X).     { Y = Z; Y.replica = strtol(X.z, NULL, 10); }
256
alter_db_optr(Y) ::= alter_db_optr(Z) quorum(X).      { Y = Z; Y.quorum = strtol(X.z, NULL, 10); }
H
hjxilinx 已提交
257 258 259 260
alter_db_optr(Y) ::= alter_db_optr(Z) keep(X).        { Y = Z; Y.keep = X; }
alter_db_optr(Y) ::= alter_db_optr(Z) blocks(X).      { Y = Z; Y.numOfBlocks = strtol(X.z, NULL, 10); }
alter_db_optr(Y) ::= alter_db_optr(Z) comp(X).        { Y = Z; Y.compressionLevel = strtol(X.z, NULL, 10); }
alter_db_optr(Y) ::= alter_db_optr(Z) wal(X).         { Y = Z; Y.walLevel = strtol(X.z, NULL, 10); }
H
Haojun Liao 已提交
261
alter_db_optr(Y) ::= alter_db_optr(Z) fsync(X).       { Y = Z; Y.fsyncPeriod = strtol(X.z, NULL, 10); }
Y
yihaoDeng 已提交
262
alter_db_optr(Y) ::= alter_db_optr(Z) update(X).       { Y = Z; Y.update = strtol(X.z, NULL, 10); }
H
hzcheng 已提交
263 264

%type typename {TAOS_FIELD}
dengyihao's avatar
dengyihao 已提交
265 266 267 268
typename(A) ::= ids(X). { 
  X.type = 0;
  tSQLSetColumnType (&A, &X); 
}
H
hzcheng 已提交
269 270 271

//define binary type, e.g., binary(10), nchar(10)
typename(A) ::= ids(X) LP signed(Y) RP.    {
dengyihao's avatar
dengyihao 已提交
272 273 274 275 276 277 278
    if (Y <= 0) {
      X.type = 0;
      tSQLSetColumnType(&A, &X);
    } else {
      X.type = -Y;          // negative value of name length
      tSQLSetColumnType(&A, &X);
    }
H
hzcheng 已提交
279 280
}

281 282
%type signed {int64_t}
signed(A) ::= INTEGER(X).         { A = strtol(X.z, NULL, 10); }
H
hzcheng 已提交
283 284 285 286 287 288
signed(A) ::= PLUS INTEGER(X).    { A = strtol(X.z, NULL, 10); }
signed(A) ::= MINUS INTEGER(X).   { A = -strtol(X.z, NULL, 10);}

////////////////////////////////// The CREATE TABLE statement ///////////////////////////////
cmd ::= CREATE TABLE ifnotexists(Y) ids(X) cpxName(Z) create_table_args.  {
    X.n += Z.n;
H
hjxilinx 已提交
289
    setCreatedTableName(pInfo, &X, &Y);
H
hzcheng 已提交
290 291 292 293
}

%type create_table_args{SCreateTableSQL*}
create_table_args(A) ::= LP columnlist(X) RP. {
294 295
    A = tSetCreateSQLElems(X, NULL, NULL, NULL, NULL, TSQL_CREATE_TABLE);
    setSQLInfo(pInfo, A, NULL, TSDB_SQL_CREATE_TABLE);
H
hzcheng 已提交
296 297
}

298
// create super table
H
hzcheng 已提交
299
create_table_args(A) ::= LP columnlist(X) RP TAGS LP columnlist(Y) RP. {
300 301
    A = tSetCreateSQLElems(X, Y, NULL, NULL, NULL, TSQL_CREATE_STABLE);
    setSQLInfo(pInfo, A, NULL, TSDB_SQL_CREATE_TABLE);
H
hzcheng 已提交
302 303
}

304 305
// create table by using super table
// create table table_name using super_table_name tags(tag_values1, tag_values2)
H
hzcheng 已提交
306 307
create_table_args(A) ::= USING ids(X) cpxName(F) TAGS LP tagitemlist(Y) RP.  {
    X.n += F.n;
308 309
    A = tSetCreateSQLElems(NULL, NULL, &X, Y, NULL, TSQL_CREATE_TABLE_FROM_STABLE);
    setSQLInfo(pInfo, A, NULL, TSDB_SQL_CREATE_TABLE);
H
hzcheng 已提交
310 311 312
}

// create stream
313
// create table table_name as select count(*) from super_table_name interval(time)
H
hzcheng 已提交
314 315
create_table_args(A) ::= AS select(S). {
    A = tSetCreateSQLElems(NULL, NULL, NULL, NULL, S, TSQL_CREATE_STREAM);
316
    setSQLInfo(pInfo, A, NULL, TSDB_SQL_CREATE_TABLE);
H
hzcheng 已提交
317 318 319 320
}

%type column{TAOS_FIELD}
%type columnlist{tFieldList*}
H
hjliao 已提交
321
%destructor columnlist {tFieldListDestroy($$);}
H
hzcheng 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
columnlist(A) ::= columnlist(X) COMMA column(Y).  {A = tFieldListAppend(X, &Y);   }
columnlist(A) ::= column(X).                      {A = tFieldListAppend(NULL, &X);}

// The information used for a column is the name and type of column:
// tinyint smallint int bigint float double bool timestamp binary(x) nchar(x)
column(A) ::= ids(X) typename(Y).          {
    tSQLSetColumnInfo(&A, &X, &Y);
}

%type tagitemlist {tVariantList*}
%destructor tagitemlist {tVariantListDestroy($$);}

%type tagitem {tVariant}
tagitemlist(A) ::= tagitemlist(X) COMMA tagitem(Y). { A = tVariantListAppend(X, &Y, -1);    }
tagitemlist(A) ::= tagitem(X).                      { A = tVariantListAppend(NULL, &X, -1); }

tagitem(A) ::= INTEGER(X).      {toTSDBType(X.type); tVariantCreate(&A, &X); }
tagitem(A) ::= FLOAT(X).        {toTSDBType(X.type); tVariantCreate(&A, &X); }
tagitem(A) ::= STRING(X).       {toTSDBType(X.type); tVariantCreate(&A, &X); }
tagitem(A) ::= BOOL(X).         {toTSDBType(X.type); tVariantCreate(&A, &X); }
S
slguan 已提交
342
tagitem(A) ::= NULL(X).         { X.type = 0; tVariantCreate(&A, &X); }
H
hzcheng 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357

tagitem(A) ::= MINUS(X) INTEGER(Y).{
    X.n += Y.n;
    X.type = Y.type;
    toTSDBType(X.type);
    tVariantCreate(&A, &X);
}

tagitem(A) ::= MINUS(X) FLOAT(Y).  {
    X.n += Y.n;
    X.type = Y.type;
    toTSDBType(X.type);
    tVariantCreate(&A, &X);
}

S
slguan 已提交
358 359 360 361 362 363 364 365 366 367 368 369 370
tagitem(A) ::= PLUS(X) INTEGER(Y). {
    X.n += Y.n;
    X.type = Y.type;
    toTSDBType(X.type);
    tVariantCreate(&A, &X);
}

tagitem(A) ::= PLUS(X) FLOAT(Y).  {
    X.n += Y.n;
    X.type = Y.type;
    toTSDBType(X.type);
    tVariantCreate(&A, &X);
}
H
hzcheng 已提交
371 372 373

//////////////////////// The SELECT statement /////////////////////////////////
%type select {SQuerySQL*}
374
%destructor select {doDestroyQuerySql($$);}
H
hzcheng 已提交
375
select(A) ::= SELECT(T) selcollist(W) from(X) where_opt(Y) interval_opt(K) fill_opt(F) sliding_opt(S) groupby_opt(P) orderby_opt(Z) having_opt(N) slimit_opt(G) limit_opt(L). {
S
slguan 已提交
376
  A = tSetQuerySQLElems(&T, W, X, Y, P, Z, &K, &S, F, &L, &G);
H
hzcheng 已提交
377 378
}

379 380 381 382 383
%type union {SSubclauseInfo*}
%destructor union {destroyAllSelectClause($$);}

union(Y) ::= select(X). { Y = setSubclause(NULL, X); }
union(Y) ::= LP union(X) RP. { Y = X; }
384 385
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); }
386 387 388

cmd ::= union(X). { setSQLInfo(pInfo, X, NULL, TSDB_SQL_SELECT); }

H
hjxilinx 已提交
389 390 391 392 393 394 395 396
// Support for the SQL exprssion without from & where subclauses, e.g.,
// select current_database(),
// select server_version(), select client_version(),
// select server_state();
select(A) ::= SELECT(T) selcollist(W). {
  A = tSetQuerySQLElems(&T, W, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
}

H
hzcheng 已提交
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
// selcollist is a list of expressions that are to become the return
// values of the SELECT statement.  The "*" in statements like
// "SELECT * FROM ..." is encoded as a special expression with an opcode of TK_ALL.
%type selcollist {tSQLExprList*}
%destructor selcollist {tSQLExprListDestroy($$);}

%type sclp {tSQLExprList*}
%destructor sclp {tSQLExprListDestroy($$);}
sclp(A) ::= selcollist(X) COMMA.             {A = X;}
sclp(A) ::= .                                {A = 0;}
selcollist(A) ::= sclp(P) expr(X) as(Y).     {
   A = tSQLExprListAppend(P, X, Y.n?&Y:0);
}

selcollist(A) ::= sclp(P) STAR. {
   tSQLExpr *pNode = tSQLExprIdValueCreate(NULL, TK_ALL);
   A = tSQLExprListAppend(P, pNode, 0);
}

// An option "AS <id>" phrase that can follow one of the expressions that
// define the result set, or one of the tables in the FROM clause.
//
H
Haojun Liao 已提交
419
%type as {SStrToken}
H
hzcheng 已提交
420 421 422 423 424
as(X) ::= AS ids(Y).    { X = Y;    }
as(X) ::= ids(Y).       { X = Y;    }
as(X) ::= .             { X.n = 0;  }

// A complete FROM clause.
S
slguan 已提交
425
%type from {tVariantList*}
H
hzcheng 已提交
426
// current not support query from no-table
S
slguan 已提交
427 428 429
from(A) ::= FROM tablelist(X).                 {A = X;}

%type tablelist {tVariantList*}
H
Haojun Liao 已提交
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
tablelist(A) ::= ids(X) cpxName(Y).                     {
  toTSDBType(X.type);
  X.n += Y.n;
  A = tVariantListAppendToken(NULL, &X, -1);
  A = tVariantListAppendToken(A, &X, -1);  // table alias name
}

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);
}
H
hzcheng 已提交
459 460

// The value of interval should be the form of "number+[a,s,m,h,d,n,y]" or "now"
H
Haojun Liao 已提交
461
%type tmvar {SStrToken}
H
hzcheng 已提交
462 463
tmvar(A) ::= VARIABLE(X).   {A = X;}

464 465 466 467
%type interval_opt {SIntervalVal}
interval_opt(N) ::= INTERVAL LP tmvar(E) RP.    {N.interval = E; N.offset.n = 0; N.offset.z = NULL; N.offset.type = 0;}
interval_opt(N) ::= INTERVAL LP tmvar(E) COMMA tmvar(O) RP.    {N.interval = E; N.offset = O;}
interval_opt(N) ::= .                           {memset(&N, 0, sizeof(N));}
H
hzcheng 已提交
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482

%type fill_opt {tVariantList*}
%destructor fill_opt {tVariantListDestroy($$);}
fill_opt(N) ::= .                               {N = 0;     }
fill_opt(N) ::= FILL LP ID(Y) COMMA tagitemlist(X) RP.      {
    tVariant A = {0};
    toTSDBType(Y.type);
    tVariantCreate(&A, &Y);

    tVariantListInsert(X, &A, -1, 0);
    N = X;
}

fill_opt(N) ::= FILL LP ID(Y) RP.               {
    toTSDBType(Y.type);
483
    N = tVariantListAppendToken(NULL, &Y, -1);
H
hzcheng 已提交
484 485
}

H
Haojun Liao 已提交
486
%type sliding_opt {SStrToken}
H
hzcheng 已提交
487
sliding_opt(K) ::= SLIDING LP tmvar(E) RP.      {K = E;     }
H
hjxilinx 已提交
488
sliding_opt(K) ::= .                            {K.n = 0; K.z = NULL; K.type = 0;   }
H
hzcheng 已提交
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509

%type orderby_opt {tVariantList*}
%destructor orderby_opt {tVariantListDestroy($$);}

%type sortlist {tVariantList*}
%destructor sortlist {tVariantListDestroy($$);}

%type sortitem {tVariant}
%destructor sortitem {tVariantDestroy(&$$);}

orderby_opt(A) ::= .                          {A = 0;}
orderby_opt(A) ::= ORDER BY sortlist(X).      {A = X;}

sortlist(A) ::= sortlist(X) COMMA item(Y) sortorder(Z). {
    A = tVariantListAppend(X, &Y, Z);
}

sortlist(A) ::= item(Y) sortorder(Z). {
  A = tVariantListAppend(NULL, &Y, Z);
}

510 511
%type item {tVariant}
item(A) ::= ids(X) cpxName(Y).   {
H
hzcheng 已提交
512
  toTSDBType(X.type);
513 514
  X.n += Y.n;

H
hzcheng 已提交
515 516 517 518
  tVariantCreate(&A, &X);
}

%type sortorder {int}
519 520 521
sortorder(A) ::= ASC.           {A = TSDB_ORDER_ASC; }
sortorder(A) ::= DESC.          {A = TSDB_ORDER_DESC;}
sortorder(A) ::= .              {A = TSDB_ORDER_ASC;}  //default is descend order
H
hzcheng 已提交
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552

//group by clause
%type groupby_opt {tVariantList*}
%destructor groupby_opt {tVariantListDestroy($$);}
%type grouplist {tVariantList*}
%destructor grouplist {tVariantListDestroy($$);}

groupby_opt(A) ::= .                       {A = 0;}
groupby_opt(A) ::= GROUP BY grouplist(X).  {A = X;}

grouplist(A) ::= grouplist(X) COMMA item(Y).    {
  A = tVariantListAppend(X, &Y, -1);
}

grouplist(A) ::= item(X).                       {
  A = tVariantListAppend(NULL, &X, -1);
}

//having clause, ignore the input condition in having
%type having_opt {tSQLExpr*}
%destructor having_opt {tSQLExprDestroy($$);}
having_opt(A) ::=.                  {A = 0;}
having_opt(A) ::= HAVING expr(X).   {A = X;}

//limit-offset subclause
%type limit_opt {SLimitVal}
limit_opt(A) ::= .                     {A.limit = -1; A.offset = 0;}
limit_opt(A) ::= LIMIT signed(X).      {A.limit = X;  A.offset = 0;}
limit_opt(A) ::= LIMIT signed(X) OFFSET signed(Y).
                                       {A.limit = X;  A.offset = Y;}
limit_opt(A) ::= LIMIT signed(X) COMMA signed(Y).
S
slguan 已提交
553
                                       {A.limit = Y;  A.offset = X;}
H
hzcheng 已提交
554 555 556 557 558 559 560

%type slimit_opt {SLimitVal}
slimit_opt(A) ::= .                    {A.limit = -1; A.offset = 0;}
slimit_opt(A) ::= SLIMIT signed(X).    {A.limit = X;  A.offset = 0;}
slimit_opt(A) ::= SLIMIT signed(X) SOFFSET signed(Y).
                                       {A.limit = X;  A.offset = Y;}
slimit_opt(A) ::= SLIMIT signed(X) COMMA  signed(Y).
S
slguan 已提交
561
                                       {A.limit = Y;  A.offset = X;}
H
hzcheng 已提交
562 563 564 565 566 567 568 569 570 571 572 573

%type where_opt {tSQLExpr*}
%destructor where_opt {tSQLExprDestroy($$);}

where_opt(A) ::= .                    {A = 0;}
where_opt(A) ::= WHERE expr(X).       {A = X;}

/////////////////////////// Expression Processing /////////////////////////////
//
%type expr {tSQLExpr*}
%destructor expr {tSQLExprDestroy($$);}

H
Haojun Liao 已提交
574
expr(A) ::= LP expr(X) RP.       {A = X; }
H
hzcheng 已提交
575

H
Haojun Liao 已提交
576 577 578
expr(A) ::= ID(X).               {A = tSQLExprIdValueCreate(&X, TK_ID);}
expr(A) ::= ID(X) DOT ID(Y).     {X.n += (1+Y.n); A = tSQLExprIdValueCreate(&X, TK_ID);}
expr(A) ::= ID(X) DOT STAR(Y).   {X.n += (1+Y.n); A = tSQLExprIdValueCreate(&X, TK_ALL);}
H
hzcheng 已提交
579

H
Haojun Liao 已提交
580
expr(A) ::= INTEGER(X).          {A = tSQLExprIdValueCreate(&X, TK_INTEGER);}
H
hzcheng 已提交
581 582
expr(A) ::= MINUS(X) INTEGER(Y). {X.n += Y.n; X.type = TK_INTEGER; A = tSQLExprIdValueCreate(&X, TK_INTEGER);}
expr(A) ::= PLUS(X)  INTEGER(Y). {X.n += Y.n; X.type = TK_INTEGER; A = tSQLExprIdValueCreate(&X, TK_INTEGER);}
H
Haojun Liao 已提交
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
expr(A) ::= FLOAT(X).            {A = tSQLExprIdValueCreate(&X, TK_FLOAT);}
expr(A) ::= MINUS(X) FLOAT(Y).   {X.n += Y.n; X.type = TK_FLOAT; A = tSQLExprIdValueCreate(&X, TK_FLOAT);}
expr(A) ::= PLUS(X) FLOAT(Y).    {X.n += Y.n; X.type = TK_FLOAT; A = tSQLExprIdValueCreate(&X, TK_FLOAT);}
expr(A) ::= STRING(X).           {A = tSQLExprIdValueCreate(&X, TK_STRING);}
expr(A) ::= NOW(X).              {A = tSQLExprIdValueCreate(&X, TK_NOW); }
expr(A) ::= VARIABLE(X).         {A = tSQLExprIdValueCreate(&X, TK_VARIABLE);}
expr(A) ::= BOOL(X).             {A = tSQLExprIdValueCreate(&X, TK_BOOL);}

// ordinary functions: min(x), max(x), top(k, 20)
expr(A) ::= ID(X) LP exprlist(Y) RP(E). { A = tSQLExprCreateFunction(Y, &X, &E, X.type); }

// for parsing sql functions with wildcard for parameters. e.g., count(*)/first(*)/last(*) operation
expr(A) ::= ID(X) LP STAR RP(Y).     { A = tSQLExprCreateFunction(NULL, &X, &Y, X.type); }

// is (not) null expression
expr(A) ::= expr(X) IS NULL.           {A = tSQLExprCreate(X, NULL, TK_ISNULL);}
expr(A) ::= expr(X) IS NOT NULL.       {A = tSQLExprCreate(X, NULL, TK_NOTNULL);}

// relational expression
expr(A) ::= expr(X) LT expr(Y).      {A = tSQLExprCreate(X, Y, TK_LT);}
expr(A) ::= expr(X) GT expr(Y).      {A = tSQLExprCreate(X, Y, TK_GT);}
expr(A) ::= expr(X) LE expr(Y).      {A = tSQLExprCreate(X, Y, TK_LE);}
expr(A) ::= expr(X) GE expr(Y).      {A = tSQLExprCreate(X, Y, TK_GE);}
expr(A) ::= expr(X) NE expr(Y).      {A = tSQLExprCreate(X, Y, TK_NE);}
expr(A) ::= expr(X) EQ expr(Y).      {A = tSQLExprCreate(X, Y, TK_EQ);}

expr(A) ::= expr(X) AND expr(Y).     {A = tSQLExprCreate(X, Y, TK_AND);}
expr(A) ::= expr(X) OR  expr(Y).     {A = tSQLExprCreate(X, Y, TK_OR); }

// binary arithmetic expression
H
hzcheng 已提交
613 614 615 616 617 618
expr(A) ::= expr(X) PLUS  expr(Y).   {A = tSQLExprCreate(X, Y, TK_PLUS);  }
expr(A) ::= expr(X) MINUS expr(Y).   {A = tSQLExprCreate(X, Y, TK_MINUS); }
expr(A) ::= expr(X) STAR  expr(Y).   {A = tSQLExprCreate(X, Y, TK_STAR);  }
expr(A) ::= expr(X) SLASH expr(Y).   {A = tSQLExprCreate(X, Y, TK_DIVIDE);}
expr(A) ::= expr(X) REM   expr(Y).   {A = tSQLExprCreate(X, Y, TK_REM);   }

H
Haojun Liao 已提交
619 620
// like expression
expr(A) ::= expr(X) LIKE expr(Y).    {A = tSQLExprCreate(X, Y, TK_LIKE);  }
H
hzcheng 已提交
621 622 623 624 625 626 627 628 629 630 631

//in expression
expr(A) ::= expr(X) IN LP exprlist(Y) RP.   {A = tSQLExprCreate(X, (tSQLExpr*)Y, TK_IN); }

%type exprlist {tSQLExprList*}
%destructor exprlist {tSQLExprListDestroy($$);}

%type expritem {tSQLExpr*}
%destructor expritem {tSQLExprDestroy($$);}

exprlist(A) ::= exprlist(X) COMMA expritem(Y). {A = tSQLExprListAppend(X,Y,0);}
H
Haojun Liao 已提交
632 633 634
exprlist(A) ::= expritem(X).                   {A = tSQLExprListAppend(0,X,0);}
expritem(A) ::= expr(X).                       {A = X;}
expritem(A) ::= .                              {A = 0;}
H
hzcheng 已提交
635 636

///////////////////////////////////reset query cache//////////////////////////////////////
637
cmd ::= RESET QUERY CACHE.  { setDCLSQLElems(pInfo, TSDB_SQL_RESET_CACHE, 0);}
H
hzcheng 已提交
638 639 640 641

///////////////////////////////////ALTER TABLE statement//////////////////////////////////
cmd ::= ALTER TABLE ids(X) cpxName(F) ADD COLUMN columnlist(A).     {
    X.n += F.n;
642 643
    SAlterTableSQL* pAlterTable = tAlterTableSQLElems(&X, A, NULL, TSDB_ALTER_TABLE_ADD_COLUMN);
    setSQLInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE);
H
hzcheng 已提交
644 645 646 647 648 649
}

cmd ::= ALTER TABLE ids(X) cpxName(F) DROP COLUMN ids(A).     {
    X.n += F.n;

    toTSDBType(A.type);
650
    tVariantList* K = tVariantListAppendToken(NULL, &A, -1);
H
hzcheng 已提交
651

652 653
    SAlterTableSQL* pAlterTable = tAlterTableSQLElems(&X, NULL, K, TSDB_ALTER_TABLE_DROP_COLUMN);
    setSQLInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE);
H
hzcheng 已提交
654 655 656 657 658
}

//////////////////////////////////ALTER TAGS statement/////////////////////////////////////
cmd ::= ALTER TABLE ids(X) cpxName(Y) ADD TAG columnlist(A).        {
    X.n += Y.n;
659 660
    SAlterTableSQL* pAlterTable = tAlterTableSQLElems(&X, A, NULL, TSDB_ALTER_TABLE_ADD_TAG_COLUMN);
    setSQLInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE);
H
hzcheng 已提交
661 662 663 664 665
}
cmd ::= ALTER TABLE ids(X) cpxName(Z) DROP TAG ids(Y).          {
    X.n += Z.n;

    toTSDBType(Y.type);
666
    tVariantList* A = tVariantListAppendToken(NULL, &Y, -1);
H
hzcheng 已提交
667

668 669
    SAlterTableSQL* pAlterTable = tAlterTableSQLElems(&X, NULL, A, TSDB_ALTER_TABLE_DROP_TAG_COLUMN);
    setSQLInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE);
H
hzcheng 已提交
670 671 672 673 674 675
}

cmd ::= ALTER TABLE ids(X) cpxName(F) CHANGE TAG ids(Y) ids(Z). {
    X.n += F.n;

    toTSDBType(Y.type);
676
    tVariantList* A = tVariantListAppendToken(NULL, &Y, -1);
H
hzcheng 已提交
677 678

    toTSDBType(Z.type);
679
    A = tVariantListAppendToken(A, &Z, -1);
H
hzcheng 已提交
680

681 682
    SAlterTableSQL* pAlterTable = tAlterTableSQLElems(&X, NULL, A, TSDB_ALTER_TABLE_CHANGE_TAG_COLUMN);
    setSQLInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE);
H
hzcheng 已提交
683 684
}

685
cmd ::= ALTER TABLE ids(X) cpxName(F) SET TAG ids(Y) EQ tagitem(Z).     {
H
hzcheng 已提交
686 687 688
    X.n += F.n;

    toTSDBType(Y.type);
689
    tVariantList* A = tVariantListAppendToken(NULL, &Y, -1);
H
hzcheng 已提交
690 691
    A = tVariantListAppend(A, &Z, -1);

692 693
    SAlterTableSQL* pAlterTable = tAlterTableSQLElems(&X, NULL, A, TSDB_ALTER_TABLE_UPDATE_TAG_VAL);
    setSQLInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE);
H
hzcheng 已提交
694 695 696
}

////////////////////////////////////////kill statement///////////////////////////////////////
H
Haojun Liao 已提交
697 698 699
cmd ::= KILL CONNECTION INTEGER(Y).   {setKillSQL(pInfo, TSDB_SQL_KILL_CONNECTION, &Y);}
cmd ::= KILL STREAM INTEGER(X) COLON(Z) INTEGER(Y).       {X.n += (Z.n + Y.n); setKillSQL(pInfo, TSDB_SQL_KILL_STREAM, &X);}
cmd ::= KILL QUERY INTEGER(X) COLON(Z) INTEGER(Y).        {X.n += (Z.n + Y.n); setKillSQL(pInfo, TSDB_SQL_KILL_QUERY, &X);}
H
hzcheng 已提交
700 701 702 703 704

%fallback ID ABORT AFTER ASC ATTACH BEFORE BEGIN CASCADE CLUSTER CONFLICT COPY DATABASE DEFERRED
  DELIMITERS DESC DETACH EACH END EXPLAIN FAIL FOR GLOB IGNORE IMMEDIATE INITIALLY INSTEAD
  LIKE MATCH KEY OF OFFSET RAISE REPLACE RESTRICT ROW STATEMENT TRIGGER VIEW ALL
  COUNT SUM AVG MIN MAX FIRST LAST TOP BOTTOM STDDEV PERCENTILE APERCENTILE LEASTSQUARES HISTOGRAM DIFF
705
  SPREAD TWA INTERP LAST_ROW RATE IRATE SUM_RATE SUM_IRATE AVG_RATE AVG_IRATE TBID NOW IPTOKEN SEMI NONE PREV LINEAR IMPORT
H
hjxilinx 已提交
706
  METRIC TBNAME JOIN METRICS STABLE NULL INSERT INTO VALUES.