capabilities.go 8.8 KB

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