{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://artifacts.apis.io/saturnsql-com/json-schema/saturnsql-com-saved-query-schema.json",
  "title": "SavedQuery",
  "x-generated": "2026-11-23",
  "x-method": "derived",
  "x-generator": "derive-json-schema.py",
  "x-source": "openapi/saturnsql-com-openapi.yaml#/components/schemas/SavedQuery",
  "type": "object",
  "properties": {
    "id": {
      "type": "string"
    },
    "name": {
      "type": "string"
    },
    "sql": {
      "type": "string"
    },
    "connectionId": {
      "type": [
        "string",
        "null"
      ]
    },
    "folderPath": {
      "type": [
        "string",
        "null"
      ],
      "description": "A single flat folder label (not a nested path); \"A/B\" is one folder named \"A/B\", not \"A\" containing \"B\". null = root."
    },
    "isShared": {
      "type": "boolean"
    },
    "description": {
      "type": [
        "string",
        "null"
      ],
      "description": "Subtitle on the report page. Returned by GET /queries; nothing can set it yet, so always null."
    },
    "chart": {
      "allOf": [
        {
          "$ref": "#/$defs/ChartSpec"
        }
      ],
      "nullable": true,
      "description": "Pinned chart, or null to let the result shape decide. Returned by GET /queries for editors only (absent from GET /queries/{id}); set with PUT /queries/{id}/chart (session token only)."
    },
    "userId": {
      "type": "string"
    },
    "variables": {
      "type": "array",
      "description": "Declared `{{name}}` variables, in first-appearance order in the SQL. Empty when the query has none.",
      "items": {
        "$ref": "#/$defs/QueryVariable"
      }
    },
    "createdAt": {
      "type": "string",
      "format": "date-time"
    },
    "updatedAt": {
      "type": "string",
      "format": "date-time"
    }
  },
  "$defs": {
    "ChartSpec": {
      "type": "object",
      "description": "How a report should be drawn. Stored verbatim and interpreted at render time, since the columns a spec refers to only exist after a run: a spec naming a column the SELECT list no longer returns falls back to the shape-derived guess rather than failing.\n\nWith no spec stored, the result shape decides — see the \"saved query is also a report page\" section of the API guide for the rules an agent should write SQL against.",
      "required": [
        "type"
      ],
      "properties": {
        "type": {
          "type": "string",
          "enum": [
            "line",
            "area",
            "bar",
            "scatter",
            "share",
            "number",
            "none"
          ],
          "description": "`none` suppresses the chart for good, which is why it is distinct from a null `chart`. `share` is never guessed and must be set explicitly: its shape is identical to `bar` and nothing in the data says which is meant."
        },
        "x": {
          "type": "string",
          "description": "Column for the x axis or the category. Absent for `number`."
        },
        "series": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "maxItems": 3,
          "description": "Measure columns, in order. Capped at 3: the validated palette separates three series by lightness and a fourth has no colour."
        },
        "group": {
          "type": "string",
          "description": "Low-cardinality column splitting a scatter into groups."
        }
      }
    },
    "QueryVariable": {
      "type": "object",
      "description": "One `{{name}}` variable declared by a saved query. Values are supplied to POST /queries/{id}/run and substituted server side.",
      "required": [
        "name",
        "type"
      ],
      "properties": {
        "name": {
          "type": "string",
          "description": "Matches `[A-Za-z_][A-Za-z0-9_]*`"
        },
        "type": {
          "type": "string",
          "enum": [
            "date",
            "text",
            "number",
            "boolean",
            "dropdown"
          ]
        },
        "required": {
          "type": "boolean",
          "description": "An empty value for a required variable is a 422, not an empty substitution."
        },
        "default": {
          "type": [
            "object",
            "null"
          ],
          "description": "Used when the value is absent. A `relative` default is only valid on a `date` variable and resolves against the caller time zone (or the schedule time zone).",
          "properties": {
            "kind": {
              "type": "string",
              "enum": [
                "fixed",
                "relative"
              ]
            },
            "value": {
              "type": "string",
              "description": "Literal for `fixed`; one of today, yesterday, 7_days_ago, 30_days_ago, 90_days_ago, start_of_this_month, start_of_last_month, end_of_last_month, start_of_this_year for `relative`."
            }
          }
        },
        "options": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "description": "Dropdown only, non-empty. A value outside this list is a 422."
        }
      }
    }
  }
}