capabilities.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. ProxyErrors bool `json:"proxy_error_relay"`
  49. TLS string `json:"tls"`
  50. }
  51. type Notifications struct {
  52. Listen Feature `json:"listen"`
  53. Unlisten Feature `json:"unlisten"`
  54. Notify Feature `json:"notify"`
  55. PayloadLimit int `json:"payload_limit_bytes"`
  56. ChannelLimit int `json:"channel_limit_bytes"`
  57. Transactional bool `json:"transactional"`
  58. DurableReplay bool `json:"durable_replay"`
  59. CommitLSNParam string `json:"commit_lsn_parameter"`
  60. }
  61. type Constraints struct {
  62. NotNull Feature `json:"not_null"`
  63. Unique Feature `json:"unique"`
  64. PrimaryKey Feature `json:"primary_key"`
  65. CompositePK Feature `json:"composite_primary_key"`
  66. ForeignKey Feature `json:"foreign_key"`
  67. Check Feature `json:"check"`
  68. DefaultValues Feature `json:"default_values"`
  69. }
  70. type DDL struct {
  71. AddColumn Feature `json:"add_column"`
  72. AddColumnBackfill Feature `json:"add_column_backfill"`
  73. DropColumn Feature `json:"drop_column"`
  74. RenameColumn Feature `json:"rename_column"`
  75. RenameColumnIndexes Feature `json:"rename_column_updates_indexes"`
  76. CreateTableIfNotExists bool `json:"create_table_if_not_exists"`
  77. AddColumnIfNotExists bool `json:"add_column_if_not_exists"`
  78. }
  79. type Transactions struct {
  80. Model string `json:"model"`
  81. Isolation string `json:"isolation"`
  82. SerializationCode string `json:"serialization_sqlstate"`
  83. RowLocks Feature `json:"row_locks"`
  84. Savepoints Feature `json:"savepoints"`
  85. RetryRecommended bool `json:"retry_recommended"`
  86. }
  87. type Types struct {
  88. Integer bool `json:"integer"`
  89. Bigint bool `json:"bigint"`
  90. Real bool `json:"real"`
  91. Text bool `json:"text"`
  92. Blob bool `json:"blob"`
  93. Boolean bool `json:"boolean"`
  94. DateTime bool `json:"datetime"`
  95. JSONText bool `json:"json_as_text"`
  96. JSONB Feature `json:"jsonb"`
  97. Arrays Feature `json:"arrays"`
  98. RowID Feature `json:"rowid"`
  99. }
  100. type Parameters struct {
  101. Positional bool `json:"positional"`
  102. MaxOID string `json:"max_oid"`
  103. Arrays Feature `json:"arrays"`
  104. BinaryNumeric bool `json:"binary_numeric"`
  105. }
  106. type JSON struct {
  107. Storage string `json:"storage"`
  108. Functions string `json:"functions"`
  109. ArrowOperators Feature `json:"arrow_operators"`
  110. InvalidInput string `json:"invalid_input"`
  111. }
  112. type Documentation struct {
  113. Compatibility string `json:"compatibility"`
  114. Isolation string `json:"isolation"`
  115. }
  116. // Current returns the capability document for this build.
  117. func Current() *Document {
  118. return &Document{
  119. SchemaVersion: SchemaVersion,
  120. Engine: "pizzasql",
  121. EngineVersion: version.String(),
  122. Commit: version.Commit,
  123. GeneratedAt: time.Now().UTC().Format(time.RFC3339),
  124. Protocol: Protocol{
  125. PostgresCompat: "14.0",
  126. SQLiteCompat: "3.35.0",
  127. SimpleQuery: true,
  128. ExtendedQuery: true,
  129. ProxyErrors: true,
  130. TLS: "terminated by the managed pgproxy, not the engine listener",
  131. },
  132. Notifications: Notifications{
  133. Listen: Feature{Supported: true, Status: "enforced"},
  134. Unlisten: Feature{Supported: true, Status: "enforced", Notes: "UNLISTEN * removes every subscription"},
  135. Notify: Feature{Supported: true, Status: "enforced", Notes: "duplicate (channel,payload) pairs collapse within one transaction"},
  136. PayloadLimit: 8000,
  137. ChannelLimit: 63,
  138. Transactional: true,
  139. DurableReplay: false,
  140. CommitLSNParam: "pizzasql.lsn",
  141. },
  142. Constraints: Constraints{
  143. NotNull: Feature{Supported: true, Status: "enforced", SQLState: "23502", Notes: "validated after defaults and assignments; explicit NULL does not select a default"},
  144. Unique: Feature{Supported: true, Status: "enforced", SQLState: "23505", Notes: "unique indexes, INLINE UNIQUE, and ON CONFLICT are enforced atomically"},
  145. PrimaryKey: Feature{Supported: true, Status: "enforced", Notes: "single-column only"},
  146. CompositePK: Feature{Supported: false, Status: "rejected", SQLState: "0A000", Notes: "declare a single-column primary key"},
  147. 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"},
  148. Check: Feature{Supported: false, Status: "rejected", SQLState: "0A000", Notes: "inline and table-level CHECK are rejected; enforce the rule in the application"},
  149. 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"},
  150. },
  151. DDL: DDL{
  152. AddColumn: Feature{Supported: true, Status: "metadata_only"},
  153. AddColumnBackfill: Feature{Supported: false, Status: "no_backfill", Notes: "existing rows read NULL; a DEFAULT applies to later INSERTs only"},
  154. DropColumn: Feature{Supported: true, Status: "rewrites_rows"},
  155. RenameColumn: Feature{Supported: true, Status: "rewrites_rows"},
  156. RenameColumnIndexes: Feature{Supported: false, Status: "stale_index_metadata", Notes: "drop and recreate indexes that reference a renamed column"},
  157. CreateTableIfNotExists: true,
  158. AddColumnIfNotExists: true,
  159. },
  160. Transactions: Transactions{
  161. Model: "optimistic compare-batch commit; no MVCC or snapshots",
  162. Isolation: "serializable-equivalent for single-row compare-batch commits; write skew is possible across independent rows",
  163. SerializationCode: "40001",
  164. RowLocks: Feature{Supported: false, Status: "rejected", SQLState: "0A000", Notes: "SELECT ... FOR UPDATE/SHARE and NOWAIT/SKIP LOCKED are rejected"},
  165. Savepoints: Feature{Supported: true, Status: "enforced"},
  166. RetryRecommended: true,
  167. },
  168. Types: Types{
  169. Integer: true,
  170. Bigint: true,
  171. Real: true,
  172. Text: true,
  173. Blob: true,
  174. Boolean: true,
  175. DateTime: true,
  176. JSONText: true,
  177. 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"},
  178. Arrays: Feature{Supported: false, Status: "rejected", SQLState: "0A000"},
  179. 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"},
  180. },
  181. Parameters: Parameters{
  182. Positional: true,
  183. MaxOID: "int8/text/bool/bytea/float; arrays are rejected",
  184. Arrays: Feature{Supported: false, Status: "rejected", SQLState: "0A000", Notes: "id = ANY($1) is rejected; expand to IN (...) with individual parameters"},
  185. BinaryNumeric: true,
  186. },
  187. JSON: JSON{
  188. Storage: "TEXT (JSON and JSONB type names are accepted without native semantics; insert JSON as text)",
  189. 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",
  190. ArrowOperators: Feature{Supported: false, Status: "rejected", SQLState: "42601", Notes: "-> and ->> are not implemented; use json_extract"},
  191. InvalidInput: "json_extract/json_set throw 'malformed JSON'; json_valid returns 0",
  192. },
  193. ErrorCodes: map[string]string{
  194. "23502": "not_null_violation",
  195. "23505": "unique_violation",
  196. "40001": "serialization_failure",
  197. "0A000": "feature_not_supported",
  198. "42501": "insufficient_privilege (managed proxy)",
  199. "42601": "syntax_error",
  200. "25P02": "in_failed_sql_transaction",
  201. "08P01": "protocol_violation (managed proxy: TLS required)",
  202. "53300": "too_many_connections (managed proxy: organization quota)",
  203. "XX000": "internal_error",
  204. },
  205. Documentation: Documentation{
  206. Compatibility: "https://docs.database.pizza/sql-reference/compatibility/",
  207. Isolation: "https://docs.database.pizza/engine/concurrency/",
  208. },
  209. }
  210. }