parser_test.go 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752
  1. package parser
  2. import (
  3. "testing"
  4. "github.com/danfragoso/pizzasql-next/pkg/lexer"
  5. )
  6. func parse(t *testing.T, input string) Statement {
  7. t.Helper()
  8. l := lexer.New(input)
  9. p := New(l)
  10. stmt, err := p.Parse()
  11. if err != nil {
  12. t.Fatalf("parse error: %v", err)
  13. }
  14. return stmt
  15. }
  16. func parseExpr(t *testing.T, input string) Expr {
  17. t.Helper()
  18. // Wrap in SELECT to parse as expression
  19. l := lexer.New("SELECT " + input)
  20. p := New(l)
  21. stmt, err := p.Parse()
  22. if err != nil {
  23. t.Fatalf("parse error: %v", err)
  24. }
  25. sel := stmt.(*SelectStmt)
  26. return sel.Columns[0].Expr
  27. }
  28. // SELECT statement tests
  29. func TestParseSelectStar(t *testing.T) {
  30. stmt := parse(t, "SELECT * FROM users")
  31. sel, ok := stmt.(*SelectStmt)
  32. if !ok {
  33. t.Fatalf("expected SelectStmt, got %T", stmt)
  34. }
  35. if len(sel.Columns) != 1 || !sel.Columns[0].Star {
  36. t.Error("expected SELECT *")
  37. }
  38. if len(sel.From) != 1 || sel.From[0].Name != "users" {
  39. t.Error("expected FROM users")
  40. }
  41. }
  42. func TestParseSelectColumns(t *testing.T) {
  43. stmt := parse(t, "SELECT id, name, email FROM users")
  44. sel := stmt.(*SelectStmt)
  45. if len(sel.Columns) != 3 {
  46. t.Fatalf("expected 3 columns, got %d", len(sel.Columns))
  47. }
  48. cols := []string{"id", "name", "email"}
  49. for i, col := range sel.Columns {
  50. ref, ok := col.Expr.(*ColumnRef)
  51. if !ok {
  52. t.Errorf("column %d: expected ColumnRef", i)
  53. continue
  54. }
  55. if ref.Column != cols[i] {
  56. t.Errorf("column %d: expected %s, got %s", i, cols[i], ref.Column)
  57. }
  58. }
  59. }
  60. func TestParseSelectQualifiedWildcard(t *testing.T) {
  61. stmt := parse(t, "SELECT DISTINCT repo.* FROM repository AS repo LEFT JOIN access ON access.repo_id = repo.id")
  62. sel := stmt.(*SelectStmt)
  63. if !sel.Distinct {
  64. t.Error("expected DISTINCT")
  65. }
  66. if len(sel.Columns) != 1 {
  67. t.Fatalf("expected 1 column, got %d", len(sel.Columns))
  68. }
  69. col := sel.Columns[0]
  70. if col.TableStar != "repo" {
  71. t.Errorf("expected TableStar=repo, got %q", col.TableStar)
  72. }
  73. if col.Star || col.Expr != nil || col.Alias != "" {
  74. t.Errorf("qualified wildcard should not set Star/Expr/Alias: %+v", col)
  75. }
  76. }
  77. func TestParseSelectQualifiedWildcardMixed(t *testing.T) {
  78. stmt := parse(t, "SELECT repo.*, access.mode FROM repository AS repo LEFT JOIN access ON access.repo_id = repo.id")
  79. sel := stmt.(*SelectStmt)
  80. if len(sel.Columns) != 2 {
  81. t.Fatalf("expected 2 columns, got %d", len(sel.Columns))
  82. }
  83. if sel.Columns[0].TableStar != "repo" {
  84. t.Errorf("column 0 TableStar = %q, want repo", sel.Columns[0].TableStar)
  85. }
  86. ref, ok := sel.Columns[1].Expr.(*ColumnRef)
  87. if !ok || ref.Table != "access" || ref.Column != "mode" {
  88. t.Errorf("column 1 = %+v, want access.mode ColumnRef", sel.Columns[1].Expr)
  89. }
  90. }
  91. func TestParseSelectCountStarStillWorks(t *testing.T) {
  92. stmt := parse(t, "SELECT COUNT(*) FROM users")
  93. sel := stmt.(*SelectStmt)
  94. if len(sel.Columns) != 1 {
  95. t.Fatalf("expected 1 column, got %d", len(sel.Columns))
  96. }
  97. col := sel.Columns[0]
  98. if col.TableStar != "" || col.Star {
  99. t.Errorf("COUNT(*) should not be a wildcard: %+v", col)
  100. }
  101. fn, ok := col.Expr.(*FunctionCall)
  102. if !ok || !fn.Star || fn.Name != "COUNT" {
  103. t.Errorf("expected COUNT(*) FunctionCall, got %+v", col.Expr)
  104. }
  105. }
  106. func TestParseSelectWithAlias(t *testing.T) {
  107. stmt := parse(t, "SELECT id AS user_id, name AS full_name FROM users u")
  108. sel := stmt.(*SelectStmt)
  109. if sel.Columns[0].Alias != "user_id" {
  110. t.Errorf("expected alias user_id, got %s", sel.Columns[0].Alias)
  111. }
  112. if sel.Columns[1].Alias != "full_name" {
  113. t.Errorf("expected alias full_name, got %s", sel.Columns[1].Alias)
  114. }
  115. if sel.From[0].Alias != "u" {
  116. t.Errorf("expected table alias u, got %s", sel.From[0].Alias)
  117. }
  118. }
  119. func TestParseSelectDistinct(t *testing.T) {
  120. stmt := parse(t, "SELECT DISTINCT name FROM users")
  121. sel := stmt.(*SelectStmt)
  122. if !sel.Distinct {
  123. t.Error("expected DISTINCT")
  124. }
  125. }
  126. func TestParseSelectWhere(t *testing.T) {
  127. stmt := parse(t, "SELECT * FROM users WHERE id = 1")
  128. sel := stmt.(*SelectStmt)
  129. if sel.Where == nil {
  130. t.Fatal("expected WHERE clause")
  131. }
  132. binary, ok := sel.Where.(*BinaryExpr)
  133. if !ok {
  134. t.Fatalf("expected BinaryExpr, got %T", sel.Where)
  135. }
  136. if binary.Op != lexer.TokenEq {
  137. t.Errorf("expected =, got %v", binary.Op)
  138. }
  139. }
  140. func TestParseSelectWhereComplex(t *testing.T) {
  141. stmt := parse(t, "SELECT * FROM users WHERE id = 1 AND name = 'John' OR active = TRUE")
  142. sel := stmt.(*SelectStmt)
  143. if sel.Where == nil {
  144. t.Fatal("expected WHERE clause")
  145. }
  146. // Should be: (id = 1 AND name = 'John') OR active = TRUE
  147. or, ok := sel.Where.(*BinaryExpr)
  148. if !ok || or.Op != lexer.TokenOR {
  149. t.Fatal("expected OR at top level")
  150. }
  151. }
  152. func TestParseSelectOrderBy(t *testing.T) {
  153. stmt := parse(t, "SELECT * FROM users ORDER BY name ASC, id DESC")
  154. sel := stmt.(*SelectStmt)
  155. if len(sel.OrderBy) != 2 {
  156. t.Fatalf("expected 2 ORDER BY items, got %d", len(sel.OrderBy))
  157. }
  158. if sel.OrderBy[0].Desc {
  159. t.Error("first item should be ASC")
  160. }
  161. if !sel.OrderBy[1].Desc {
  162. t.Error("second item should be DESC")
  163. }
  164. }
  165. func TestParseSelectLimitOffset(t *testing.T) {
  166. stmt := parse(t, "SELECT * FROM users LIMIT 10 OFFSET 20")
  167. sel := stmt.(*SelectStmt)
  168. if sel.Limit == nil {
  169. t.Error("expected LIMIT")
  170. }
  171. if sel.Offset == nil {
  172. t.Error("expected OFFSET")
  173. }
  174. limit := sel.Limit.(*LiteralExpr)
  175. if limit.Value != "10" {
  176. t.Errorf("expected LIMIT 10, got %s", limit.Value)
  177. }
  178. offset := sel.Offset.(*LiteralExpr)
  179. if offset.Value != "20" {
  180. t.Errorf("expected OFFSET 20, got %s", offset.Value)
  181. }
  182. }
  183. func TestParseSelectGroupBy(t *testing.T) {
  184. stmt := parse(t, "SELECT name, COUNT(*) FROM users GROUP BY name")
  185. sel := stmt.(*SelectStmt)
  186. if len(sel.GroupBy) != 1 {
  187. t.Fatalf("expected 1 GROUP BY column, got %d", len(sel.GroupBy))
  188. }
  189. }
  190. func TestParseSelectHaving(t *testing.T) {
  191. stmt := parse(t, "SELECT name, COUNT(*) as cnt FROM users GROUP BY name HAVING COUNT(*) > 5")
  192. sel := stmt.(*SelectStmt)
  193. if sel.Having == nil {
  194. t.Fatal("expected HAVING clause")
  195. }
  196. }
  197. func TestParseSelectJoin(t *testing.T) {
  198. tests := []struct {
  199. input string
  200. joinType JoinType
  201. }{
  202. {"SELECT * FROM a JOIN b ON a.id = b.id", JoinInner},
  203. {"SELECT * FROM a INNER JOIN b ON a.id = b.id", JoinInner},
  204. {"SELECT * FROM a LEFT JOIN b ON a.id = b.id", JoinLeft},
  205. {"SELECT * FROM a LEFT OUTER JOIN b ON a.id = b.id", JoinLeft},
  206. {"SELECT * FROM a RIGHT JOIN b ON a.id = b.id", JoinRight},
  207. {"SELECT * FROM a CROSS JOIN b", JoinCross},
  208. }
  209. for _, tt := range tests {
  210. t.Run(tt.input, func(t *testing.T) {
  211. stmt := parse(t, tt.input)
  212. sel := stmt.(*SelectStmt)
  213. if sel.From[0].Join == nil {
  214. t.Fatal("expected JOIN")
  215. }
  216. if sel.From[0].Join.Type != tt.joinType {
  217. t.Errorf("expected join type %v, got %v", tt.joinType, sel.From[0].Join.Type)
  218. }
  219. })
  220. }
  221. }
  222. // INSERT statement tests
  223. func TestParseInsertValues(t *testing.T) {
  224. stmt := parse(t, "INSERT INTO users (name, age) VALUES ('John', 30)")
  225. ins, ok := stmt.(*InsertStmt)
  226. if !ok {
  227. t.Fatalf("expected InsertStmt, got %T", stmt)
  228. }
  229. if ins.Table.Name != "users" {
  230. t.Errorf("expected table users, got %s", ins.Table.Name)
  231. }
  232. if len(ins.Columns) != 2 {
  233. t.Fatalf("expected 2 columns, got %d", len(ins.Columns))
  234. }
  235. if len(ins.Values) != 1 || len(ins.Values[0]) != 2 {
  236. t.Error("expected 1 row with 2 values")
  237. }
  238. }
  239. func TestParseJoinUsingMultipleColumns(t *testing.T) {
  240. stmt := parse(t, "SELECT * FROM hit_counts JOIN paths USING (site_id, path_id)")
  241. selectStmt, ok := stmt.(*SelectStmt)
  242. if !ok || len(selectStmt.From) != 1 || selectStmt.From[0].Join == nil {
  243. t.Fatalf("unexpected statement %#v", stmt)
  244. }
  245. using := selectStmt.From[0].Join.Using
  246. if len(using) != 2 || using[0] != "site_id" || using[1] != "path_id" {
  247. t.Fatalf("USING columns = %v", using)
  248. }
  249. }
  250. func TestParseInsertMultipleRows(t *testing.T) {
  251. stmt := parse(t, "INSERT INTO users VALUES (1, 'John'), (2, 'Jane')")
  252. ins := stmt.(*InsertStmt)
  253. if len(ins.Values) != 2 {
  254. t.Fatalf("expected 2 rows, got %d", len(ins.Values))
  255. }
  256. }
  257. func TestParseInsertOrReplace(t *testing.T) {
  258. stmt := parse(t, "INSERT OR REPLACE INTO users (id, name) VALUES (1, 'John')")
  259. ins := stmt.(*InsertStmt)
  260. if ins.OnConflict != ConflictReplace {
  261. t.Errorf("expected ConflictReplace, got %v", ins.OnConflict)
  262. }
  263. if ins.Table.Name != "users" {
  264. t.Errorf("expected table users, got %s", ins.Table.Name)
  265. }
  266. }
  267. func TestParseInsertOrIgnore(t *testing.T) {
  268. stmt := parse(t, "INSERT OR IGNORE INTO users (id, name) VALUES (1, 'John')")
  269. ins := stmt.(*InsertStmt)
  270. if ins.OnConflict != ConflictIgnore {
  271. t.Errorf("expected ConflictIgnore, got %v", ins.OnConflict)
  272. }
  273. }
  274. func TestParseInsertOrFail(t *testing.T) {
  275. stmt := parse(t, "INSERT OR FAIL INTO users (id, name) VALUES (1, 'John')")
  276. ins := stmt.(*InsertStmt)
  277. if ins.OnConflict != ConflictFail {
  278. t.Errorf("expected ConflictFail, got %v", ins.OnConflict)
  279. }
  280. }
  281. func TestParseInsertOrAbort(t *testing.T) {
  282. stmt := parse(t, "INSERT OR ABORT INTO users (id, name) VALUES (1, 'John')")
  283. ins := stmt.(*InsertStmt)
  284. if ins.OnConflict != ConflictAbort {
  285. t.Errorf("expected ConflictAbort, got %v", ins.OnConflict)
  286. }
  287. }
  288. // UPDATE statement tests
  289. func TestParseUpdate(t *testing.T) {
  290. stmt := parse(t, "UPDATE users SET name = 'John', age = 30 WHERE id = 1")
  291. upd, ok := stmt.(*UpdateStmt)
  292. if !ok {
  293. t.Fatalf("expected UpdateStmt, got %T", stmt)
  294. }
  295. if upd.Table.Name != "users" {
  296. t.Errorf("expected table users, got %s", upd.Table.Name)
  297. }
  298. if len(upd.Set) != 2 {
  299. t.Fatalf("expected 2 assignments, got %d", len(upd.Set))
  300. }
  301. if upd.Where == nil {
  302. t.Error("expected WHERE clause")
  303. }
  304. }
  305. // DELETE statement tests
  306. func TestParseDelete(t *testing.T) {
  307. stmt := parse(t, "DELETE FROM users WHERE id = 1")
  308. del, ok := stmt.(*DeleteStmt)
  309. if !ok {
  310. t.Fatalf("expected DeleteStmt, got %T", stmt)
  311. }
  312. if del.Table.Name != "users" {
  313. t.Errorf("expected table users, got %s", del.Table.Name)
  314. }
  315. if del.Where == nil {
  316. t.Error("expected WHERE clause")
  317. }
  318. }
  319. func TestParseDeleteAll(t *testing.T) {
  320. stmt := parse(t, "DELETE FROM users")
  321. del := stmt.(*DeleteStmt)
  322. if del.Where != nil {
  323. t.Error("expected no WHERE clause")
  324. }
  325. }
  326. // CREATE TABLE tests
  327. func TestParseCreateTable(t *testing.T) {
  328. stmt := parse(t, `CREATE TABLE users (
  329. id INTEGER PRIMARY KEY,
  330. name TEXT NOT NULL,
  331. email VARCHAR(255) UNIQUE,
  332. age INTEGER DEFAULT 0
  333. )`)
  334. create, ok := stmt.(*CreateTableStmt)
  335. if !ok {
  336. t.Fatalf("expected CreateTableStmt, got %T", stmt)
  337. }
  338. if create.Table.Name != "users" {
  339. t.Errorf("expected table users, got %s", create.Table.Name)
  340. }
  341. if len(create.Columns) != 4 {
  342. t.Fatalf("expected 4 columns, got %d", len(create.Columns))
  343. }
  344. // Check id column
  345. if create.Columns[0].Name != "id" {
  346. t.Error("expected first column to be id")
  347. }
  348. if create.Columns[0].Type.Name != "INTEGER" {
  349. t.Error("expected INTEGER type")
  350. }
  351. // Check name column has NOT NULL
  352. found := false
  353. for _, c := range create.Columns[1].Constraints {
  354. if c.Type == ConstraintNotNull {
  355. found = true
  356. }
  357. }
  358. if !found {
  359. t.Error("expected NOT NULL constraint on name")
  360. }
  361. // Check email has VARCHAR(255)
  362. if create.Columns[2].Type.Name != "VARCHAR" || create.Columns[2].Type.Precision != 255 {
  363. t.Error("expected VARCHAR(255) for email")
  364. }
  365. }
  366. func TestParseCreateTableIfNotExists(t *testing.T) {
  367. stmt := parse(t, "CREATE TABLE IF NOT EXISTS users (id INTEGER)")
  368. create := stmt.(*CreateTableStmt)
  369. if !create.IfNotExists {
  370. t.Error("expected IF NOT EXISTS")
  371. }
  372. }
  373. func TestParseCreateTableWithConstraints(t *testing.T) {
  374. stmt := parse(t, `CREATE TABLE orders (
  375. id INTEGER,
  376. user_id INTEGER,
  377. PRIMARY KEY (id),
  378. FOREIGN KEY (user_id) REFERENCES users(id)
  379. )`)
  380. create := stmt.(*CreateTableStmt)
  381. if len(create.Constraints) != 2 {
  382. t.Fatalf("expected 2 table constraints, got %d", len(create.Constraints))
  383. }
  384. // Check PRIMARY KEY
  385. if create.Constraints[0].Type != ConstraintPrimaryKey {
  386. t.Error("expected PRIMARY KEY constraint")
  387. }
  388. // Check FOREIGN KEY
  389. if create.Constraints[1].Type != ConstraintForeignKey {
  390. t.Error("expected FOREIGN KEY constraint")
  391. }
  392. if create.Constraints[1].RefTable != "users" {
  393. t.Errorf("expected reference to users, got %s", create.Constraints[1].RefTable)
  394. }
  395. }
  396. func TestParseCreateTableExplicitNullable(t *testing.T) {
  397. stmt := parse(t, `CREATE TABLE users (
  398. id INTEGER,
  399. full_name TEXT NULL,
  400. nickname TEXT NOT NULL,
  401. created_at TIMESTAMP NULL NOT NULL
  402. )`)
  403. create, ok := stmt.(*CreateTableStmt)
  404. if !ok {
  405. t.Fatalf("expected CreateTableStmt, got %T", stmt)
  406. }
  407. if len(create.Columns) != 4 {
  408. t.Fatalf("expected 4 columns, got %d", len(create.Columns))
  409. }
  410. // full_name TEXT NULL: explicit NULL is a no-op, so no constraint is added.
  411. fullName := create.Columns[1]
  412. if fullName.Name != "full_name" || fullName.Type.Name != "TEXT" {
  413. t.Fatalf("unexpected full_name column: %+v", fullName)
  414. }
  415. if len(fullName.Constraints) != 0 {
  416. t.Errorf("explicit NULL should not produce a constraint, got %d", len(fullName.Constraints))
  417. }
  418. // nickname TEXT NOT NULL still records NOT NULL.
  419. nickname := create.Columns[2]
  420. if len(nickname.Constraints) != 1 || nickname.Constraints[0].Type != ConstraintNotNull {
  421. t.Errorf("expected NOT NULL on nickname, got %+v", nickname.Constraints)
  422. }
  423. // created_at TIMESTAMP NULL NOT NULL: NOT NULL wins regardless of ordering.
  424. created := create.Columns[3]
  425. if len(created.Constraints) != 1 || created.Constraints[0].Type != ConstraintNotNull {
  426. t.Errorf("expected NOT NULL on created_at, got %+v", created.Constraints)
  427. }
  428. }
  429. // DROP TABLE tests
  430. func TestParseDropTable(t *testing.T) {
  431. stmt := parse(t, "DROP TABLE users")
  432. drop, ok := stmt.(*DropTableStmt)
  433. if !ok {
  434. t.Fatalf("expected DropTableStmt, got %T", stmt)
  435. }
  436. if len(drop.Tables) != 1 || drop.Tables[0].Name != "users" {
  437. t.Error("expected DROP TABLE users")
  438. }
  439. }
  440. func TestParseCreateTableDefaultSignedNumeric(t *testing.T) {
  441. stmt := parse(t, `CREATE TABLE repo (
  442. id INTEGER PRIMARY KEY,
  443. max_repo_creation INTEGER DEFAULT -1 NOT NULL,
  444. delta INTEGER DEFAULT +5,
  445. tally INTEGER DEFAULT (-7)
  446. )`)
  447. create, ok := stmt.(*CreateTableStmt)
  448. if !ok {
  449. t.Fatalf("expected CreateTableStmt, got %T", stmt)
  450. }
  451. if len(create.Columns) != 4 {
  452. t.Fatalf("expected 4 columns, got %d", len(create.Columns))
  453. }
  454. // xorm real shape: DEFAULT -1 followed by NOT NULL.
  455. col := create.Columns[1]
  456. if len(col.Constraints) != 2 {
  457. t.Fatalf("expected DEFAULT + NOT NULL, got %d constraints", len(col.Constraints))
  458. }
  459. var defaultExpr Expr
  460. var notNull bool
  461. for _, c := range col.Constraints {
  462. switch c.Type {
  463. case ConstraintDefault:
  464. defaultExpr = c.Default
  465. case ConstraintNotNull:
  466. notNull = true
  467. }
  468. }
  469. if !notNull {
  470. t.Error("expected NOT NULL constraint on max_repo_creation")
  471. }
  472. unary, ok := defaultExpr.(*UnaryExpr)
  473. if !ok || unary.Op != lexer.TokenMinus {
  474. t.Fatalf("expected unary minus default, got %T %+v", defaultExpr, defaultExpr)
  475. }
  476. lit, ok := unary.Operand.(*LiteralExpr)
  477. if !ok || lit.Value != "1" {
  478. t.Fatalf("expected -1 literal, got %+v", unary.Operand)
  479. }
  480. // Positive signed default: DEFAULT +5.
  481. plus, ok := create.Columns[2].Constraints[0].Default.(*UnaryExpr)
  482. if !ok || plus.Op != lexer.TokenPlus {
  483. t.Fatalf("expected unary plus default, got %+v", create.Columns[2].Constraints[0].Default)
  484. }
  485. // Parenthesized signed default: DEFAULT (-7).
  486. paren, ok := create.Columns[3].Constraints[0].Default.(*ParenExpr)
  487. if !ok {
  488. t.Fatalf("expected parenthesized default, got %T", create.Columns[3].Constraints[0].Default)
  489. }
  490. inner, ok := paren.Expr.(*UnaryExpr)
  491. if !ok || inner.Op != lexer.TokenMinus {
  492. t.Fatalf("expected unary minus inside parens, got %T %+v", paren.Expr, paren.Expr)
  493. }
  494. }
  495. func TestParseCreateTableUUIDType(t *testing.T) {
  496. stmt := parse(t, `CREATE TABLE upload (
  497. id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  498. uuid UUID NULL,
  499. name TEXT NULL
  500. )`)
  501. create, ok := stmt.(*CreateTableStmt)
  502. if !ok {
  503. t.Fatalf("expected CreateTableStmt, got %T", stmt)
  504. }
  505. if len(create.Columns) != 3 {
  506. t.Fatalf("expected 3 columns, got %d", len(create.Columns))
  507. }
  508. uuidCol := create.Columns[1]
  509. if uuidCol.Name != "uuid" {
  510. t.Errorf("column name = %q, want %q", uuidCol.Name, "uuid")
  511. }
  512. if uuidCol.Type.Name != "UUID" {
  513. t.Errorf("column type = %q, want %q", uuidCol.Type.Name, "UUID")
  514. }
  515. if len(uuidCol.Constraints) != 0 {
  516. t.Errorf("explicit NULL should produce no constraints, got %d", len(uuidCol.Constraints))
  517. }
  518. }
  519. func TestParseDropTableIfExists(t *testing.T) {
  520. stmt := parse(t, "DROP TABLE IF EXISTS users")
  521. drop := stmt.(*DropTableStmt)
  522. if !drop.IfExists {
  523. t.Error("expected IF EXISTS")
  524. }
  525. }
  526. // Expression tests
  527. func TestParseExprArithmetic(t *testing.T) {
  528. expr := parseExpr(t, "1 + 2 * 3")
  529. // Should be: 1 + (2 * 3) due to precedence
  530. add, ok := expr.(*BinaryExpr)
  531. if !ok || add.Op != lexer.TokenPlus {
  532. t.Fatal("expected + at top level")
  533. }
  534. mul, ok := add.Right.(*BinaryExpr)
  535. if !ok || mul.Op != lexer.TokenStar {
  536. t.Fatal("expected * on right side")
  537. }
  538. }
  539. func TestParseExprParens(t *testing.T) {
  540. expr := parseExpr(t, "(1 + 2) * 3")
  541. // Should be: (1 + 2) * 3
  542. mul, ok := expr.(*BinaryExpr)
  543. if !ok || mul.Op != lexer.TokenStar {
  544. t.Fatal("expected * at top level")
  545. }
  546. paren, ok := mul.Left.(*ParenExpr)
  547. if !ok {
  548. t.Fatal("expected ParenExpr on left")
  549. }
  550. add, ok := paren.Expr.(*BinaryExpr)
  551. if !ok || add.Op != lexer.TokenPlus {
  552. t.Fatal("expected + inside parens")
  553. }
  554. }
  555. func TestParseExprComparison(t *testing.T) {
  556. tests := []struct {
  557. input string
  558. op lexer.TokenType
  559. }{
  560. {"a = b", lexer.TokenEq},
  561. {"a <> b", lexer.TokenNeq},
  562. {"a != b", lexer.TokenNeq},
  563. {"a < b", lexer.TokenLt},
  564. {"a <= b", lexer.TokenLte},
  565. {"a > b", lexer.TokenGt},
  566. {"a >= b", lexer.TokenGte},
  567. }
  568. for _, tt := range tests {
  569. t.Run(tt.input, func(t *testing.T) {
  570. expr := parseExpr(t, tt.input)
  571. binary, ok := expr.(*BinaryExpr)
  572. if !ok {
  573. t.Fatalf("expected BinaryExpr, got %T", expr)
  574. }
  575. if binary.Op != tt.op {
  576. t.Errorf("expected %v, got %v", tt.op, binary.Op)
  577. }
  578. })
  579. }
  580. }
  581. func TestParseExprIsNull(t *testing.T) {
  582. tests := []struct {
  583. input string
  584. not bool
  585. }{
  586. {"a IS NULL", false},
  587. {"a IS NOT NULL", true},
  588. }
  589. for _, tt := range tests {
  590. t.Run(tt.input, func(t *testing.T) {
  591. expr := parseExpr(t, tt.input)
  592. isNull, ok := expr.(*IsNullExpr)
  593. if !ok {
  594. t.Fatalf("expected IsNullExpr, got %T", expr)
  595. }
  596. if isNull.Not != tt.not {
  597. t.Errorf("expected Not=%v, got %v", tt.not, isNull.Not)
  598. }
  599. })
  600. }
  601. }
  602. func TestParseExprIn(t *testing.T) {
  603. tests := []struct {
  604. input string
  605. not bool
  606. }{
  607. {"a IN (1, 2, 3)", false},
  608. {"a NOT IN (1, 2, 3)", true},
  609. }
  610. for _, tt := range tests {
  611. t.Run(tt.input, func(t *testing.T) {
  612. expr := parseExpr(t, tt.input)
  613. in, ok := expr.(*InExpr)
  614. if !ok {
  615. t.Fatalf("expected InExpr, got %T", expr)
  616. }
  617. if in.Not != tt.not {
  618. t.Errorf("expected Not=%v, got %v", tt.not, in.Not)
  619. }
  620. if len(in.Values) != 3 {
  621. t.Errorf("expected 3 values, got %d", len(in.Values))
  622. }
  623. })
  624. }
  625. }
  626. func TestParseExprBetween(t *testing.T) {
  627. tests := []struct {
  628. input string
  629. not bool
  630. }{
  631. {"a BETWEEN 1 AND 10", false},
  632. {"a NOT BETWEEN 1 AND 10", true},
  633. }
  634. for _, tt := range tests {
  635. t.Run(tt.input, func(t *testing.T) {
  636. expr := parseExpr(t, tt.input)
  637. between, ok := expr.(*BetweenExpr)
  638. if !ok {
  639. t.Fatalf("expected BetweenExpr, got %T", expr)
  640. }
  641. if between.Not != tt.not {
  642. t.Errorf("expected Not=%v, got %v", tt.not, between.Not)
  643. }
  644. })
  645. }
  646. }
  647. func TestParseExprLike(t *testing.T) {
  648. tests := []struct {
  649. input string
  650. not bool
  651. }{
  652. {"name LIKE '%test%'", false},
  653. {"name NOT LIKE '%test%'", true},
  654. }
  655. for _, tt := range tests {
  656. t.Run(tt.input, func(t *testing.T) {
  657. expr := parseExpr(t, tt.input)
  658. like, ok := expr.(*LikeExpr)
  659. if !ok {
  660. t.Fatalf("expected LikeExpr, got %T", expr)
  661. }
  662. if like.Not != tt.not {
  663. t.Errorf("expected Not=%v, got %v", tt.not, like.Not)
  664. }
  665. })
  666. }
  667. }
  668. func TestParseExprCase(t *testing.T) {
  669. expr := parseExpr(t, "CASE WHEN x = 1 THEN 'one' WHEN x = 2 THEN 'two' ELSE 'other' END")
  670. caseExpr, ok := expr.(*CaseExpr)
  671. if !ok {
  672. t.Fatalf("expected CaseExpr, got %T", expr)
  673. }
  674. if len(caseExpr.Whens) != 2 {
  675. t.Errorf("expected 2 WHEN clauses, got %d", len(caseExpr.Whens))
  676. }
  677. if caseExpr.Else == nil {
  678. t.Error("expected ELSE clause")
  679. }
  680. }
  681. func TestParseExprCast(t *testing.T) {
  682. expr := parseExpr(t, "CAST(x AS INTEGER)")
  683. cast, ok := expr.(*CastExpr)
  684. if !ok {
  685. t.Fatalf("expected CastExpr, got %T", expr)
  686. }
  687. if cast.Type.Name != "INTEGER" {
  688. t.Errorf("expected INTEGER type, got %s", cast.Type.Name)
  689. }
  690. }
  691. func TestParseExprFunction(t *testing.T) {
  692. tests := []struct {
  693. input string
  694. name string
  695. argCount int
  696. star bool
  697. distinct bool
  698. }{
  699. {"COUNT(*)", "COUNT", 0, true, false},
  700. {"COUNT(id)", "COUNT", 1, false, false},
  701. {"COUNT(DISTINCT id)", "COUNT", 1, false, true},
  702. {"SUM(amount)", "SUM", 1, false, false},
  703. {"UPPER(name)", "UPPER", 1, false, false},
  704. {"COALESCE(a, b, c)", "COALESCE", 3, false, false},
  705. }
  706. for _, tt := range tests {
  707. t.Run(tt.input, func(t *testing.T) {
  708. expr := parseExpr(t, tt.input)
  709. fn, ok := expr.(*FunctionCall)
  710. if !ok {
  711. t.Fatalf("expected FunctionCall, got %T", expr)
  712. }
  713. if fn.Name != tt.name {
  714. t.Errorf("expected name %s, got %s", tt.name, fn.Name)
  715. }
  716. if len(fn.Args) != tt.argCount {
  717. t.Errorf("expected %d args, got %d", tt.argCount, len(fn.Args))
  718. }
  719. if fn.Star != tt.star {
  720. t.Errorf("expected Star=%v, got %v", tt.star, fn.Star)
  721. }
  722. if fn.Distinct != tt.distinct {
  723. t.Errorf("expected Distinct=%v, got %v", tt.distinct, fn.Distinct)
  724. }
  725. })
  726. }
  727. }
  728. func TestParseExprSubquery(t *testing.T) {
  729. expr := parseExpr(t, "id IN (SELECT user_id FROM orders)")
  730. in, ok := expr.(*InExpr)
  731. if !ok {
  732. t.Fatalf("expected InExpr, got %T", expr)
  733. }
  734. if in.Subquery == nil {
  735. t.Error("expected subquery")
  736. }
  737. }
  738. func TestParseExprExists(t *testing.T) {
  739. expr := parseExpr(t, "EXISTS (SELECT 1 FROM users WHERE id = 1)")
  740. exists, ok := expr.(*ExistsExpr)
  741. if !ok {
  742. t.Fatalf("expected ExistsExpr, got %T", expr)
  743. }
  744. if exists.Subquery == nil {
  745. t.Error("expected subquery")
  746. }
  747. }
  748. func TestParseExprColumnRef(t *testing.T) {
  749. tests := []struct {
  750. input string
  751. table string
  752. column string
  753. }{
  754. {"id", "", "id"},
  755. {"users.id", "users", "id"},
  756. {"u.name", "u", "name"},
  757. }
  758. for _, tt := range tests {
  759. t.Run(tt.input, func(t *testing.T) {
  760. expr := parseExpr(t, tt.input)
  761. ref, ok := expr.(*ColumnRef)
  762. if !ok {
  763. t.Fatalf("expected ColumnRef, got %T", expr)
  764. }
  765. if ref.Table != tt.table {
  766. t.Errorf("expected table %q, got %q", tt.table, ref.Table)
  767. }
  768. if ref.Column != tt.column {
  769. t.Errorf("expected column %q, got %q", tt.column, ref.Column)
  770. }
  771. })
  772. }
  773. }
  774. // Error cases
  775. func TestParseErrors(t *testing.T) {
  776. tests := []struct {
  777. name string
  778. input string
  779. }{
  780. {"missing columns", "SELECT FROM users"},
  781. {"missing VALUES", "INSERT INTO users"},
  782. {"missing SET", "UPDATE users WHERE id = 1"},
  783. {"missing table name", "DELETE FROM WHERE id = 1"},
  784. {"unclosed paren", "SELECT * FROM users WHERE (id = 1"},
  785. {"invalid token", "SELECT @ FROM users"},
  786. }
  787. for _, tt := range tests {
  788. t.Run(tt.name, func(t *testing.T) {
  789. l := lexer.New(tt.input)
  790. p := New(l)
  791. _, err := p.Parse()
  792. if err == nil {
  793. t.Error("expected parse error")
  794. }
  795. })
  796. }
  797. }
  798. // Multiple statements
  799. func TestParseMultiple(t *testing.T) {
  800. input := `
  801. SELECT * FROM users;
  802. INSERT INTO users VALUES (1, 'John');
  803. DELETE FROM users WHERE id = 1
  804. `
  805. l := lexer.New(input)
  806. p := New(l)
  807. stmts, err := p.ParseMultiple()
  808. if err != nil {
  809. t.Fatalf("parse error: %v", err)
  810. }
  811. if len(stmts) != 3 {
  812. t.Errorf("expected 3 statements, got %d", len(stmts))
  813. }
  814. }
  815. func TestParseInsertReturning(t *testing.T) {
  816. stmt := parse(t, "INSERT INTO users (id, name) VALUES (1, 'a') RETURNING id, name AS n, id + 1")
  817. ins, ok := stmt.(*InsertStmt)
  818. if !ok {
  819. t.Fatalf("expected InsertStmt, got %T", stmt)
  820. }
  821. if len(ins.Returning) != 3 {
  822. t.Fatalf("expected 3 returning columns, got %d", len(ins.Returning))
  823. }
  824. if ref, ok := ins.Returning[0].Expr.(*ColumnRef); !ok || ref.Column != "id" {
  825. t.Fatalf("unexpected first returning column: %#v", ins.Returning[0])
  826. }
  827. if ins.Returning[1].Alias != "n" {
  828. t.Fatalf("expected alias n, got %q", ins.Returning[1].Alias)
  829. }
  830. if _, ok := ins.Returning[2].Expr.(*BinaryExpr); !ok {
  831. t.Fatalf("expected expression in third returning column, got %T", ins.Returning[2].Expr)
  832. }
  833. }
  834. func TestParseUpdateAndDeleteReturning(t *testing.T) {
  835. upd := parse(t, "UPDATE users SET name = 'b' WHERE id = 1 RETURNING id, name")
  836. if len(upd.(*UpdateStmt).Returning) != 2 {
  837. t.Fatalf("expected 2 returning columns on UPDATE")
  838. }
  839. del := parse(t, "DELETE FROM users WHERE id = 1 RETURNING *")
  840. if len(del.(*DeleteStmt).Returning) != 1 || !del.(*DeleteStmt).Returning[0].Star {
  841. t.Fatalf("expected RETURNING * on DELETE")
  842. }
  843. }
  844. func TestParseGeneratedColumn(t *testing.T) {
  845. stmt := parse(t, `CREATE TABLE t (
  846. a INTEGER,
  847. b INTEGER,
  848. total INTEGER GENERATED ALWAYS AS (a + b) STORED,
  849. vit TEXT AS (a || '-') VIRTUAL
  850. )`)
  851. ct := stmt.(*CreateTableStmt)
  852. if len(ct.Columns) != 4 {
  853. t.Fatalf("expected 4 columns, got %d", len(ct.Columns))
  854. }
  855. if ct.Columns[2].GeneratedExpr == nil || !ct.Columns[2].GeneratedStored {
  856. t.Fatalf("expected stored generated column, got %#v", ct.Columns[2])
  857. }
  858. if ct.Columns[3].GeneratedExpr == nil || ct.Columns[3].GeneratedStored {
  859. t.Fatalf("expected virtual generated column, got %#v", ct.Columns[3])
  860. }
  861. }
  862. func TestParseTableConstraintOnConflictReplace(t *testing.T) {
  863. stmt := parse(t, `CREATE TABLE t (
  864. a INTEGER,
  865. b INTEGER,
  866. CONSTRAINT "t#a#b" UNIQUE(a, b) ON CONFLICT REPLACE
  867. )`)
  868. ct := stmt.(*CreateTableStmt)
  869. if len(ct.Constraints) != 1 {
  870. t.Fatalf("expected 1 constraint, got %d", len(ct.Constraints))
  871. }
  872. c := ct.Constraints[0]
  873. if !c.HasOnConflict || c.OnConflict != ConflictReplace {
  874. t.Fatalf("expected ON CONFLICT REPLACE, got %#v", c)
  875. }
  876. }
  877. func TestParseExpressionIndex(t *testing.T) {
  878. stmt := parse(t, "CREATE UNIQUE INDEX users_email ON users (lower(email))")
  879. ci := stmt.(*CreateIndexStmt)
  880. if len(ci.Columns) != 1 {
  881. t.Fatalf("expected 1 index column, got %d", len(ci.Columns))
  882. }
  883. if ci.Columns[0].Expr == nil {
  884. t.Fatalf("expected expression index column, got %#v", ci.Columns[0])
  885. }
  886. if ci.Columns[0].Name != "lower(email)" {
  887. t.Fatalf("expected name lower(email), got %q", ci.Columns[0].Name)
  888. }
  889. }
  890. func TestParseBitwisePrecedence(t *testing.T) {
  891. // 1 + 2 | 4 must parse as (1 + 2) | 4 because + binds tighter than |.
  892. expr := parseExpr(t, "1 + 2 | 4")
  893. bin, ok := expr.(*BinaryExpr)
  894. if !ok || bin.Op != lexer.TokenBitOr {
  895. t.Fatalf("expected top-level bit-or, got %#v", expr)
  896. }
  897. if left, ok := bin.Left.(*BinaryExpr); !ok || left.Op != lexer.TokenPlus {
  898. t.Fatalf("expected (1+2) on the left, got %#v", bin.Left)
  899. }
  900. }
  901. func TestParseBlobLiteral(t *testing.T) {
  902. expr := parseExpr(t, "X'00FF'")
  903. lit, ok := expr.(*LiteralExpr)
  904. if !ok || lit.Type != lexer.TokenBlob {
  905. t.Fatalf("expected blob literal, got %#v", expr)
  906. }
  907. if []byte(lit.Value)[0] != 0x00 || []byte(lit.Value)[1] != 0xFF {
  908. t.Fatalf("unexpected blob bytes: %v", []byte(lit.Value))
  909. }
  910. }
  911. func TestParseAnalyze(t *testing.T) {
  912. if _, ok := parse(t, "ANALYZE").(*AnalyzeStmt); !ok {
  913. t.Fatalf("ANALYZE did not parse as AnalyzeStmt")
  914. }
  915. stmt := parse(t, "ANALYZE main.users")
  916. if got := stmt.(*AnalyzeStmt).Name; got != "users" {
  917. t.Fatalf("ANALYZE name = %q, want users", got)
  918. }
  919. }
  920. func TestParseIsDistinctFrom(t *testing.T) {
  921. expr := parseExpr(t, "a IS DISTINCT FROM b")
  922. d, ok := expr.(*IsDistinctExpr)
  923. if !ok {
  924. t.Fatalf("expected IsDistinctExpr, got %T", expr)
  925. }
  926. if d.Not {
  927. t.Fatalf("IS DISTINCT FROM should not set Not")
  928. }
  929. expr = parseExpr(t, "a IS NOT DISTINCT FROM b")
  930. if d, ok := expr.(*IsDistinctExpr); !ok || !d.Not {
  931. t.Fatalf("expected IsDistinctExpr with Not=true, got %#v", expr)
  932. }
  933. }
  934. func TestParseUpdateFrom(t *testing.T) {
  935. stmt := parse(t, "UPDATE users SET access = 'x' FROM other WHERE other.id = users.id RETURNING users.id")
  936. upd, ok := stmt.(*UpdateStmt)
  937. if !ok {
  938. t.Fatalf("expected UpdateStmt, got %T", stmt)
  939. }
  940. if len(upd.From) != 1 || upd.From[0].Name != "other" {
  941. t.Fatalf("unexpected FROM: %#v", upd.From)
  942. }
  943. if len(upd.Returning) != 1 {
  944. t.Fatalf("expected 1 RETURNING column, got %d", len(upd.Returning))
  945. }
  946. }
  947. func TestParseWithUpdate(t *testing.T) {
  948. stmt := parse(t, `WITH x AS (SELECT count(*) AS c, site_id FROM users GROUP BY site_id)
  949. UPDATE users SET access = 'y' FROM x WHERE x.c = 1 AND users.site_id = x.site_id`)
  950. upd, ok := stmt.(*UpdateStmt)
  951. if !ok {
  952. t.Fatalf("expected UpdateStmt, got %T", stmt)
  953. }
  954. if len(upd.From) != 1 || upd.From[0].Subquery == nil {
  955. t.Fatalf("CTE was not desugared into the FROM clause: %#v", upd.From)
  956. }
  957. }
  958. func TestParseInsertSelectReturning(t *testing.T) {
  959. // RETURNING after INSERT ... SELECT must not be swallowed as a table alias.
  960. stmt := parse(t, "INSERT INTO dst (a, b) SELECT a, b FROM src RETURNING a")
  961. ins, ok := stmt.(*InsertStmt)
  962. if !ok {
  963. t.Fatalf("expected InsertStmt, got %T", stmt)
  964. }
  965. if ins.Select == nil || len(ins.Select.From) != 1 {
  966. t.Fatalf("unexpected SELECT: %#v", ins.Select)
  967. }
  968. if ins.Select.From[0].Alias != "" {
  969. t.Fatalf("RETURNING was parsed as a table alias: %q", ins.Select.From[0].Alias)
  970. }
  971. if len(ins.Returning) != 1 {
  972. t.Fatalf("expected 1 RETURNING column, got %d", len(ins.Returning))
  973. }
  974. }
  975. func TestParseInsertWithSelect(t *testing.T) {
  976. stmt := parse(t, `INSERT INTO dst (id, value)
  977. WITH source AS (SELECT id, value FROM src WHERE id > 1)
  978. SELECT id, value FROM source`)
  979. insert, ok := stmt.(*InsertStmt)
  980. if !ok {
  981. t.Fatalf("expected InsertStmt, got %T", stmt)
  982. }
  983. if insert.Select == nil || len(insert.Select.From) != 1 || insert.Select.From[0].Subquery == nil {
  984. t.Fatalf("WITH SELECT was not attached to INSERT: %#v", insert.Select)
  985. }
  986. }
  987. func TestParsePostgresCompatibilityClauses(t *testing.T) {
  988. t.Run("alter add column if not exists", func(t *testing.T) {
  989. stmt := parse(t, "ALTER TABLE users ADD COLUMN IF NOT EXISTS revision INTEGER DEFAULT 0")
  990. action := stmt.(*AlterTableStmt).Action.(*AddColumnAction)
  991. if !action.IfNotExists || action.Column.Name != "revision" {
  992. t.Fatalf("unexpected action: %#v", action)
  993. }
  994. })
  995. t.Run("on conflict do update", func(t *testing.T) {
  996. stmt := parse(t, "INSERT INTO users (id, count) VALUES (1, 1) ON CONFLICT (id) DO UPDATE SET count = users.count + 1")
  997. insert := stmt.(*InsertStmt)
  998. if len(insert.ConflictTarget) != 1 || insert.ConflictTarget[0] != "id" || len(insert.ConflictUpdate) != 1 {
  999. t.Fatalf("unexpected conflict clause: %#v", insert)
  1000. }
  1001. })
  1002. t.Run("jsonb cast", func(t *testing.T) {
  1003. parse(t, "SELECT CAST('{}' AS JSONB)")
  1004. })
  1005. }
  1006. // Phase 4: PRAGMA and EXPLAIN tests
  1007. func TestParsePragmaTableInfo(t *testing.T) {
  1008. stmt := parse(t, "PRAGMA table_info(users)")
  1009. pragma, ok := stmt.(*PragmaStmt)
  1010. if !ok {
  1011. t.Fatalf("expected PragmaStmt, got %T", stmt)
  1012. }
  1013. if pragma.Name != "table_info" {
  1014. t.Errorf("expected pragma name 'table_info', got %s", pragma.Name)
  1015. }
  1016. if pragma.Arg != "users" {
  1017. t.Errorf("expected arg 'users', got %s", pragma.Arg)
  1018. }
  1019. }
  1020. func TestParsePragmaTableList(t *testing.T) {
  1021. stmt := parse(t, "PRAGMA table_list")
  1022. pragma, ok := stmt.(*PragmaStmt)
  1023. if !ok {
  1024. t.Fatalf("expected PragmaStmt, got %T", stmt)
  1025. }
  1026. if pragma.Name != "table_list" {
  1027. t.Errorf("expected pragma name 'table_list', got %s", pragma.Name)
  1028. }
  1029. }
  1030. func TestParsePragmaDatabaseList(t *testing.T) {
  1031. stmt := parse(t, "PRAGMA database_list")
  1032. pragma := stmt.(*PragmaStmt)
  1033. if pragma.Name != "database_list" {
  1034. t.Errorf("expected pragma name 'database_list', got %s", pragma.Name)
  1035. }
  1036. }
  1037. func TestParsePragmaVersion(t *testing.T) {
  1038. stmt := parse(t, "PRAGMA version")
  1039. pragma := stmt.(*PragmaStmt)
  1040. if pragma.Name != "version" {
  1041. t.Errorf("expected pragma name 'version', got %s", pragma.Name)
  1042. }
  1043. }
  1044. func TestParseExplain(t *testing.T) {
  1045. stmt := parse(t, "EXPLAIN SELECT * FROM users")
  1046. explain, ok := stmt.(*ExplainStmt)
  1047. if !ok {
  1048. t.Fatalf("expected ExplainStmt, got %T", stmt)
  1049. }
  1050. if explain.QueryPlan {
  1051. t.Error("expected QueryPlan to be false")
  1052. }
  1053. _, ok = explain.Statement.(*SelectStmt)
  1054. if !ok {
  1055. t.Errorf("expected SelectStmt inside EXPLAIN, got %T", explain.Statement)
  1056. }
  1057. }
  1058. func TestParseExplainQueryPlan(t *testing.T) {
  1059. stmt := parse(t, "EXPLAIN QUERY PLAN SELECT * FROM users WHERE id = 1")
  1060. explain, ok := stmt.(*ExplainStmt)
  1061. if !ok {
  1062. t.Fatalf("expected ExplainStmt, got %T", stmt)
  1063. }
  1064. if !explain.QueryPlan {
  1065. t.Error("expected QueryPlan to be true")
  1066. }
  1067. sel, ok := explain.Statement.(*SelectStmt)
  1068. if !ok {
  1069. t.Errorf("expected SelectStmt inside EXPLAIN, got %T", explain.Statement)
  1070. }
  1071. if sel.Where == nil {
  1072. t.Error("expected WHERE clause in explained statement")
  1073. }
  1074. }
  1075. func TestParseExplainInsert(t *testing.T) {
  1076. stmt := parse(t, "EXPLAIN INSERT INTO users (name) VALUES ('John')")
  1077. explain := stmt.(*ExplainStmt)
  1078. _, ok := explain.Statement.(*InsertStmt)
  1079. if !ok {
  1080. t.Errorf("expected InsertStmt inside EXPLAIN, got %T", explain.Statement)
  1081. }
  1082. }
  1083. // Phase 5: Transaction statement tests
  1084. func TestParseBegin(t *testing.T) {
  1085. stmt := parse(t, "BEGIN")
  1086. _, ok := stmt.(*BeginStmt)
  1087. if !ok {
  1088. t.Fatalf("expected BeginStmt, got %T", stmt)
  1089. }
  1090. }
  1091. func TestParseBeginTransaction(t *testing.T) {
  1092. stmt := parse(t, "BEGIN TRANSACTION")
  1093. _, ok := stmt.(*BeginStmt)
  1094. if !ok {
  1095. t.Fatalf("expected BeginStmt, got %T", stmt)
  1096. }
  1097. }
  1098. func TestParseCommit(t *testing.T) {
  1099. stmt := parse(t, "COMMIT")
  1100. _, ok := stmt.(*CommitStmt)
  1101. if !ok {
  1102. t.Fatalf("expected CommitStmt, got %T", stmt)
  1103. }
  1104. }
  1105. func TestParseCommitTransaction(t *testing.T) {
  1106. stmt := parse(t, "COMMIT TRANSACTION")
  1107. _, ok := stmt.(*CommitStmt)
  1108. if !ok {
  1109. t.Fatalf("expected CommitStmt, got %T", stmt)
  1110. }
  1111. }
  1112. func TestParseRollback(t *testing.T) {
  1113. stmt := parse(t, "ROLLBACK")
  1114. rollback, ok := stmt.(*RollbackStmt)
  1115. if !ok {
  1116. t.Fatalf("expected RollbackStmt, got %T", stmt)
  1117. }
  1118. if rollback.Savepoint != "" {
  1119. t.Errorf("expected empty savepoint, got %s", rollback.Savepoint)
  1120. }
  1121. }
  1122. func TestParseRollbackToSavepoint(t *testing.T) {
  1123. stmt := parse(t, "ROLLBACK TO SAVEPOINT sp1")
  1124. rollback, ok := stmt.(*RollbackStmt)
  1125. if !ok {
  1126. t.Fatalf("expected RollbackStmt, got %T", stmt)
  1127. }
  1128. if rollback.Savepoint != "sp1" {
  1129. t.Errorf("expected savepoint 'sp1', got %s", rollback.Savepoint)
  1130. }
  1131. }
  1132. func TestParseRollbackTo(t *testing.T) {
  1133. stmt := parse(t, "ROLLBACK TO sp1")
  1134. rollback := stmt.(*RollbackStmt)
  1135. if rollback.Savepoint != "sp1" {
  1136. t.Errorf("expected savepoint 'sp1', got %s", rollback.Savepoint)
  1137. }
  1138. }
  1139. func TestParseSavepoint(t *testing.T) {
  1140. stmt := parse(t, "SAVEPOINT my_savepoint")
  1141. sp, ok := stmt.(*SavepointStmt)
  1142. if !ok {
  1143. t.Fatalf("expected SavepointStmt, got %T", stmt)
  1144. }
  1145. if sp.Name != "my_savepoint" {
  1146. t.Errorf("expected savepoint name 'my_savepoint', got %s", sp.Name)
  1147. }
  1148. }
  1149. func TestParseReleaseSavepoint(t *testing.T) {
  1150. stmt := parse(t, "RELEASE SAVEPOINT sp1")
  1151. rel, ok := stmt.(*ReleaseStmt)
  1152. if !ok {
  1153. t.Fatalf("expected ReleaseStmt, got %T", stmt)
  1154. }
  1155. if rel.Name != "sp1" {
  1156. t.Errorf("expected savepoint name 'sp1', got %s", rel.Name)
  1157. }
  1158. }
  1159. func TestParseRelease(t *testing.T) {
  1160. stmt := parse(t, "RELEASE sp1")
  1161. rel := stmt.(*ReleaseStmt)
  1162. if rel.Name != "sp1" {
  1163. t.Errorf("expected savepoint name 'sp1', got %s", rel.Name)
  1164. }
  1165. }
  1166. // Benchmark
  1167. func BenchmarkParseSelect(b *testing.B) {
  1168. input := `SELECT u.id, u.name, u.email, COUNT(o.id) as order_count
  1169. FROM users u
  1170. LEFT JOIN orders o ON u.id = o.user_id
  1171. WHERE u.active = TRUE AND u.created_at >= '2024-01-01'
  1172. GROUP BY u.id, u.name, u.email
  1173. HAVING COUNT(o.id) > 5
  1174. ORDER BY order_count DESC
  1175. LIMIT 100 OFFSET 0`
  1176. b.ResetTimer()
  1177. for i := 0; i < b.N; i++ {
  1178. l := lexer.New(input)
  1179. p := New(l)
  1180. _, _ = p.Parse()
  1181. }
  1182. }
  1183. func BenchmarkParseCreateTable(b *testing.B) {
  1184. input := `CREATE TABLE users (
  1185. id INTEGER PRIMARY KEY AUTOINCREMENT,
  1186. name TEXT NOT NULL,
  1187. email VARCHAR(255) UNIQUE,
  1188. age INTEGER DEFAULT 0,
  1189. active BOOLEAN DEFAULT TRUE,
  1190. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  1191. )`
  1192. b.ResetTimer()
  1193. for i := 0; i < b.N; i++ {
  1194. l := lexer.New(input)
  1195. p := New(l)
  1196. _, _ = p.Parse()
  1197. }
  1198. }
  1199. // CREATE INDEX tests
  1200. func TestParseCreateIndex(t *testing.T) {
  1201. stmt := parse(t, "CREATE INDEX idx_email ON users (email)")
  1202. idx, ok := stmt.(*CreateIndexStmt)
  1203. if !ok {
  1204. t.Fatalf("expected CreateIndexStmt, got %T", stmt)
  1205. }
  1206. if idx.Name != "idx_email" {
  1207. t.Errorf("expected index name idx_email, got %s", idx.Name)
  1208. }
  1209. if idx.Table != "users" {
  1210. t.Errorf("expected table users, got %s", idx.Table)
  1211. }
  1212. if len(idx.Columns) != 1 || idx.Columns[0].Name != "email" {
  1213. t.Error("expected column email")
  1214. }
  1215. if idx.Unique {
  1216. t.Error("expected non-unique index")
  1217. }
  1218. if idx.IfNotExists {
  1219. t.Error("expected IfNotExists to be false")
  1220. }
  1221. }
  1222. func TestParseCreateUniqueIndex(t *testing.T) {
  1223. stmt := parse(t, "CREATE UNIQUE INDEX idx_email ON users (email)")
  1224. idx, ok := stmt.(*CreateIndexStmt)
  1225. if !ok {
  1226. t.Fatalf("expected CreateIndexStmt, got %T", stmt)
  1227. }
  1228. if !idx.Unique {
  1229. t.Error("expected unique index")
  1230. }
  1231. }
  1232. func TestParseCreateIndexIfNotExists(t *testing.T) {
  1233. stmt := parse(t, "CREATE INDEX IF NOT EXISTS idx_email ON users (email)")
  1234. idx, ok := stmt.(*CreateIndexStmt)
  1235. if !ok {
  1236. t.Fatalf("expected CreateIndexStmt, got %T", stmt)
  1237. }
  1238. if !idx.IfNotExists {
  1239. t.Error("expected IfNotExists to be true")
  1240. }
  1241. }
  1242. func TestParseCreateIndexMultiColumn(t *testing.T) {
  1243. stmt := parse(t, "CREATE INDEX idx_name_email ON users (name, email)")
  1244. idx, ok := stmt.(*CreateIndexStmt)
  1245. if !ok {
  1246. t.Fatalf("expected CreateIndexStmt, got %T", stmt)
  1247. }
  1248. if len(idx.Columns) != 2 {
  1249. t.Fatalf("expected 2 columns, got %d", len(idx.Columns))
  1250. }
  1251. if idx.Columns[0].Name != "name" {
  1252. t.Errorf("expected first column name, got %s", idx.Columns[0].Name)
  1253. }
  1254. if idx.Columns[1].Name != "email" {
  1255. t.Errorf("expected second column email, got %s", idx.Columns[1].Name)
  1256. }
  1257. }
  1258. func TestParseCreateIndexWithDesc(t *testing.T) {
  1259. stmt := parse(t, "CREATE INDEX idx_created ON users (created_at DESC)")
  1260. idx, ok := stmt.(*CreateIndexStmt)
  1261. if !ok {
  1262. t.Fatalf("expected CreateIndexStmt, got %T", stmt)
  1263. }
  1264. if len(idx.Columns) != 1 {
  1265. t.Fatalf("expected 1 column, got %d", len(idx.Columns))
  1266. }
  1267. if !idx.Columns[0].Desc {
  1268. t.Error("expected DESC ordering")
  1269. }
  1270. }
  1271. // DROP INDEX tests
  1272. func TestParseDropIndex(t *testing.T) {
  1273. stmt := parse(t, "DROP INDEX idx_email")
  1274. drop, ok := stmt.(*DropIndexStmt)
  1275. if !ok {
  1276. t.Fatalf("expected DropIndexStmt, got %T", stmt)
  1277. }
  1278. if drop.Name != "idx_email" {
  1279. t.Errorf("expected index name idx_email, got %s", drop.Name)
  1280. }
  1281. if drop.IfExists {
  1282. t.Error("expected IfExists to be false")
  1283. }
  1284. }
  1285. func TestParseDropIndexIfExists(t *testing.T) {
  1286. stmt := parse(t, "DROP INDEX IF EXISTS idx_email")
  1287. drop, ok := stmt.(*DropIndexStmt)
  1288. if !ok {
  1289. t.Fatalf("expected DropIndexStmt, got %T", stmt)
  1290. }
  1291. if !drop.IfExists {
  1292. t.Error("expected IfExists to be true")
  1293. }
  1294. }
  1295. // Subquery in FROM clause tests
  1296. func TestParseSelectFromSubquery(t *testing.T) {
  1297. stmt := parse(t, "SELECT * FROM (SELECT id, name FROM users) AS u")
  1298. sel, ok := stmt.(*SelectStmt)
  1299. if !ok {
  1300. t.Fatalf("expected SelectStmt, got %T", stmt)
  1301. }
  1302. if len(sel.From) != 1 {
  1303. t.Fatalf("expected 1 FROM item, got %d", len(sel.From))
  1304. }
  1305. if sel.From[0].Subquery == nil {
  1306. t.Fatal("expected subquery in FROM")
  1307. }
  1308. if sel.From[0].Alias != "u" {
  1309. t.Errorf("expected alias 'u', got '%s'", sel.From[0].Alias)
  1310. }
  1311. // Check subquery
  1312. subquery := sel.From[0].Subquery
  1313. if len(subquery.Columns) != 2 {
  1314. t.Errorf("expected 2 columns in subquery, got %d", len(subquery.Columns))
  1315. }
  1316. if len(subquery.From) != 1 || subquery.From[0].Name != "users" {
  1317. t.Error("expected subquery FROM users")
  1318. }
  1319. }
  1320. func TestParseSelectFromSubqueryWithWhere(t *testing.T) {
  1321. stmt := parse(t, "SELECT name FROM (SELECT id, name FROM users WHERE active = TRUE) AS active_users WHERE id > 10")
  1322. sel, ok := stmt.(*SelectStmt)
  1323. if !ok {
  1324. t.Fatalf("expected SelectStmt, got %T", stmt)
  1325. }
  1326. if sel.From[0].Subquery == nil {
  1327. t.Fatal("expected subquery in FROM")
  1328. }
  1329. // Check outer WHERE clause
  1330. if sel.Where == nil {
  1331. t.Error("expected outer WHERE clause")
  1332. }
  1333. // Check subquery WHERE clause
  1334. if sel.From[0].Subquery.Where == nil {
  1335. t.Error("expected subquery WHERE clause")
  1336. }
  1337. }
  1338. func TestParseSelectFromSubqueryComplex(t *testing.T) {
  1339. stmt := parse(t, "SELECT u.name, u.total FROM (SELECT user_id, SUM(amount) AS total FROM orders GROUP BY user_id) AS u")
  1340. sel, ok := stmt.(*SelectStmt)
  1341. if !ok {
  1342. t.Fatalf("expected SelectStmt, got %T", stmt)
  1343. }
  1344. if sel.From[0].Subquery == nil {
  1345. t.Fatal("expected subquery in FROM")
  1346. }
  1347. subquery := sel.From[0].Subquery
  1348. if len(subquery.GroupBy) == 0 {
  1349. t.Error("expected GROUP BY in subquery")
  1350. }
  1351. // Check that columns reference the alias
  1352. if len(sel.Columns) != 2 {
  1353. t.Fatalf("expected 2 columns, got %d", len(sel.Columns))
  1354. }
  1355. }
  1356. func TestParseSelectFromNestedSubquery(t *testing.T) {
  1357. stmt := parse(t, "SELECT * FROM (SELECT * FROM (SELECT id FROM users) AS inner_q) AS outer_q")
  1358. sel, ok := stmt.(*SelectStmt)
  1359. if !ok {
  1360. t.Fatalf("expected SelectStmt, got %T", stmt)
  1361. }
  1362. if sel.From[0].Subquery == nil {
  1363. t.Fatal("expected subquery in FROM")
  1364. }
  1365. // Check nested subquery
  1366. outerSubquery := sel.From[0].Subquery
  1367. if len(outerSubquery.From) == 0 || outerSubquery.From[0].Subquery == nil {
  1368. t.Error("expected nested subquery")
  1369. }
  1370. }
  1371. // ALTER TABLE tests
  1372. func TestParseAlterTableAddColumn(t *testing.T) {
  1373. stmt := parse(t, "ALTER TABLE users ADD COLUMN age INTEGER")
  1374. alter, ok := stmt.(*AlterTableStmt)
  1375. if !ok {
  1376. t.Fatalf("expected AlterTableStmt, got %T", stmt)
  1377. }
  1378. if alter.Table != "users" {
  1379. t.Errorf("expected table users, got %s", alter.Table)
  1380. }
  1381. action, ok := alter.Action.(*AddColumnAction)
  1382. if !ok {
  1383. t.Fatalf("expected AddColumnAction, got %T", alter.Action)
  1384. }
  1385. if action.Column.Name != "age" {
  1386. t.Errorf("expected column name age, got %s", action.Column.Name)
  1387. }
  1388. if action.Column.Type.Name != "INTEGER" {
  1389. t.Errorf("expected column type INTEGER, got %s", action.Column.Type.Name)
  1390. }
  1391. }
  1392. func TestParseAlterTableAddColumnOptional(t *testing.T) {
  1393. stmt := parse(t, "ALTER TABLE users ADD age INTEGER")
  1394. alter, ok := stmt.(*AlterTableStmt)
  1395. if !ok {
  1396. t.Fatalf("expected AlterTableStmt, got %T", stmt)
  1397. }
  1398. action, ok := alter.Action.(*AddColumnAction)
  1399. if !ok {
  1400. t.Fatalf("expected AddColumnAction, got %T", alter.Action)
  1401. }
  1402. if action.Column.Name != "age" {
  1403. t.Errorf("expected column name age, got %s", action.Column.Name)
  1404. }
  1405. }
  1406. func TestParseAlterTableDropColumn(t *testing.T) {
  1407. stmt := parse(t, "ALTER TABLE users DROP COLUMN email")
  1408. alter, ok := stmt.(*AlterTableStmt)
  1409. if !ok {
  1410. t.Fatalf("expected AlterTableStmt, got %T", stmt)
  1411. }
  1412. action, ok := alter.Action.(*DropColumnAction)
  1413. if !ok {
  1414. t.Fatalf("expected DropColumnAction, got %T", alter.Action)
  1415. }
  1416. if action.Column != "email" {
  1417. t.Errorf("expected column email, got %s", action.Column)
  1418. }
  1419. }
  1420. func TestParseAlterTableRename(t *testing.T) {
  1421. stmt := parse(t, "ALTER TABLE users RENAME TO customers")
  1422. alter, ok := stmt.(*AlterTableStmt)
  1423. if !ok {
  1424. t.Fatalf("expected AlterTableStmt, got %T", stmt)
  1425. }
  1426. action, ok := alter.Action.(*RenameTableAction)
  1427. if !ok {
  1428. t.Fatalf("expected RenameTableAction, got %T", alter.Action)
  1429. }
  1430. if action.NewName != "customers" {
  1431. t.Errorf("expected new name customers, got %s", action.NewName)
  1432. }
  1433. }
  1434. func TestParseAlterTableRenameColumn(t *testing.T) {
  1435. stmt := parse(t, "ALTER TABLE users RENAME COLUMN name TO full_name")
  1436. alter, ok := stmt.(*AlterTableStmt)
  1437. if !ok {
  1438. t.Fatalf("expected AlterTableStmt, got %T", stmt)
  1439. }
  1440. action, ok := alter.Action.(*RenameColumnAction)
  1441. if !ok {
  1442. t.Fatalf("expected RenameColumnAction, got %T", alter.Action)
  1443. }
  1444. if action.OldName != "name" {
  1445. t.Errorf("expected old name 'name', got %s", action.OldName)
  1446. }
  1447. if action.NewName != "full_name" {
  1448. t.Errorf("expected new name 'full_name', got %s", action.NewName)
  1449. }
  1450. }
  1451. // ATTACH/DETACH DATABASE tests
  1452. func TestParseAttach(t *testing.T) {
  1453. stmt := parse(t, "ATTACH DATABASE 'test.db' AS testdb")
  1454. attach, ok := stmt.(*AttachStmt)
  1455. if !ok {
  1456. t.Fatalf("expected AttachStmt, got %T", stmt)
  1457. }
  1458. if attach.FilePath != "test.db" {
  1459. t.Errorf("expected file path 'test.db', got '%s'", attach.FilePath)
  1460. }
  1461. if attach.Alias != "testdb" {
  1462. t.Errorf("expected alias 'testdb', got '%s'", attach.Alias)
  1463. }
  1464. }
  1465. func TestParseAttachOptional(t *testing.T) {
  1466. stmt := parse(t, "ATTACH 'another.db' AS other")
  1467. attach, ok := stmt.(*AttachStmt)
  1468. if !ok {
  1469. t.Fatalf("expected AttachStmt, got %T", stmt)
  1470. }
  1471. if attach.FilePath != "another.db" {
  1472. t.Errorf("expected file path 'another.db', got '%s'", attach.FilePath)
  1473. }
  1474. if attach.Alias != "other" {
  1475. t.Errorf("expected alias 'other', got '%s'", attach.Alias)
  1476. }
  1477. }
  1478. func TestParseDetach(t *testing.T) {
  1479. stmt := parse(t, "DETACH DATABASE testdb")
  1480. detach, ok := stmt.(*DetachStmt)
  1481. if !ok {
  1482. t.Fatalf("expected DetachStmt, got %T", stmt)
  1483. }
  1484. if detach.Alias != "testdb" {
  1485. t.Errorf("expected alias 'testdb', got '%s'", detach.Alias)
  1486. }
  1487. }
  1488. func TestParseDetachOptional(t *testing.T) {
  1489. stmt := parse(t, "DETACH other")
  1490. detach, ok := stmt.(*DetachStmt)
  1491. if !ok {
  1492. t.Fatalf("expected DetachStmt, got %T", stmt)
  1493. }
  1494. if detach.Alias != "other" {
  1495. t.Errorf("expected alias 'other', got '%s'", detach.Alias)
  1496. }
  1497. }