{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "https://raw.githubusercontent.com/ksco92/eevee/main/packages/core/src/schema/table.schema.json",
    "title": "FDD Table Definition",
    "description": "Structural schema (Layer 1) for an FDD table-definition file.",
    "type": "object",
    "required": [
        "specVersion",
        "description",
        "tableType",
        "isRawData",
        "columns",
        "primaryKey"
    ],
    "additionalProperties": false,
    "properties": {
        "$schema": {
            "type": "string"
        },
        "specVersion": {
            "type": "string",
            "enum": [
                "0"
            ]
        },
        "description": {
            "type": "string",
            "minLength": 1
        },
        "tableType": {
            "type": "string",
            "enum": [
                "hive_parquet",
                "iceberg_parquet_v2",
                "postgres_18"
            ]
        },
        "isRawData": {
            "type": "boolean"
        },
        "formatVersion": {
            "type": "integer"
        },
        "columns": {
            "type": "array",
            "minItems": 1,
            "items": {
                "$ref": "#/definitions/column"
            }
        },
        "primaryKey": {
            "type": "array",
            "minItems": 1,
            "items": {
                "type": "string",
                "minLength": 1
            }
        },
        "partitions": {
            "type": "array",
            "items": {
                "$ref": "#/definitions/partition"
            }
        },
        "sortOrder": {
            "type": "array",
            "items": {
                "$ref": "#/definitions/sortField"
            }
        },
        "identifierFields": {
            "type": "array",
            "items": {
                "type": "string",
                "minLength": 1
            }
        },
        "indexes": {
            "type": "array",
            "items": {
                "$ref": "#/definitions/index"
            }
        },
        "uniqueConstraints": {
            "type": "array",
            "items": {
                "$ref": "#/definitions/uniqueConstraint"
            }
        },
        "checkConstraints": {
            "type": "array",
            "items": {
                "$ref": "#/definitions/checkConstraint"
            }
        },
        "exclusionConstraints": {
            "type": "array",
            "items": {
                "$ref": "#/definitions/exclusionConstraint"
            }
        },
        "bucketing": {
            "$ref": "#/definitions/bucketing"
        },
        "skewedBy": {
            "$ref": "#/definitions/skewedBy"
        },
        "storage": {
            "$ref": "#/definitions/hiveStorage"
        },
        "dataQuality": {
            "$ref": "#/definitions/dataQuality"
        },
        "tableProperties": {
            "type": "object",
            "additionalProperties": {
                "type": "string"
            }
        },
        "dependsOn": {
            "type": "array",
            "items": {
                "type": "string",
                "pattern": "^[a-z0-9_]+\\.[a-z0-9_]+$"
            }
        },
        "foreignKeys": {
            "type": "array",
            "items": {
                "$ref": "#/definitions/foreignKey"
            }
        }
    },
    "definitions": {
        "column": {
            "type": "object",
            "required": [
                "name",
                "type",
                "description"
            ],
            "additionalProperties": false,
            "properties": {
                "name": {
                    "type": "string",
                    "minLength": 1
                },
                "type": {
                    "type": "string",
                    "minLength": 1
                },
                "description": {
                    "type": "string",
                    "minLength": 1
                },
                "id": {
                    "type": "integer"
                },
                "nullable": {
                    "type": "boolean"
                },
                "generated": {
                    "type": "string",
                    "minLength": 1
                },
                "expression": {
                    "type": "string",
                    "minLength": 1
                },
                "expressionColumns": {
                    "type": "array",
                    "items": {
                        "type": "string",
                        "minLength": 1
                    }
                },
                "identity": {
                    "type": "string",
                    "minLength": 1
                },
                "default": {
                    "type": "string",
                    "minLength": 1
                },
                "collation": {
                    "type": "string",
                    "minLength": 1
                },
                "compression": {
                    "type": "string",
                    "minLength": 1
                },
                "storage": {
                    "type": "string",
                    "minLength": 1
                }
            }
        },
        "partition": {
            "type": "object",
            "required": [
                "name",
                "type",
                "description"
            ],
            "additionalProperties": false,
            "properties": {
                "name": {
                    "type": "string",
                    "minLength": 1
                },
                "type": {
                    "type": "string",
                    "minLength": 1
                },
                "description": {
                    "type": "string",
                    "minLength": 1
                },
                "fieldId": {
                    "type": "integer"
                }
            }
        },
        "index": {
            "type": "object",
            "required": [
                "name",
                "method",
                "columns"
            ],
            "additionalProperties": false,
            "properties": {
                "name": {
                    "type": "string",
                    "minLength": 1
                },
                "method": {
                    "type": "string",
                    "minLength": 1
                },
                "unique": {
                    "type": "boolean"
                },
                "columns": {
                    "type": "array",
                    "minItems": 1,
                    "items": {
                        "$ref": "#/definitions/indexColumn"
                    }
                },
                "include": {
                    "type": "array",
                    "items": {
                        "type": "string",
                        "minLength": 1
                    }
                },
                "where": {
                    "type": "string",
                    "minLength": 1
                }
            }
        },
        "exclusionConstraint": {
            "type": "object",
            "required": [
                "name",
                "using",
                "elements"
            ],
            "additionalProperties": false,
            "properties": {
                "name": {
                    "type": "string",
                    "minLength": 1
                },
                "using": {
                    "type": "string",
                    "minLength": 1
                },
                "elements": {
                    "type": "array",
                    "minItems": 1,
                    "items": {
                        "$ref": "#/definitions/exclusionElement"
                    }
                }
            }
        },
        "exclusionElement": {
            "type": "object",
            "required": [
                "column",
                "operator"
            ],
            "additionalProperties": false,
            "properties": {
                "column": {
                    "type": "string",
                    "minLength": 1
                },
                "operator": {
                    "type": "string",
                    "minLength": 1
                }
            }
        },
        "skewedBy": {
            "type": "object",
            "required": [
                "columns",
                "on"
            ],
            "additionalProperties": false,
            "properties": {
                "columns": {
                    "type": "array",
                    "minItems": 1,
                    "items": {
                        "type": "string",
                        "minLength": 1
                    }
                },
                "on": {
                    "type": "array",
                    "items": {
                        "type": "array",
                        "items": {
                            "type": "string",
                            "minLength": 1
                        }
                    }
                },
                "storedAsDirectories": {
                    "type": "boolean"
                }
            }
        },
        "hiveStorage": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "storedAs": {
                    "type": "string",
                    "minLength": 1
                },
                "serdeProperties": {
                    "type": "object",
                    "additionalProperties": {
                        "type": "string"
                    }
                }
            }
        },
        "dataQuality": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "awsDqdl": {
                    "type": "array",
                    "items": {
                        "type": "string",
                        "minLength": 1
                    }
                }
            }
        },
        "bucketing": {
            "type": "object",
            "required": [
                "columns",
                "bucketCount"
            ],
            "additionalProperties": false,
            "properties": {
                "columns": {
                    "type": "array",
                    "minItems": 1,
                    "items": {
                        "type": "string",
                        "minLength": 1
                    }
                },
                "bucketCount": {
                    "type": "integer"
                },
                "sortedBy": {
                    "type": "array",
                    "items": {
                        "$ref": "#/definitions/bucketSort"
                    }
                }
            }
        },
        "bucketSort": {
            "type": "object",
            "required": [
                "column",
                "direction"
            ],
            "additionalProperties": false,
            "properties": {
                "column": {
                    "type": "string",
                    "minLength": 1
                },
                "direction": {
                    "type": "string",
                    "minLength": 1
                }
            }
        },
        "uniqueConstraint": {
            "type": "object",
            "required": [
                "name",
                "columns"
            ],
            "additionalProperties": false,
            "properties": {
                "name": {
                    "type": "string",
                    "minLength": 1
                },
                "columns": {
                    "type": "array",
                    "minItems": 1,
                    "items": {
                        "type": "string",
                        "minLength": 1
                    }
                },
                "nullsNotDistinct": {
                    "type": "boolean"
                }
            }
        },
        "checkConstraint": {
            "type": "object",
            "required": [
                "name",
                "expression",
                "columns"
            ],
            "additionalProperties": false,
            "properties": {
                "name": {
                    "type": "string",
                    "minLength": 1
                },
                "expression": {
                    "type": "string",
                    "minLength": 1
                },
                "columns": {
                    "type": "array",
                    "minItems": 1,
                    "items": {
                        "type": "string",
                        "minLength": 1
                    }
                }
            }
        },
        "indexColumn": {
            "type": "object",
            "required": [
                "name"
            ],
            "additionalProperties": false,
            "properties": {
                "name": {
                    "type": "string",
                    "minLength": 1
                },
                "sort": {
                    "type": "string",
                    "minLength": 1
                },
                "nulls": {
                    "type": "string",
                    "minLength": 1
                }
            }
        },
        "sortField": {
            "type": "object",
            "required": [
                "column",
                "direction",
                "nullOrder"
            ],
            "additionalProperties": false,
            "properties": {
                "column": {
                    "type": "string",
                    "minLength": 1
                },
                "transform": {
                    "type": "string",
                    "minLength": 1
                },
                "direction": {
                    "type": "string",
                    "minLength": 1
                },
                "nullOrder": {
                    "type": "string",
                    "minLength": 1
                }
            }
        },
        "foreignKey": {
            "type": "object",
            "required": [
                "sourceTable",
                "sourceColumn",
                "localColumn",
                "allowNulls"
            ],
            "additionalProperties": false,
            "properties": {
                "sourceTable": {
                    "type": "string",
                    "pattern": "^[a-z0-9_]+\\.[a-z0-9_]+$"
                },
                "sourceColumn": {
                    "type": "string",
                    "minLength": 1
                },
                "localColumn": {
                    "type": "string",
                    "minLength": 1
                },
                "allowNulls": {
                    "type": "boolean"
                }
            }
        }
    }
}
