tests_config.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. sql_parser_tests = [
  2. ("create table asd;", [{"type": "create_stmt", "table_name": "asd", "cols": []}]),
  3. (
  4. "create table tb (col1 INT, col2 string, col3 FLOAT);",
  5. [
  6. {
  7. "type": "create_stmt",
  8. "table_name": "tb",
  9. "cols": [
  10. {
  11. "type": "create_column",
  12. "column_name": "col1",
  13. "data_type": "INT",
  14. "primary_key": False,
  15. },
  16. {
  17. "type": "create_column",
  18. "column_name": "col2",
  19. "data_type": "string",
  20. "primary_key": False,
  21. },
  22. {
  23. "type": "create_column",
  24. "column_name": "col3",
  25. "data_type": "FLOAT",
  26. "primary_key": False,
  27. },
  28. ],
  29. }
  30. ],
  31. ),
  32. (
  33. """
  34. create table tb1 (
  35. col1 int primary key,
  36. col2 FLOAT
  37. );
  38. """,
  39. [
  40. {
  41. "type": "create_stmt",
  42. "table_name": "tb1",
  43. "cols": [
  44. {
  45. "type": "create_column",
  46. "column_name": "col1",
  47. "data_type": "int",
  48. "primary_key": True,
  49. },
  50. {
  51. "type": "create_column",
  52. "column_name": "col2",
  53. "data_type": "FLOAT",
  54. "primary_key": False,
  55. },
  56. ],
  57. }
  58. ],
  59. ),
  60. (
  61. """
  62. create table tb2 (
  63. x float,
  64. y int,
  65. z int
  66. );
  67. """,
  68. [
  69. {
  70. "type": "create_stmt",
  71. "table_name": "tb2",
  72. "cols": [
  73. {
  74. "type": "create_column",
  75. "column_name": "x",
  76. "data_type": "float",
  77. "primary_key": False,
  78. },
  79. {
  80. "type": "create_column",
  81. "column_name": "y",
  82. "data_type": "int",
  83. "primary_key": False,
  84. },
  85. {
  86. "type": "create_column",
  87. "column_name": "z",
  88. "data_type": "int",
  89. "primary_key": False,
  90. },
  91. ],
  92. }
  93. ],
  94. ),
  95. (
  96. """insert into tb1 values (1, 'foo');""",
  97. [
  98. {
  99. "type": "insert_stmt",
  100. "table_name": "tb1",
  101. "values": [
  102. {"type": "int", "value": 1},
  103. {"type": "string", "value": "'foo'"},
  104. ],
  105. }
  106. ],
  107. ),
  108. (
  109. """insert into tb1 values (2, 'foo', 'zxc', 1234.234);""",
  110. [
  111. {
  112. "type": "insert_stmt",
  113. "table_name": "tb1",
  114. "values": [
  115. {"type": "int", "value": 2},
  116. {"type": "string", "value": "'foo'"},
  117. {"type": "string", "value": "'zxc'"},
  118. {"type": "float", "value": 1234.234},
  119. ],
  120. }
  121. ],
  122. ),
  123. (
  124. "update tb1 set col1=3, col4=4 where col1=2 and col2=4;",
  125. [
  126. {
  127. "type": "update_stmt",
  128. "table_name": "tb1",
  129. "set": [
  130. {
  131. "type": "assign_const",
  132. "left": {"type": "identifier", "value": "col1"},
  133. "right": {"type": "int", "value": 3},
  134. },
  135. {
  136. "type": "assign_const",
  137. "left": {"type": "identifier", "value": "col4"},
  138. "right": {"type": "int", "value": 4},
  139. },
  140. ],
  141. "where": {
  142. "type": "且",
  143. "left": {
  144. "type": "相等",
  145. "left": {"type": "identifier", "value": "col1"},
  146. "right": {"type": "int", "value": 2},
  147. },
  148. "right": {
  149. "type": "相等",
  150. "left": {"type": "identifier", "value": "col2"},
  151. "right": {"type": "int", "value": 4},
  152. },
  153. },
  154. }
  155. ],
  156. ),
  157. (
  158. "update tb1 set col1=3, col4=4 where not not not col1=2 and col2=4 or col3=col2;",
  159. [
  160. {
  161. "type": "update_stmt",
  162. "table_name": "tb1",
  163. "set": [
  164. {
  165. "type": "assign_const",
  166. "left": {"type": "identifier", "value": "col1"},
  167. "right": {"type": "int", "value": 3},
  168. },
  169. {
  170. "type": "assign_const",
  171. "left": {"type": "identifier", "value": "col4"},
  172. "right": {"type": "int", "value": 4},
  173. },
  174. ],
  175. "where": {
  176. "type": "或",
  177. "left": {
  178. "type": "且",
  179. "left": {
  180. "type": "非",
  181. "left": {
  182. "type": "非",
  183. "left": {
  184. "type": "非",
  185. "left": {
  186. "type": "相等",
  187. "left": {"type": "identifier", "value": "col1"},
  188. "right": {"type": "int", "value": 2},
  189. },
  190. },
  191. },
  192. },
  193. "right": {
  194. "type": "相等",
  195. "left": {"type": "identifier", "value": "col2"},
  196. "right": {"type": "int", "value": 4},
  197. },
  198. },
  199. "right": {
  200. "type": "相等",
  201. "left": {"type": "identifier", "value": "col3"},
  202. "right": {"type": "identifier", "value": "col2"},
  203. },
  204. },
  205. }
  206. ],
  207. ),
  208. (
  209. "delete from tb1 where c1 = 1 and c2= 3 or c3=3;",
  210. [
  211. {
  212. "type": "delete_stmt",
  213. "table_name": "tb1",
  214. "where": {
  215. "type": "或",
  216. "left": {
  217. "left": {
  218. "type": "相等",
  219. "left": {"type": "identifier", "value": "c1"},
  220. "right": {"type": "int", "value": 1},
  221. },
  222. "type": "且",
  223. "right": {
  224. "type": "相等",
  225. "left": {"type": "identifier", "value": "c2"},
  226. "right": {"type": "int", "value": 3},
  227. },
  228. },
  229. "right": {
  230. "type": "相等",
  231. "left": {"type": "identifier", "value": "c3"},
  232. "right": {"type": "int", "value": 3},
  233. },
  234. },
  235. }
  236. ],
  237. ),
  238. (
  239. "delete from tb1 where c1 = 1 and (c2= 3 or c3=3) or (c4='asd');",
  240. [
  241. {
  242. "type": "delete_stmt",
  243. "table_name": "tb1",
  244. "where": {
  245. "type": "或",
  246. "left": {
  247. "type": "且",
  248. "left": {
  249. "type": "相等",
  250. "left": {"type": "identifier", "value": "c1"},
  251. "right": {"type": "int", "value": 1},
  252. },
  253. "right": {
  254. "type": "或",
  255. "left": {
  256. "type": "相等",
  257. "left": {"type": "identifier", "value": "c2"},
  258. "right": {"type": "int", "value": 3},
  259. },
  260. "right": {
  261. "type": "相等",
  262. "left": {"type": "identifier", "value": "c3"},
  263. "right": {"type": "int", "value": 3},
  264. },
  265. },
  266. },
  267. "right": {
  268. "type": "相等",
  269. "left": {"type": "identifier", "value": "c4"},
  270. "right": {"type": "string", "value": "'asd'"},
  271. },
  272. },
  273. }
  274. ],
  275. ),
  276. (
  277. "select * from t2;",
  278. [
  279. {
  280. "type": "select_stmt",
  281. "select_cols": [{"type": "select_all_column"}],
  282. "table_names": ["t2"],
  283. "where": {},
  284. }
  285. ],
  286. ),
  287. (
  288. "select c2 as t from t2 where col1>2;",
  289. [
  290. {
  291. "type": "select_stmt",
  292. "select_cols": [
  293. {
  294. "type": "select_column",
  295. "target": {"type": "identifier", "value": "c2"},
  296. "alias": "t",
  297. }
  298. ],
  299. "table_names": ["t2"],
  300. "where": {
  301. "type": "大于",
  302. "left": {"type": "identifier", "value": "col1"},
  303. "right": {"type": "int", "value": 2},
  304. },
  305. }
  306. ],
  307. ),
  308. (
  309. """SELECT Sname FROM Student WHERE Sno IN (1,2) and c in (3, 4, 5);""",
  310. [
  311. {
  312. "type": "select_stmt",
  313. "select_cols": [
  314. {
  315. "type": "select_column",
  316. "target": {"type": "identifier", "value": "Sname"},
  317. }
  318. ],
  319. "table_names": ["Student"],
  320. "where": {
  321. "type": "且",
  322. "left": {
  323. "type": "包含于",
  324. "left": {"type": "identifier", "value": "Sno"},
  325. "right": [
  326. {"type": "int", "value": 1},
  327. {"type": "int", "value": 2},
  328. ],
  329. },
  330. "right": {
  331. "type": "包含于",
  332. "left": {"type": "identifier", "value": "c"},
  333. "right": [
  334. {"type": "int", "value": 3},
  335. {"type": "int", "value": 4},
  336. {"type": "int", "value": 5},
  337. ],
  338. },
  339. },
  340. }
  341. ],
  342. ),
  343. (
  344. """SELECT Student.Sname
  345. FROM Student
  346. WHERE Sno IN (
  347. SELECT Sno
  348. FROM SC
  349. WHERE SC.Cno='81003'
  350. );
  351. """,
  352. [
  353. {
  354. "type": "select_stmt",
  355. "select_cols": [
  356. {
  357. "type": "select_column",
  358. "target": {
  359. "type": "table_field",
  360. "table": "Student",
  361. "field": "Sname",
  362. },
  363. }
  364. ],
  365. "table_names": ["Student"],
  366. "where": {
  367. "type": "包含于",
  368. "left": {"type": "identifier", "value": "Sno"},
  369. "right": {
  370. "type": "select_stmt",
  371. "select_cols": [
  372. {
  373. "type": "select_column",
  374. "target": {"type": "identifier", "value": "Sno"},
  375. }
  376. ],
  377. "table_names": ["SC"],
  378. "where": {
  379. "type": "相等",
  380. "left": {
  381. "type": "table_field",
  382. "table": "SC",
  383. "field": "Cno",
  384. },
  385. "right": {"type": "string", "value": "'81003'"},
  386. },
  387. },
  388. },
  389. }
  390. ],
  391. ),
  392. (
  393. """
  394. select Student.Sname
  395. from Student join SC
  396. on Student.Sno=SC.Sno
  397. where SC.Cno='81003';
  398. """,
  399. [
  400. {
  401. "type": "select_stmt",
  402. "select_cols": [
  403. {
  404. "type": "select_column",
  405. "target": {
  406. "type": "table_field",
  407. "table": "Student",
  408. "field": "Sname",
  409. },
  410. }
  411. ],
  412. "table_names": ["Student"],
  413. "join_options": {
  414. "type": "join_options",
  415. "join_with": {"type": "identifier", "value": "SC"},
  416. "on": {
  417. "type": "相等",
  418. "left": {
  419. "type": "table_field",
  420. "table": "Student",
  421. "field": "Sno",
  422. },
  423. "right": {"type": "table_field", "table": "SC", "field": "Sno"},
  424. },
  425. },
  426. "where": {
  427. "type": "相等",
  428. "left": {"type": "table_field", "table": "SC", "field": "Cno"},
  429. "right": {"type": "string", "value": "'81003'"},
  430. },
  431. }
  432. ],
  433. ),
  434. (
  435. """
  436. select Student.Sname
  437. from Student join SC
  438. on Student.Sno=SC.Sno
  439. and SC.Cno='81003';
  440. """,
  441. [
  442. {
  443. "type": "select_stmt",
  444. "select_cols": [
  445. {
  446. "type": "select_column",
  447. "target": {
  448. "type": "table_field",
  449. "table": "Student",
  450. "field": "Sname",
  451. },
  452. }
  453. ],
  454. "table_names": ["Student"],
  455. "join_options": {
  456. "type": "join_options",
  457. "join_with": {"type": "identifier", "value": "SC"},
  458. "on": {
  459. "type": "且",
  460. "left": {
  461. "type": "相等",
  462. "left": {
  463. "type": "table_field",
  464. "table": "Student",
  465. "field": "Sno",
  466. },
  467. "right": {
  468. "type": "table_field",
  469. "table": "SC",
  470. "field": "Sno",
  471. },
  472. },
  473. "right": {
  474. "type": "相等",
  475. "left": {
  476. "type": "table_field",
  477. "table": "SC",
  478. "field": "Cno",
  479. },
  480. "right": {"type": "string", "value": "'81003'"},
  481. },
  482. },
  483. },
  484. "where": {},
  485. }
  486. ],
  487. ),
  488. ('drop table t1;', [{"type": "drop_stmt", "table_name": "t1"}]),
  489. ]
  490. sql_checker_tests = [
  491. ('drop table person;', True),
  492. ('create table person(name string, age int, classId int);', True),
  493. ('select age from person;', True),
  494. ('select * from person;', True),
  495. ('select gender from person;', 'column `"gender"` not exists in `person`'),
  496. ('select 123 from person;', True),
  497. ('drop table class;', True),
  498. ('create table class (id int, grade int, faculty string);', True),
  499. ('select * from class where grade = 2 and faculty = \'Computer Science\';', True),
  500. ('select * from class where grade = 2 and count=33;', 'column `"count"` not exists in `class`'),
  501. ]