sql.y 33.7 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
Haojun Liao 已提交
39
  int32_t outputBufLen = tListLen(pInfo->msg);
H
hzcheng 已提交
40 41 42 43 44 45 46 47 48
  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);
H
Haojun Liao 已提交
49
        len = sprintf(pInfo->msg, msg, tmpstr);
H
hzcheng 已提交
50
    } else {
H
Haojun Liao 已提交
51
        len = sprintf(pInfo->msg, msg, &TOKEN.z[0]);
H
hzcheng 已提交
52 53 54
    }

  } else {
H
Haojun Liao 已提交
55
    len = sprintf(pInfo->msg, "Incomplete SQL statement");
H
hzcheng 已提交
56 57 58 59 60 61 62 63 64 65
  }

  assert(len <= outputBufLen);
}

%parse_accept       {}

program ::= cmd.    {}

//////////////////////////////////THE SHOW STATEMENT///////////////////////////////////////////
66
cmd ::= SHOW DATABASES.  { setShowOptions(pInfo, TSDB_MGMT_TABLE_DB, 0, 0);}
D
fix bug  
dapan1121 已提交
67
cmd ::= SHOW TOPICS.     { setShowOptions(pInfo, TSDB_MGMT_TABLE_TP, 0, 0);}
68 69 70 71 72 73 74 75 76
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 已提交
77
cmd ::= SHOW VARIABLES.  { setShowOptions(pInfo, TSDB_MGMT_TABLE_VARIABLES, 0, 0);  }
78 79 80 81 82
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 已提交
83

84

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

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

93 94
cmd ::= SHOW CREATE TABLE ids(X) cpxName(Y).    {
   X.n += Y.n;
H
Haojun Liao 已提交
95
   setDCLSqlElems(pInfo, TSDB_SQL_SHOW_CREATE_TABLE, 1, &X);
96 97 98
}    

cmd ::= SHOW CREATE DATABASE ids(X). {
H
Haojun Liao 已提交
99
  setDCLSqlElems(pInfo, TSDB_SQL_SHOW_CREATE_DATABASE, 1, &X);
100 101
} 

H
hzcheng 已提交
102
cmd ::= SHOW dbPrefix(X) TABLES.         {
103
    setShowOptions(pInfo, TSDB_MGMT_TABLE_TABLE, &X, 0);
H
hzcheng 已提交
104 105 106
}

cmd ::= SHOW dbPrefix(X) TABLES LIKE ids(Y).         {
107
    setShowOptions(pInfo, TSDB_MGMT_TABLE_TABLE, &X, &Y);
H
hzcheng 已提交
108 109 110
}

cmd ::= SHOW dbPrefix(X) STABLES.      {
111
    setShowOptions(pInfo, TSDB_MGMT_TABLE_METRIC, &X, 0);
H
hzcheng 已提交
112 113 114
}

cmd ::= SHOW dbPrefix(X) STABLES LIKE ids(Y).      {
H
Haojun Liao 已提交
115
    SStrToken token;
H
Haojun Liao 已提交
116
    tSetDbName(&token, &X);
117
    setShowOptions(pInfo, TSDB_MGMT_TABLE_METRIC, &token, &Y);
H
hzcheng 已提交
118 119 120
}

cmd ::= SHOW dbPrefix(X) VGROUPS.    {
H
Haojun Liao 已提交
121
    SStrToken token;
H
Haojun Liao 已提交
122
    tSetDbName(&token, &X);
123
    setShowOptions(pInfo, TSDB_MGMT_TABLE_VGROUP, &token, 0);
H
hzcheng 已提交
124 125
}

L
[1361]  
lihui 已提交
126
cmd ::= SHOW dbPrefix(X) VGROUPS ids(Y).    {
H
Haojun Liao 已提交
127
    SStrToken token;
H
Haojun Liao 已提交
128
    tSetDbName(&token, &X);
H
hjxilinx 已提交
129
    setShowOptions(pInfo, TSDB_MGMT_TABLE_VGROUP, &token, &Y);
L
[1361]  
lihui 已提交
130 131
}

H
hzcheng 已提交
132 133 134
//drop configure for tables
cmd ::= DROP TABLE ifexists(Y) ids(X) cpxName(Z).   {
    X.n += Z.n;
D
fix bug  
dapan1121 已提交
135
    setDropDbTableInfo(pInfo, TSDB_SQL_DROP_TABLE, &X, &Y, -1, -1);
H
hzcheng 已提交
136 137
}

D
dapan1121 已提交
138 139 140
//drop stable
cmd ::= DROP STABLE ifexists(Y) ids(X) cpxName(Z).   {
    X.n += Z.n;
D
fix bug  
dapan1121 已提交
141
    setDropDbTableInfo(pInfo, TSDB_SQL_DROP_TABLE, &X, &Y, -1, TSDB_SUPER_TABLE);
D
dapan1121 已提交
142 143
}

D
fix bug  
dapan1121 已提交
144 145 146
cmd ::= DROP DATABASE ifexists(Y) ids(X).    { setDropDbTableInfo(pInfo, TSDB_SQL_DROP_DB, &X, &Y, TSDB_DB_TYPE_DEFAULT, -1); }
cmd ::= DROP TOPIC ifexists(Y) ids(X).    { setDropDbTableInfo(pInfo, TSDB_SQL_DROP_DB, &X, &Y, TSDB_DB_TYPE_TOPIC, -1); }

H
Haojun Liao 已提交
147 148 149
cmd ::= DROP DNODE ids(X).       { setDCLSqlElems(pInfo, TSDB_SQL_DROP_DNODE, 1, &X);    }
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 已提交
150 151

/////////////////////////////////THE USE STATEMENT//////////////////////////////////////////
H
Haojun Liao 已提交
152
cmd ::= USE ids(X).              { setDCLSqlElems(pInfo, TSDB_SQL_USE_DB, 1, &X);}
H
hzcheng 已提交
153 154 155 156

/////////////////////////////////THE DESCRIBE STATEMENT/////////////////////////////////////
cmd ::= DESCRIBE ids(X) cpxName(Y). {
    X.n += Y.n;
H
Haojun Liao 已提交
157
    setDCLSqlElems(pInfo, TSDB_SQL_DESCRIBE_TABLE, 1, &X);
H
hzcheng 已提交
158 159 160
}

/////////////////////////////////THE ALTER STATEMENT////////////////////////////////////////
D
dapan1121 已提交
161 162
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
Haojun Liao 已提交
163 164 165 166
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);      }
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 已提交
167
cmd ::= ALTER DATABASE ids(X) alter_db_optr(Y). { SStrToken t = {0};  setCreateDbInfo(pInfo, TSDB_SQL_ALTER_DB, &X, &Y, &t);}
D
fix bug  
dapan1121 已提交
168
cmd ::= ALTER TOPIC ids(X) alter_topic_optr(Y). { SStrToken t = {0};  setCreateDbInfo(pInfo, TSDB_SQL_ALTER_DB, &X, &Y, &t);}
H
hzcheng 已提交
169

D
dapan1121 已提交
170 171
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 已提交
172

H
hzcheng 已提交
173 174 175
// 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 已提交
176
%type ids {SStrToken}
H
hzcheng 已提交
177 178 179
ids(A) ::= ID(X).        {A = X; }
ids(A) ::= STRING(X).    {A = X; }

H
Haojun Liao 已提交
180
%type ifexists {SStrToken}
181 182
ifexists(X) ::= IF EXISTS.          { X.n = 1;}
ifexists(X) ::= .                   { X.n = 0;}
H
hzcheng 已提交
183

H
Haojun Liao 已提交
184
%type ifnotexists {SStrToken}
185 186
ifnotexists(X) ::= IF NOT EXISTS.   { X.n = 1;}
ifnotexists(X) ::= .                { X.n = 0;}
H
hzcheng 已提交
187 188

/////////////////////////////////THE CREATE STATEMENT///////////////////////////////////////
S
slguan 已提交
189
//create option for dnode/db/user/account
H
Haojun Liao 已提交
190
cmd ::= CREATE DNODE   ids(X).     { setDCLSqlElems(pInfo, TSDB_SQL_CREATE_DNODE, 1, &X);}
S
slguan 已提交
191
cmd ::= CREATE ACCOUNT ids(X) PASS ids(Y) acct_optr(Z).
D
dapan1121 已提交
192
                                { setCreateAcctSql(pInfo, TSDB_SQL_CREATE_ACCT, &X, &Y, &Z);}
H
Haojun Liao 已提交
193
cmd ::= CREATE DATABASE ifnotexists(Z) ids(X) db_optr(Y).  { setCreateDbInfo(pInfo, TSDB_SQL_CREATE_DB, &X, &Y, &Z);}
D
fix bug  
dapan1121 已提交
194
cmd ::= CREATE TOPIC ifnotexists(Z) ids(X) topic_optr(Y).  { setCreateDbInfo(pInfo, TSDB_SQL_CREATE_DB, &X, &Y, &Z);}
D
dapan1121 已提交
195
cmd ::= CREATE USER ids(X) PASS ids(Y).     { setCreateUserSql(pInfo, &X, &Y);}
H
hzcheng 已提交
196

197 198
pps(Y) ::= .                                { Y.n = 0;   }
pps(Y) ::= PPS INTEGER(X).                  { Y = X;     }
S
slguan 已提交
199

200 201
tseries(Y) ::= .                            { Y.n = 0;   }
tseries(Y) ::= TSERIES INTEGER(X).          { Y = X;     }
S
slguan 已提交
202

203 204
dbs(Y) ::= .                                { Y.n = 0;   }
dbs(Y) ::= DBS INTEGER(X).                  { Y = X;     }
S
slguan 已提交
205

206 207
streams(Y) ::= .                            { Y.n = 0;   }
streams(Y) ::= STREAMS INTEGER(X).          { Y = X;     }
S
slguan 已提交
208

209 210
storage(Y) ::= .                            { Y.n = 0;   }
storage(Y) ::= STORAGE INTEGER(X).          { Y = X;     }
S
slguan 已提交
211

212 213
qtime(Y) ::= .                              { Y.n = 0;   }
qtime(Y) ::= QTIME INTEGER(X).              { Y = X;     }
S
slguan 已提交
214

215 216
users(Y) ::= .                              { Y.n = 0;   }
users(Y) ::= USERS INTEGER(X).              { Y = X;     }
S
slguan 已提交
217

218 219
conns(Y) ::= .                              { Y.n = 0;   }
conns(Y) ::= CONNS INTEGER(X).              { Y = X;     }
S
slguan 已提交
220

221 222
state(Y) ::= .                              { Y.n = 0;   }
state(Y) ::= STATE ids(X).                  { Y = X;     }
S
slguan 已提交
223

H
Haojun Liao 已提交
224
%type acct_optr {SCreateAcctInfo}
S
slguan 已提交
225
acct_optr(Y) ::= pps(C) tseries(D) storage(P) streams(F) qtime(Q) dbs(E) users(K) conns(L) state(M). {
226 227 228 229 230 231 232 233
    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 已提交
234 235 236
    Y.stat    = M;
}

H
Haojun Liao 已提交
237 238
%type keep {SArray*}
%destructor keep {taosArrayDestroy($$);}
239 240 241 242
keep(Y)    ::= KEEP tagitemlist(X).           { Y = X; }

cache(Y)   ::= CACHE INTEGER(X).              { Y = X; }
replica(Y) ::= REPLICA INTEGER(X).            { Y = X; }
243
quorum(Y)  ::= QUORUM INTEGER(X).             { Y = X; }
244
days(Y)    ::= DAYS INTEGER(X).               { Y = X; }
H
hjxilinx 已提交
245 246 247
minrows(Y) ::= MINROWS INTEGER(X).            { Y = X; }
maxrows(Y) ::= MAXROWS INTEGER(X).            { Y = X; }
blocks(Y)  ::= BLOCKS INTEGER(X).             { Y = X; }
248
ctime(Y)   ::= CTIME INTEGER(X).              { Y = X; }
H
hjxilinx 已提交
249
wal(Y)     ::= WAL INTEGER(X).                { Y = X; }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
250
fsync(Y)   ::= FSYNC INTEGER(X).              { Y = X; }
251 252
comp(Y)    ::= COMP INTEGER(X).               { Y = X; }
prec(Y)    ::= PRECISION STRING(X).           { Y = X; }
Y
yihaoDeng 已提交
253
update(Y)  ::= UPDATE INTEGER(X).             { Y = X; }     
D
dapan1121 已提交
254
cachelast(Y) ::= CACHELAST INTEGER(X).        { Y = X; }
D
fix bug  
dapan1121 已提交
255
partitions(Y) ::= PARTITIONS INTEGER(X).      { Y = X; }
256

H
Haojun Liao 已提交
257
%type db_optr {SCreateDbInfo}
D
fix bug  
dapan1121 已提交
258
db_optr(Y) ::= . {setDefaultCreateDbOption(&Y); Y.dbType = TSDB_DB_TYPE_DEFAULT;}
H
hjxilinx 已提交
259

260 261
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); }
262
db_optr(Y) ::= db_optr(Z) quorum(X).         { Y = Z; Y.quorum = strtol(X.z, NULL, 10); }
263
db_optr(Y) ::= db_optr(Z) days(X).           { Y = Z; Y.daysPerFile = strtol(X.z, NULL, 10); }
H
hjxilinx 已提交
264 265 266
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); }
267
db_optr(Y) ::= db_optr(Z) ctime(X).          { Y = Z; Y.commitTime = strtol(X.z, NULL, 10); }
H
hjxilinx 已提交
268
db_optr(Y) ::= db_optr(Z) wal(X).            { Y = Z; Y.walLevel = strtol(X.z, NULL, 10); }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
269
db_optr(Y) ::= db_optr(Z) fsync(X).          { Y = Z; Y.fsyncPeriod = strtol(X.z, NULL, 10); }
270 271 272
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 已提交
273
db_optr(Y) ::= db_optr(Z) update(X).         { Y = Z; Y.update = strtol(X.z, NULL, 10); }
D
dapan1121 已提交
274
db_optr(Y) ::= db_optr(Z) cachelast(X).      { Y = Z; Y.cachelast = strtol(X.z, NULL, 10); }
275

D
fix bug  
dapan1121 已提交
276 277 278 279 280
%type topic_optr {SCreateDbInfo}

topic_optr(Y) ::= db_optr(Z).                       { Y = Z; Y.dbType = TSDB_DB_TYPE_TOPIC; }
topic_optr(Y) ::= topic_optr(Z) partitions(X).      { Y = Z; Y.partitions = strtol(X.z, NULL, 10); }

H
Haojun Liao 已提交
281
%type alter_db_optr {SCreateDbInfo}
D
fix bug  
dapan1121 已提交
282
alter_db_optr(Y) ::= . { setDefaultCreateDbOption(&Y); Y.dbType = TSDB_DB_TYPE_DEFAULT;}
S
slguan 已提交
283 284

alter_db_optr(Y) ::= alter_db_optr(Z) replica(X).     { Y = Z; Y.replica = strtol(X.z, NULL, 10); }
285
alter_db_optr(Y) ::= alter_db_optr(Z) quorum(X).      { Y = Z; Y.quorum = strtol(X.z, NULL, 10); }
H
hjxilinx 已提交
286 287 288 289
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 已提交
290
alter_db_optr(Y) ::= alter_db_optr(Z) fsync(X).       { Y = Z; Y.fsyncPeriod = strtol(X.z, NULL, 10); }
291
alter_db_optr(Y) ::= alter_db_optr(Z) update(X).      { Y = Z; Y.update = strtol(X.z, NULL, 10); }
D
dapan1121 已提交
292
alter_db_optr(Y) ::= alter_db_optr(Z) cachelast(X).   { Y = Z; Y.cachelast = strtol(X.z, NULL, 10); }
H
hzcheng 已提交
293

D
fix bug  
dapan1121 已提交
294 295 296 297 298
%type alter_topic_optr {SCreateDbInfo}

alter_topic_optr(Y) ::= alter_db_optr(Z).                       { Y = Z; Y.dbType = TSDB_DB_TYPE_TOPIC; }
alter_topic_optr(Y) ::= alter_topic_optr(Z) partitions(X).      { Y = Z; Y.partitions = strtol(X.z, NULL, 10); }

H
hzcheng 已提交
299
%type typename {TAOS_FIELD}
dengyihao's avatar
dengyihao 已提交
300 301
typename(A) ::= ids(X). { 
  X.type = 0;
H
Haojun Liao 已提交
302
  tSetColumnType (&A, &X);
dengyihao's avatar
dengyihao 已提交
303
}
H
hzcheng 已提交
304 305 306

//define binary type, e.g., binary(10), nchar(10)
typename(A) ::= ids(X) LP signed(Y) RP.    {
307 308
  if (Y <= 0) {
    X.type = 0;
H
Haojun Liao 已提交
309
    tSetColumnType(&A, &X);
310 311
  } else {
    X.type = -Y;  // negative value of name length
H
Haojun Liao 已提交
312
    tSetColumnType(&A, &X);
313
  }
H
hzcheng 已提交
314 315
}

316 317 318 319
// define the unsigned number type
typename(A) ::= ids(X) UNSIGNED(Z). {
  X.type = 0;
  X.n = ((Z.z + Z.n) - X.z);
H
Haojun Liao 已提交
320
  tSetColumnType (&A, &X);
321 322
}

323 324
%type signed {int64_t}
signed(A) ::= INTEGER(X).         { A = strtol(X.z, NULL, 10); }
H
hzcheng 已提交
325 326 327 328
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 ///////////////////////////////
329 330
cmd ::= CREATE TABLE  create_table_args.  {}
cmd ::= CREATE TABLE  create_stable_args. {}
D
dapan1121 已提交
331
cmd ::= CREATE STABLE create_stable_args. {}
332 333
cmd ::= CREATE TABLE create_table_list(Z). { pInfo->type = TSDB_SQL_CREATE_TABLE; pInfo->pCreateTableInfo = Z;}

H
Haojun Liao 已提交
334
%type create_table_list{SCreateTableSql*}
H
Haojun Liao 已提交
335
%destructor create_table_list{destroyCreateTableSql($$);}
336
create_table_list(A) ::= create_from_stable(Z). {
H
Haojun Liao 已提交
337
  SCreateTableSql* pCreateTable = calloc(1, sizeof(SCreateTableSql));
338 339 340 341 342 343 344 345 346 347
  pCreateTable->childTableInfo = taosArrayInit(4, sizeof(SCreatedTableInfo));

  taosArrayPush(pCreateTable->childTableInfo, &Z);
  pCreateTable->type = TSQL_CREATE_TABLE_FROM_STABLE;
  A = pCreateTable;
}

create_table_list(A) ::= create_table_list(X) create_from_stable(Z). {
  taosArrayPush(X->childTableInfo, &Z);
  A = X;
H
hzcheng 已提交
348 349
}

H
Haojun Liao 已提交
350
%type create_table_args{SCreateTableSql*}
351
create_table_args(A) ::= ifnotexists(U) ids(V) cpxName(Z) LP columnlist(X) RP. {
H
Haojun Liao 已提交
352
  A = tSetCreateTableInfo(X, NULL, NULL, TSQL_CREATE_TABLE);
D
dapan1121 已提交
353
  setSqlInfo(pInfo, A, NULL, TSDB_SQL_CREATE_TABLE);
354 355 356

  V.n += Z.n;
  setCreatedTableName(pInfo, &V, &U);
H
hzcheng 已提交
357 358
}

359
// create super table
H
Haojun Liao 已提交
360
%type create_stable_args{SCreateTableSql*}
D
dapan1121 已提交
361
create_stable_args(A) ::= ifnotexists(U) ids(V) cpxName(Z) LP columnlist(X) RP TAGS LP columnlist(Y) RP. {
H
Haojun Liao 已提交
362
  A = tSetCreateTableInfo(X, Y, NULL, TSQL_CREATE_STABLE);
D
dapan1121 已提交
363
  setSqlInfo(pInfo, A, NULL, TSDB_SQL_CREATE_TABLE);
364 365 366

  V.n += Z.n;
  setCreatedTableName(pInfo, &V, &U);
H
hzcheng 已提交
367 368
}

369 370
// create table by using super table
// create table table_name using super_table_name tags(tag_values1, tag_values2)
371 372 373 374
%type create_from_stable{SCreatedTableInfo}
create_from_stable(A) ::= ifnotexists(U) ids(V) cpxName(Z) USING ids(X) cpxName(F) TAGS LP tagitemlist(Y) RP.  {
  X.n += F.n;
  V.n += Z.n;
375
  A = createNewChildTableInfo(&X, NULL, Y, &V, &U);
H
hzcheng 已提交
376 377
}

378 379 380 381 382 383 384 385 386 387 388
create_from_stable(A) ::= ifnotexists(U) ids(V) cpxName(Z) USING ids(X) cpxName(F) LP tagNamelist(P) RP TAGS LP tagitemlist(Y) RP.  {
  X.n += F.n;
  V.n += Z.n;
  A = createNewChildTableInfo(&X, P, Y, &V, &U);
}

%type tagNamelist{SArray*}
%destructor tagNamelist {taosArrayDestroy($$);}
tagNamelist(A) ::= tagNamelist(X) COMMA ids(Y).  {taosArrayPush(X, &Y); A = X;  }
tagNamelist(A) ::= ids(X).                      {A = taosArrayInit(4, sizeof(SStrToken)); taosArrayPush(A, &X);}

H
hzcheng 已提交
389
// create stream
390
// create table table_name as select count(*) from super_table_name interval(time)
391
create_table_args(A) ::= ifnotexists(U) ids(V) cpxName(Z) AS select(S). {
H
Haojun Liao 已提交
392
  A = tSetCreateTableInfo(NULL, NULL, S, TSQL_CREATE_STREAM);
D
dapan1121 已提交
393
  setSqlInfo(pInfo, A, NULL, TSDB_SQL_CREATE_TABLE);
394

B
Bomin Zhang 已提交
395 396
  V.n += Z.n;
  setCreatedTableName(pInfo, &V, &U);
H
hzcheng 已提交
397 398 399
}

%type column{TAOS_FIELD}
H
Haojun Liao 已提交
400 401
%type columnlist{SArray*}
%destructor columnlist {taosArrayDestroy($$);}
H
Haojun Liao 已提交
402
columnlist(A) ::= columnlist(X) COMMA column(Y).  {taosArrayPush(X, &Y); A = X;  }
H
Haojun Liao 已提交
403
columnlist(A) ::= column(X).                      {A = taosArrayInit(4, sizeof(TAOS_FIELD)); taosArrayPush(A, &X);}
H
hzcheng 已提交
404 405 406 407

// 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).          {
H
Haojun Liao 已提交
408
  tSetColumnInfo(&A, &X, &Y);
H
hzcheng 已提交
409 410
}

H
Haojun Liao 已提交
411 412
%type tagitemlist {SArray*}
%destructor tagitemlist {taosArrayDestroy($$);}
H
hzcheng 已提交
413 414 415 416 417

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

418 419 420 421
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 已提交
422
tagitem(A) ::= NULL(X).         { X.type = 0; tVariantCreate(&A, &X); }
H
hzcheng 已提交
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437

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 已提交
438 439 440 441 442 443 444 445 446 447 448 449 450
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 已提交
451 452

//////////////////////// The SELECT statement /////////////////////////////////
453 454
%type select {SSqlNode*}
%destructor select {destroySqlNode($$);}
455
select(A) ::= SELECT(T) selcollist(W) from(X) where_opt(Y) interval_opt(K) session_option(H) fill_opt(F) sliding_opt(S) groupby_opt(P) orderby_opt(Z) having_opt(N) slimit_opt(G) limit_opt(L). {
456
  A = tSetQuerySqlNode(&T, W, X, Y, P, Z, &K, &H, &S, F, &L, &G, N);
H
hzcheng 已提交
457 458
}

H
Haojun Liao 已提交
459 460
select(A) ::= LP select(B) RP. {A = B;}

461 462
%type union {SArray*}
%destructor union {destroyAllSqlNode($$);}
463
union(Y) ::= select(X). { Y = setSubclause(NULL, X); }
464
union(Y) ::= union(Z) UNION ALL select(X). { Y = appendSelectClause(Z, X); }
465

D
dapan1121 已提交
466
cmd ::= union(X). { setSqlInfo(pInfo, X, NULL, TSDB_SQL_SELECT); }
467

H
hjxilinx 已提交
468
// Support for the SQL exprssion without from & where subclauses, e.g.,
H
Haojun Liao 已提交
469 470 471 472
// select current_database()
// select server_version()
// select client_version()
// select server_state()
H
hjxilinx 已提交
473
select(A) ::= SELECT(T) selcollist(W). {
D
dapan1121 已提交
474
  A = tSetQuerySqlNode(&T, W, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
H
hjxilinx 已提交
475 476
}

H
hzcheng 已提交
477 478 479
// 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.
H
Haojun Liao 已提交
480
%type selcollist {SArray*}
D
dapan1121 已提交
481
%destructor selcollist {tSqlExprListDestroy($$);}
H
hzcheng 已提交
482

H
Haojun Liao 已提交
483
%type sclp {SArray*}
D
dapan1121 已提交
484
%destructor sclp {tSqlExprListDestroy($$);}
H
hzcheng 已提交
485 486
sclp(A) ::= selcollist(X) COMMA.             {A = X;}
sclp(A) ::= .                                {A = 0;}
dengyihao's avatar
TD-2571  
dengyihao 已提交
487 488
selcollist(A) ::= sclp(P) distinct(Z) expr(X) as(Y).     {
   A = tSqlExprListAppend(P, X,  Z.n? &Z:0, Y.n?&Y:0);
H
hzcheng 已提交
489 490 491
}

selcollist(A) ::= sclp(P) STAR. {
H
Haojun Liao 已提交
492
   tSqlExpr *pNode = tSqlExprCreateIdValue(NULL, TK_ALL);
dengyihao's avatar
TD-2571  
dengyihao 已提交
493
   A = tSqlExprListAppend(P, pNode, 0, 0);
H
hzcheng 已提交
494 495 496 497
}

// 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 已提交
498
%type as {SStrToken}
H
hzcheng 已提交
499 500 501 502
as(X) ::= AS ids(Y).    { X = Y;    }
as(X) ::= ids(Y).       { X = Y;    }
as(X) ::= .             { X.n = 0;  }

dengyihao's avatar
TD-2571  
dengyihao 已提交
503 504 505 506
%type distinct {SStrToken}
distinct(X) ::= DISTINCT(Y). { X = Y;  }
distinct(X) ::= .            { X.n = 0;}

H
hzcheng 已提交
507
// A complete FROM clause.
508
%type from {SRelationInfo*}
H
Haojun Liao 已提交
509
%destructor from {destroyRelationInfo($$);}
S
slguan 已提交
510
from(A) ::= FROM tablelist(X).                 {A = X;}
H
Haojun Liao 已提交
511
from(A) ::= FROM LP union(Y) RP.               {A = setSubquery(NULL, Y);}
S
slguan 已提交
512

513
%type tablelist {SRelationInfo*}
H
Haojun Liao 已提交
514
%destructor tablelist {destroyRelationInfo($$);}
H
Haojun Liao 已提交
515 516
tablelist(A) ::= ids(X) cpxName(Y).                     {
  X.n += Y.n;
H
Haojun Liao 已提交
517
  A = setTableNameList(NULL, &X, NULL);
H
Haojun Liao 已提交
518 519 520
}

tablelist(A) ::= ids(X) cpxName(Y) ids(Z).             {
521
  X.n += Y.n;
H
Haojun Liao 已提交
522
  A = setTableNameList(NULL, &X, &Z);
H
Haojun Liao 已提交
523 524 525 526
}

tablelist(A) ::= tablelist(Y) COMMA ids(X) cpxName(Z).  {
  X.n += Z.n;
H
Haojun Liao 已提交
527
  A = setTableNameList(Y, &X, NULL);
H
Haojun Liao 已提交
528 529 530
}

tablelist(A) ::= tablelist(Y) COMMA ids(X) cpxName(Z) ids(F). {
531
  X.n += Z.n;
H
Haojun Liao 已提交
532
  A = setTableNameList(Y, &X, &F);
H
Haojun Liao 已提交
533
}
H
hzcheng 已提交
534 535

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

539
%type interval_opt {SIntervalVal}
540 541
interval_opt(N) ::= INTERVAL LP tmvar(E) RP.    {N.interval = E; N.offset.n = 0;}
interval_opt(N) ::= INTERVAL LP tmvar(E) COMMA tmvar(X) RP.    {N.interval = E; N.offset = X;}
542
interval_opt(N) ::= .                           {memset(&N, 0, sizeof(N));}
H
hzcheng 已提交
543

544 545 546 547 548 549 550 551
%type session_option {SSessionWindowVal}
session_option(X) ::= .                                                  {X.col.n = 0; X.gap.n = 0;}
session_option(X) ::= SESSION LP ids(V) cpxName(Z) COMMA tmvar(Y) RP.    {
   V.n += Z.n;
   X.col = V;
   X.gap = Y;
}

H
Haojun Liao 已提交
552 553
%type fill_opt {SArray*}
%destructor fill_opt {taosArrayDestroy($$);}
554
fill_opt(N) ::= .                                           { N = 0;     }
H
hzcheng 已提交
555 556 557 558 559
fill_opt(N) ::= FILL LP ID(Y) COMMA tagitemlist(X) RP.      {
    tVariant A = {0};
    toTSDBType(Y.type);
    tVariantCreate(&A, &Y);

H
Haojun Liao 已提交
560
    tVariantListInsert(X, &A, -1, 0);
H
hzcheng 已提交
561 562 563 564 565
    N = X;
}

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

H
Haojun Liao 已提交
569
%type sliding_opt {SStrToken}
H
hzcheng 已提交
570
sliding_opt(K) ::= SLIDING LP tmvar(E) RP.      {K = E;     }
H
hjxilinx 已提交
571
sliding_opt(K) ::= .                            {K.n = 0; K.z = NULL; K.type = 0;   }
H
hzcheng 已提交
572

H
Haojun Liao 已提交
573 574
%type orderby_opt {SArray*}
%destructor orderby_opt {taosArrayDestroy($$);}
H
hzcheng 已提交
575

H
Haojun Liao 已提交
576 577
%type sortlist {SArray*}
%destructor sortlist {taosArrayDestroy($$);}
H
hzcheng 已提交
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592

%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);
}

593 594
%type item {tVariant}
item(A) ::= ids(X) cpxName(Y).   {
H
hzcheng 已提交
595
  toTSDBType(X.type);
596 597
  X.n += Y.n;

H
hzcheng 已提交
598 599 600 601
  tVariantCreate(&A, &X);
}

%type sortorder {int}
602 603 604
sortorder(A) ::= ASC.           { A = TSDB_ORDER_ASC; }
sortorder(A) ::= DESC.          { A = TSDB_ORDER_DESC;}
sortorder(A) ::= .              { A = TSDB_ORDER_ASC; }  // Ascending order by default
H
hzcheng 已提交
605 606

//group by clause
H
Haojun Liao 已提交
607 608 609 610
%type groupby_opt {SArray*}
%destructor groupby_opt {taosArrayDestroy($$);}
%type grouplist {SArray*}
%destructor grouplist {taosArrayDestroy($$);}
H
hzcheng 已提交
611

612 613
groupby_opt(A) ::= .                       { A = 0;}
groupby_opt(A) ::= GROUP BY grouplist(X).  { A = X;}
H
hzcheng 已提交
614 615 616 617 618 619 620 621 622 623

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
H
Haojun Liao 已提交
624
%type having_opt {tSqlExpr*}
D
dapan1121 已提交
625
%destructor having_opt {tSqlExprDestroy($$);}
H
hzcheng 已提交
626 627 628 629 630 631
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;}
632
limit_opt(A) ::= LIMIT signed(X).      {A.limit = X;  A.offset = 0;}
H
hzcheng 已提交
633
limit_opt(A) ::= LIMIT signed(X) OFFSET signed(Y).
634
                                       { A.limit = X;  A.offset = Y;}
H
hzcheng 已提交
635
limit_opt(A) ::= LIMIT signed(X) COMMA signed(Y).
636
                                       { A.limit = Y;  A.offset = X;}
H
hzcheng 已提交
637 638 639 640 641 642 643

%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 已提交
644
                                       {A.limit = Y;  A.offset = X;}
H
hzcheng 已提交
645

H
Haojun Liao 已提交
646
%type where_opt {tSqlExpr*}
D
dapan1121 已提交
647
%destructor where_opt {tSqlExprDestroy($$);}
H
hzcheng 已提交
648 649 650 651 652 653

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

/////////////////////////// Expression Processing /////////////////////////////
//
H
Haojun Liao 已提交
654
%type expr {tSqlExpr*}
D
dapan1121 已提交
655
%destructor expr {tSqlExprDestroy($$);}
H
hzcheng 已提交
656

657
expr(A) ::= LP(X) expr(Y) RP(Z).       {A = Y; A->token.z = X.z; A->token.n = (Z.z - X.z + 1);}
H
hzcheng 已提交
658

H
Haojun Liao 已提交
659 660 661 662 663 664 665 666 667 668 669 670 671
expr(A) ::= ID(X).               { A = tSqlExprCreateIdValue(&X, TK_ID);}
expr(A) ::= ID(X) DOT ID(Y).     { X.n += (1+Y.n); A = tSqlExprCreateIdValue(&X, TK_ID);}
expr(A) ::= ID(X) DOT STAR(Y).   { X.n += (1+Y.n); A = tSqlExprCreateIdValue(&X, TK_ALL);}

expr(A) ::= INTEGER(X).          { A = tSqlExprCreateIdValue(&X, TK_INTEGER);}
expr(A) ::= MINUS(X) INTEGER(Y). { X.n += Y.n; X.type = TK_INTEGER; A = tSqlExprCreateIdValue(&X, TK_INTEGER);}
expr(A) ::= PLUS(X)  INTEGER(Y). { X.n += Y.n; X.type = TK_INTEGER; A = tSqlExprCreateIdValue(&X, TK_INTEGER);}
expr(A) ::= FLOAT(X).            { A = tSqlExprCreateIdValue(&X, TK_FLOAT);}
expr(A) ::= MINUS(X) FLOAT(Y).   { X.n += Y.n; X.type = TK_FLOAT; A = tSqlExprCreateIdValue(&X, TK_FLOAT);}
expr(A) ::= PLUS(X) FLOAT(Y).    { X.n += Y.n; X.type = TK_FLOAT; A = tSqlExprCreateIdValue(&X, TK_FLOAT);}
expr(A) ::= STRING(X).           { A = tSqlExprCreateIdValue(&X, TK_STRING);}
expr(A) ::= NOW(X).              { A = tSqlExprCreateIdValue(&X, TK_NOW); }
expr(A) ::= VARIABLE(X).         { A = tSqlExprCreateIdValue(&X, TK_VARIABLE);}
D
dapan1121 已提交
672 673
expr(A) ::= PLUS(X) VARIABLE(Y).   { X.n += Y.n; X.type = TK_VARIABLE; A = tSqlExprCreateIdValue(&X, TK_VARIABLE);}
expr(A) ::= MINUS(X) VARIABLE(Y).  { X.n += Y.n; X.type = TK_VARIABLE; A = tSqlExprCreateIdValue(&X, TK_VARIABLE);}
H
Haojun Liao 已提交
674
expr(A) ::= BOOL(X).             { A = tSqlExprCreateIdValue(&X, TK_BOOL);}
675
expr(A) ::= NULL(X).             { A = tSqlExprCreateIdValue(&X, TK_NULL);}
H
Haojun Liao 已提交
676 677

// ordinary functions: min(x), max(x), top(k, 20)
D
dapan1121 已提交
678
expr(A) ::= ID(X) LP exprlist(Y) RP(E). { A = tSqlExprCreateFunction(Y, &X, &E, X.type); }
H
Haojun Liao 已提交
679 680

// for parsing sql functions with wildcard for parameters. e.g., count(*)/first(*)/last(*) operation
D
dapan1121 已提交
681
expr(A) ::= ID(X) LP STAR RP(Y).     { A = tSqlExprCreateFunction(NULL, &X, &Y, X.type); }
H
Haojun Liao 已提交
682 683

// is (not) null expression
D
dapan1121 已提交
684 685
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);}
H
Haojun Liao 已提交
686 687

// relational expression
D
dapan1121 已提交
688 689 690 691 692 693
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);}
H
Haojun Liao 已提交
694

H
Haojun Liao 已提交
695
expr(A) ::= expr(X) BETWEEN expr(Y) AND expr(Z).      { tSqlExpr* X2 = tSqlExprClone(X); A = tSqlExprCreate(tSqlExprCreate(X, Y, TK_GE), tSqlExprCreate(X2, Z, TK_LE), TK_AND);}
D
dapan1121 已提交
696

D
dapan1121 已提交
697 698
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); }
H
Haojun Liao 已提交
699 700

// binary arithmetic expression
D
dapan1121 已提交
701 702 703 704 705
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
hzcheng 已提交
706

H
Haojun Liao 已提交
707
// like expression
D
dapan1121 已提交
708
expr(A) ::= expr(X) LIKE expr(Y).    {A = tSqlExprCreate(X, Y, TK_LIKE);  }
H
hzcheng 已提交
709 710

//in expression
H
Haojun Liao 已提交
711
expr(A) ::= expr(X) IN LP exprlist(Y) RP.   {A = tSqlExprCreate(X, (tSqlExpr*)Y, TK_IN); }
H
hzcheng 已提交
712

H
Haojun Liao 已提交
713
%type exprlist {SArray*}
D
dapan1121 已提交
714
%destructor exprlist {tSqlExprListDestroy($$);}
H
hzcheng 已提交
715

H
Haojun Liao 已提交
716
%type expritem {tSqlExpr*}
D
dapan1121 已提交
717
%destructor expritem {tSqlExprDestroy($$);}
H
hzcheng 已提交
718

dengyihao's avatar
TD-2571  
dengyihao 已提交
719 720
exprlist(A) ::= exprlist(X) COMMA expritem(Y). {A = tSqlExprListAppend(X,Y,0, 0);}
exprlist(A) ::= expritem(X).                   {A = tSqlExprListAppend(0,X,0, 0);}
H
Haojun Liao 已提交
721 722
expritem(A) ::= expr(X).                       {A = X;}
expritem(A) ::= .                              {A = 0;}
H
hzcheng 已提交
723 724

///////////////////////////////////reset query cache//////////////////////////////////////
H
Haojun Liao 已提交
725
cmd ::= RESET QUERY CACHE.  { setDCLSqlElems(pInfo, TSDB_SQL_RESET_CACHE, 0);}
H
hzcheng 已提交
726

727 728 729
///////////////////////////////////sync replica database//////////////////////////////////
cmd ::= SYNCDB ids(X) REPLICA.{ setDCLSqlElems(pInfo, TSDB_SQL_SYNC_DB_REPLICA, 1, &X);}

H
hzcheng 已提交
730 731 732
///////////////////////////////////ALTER TABLE statement//////////////////////////////////
cmd ::= ALTER TABLE ids(X) cpxName(F) ADD COLUMN columnlist(A).     {
    X.n += F.n;
H
Haojun Liao 已提交
733
    SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, A, NULL, TSDB_ALTER_TABLE_ADD_COLUMN, -1);
D
dapan1121 已提交
734
    setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE);
H
hzcheng 已提交
735 736 737 738 739 740
}

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

    toTSDBType(A.type);
H
Haojun Liao 已提交
741
    SArray* K = tVariantListAppendToken(NULL, &A, -1);
H
hzcheng 已提交
742

H
Haojun Liao 已提交
743
    SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, NULL, K, TSDB_ALTER_TABLE_DROP_COLUMN, -1);
D
dapan1121 已提交
744
    setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE);
H
hzcheng 已提交
745 746 747 748 749
}

//////////////////////////////////ALTER TAGS statement/////////////////////////////////////
cmd ::= ALTER TABLE ids(X) cpxName(Y) ADD TAG columnlist(A).        {
    X.n += Y.n;
H
Haojun Liao 已提交
750
    SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, A, NULL, TSDB_ALTER_TABLE_ADD_TAG_COLUMN, -1);
D
dapan1121 已提交
751
    setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE);
H
hzcheng 已提交
752 753 754 755 756
}
cmd ::= ALTER TABLE ids(X) cpxName(Z) DROP TAG ids(Y).          {
    X.n += Z.n;

    toTSDBType(Y.type);
H
Haojun Liao 已提交
757
    SArray* A = tVariantListAppendToken(NULL, &Y, -1);
H
hzcheng 已提交
758

H
Haojun Liao 已提交
759
    SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, NULL, A, TSDB_ALTER_TABLE_DROP_TAG_COLUMN, -1);
D
dapan1121 已提交
760
    setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE);
H
hzcheng 已提交
761 762 763 764 765 766
}

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

    toTSDBType(Y.type);
H
Haojun Liao 已提交
767
    SArray* A = tVariantListAppendToken(NULL, &Y, -1);
H
hzcheng 已提交
768 769

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

H
Haojun Liao 已提交
772
    SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, NULL, A, TSDB_ALTER_TABLE_CHANGE_TAG_COLUMN, -1);
D
dapan1121 已提交
773
    setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE);
H
hzcheng 已提交
774 775
}

776
cmd ::= ALTER TABLE ids(X) cpxName(F) SET TAG ids(Y) EQ tagitem(Z).     {
H
hzcheng 已提交
777 778 779
    X.n += F.n;

    toTSDBType(Y.type);
H
Haojun Liao 已提交
780
    SArray* A = tVariantListAppendToken(NULL, &Y, -1);
H
hzcheng 已提交
781 782
    A = tVariantListAppend(A, &Z, -1);

H
Haojun Liao 已提交
783
    SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, NULL, A, TSDB_ALTER_TABLE_UPDATE_TAG_VAL, -1);
D
dapan1121 已提交
784 785 786 787 788 789 790
    setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE);
}


///////////////////////////////////ALTER STABLE statement//////////////////////////////////
cmd ::= ALTER STABLE ids(X) cpxName(F) ADD COLUMN columnlist(A).     {
    X.n += F.n;
H
Haojun Liao 已提交
791
    SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, A, NULL, TSDB_ALTER_TABLE_ADD_COLUMN, TSDB_SUPER_TABLE);
D
dapan1121 已提交
792 793 794 795 796 797 798 799 800
    setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE);
}

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

    toTSDBType(A.type);
    SArray* K = tVariantListAppendToken(NULL, &A, -1);

H
Haojun Liao 已提交
801
    SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, NULL, K, TSDB_ALTER_TABLE_DROP_COLUMN, TSDB_SUPER_TABLE);
D
dapan1121 已提交
802 803 804 805 806 807
    setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE);
}

//////////////////////////////////ALTER TAGS statement/////////////////////////////////////
cmd ::= ALTER STABLE ids(X) cpxName(Y) ADD TAG columnlist(A).        {
    X.n += Y.n;
H
Haojun Liao 已提交
808
    SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, A, NULL, TSDB_ALTER_TABLE_ADD_TAG_COLUMN, TSDB_SUPER_TABLE);
D
dapan1121 已提交
809 810 811 812 813 814 815 816
    setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE);
}
cmd ::= ALTER STABLE ids(X) cpxName(Z) DROP TAG ids(Y).          {
    X.n += Z.n;

    toTSDBType(Y.type);
    SArray* A = tVariantListAppendToken(NULL, &Y, -1);

H
Haojun Liao 已提交
817
    SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, NULL, A, TSDB_ALTER_TABLE_DROP_TAG_COLUMN, TSDB_SUPER_TABLE);
D
dapan1121 已提交
818 819 820 821 822 823 824 825 826 827 828 829
    setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE);
}

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

    toTSDBType(Y.type);
    SArray* A = tVariantListAppendToken(NULL, &Y, -1);

    toTSDBType(Z.type);
    A = tVariantListAppendToken(A, &Z, -1);

H
Haojun Liao 已提交
830
    SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, NULL, A, TSDB_ALTER_TABLE_CHANGE_TAG_COLUMN, TSDB_SUPER_TABLE);
D
dapan1121 已提交
831
    setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE);
H
hzcheng 已提交
832 833 834
}

////////////////////////////////////////kill statement///////////////////////////////////////
D
dapan1121 已提交
835 836 837
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 已提交
838 839 840 841

%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
842
  NOW IPTOKEN SEMI NONE PREV LINEAR IMPORT TBNAME JOIN STABLE NULL INSERT INTO VALUES.