Restrictions are utilized to validate queries by ensuring that only supported operations are applied to the columns of tables. The restrictions determine how values are compared against table columns in SQL queries. Below is a breakdown of how the restrictions are validated, the available operations, and the conditions under which they are applied.
The following operations are supported in the restriction schema:
=: Equal to – Checks if the column value is equal to a given value.>: Greater than – Checks if the column value is greater than a specified value.<: Less than – Checks if the column value is less than a given value.>=: Greater than or equal to – Checks if the column value is greater than or equal to a specified value.<=: Less than or equal to – Checks if the column value is less than or equal to a given value.BETWEEN: Between two values – Validates if the column value is within a specified range.IN: In a specified list of values – Validates if the column value matches any of the values in the given list.
Each restriction in the configuration consists of the following keys:
column: The name of the column the restriction is applied to (e.g.,"price"or"order_id").valueorvalues: The value(s) to compare the column against:- If the operation is
BETWEEN, thevaluesfield should contain a list of two numeric values, representing the lower and upper bounds of the range. - For operations like
INor comparison operations (e.g.,=,>,<=), thevalueorvaluesfield will contain one or more values to compare.
- If the operation is
operation: The operation to apply to the column. This could be any of the supported operations, such asBETWEEN,IN,=,>, etc.
-
BETWEEN:
- The
BETWEENoperation requires thevaluesfield to contain a list of exactly two numeric values. The first value must be less than the second. - Example:
"operation" : "BETWEEN", "values": [100, 200] - In this case, the
pricecolumn must have a value between 100 and 200.
- The
-
IN:
- The
INoperation requires thevaluesfield to be a list containing multiple values to match the column against. The values can be of types such as integers, floats, or strings. - Example:
"operation": "IN", "values": [100, 200, 300] - In this case, the
categorycolumn will be checked to see if its value matches one of the values in the list: 100, 200, or 300.
- The
-
Comparison Operations (>=, <=, =, <, >):
- These operations apply a comparison between the column and a single value. The value must be numeric for comparison operations like
>=,<, etc. - Example:
"operation": ">=", "value": 100 - In this case, the
pricecolumn must have a value greater than or equal to 100.
- These operations apply a comparison between the column and a single value. The value must be numeric for comparison operations like
The validation function checks that the restrictions adhere to the following rules and raises errors if any of these conditions are violated:
-
Unsupported Operations:
- If an unsupported operation is used in the configuration, an
UnsupportedRestrictionErroris raised. Only operations listed in the "Supported Operations" section are allowed.
- If an unsupported operation is used in the configuration, an
-
Missing Columns or Tables:
- If a table in the configuration is missing either the
columnsortable_namefields, or if no tables are provided in the configuration, aValueErroris raised. Every table must specify these fields.
- If a table in the configuration is missing either the
-
Invalid Data Types:
- If the
valueorvaluesin the restriction do not match the expected data types (e.g., using non-numeric values for comparison operations), aValueErrorwill be raised. For example: - A
BETWEENoperation that doesn’t provide a list of two numeric values will trigger an error:This would raise an error because the values are not numeric."operation": "BETWEEN", "values": ["A", "B"]
- If the
-
Invalid
INFormat:- If the
INoperation is provided with invalid data types (e.g., a list with mixed types like numbers and strings), it will also result in a validation error:This would raise an error because the values are not consistently of the same data type."operation": "IN", "values": [100, "Electronics"]
- If the