ast.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. package parser
  2. import (
  3. "github.com/danfragoso/pizzasql-next/pkg/lexer"
  4. "github.com/danfragoso/pizzasql-next/pkg/pgerror"
  5. )
  6. // Node is the base interface for all AST nodes.
  7. type Node interface {
  8. node()
  9. }
  10. // Statement represents a SQL statement.
  11. type Statement interface {
  12. Node
  13. stmtNode()
  14. }
  15. // Expr represents an expression.
  16. type Expr interface {
  17. Node
  18. exprNode()
  19. }
  20. // SetOpType represents a set operation type.
  21. type SetOpType int
  22. const (
  23. SetOpUnion SetOpType = iota
  24. SetOpUnionAll
  25. SetOpIntersect
  26. SetOpExcept
  27. )
  28. // CompoundSelect chains two SELECT statements with a set operation.
  29. type CompoundSelect struct {
  30. Left *SelectStmt
  31. Op SetOpType
  32. Right *SelectStmt // may itself have Compound set for chained ops
  33. OrderBy []OrderByItem
  34. Limit Expr
  35. Offset Expr
  36. }
  37. func (c *CompoundSelect) node() {}
  38. func (c *CompoundSelect) stmtNode() {}
  39. // SelectStmt represents a SELECT statement.
  40. type SelectStmt struct {
  41. Distinct bool
  42. Columns []SelectColumn
  43. From []TableRef
  44. Where Expr
  45. GroupBy []Expr
  46. Having Expr
  47. OrderBy []OrderByItem
  48. Limit Expr
  49. Offset Expr
  50. // Compound chains a set operation onto this SELECT (UNION/INTERSECT/EXCEPT).
  51. Compound *CompoundSelect
  52. // With holds common table expressions that must be materialized before this
  53. // SELECT runs. Non-recursive CTEs are desugared by the parser instead; this
  54. // list carries recursive CTEs (and the CTEs that depend on them).
  55. With []*CTE
  56. }
  57. // CTE is a common table expression from a WITH clause.
  58. type CTE struct {
  59. Name string
  60. Columns []string
  61. Recursive bool
  62. Query *SelectStmt
  63. }
  64. func (s *SelectStmt) node() {}
  65. func (s *SelectStmt) stmtNode() {}
  66. // SelectColumn represents a column in SELECT.
  67. type SelectColumn struct {
  68. Expr Expr
  69. Alias string
  70. Star bool // true if this is *
  71. TableStar string // table name/alias if this is a qualified wildcard (table.*)
  72. }
  73. // TableRef represents a table reference.
  74. type TableRef struct {
  75. Schema string
  76. Name string
  77. Alias string
  78. Subquery *SelectStmt // for derived tables (SELECT ... FROM (SELECT ...) AS alias)
  79. Join *JoinClause // for joined tables
  80. // Function is set for a table function in FROM (currently only unnest() and
  81. // the jsonb set-returning functions). Name holds the function name so
  82. // ordinary table dispatch ignores it.
  83. Function *FunctionCall
  84. // ColumnAliases holds the optional `AS alias(col, ...)` list that renames a
  85. // table function's output columns.
  86. ColumnAliases []string
  87. }
  88. // ApplyColumnAliases renames a table function's output columns with the
  89. // reference's `AS alias(col, ...)` list. A list whose length does not match the
  90. // function's output arity is rejected, as PostgreSQL does.
  91. func (t *TableRef) ApplyColumnAliases(columns []string) ([]string, error) {
  92. if len(t.ColumnAliases) == 0 {
  93. return columns, nil
  94. }
  95. if len(t.ColumnAliases) != len(columns) {
  96. name := t.Alias
  97. if name == "" {
  98. name = t.Name
  99. }
  100. return nil, pgerror.InvalidColumnReference(
  101. "table %q has %d columns available but %d columns specified",
  102. name, len(columns), len(t.ColumnAliases))
  103. }
  104. return append([]string(nil), t.ColumnAliases...), nil
  105. }
  106. // JoinClause represents a JOIN clause.
  107. type JoinClause struct {
  108. Type JoinType
  109. Table *TableRef
  110. Condition Expr // ON condition
  111. Using []string // USING columns
  112. }
  113. // JoinType represents the type of JOIN.
  114. type JoinType int
  115. const (
  116. JoinInner JoinType = iota
  117. JoinLeft
  118. JoinRight
  119. JoinFull
  120. JoinCross
  121. )
  122. // NullsOrder selects where NULLs sort in an ORDER BY item.
  123. type NullsOrder int
  124. const (
  125. NullsDefault NullsOrder = iota // SQLite default: NULLs are smallest
  126. NullsFirst
  127. NullsLast
  128. )
  129. // OrderByItem represents an ORDER BY item.
  130. type OrderByItem struct {
  131. Expr Expr
  132. Desc bool
  133. NullsOrder NullsOrder
  134. }
  135. // ConflictAction represents the action to take on conflict.
  136. type ConflictAction int
  137. const (
  138. ConflictAbort ConflictAction = iota // Default
  139. ConflictReplace // INSERT OR REPLACE
  140. ConflictIgnore // INSERT OR IGNORE
  141. ConflictFail // INSERT OR FAIL
  142. ConflictRollback // INSERT OR ROLLBACK
  143. )
  144. // InsertStmt represents an INSERT statement.
  145. type InsertStmt struct {
  146. Table *TableRef
  147. Columns []string
  148. Values [][]Expr
  149. // DefaultValues is true for SQLite's INSERT ... DEFAULT VALUES, which
  150. // supplies no values. Values then holds one empty row so the executor
  151. // follows the normal single-row insert path.
  152. DefaultValues bool
  153. Select *SelectStmt // INSERT ... SELECT
  154. OnConflict ConflictAction // OR REPLACE/IGNORE/etc.
  155. ConflictTarget []string
  156. ConflictUpdate []Assignment
  157. ConflictDoNothing bool
  158. Returning []SelectColumn
  159. }
  160. func (s *InsertStmt) node() {}
  161. func (s *InsertStmt) stmtNode() {}
  162. // UpdateStmt represents an UPDATE statement.
  163. type UpdateStmt struct {
  164. Table *TableRef
  165. Set []Assignment
  166. From []TableRef
  167. Where Expr
  168. Returning []SelectColumn
  169. }
  170. func (s *UpdateStmt) node() {}
  171. func (s *UpdateStmt) stmtNode() {}
  172. // Assignment represents a SET assignment.
  173. type Assignment struct {
  174. Column string
  175. Value Expr
  176. }
  177. // DeleteStmt represents a DELETE statement.
  178. type DeleteStmt struct {
  179. Table *TableRef
  180. Where Expr
  181. Returning []SelectColumn
  182. }
  183. func (s *DeleteStmt) node() {}
  184. func (s *DeleteStmt) stmtNode() {}
  185. // CreateTableStmt represents a CREATE TABLE statement.
  186. type CreateTableStmt struct {
  187. IfNotExists bool
  188. Table *TableRef
  189. Columns []ColumnDef
  190. Constraints []TableConstraint
  191. }
  192. func (s *CreateTableStmt) node() {}
  193. func (s *CreateTableStmt) stmtNode() {}
  194. // ColumnDef represents a column definition.
  195. type ColumnDef struct {
  196. Name string
  197. Type DataType
  198. Constraints []ColumnConstraint
  199. // GeneratedExpr is non-nil for a GENERATED ALWAYS AS (expr) column. A
  200. // generated column's value is computed rather than supplied by the user.
  201. GeneratedExpr Expr
  202. GeneratedStored bool // true for STORED, false for VIRTUAL
  203. }
  204. // DataType represents a SQL data type.
  205. type DataType struct {
  206. Name string
  207. Precision int // for VARCHAR(n), NUMERIC(p,s)
  208. Scale int // for NUMERIC(p,s)
  209. Array bool // trailing [] declares a one-dimensional array
  210. }
  211. // SQLTypeName returns the type name as it is persisted in the catalog: the
  212. // base name plus a [] suffix for arrays. Precision and scale are not part of
  213. // the catalog shape because the engine does not enforce them.
  214. func (d DataType) SQLTypeName() string {
  215. if d.Array {
  216. return d.Name + "[]"
  217. }
  218. return d.Name
  219. }
  220. // ColumnConstraint represents a column-level constraint.
  221. type ColumnConstraint struct {
  222. Type ConstraintType
  223. Name string // optional constraint name
  224. Default Expr // for DEFAULT
  225. RefTable string // for REFERENCES
  226. RefColumns []string // for REFERENCES (empty means the parent's primary key)
  227. Check Expr // for CHECK
  228. // OnDelete and OnUpdate record the foreign-key referential actions. The
  229. // engine enforces foreign keys immediately and implements no cascades, so
  230. // only NO ACTION and RESTRICT are accepted; the rest are rejected at DDL.
  231. OnDelete ReferentialAction
  232. OnUpdate ReferentialAction
  233. // Deferrable is set for a DEFERRABLE clause. Deferred enforcement is not
  234. // implemented, so DDL rejects it instead of accepting a weaker guarantee.
  235. Deferrable bool
  236. // OnConflict is the conflict resolution algorithm declared with
  237. // ON CONFLICT REPLACE/etc. (ConflictAbort is the zero value/default).
  238. OnConflict ConflictAction
  239. HasOnConflict bool
  240. }
  241. // ConstraintType represents the type of constraint.
  242. type ConstraintType int
  243. const (
  244. ConstraintPrimaryKey ConstraintType = iota
  245. ConstraintNotNull
  246. ConstraintUnique
  247. ConstraintDefault
  248. ConstraintCheck
  249. ConstraintForeignKey
  250. ConstraintAutoIncrement
  251. )
  252. // ReferentialAction is a parsed foreign-key ON DELETE/ON UPDATE action.
  253. type ReferentialAction int
  254. const (
  255. // RefActionUnspecified is the default (no clause, equivalent to NO ACTION).
  256. RefActionUnspecified ReferentialAction = iota
  257. RefActionNoAction
  258. RefActionRestrict
  259. RefActionCascade
  260. RefActionSetNull
  261. RefActionSetDefault
  262. )
  263. // String returns the SQL spelling of the action.
  264. func (a ReferentialAction) String() string {
  265. switch a {
  266. case RefActionNoAction:
  267. return "NO ACTION"
  268. case RefActionRestrict:
  269. return "RESTRICT"
  270. case RefActionCascade:
  271. return "CASCADE"
  272. case RefActionSetNull:
  273. return "SET NULL"
  274. case RefActionSetDefault:
  275. return "SET DEFAULT"
  276. default:
  277. return ""
  278. }
  279. }
  280. // TableConstraint represents a table-level constraint.
  281. type TableConstraint struct {
  282. Type ConstraintType
  283. Name string // optional constraint name
  284. Columns []string // columns involved
  285. RefTable string // for FOREIGN KEY
  286. RefColumns []string // for FOREIGN KEY
  287. Check Expr // for CHECK
  288. // OnDelete and OnUpdate record the foreign-key referential actions.
  289. OnDelete ReferentialAction
  290. OnUpdate ReferentialAction
  291. // Deferrable is set for a DEFERRABLE clause.
  292. Deferrable bool
  293. // OnConflict is the conflict resolution declared with ON CONFLICT
  294. // REPLACE/etc.; HasOnConflict distinguishes it from the default ABORT.
  295. OnConflict ConflictAction
  296. HasOnConflict bool
  297. }
  298. // DropTableStmt represents a DROP TABLE statement.
  299. type DropTableStmt struct {
  300. IfExists bool
  301. Tables []*TableRef
  302. }
  303. func (s *DropTableStmt) node() {}
  304. func (s *DropTableStmt) stmtNode() {}
  305. // CreateIndexStmt represents a CREATE INDEX statement.
  306. type CreateIndexStmt struct {
  307. IfNotExists bool
  308. Unique bool
  309. Name string
  310. Table string
  311. Columns []IndexColumn
  312. }
  313. func (s *CreateIndexStmt) node() {}
  314. func (s *CreateIndexStmt) stmtNode() {}
  315. // IndexColumn represents a column in an index. Expr is set for expression
  316. // indexes (e.g. an index on lower(email)); a plain column index leaves it nil
  317. // and uses Name.
  318. type IndexColumn struct {
  319. Name string
  320. Desc bool // true for DESC ordering
  321. Expr Expr
  322. }
  323. // DropIndexStmt represents a DROP INDEX statement.
  324. type DropIndexStmt struct {
  325. IfExists bool
  326. Name string
  327. }
  328. func (s *DropIndexStmt) node() {}
  329. func (s *DropIndexStmt) stmtNode() {}
  330. // CreateViewStmt represents a CREATE VIEW statement.
  331. type CreateViewStmt struct {
  332. IfNotExists bool
  333. View *TableRef
  334. Select *SelectStmt
  335. }
  336. func (s *CreateViewStmt) node() {}
  337. func (s *CreateViewStmt) stmtNode() {}
  338. // DropViewStmt represents a DROP VIEW statement.
  339. type DropViewStmt struct {
  340. IfExists bool
  341. Views []*TableRef
  342. }
  343. func (s *DropViewStmt) node() {}
  344. func (s *DropViewStmt) stmtNode() {}
  345. // AlterTableStmt represents an ALTER TABLE statement.
  346. type AlterTableStmt struct {
  347. Table string
  348. Action AlterAction
  349. }
  350. func (s *AlterTableStmt) node() {}
  351. func (s *AlterTableStmt) stmtNode() {}
  352. // AlterAction represents an ALTER TABLE action.
  353. type AlterAction interface {
  354. Node
  355. alterAction()
  356. }
  357. // AddColumnAction represents ADD COLUMN action.
  358. type AddColumnAction struct {
  359. Column *ColumnDef
  360. IfNotExists bool
  361. }
  362. func (a *AddColumnAction) node() {}
  363. func (a *AddColumnAction) alterAction() {}
  364. // DropColumnAction represents DROP COLUMN action.
  365. type DropColumnAction struct {
  366. Column string
  367. }
  368. func (a *DropColumnAction) node() {}
  369. func (a *DropColumnAction) alterAction() {}
  370. // RenameTableAction represents RENAME TO action.
  371. type RenameTableAction struct {
  372. NewName string
  373. }
  374. func (a *RenameTableAction) node() {}
  375. func (a *RenameTableAction) alterAction() {}
  376. // RenameColumnAction represents RENAME COLUMN action.
  377. type RenameColumnAction struct {
  378. OldName string
  379. NewName string
  380. }
  381. func (a *RenameColumnAction) node() {}
  382. func (a *RenameColumnAction) alterAction() {}
  383. // AttachStmt represents an ATTACH DATABASE statement.
  384. type AttachStmt struct {
  385. FilePath string // Database file path or identifier
  386. Alias string // Database alias name
  387. }
  388. func (s *AttachStmt) node() {}
  389. func (s *AttachStmt) stmtNode() {}
  390. // DetachStmt represents a DETACH DATABASE statement.
  391. type DetachStmt struct {
  392. Alias string // Database alias to detach
  393. }
  394. func (s *DetachStmt) node() {}
  395. func (s *DetachStmt) stmtNode() {}
  396. // PragmaStmt represents a PRAGMA statement.
  397. type PragmaStmt struct {
  398. Name string // pragma name (e.g., "table_info")
  399. Arg string // optional argument (e.g., table name)
  400. Value Expr // optional value for SET pragmas
  401. }
  402. func (s *PragmaStmt) node() {}
  403. func (s *PragmaStmt) stmtNode() {}
  404. // AnalyzeStmt represents an ANALYZE statement. PizzaSQL does not maintain
  405. // optimizer statistics, so it is accepted and executed as a documented no-op.
  406. type AnalyzeStmt struct {
  407. Name string // optional table name
  408. }
  409. func (s *AnalyzeStmt) node() {}
  410. func (s *AnalyzeStmt) stmtNode() {}
  411. // ExplainStmt represents an EXPLAIN statement.
  412. type ExplainStmt struct {
  413. QueryPlan bool // true for EXPLAIN QUERY PLAN
  414. Statement Statement // the statement being explained
  415. }
  416. func (s *ExplainStmt) node() {}
  417. func (s *ExplainStmt) stmtNode() {}
  418. // Transaction statements
  419. // BeginStmt represents a BEGIN TRANSACTION statement.
  420. type BeginStmt struct {
  421. // Transaction mode (DEFERRED, IMMEDIATE, EXCLUSIVE) - for future use
  422. Mode string
  423. }
  424. func (s *BeginStmt) node() {}
  425. func (s *BeginStmt) stmtNode() {}
  426. // CommitStmt represents a COMMIT statement.
  427. type CommitStmt struct{}
  428. func (s *CommitStmt) node() {}
  429. func (s *CommitStmt) stmtNode() {}
  430. // RollbackStmt represents a ROLLBACK statement.
  431. type RollbackStmt struct {
  432. Savepoint string // for ROLLBACK TO SAVEPOINT
  433. }
  434. func (s *RollbackStmt) node() {}
  435. func (s *RollbackStmt) stmtNode() {}
  436. // SavepointStmt represents a SAVEPOINT statement.
  437. type SavepointStmt struct {
  438. Name string
  439. }
  440. func (s *SavepointStmt) node() {}
  441. func (s *SavepointStmt) stmtNode() {}
  442. // ReleaseStmt represents a RELEASE SAVEPOINT statement.
  443. type ReleaseStmt struct {
  444. Name string
  445. }
  446. func (s *ReleaseStmt) node() {}
  447. func (s *ReleaseStmt) stmtNode() {}
  448. // Notification statements
  449. // ListenStmt represents LISTEN channel.
  450. type ListenStmt struct {
  451. Channel string
  452. }
  453. func (s *ListenStmt) node() {}
  454. func (s *ListenStmt) stmtNode() {}
  455. // UnlistenStmt represents UNLISTEN channel or UNLISTEN *.
  456. type UnlistenStmt struct {
  457. Channel string
  458. All bool
  459. }
  460. func (s *UnlistenStmt) node() {}
  461. func (s *UnlistenStmt) stmtNode() {}
  462. // NotifyStmt represents NOTIFY channel [, payload].
  463. type NotifyStmt struct {
  464. Channel string
  465. Payload string
  466. }
  467. func (s *NotifyStmt) node() {}
  468. func (s *NotifyStmt) stmtNode() {}
  469. // SetStmt represents a PostgreSQL SET statement. Drivers and ORMs issue these
  470. // for session configuration; PizzaSQL accepts them for compatibility and only
  471. // gives search_path observable behavior. Name is the normalized setting name
  472. // (for example "search_path" or "time zone") and Values holds the unquoted
  473. // value tokens in declaration order.
  474. type SetStmt struct {
  475. Name string
  476. Values []string
  477. Local bool
  478. }
  479. func (s *SetStmt) node() {}
  480. func (s *SetStmt) stmtNode() {}
  481. // ShowStmt represents a PostgreSQL SHOW statement for a single setting.
  482. type ShowStmt struct {
  483. Name string
  484. }
  485. func (s *ShowStmt) node() {}
  486. func (s *ShowStmt) stmtNode() {}
  487. // Expression types
  488. // BinaryExpr represents a binary expression.
  489. type BinaryExpr struct {
  490. Left Expr
  491. Op lexer.TokenType
  492. Right Expr
  493. }
  494. func (e *BinaryExpr) node() {}
  495. func (e *BinaryExpr) exprNode() {}
  496. // UnaryExpr represents a unary expression.
  497. type UnaryExpr struct {
  498. Op lexer.TokenType
  499. Operand Expr
  500. }
  501. func (e *UnaryExpr) node() {}
  502. func (e *UnaryExpr) exprNode() {}
  503. // LiteralExpr represents a literal value.
  504. type LiteralExpr struct {
  505. Type lexer.TokenType // TokenNumber, TokenString, TokenNULL, TokenTRUE, TokenFALSE
  506. Value string
  507. }
  508. func (e *LiteralExpr) node() {}
  509. func (e *LiteralExpr) exprNode() {}
  510. // ColumnRef represents a column reference.
  511. type ColumnRef struct {
  512. Table string
  513. Column string
  514. }
  515. func (e *ColumnRef) node() {}
  516. func (e *ColumnRef) exprNode() {}
  517. // FunctionCall represents a function call.
  518. type FunctionCall struct {
  519. Name string
  520. Args []Expr
  521. Distinct bool // for COUNT(DISTINCT x)
  522. Star bool // for COUNT(*)
  523. }
  524. func (e *FunctionCall) node() {}
  525. func (e *FunctionCall) exprNode() {}
  526. // WindowExpr represents a function call with an OVER clause.
  527. type WindowExpr struct {
  528. Func *FunctionCall
  529. PartitionBy []Expr
  530. OrderBy []OrderByItem
  531. }
  532. func (e *WindowExpr) node() {}
  533. func (e *WindowExpr) exprNode() {}
  534. // SubqueryExpr represents a subquery expression.
  535. type SubqueryExpr struct {
  536. Query *SelectStmt
  537. }
  538. func (e *SubqueryExpr) node() {}
  539. func (e *SubqueryExpr) exprNode() {}
  540. // CaseExpr represents a CASE expression.
  541. type CaseExpr struct {
  542. Operand Expr // for CASE operand WHEN...
  543. Whens []WhenClause
  544. Else Expr
  545. }
  546. func (e *CaseExpr) node() {}
  547. func (e *CaseExpr) exprNode() {}
  548. // WhenClause represents a WHEN clause in CASE.
  549. type WhenClause struct {
  550. Condition Expr
  551. Result Expr
  552. }
  553. // InExpr represents an IN expression.
  554. type InExpr struct {
  555. Left Expr
  556. Not bool
  557. Values []Expr // IN (1, 2, 3)
  558. Subquery *SelectStmt // IN (SELECT ...)
  559. }
  560. func (e *InExpr) node() {}
  561. func (e *InExpr) exprNode() {}
  562. // BetweenExpr represents a BETWEEN expression.
  563. type BetweenExpr struct {
  564. Left Expr
  565. Not bool
  566. Low Expr
  567. High Expr
  568. }
  569. func (e *BetweenExpr) node() {}
  570. func (e *BetweenExpr) exprNode() {}
  571. // LikeExpr represents a LIKE expression.
  572. type LikeExpr struct {
  573. Left Expr
  574. Not bool
  575. Pattern Expr
  576. Escape Expr
  577. }
  578. func (e *LikeExpr) node() {}
  579. func (e *LikeExpr) exprNode() {}
  580. // IsNullExpr represents an IS NULL expression.
  581. type IsNullExpr struct {
  582. Left Expr
  583. Not bool
  584. }
  585. func (e *IsNullExpr) node() {}
  586. func (e *IsNullExpr) exprNode() {}
  587. // IsDistinctExpr represents `left IS DISTINCT FROM right` (Not=false) or
  588. // `left IS NOT DISTINCT FROM right` (Not=true). Unlike `=`, NULLs compare
  589. // equal to each other and distinct from non-NULLs.
  590. type IsDistinctExpr struct {
  591. Left Expr
  592. Right Expr
  593. Not bool
  594. }
  595. func (e *IsDistinctExpr) node() {}
  596. func (e *IsDistinctExpr) exprNode() {}
  597. // CastExpr represents a CAST expression.
  598. type CastExpr struct {
  599. Expr Expr
  600. Type DataType
  601. }
  602. func (e *CastExpr) node() {}
  603. func (e *CastExpr) exprNode() {}
  604. // ExistsExpr represents an EXISTS expression.
  605. type ExistsExpr struct {
  606. Subquery *SelectStmt
  607. }
  608. func (e *ExistsExpr) node() {}
  609. func (e *ExistsExpr) exprNode() {}
  610. // ArrayExpr represents the ARRAY[...] constructor. Elements are evaluated as
  611. // ordinary expressions; NULL elements survive as NULL array elements.
  612. type ArrayExpr struct {
  613. Elements []Expr
  614. }
  615. func (e *ArrayExpr) node() {}
  616. func (e *ArrayExpr) exprNode() {}
  617. // SubscriptExpr represents a 1-based array element access, arr[index].
  618. type SubscriptExpr struct {
  619. Expr Expr
  620. Index Expr
  621. }
  622. func (e *SubscriptExpr) node() {}
  623. func (e *SubscriptExpr) exprNode() {}
  624. // AnyAllExpr represents `left op ANY(array)` (All=false) or
  625. // `left op ALL(array)` (All=true). It evaluates with SQL three-valued logic.
  626. type AnyAllExpr struct {
  627. Left Expr
  628. Op lexer.TokenType
  629. All bool
  630. Array Expr
  631. }
  632. func (e *AnyAllExpr) node() {}
  633. func (e *AnyAllExpr) exprNode() {}
  634. // ParenExpr represents a parenthesized expression.
  635. type ParenExpr struct {
  636. Expr Expr
  637. }
  638. func (e *ParenExpr) node() {}
  639. func (e *ParenExpr) exprNode() {}