2
0

ast.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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. // DefaultValues is true for SQLite's INSERT ... DEFAULT VALUES, which
  122. // supplies no values. Values then holds one empty row so the executor
  123. // follows the normal single-row insert path.
  124. DefaultValues bool
  125. Select *SelectStmt // INSERT ... SELECT
  126. OnConflict ConflictAction // OR REPLACE/IGNORE/etc.
  127. ConflictTarget []string
  128. ConflictUpdate []Assignment
  129. ConflictDoNothing bool
  130. Returning []SelectColumn
  131. }
  132. func (s *InsertStmt) node() {}
  133. func (s *InsertStmt) stmtNode() {}
  134. // UpdateStmt represents an UPDATE statement.
  135. type UpdateStmt struct {
  136. Table *TableRef
  137. Set []Assignment
  138. From []TableRef
  139. Where Expr
  140. Returning []SelectColumn
  141. }
  142. func (s *UpdateStmt) node() {}
  143. func (s *UpdateStmt) stmtNode() {}
  144. // Assignment represents a SET assignment.
  145. type Assignment struct {
  146. Column string
  147. Value Expr
  148. }
  149. // DeleteStmt represents a DELETE statement.
  150. type DeleteStmt struct {
  151. Table *TableRef
  152. Where Expr
  153. Returning []SelectColumn
  154. }
  155. func (s *DeleteStmt) node() {}
  156. func (s *DeleteStmt) stmtNode() {}
  157. // CreateTableStmt represents a CREATE TABLE statement.
  158. type CreateTableStmt struct {
  159. IfNotExists bool
  160. Table *TableRef
  161. Columns []ColumnDef
  162. Constraints []TableConstraint
  163. }
  164. func (s *CreateTableStmt) node() {}
  165. func (s *CreateTableStmt) stmtNode() {}
  166. // ColumnDef represents a column definition.
  167. type ColumnDef struct {
  168. Name string
  169. Type DataType
  170. Constraints []ColumnConstraint
  171. // GeneratedExpr is non-nil for a GENERATED ALWAYS AS (expr) column. A
  172. // generated column's value is computed rather than supplied by the user.
  173. GeneratedExpr Expr
  174. GeneratedStored bool // true for STORED, false for VIRTUAL
  175. }
  176. // DataType represents a SQL data type.
  177. type DataType struct {
  178. Name string
  179. Precision int // for VARCHAR(n), NUMERIC(p,s)
  180. Scale int // for NUMERIC(p,s)
  181. }
  182. // ColumnConstraint represents a column-level constraint.
  183. type ColumnConstraint struct {
  184. Type ConstraintType
  185. Name string // optional constraint name
  186. Default Expr // for DEFAULT
  187. RefTable string // for REFERENCES
  188. RefColumn string // for REFERENCES
  189. Check Expr // for CHECK
  190. // OnConflict is the conflict resolution algorithm declared with
  191. // ON CONFLICT REPLACE/etc. (ConflictAbort is the zero value/default).
  192. OnConflict ConflictAction
  193. HasOnConflict bool
  194. }
  195. // ConstraintType represents the type of constraint.
  196. type ConstraintType int
  197. const (
  198. ConstraintPrimaryKey ConstraintType = iota
  199. ConstraintNotNull
  200. ConstraintUnique
  201. ConstraintDefault
  202. ConstraintCheck
  203. ConstraintForeignKey
  204. ConstraintAutoIncrement
  205. )
  206. // TableConstraint represents a table-level constraint.
  207. type TableConstraint struct {
  208. Type ConstraintType
  209. Name string // optional constraint name
  210. Columns []string // columns involved
  211. RefTable string // for FOREIGN KEY
  212. RefColumns []string // for FOREIGN KEY
  213. Check Expr // for CHECK
  214. // OnConflict is the conflict resolution declared with ON CONFLICT
  215. // REPLACE/etc.; HasOnConflict distinguishes it from the default ABORT.
  216. OnConflict ConflictAction
  217. HasOnConflict bool
  218. }
  219. // DropTableStmt represents a DROP TABLE statement.
  220. type DropTableStmt struct {
  221. IfExists bool
  222. Tables []*TableRef
  223. }
  224. func (s *DropTableStmt) node() {}
  225. func (s *DropTableStmt) stmtNode() {}
  226. // CreateIndexStmt represents a CREATE INDEX statement.
  227. type CreateIndexStmt struct {
  228. IfNotExists bool
  229. Unique bool
  230. Name string
  231. Table string
  232. Columns []IndexColumn
  233. }
  234. func (s *CreateIndexStmt) node() {}
  235. func (s *CreateIndexStmt) stmtNode() {}
  236. // IndexColumn represents a column in an index. Expr is set for expression
  237. // indexes (e.g. an index on lower(email)); a plain column index leaves it nil
  238. // and uses Name.
  239. type IndexColumn struct {
  240. Name string
  241. Desc bool // true for DESC ordering
  242. Expr Expr
  243. }
  244. // DropIndexStmt represents a DROP INDEX statement.
  245. type DropIndexStmt struct {
  246. IfExists bool
  247. Name string
  248. }
  249. func (s *DropIndexStmt) node() {}
  250. func (s *DropIndexStmt) stmtNode() {}
  251. // CreateViewStmt represents a CREATE VIEW statement.
  252. type CreateViewStmt struct {
  253. IfNotExists bool
  254. View *TableRef
  255. Select *SelectStmt
  256. }
  257. func (s *CreateViewStmt) node() {}
  258. func (s *CreateViewStmt) stmtNode() {}
  259. // DropViewStmt represents a DROP VIEW statement.
  260. type DropViewStmt struct {
  261. IfExists bool
  262. Views []*TableRef
  263. }
  264. func (s *DropViewStmt) node() {}
  265. func (s *DropViewStmt) stmtNode() {}
  266. // AlterTableStmt represents an ALTER TABLE statement.
  267. type AlterTableStmt struct {
  268. Table string
  269. Action AlterAction
  270. }
  271. func (s *AlterTableStmt) node() {}
  272. func (s *AlterTableStmt) stmtNode() {}
  273. // AlterAction represents an ALTER TABLE action.
  274. type AlterAction interface {
  275. Node
  276. alterAction()
  277. }
  278. // AddColumnAction represents ADD COLUMN action.
  279. type AddColumnAction struct {
  280. Column *ColumnDef
  281. IfNotExists bool
  282. }
  283. func (a *AddColumnAction) node() {}
  284. func (a *AddColumnAction) alterAction() {}
  285. // DropColumnAction represents DROP COLUMN action.
  286. type DropColumnAction struct {
  287. Column string
  288. }
  289. func (a *DropColumnAction) node() {}
  290. func (a *DropColumnAction) alterAction() {}
  291. // RenameTableAction represents RENAME TO action.
  292. type RenameTableAction struct {
  293. NewName string
  294. }
  295. func (a *RenameTableAction) node() {}
  296. func (a *RenameTableAction) alterAction() {}
  297. // RenameColumnAction represents RENAME COLUMN action.
  298. type RenameColumnAction struct {
  299. OldName string
  300. NewName string
  301. }
  302. func (a *RenameColumnAction) node() {}
  303. func (a *RenameColumnAction) alterAction() {}
  304. // AttachStmt represents an ATTACH DATABASE statement.
  305. type AttachStmt struct {
  306. FilePath string // Database file path or identifier
  307. Alias string // Database alias name
  308. }
  309. func (s *AttachStmt) node() {}
  310. func (s *AttachStmt) stmtNode() {}
  311. // DetachStmt represents a DETACH DATABASE statement.
  312. type DetachStmt struct {
  313. Alias string // Database alias to detach
  314. }
  315. func (s *DetachStmt) node() {}
  316. func (s *DetachStmt) stmtNode() {}
  317. // PragmaStmt represents a PRAGMA statement.
  318. type PragmaStmt struct {
  319. Name string // pragma name (e.g., "table_info")
  320. Arg string // optional argument (e.g., table name)
  321. Value Expr // optional value for SET pragmas
  322. }
  323. func (s *PragmaStmt) node() {}
  324. func (s *PragmaStmt) stmtNode() {}
  325. // AnalyzeStmt represents an ANALYZE statement. PizzaSQL does not maintain
  326. // optimizer statistics, so it is accepted and executed as a documented no-op.
  327. type AnalyzeStmt struct {
  328. Name string // optional table name
  329. }
  330. func (s *AnalyzeStmt) node() {}
  331. func (s *AnalyzeStmt) stmtNode() {}
  332. // ExplainStmt represents an EXPLAIN statement.
  333. type ExplainStmt struct {
  334. QueryPlan bool // true for EXPLAIN QUERY PLAN
  335. Statement Statement // the statement being explained
  336. }
  337. func (s *ExplainStmt) node() {}
  338. func (s *ExplainStmt) stmtNode() {}
  339. // Transaction statements
  340. // BeginStmt represents a BEGIN TRANSACTION statement.
  341. type BeginStmt struct {
  342. // Transaction mode (DEFERRED, IMMEDIATE, EXCLUSIVE) - for future use
  343. Mode string
  344. }
  345. func (s *BeginStmt) node() {}
  346. func (s *BeginStmt) stmtNode() {}
  347. // CommitStmt represents a COMMIT statement.
  348. type CommitStmt struct{}
  349. func (s *CommitStmt) node() {}
  350. func (s *CommitStmt) stmtNode() {}
  351. // RollbackStmt represents a ROLLBACK statement.
  352. type RollbackStmt struct {
  353. Savepoint string // for ROLLBACK TO SAVEPOINT
  354. }
  355. func (s *RollbackStmt) node() {}
  356. func (s *RollbackStmt) stmtNode() {}
  357. // SavepointStmt represents a SAVEPOINT statement.
  358. type SavepointStmt struct {
  359. Name string
  360. }
  361. func (s *SavepointStmt) node() {}
  362. func (s *SavepointStmt) stmtNode() {}
  363. // ReleaseStmt represents a RELEASE SAVEPOINT statement.
  364. type ReleaseStmt struct {
  365. Name string
  366. }
  367. func (s *ReleaseStmt) node() {}
  368. func (s *ReleaseStmt) stmtNode() {}
  369. // Notification statements
  370. // ListenStmt represents LISTEN channel.
  371. type ListenStmt struct {
  372. Channel string
  373. }
  374. func (s *ListenStmt) node() {}
  375. func (s *ListenStmt) stmtNode() {}
  376. // UnlistenStmt represents UNLISTEN channel or UNLISTEN *.
  377. type UnlistenStmt struct {
  378. Channel string
  379. All bool
  380. }
  381. func (s *UnlistenStmt) node() {}
  382. func (s *UnlistenStmt) stmtNode() {}
  383. // NotifyStmt represents NOTIFY channel [, payload].
  384. type NotifyStmt struct {
  385. Channel string
  386. Payload string
  387. }
  388. func (s *NotifyStmt) node() {}
  389. func (s *NotifyStmt) stmtNode() {}
  390. // Expression types
  391. // BinaryExpr represents a binary expression.
  392. type BinaryExpr struct {
  393. Left Expr
  394. Op lexer.TokenType
  395. Right Expr
  396. }
  397. func (e *BinaryExpr) node() {}
  398. func (e *BinaryExpr) exprNode() {}
  399. // UnaryExpr represents a unary expression.
  400. type UnaryExpr struct {
  401. Op lexer.TokenType
  402. Operand Expr
  403. }
  404. func (e *UnaryExpr) node() {}
  405. func (e *UnaryExpr) exprNode() {}
  406. // LiteralExpr represents a literal value.
  407. type LiteralExpr struct {
  408. Type lexer.TokenType // TokenNumber, TokenString, TokenNULL, TokenTRUE, TokenFALSE
  409. Value string
  410. }
  411. func (e *LiteralExpr) node() {}
  412. func (e *LiteralExpr) exprNode() {}
  413. // ColumnRef represents a column reference.
  414. type ColumnRef struct {
  415. Table string
  416. Column string
  417. }
  418. func (e *ColumnRef) node() {}
  419. func (e *ColumnRef) exprNode() {}
  420. // FunctionCall represents a function call.
  421. type FunctionCall struct {
  422. Name string
  423. Args []Expr
  424. Distinct bool // for COUNT(DISTINCT x)
  425. Star bool // for COUNT(*)
  426. }
  427. func (e *FunctionCall) node() {}
  428. func (e *FunctionCall) exprNode() {}
  429. // WindowExpr represents a function call with an OVER clause.
  430. type WindowExpr struct {
  431. Func *FunctionCall
  432. PartitionBy []Expr
  433. OrderBy []OrderByItem
  434. }
  435. func (e *WindowExpr) node() {}
  436. func (e *WindowExpr) exprNode() {}
  437. // SubqueryExpr represents a subquery expression.
  438. type SubqueryExpr struct {
  439. Query *SelectStmt
  440. }
  441. func (e *SubqueryExpr) node() {}
  442. func (e *SubqueryExpr) exprNode() {}
  443. // CaseExpr represents a CASE expression.
  444. type CaseExpr struct {
  445. Operand Expr // for CASE operand WHEN...
  446. Whens []WhenClause
  447. Else Expr
  448. }
  449. func (e *CaseExpr) node() {}
  450. func (e *CaseExpr) exprNode() {}
  451. // WhenClause represents a WHEN clause in CASE.
  452. type WhenClause struct {
  453. Condition Expr
  454. Result Expr
  455. }
  456. // InExpr represents an IN expression.
  457. type InExpr struct {
  458. Left Expr
  459. Not bool
  460. Values []Expr // IN (1, 2, 3)
  461. Subquery *SelectStmt // IN (SELECT ...)
  462. }
  463. func (e *InExpr) node() {}
  464. func (e *InExpr) exprNode() {}
  465. // BetweenExpr represents a BETWEEN expression.
  466. type BetweenExpr struct {
  467. Left Expr
  468. Not bool
  469. Low Expr
  470. High Expr
  471. }
  472. func (e *BetweenExpr) node() {}
  473. func (e *BetweenExpr) exprNode() {}
  474. // LikeExpr represents a LIKE expression.
  475. type LikeExpr struct {
  476. Left Expr
  477. Not bool
  478. Pattern Expr
  479. Escape Expr
  480. }
  481. func (e *LikeExpr) node() {}
  482. func (e *LikeExpr) exprNode() {}
  483. // IsNullExpr represents an IS NULL expression.
  484. type IsNullExpr struct {
  485. Left Expr
  486. Not bool
  487. }
  488. func (e *IsNullExpr) node() {}
  489. func (e *IsNullExpr) exprNode() {}
  490. // IsDistinctExpr represents `left IS DISTINCT FROM right` (Not=false) or
  491. // `left IS NOT DISTINCT FROM right` (Not=true). Unlike `=`, NULLs compare
  492. // equal to each other and distinct from non-NULLs.
  493. type IsDistinctExpr struct {
  494. Left Expr
  495. Right Expr
  496. Not bool
  497. }
  498. func (e *IsDistinctExpr) node() {}
  499. func (e *IsDistinctExpr) exprNode() {}
  500. // CastExpr represents a CAST expression.
  501. type CastExpr struct {
  502. Expr Expr
  503. Type DataType
  504. }
  505. func (e *CastExpr) node() {}
  506. func (e *CastExpr) exprNode() {}
  507. // ExistsExpr represents an EXISTS expression.
  508. type ExistsExpr struct {
  509. Subquery *SelectStmt
  510. }
  511. func (e *ExistsExpr) node() {}
  512. func (e *ExistsExpr) exprNode() {}
  513. // ParenExpr represents a parenthesized expression.
  514. type ParenExpr struct {
  515. Expr Expr
  516. }
  517. func (e *ParenExpr) node() {}
  518. func (e *ParenExpr) exprNode() {}