DeepLcom / sql-mock

A Python library to test your SQL models using mocked input data
MIT License
34 stars 5 forks source link

Create a column type for StringArray #58

Open amane-toda opened 6 months ago

amane-toda commented 6 months ago

Context: My team are using Clickhouse, and integrate sql-mock into it.

Problem: While we are able to support most column types, I am currently writing a custom object to handle StringArrays.

class StringArray(col.ClickhouseColumnMock):
    dtype = "Array(String)"

    def to_sql(self, column_name: str, value=NO_INPUT) -> str:
        # Note: Compare against NO_INPUT instead of checking for None since None could be a valid input for nullable columns
        val = value if not isinstance(value, NoInput) else self.default
        # In case the val is None, we convert it to NULL
        if val is None:
            return f"cast(NULL AS {self.dtype}) AS {column_name}"
        return f"cast({val} AS {self.dtype}) AS {column_name}" 

Proposal: Would it be possible to write this directly into the sql-mock object, similar to how I can call col.String or col.Decimal. Would be great if I could call col.StringArray