tests_config.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. [{'type': 'select_stmt', 'select_cols': [{'type': 'select_all_column', 'target': {'type': 'select_all_column', 'value': 'select_all_column'}}], 'table_names': ['t2'], 'where': {}}]
  279. ),
  280. (
  281. "select c2 as t from t2 where col1>2;",
  282. [
  283. {
  284. "type": "select_stmt",
  285. "select_cols": [
  286. {
  287. "type": "select_column",
  288. "target": {"type": "identifier", "value": "c2"},
  289. "alias": "t",
  290. }
  291. ],
  292. "table_names": ["t2"],
  293. "where": {
  294. "type": "大于",
  295. "left": {"type": "identifier", "value": "col1"},
  296. "right": {"type": "int", "value": 2},
  297. },
  298. }
  299. ],
  300. ),
  301. (
  302. """SELECT Sname FROM Student WHERE Sno IN (1,2) and c in (3, 4, 5);""",
  303. [
  304. {
  305. "type": "select_stmt",
  306. "select_cols": [
  307. {
  308. "type": "select_column",
  309. "target": {"type": "identifier", "value": "Sname"},
  310. }
  311. ],
  312. "table_names": ["Student"],
  313. "where": {
  314. "type": "且",
  315. "left": {
  316. "type": "包含于",
  317. "left": {"type": "identifier", "value": "Sno"},
  318. "right": [
  319. {"type": "int", "value": 1},
  320. {"type": "int", "value": 2},
  321. ],
  322. },
  323. "right": {
  324. "type": "包含于",
  325. "left": {"type": "identifier", "value": "c"},
  326. "right": [
  327. {"type": "int", "value": 3},
  328. {"type": "int", "value": 4},
  329. {"type": "int", "value": 5},
  330. ],
  331. },
  332. },
  333. }
  334. ],
  335. ),
  336. (
  337. """SELECT Student.Sname
  338. FROM Student
  339. WHERE Sno IN (
  340. SELECT Sno
  341. FROM SC
  342. WHERE SC.Cno='81003'
  343. );
  344. """,
  345. [
  346. {
  347. "type": "select_stmt",
  348. "select_cols": [
  349. {
  350. "type": "select_column",
  351. "target": {
  352. "type": "table_field",
  353. "table": "Student",
  354. "field": "Sname",
  355. },
  356. }
  357. ],
  358. "table_names": ["Student"],
  359. "where": {
  360. "type": "包含于",
  361. "left": {"type": "identifier", "value": "Sno"},
  362. "right": {
  363. "type": "select_stmt",
  364. "select_cols": [
  365. {
  366. "type": "select_column",
  367. "target": {"type": "identifier", "value": "Sno"},
  368. }
  369. ],
  370. "table_names": ["SC"],
  371. "where": {
  372. "type": "相等",
  373. "left": {
  374. "type": "table_field",
  375. "table": "SC",
  376. "field": "Cno",
  377. },
  378. "right": {"type": "string", "value": "'81003'"},
  379. },
  380. },
  381. },
  382. }
  383. ],
  384. ),
  385. (
  386. """
  387. select Student.Sname
  388. from Student join SC
  389. on Student.Sno=SC.Sno
  390. where SC.Cno='81003';
  391. """,
  392. [
  393. {
  394. "type": "select_stmt",
  395. "select_cols": [
  396. {
  397. "type": "select_column",
  398. "target": {
  399. "type": "table_field",
  400. "table": "Student",
  401. "field": "Sname",
  402. },
  403. }
  404. ],
  405. "table_names": ["Student"],
  406. "join_options": {
  407. "type": "join_options",
  408. "join_with": {"type": "identifier", "value": "SC"},
  409. "on": {
  410. "type": "相等",
  411. "left": {
  412. "type": "table_field",
  413. "table": "Student",
  414. "field": "Sno",
  415. },
  416. "right": {"type": "table_field", "table": "SC", "field": "Sno"},
  417. },
  418. },
  419. "where": {
  420. "type": "相等",
  421. "left": {"type": "table_field", "table": "SC", "field": "Cno"},
  422. "right": {"type": "string", "value": "'81003'"},
  423. },
  424. }
  425. ],
  426. ),
  427. (
  428. """
  429. select Student.Sname
  430. from Student join SC
  431. on Student.Sno=SC.Sno
  432. and SC.Cno='81003';
  433. """,
  434. [
  435. {
  436. "type": "select_stmt",
  437. "select_cols": [
  438. {
  439. "type": "select_column",
  440. "target": {
  441. "type": "table_field",
  442. "table": "Student",
  443. "field": "Sname",
  444. },
  445. }
  446. ],
  447. "table_names": ["Student"],
  448. "join_options": {
  449. "type": "join_options",
  450. "join_with": {"type": "identifier", "value": "SC"},
  451. "on": {
  452. "type": "且",
  453. "left": {
  454. "type": "相等",
  455. "left": {
  456. "type": "table_field",
  457. "table": "Student",
  458. "field": "Sno",
  459. },
  460. "right": {
  461. "type": "table_field",
  462. "table": "SC",
  463. "field": "Sno",
  464. },
  465. },
  466. "right": {
  467. "type": "相等",
  468. "left": {
  469. "type": "table_field",
  470. "table": "SC",
  471. "field": "Cno",
  472. },
  473. "right": {"type": "string", "value": "'81003'"},
  474. },
  475. },
  476. },
  477. "where": {},
  478. }
  479. ],
  480. ),
  481. ("drop table t1;", [{"type": "drop_stmt", "table_name": "t1"}]),
  482. (
  483. "select * from tb1, tb2;",
  484. [
  485. {
  486. "type": "select_stmt",
  487. "select_cols": [{'type': 'select_all_column', 'target': {'type': 'select_all_column', 'value': 'select_all_column'}}],
  488. "table_names": ["tb1", "tb2"],
  489. "where": {},
  490. }
  491. ],
  492. ),
  493. ]
  494. sql_checker_tests = [
  495. ("drop table person;", True),
  496. ("create table person(name string, age int, classId int);", True),
  497. ("select age from person;", True),
  498. ("select * from person;", True),
  499. ("select gender from person;", 'column `gender` not exists in `person`'),
  500. ("select 123 from person;", True),
  501. ("drop table class;", True),
  502. ("create table class (id int, grade int, faculty string);", True),
  503. ("select * from class where grade = 2 and faculty = 'Computer Science';", True),
  504. (
  505. "select * from class where grade = 2 and count=33;",
  506. 'column `count` not exists in `class`',
  507. ),
  508. ("select age, class.grade from class, person;", True),
  509. ("select age, person.grade from class, person;", 'column `person.grade` not exists in `class, person`'),
  510. ]