{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Source Comparer Configuration",
  "description": "Defines required fields for the application that uses the DW Source Comparer library. The application configuration can have other root-level fields.",
  "type": "object",
  "$defs": {
    "commentFields": {
      "patternProperties": {
        "^__": {
          "type": ["string", "number", "integer", "boolean", "object", "array", "null"],
          "description": "Comment field begins with two underscores. There can be any number of comment fields."
        }
      }
    },
    "jdbcOptions": {
      "title":  "JDBC Options",
      "description": "These options are applicable to all JDBC databases - currently SQL Server and Oracle. See https://spark.apache.org/docs/latest/sql-data-sources-jdbc.html for details.",
      "type": "object",
      "properties": {
        "url": {
          "type": "string"
        },
        "dbtable": {
          "anyOf": [
            {
              "type": "string"
            },
            {
              "type": "array",
              "items": {
                "type": "string"
              }
            }
          ]
        },
        "query": {
          "anyOf": [
            {
              "type": "string"
            },
            {
              "type": "array",
              "items": {
                "type": "string"
              }
            }
          ]
        },
        "prepareQuery": {
          "anyOf": [
            {
              "type": "string"
            },
            {
              "type": "array",
              "items": {
                "type": "string"
              }
            }
          ]
        },
        "lowerBound": {
          "type": "string"
        },
        "upperBound": {
          "type": "string"
        },
        "numPartitions": {
          "type": "number"
        },
        "queryTimeout": {
          "type": "number"
        },
        "fetchsize": {
          "type": "number"
        },
        "sessionInitStatement": {
          "type": "string"
        },
        "pushDownPredicate": {
          "type": "boolean"
        },
        "pushDownAggregate": {
          "type": "boolean"
        },
        "pushDownLimit": {
          "type": "boolean"
        },
        "pushDownOffset": {
          "type": "boolean"
        },
        "pushDownTableSample": {
          "type": "boolean"
        },
        "keytab": {
          "type": "string"
        },
        "principal": {
          "type": "string"
        },
        "refreshKrb5Config": {
          "type": "boolean"
        },
        "preferTimestampNTZ": {
          "type": "boolean"
        },
        "hint": {
          "type": "string"
        }
      },
      "not": {
        "required": ["partitionColumn"],
        "description": "\"partitionColumn\" is excluded from the list of options for the JDBC source, since this column has to be defined in \"schema\" with the \"isPartitionColumn\" flag."
      },
      "allOf": [ { "$ref": "#/$defs/commentFields" } ],
      "required": [
        "url"
      ],
      "customMessage": {
        "not": "The field \"partitionColumn\" is not allowed within the dbms object, as it must be defined in the \"schema\" with the \"isPartitionColumn\" flag set to true (the tool needs the column type for internal logic)."
      },
      "unevaluatedProperties": false
    },
    "compareSource": {
      "title":  "Compare source",
      "description": "This definition is used for both the left and right compare sources.",
      "type": "object",
      "oneOf": [
        {
          "type": "object",
          "properties": {
            "file": {
              "type": "object",
              "oneOf": [
                {
                  "type": "object",
                  "properties": {
                    "parquet": {
                      "type": "object",
                      "properties": {
                      }
                    }
                  },
                  "allOf": [ { "$ref": "#/$defs/commentFields" } ],
                  "required": [
                    "parquet"
                  ]
                },
                {
                  "type": "object",
                  "properties": {
                    "csv": {
                      "type": "object",
                      "properties": {
                        "mode": {
                          "type": "string"
                        },
                        "header": {
                          "type": "boolean"
                        },
                        "encoding": {
                          "type": "string"
                        },
                        "dateFormat": {
                          "type": "string"
                        },
                        "nullValue": {
                          "type": "string"
                        },
                        "escape": {
                          "type": "string"
                        },
                        "quote": {
                          "type": "string"
                        },
                        "delimiter": {
                          "type": "string"
                        },
                        "multiLine": {
                          "type": "boolean"
                        },
                        "inferSchema": {
                          "type": "boolean"
                        },
                        "prefersDate": {
                          "type": "boolean"
                        },
                        "trueValue": {
                          "type": "string"
                        },
                        "falseValue": {
                          "type": "string"
                        }
                      },
                      "allOf": [ { "$ref": "#/$defs/commentFields" } ],
                      "unevaluatedProperties": false
                    }
                  },
                  "allOf": [ { "$ref": "#/$defs/commentFields" } ],
                  "required": [
                    "csv"
                  ]
                }
              ],
              "properties": {
                "path": {
                  "type": "string"
                }
              },
              "allOf": [ { "$ref": "#/$defs/commentFields" } ],
              "required": [
                "path"
              ],
              "unevaluatedProperties": false
            }
          },
          "allOf": [ { "$ref": "#/$defs/commentFields" } ],
          "required": [
            "file"
          ]
        },
        {
          "type": "object",
          "properties": {
            "dbms": {
              "type": "object",
              "oneOf": [
                {
                  "type": "object",
                  "properties": {
                    "sqlServer": {
                      "$ref": "#/$defs/jdbcOptions",
                      "description": "Options for the SQL Server JDBC connection."
                    }
                  },
                  "allOf": [ { "$ref": "#/$defs/commentFields" } ],
                  "required": [
                    "sqlServer"
                  ]
                },
                {
                  "type": "object",
                  "properties": {
                    "oracle": {
                      "$ref": "#/$defs/jdbcOptions",
                      "description": "Options for the Oracle JDBC connection."                    }
                  },
                  "allOf": [ { "$ref": "#/$defs/commentFields" } ],
                  "required": [
                    "oracle"
                  ]
                },
                {
                  "type": "object",
                  "properties": {
                    "mongoDb": {
                      "type": "object",
                      "description": "Properties for mongoDb can be any of the options supported by the MongoDB Spark connector in batch mode. The schema must be defined.",
                      "properties": {
                        "connection.uri": {
                          "type": "string"
                        },
                        "database": {
                          "type": "string"
                        },
                        "collection": {
                          "type": "string"
                        },
                        "comment": {
                          "type": "string"
                        },
                        "mode": {
                          "type": "string"
                        },
                        "columnNameOfCorruptRecord": {
                          "type": "string"
                        },
                        "mongoClientFactory": {
                          "type": "string"
                        },
                        "partitioner": {
                          "type": "string"
                        },
                        "sampleSize": {
                          "type": "number"
                        },
                        "sql.inferSchema.mapTypes.enabled": {
                          "type": "boolean"
                        },
                        "sql.inferSchema.mapTypes.minimum.key.size": {
                          "type": "number"
                        },
                        "aggregation.pipeline": {
                          "anyOf": [
                            { "type": "string" },
                            {
                              "type": "array",
                              "items": { "type": "string" }
                            }
                          ]
                        },
                        "aggregation.allowDiskUse": {
                          "type": "boolean"
                        },
                        "outputExtendedJson": {
                          "type": "boolean"
                        }
                      },
                      "allOf": [ { "$ref": "#/$defs/commentFields" } ],
                      "patternProperties": {
                        "^partitioner\\.options\\.": { "type": "string" }
                      },
                      "required": [
                        "connection.uri", "collection"
                      ],
                      "unevaluatedProperties": false                          }
                  },
                  "allOf": [ { "$ref": "#/$defs/commentFields" } ],
                  "required": [
                    "mongoDb"
                  ]
                }
              ],
              "properties": {
                "user": {
                  "type": "string"
                },
                "password": {
                  "type": "string"
                }
              },
              "allOf": [ { "$ref": "#/$defs/commentFields" } ],
              "required": [
              ],
              "unevaluatedProperties": false
            }
          },
          "required": [
            "dbms"
          ]
        }
      ],
      "properties": {
        "label": {
          "type": "string",
          "description": "Used to identify the source in the compare results. If none is defined, the default is \"left\" for the first source and \"right\" for the second one."
        },
        "schema": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "colName": {
                "type": "string",
                "description": "Column name"
              },
              "colType": {
                "type": "string",
                "description": "Column type"
              },
              "isPartitionColumn": {
                "type": "boolean",
                "description": "For a DBMS source, one column can be defined as the partition column to read data on multiple connections in parallel. isPartitionColumn must be defined if numPartitions is defined."
              },
              "formats": {
                "description": "Allowed formats for a Timestamp or Date column when loaded from a csv file. Requires colType to be Timestamp or Date.",
                "type": "array",
                "minItems": 1,
                "items": {
                  "type": "string"
                },
                "customMessage": {
                  "minItems": "\"formats\" array must have at least one format defined."
                }
              }
            },
            "required": [
              "colName"
            ],
            "allOf": [
              { "$ref": "#/$defs/commentFields" },
              {
                "if": {
                  "properties": {
                    "isPartitionColumn": {
                      "const": true
                    }
                  },
                  "required": ["isPartitionColumn"]
                },
                "then": {
                  "description": "If \"isPartitionColumn\" attribute of a column is set to true, colType must be defined (it is needed to determine the type of the upper and lower bounds).",
                  "required": ["colType"],
                  "customMessage": {
                    "required": "\"colType\" is required when \"isPartitionColumn\" is true."
                  }
                }
              },
              {
                "if": {
                  "required": ["formats"]
                },
                "then": {
                  "description": "\"formats\" is only allowed for columns of type Timestamp or Date, so \"colType\" must be defined and be one of them.",
                  "required": ["colType"],
                  "properties": {
                    "colType": {
                      "pattern": "^([Tt][Ii][Mm][Ee][Ss][Tt][Aa][Mm][Pp]|[Dd][Aa][Tt][Ee])$",
                      "customMessage": {
                        "pattern": "\"formats\" is only allowed when \"colType\" is Timestamp or Date."
                      }
                    }
                  },
                  "customMessage": {
                    "required": "\"colType\" is required when \"formats\" is defined, and it must be Timestamp or Date."
                  }
                }
              }
            ],
            "unevaluatedProperties": false
          }
        },
        "subsetQuery": {
          "description": "A query to create a subset of columns and/or rows to compare. It can be any valid select statement using Spark SQL syntax, executed on the data loaded from the source. The source table must be designated by three underscores, e.g., SELECT CAST(id AS INT) AS id FROM ___ . If not defined, all fields and all rows from the source will be used.",
          "anyOf": [
            {
              "type": "string",
              "pattern":  "^.*___.*$",
              "customMessage": {
                "type": "\"subsetQuery\" must be a string or an array of strings (this message may be displayed even if the type is string or array, when there are other errors; ignore it in that case).",
                "pattern": "The query must have a three-underscore ( ___ ) placeholder for the source table."
              }
            },
            {
              "type": "array",
              "items": {
                "type": "string"
              },
              "contains": {
                "pattern": "^.*___.*$"
              },
              "customMessage": {
                "type": "\"subsetQuery\" must be a string or an array of strings (this message may be displayed even if the type is string or array, when there are other errors; ignore it in that case).",
                "contains": "At least one element in the array must have a three-underscore ( ___ ) placeholder for the source table."
              }
            }
          ]
        }
      },
      "allOf": [
        { "$ref": "#/$defs/commentFields" },
        {
          "if": {
            "description": "If any column in the schema defines \"formats\", the source must be a csv file.",
            "properties": {
              "schema": {
                "contains": { "required": ["formats"] }
              }
            },
            "required": ["schema"]
          },
          "then": {
            "properties": {
              "file": {
                "required": ["csv"],
                "customMessage": {
                  "required": "\"formats\" is only allowed for a csv file source. A parquet file source does not use \"formats\"."
                }
              }
            },
            "required": ["file"],
            "customMessage": {
              "required": "\"formats\" is only allowed for a csv file source (\"file\" with \"csv\"). It is not allowed for a DBMS source."
            }
          }
        }
      ],
      "required": [
        "label"
      ],
      "unevaluatedProperties": false
    }
  },
  "properties": {
    "sourceCompare": {
      "type": "object",
      "description": "Configuration for source compare",
      "properties": {
        "compareScenarios": {
          "type": "array",
          "description": "Specifications for comparing sources",
          "items": {
            "type": "object",
            "properties": {
              "name": {
                "type": "string",
                "description": "The name of the compare scenario, used to differentiate multiple scenarios. Names must be unique."
              },
              "isActive": {
                "type": "boolean",
                "description": "If the value of isActive is false, the compare scenario will not be processed."
              },
              "isDebugDwLib": {
                "type": "boolean",
                "description": "If the value is true, the library will send some dataframe-related output to standard output, which slows down job execution."
              },
              "floatThreshold": {
                "type": "number",
                "description": "When comparing two floating-point values, they will be considered equal if the absolute value of their difference is less than floatThreshold."
              },
              "timestampTruncateUnit": {
                "type": "string",
                "description": "When comparing two timestamp fields, they will be truncated using the defined unit before being compared.",
                "pattern": "^(?i)(year|yyyy|yy|month|mon|mm|day|dd|microsecond|millisecond|second|minute|hour|week|quarter)$",
                "customMessage": {
                  "pattern": "The allowed values for timestampTruncateUnit are year, yyyy, yy, month, mon, mm, day, dd, microsecond, millisecond, second, minute, hour, week, and quarter."
                }
              },
              "dateTruncateUnit": {
                "type": "string",
                "description": "When comparing two date fields, they will be truncated using the defined unit before being compared.",
                "pattern": "^(?i)(year|yyyy|yy|month|mon|mm|week|quarter)$",
                "customMessage": {
                  "pattern": "The allowed values for dateTruncateUnit are year, yyyy, yy, month, mon, mm, week, and quarter."
                }
              },
              "maxSameDifferences": {
                "type": "number",
                "description": "When defined, the number of reported differences for each column will not exceed the specified value. That will reduce the size of the resulting differences file."
              },
              "compareResult": {
                "type": "object",
                "description": "Specification for the result of the comparison",
                "properties": {
                  "file": {
                    "type": "object",
                    "properties": {
                      "dir": {
                        "type": "string"
                      },
                      "csv": {
                        "type": "object",
                        "description": "Properties for the CSV file. By default, the saved file is multiline.",
                        "properties": {
                          "mode": {
                            "type": "string"
                          },
                          "header": {
                            "type": "boolean"
                          },
                          "encoding": {
                            "type": "string"
                          },
                          "dateFormat": {
                            "type": "string"
                          },
                          "nullValue": {
                            "type": "string"
                          },
                          "escape": {
                            "type": "string"
                          },
                          "quote": {
                            "type": "string"
                          },
                          "delimiter": {
                            "type": "string"
                          }
                        },
                        "unevaluatedProperties": false
                      }
                    },
                    "allOf": [ { "$ref": "#/$defs/commentFields" } ],
                    "required": [
                      "dir",
                      "csv"
                    ],
                    "unevaluatedProperties": false
                  },
                  "schema": {
                    "type": "array",
                    "minItems": 1,
                    "items": {
                      "type": "object",
                      "properties": {
                        "colName": {
                          "type": "string",
                          "description": "Column name"
                        },
                        "isUniqueKey": {
                          "type": "boolean",
                          "description": "At least one column must have this property set to true.\nMultiple columns can be designated as unique keys to create a compound unique key.\nBy default, a unique key is included unless the isInclude value is set to false.\n"
                        },
                        "isInclude": {
                          "type": "boolean",
                          "description": "By default, all columns are compared and included in the comparison result, unless at least one column has an explicit isInclude flag set (to true or false).\nIf that's the case, only columns with the isInclude flag set to true will be compared and included in the comparison result.\nIf columns are listed that do not have the isInclude flag, they will be ignored, unless they are keys.\nFor keys (isUniqueKey is set to true), the default for isInclude is true.\n"
                        }
                      },
                      "allOf": [ { "$ref": "#/$defs/commentFields" } ],
                      "required": [
                        "colName"
                      ],
                      "unevaluatedProperties": false
                    },
                    "allOf" : [
                      {
                        "contains": {
                          "type": "object",
                          "required": [
                            "isUniqueKey"
                          ],
                          "properties": {
                            "isUniqueKey": {
                              "const": true
                            }
                          }
                        },
                        "minContains": 1,
                        "customMessage": {
                          "contains": "Compare result's schema does not have a unique key defined. At least one column must have \"isUniqueKey\" flag set to true, i.e., ... \"isUniqueKey\" : true, ..."
                        }
                      }
                    ],
                    "customMessage": {
                      "minItems": "Compare result's schema does not have any columns defined. It must have at least one column."
                    }
                  }
                },
                "allOf": [ { "$ref": "#/$defs/commentFields" } ],
                "required": [
                  "file",
                  "schema"
                ],
                "unevaluatedProperties": false
              },
              "leftSource": {
                "$ref": "#/$defs/compareSource",
                "description": "The first (left) source to compare. Both the left and right sections are identical in structure, but they can have different types of sources. For example, one can be a DBMS and the other a file. The sets being compared must have common columns that match compareResult.schema."
              },
              "rightSource": {
                "$ref": "#/$defs/compareSource",
                "description": "The second (right) source to compare. Both the left and right sections are identical in structure, but they can have different types of sources. For example, one can be a DBMS and the other a file. The sets being compared must have common columns that match compareResult.schema."
              }
            },
            "allOf": [ { "$ref": "#/$defs/commentFields" } ],
            "required": [
              "name",
              "isActive",
              "compareResult",
              "leftSource",
              "rightSource"
            ],
            "unevaluatedProperties": false
          }
        }
      },
      "allOf": [ { "$ref": "#/$defs/commentFields" } ],
      "required": [
        "compareScenarios"
      ],
      "unevaluatedProperties": false
    }
  },
  "required": [
    "sourceCompare"
  ],
  "unevaluatedProperties": true
}
