ast.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. package parser
  2. import "github.com/danfragoso/pizzasql-next/pkg/lexer"
  3. // Node is the base interface for all AST nodes.
  4. type Node interface {
  5. node()
  6. }
  7. // Statement represents a SQL statement.
  8. type Statement interface {
  9. Node
  10. stmtNode()
  11. }
  12. // Expr represents an expression.
  13. type Expr interface {
  14. Node
  15. exprNode()
  16. }
  17. // SetOpType represents a set operation type.
  18. type SetOpType int
  19. const (
  20. SetOpUnion SetOpType = iota
  21. SetOpUnionAll
  22. SetOpIntersect
  23. SetOpExcept
  24. )
  25. // CompoundSelect chains two SELECT statements with a set operation.
  26. type CompoundSelect struct {
  27. Left *SelectStmt
  28. Op SetOpType
  29. Right *SelectStmt // may itself have Compound set for chained ops
  30. OrderBy []OrderByItem
  31. Limit Expr
  32. Offset Expr
  33. }
  34. func (c *CompoundSelect) node() {}
  35. func (c *CompoundSelect) stmtNode() {}
  36. // SelectStmt represents a SELECT statement.
  37. type SelectStmt struct {
  38. Distinct bool
  39. Columns []SelectColumn
  40. From []TableRef
  41. Where Expr
  42. GroupBy []Expr
  43. Having Expr
  44. OrderBy []OrderByItem
  45. Limit Expr
  46. Offset Expr
  47. // Compound chains a set operation onto this SELECT (UNION/INTERSECT/EXCEPT).
  48. Compound *CompoundSelect
  49. // With holds common table expressions that must be materialized before this
  50. // SELECT runs. Non-recursive CTEs are desugared by the parser instead; this
  51. // list carries recursive CTEs (and the CTEs that depend on them).
  52. With []*CTE
  53. }
  54. // CTE is a common table expression from a WITH clause.
  55. type CTE struct {
  56. Name string
  57. Columns []string
  58. Recursive bool
  59. Query *SelectStmt
  60. }
  61. func (s *SelectStmt) node() {}
  62. func (s *SelectStmt) stmtNode() {}
  63. // SelectColumn represents a column in SELECT.
  64. type SelectColumn struct {
  65. Expr Expr
  66. Alias string
  67. Star bool // true if this is *
  68. TableStar string // table name/alias if this is a qualified wildcard (table.*)
  69. }
  70. // TableRef represents a table reference.
  71. type TableRef struct {
  72. Schema string
  73. Name string
  74. Alias string
  75. Subquery *SelectStmt // for derived tables (SELECT ... FROM (SELECT ...) AS alias)
  76. Join *JoinClause // for joined tables
  77. }
  78. // JoinClause represents a JOIN clause.
  79. type JoinClause struct {
  80. Type JoinType
  81. Table *TableRef
  82. Condition Expr // ON condition
  83. Using []string // USING columns
  84. }
  85. // JoinType represents the type of JOIN.
  86. type JoinType int
  87. const (
  88. JoinInner JoinType = iota
  89. JoinLeft
  90. JoinRight
  91. JoinFull
  92. JoinCross
  93. )
  94. // NullsOrder selects where NULLs sort in an ORDER BY item.
  95. type NullsOrder int
  96. const (
  97. NullsDefault NullsOrder = iota // SQLite default: NULLs are smallest
  98. NullsFirst
  99. NullsLast
  100. )
  101. // OrderByItem represents an ORDER BY item.
  102. type OrderByItem struct {
  103. Expr Expr
  104. Desc bool
  105. NullsOrder NullsOrder
  106. }
  107. // ConflictAction represents the action to take on conflict.
  108. type ConflictAction int
  109. const (
  110. ConflictAbort ConflictAction = iota // Default
  111. ConflictReplace // INSERT OR REPLACE
  112. ConflictIgnore // INSERT OR IGNORE
  113. ConflictFail // INSERT OR FAIL
  114. ConflictRollback // INSERT OR ROLLBACK
  115. )
  116. // InsertStmt represents an INSERT statement.
  117. type InsertStmt struct {
  118. Table *TableRef
  119. Columns []string
  120. Values [][]Expr
  121. Select *SelectStmt // INSERT ... SELECT
  122. OnConflict ConflictAction // OR REPLACE/IGNORE/etc.
  123. ConflictTarget []string
  124. ConflictUpdate []Assignment
  125. ConflictDoNothing bool
  126. Returning []SelectColumn
  127. }
  128. func (s *InsertStmt) node() {}
  129. func (s *InsertStmt) stmtNode() {}
  130. // UpdateStmt represents an UPDATE statement.
  131. type UpdateStmt struct {
  132. Table *TableRef
  133. Set []Assignment
  134. From []TableRef
  135. Where Expr
  136. Returning []SelectColumn
  137. }
  138. func (s *UpdateStmt) node() {}
  139. func (s *UpdateStmt) stmtNode() {}
  140. // Assignment represents a SET assignment.
  141. type Assignment struct {
  142. Column string
  143. Value Expr
  144. }
  145. // DeleteStmt represents a DELETE statement.
  146. type DeleteStmt struct {
  147. Table *TableRef
  148. Where Expr
  149. Returning []SelectColumn
  150. }
  151. func (s *DeleteStmt) node() {}
  152. func (s *DeleteStmt) stmtNode() {}
  153. // CreateTableStmt represents a CREATE TABLE statement.
  154. type CreateTableStmt struct {
  155. IfNotExists bool
  156. Table *TableRef
  157. Columns []ColumnDef
  158. Constraints []TableConstraint
  159. }
  160. func (s *CreateTableStmt) node() {}
  161. func (s *CreateTableStmt) stmtNode() {}
  162. // ColumnDef represents a column definition.
  163. type ColumnDef struct {
  164. Name string
  165. Type DataType
  166. Constraints []ColumnConstraint
  167. // GeneratedExpr is non-nil for a GENERATED ALWAYS AS (expr) column. A
  168. // generated column's value is computed rather than supplied by the user.
  169. GeneratedExpr Expr
  170. GeneratedStored bool // true for STORED, false for VIRTUAL
  171. }
  172. // DataType represents a SQL data type.
  173. type DataType struct {
  174. Name string
  175. Precision int // for VARCHAR(n), NUMERIC(p,s)
  176. Scale int // for NUMERIC(p,s)
  177. }
  178. // ColumnConstraint represents a column-level constraint.
  179. type ColumnConstraint struct {
  180. Type ConstraintType
  181. Name string // optional constraint name
  182. Default Expr // for DEFAULT
  183. RefTable string // for REFERENCES
  184. RefColumn string // for REFERENCES
  185. Check Expr // for CHECK
  186. // OnConflict is the conflict resolution algorithm declared with
  187. // ON CONFLICT REPLACE/etc. (ConflictAbort is the zero value/default).
  188. OnConflict ConflictAction
  189. HasOnConflict bool
  190. }
  191. // ConstraintType represents the type of constraint.
  192. type ConstraintType int
  193. const (
  194. ConstraintPrimaryKey ConstraintType = iota
  195. ConstraintNotNull
  196. ConstraintUnique
  197. ConstraintDefault
  198. ConstraintCheck
  199. ConstraintForeignKey
  200. ConstraintAutoIncrement
  201. )
  202. // TableConstraint represents a table-level constraint.
  203. type TableConstraint struct {
  204. Type ConstraintType
  205. Name string // optional constraint name
  206. Columns []string // columns involved
  207. RefTable string // for FOREIGN KEY
  208. RefColumns []string // for FOREIGN KEY
  209. Check Expr // for CHECK
  210. // OnConflict is the conflict resolution declared with ON CONFLICT
  211. // REPLACE/etc.; HasOnConflict distinguishes it from the default ABORT.
  212. OnConflict ConflictAction
  213. HasOnConflict bool
  214. }
  215. // DropTableStmt represents a DROP TABLE statement.
  216. type DropTableStmt struct {
  217. IfExists bool
  218. Tables []*TableRef
  219. }
  220. func (s *DropTableStmt) node() {}
  221. func (s *DropTableStmt) stmtNode() {}
  222. // CreateIndexStmt represents a CREATE INDEX statement.
  223. type CreateIndexStmt struct {
  224. IfNotExists bool
  225. Unique bool
  226. Name string
  227. Table string
  228. Columns []IndexColumn
  229. }
  230. func (s *CreateIndexStmt) node() {}
  231. func (s *CreateIndexStmt) stmtNode() {}
  232. // IndexColumn represents a column in an index. Expr is set for expression
  233. // indexes (e.g. an index on lower(email)); a plain column index leaves it nil
  234. // and uses Name.
  235. type IndexColumn struct {
  236. Name string
  237. Desc bool // true for DESC ordering
  238. Expr Expr
  239. }
  240. // DropIndexStmt represents a DROP INDEX statement.
  241. type DropIndexStmt struct {
  242. IfExists bool
  243. Name string
  244. }
  245. func (s *DropIndexStmt) node() {}
  246. func (s *DropIndexStmt) stmtNode() {}
  247. // CreateViewStmt represents a CREATE VIEW statement.
  248. type CreateViewStmt struct {
  249. IfNotExists bool
  250. View *TableRef
  251. Select *SelectStmt
  252. }
  253. func (s *CreateViewStmt) node() {}
  254. func (s *CreateViewStmt) stmtNode() {}
  255. // DropViewStmt represents a DROP VIEW statement.
  256. type DropViewStmt struct {
  257. IfExists bool
  258. Views []*TableRef
  259. }
  260. func (s *DropViewStmt) node() {}
  261. func (s *DropViewStmt) stmtNode() {}
  262. // AlterTableStmt represents an ALTER TABLE statement.
  263. type AlterTableStmt struct {
  264. Table string
  265. Action AlterAction
  266. }
  267. func (s *AlterTableStmt) node() {}
  268. func (s *AlterTableStmt) stmtNode() {}
  269. // AlterAction represents an ALTER TABLE action.
  270. type AlterAction interface {
  271. Node
  272. alterAction()
  273. }
  274. // AddColumnAction represents ADD COLUMN action.
  275. type AddColumnAction struct {
  276. Column *ColumnDef
  277. IfNotExists bool
  278. }
  279. func (a *AddColumnAction) node() {}
  280. func (a *AddColumnAction) alterAction() {}
  281. // DropColumnAction represents DROP COLUMN action.
  282. type DropColumnAction struct {
  283. Column string
  284. }
  285. func (a *DropColumnAction) node() {}
  286. func (a *DropColumnAction) alterAction() {}
  287. // RenameTableAction represents RENAME TO action.
  288. type RenameTableAction struct {
  289. NewName string
  290. }
  291. func (a *RenameTableAction) node() {}
  292. func (a *RenameTableAction) alterAction() {}
  293. // RenameColumnAction represents RENAME COLUMN action.
  294. type RenameColumnAction struct {
  295. OldName string
  296. NewName string
  297. }
  298. func (a *RenameColumnAction) node() {}
  299. func (a *RenameColumnAction) alterAction() {}
  300. // AttachStmt represents an ATTACH DATABASE statement.
  301. type AttachStmt struct {
  302. FilePath string // Database file path or identifier
  303. Alias string // Database alias name
  304. }
  305. func (s *AttachStmt) node() {}
  306. func (s *AttachStmt) stmtNode() {}
  307. // DetachStmt represents a DETACH DATABASE statement.
  308. type DetachStmt struct {
  309. Alias string // Database alias to detach
  310. }
  311. func (s *DetachStmt) node() {}
  312. func (s *DetachStmt) stmtNode() {}
  313. // PragmaStmt represents a PRAGMA statement.
  314. type PragmaStmt struct {
  315. Name string // pragma name (e.g., "table_info")
  316. Arg string // optional argument (e.g., table name)
  317. Value Expr // optional value for SET pragmas
  318. }
  319. func (s *PragmaStmt) node() {}
  320. func (s *PragmaStmt) stmtNode() {}
  321. // AnalyzeStmt represents an ANALYZE statement. PizzaSQL does not maintain
  322. // optimizer statistics, so it is accepted and executed as a documented no-op.
  323. type AnalyzeStmt struct {
  324. Name string // optional table name
  325. }
  326. func (s *AnalyzeStmt) node() {}
  327. func (s *AnalyzeStmt) stmtNode() {}
  328. // ExplainStmt represents an EXPLAIN statement.
  329. type ExplainStmt struct {
  330. QueryPlan bool // true for EXPLAIN QUERY PLAN
  331. Statement Statement // the statement being explained
  332. }
  333. func (s *ExplainStmt) node() {}
  334. func (s *ExplainStmt) stmtNode() {}
  335. // Transaction statements
  336. // BeginStmt represents a BEGIN TRANSACTION statement.
  337. type BeginStmt struct {
  338. // Transaction mode (DEFERRED, IMMEDIATE, EXCLUSIVE) - for future use
  339. Mode string
  340. }
  341. func (s *BeginStmt) node() {}
  342. func (s *BeginStmt) stmtNode() {}
  343. // CommitStmt represents a COMMIT statement.
  344. type CommitStmt struct{}
  345. func (s *CommitStmt) node() {}
  346. func (s *CommitStmt) stmtNode() {}
  347. // RollbackStmt represents a ROLLBACK statement.
  348. type RollbackStmt struct {
  349. Savepoint string // for ROLLBACK TO SAVEPOINT
  350. }
  351. func (s *RollbackStmt) node() {}
  352. func (s *RollbackStmt) stmtNode() {}
  353. // SavepointStmt represents a SAVEPOINT statement.
  354. type SavepointStmt struct {
  355. Name string
  356. }
  357. func (s *SavepointStmt) node() {}
  358. func (s *SavepointStmt) stmtNode() {}
  359. // ReleaseStmt represents a RELEASE SAVEPOINT statement.
  360. type ReleaseStmt struct {
  361. Name string
  362. }
  363. func (s *ReleaseStmt) node() {}
  364. func (s *ReleaseStmt) stmtNode() {}
  365. // Notification statements
  366. // ListenStmt represents LISTEN channel.
  367. type ListenStmt struct {
  368. Channel string
  369. }
  370. func (s *ListenStmt) node() {}
  371. func (s *ListenStmt) stmtNode() {}
  372. // UnlistenStmt represents UNLISTEN channel or UNLISTEN *.
  373. type UnlistenStmt struct {
  374. Channel string
  375. All bool
  376. }
  377. func (s *UnlistenStmt) node() {}
  378. func (s *UnlistenStmt) stmtNode() {}
  379. // NotifyStmt represents NOTIFY channel [, payload].
  380. type NotifyStmt struct {
  381. Channel string
  382. Payload string
  383. }
  384. func (s *NotifyStmt) node() {}
  385. func (s *NotifyStmt) stmtNode() {}
  386. // Expression types
  387. // BinaryExpr represents a binary expression.
  388. type BinaryExpr struct {
  389. Left Expr
  390. Op lexer.TokenType
  391. Right Expr
  392. }
  393. func (e *BinaryExpr) node() {}
  394. func (e *BinaryExpr) exprNode() {}
  395. // UnaryExpr represents a unary expression.
  396. type UnaryExpr struct {
  397. Op lexer.TokenType
  398. Operand Expr
  399. }
  400. func (e *UnaryExpr) node() {}
  401. func (e *UnaryExpr) exprNode() {}
  402. // LiteralExpr represents a literal value.
  403. type LiteralExpr struct {
  404. Type lexer.TokenType // TokenNumber, TokenString, TokenNULL, TokenTRUE, TokenFALSE
  405. Value string
  406. }
  407. func (e *LiteralExpr) node() {}
  408. func (e *LiteralExpr) exprNode() {}
  409. // ColumnRef represents a column reference.
  410. type ColumnRef struct {
  411. Table string
  412. Column string
  413. }
  414. func (e *ColumnRef) node() {}
  415. func (e *ColumnRef) exprNode() {}
  416. // FunctionCall represents a function call.
  417. type FunctionCall struct {
  418. Name string
  419. Args []Expr
  420. Distinct bool // for COUNT(DISTINCT x)
  421. Star bool // for COUNT(*)
  422. }
  423. func (e *FunctionCall) node() {}
  424. func (e *FunctionCall) exprNode() {}
  425. // WindowExpr represents a function call with an OVER clause.
  426. type WindowExpr struct {
  427. Func *FunctionCall
  428. PartitionBy []Expr
  429. OrderBy []OrderByItem
  430. }
  431. func (e *WindowExpr) node() {}
  432. func (e *WindowExpr) exprNode() {}
  433. // SubqueryExpr represents a subquery expression.
  434. type SubqueryExpr struct {
  435. Query *SelectStmt
  436. }
  437. func (e *SubqueryExpr) node() {}
  438. func (e *SubqueryExpr) exprNode() {}
  439. // CaseExpr represents a CASE expression.
  440. type CaseExpr struct {
  441. Operand Expr // for CASE operand WHEN...
  442. Whens []WhenClause
  443. Else Expr
  444. }
  445. func (e *CaseExpr) node() {}
  446. func (e *CaseExpr) exprNode() {}
  447. // WhenClause represents a WHEN clause in CASE.
  448. type WhenClause struct {
  449. Condition Expr
  450. Result Expr
  451. }
  452. // InExpr represents an IN expression.
  453. type InExpr struct {
  454. Left Expr
  455. Not bool
  456. Values []Expr // IN (1, 2, 3)
  457. Subquery *SelectStmt // IN (SELECT ...)
  458. }
  459. func (e *InExpr) node() {}
  460. func (e *InExpr) exprNode() {}
  461. // BetweenExpr represents a BETWEEN expression.
  462. type BetweenExpr struct {
  463. Left Expr
  464. Not bool
  465. Low Expr
  466. High Expr
  467. }
  468. func (e *BetweenExpr) node() {}
  469. func (e *BetweenExpr) exprNode() {}
  470. // LikeExpr represents a LIKE expression.
  471. type LikeExpr struct {
  472. Left Expr
  473. Not bool
  474. Pattern Expr
  475. Escape Expr
  476. }
  477. func (e *LikeExpr) node() {}
  478. func (e *LikeExpr) exprNode() {}
  479. // IsNullExpr represents an IS NULL expression.
  480. type IsNullExpr struct {
  481. Left Expr
  482. Not bool
  483. }
  484. func (e *IsNullExpr) node() {}
  485. func (e *IsNullExpr) exprNode() {}
  486. // IsDistinctExpr represents `left IS DISTINCT FROM right` (Not=false) or
  487. // `left IS NOT DISTINCT FROM right` (Not=true). Unlike `=`, NULLs compare
  488. // equal to each other and distinct from non-NULLs.
  489. type IsDistinctExpr struct {
  490. Left Expr
  491. Right Expr
  492. Not bool
  493. }
  494. func (e *IsDistinctExpr) node() {}
  495. func (e *IsDistinctExpr) exprNode() {}
  496. // CastExpr represents a CAST expression.
  497. type CastExpr struct {
  498. Expr Expr
  499. Type DataType
  500. }
  501. func (e *CastExpr) node() {}
  502. func (e *CastExpr) exprNode() {}
  503. // ExistsExpr represents an EXISTS expression.
  504. type ExistsExpr struct {
  505. Subquery *SelectStmt
  506. }
  507. func (e *ExistsExpr) node() {}
  508. func (e *ExistsExpr) exprNode() {}
  509. // ParenExpr represents a parenthesized expression.
  510. type ParenExpr struct {
  511. Expr Expr
  512. }
  513. func (e *ParenExpr) node() {}
  514. func (e *ParenExpr) exprNode() {}