executor_test.go 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593
  1. package executor
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. "github.com/danfragoso/pizzasql-next/pkg/lexer"
  7. "github.com/danfragoso/pizzasql-next/pkg/parser"
  8. "github.com/danfragoso/pizzasql-next/pkg/storage"
  9. )
  10. func parse(t *testing.T, sql string) parser.Statement {
  11. t.Helper()
  12. l := lexer.New(sql)
  13. p := parser.New(l)
  14. stmt, err := p.Parse()
  15. if err != nil {
  16. t.Fatalf("parse error: %v", err)
  17. }
  18. return stmt
  19. }
  20. // execSQL parses and executes a SQL string, used by benchmarks
  21. func execSQL(exec *Executor, sql string) (*Result, error) {
  22. l := lexer.New(sql)
  23. p := parser.New(l)
  24. stmt, err := p.Parse()
  25. if err != nil {
  26. return nil, fmt.Errorf("parse error: %w", err)
  27. }
  28. return exec.Execute(stmt)
  29. }
  30. // Test expression evaluation without database
  31. func TestEvalLiteral(t *testing.T) {
  32. exec := &Executor{}
  33. tests := []struct {
  34. input string
  35. expected interface{}
  36. }{
  37. {"42", int64(42)},
  38. {"3.14", 3.14},
  39. {"'hello'", "hello"},
  40. {"TRUE", true},
  41. {"FALSE", false},
  42. }
  43. for _, tt := range tests {
  44. t.Run(tt.input, func(t *testing.T) {
  45. stmt := parse(t, "SELECT "+tt.input)
  46. sel := stmt.(*parser.SelectStmt)
  47. val, err := exec.evalExpr(sel.Columns[0].Expr, nil)
  48. if err != nil {
  49. t.Errorf("evalExpr error: %v", err)
  50. return
  51. }
  52. if val != tt.expected {
  53. t.Errorf("expected %v (%T), got %v (%T)", tt.expected, tt.expected, val, val)
  54. }
  55. })
  56. }
  57. }
  58. func TestEvalArithmetic(t *testing.T) {
  59. exec := &Executor{}
  60. tests := []struct {
  61. input string
  62. expected float64
  63. }{
  64. {"1 + 2", 3},
  65. {"5 - 3", 2},
  66. {"4 * 3", 12},
  67. {"10 / 2", 5},
  68. {"1 + 2 * 3", 7},
  69. {"(1 + 2) * 3", 9},
  70. {"-5", -5},
  71. {"10 % 3", 1},
  72. }
  73. for _, tt := range tests {
  74. t.Run(tt.input, func(t *testing.T) {
  75. stmt := parse(t, "SELECT "+tt.input)
  76. sel := stmt.(*parser.SelectStmt)
  77. val, err := exec.evalExpr(sel.Columns[0].Expr, nil)
  78. if err != nil {
  79. t.Errorf("evalExpr error: %v", err)
  80. return
  81. }
  82. if toFloat(val) != tt.expected {
  83. t.Errorf("expected %v, got %v", tt.expected, val)
  84. }
  85. })
  86. }
  87. }
  88. func TestEvalConstantWhereClause(t *testing.T) {
  89. // Test constant WHERE clauses that don't reference any columns
  90. tests := []struct {
  91. name string
  92. expr string
  93. expected bool
  94. }{
  95. {"NULL IS NULL", "NULL IS NULL", true},
  96. {"NULL IS NOT NULL", "NULL IS NOT NULL", false},
  97. {"NOT NULL IS NOT NULL", "NOT NULL IS NOT NULL", true},
  98. {"79 IS NOT NULL", "79 IS NOT NULL", true},
  99. {"79 IS NULL", "79 IS NULL", false},
  100. {"+ 79 IS NOT NULL", "+ 79 IS NOT NULL", true},
  101. {"- 78 IS NOT NULL", "- 78 IS NOT NULL", true},
  102. }
  103. exec := &Executor{}
  104. for _, tt := range tests {
  105. t.Run(tt.name, func(t *testing.T) {
  106. stmt := parse(t, "SELECT 1 WHERE "+tt.expr)
  107. sel := stmt.(*parser.SelectStmt)
  108. val, err := exec.evalExpr(sel.Where, nil)
  109. if err != nil {
  110. t.Fatalf("evalExpr error: %v", err)
  111. }
  112. result := toBool(val)
  113. if result != tt.expected {
  114. t.Errorf("expected %v, got %v", tt.expected, result)
  115. }
  116. })
  117. }
  118. }
  119. func TestConstantWhereClauseWithTable(t *testing.T) {
  120. // This test requires a real database connection
  121. // Skip if not available
  122. tests := []struct {
  123. name string
  124. whereClause string
  125. expectAllRows bool
  126. expectNoRows bool
  127. }{
  128. {"WHERE NULL IS NULL", "NULL IS NULL", true, false},
  129. {"WHERE NULL IS NOT NULL", "NULL IS NOT NULL", false, true},
  130. {"WHERE NOT NULL IS NOT NULL", "NOT NULL IS NOT NULL", true, false},
  131. {"WHERE 79 IS NOT NULL", "79 IS NOT NULL", true, false},
  132. {"WHERE 79 IS NULL", "79 IS NULL", false, true},
  133. {"WHERE + 79 IS NOT NULL", "+ 79 IS NOT NULL", true, false},
  134. {"WHERE - 78 IS NOT NULL", "- 78 IS NOT NULL", true, false},
  135. {"WHERE 1 = 1", "1 = 1", true, false},
  136. {"WHERE 1 = 0", "1 = 0", false, true},
  137. {"WHERE TRUE", "TRUE", true, false},
  138. {"WHERE FALSE", "FALSE", false, true},
  139. }
  140. for _, tt := range tests {
  141. t.Run(tt.name, func(t *testing.T) {
  142. // Parse the WHERE clause
  143. stmt := parse(t, "SELECT col0 FROM test WHERE "+tt.whereClause)
  144. sel := stmt.(*parser.SelectStmt)
  145. // Check that the WHERE clause doesn't reference any columns
  146. refs := collectColumnRefs(sel.Where)
  147. if len(refs) != 0 {
  148. t.Errorf("expected constant WHERE clause (no column refs), got %d refs", len(refs))
  149. }
  150. // Create a minimal executor to test constant evaluation
  151. exec := &Executor{}
  152. val, err := exec.evalExpr(sel.Where, nil)
  153. if err != nil {
  154. t.Fatalf("evalExpr error: %v", err)
  155. }
  156. result := toBool(val)
  157. if tt.expectAllRows && !result {
  158. t.Errorf("expected WHERE to evaluate to TRUE (select all rows), got FALSE")
  159. }
  160. if tt.expectNoRows && result {
  161. t.Errorf("expected WHERE to evaluate to FALSE (select no rows), got TRUE")
  162. }
  163. })
  164. }
  165. }
  166. func TestComparison(t *testing.T) {
  167. exec := &Executor{}
  168. tests := []struct {
  169. input string
  170. expected bool
  171. }{
  172. {"1 = 1", true},
  173. {"1 = 2", false},
  174. {"1 <> 2", true},
  175. {"1 < 2", true},
  176. {"2 > 1", true},
  177. {"1 <= 1", true},
  178. {"1 >= 1", true},
  179. {"'a' = 'a'", true},
  180. {"'a' < 'b'", true},
  181. }
  182. for _, tt := range tests {
  183. t.Run(tt.input, func(t *testing.T) {
  184. stmt := parse(t, "SELECT "+tt.input)
  185. sel := stmt.(*parser.SelectStmt)
  186. val, err := exec.evalExpr(sel.Columns[0].Expr, nil)
  187. if err != nil {
  188. t.Errorf("evalExpr error: %v", err)
  189. return
  190. }
  191. if toBool(val) != tt.expected {
  192. t.Errorf("expected %v, got %v", tt.expected, val)
  193. }
  194. })
  195. }
  196. }
  197. func TestEvalLogical(t *testing.T) {
  198. exec := &Executor{}
  199. tests := []struct {
  200. input string
  201. expected bool
  202. }{
  203. {"TRUE AND TRUE", true},
  204. {"TRUE AND FALSE", false},
  205. {"TRUE OR FALSE", true},
  206. {"FALSE OR FALSE", false},
  207. {"NOT TRUE", false},
  208. {"NOT FALSE", true},
  209. {"1 = 1 AND 2 = 2", true},
  210. {"1 = 1 OR 1 = 2", true},
  211. }
  212. for _, tt := range tests {
  213. t.Run(tt.input, func(t *testing.T) {
  214. stmt := parse(t, "SELECT "+tt.input)
  215. sel := stmt.(*parser.SelectStmt)
  216. val, err := exec.evalExpr(sel.Columns[0].Expr, nil)
  217. if err != nil {
  218. t.Errorf("evalExpr error: %v", err)
  219. return
  220. }
  221. if toBool(val) != tt.expected {
  222. t.Errorf("expected %v, got %v", tt.expected, val)
  223. }
  224. })
  225. }
  226. }
  227. func TestEvalFunctions(t *testing.T) {
  228. exec := &Executor{}
  229. tests := []struct {
  230. input string
  231. expected interface{}
  232. }{
  233. {"UPPER('hello')", "HELLO"},
  234. {"LOWER('HELLO')", "hello"},
  235. {"LENGTH('hello')", int64(5)},
  236. {"ABS(-5)", float64(5)},
  237. {"COALESCE(NULL, 'default')", "default"},
  238. {"COALESCE('value', 'default')", "value"},
  239. {"NULLIF(1, 1)", nil},
  240. {"NULLIF(1, 2)", int64(1)},
  241. {"IFNULL(NULL, 'default')", "default"},
  242. {"IFNULL('value', 'default')", "value"},
  243. {"TYPEOF(42)", "integer"},
  244. {"TYPEOF(3.14)", "real"},
  245. {"TYPEOF('hello')", "text"},
  246. {"TYPEOF(NULL)", "null"},
  247. {"TRIM(' hello ')", "hello"},
  248. {"SUBSTR('hello', 2, 3)", "ell"},
  249. {"REPLACE('hello', 'l', 'L')", "heLLo"},
  250. }
  251. for _, tt := range tests {
  252. t.Run(tt.input, func(t *testing.T) {
  253. stmt := parse(t, "SELECT "+tt.input)
  254. sel := stmt.(*parser.SelectStmt)
  255. val, err := exec.evalExpr(sel.Columns[0].Expr, nil)
  256. if err != nil {
  257. t.Errorf("evalExpr error: %v", err)
  258. return
  259. }
  260. if val != tt.expected {
  261. t.Errorf("expected %v (%T), got %v (%T)", tt.expected, tt.expected, val, val)
  262. }
  263. })
  264. }
  265. }
  266. func TestEvalCase(t *testing.T) {
  267. exec := &Executor{}
  268. tests := []struct {
  269. input string
  270. expected interface{}
  271. }{
  272. {"CASE WHEN TRUE THEN 'yes' ELSE 'no' END", "yes"},
  273. {"CASE WHEN FALSE THEN 'yes' ELSE 'no' END", "no"},
  274. {"CASE WHEN 1 = 1 THEN 'one' WHEN 1 = 2 THEN 'two' ELSE 'other' END", "one"},
  275. {"CASE 1 WHEN 1 THEN 'one' WHEN 2 THEN 'two' ELSE 'other' END", "one"},
  276. {"CASE 2 WHEN 1 THEN 'one' WHEN 2 THEN 'two' ELSE 'other' END", "two"},
  277. }
  278. for _, tt := range tests {
  279. t.Run(tt.input, func(t *testing.T) {
  280. stmt := parse(t, "SELECT "+tt.input)
  281. sel := stmt.(*parser.SelectStmt)
  282. val, err := exec.evalExpr(sel.Columns[0].Expr, nil)
  283. if err != nil {
  284. t.Errorf("evalExpr error: %v", err)
  285. return
  286. }
  287. if val != tt.expected {
  288. t.Errorf("expected %v, got %v", tt.expected, val)
  289. }
  290. })
  291. }
  292. }
  293. func TestEvalIn(t *testing.T) {
  294. exec := &Executor{}
  295. tests := []struct {
  296. input string
  297. expected bool
  298. }{
  299. {"1 IN (1, 2, 3)", true},
  300. {"4 IN (1, 2, 3)", false},
  301. {"1 NOT IN (1, 2, 3)", false},
  302. {"4 NOT IN (1, 2, 3)", true},
  303. {"'a' IN ('a', 'b', 'c')", true},
  304. }
  305. for _, tt := range tests {
  306. t.Run(tt.input, func(t *testing.T) {
  307. stmt := parse(t, "SELECT "+tt.input)
  308. sel := stmt.(*parser.SelectStmt)
  309. val, err := exec.evalExpr(sel.Columns[0].Expr, nil)
  310. if err != nil {
  311. t.Errorf("evalExpr error: %v", err)
  312. return
  313. }
  314. if toBool(val) != tt.expected {
  315. t.Errorf("expected %v, got %v", tt.expected, val)
  316. }
  317. })
  318. }
  319. }
  320. func TestEvalBetween(t *testing.T) {
  321. exec := &Executor{}
  322. tests := []struct {
  323. input string
  324. expected bool
  325. }{
  326. {"5 BETWEEN 1 AND 10", true},
  327. {"0 BETWEEN 1 AND 10", false},
  328. {"11 BETWEEN 1 AND 10", false},
  329. {"5 NOT BETWEEN 1 AND 10", false},
  330. {"0 NOT BETWEEN 1 AND 10", true},
  331. }
  332. for _, tt := range tests {
  333. t.Run(tt.input, func(t *testing.T) {
  334. stmt := parse(t, "SELECT "+tt.input)
  335. sel := stmt.(*parser.SelectStmt)
  336. val, err := exec.evalExpr(sel.Columns[0].Expr, nil)
  337. if err != nil {
  338. t.Errorf("evalExpr error: %v", err)
  339. return
  340. }
  341. if toBool(val) != tt.expected {
  342. t.Errorf("expected %v, got %v", tt.expected, val)
  343. }
  344. })
  345. }
  346. }
  347. func TestEvalLike(t *testing.T) {
  348. exec := &Executor{}
  349. tests := []struct {
  350. input string
  351. expected bool
  352. }{
  353. {"'hello' LIKE 'hello'", true},
  354. {"'hello' LIKE 'h%'", true},
  355. {"'hello' LIKE '%o'", true},
  356. {"'hello' LIKE '%ll%'", true},
  357. {"'hello' LIKE 'h_llo'", true},
  358. {"'hello' LIKE 'world'", false},
  359. {"'hello' NOT LIKE 'world'", true},
  360. }
  361. for _, tt := range tests {
  362. t.Run(tt.input, func(t *testing.T) {
  363. stmt := parse(t, "SELECT "+tt.input)
  364. sel := stmt.(*parser.SelectStmt)
  365. val, err := exec.evalExpr(sel.Columns[0].Expr, nil)
  366. if err != nil {
  367. t.Errorf("evalExpr error: %v", err)
  368. return
  369. }
  370. if toBool(val) != tt.expected {
  371. t.Errorf("expected %v, got %v", tt.expected, val)
  372. }
  373. })
  374. }
  375. }
  376. func TestEvalIsNull(t *testing.T) {
  377. exec := &Executor{}
  378. tests := []struct {
  379. input string
  380. expected bool
  381. }{
  382. {"NULL IS NULL", true},
  383. {"1 IS NULL", false},
  384. {"NULL IS NOT NULL", false},
  385. {"1 IS NOT NULL", true},
  386. }
  387. for _, tt := range tests {
  388. t.Run(tt.input, func(t *testing.T) {
  389. stmt := parse(t, "SELECT "+tt.input)
  390. sel := stmt.(*parser.SelectStmt)
  391. val, err := exec.evalExpr(sel.Columns[0].Expr, nil)
  392. if err != nil {
  393. t.Errorf("evalExpr error: %v", err)
  394. return
  395. }
  396. if toBool(val) != tt.expected {
  397. t.Errorf("expected %v, got %v", tt.expected, val)
  398. }
  399. })
  400. }
  401. }
  402. func TestEvalCast(t *testing.T) {
  403. exec := &Executor{}
  404. tests := []struct {
  405. input string
  406. expected interface{}
  407. }{
  408. {"CAST(3.14 AS INTEGER)", int64(3)},
  409. {"CAST(42 AS REAL)", float64(42)},
  410. {"CAST(123 AS TEXT)", "123"},
  411. }
  412. for _, tt := range tests {
  413. t.Run(tt.input, func(t *testing.T) {
  414. stmt := parse(t, "SELECT "+tt.input)
  415. sel := stmt.(*parser.SelectStmt)
  416. val, err := exec.evalExpr(sel.Columns[0].Expr, nil)
  417. if err != nil {
  418. t.Errorf("evalExpr error: %v", err)
  419. return
  420. }
  421. if val != tt.expected {
  422. t.Errorf("expected %v (%T), got %v (%T)", tt.expected, tt.expected, val, val)
  423. }
  424. })
  425. }
  426. }
  427. func TestEvalWithRow(t *testing.T) {
  428. exec := &Executor{}
  429. row := map[string]interface{}{
  430. "id": int64(1),
  431. "name": "John",
  432. "age": 30,
  433. "active": true,
  434. }
  435. tests := []struct {
  436. input string
  437. expected interface{}
  438. }{
  439. {"id", int64(1)},
  440. {"name", "John"},
  441. {"age", 30},
  442. {"active", true},
  443. {"id + 1", float64(2)},
  444. {"age * 2", float64(60)},
  445. {"name = 'John'", true},
  446. {"age > 25", true},
  447. {"active AND age > 20", true},
  448. }
  449. for _, tt := range tests {
  450. t.Run(tt.input, func(t *testing.T) {
  451. stmt := parse(t, "SELECT "+tt.input)
  452. sel := stmt.(*parser.SelectStmt)
  453. val, err := exec.evalExpr(sel.Columns[0].Expr, row)
  454. if err != nil {
  455. t.Errorf("evalExpr error: %v", err)
  456. return
  457. }
  458. // Handle numeric comparisons
  459. if expected, ok := tt.expected.(float64); ok {
  460. if toFloat(val) != expected {
  461. t.Errorf("expected %v, got %v", tt.expected, val)
  462. }
  463. } else if val != tt.expected {
  464. t.Errorf("expected %v (%T), got %v (%T)", tt.expected, tt.expected, val, val)
  465. }
  466. })
  467. }
  468. }
  469. func TestResultString(t *testing.T) {
  470. result := NewResult("SELECT")
  471. result.AddColumn("id")
  472. result.AddColumn("name")
  473. result.AddRow(int64(1), "Alice")
  474. result.AddRow(int64(2), "Bob")
  475. output := result.String()
  476. // Check that output contains expected elements
  477. if output == "" {
  478. t.Error("expected non-empty output")
  479. }
  480. if result.RowCount != 2 {
  481. t.Errorf("expected 2 rows, got %d", result.RowCount)
  482. }
  483. }
  484. func TestMatchLike(t *testing.T) {
  485. tests := []struct {
  486. s string
  487. pattern string
  488. expected bool
  489. }{
  490. {"hello", "hello", true},
  491. {"hello", "h%", true},
  492. {"hello", "%o", true},
  493. {"hello", "%ll%", true},
  494. {"hello", "h_llo", true},
  495. {"hello", "H%", true}, // case insensitive
  496. {"hello", "world", false},
  497. {"", "%", true},
  498. {"abc", "a%c", true},
  499. {"abc", "a_c", true},
  500. {"abc", "__c", true},
  501. {"abc", "___", true},
  502. {"abc", "____", false},
  503. }
  504. for _, tt := range tests {
  505. t.Run(tt.s+"_"+tt.pattern, func(t *testing.T) {
  506. got := matchLike(tt.s, tt.pattern)
  507. if got != tt.expected {
  508. t.Errorf("matchLike(%q, %q) = %v, want %v", tt.s, tt.pattern, got, tt.expected)
  509. }
  510. })
  511. }
  512. }
  513. // Phase 4: SQLite function tests
  514. func TestEvalSQLiteFunctions(t *testing.T) {
  515. exec := &Executor{}
  516. tests := []struct {
  517. input string
  518. expected interface{}
  519. isInt bool // for RANDOM which returns int64
  520. }{
  521. // PRINTF
  522. {"PRINTF('%d', 42)", "42", false},
  523. {"PRINTF('%s', 'hello')", "hello", false},
  524. {"PRINTF('%d + %d = %d', 1, 2, 3)", "1 + 2 = 3", false},
  525. // HEX
  526. {"HEX('ABC')", "414243", false},
  527. {"HEX('hello')", "68656C6C6F", false},
  528. // INSTR
  529. {"INSTR('hello world', 'world')", int64(7), false},
  530. {"INSTR('hello', 'x')", int64(0), false},
  531. {"INSTR('hello', 'l')", int64(3), false},
  532. // ROUND
  533. {"ROUND(3.14159)", float64(3), false},
  534. {"ROUND(3.14159, 2)", float64(3.14), false},
  535. {"ROUND(3.5)", float64(4), false},
  536. // CONCAT
  537. {"CONCAT('hello', ' ', 'world')", "hello world", false},
  538. {"CONCAT('a', 'b', 'c')", "abc", false},
  539. // MAX/MIN (scalar versions)
  540. {"MAX(1, 5, 3)", int64(5), false},
  541. {"MIN(1, 5, 3)", int64(1), false},
  542. {"MAX('a', 'c', 'b')", "c", false},
  543. }
  544. for _, tt := range tests {
  545. t.Run(tt.input, func(t *testing.T) {
  546. stmt := parse(t, "SELECT "+tt.input)
  547. sel := stmt.(*parser.SelectStmt)
  548. val, err := exec.evalExpr(sel.Columns[0].Expr, nil)
  549. if err != nil {
  550. t.Errorf("evalExpr error: %v", err)
  551. return
  552. }
  553. if val != tt.expected {
  554. t.Errorf("expected %v (%T), got %v (%T)", tt.expected, tt.expected, val, val)
  555. }
  556. })
  557. }
  558. }
  559. func TestEvalRandom(t *testing.T) {
  560. exec := &Executor{}
  561. stmt := parse(t, "SELECT RANDOM()")
  562. sel := stmt.(*parser.SelectStmt)
  563. val, err := exec.evalExpr(sel.Columns[0].Expr, nil)
  564. if err != nil {
  565. t.Fatalf("evalExpr error: %v", err)
  566. }
  567. // RANDOM() should return an int64
  568. if _, ok := val.(int64); !ok {
  569. t.Errorf("RANDOM() should return int64, got %T", val)
  570. }
  571. }
  572. func TestEvalGlob(t *testing.T) {
  573. exec := &Executor{}
  574. tests := []struct {
  575. input string
  576. expected bool
  577. }{
  578. {"GLOB('*.txt', 'file.txt')", true},
  579. {"GLOB('*.txt', 'file.doc')", false},
  580. {"GLOB('hello*', 'hello world')", true},
  581. {"GLOB('h?llo', 'hello')", true},
  582. {"GLOB('h?llo', 'hallo')", true},
  583. {"GLOB('[abc]*', 'apple')", true},
  584. {"GLOB('[abc]*', 'dog')", false},
  585. }
  586. for _, tt := range tests {
  587. t.Run(tt.input, func(t *testing.T) {
  588. stmt := parse(t, "SELECT "+tt.input)
  589. sel := stmt.(*parser.SelectStmt)
  590. val, err := exec.evalExpr(sel.Columns[0].Expr, nil)
  591. if err != nil {
  592. t.Errorf("evalExpr error: %v", err)
  593. return
  594. }
  595. if toBool(val) != tt.expected {
  596. t.Errorf("expected %v, got %v", tt.expected, val)
  597. }
  598. })
  599. }
  600. }
  601. func TestMatchGlob(t *testing.T) {
  602. tests := []struct {
  603. pattern string
  604. s string
  605. expected bool
  606. }{
  607. {"*", "anything", true},
  608. {"*", "", true},
  609. {"?", "a", true},
  610. {"?", "ab", false},
  611. {"a*b", "ab", true},
  612. {"a*b", "aXXXb", true},
  613. {"a*b", "aXXXc", false},
  614. {"[abc]", "a", true},
  615. {"[abc]", "d", false},
  616. {"[^abc]", "d", true},
  617. {"[^abc]", "a", false},
  618. {"*.go", "main.go", true},
  619. {"*.go", "main.txt", false},
  620. }
  621. for _, tt := range tests {
  622. t.Run(tt.pattern+"_"+tt.s, func(t *testing.T) {
  623. got := matchGlob(tt.pattern, tt.s)
  624. if got != tt.expected {
  625. t.Errorf("matchGlob(%q, %q) = %v, want %v", tt.pattern, tt.s, got, tt.expected)
  626. }
  627. })
  628. }
  629. }
  630. // Test subquery expressions
  631. func TestEvalSubqueryExpr(t *testing.T) {
  632. pool, err := storage.NewKVPool("localhost:8085", 5, 5*time.Second)
  633. if err != nil {
  634. t.Skip("PizzaKV not available, skipping subquery tests")
  635. }
  636. defer pool.Close()
  637. schema := storage.NewSchemaManager(pool, "test_subquery_db")
  638. table := storage.NewTableManager(pool, schema, "test_subquery_db")
  639. exec := New(schema, table)
  640. // Setup test tables
  641. execSQL(exec, "DROP TABLE IF EXISTS products")
  642. execSQL(exec, "DROP TABLE IF EXISTS categories")
  643. _, err = execSQL(exec, "CREATE TABLE categories (id INTEGER PRIMARY KEY, name TEXT)")
  644. if err != nil {
  645. t.Fatalf("failed to create categories: %v", err)
  646. }
  647. _, err = execSQL(exec, "CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT, category_id INTEGER, price REAL)")
  648. if err != nil {
  649. t.Fatalf("failed to create products: %v", err)
  650. }
  651. // Insert test data
  652. execSQL(exec, "INSERT INTO categories VALUES (1, 'Electronics')")
  653. execSQL(exec, "INSERT INTO categories VALUES (2, 'Books')")
  654. execSQL(exec, "INSERT INTO categories VALUES (3, 'Clothing')")
  655. execSQL(exec, "INSERT INTO products VALUES (1, 'Laptop', 1, 999.99)")
  656. execSQL(exec, "INSERT INTO products VALUES (2, 'Phone', 1, 599.99)")
  657. execSQL(exec, "INSERT INTO products VALUES (3, 'Novel', 2, 19.99)")
  658. execSQL(exec, "INSERT INTO products VALUES (4, 'T-Shirt', 3, 29.99)")
  659. // Test scalar subquery
  660. t.Run("scalar_subquery", func(t *testing.T) {
  661. result, err := execSQL(exec, "SELECT (SELECT MAX(price) FROM products)")
  662. if err != nil {
  663. t.Fatalf("query failed: %v", err)
  664. }
  665. if result.RowCount != 1 {
  666. t.Errorf("expected 1 row, got %d", result.RowCount)
  667. }
  668. if result.Rows[0][0] != 999.99 {
  669. t.Errorf("expected 999.99, got %v", result.Rows[0][0])
  670. }
  671. })
  672. // Test IN subquery
  673. t.Run("in_subquery", func(t *testing.T) {
  674. result, err := execSQL(exec, "SELECT name FROM products WHERE category_id IN (SELECT id FROM categories WHERE name = 'Electronics')")
  675. if err != nil {
  676. t.Fatalf("query failed: %v", err)
  677. }
  678. if result.RowCount != 2 {
  679. t.Errorf("expected 2 rows, got %d", result.RowCount)
  680. }
  681. })
  682. // Test NOT IN subquery
  683. t.Run("not_in_subquery", func(t *testing.T) {
  684. result, err := execSQL(exec, "SELECT name FROM products WHERE category_id NOT IN (SELECT id FROM categories WHERE name = 'Electronics')")
  685. if err != nil {
  686. t.Fatalf("query failed: %v", err)
  687. }
  688. if result.RowCount != 2 {
  689. t.Errorf("expected 2 rows, got %d", result.RowCount)
  690. }
  691. })
  692. // Test EXISTS subquery
  693. t.Run("exists_subquery", func(t *testing.T) {
  694. result, err := execSQL(exec, "SELECT EXISTS (SELECT 1 FROM products WHERE price > 500)")
  695. if err != nil {
  696. t.Fatalf("query failed: %v", err)
  697. }
  698. if result.RowCount != 1 {
  699. t.Errorf("expected 1 row, got %d", result.RowCount)
  700. }
  701. if result.Rows[0][0] != true {
  702. t.Errorf("expected true, got %v", result.Rows[0][0])
  703. }
  704. })
  705. // Test EXISTS with no matches
  706. t.Run("exists_no_match", func(t *testing.T) {
  707. result, err := execSQL(exec, "SELECT EXISTS (SELECT 1 FROM products WHERE price > 10000)")
  708. if err != nil {
  709. t.Fatalf("query failed: %v", err)
  710. }
  711. if result.Rows[0][0] != false {
  712. t.Errorf("expected false, got %v", result.Rows[0][0])
  713. }
  714. })
  715. // Cleanup
  716. execSQL(exec, "DROP TABLE IF EXISTS products")
  717. execSQL(exec, "DROP TABLE IF EXISTS categories")
  718. }
  719. // Benchmark
  720. func BenchmarkEvalExpr(b *testing.B) {
  721. exec := &Executor{}
  722. stmt := parse(&testing.T{}, "SELECT (1 + 2) * 3 - 4 / 2")
  723. sel := stmt.(*parser.SelectStmt)
  724. expr := sel.Columns[0].Expr
  725. b.ResetTimer()
  726. for i := 0; i < b.N; i++ {
  727. exec.evalExpr(expr, nil)
  728. }
  729. }
  730. // BenchmarkIndexVsNoIndex compares query performance with and without indexes.
  731. // Requires a running PizzaKV instance at localhost:8085.
  732. func BenchmarkIndexVsNoIndex(b *testing.B) {
  733. pool, err := storage.NewKVPool("localhost:8085", 5, 5*time.Second)
  734. if err != nil {
  735. b.Skip("PizzaKV not available, skipping index benchmark")
  736. }
  737. defer pool.Close()
  738. schema := storage.NewSchemaManager(pool, "bench_db")
  739. table := storage.NewTableManager(pool, schema, "bench_db")
  740. exec := New(schema, table)
  741. // Cleanup first to ensure fresh state
  742. execSQL(exec, "DROP INDEX IF EXISTS idx_bench_status")
  743. execSQL(exec, "DROP TABLE IF EXISTS bench_users")
  744. _, err = execSQL(exec, "CREATE TABLE bench_users (id INTEGER PRIMARY KEY, name TEXT, email TEXT, status TEXT)")
  745. if err != nil {
  746. b.Fatalf("failed to create table: %v", err)
  747. }
  748. // Insert 1000 rows
  749. statuses := []string{"active", "inactive", "pending", "suspended"}
  750. for i := 1; i <= 1000; i++ {
  751. status := statuses[i%len(statuses)]
  752. _, err := execSQL(exec, fmt.Sprintf("INSERT INTO bench_users (id, name, email, status) VALUES (%d, 'User%d', 'user%d@test.com', '%s')", i, i, i, status))
  753. if err != nil {
  754. b.Fatalf("failed to insert row %d: %v", i, err)
  755. }
  756. }
  757. // Benchmark WITHOUT index
  758. b.Run("NoIndex", func(b *testing.B) {
  759. for i := 0; i < b.N; i++ {
  760. _, err := execSQL(exec, "SELECT * FROM bench_users WHERE status = 'active'")
  761. if err != nil {
  762. b.Fatalf("query failed: %v", err)
  763. }
  764. }
  765. })
  766. // Create index on status column
  767. _, err = execSQL(exec, "CREATE INDEX idx_bench_status ON bench_users (status)")
  768. if err != nil {
  769. b.Fatalf("failed to create index: %v", err)
  770. }
  771. // Benchmark WITH index
  772. b.Run("WithIndex", func(b *testing.B) {
  773. for i := 0; i < b.N; i++ {
  774. _, err := execSQL(exec, "SELECT * FROM bench_users WHERE status = 'active'")
  775. if err != nil {
  776. b.Fatalf("query failed: %v", err)
  777. }
  778. }
  779. })
  780. // Cleanup
  781. execSQL(exec, "DROP INDEX IF EXISTS idx_bench_status")
  782. execSQL(exec, "DROP TABLE IF EXISTS bench_users")
  783. }
  784. // BenchmarkIndexVsNoIndexLargeTable tests with more rows
  785. func BenchmarkIndexVsNoIndexLargeTable(b *testing.B) {
  786. pool, err := storage.NewKVPool("localhost:8085", 5, 5*time.Second)
  787. if err != nil {
  788. b.Skip("PizzaKV not available, skipping index benchmark")
  789. }
  790. defer pool.Close()
  791. schema := storage.NewSchemaManager(pool, "bench_db")
  792. table := storage.NewTableManager(pool, schema, "bench_db")
  793. exec := New(schema, table)
  794. // Cleanup first to ensure fresh state
  795. execSQL(exec, "DROP INDEX IF EXISTS idx_bench_category")
  796. execSQL(exec, "DROP TABLE IF EXISTS bench_large")
  797. _, err = execSQL(exec, "CREATE TABLE bench_large (id INTEGER PRIMARY KEY, category INTEGER, value TEXT)")
  798. if err != nil {
  799. b.Fatalf("failed to create table: %v", err)
  800. }
  801. // Insert 5000 rows with 100 distinct categories
  802. for i := 1; i <= 5000; i++ {
  803. category := i % 100
  804. _, err := execSQL(exec, fmt.Sprintf("INSERT INTO bench_large (id, category, value) VALUES (%d, %d, 'value_%d')", i, category, i))
  805. if err != nil {
  806. b.Fatalf("failed to insert row %d: %v", i, err)
  807. }
  808. }
  809. // Benchmark WITHOUT index (should scan all 5000 rows)
  810. b.Run("NoIndex_5000rows", func(b *testing.B) {
  811. for i := 0; i < b.N; i++ {
  812. _, err := execSQL(exec, "SELECT * FROM bench_large WHERE category = 42")
  813. if err != nil {
  814. b.Fatalf("query failed: %v", err)
  815. }
  816. }
  817. })
  818. // Create index
  819. _, err = execSQL(exec, "CREATE INDEX idx_bench_category ON bench_large (category)")
  820. if err != nil {
  821. b.Fatalf("failed to create index: %v", err)
  822. }
  823. // Benchmark WITH index (should only retrieve ~50 rows)
  824. b.Run("WithIndex_5000rows", func(b *testing.B) {
  825. for i := 0; i < b.N; i++ {
  826. _, err := execSQL(exec, "SELECT * FROM bench_large WHERE category = 42")
  827. if err != nil {
  828. b.Fatalf("query failed: %v", err)
  829. }
  830. }
  831. })
  832. // Cleanup
  833. execSQL(exec, "DROP INDEX IF EXISTS idx_bench_category")
  834. execSQL(exec, "DROP TABLE IF EXISTS bench_large")
  835. }
  836. // Test transaction statements
  837. func TestTransactions(t *testing.T) {
  838. pool, err := storage.NewKVPool("localhost:8085", 5, 5*time.Second)
  839. if err != nil {
  840. t.Skip("PizzaKV not available, skipping transaction tests")
  841. }
  842. defer pool.Close()
  843. schema := storage.NewSchemaManager(pool, "test_tx_db")
  844. table := storage.NewTableManager(pool, schema, "test_tx_db")
  845. exec := New(schema, table)
  846. // Setup test table
  847. execSQL(exec, "DROP TABLE IF EXISTS tx_test")
  848. _, err = execSQL(exec, "CREATE TABLE tx_test (id INTEGER PRIMARY KEY, value TEXT)")
  849. if err != nil {
  850. t.Fatalf("failed to create table: %v", err)
  851. }
  852. t.Run("begin_transaction", func(t *testing.T) {
  853. result, err := execSQL(exec, "BEGIN")
  854. if err != nil {
  855. t.Fatalf("BEGIN failed: %v", err)
  856. }
  857. if result.CommandTag != "BEGIN" {
  858. t.Errorf("expected StatementType 'BEGIN', got '%s'", result.CommandTag)
  859. }
  860. if !exec.inTransaction {
  861. t.Error("expected inTransaction to be true")
  862. }
  863. // Rollback to reset state
  864. execSQL(exec, "ROLLBACK")
  865. })
  866. t.Run("begin_transaction_keyword", func(t *testing.T) {
  867. result, err := execSQL(exec, "BEGIN TRANSACTION")
  868. if err != nil {
  869. t.Fatalf("BEGIN TRANSACTION failed: %v", err)
  870. }
  871. if result.CommandTag != "BEGIN" {
  872. t.Errorf("expected StatementType 'BEGIN', got '%s'", result.CommandTag)
  873. }
  874. execSQL(exec, "ROLLBACK")
  875. })
  876. t.Run("commit_transaction", func(t *testing.T) {
  877. // Clean up any previous data
  878. execSQL(exec, "DELETE FROM tx_test WHERE id = 1")
  879. execSQL(exec, "BEGIN")
  880. _, err := execSQL(exec, "INSERT INTO tx_test (id, value) VALUES (1, 'test1')")
  881. if err != nil {
  882. t.Fatalf("INSERT failed: %v", err)
  883. }
  884. result, err := execSQL(exec, "COMMIT")
  885. if err != nil {
  886. t.Fatalf("COMMIT failed: %v", err)
  887. }
  888. if result.CommandTag != "COMMIT" {
  889. t.Errorf("expected StatementType 'COMMIT', got '%s'", result.CommandTag)
  890. }
  891. if exec.inTransaction {
  892. t.Error("expected inTransaction to be false after COMMIT")
  893. }
  894. // Verify data was committed
  895. checkResult, _ := execSQL(exec, "SELECT * FROM tx_test WHERE id = 1")
  896. if checkResult.RowCount != 1 {
  897. t.Errorf("expected 1 row after commit, got %d", checkResult.RowCount)
  898. }
  899. })
  900. t.Run("rollback_transaction", func(t *testing.T) {
  901. // Clean up any previous data
  902. execSQL(exec, "DELETE FROM tx_test WHERE id = 2")
  903. execSQL(exec, "BEGIN")
  904. _, err := execSQL(exec, "INSERT INTO tx_test (id, value) VALUES (2, 'test2')")
  905. if err != nil {
  906. t.Fatalf("INSERT failed: %v", err)
  907. }
  908. result, err := execSQL(exec, "ROLLBACK")
  909. if err != nil {
  910. t.Fatalf("ROLLBACK failed: %v", err)
  911. }
  912. if result.CommandTag != "ROLLBACK" {
  913. t.Errorf("expected StatementType 'ROLLBACK', got '%s'", result.CommandTag)
  914. }
  915. if exec.inTransaction {
  916. t.Error("expected inTransaction to be false after ROLLBACK")
  917. }
  918. // Verify data was NOT committed (rollback currently doesn't undo changes due to PizzaKV limitations)
  919. // This is a known limitation - the transaction log is built but rollback doesn't restore state
  920. checkResult, _ := execSQL(exec, "SELECT * FROM tx_test WHERE id = 2")
  921. // Note: In the current implementation, rollback doesn't actually undo changes
  922. // This test documents current behavior
  923. if checkResult.RowCount == 0 {
  924. t.Log("ROLLBACK successfully prevented data persistence (ideal)")
  925. } else {
  926. t.Log("ROLLBACK did not undo changes (current limitation)")
  927. }
  928. })
  929. t.Run("savepoint_create", func(t *testing.T) {
  930. execSQL(exec, "BEGIN")
  931. result, err := execSQL(exec, "SAVEPOINT sp1")
  932. if err != nil {
  933. t.Fatalf("SAVEPOINT failed: %v", err)
  934. }
  935. if result.CommandTag != "SAVEPOINT" {
  936. t.Errorf("expected StatementType 'SAVEPOINT', got '%s'", result.CommandTag)
  937. }
  938. if len(exec.savepoints) != 1 || exec.savepoints[0] != "sp1" {
  939. t.Errorf("expected savepoint 'sp1', got %v", exec.savepoints)
  940. }
  941. execSQL(exec, "ROLLBACK")
  942. })
  943. t.Run("nested_savepoints", func(t *testing.T) {
  944. execSQL(exec, "BEGIN")
  945. execSQL(exec, "SAVEPOINT sp1")
  946. execSQL(exec, "SAVEPOINT sp2")
  947. execSQL(exec, "SAVEPOINT sp3")
  948. if len(exec.savepoints) != 3 {
  949. t.Errorf("expected 3 savepoints, got %d", len(exec.savepoints))
  950. }
  951. if exec.savepoints[2] != "sp3" {
  952. t.Errorf("expected last savepoint to be 'sp3', got '%s'", exec.savepoints[2])
  953. }
  954. execSQL(exec, "ROLLBACK")
  955. })
  956. t.Run("rollback_to_savepoint", func(t *testing.T) {
  957. execSQL(exec, "BEGIN")
  958. execSQL(exec, "INSERT INTO tx_test (id, value) VALUES (10, 'before_sp')")
  959. execSQL(exec, "SAVEPOINT sp1")
  960. execSQL(exec, "INSERT INTO tx_test (id, value) VALUES (11, 'after_sp')")
  961. result, err := execSQL(exec, "ROLLBACK TO sp1")
  962. if err != nil {
  963. t.Fatalf("ROLLBACK TO failed: %v", err)
  964. }
  965. if result.CommandTag != "ROLLBACK" {
  966. t.Errorf("expected StatementType 'ROLLBACK', got '%s'", result.CommandTag)
  967. }
  968. // Should still be in transaction
  969. if !exec.inTransaction {
  970. t.Error("expected to still be in transaction after ROLLBACK TO")
  971. }
  972. execSQL(exec, "ROLLBACK")
  973. })
  974. t.Run("release_savepoint", func(t *testing.T) {
  975. execSQL(exec, "BEGIN")
  976. execSQL(exec, "SAVEPOINT sp1")
  977. execSQL(exec, "SAVEPOINT sp2")
  978. result, err := execSQL(exec, "RELEASE sp1")
  979. if err != nil {
  980. t.Fatalf("RELEASE failed: %v", err)
  981. }
  982. if result.CommandTag != "RELEASE" {
  983. t.Errorf("expected StatementType 'RELEASE', got '%s'", result.CommandTag)
  984. }
  985. // Releasing sp1 should also remove sp2 (all nested savepoints)
  986. if len(exec.savepoints) != 0 {
  987. t.Errorf("expected no savepoints after RELEASE, got %d", len(exec.savepoints))
  988. }
  989. execSQL(exec, "ROLLBACK")
  990. })
  991. t.Run("release_savepoint_explicit", func(t *testing.T) {
  992. execSQL(exec, "BEGIN")
  993. execSQL(exec, "SAVEPOINT sp1")
  994. result, err := execSQL(exec, "RELEASE SAVEPOINT sp1")
  995. if err != nil {
  996. t.Fatalf("RELEASE SAVEPOINT failed: %v", err)
  997. }
  998. if result.CommandTag != "RELEASE" {
  999. t.Errorf("expected StatementType 'RELEASE', got '%s'", result.CommandTag)
  1000. }
  1001. execSQL(exec, "ROLLBACK")
  1002. })
  1003. // Cleanup
  1004. execSQL(exec, "DROP TABLE IF EXISTS tx_test")
  1005. }
  1006. // Test subqueries in FROM clause
  1007. func TestSubqueryInFrom(t *testing.T) {
  1008. pool, err := storage.NewKVPool("localhost:8085", 5, 5*time.Second)
  1009. if err != nil {
  1010. t.Skip("PizzaKV not available, skipping subquery in FROM tests")
  1011. }
  1012. defer pool.Close()
  1013. schema := storage.NewSchemaManager(pool, "test_subquery_from_db")
  1014. table := storage.NewTableManager(pool, schema, "test_subquery_from_db")
  1015. exec := New(schema, table)
  1016. // Setup test table
  1017. execSQL(exec, "DROP TABLE IF EXISTS employees")
  1018. _, err = execSQL(exec, "CREATE TABLE employees (id INTEGER PRIMARY KEY, name TEXT, department TEXT, salary INTEGER)")
  1019. if err != nil {
  1020. t.Fatalf("failed to create table: %v", err)
  1021. }
  1022. // Insert test data
  1023. execSQL(exec, "INSERT INTO employees (id, name, department, salary) VALUES (1, 'Alice', 'Engineering', 100000)")
  1024. execSQL(exec, "INSERT INTO employees (id, name, department, salary) VALUES (2, 'Bob', 'Engineering', 90000)")
  1025. execSQL(exec, "INSERT INTO employees (id, name, department, salary) VALUES (3, 'Charlie', 'Sales', 80000)")
  1026. execSQL(exec, "INSERT INTO employees (id, name, department, salary) VALUES (4, 'Diana', 'Sales', 75000)")
  1027. t.Run("simple_subquery_from", func(t *testing.T) {
  1028. result, err := execSQL(exec, "SELECT * FROM (SELECT name, department FROM employees) AS emp")
  1029. if err != nil {
  1030. t.Fatalf("query failed: %v", err)
  1031. }
  1032. if result.RowCount != 4 {
  1033. t.Errorf("expected 4 rows, got %d", result.RowCount)
  1034. }
  1035. if len(result.Columns) != 2 {
  1036. t.Errorf("expected 2 columns, got %d", len(result.Columns))
  1037. }
  1038. })
  1039. t.Run("subquery_with_where", func(t *testing.T) {
  1040. result, err := execSQL(exec, "SELECT name FROM (SELECT id, name, salary FROM employees WHERE salary > 80000) AS high_earners")
  1041. if err != nil {
  1042. t.Fatalf("query failed: %v", err)
  1043. }
  1044. if result.RowCount != 2 {
  1045. t.Errorf("expected 2 rows, got %d", result.RowCount)
  1046. }
  1047. })
  1048. t.Run("subquery_with_outer_where", func(t *testing.T) {
  1049. result, err := execSQL(exec, "SELECT * FROM (SELECT name, department FROM employees) AS emp WHERE department = 'Engineering'")
  1050. if err != nil {
  1051. t.Fatalf("query failed: %v", err)
  1052. }
  1053. if result.RowCount != 2 {
  1054. t.Errorf("expected 2 rows, got %d", result.RowCount)
  1055. }
  1056. })
  1057. t.Run("subquery_select_specific_columns", func(t *testing.T) {
  1058. result, err := execSQL(exec, "SELECT name FROM (SELECT id, name, department FROM employees WHERE department = 'Sales') AS sales_emp")
  1059. if err != nil {
  1060. t.Fatalf("query failed: %v", err)
  1061. }
  1062. if result.RowCount != 2 {
  1063. t.Errorf("expected 2 rows, got %d", result.RowCount)
  1064. }
  1065. if len(result.Columns) != 1 || result.Columns[0] != "name" {
  1066. t.Errorf("expected column 'name', got %v", result.Columns)
  1067. }
  1068. })
  1069. t.Run("nested_subquery", func(t *testing.T) {
  1070. result, err := execSQL(exec, "SELECT * FROM (SELECT * FROM (SELECT name FROM employees) AS inner_q) AS outer_q")
  1071. if err != nil {
  1072. t.Fatalf("query failed: %v", err)
  1073. }
  1074. if result.RowCount != 4 {
  1075. t.Errorf("expected 4 rows, got %d", result.RowCount)
  1076. }
  1077. })
  1078. // Cleanup
  1079. execSQL(exec, "DROP TABLE IF EXISTS employees")
  1080. }
  1081. // Test ALTER TABLE statements
  1082. func TestAlterTable(t *testing.T) {
  1083. pool, err := storage.NewKVPool("localhost:8085", 5, 5*time.Second)
  1084. if err != nil {
  1085. t.Skip("PizzaKV not available, skipping ALTER TABLE tests")
  1086. }
  1087. defer pool.Close()
  1088. schema := storage.NewSchemaManager(pool, "test_alter_db")
  1089. table := storage.NewTableManager(pool, schema, "test_alter_db")
  1090. exec := New(schema, table)
  1091. // Setup test table
  1092. execSQL(exec, "DROP TABLE IF EXISTS test_alter")
  1093. _, err = execSQL(exec, "CREATE TABLE test_alter (id INTEGER PRIMARY KEY, name TEXT)")
  1094. if err != nil {
  1095. t.Fatalf("failed to create table: %v", err)
  1096. }
  1097. t.Run("add_column", func(t *testing.T) {
  1098. _, err := execSQL(exec, "ALTER TABLE test_alter ADD COLUMN age INTEGER")
  1099. if err != nil {
  1100. t.Fatalf("ALTER TABLE ADD COLUMN failed: %v", err)
  1101. }
  1102. // Verify column was added
  1103. tSchema, err := schema.GetSchema("test_alter")
  1104. if err != nil {
  1105. t.Fatalf("failed to get schema: %v", err)
  1106. }
  1107. found := false
  1108. for _, col := range tSchema.Columns {
  1109. if col.Name == "age" {
  1110. found = true
  1111. if col.Type != "INTEGER" {
  1112. t.Errorf("expected type INTEGER, got %s", col.Type)
  1113. }
  1114. break
  1115. }
  1116. }
  1117. if !found {
  1118. t.Error("column 'age' not found after ADD COLUMN")
  1119. }
  1120. })
  1121. t.Run("add_column_optional_keyword", func(t *testing.T) {
  1122. _, err := execSQL(exec, "ALTER TABLE test_alter ADD email TEXT")
  1123. if err != nil {
  1124. t.Fatalf("ALTER TABLE ADD failed: %v", err)
  1125. }
  1126. // Verify column was added
  1127. tSchema, _ := schema.GetSchema("test_alter")
  1128. found := false
  1129. for _, col := range tSchema.Columns {
  1130. if col.Name == "email" {
  1131. found = true
  1132. break
  1133. }
  1134. }
  1135. if !found {
  1136. t.Error("column 'email' not found after ADD")
  1137. }
  1138. })
  1139. t.Run("rename_column", func(t *testing.T) {
  1140. _, err := execSQL(exec, "ALTER TABLE test_alter RENAME COLUMN name TO full_name")
  1141. if err != nil {
  1142. t.Fatalf("ALTER TABLE RENAME COLUMN failed: %v", err)
  1143. }
  1144. // Verify column was renamed
  1145. tSchema, _ := schema.GetSchema("test_alter")
  1146. hasOld := false
  1147. hasNew := false
  1148. for _, col := range tSchema.Columns {
  1149. if col.Name == "name" {
  1150. hasOld = true
  1151. }
  1152. if col.Name == "full_name" {
  1153. hasNew = true
  1154. }
  1155. }
  1156. if hasOld {
  1157. t.Error("old column 'name' still exists after RENAME COLUMN")
  1158. }
  1159. if !hasNew {
  1160. t.Error("new column 'full_name' not found after RENAME COLUMN")
  1161. }
  1162. })
  1163. t.Run("drop_column", func(t *testing.T) {
  1164. _, err := execSQL(exec, "ALTER TABLE test_alter DROP COLUMN email")
  1165. if err != nil {
  1166. t.Fatalf("ALTER TABLE DROP COLUMN failed: %v", err)
  1167. }
  1168. // Verify column was dropped
  1169. tSchema, _ := schema.GetSchema("test_alter")
  1170. for _, col := range tSchema.Columns {
  1171. if col.Name == "email" {
  1172. t.Error("column 'email' still exists after DROP COLUMN")
  1173. }
  1174. }
  1175. })
  1176. t.Run("rename_table", func(t *testing.T) {
  1177. _, err := execSQL(exec, "ALTER TABLE test_alter RENAME TO test_renamed")
  1178. if err != nil {
  1179. t.Fatalf("ALTER TABLE RENAME TO failed: %v", err)
  1180. }
  1181. // Verify old table doesn't exist
  1182. _, err = schema.GetSchema("test_alter")
  1183. if err == nil {
  1184. t.Error("old table 'test_alter' still exists after RENAME TO")
  1185. }
  1186. // Verify new table exists
  1187. _, err = schema.GetSchema("test_renamed")
  1188. if err != nil {
  1189. t.Errorf("new table 'test_renamed' not found after RENAME TO: %v", err)
  1190. }
  1191. // Cleanup with new name
  1192. execSQL(exec, "DROP TABLE IF EXISTS test_renamed")
  1193. })
  1194. // Final cleanup
  1195. execSQL(exec, "DROP TABLE IF EXISTS test_alter")
  1196. execSQL(exec, "DROP TABLE IF EXISTS test_renamed")
  1197. }
  1198. // Test ATTACH/DETACH DATABASE statements
  1199. func TestAttachDetach(t *testing.T) {
  1200. pool, err := storage.NewKVPool("localhost:8085", 5, 5*time.Second)
  1201. if err != nil {
  1202. t.Skip("PizzaKV not available, skipping ATTACH/DETACH tests")
  1203. }
  1204. defer pool.Close()
  1205. schema := storage.NewSchemaManager(pool, "test_main_db")
  1206. table := storage.NewTableManager(pool, schema, "test_main_db")
  1207. exec := New(schema, table)
  1208. // Create a table in main database
  1209. execSQL(exec, "DROP TABLE IF EXISTS main_table")
  1210. _, err = execSQL(exec, "CREATE TABLE main_table (id INTEGER PRIMARY KEY, data TEXT)")
  1211. if err != nil {
  1212. t.Fatalf("failed to create main table: %v", err)
  1213. }
  1214. execSQL(exec, "INSERT INTO main_table (id, data) VALUES (1, 'main data')")
  1215. t.Run("attach_database", func(t *testing.T) {
  1216. result, err := execSQL(exec, "ATTACH DATABASE 'test_other_db' AS other")
  1217. if err != nil {
  1218. t.Fatalf("ATTACH DATABASE failed: %v", err)
  1219. }
  1220. if result.CommandTag != "ATTACH" {
  1221. t.Errorf("expected command tag 'ATTACH', got '%s'", result.CommandTag)
  1222. }
  1223. // Verify database is attached
  1224. if _, exists := exec.attachedDatabases["other"]; !exists {
  1225. t.Error("database 'other' not found in attached databases")
  1226. }
  1227. })
  1228. t.Run("attach_duplicate_alias", func(t *testing.T) {
  1229. _, err := execSQL(exec, "ATTACH DATABASE 'test_dup_db' AS other")
  1230. if err == nil {
  1231. t.Error("expected error when attaching with duplicate alias")
  1232. }
  1233. })
  1234. t.Run("attach_reserved_alias", func(t *testing.T) {
  1235. _, err := execSQL(exec, "ATTACH DATABASE 'test_temp_db' AS temp")
  1236. if err == nil {
  1237. t.Error("expected error when using reserved alias 'temp'")
  1238. }
  1239. })
  1240. t.Run("detach_database", func(t *testing.T) {
  1241. result, err := execSQL(exec, "DETACH DATABASE other")
  1242. if err != nil {
  1243. t.Fatalf("DETACH DATABASE failed: %v", err)
  1244. }
  1245. if result.CommandTag != "DETACH" {
  1246. t.Errorf("expected command tag 'DETACH', got '%s'", result.CommandTag)
  1247. }
  1248. // Verify database is detached
  1249. if _, exists := exec.attachedDatabases["other"]; exists {
  1250. t.Error("database 'other' still attached after DETACH")
  1251. }
  1252. })
  1253. t.Run("detach_nonexistent", func(t *testing.T) {
  1254. _, err := execSQL(exec, "DETACH DATABASE nonexistent")
  1255. if err == nil {
  1256. t.Error("expected error when detaching nonexistent database")
  1257. }
  1258. })
  1259. t.Run("detach_main_database", func(t *testing.T) {
  1260. _, err := execSQL(exec, "DETACH DATABASE main")
  1261. if err == nil {
  1262. t.Error("expected error when detaching main database")
  1263. }
  1264. })
  1265. t.Run("attach_without_database_keyword", func(t *testing.T) {
  1266. result, err := execSQL(exec, "ATTACH 'test_short_db' AS short")
  1267. if err != nil {
  1268. t.Fatalf("ATTACH (without DATABASE) failed: %v", err)
  1269. }
  1270. if result.CommandTag != "ATTACH" {
  1271. t.Errorf("expected command tag 'ATTACH', got '%s'", result.CommandTag)
  1272. }
  1273. // Cleanup
  1274. execSQL(exec, "DETACH short")
  1275. })
  1276. t.Run("detach_without_database_keyword", func(t *testing.T) {
  1277. execSQL(exec, "ATTACH 'test_det_db' AS det")
  1278. result, err := execSQL(exec, "DETACH det")
  1279. if err != nil {
  1280. t.Fatalf("DETACH (without DATABASE) failed: %v", err)
  1281. }
  1282. if result.CommandTag != "DETACH" {
  1283. t.Errorf("expected command tag 'DETACH', got '%s'", result.CommandTag)
  1284. }
  1285. })
  1286. // Cleanup
  1287. execSQL(exec, "DROP TABLE IF EXISTS main_table")
  1288. }
  1289. func TestDistinct(t *testing.T) {
  1290. // Simple test without requiring KV connection
  1291. exec := &Executor{}
  1292. // Test applyDistinct function directly
  1293. t.Run("ApplyDistinct", func(t *testing.T) {
  1294. rows := [][]interface{}{
  1295. {"a", 1},
  1296. {"b", 2},
  1297. {"a", 1}, // duplicate
  1298. {"c", 3},
  1299. {"b", 2}, // duplicate
  1300. }
  1301. result := exec.applyDistinct(rows)
  1302. if len(result) != 3 {
  1303. t.Errorf("expected 3 unique rows, got %d", len(result))
  1304. }
  1305. // Check that we have the expected unique rows
  1306. expected := map[string]bool{
  1307. "a\x001": true,
  1308. "b\x002": true,
  1309. "c\x003": true,
  1310. }
  1311. for _, row := range result {
  1312. key := fmt.Sprintf("%v\x00%v", row[0], row[1])
  1313. if !expected[key] {
  1314. t.Errorf("unexpected row in result: %v", row)
  1315. }
  1316. }
  1317. })
  1318. }
  1319. // TestGroupByWithConstantFalseWhere tests the distinction between:
  1320. // 1. Aggregate without GROUP BY + WHERE false -> returns [NULL] (one row with aggregate result on empty set)
  1321. // 2. Aggregate with GROUP BY + WHERE false -> returns [] (no groups match, so no rows)
  1322. func TestGroupByWithConstantFalseWhere(t *testing.T) {
  1323. pool, err := storage.NewKVPool("localhost:8085", 5, 5*time.Second)
  1324. if err != nil {
  1325. t.Skip("PizzaKV not available, skipping test")
  1326. }
  1327. defer pool.Close()
  1328. schema := storage.NewSchemaManager(pool, "test_groupby_db")
  1329. table := storage.NewTableManager(pool, schema, "test_groupby_db")
  1330. exec := New(schema, table)
  1331. // Setup test tables
  1332. execSQL(exec, "DROP TABLE IF EXISTS tab0")
  1333. execSQL(exec, "DROP TABLE IF EXISTS tab1")
  1334. _, err = execSQL(exec, "CREATE TABLE tab0 (col0 INTEGER, col1 INTEGER, col2 INTEGER)")
  1335. if err != nil {
  1336. t.Fatalf("failed to create tab0: %v", err)
  1337. }
  1338. _, err = execSQL(exec, "CREATE TABLE tab1 (col0 INTEGER, col1 INTEGER, col2 INTEGER)")
  1339. if err != nil {
  1340. t.Fatalf("failed to create tab1: %v", err)
  1341. }
  1342. // Insert some test data
  1343. execSQL(exec, "INSERT INTO tab0 VALUES (1, 10, 100)")
  1344. execSQL(exec, "INSERT INTO tab0 VALUES (2, 20, 200)")
  1345. execSQL(exec, "INSERT INTO tab0 VALUES (3, 30, 300)")
  1346. execSQL(exec, "INSERT INTO tab1 VALUES (1, 10, 100)")
  1347. execSQL(exec, "INSERT INTO tab1 VALUES (2, 20, 200)")
  1348. execSQL(exec, "INSERT INTO tab1 VALUES (3, 30, 300)")
  1349. // Test 1: Aggregate with GROUP BY and constant FALSE WHERE -> should return empty result []
  1350. t.Run("aggregate_with_groupby_where_false", func(t *testing.T) {
  1351. result, err := execSQL(exec, "SELECT AVG(col1) FROM tab1 WHERE NULL IS NOT NULL GROUP BY col1")
  1352. if err != nil {
  1353. t.Fatalf("query failed: %v", err)
  1354. }
  1355. if result.RowCount != 0 {
  1356. t.Errorf("expected 0 rows (no groups), got %d rows with values: %v", result.RowCount, result.Rows)
  1357. }
  1358. })
  1359. // Test 2: Aggregate without GROUP BY and constant FALSE WHERE -> should return [NULL]
  1360. t.Run("aggregate_without_groupby_where_false", func(t *testing.T) {
  1361. result, err := execSQL(exec, "SELECT AVG(col1) FROM tab1 WHERE NULL IS NOT NULL")
  1362. if err != nil {
  1363. t.Fatalf("query failed: %v", err)
  1364. }
  1365. if result.RowCount != 1 {
  1366. t.Errorf("expected 1 row, got %d", result.RowCount)
  1367. }
  1368. if result.RowCount == 1 && result.Rows[0][0] != nil {
  1369. t.Errorf("expected NULL for aggregate on empty set, got %v", result.Rows[0][0])
  1370. }
  1371. })
  1372. // Test 3: More complex case from test failures
  1373. t.Run("complex_groupby_where_false", func(t *testing.T) {
  1374. result, err := execSQL(exec, "SELECT ALL AVG(+ col1) FROM tab1 WHERE NULL IS NULL AND NOT NULL IS NULL GROUP BY col1")
  1375. if err != nil {
  1376. t.Fatalf("query failed: %v", err)
  1377. }
  1378. if result.RowCount != 0 {
  1379. t.Errorf("expected 0 rows (no groups), got %d rows with values: %v", result.RowCount, result.Rows)
  1380. }
  1381. })
  1382. // Test 4: DISTINCT aggregate with GROUP BY and constant FALSE WHERE
  1383. t.Run("distinct_aggregate_with_groupby_where_false", func(t *testing.T) {
  1384. result, err := execSQL(exec, "SELECT DISTINCT AVG(DISTINCT - col2) FROM tab0 WHERE NOT NULL IS NULL GROUP BY col2")
  1385. if err != nil {
  1386. t.Fatalf("query failed: %v", err)
  1387. }
  1388. if result.RowCount != 0 {
  1389. t.Errorf("expected 0 rows (no groups), got %d rows with values: %v", result.RowCount, result.Rows)
  1390. }
  1391. })
  1392. // Test 5: Verify normal GROUP BY still works (WHERE true)
  1393. t.Run("normal_groupby_sanity_check", func(t *testing.T) {
  1394. result, err := execSQL(exec, "SELECT AVG(col1) FROM tab1 WHERE NULL IS NULL GROUP BY col1")
  1395. if err != nil {
  1396. t.Fatalf("query failed: %v", err)
  1397. }
  1398. if result.RowCount != 3 {
  1399. t.Errorf("expected 3 groups, got %d", result.RowCount)
  1400. }
  1401. })
  1402. // Cleanup
  1403. execSQL(exec, "DROP TABLE IF EXISTS tab0")
  1404. execSQL(exec, "DROP TABLE IF EXISTS tab1")
  1405. }