ast.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. // Expression types
  366. // BinaryExpr represents a binary expression.
  367. type BinaryExpr struct {
  368. Left Expr
  369. Op lexer.TokenType
  370. Right Expr
  371. }
  372. func (e *BinaryExpr) node() {}
  373. func (e *BinaryExpr) exprNode() {}
  374. // UnaryExpr represents a unary expression.
  375. type UnaryExpr struct {
  376. Op lexer.TokenType
  377. Operand Expr
  378. }
  379. func (e *UnaryExpr) node() {}
  380. func (e *UnaryExpr) exprNode() {}
  381. // LiteralExpr represents a literal value.
  382. type LiteralExpr struct {
  383. Type lexer.TokenType // TokenNumber, TokenString, TokenNULL, TokenTRUE, TokenFALSE
  384. Value string
  385. }
  386. func (e *LiteralExpr) node() {}
  387. func (e *LiteralExpr) exprNode() {}
  388. // ColumnRef represents a column reference.
  389. type ColumnRef struct {
  390. Table string
  391. Column string
  392. }
  393. func (e *ColumnRef) node() {}
  394. func (e *ColumnRef) exprNode() {}
  395. // FunctionCall represents a function call.
  396. type FunctionCall struct {
  397. Name string
  398. Args []Expr
  399. Distinct bool // for COUNT(DISTINCT x)
  400. Star bool // for COUNT(*)
  401. }
  402. func (e *FunctionCall) node() {}
  403. func (e *FunctionCall) exprNode() {}
  404. // WindowExpr represents a function call with an OVER clause.
  405. type WindowExpr struct {
  406. Func *FunctionCall
  407. PartitionBy []Expr
  408. OrderBy []OrderByItem
  409. }
  410. func (e *WindowExpr) node() {}
  411. func (e *WindowExpr) exprNode() {}
  412. // SubqueryExpr represents a subquery expression.
  413. type SubqueryExpr struct {
  414. Query *SelectStmt
  415. }
  416. func (e *SubqueryExpr) node() {}
  417. func (e *SubqueryExpr) exprNode() {}
  418. // CaseExpr represents a CASE expression.
  419. type CaseExpr struct {
  420. Operand Expr // for CASE operand WHEN...
  421. Whens []WhenClause
  422. Else Expr
  423. }
  424. func (e *CaseExpr) node() {}
  425. func (e *CaseExpr) exprNode() {}
  426. // WhenClause represents a WHEN clause in CASE.
  427. type WhenClause struct {
  428. Condition Expr
  429. Result Expr
  430. }
  431. // InExpr represents an IN expression.
  432. type InExpr struct {
  433. Left Expr
  434. Not bool
  435. Values []Expr // IN (1, 2, 3)
  436. Subquery *SelectStmt // IN (SELECT ...)
  437. }
  438. func (e *InExpr) node() {}
  439. func (e *InExpr) exprNode() {}
  440. // BetweenExpr represents a BETWEEN expression.
  441. type BetweenExpr struct {
  442. Left Expr
  443. Not bool
  444. Low Expr
  445. High Expr
  446. }
  447. func (e *BetweenExpr) node() {}
  448. func (e *BetweenExpr) exprNode() {}
  449. // LikeExpr represents a LIKE expression.
  450. type LikeExpr struct {
  451. Left Expr
  452. Not bool
  453. Pattern Expr
  454. Escape Expr
  455. }
  456. func (e *LikeExpr) node() {}
  457. func (e *LikeExpr) exprNode() {}
  458. // IsNullExpr represents an IS NULL expression.
  459. type IsNullExpr struct {
  460. Left Expr
  461. Not bool
  462. }
  463. func (e *IsNullExpr) node() {}
  464. func (e *IsNullExpr) exprNode() {}
  465. // IsDistinctExpr represents `left IS DISTINCT FROM right` (Not=false) or
  466. // `left IS NOT DISTINCT FROM right` (Not=true). Unlike `=`, NULLs compare
  467. // equal to each other and distinct from non-NULLs.
  468. type IsDistinctExpr struct {
  469. Left Expr
  470. Right Expr
  471. Not bool
  472. }
  473. func (e *IsDistinctExpr) node() {}
  474. func (e *IsDistinctExpr) exprNode() {}
  475. // CastExpr represents a CAST expression.
  476. type CastExpr struct {
  477. Expr Expr
  478. Type DataType
  479. }
  480. func (e *CastExpr) node() {}
  481. func (e *CastExpr) exprNode() {}
  482. // ExistsExpr represents an EXISTS expression.
  483. type ExistsExpr struct {
  484. Subquery *SelectStmt
  485. }
  486. func (e *ExistsExpr) node() {}
  487. func (e *ExistsExpr) exprNode() {}
  488. // ParenExpr represents a parenthesized expression.
  489. type ParenExpr struct {
  490. Expr Expr
  491. }
  492. func (e *ParenExpr) node() {}
  493. func (e *ParenExpr) exprNode() {}