sql_parser.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. "op_type": "bin_cmp_op",
  148. },
  149. "right": {
  150. "type": "相等",
  151. "left": {"type": "identifier", "value": "col2"},
  152. "right": {"type": "int", "value": 4},
  153. "op_type": "bin_cmp_op",
  154. },
  155. "op_type": "bin_logical_op",
  156. },
  157. }
  158. ],
  159. ),
  160. (
  161. "update tb1 set col1=3, col4=4 where not not not col1=2 and col2=4 or col3=col2;",
  162. [
  163. {
  164. "type": "update_stmt",
  165. "table_name": "tb1",
  166. "set": [
  167. {
  168. "type": "assign_const",
  169. "left": {"type": "identifier", "value": "col1"},
  170. "right": {"type": "int", "value": 3},
  171. },
  172. {
  173. "type": "assign_const",
  174. "left": {"type": "identifier", "value": "col4"},
  175. "right": {"type": "int", "value": 4},
  176. },
  177. ],
  178. "where": {
  179. "type": "或",
  180. "left": {
  181. "type": "且",
  182. "left": {
  183. "type": "非",
  184. "left": {
  185. "type": "非",
  186. "left": {
  187. "type": "非",
  188. "left": {
  189. "type": "相等",
  190. "left": {"type": "identifier", "value": "col1"},
  191. "right": {"type": "int", "value": 2},
  192. "op_type": "bin_cmp_op",
  193. },
  194. },
  195. },
  196. },
  197. "right": {
  198. "type": "相等",
  199. "left": {"type": "identifier", "value": "col2"},
  200. "right": {"type": "int", "value": 4},
  201. "op_type": "bin_cmp_op",
  202. },
  203. "op_type": "bin_logical_op",
  204. },
  205. "right": {
  206. "type": "相等",
  207. "left": {"type": "identifier", "value": "col3"},
  208. "right": {"type": "identifier", "value": "col2"},
  209. "op_type": "bin_cmp_op",
  210. },
  211. "op_type": "bin_logical_op",
  212. },
  213. }
  214. ],
  215. ),
  216. (
  217. "delete from tb1 where c1 = 1 and c2= 3 or c3=3;",
  218. [
  219. {
  220. "type": "delete_stmt",
  221. "table_name": "tb1",
  222. "where": {
  223. "type": "或",
  224. "left": {
  225. "type": "且",
  226. "left": {
  227. "type": "相等",
  228. "left": {"type": "identifier", "value": "c1"},
  229. "right": {"type": "int", "value": 1},
  230. "op_type": "bin_cmp_op",
  231. },
  232. "right": {
  233. "type": "相等",
  234. "left": {"type": "identifier", "value": "c2"},
  235. "right": {"type": "int", "value": 3},
  236. "op_type": "bin_cmp_op",
  237. },
  238. "op_type": "bin_logical_op",
  239. },
  240. "right": {
  241. "type": "相等",
  242. "left": {"type": "identifier", "value": "c3"},
  243. "right": {"type": "int", "value": 3},
  244. "op_type": "bin_cmp_op",
  245. },
  246. "op_type": "bin_logical_op",
  247. },
  248. }
  249. ],
  250. ),
  251. (
  252. "delete from tb1 where c1 = 1 and (c2= 3 or c3=3) or (c4='asd');",
  253. [
  254. {
  255. "type": "delete_stmt",
  256. "table_name": "tb1",
  257. "where": {
  258. "type": "或",
  259. "left": {
  260. "type": "且",
  261. "left": {
  262. "type": "相等",
  263. "left": {"type": "identifier", "value": "c1"},
  264. "right": {"type": "int", "value": 1},
  265. "op_type": "bin_cmp_op",
  266. },
  267. "right": {
  268. "type": "或",
  269. "left": {
  270. "type": "相等",
  271. "left": {"type": "identifier", "value": "c2"},
  272. "right": {"type": "int", "value": 3},
  273. "op_type": "bin_cmp_op",
  274. },
  275. "right": {
  276. "type": "相等",
  277. "left": {"type": "identifier", "value": "c3"},
  278. "right": {"type": "int", "value": 3},
  279. "op_type": "bin_cmp_op",
  280. },
  281. "op_type": "bin_logical_op",
  282. },
  283. "op_type": "bin_logical_op",
  284. },
  285. "right": {
  286. "type": "相等",
  287. "left": {"type": "identifier", "value": "c4"},
  288. "right": {"type": "string", "value": "'asd'"},
  289. "op_type": "bin_cmp_op",
  290. },
  291. "op_type": "bin_logical_op",
  292. },
  293. }
  294. ],
  295. ),
  296. (
  297. "select * from t2;",
  298. [
  299. {
  300. "type": "select_stmt",
  301. "select_cols": [
  302. {
  303. "type": "select_all_column",
  304. "target": {
  305. "type": "select_all_column",
  306. "value": "select_all_column",
  307. },
  308. }
  309. ],
  310. "table_names": ["t2"],
  311. "where": {},
  312. }
  313. ],
  314. ),
  315. (
  316. "select c2 as t from t2 where col1>2;",
  317. [
  318. {
  319. "type": "select_stmt",
  320. "select_cols": [
  321. {
  322. "type": "select_column",
  323. "target": {"type": "identifier", "value": "c2"},
  324. "alias": "t",
  325. }
  326. ],
  327. "table_names": ["t2"],
  328. "where": {
  329. "type": "大于",
  330. "left": {"type": "identifier", "value": "col1"},
  331. "right": {"type": "int", "value": 2},
  332. "op_type": "bin_cmp_op",
  333. },
  334. }
  335. ],
  336. ),
  337. (
  338. """SELECT Sname FROM Student WHERE Sno IN (1,2) and c in (3, 4, 5);""",
  339. [
  340. {
  341. "type": "select_stmt",
  342. "select_cols": [
  343. {
  344. "type": "select_column",
  345. "target": {"type": "identifier", "value": "Sname"},
  346. }
  347. ],
  348. "table_names": ["Student"],
  349. "where": {
  350. "type": "且",
  351. "left": {
  352. "type": "包含于",
  353. "left": {"type": "identifier", "value": "Sno"},
  354. "right": [
  355. {"type": "int", "value": 1},
  356. {"type": "int", "value": 2},
  357. ],
  358. "op_type": "bin_contains_op",
  359. },
  360. "right": {
  361. "type": "包含于",
  362. "left": {"type": "identifier", "value": "c"},
  363. "right": [
  364. {"type": "int", "value": 3},
  365. {"type": "int", "value": 4},
  366. {"type": "int", "value": 5},
  367. ],
  368. "op_type": "bin_contains_op",
  369. },
  370. "op_type": "bin_logical_op",
  371. },
  372. }
  373. ],
  374. ),
  375. (
  376. """SELECT Student.Sname
  377. FROM Student
  378. WHERE Sno IN (
  379. SELECT Sno
  380. FROM SC
  381. WHERE SC.Cno='81003'
  382. );
  383. """,
  384. [
  385. {
  386. "type": "select_stmt",
  387. "select_cols": [
  388. {
  389. "type": "select_column",
  390. "target": {
  391. "type": "table_field",
  392. "table": "Student",
  393. "field": "Sname",
  394. },
  395. }
  396. ],
  397. "table_names": ["Student"],
  398. "where": {
  399. "type": "包含于",
  400. "left": {"type": "identifier", "value": "Sno"},
  401. "right": {
  402. "type": "select_stmt",
  403. "select_cols": [
  404. {
  405. "type": "select_column",
  406. "target": {"type": "identifier", "value": "Sno"},
  407. }
  408. ],
  409. "table_names": ["SC"],
  410. "where": {
  411. "type": "相等",
  412. "left": {
  413. "type": "table_field",
  414. "table": "SC",
  415. "field": "Cno",
  416. },
  417. "right": {"type": "string", "value": "'81003'"},
  418. "op_type": "bin_cmp_op",
  419. },
  420. },
  421. "op_type": "bin_contains_op",
  422. },
  423. }
  424. ],
  425. ),
  426. (
  427. """
  428. select Student.Sname
  429. from Student join SC
  430. on Student.Sno=SC.Sno
  431. where SC.Cno='81003';
  432. """,
  433. [
  434. {
  435. "type": "select_stmt",
  436. "select_cols": [
  437. {
  438. "type": "select_column",
  439. "target": {
  440. "type": "table_field",
  441. "table": "Student",
  442. "field": "Sname",
  443. },
  444. }
  445. ],
  446. "table_names": ["Student"],
  447. "join_options": {
  448. "type": "join_options",
  449. "join_with": {"type": "identifier", "value": "SC"},
  450. "on": {
  451. "type": "相等",
  452. "left": {
  453. "type": "table_field",
  454. "table": "Student",
  455. "field": "Sno",
  456. },
  457. "right": {"type": "table_field", "table": "SC", "field": "Sno"},
  458. "op_type": "bin_cmp_op",
  459. },
  460. },
  461. "where": {
  462. "type": "相等",
  463. "left": {"type": "table_field", "table": "SC", "field": "Cno"},
  464. "right": {"type": "string", "value": "'81003'"},
  465. "op_type": "bin_cmp_op",
  466. },
  467. }
  468. ],
  469. ),
  470. (
  471. """
  472. select Student.Sname
  473. from Student join SC
  474. on Student.Sno=SC.Sno
  475. and SC.Cno='81003';
  476. """,
  477. [
  478. {
  479. "type": "select_stmt",
  480. "select_cols": [
  481. {
  482. "type": "select_column",
  483. "target": {
  484. "type": "table_field",
  485. "table": "Student",
  486. "field": "Sname",
  487. },
  488. }
  489. ],
  490. "table_names": ["Student"],
  491. "join_options": {
  492. "type": "join_options",
  493. "join_with": {"type": "identifier", "value": "SC"},
  494. "on": {
  495. "type": "且",
  496. "left": {
  497. "type": "相等",
  498. "left": {
  499. "type": "table_field",
  500. "table": "Student",
  501. "field": "Sno",
  502. },
  503. "right": {
  504. "type": "table_field",
  505. "table": "SC",
  506. "field": "Sno",
  507. },
  508. "op_type": "bin_cmp_op",
  509. },
  510. "right": {
  511. "type": "相等",
  512. "left": {
  513. "type": "table_field",
  514. "table": "SC",
  515. "field": "Cno",
  516. },
  517. "right": {"type": "string", "value": "'81003'"},
  518. "op_type": "bin_cmp_op",
  519. },
  520. "op_type": "bin_logical_op",
  521. },
  522. },
  523. "where": {},
  524. }
  525. ],
  526. ),
  527. ("drop table t1;", [{"type": "drop_stmt", "table_name": "t1"}]),
  528. (
  529. "select * from tb1, tb2;",
  530. [
  531. {
  532. "type": "select_stmt",
  533. "select_cols": [
  534. {
  535. "type": "select_all_column",
  536. "target": {
  537. "type": "select_all_column",
  538. "value": "select_all_column",
  539. },
  540. }
  541. ],
  542. "table_names": ["tb1", "tb2"],
  543. "where": {},
  544. }
  545. ],
  546. ),
  547. ]