capabilities.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Package capabilities describes the SQL surface an engine build supports.
  2. //
  3. // The document is machine-readable and versioned separately from the engine
  4. // build. Clients should compare SchemaVersion when parsing it and use
  5. // EngineVersion to pin behavior to a specific PizzaSQL release. Keep this file
  6. // in sync with the tests it advertises: every claim here must be covered by an
  7. // engine test in the same repository.
  8. package capabilities
  9. import (
  10. "time"
  11. "github.com/danfragoso/pizzasql-next/pkg/version"
  12. )
  13. // SchemaVersion changes only when the document's shape or semantics change in a
  14. // way clients must handle. Additive fields do not require a bump.
  15. const SchemaVersion = 1
  16. // Feature reports whether a capability is supported and, when it is not,
  17. // whether the engine rejects the syntax or accepts it with different semantics.
  18. type Feature struct {
  19. Supported bool `json:"supported"`
  20. Status string `json:"status"`
  21. SQLState string `json:"sqlstate,omitempty"`
  22. Notes string `json:"notes,omitempty"`
  23. }
  24. // Document is the machine-readable capability description served by
  25. // GET /capabilities.
  26. type Document struct {
  27. SchemaVersion int `json:"schema_version"`
  28. Engine string `json:"engine"`
  29. EngineVersion string `json:"engine_version"`
  30. Commit string `json:"commit"`
  31. GeneratedAt string `json:"generated_at,omitempty"`
  32. Protocol Protocol `json:"protocol"`
  33. Notifications Notifications `json:"notifications"`
  34. Constraints Constraints `json:"constraints"`
  35. DDL DDL `json:"ddl"`
  36. Transactions Transactions `json:"transactions"`
  37. Types Types `json:"types"`
  38. Parameters Parameters `json:"parameters"`
  39. JSON JSON `json:"json"`
  40. ErrorCodes map[string]string `json:"error_codes"`
  41. Documentation Documentation `json:"documentation"`
  42. }
  43. type Protocol struct {
  44. PostgresCompat string `json:"postgres_compat"`
  45. SQLiteCompat string `json:"sqlite_compat"`
  46. SimpleQuery bool `json:"simple_query"`
  47. ExtendedQuery bool `json:"extended_query"`
  48. TLS string `json:"tls"`
  49. }
  50. type Notifications struct {
  51. Listen Feature `json:"listen"`
  52. Unlisten Feature `json:"unlisten"`
  53. Notify Feature `json:"notify"`
  54. PayloadLimit int `json:"payload_limit_bytes"`
  55. ChannelLimit int `json:"channel_limit_bytes"`
  56. Transactional bool `json:"transactional"`
  57. DurableReplay bool `json:"durable_replay"`
  58. CommitLSNParam string `json:"commit_lsn_parameter"`
  59. }
  60. type Constraints struct {
  61. NotNull Feature `json:"not_null"`
  62. Unique Feature `json:"unique"`
  63. PrimaryKey Feature `json:"primary_key"`
  64. CompositePK Feature `json:"composite_primary_key"`
  65. ForeignKey Feature `json:"foreign_key"`
  66. Check Feature `json:"check"`
  67. DefaultValues Feature `json:"default_values"`
  68. }
  69. type DDL struct {
  70. AddColumn Feature `json:"add_column"`
  71. AddColumnBackfill Feature `json:"add_column_backfill"`
  72. DropColumn Feature `json:"drop_column"`
  73. RenameColumn Feature `json:"rename_column"`
  74. RenameColumnIndexes Feature `json:"rename_column_updates_indexes"`
  75. CreateTableIfNotExists bool `json:"create_table_if_not_exists"`
  76. AddColumnIfNotExists bool `json:"add_column_if_not_exists"`
  77. }
  78. type Transactions struct {
  79. Model string `json:"model"`
  80. Isolation string `json:"isolation"`
  81. SerializationCode string `json:"serialization_sqlstate"`
  82. RowLocks Feature `json:"row_locks"`
  83. Savepoints Feature `json:"savepoints"`
  84. RetryRecommended bool `json:"retry_recommended"`
  85. }
  86. type Types struct {
  87. Integer bool `json:"integer"`
  88. Bigint bool `json:"bigint"`
  89. Real bool `json:"real"`
  90. Text bool `json:"text"`
  91. Blob bool `json:"blob"`
  92. Boolean bool `json:"boolean"`
  93. DateTime bool `json:"datetime"`
  94. JSONText bool `json:"json_as_text"`
  95. JSONB Feature `json:"jsonb"`
  96. Arrays Feature `json:"arrays"`
  97. RowID Feature `json:"rowid"`
  98. }
  99. type Parameters struct {
  100. Positional bool `json:"positional"`
  101. MaxOID string `json:"max_oid"`
  102. Arrays Feature `json:"arrays"`
  103. BinaryNumeric bool `json:"binary_numeric"`
  104. }
  105. type JSON struct {
  106. Storage string `json:"storage"`
  107. Functions string `json:"functions"`
  108. ArrowOperators Feature `json:"arrow_operators"`
  109. InvalidInput string `json:"invalid_input"`
  110. }
  111. type Documentation struct {
  112. Compatibility string `json:"compatibility"`
  113. Isolation string `json:"isolation"`
  114. }
  115. // Current returns the capability document for this build.
  116. func Current() *Document {
  117. return &Document{
  118. SchemaVersion: SchemaVersion,
  119. Engine: "pizzasql",
  120. EngineVersion: version.String(),
  121. Commit: version.Commit,
  122. GeneratedAt: time.Now().UTC().Format(time.RFC3339),
  123. Protocol: Protocol{
  124. PostgresCompat: "14.0",
  125. SQLiteCompat: "3.35.0",
  126. SimpleQuery: true,
  127. ExtendedQuery: true,
  128. TLS: "terminated by the managed pgproxy, not the engine listener",
  129. },
  130. Notifications: Notifications{
  131. Listen: Feature{Supported: true, Status: "enforced"},
  132. Unlisten: Feature{Supported: true, Status: "enforced", Notes: "UNLISTEN * removes every subscription"},
  133. Notify: Feature{Supported: true, Status: "enforced", Notes: "duplicate (channel,payload) pairs collapse within one transaction"},
  134. PayloadLimit: 8000,
  135. ChannelLimit: 63,
  136. Transactional: true,
  137. DurableReplay: false,
  138. CommitLSNParam: "pizzasql.lsn",
  139. },
  140. Constraints: Constraints{
  141. NotNull: Feature{Supported: true, Status: "enforced"},
  142. Unique: Feature{Supported: true, Status: "enforced", SQLState: "23505", Notes: "unique indexes, INLINE UNIQUE, and ON CONFLICT are enforced atomically"},
  143. PrimaryKey: Feature{Supported: true, Status: "enforced", Notes: "single-column only"},
  144. CompositePK: Feature{Supported: false, Status: "rejected", SQLState: "0A000", Notes: "declare a single-column primary key"},
  145. ForeignKey: Feature{Supported: false, Status: "rejected", SQLState: "0A000", Notes: "inline and table-level FOREIGN KEY are rejected; enforce referential integrity in the application, cascades are not implemented"},
  146. Check: Feature{Supported: false, Status: "rejected", SQLState: "0A000", Notes: "inline and table-level CHECK are rejected; enforce the rule in the application"},
  147. DefaultValues: Feature{Supported: true, Status: "constant_only", SQLState: "0A000", Notes: "non-constant defaults (CURRENT_TIMESTAMP, function calls) are rejected instead of being frozen at DDL time"},
  148. },
  149. DDL: DDL{
  150. AddColumn: Feature{Supported: true, Status: "metadata_only"},
  151. AddColumnBackfill: Feature{Supported: false, Status: "no_backfill", Notes: "existing rows read NULL; a DEFAULT applies to later INSERTs only"},
  152. DropColumn: Feature{Supported: true, Status: "rewrites_rows"},
  153. RenameColumn: Feature{Supported: true, Status: "rewrites_rows"},
  154. RenameColumnIndexes: Feature{Supported: false, Status: "stale_index_metadata", Notes: "drop and recreate indexes that reference a renamed column"},
  155. CreateTableIfNotExists: true,
  156. AddColumnIfNotExists: true,
  157. },
  158. Transactions: Transactions{
  159. Model: "optimistic compare-batch commit; no MVCC or snapshots",
  160. Isolation: "serializable-equivalent for single-row compare-batch commits; write skew is possible across independent rows",
  161. SerializationCode: "40001",
  162. RowLocks: Feature{Supported: false, Status: "rejected", SQLState: "0A000", Notes: "SELECT ... FOR UPDATE/SHARE and NOWAIT/SKIP LOCKED are rejected"},
  163. Savepoints: Feature{Supported: true, Status: "enforced"},
  164. RetryRecommended: true,
  165. },
  166. Types: Types{
  167. Integer: true,
  168. Bigint: true,
  169. Real: true,
  170. Text: true,
  171. Blob: true,
  172. Boolean: true,
  173. DateTime: true,
  174. JSONText: true,
  175. JSONB: Feature{Supported: false, Status: "type_name_alias", Notes: "JSONB is accepted as a column type name with no native semantics; values are stored as text and reported as TEXT over the wire"},
  176. Arrays: Feature{Supported: false, Status: "rejected", SQLState: "0A000"},
  177. RowID: Feature{Supported: true, Status: "auto_assigned", Notes: "rowid/oid/_rowid_ are BIGINT over the wire; monotonic when the engine assigns the rowid, stable across UPDATE, not monotonic for client-assigned descending INTEGER PRIMARY KEY values"},
  178. },
  179. Parameters: Parameters{
  180. Positional: true,
  181. MaxOID: "int8/text/bool/bytea/float; arrays are rejected",
  182. Arrays: Feature{Supported: false, Status: "rejected", SQLState: "0A000", Notes: "id = ANY($1) is rejected; expand to IN (...) with individual parameters"},
  183. BinaryNumeric: true,
  184. },
  185. JSON: JSON{
  186. Storage: "TEXT (JSON and JSONB type names are accepted without native semantics; insert JSON as text)",
  187. Functions: "json, json_extract, json_set, json_insert, json_replace, json_remove, json_valid, json_type, json_array, json_object, json_quote, json_group_array",
  188. ArrowOperators: Feature{Supported: false, Status: "rejected", SQLState: "42601", Notes: "-> and ->> are not implemented; use json_extract"},
  189. InvalidInput: "json_extract/json_set throw 'malformed JSON'; json_valid returns 0",
  190. },
  191. ErrorCodes: map[string]string{
  192. "23505": "unique_violation",
  193. "40001": "serialization_failure",
  194. "0A000": "feature_not_supported",
  195. "42601": "syntax_error",
  196. "25P02": "in_failed_sql_transaction",
  197. "08P01": "protocol_violation (managed proxy: TLS required)",
  198. "53300": "too_many_connections (managed proxy: organization quota)",
  199. "XX000": "internal_error",
  200. },
  201. Documentation: Documentation{
  202. Compatibility: "https://docs.database.pizza/sql-reference/compatibility/",
  203. Isolation: "https://docs.database.pizza/engine/concurrency/",
  204. },
  205. }
  206. }