Repository: gventuri/pandas-ai
File: pandasai/data_loader/semantic_layer_schema.py (Python)

Code before:
            raise ValueError(
                f"Unsupported column type: {type}. Supported types are: {VALID_COLUMN_TYPES}"
            )
        return type

    @field_validator("expression")
    @classmethod
    def is_expression_valid(cls, expr: str) -> str | None:
        if expr is None:
            return expr
        try:
            parse_one(expr)
            return expr
        except ParseError as e:
            raise ValueError(f"Invalid SQL expression: {expr}. Error: {str(e)}")


class Relation(BaseModel):
    name: str | None = Field(None, description="Name of the relationship.")
    description: str | None = Field(None, description="Description of the relationship.")
    from_: str = Field(..., alias="from", description="Source column for the relationship.")
    to: str = Field(..., description="Target column for the relationship.")


class TransformationParams(BaseModel):
    column: str | None = Field(None, description="Column to transform")
    value: str | int | float | bool | None = Field(
        None, description="Value for fill_na and other transformations"
    )
    mapping: Dict[str, str] | None = Field(
        None, description="Mapping dictionary for map_values transformation"
    )
    format: str | None = Field(None, description="Format string for date formatting")
    decimals: int | None = Field(
        None, description="Number of decimal places for rounding"
    )
    factor: int | float | None = Field(None, description="Scaling factor")
    to_tz: str | None = Field(None, description="Target timezone or format")
    from_tz: str | None = Field(None, description="From timezone or format")
    errors: str | None = Field(
        None, description="Error handling mode for numeric/datetime conversion"
    )
    old_value: Any | None = Field(
        None, description="Old value for replace transformation"
    )
    new_value: Any | None = Field(
        None, description="New value for replace transformation"
    )
    new_name: str | None = Field(
        None, description="New name for column in rename transformation"
    )

Review comment (suggestion):
Rest of things looks good but let's revert it to Optional the library supports python >=3.9.  `str | None` this won't work for python 3.9

Code after:
            raise ValueError(
                f"Unsupported column type: {type}. Supported types are: {VALID_COLUMN_TYPES}"
            )
        return type

    @field_validator("expression")
    @classmethod
    def is_expression_valid(cls, expr: str) -> Optional[str]:
        if expr is None:
            return expr
        try:
            parse_one(expr)
            return expr
        except ParseError as e:
            raise ValueError(f"Invalid SQL expression: {expr}. Error: {str(e)}")


class Relation(BaseModel):
    name: Optional[str] = Field(None, description="Name of the relationship.")
    description: Optional[str] = Field(None, description="Description of the relationship.")
    from_: str = Field(..., alias="from", description="Source column for the relationship.")
    to: str = Field(..., description="Target column for the relationship.")


class TransformationParams(BaseModel):
    column: Optional[str] = Field(None, description="Column to transform")
    value: Optional[Union[str, int, float, bool]] = Field(
        None, description="Value for fill_na and other transformations"
    )
    mapping: Optional[Dict[str, str]] = Field(
        None, description="Mapping dictionary for map_values transformation"
    )
    format: Optional[str] = Field(None, description="Format string for date formatting")
    decimals: Optional[int] = Field(
        None, description="Number of decimal places for rounding"
    )
    factor: Optional[Union[int, float]] = Field(None, description="Scaling factor")
    to_tz: Optional[str] = Field(None, description="Target timezone or format")
    from_tz: Optional[str] = Field(None, description="From timezone or format")
    errors: Optional[str] = Field(
        None, description="Error handling mode for numeric/datetime conversion"
    )
    old_value: Optional[Any] = Field(
        None, description="Old value for replace transformation"
    )
    new_value: Optional[Any] = Field(
        None, description="New value for replace transformation"
    )
    new_name: Optional[str] = Field(
        None, description="New name for column in rename transformation"
    )