Skip to content

literals

AboveMax

Bases: Literal[L]

Source code in pyiceberg/expressions/literals.py
class AboveMax(Literal[L]):
    def __repr__(self) -> str:
        """Return the string representation of the AboveMax class."""
        return f"{self.__class__.__name__}()"

    def __str__(self) -> str:
        """Return the string representation of the AboveMax class."""
        return self.__class__.__name__

__repr__()

Return the string representation of the AboveMax class.

Source code in pyiceberg/expressions/literals.py
def __repr__(self) -> str:
    """Return the string representation of the AboveMax class."""
    return f"{self.__class__.__name__}()"

__str__()

Return the string representation of the AboveMax class.

Source code in pyiceberg/expressions/literals.py
def __str__(self) -> str:
    """Return the string representation of the AboveMax class."""
    return self.__class__.__name__

BelowMin

Bases: Literal[L]

Source code in pyiceberg/expressions/literals.py
class BelowMin(Literal[L]):
    def __repr__(self) -> str:
        """Return the string representation of the BelowMin class."""
        return f"{self.__class__.__name__}()"

    def __str__(self) -> str:
        """Return the string representation of the BelowMin class."""
        return self.__class__.__name__

__repr__()

Return the string representation of the BelowMin class.

Source code in pyiceberg/expressions/literals.py
def __repr__(self) -> str:
    """Return the string representation of the BelowMin class."""
    return f"{self.__class__.__name__}()"

__str__()

Return the string representation of the BelowMin class.

Source code in pyiceberg/expressions/literals.py
def __str__(self) -> str:
    """Return the string representation of the BelowMin class."""
    return self.__class__.__name__

FloatLiteral

Bases: Literal[float]

Source code in pyiceberg/expressions/literals.py
class FloatLiteral(Literal[float]):
    def __init__(self, value: float) -> None:
        super().__init__(value, float)
        self._value32 = struct.unpack("<f", struct.pack("<f", value))[0]

    def __eq__(self, other: Any) -> bool:
        """Return the equality of two instances of the FloatLiteral class."""
        return self._value32 == other

    def __lt__(self, other: Any) -> bool:
        """Return if one instance of the FloatLiteral class is less than another instance."""
        return self._value32 < other

    def __gt__(self, other: Any) -> bool:
        """Return if one instance of the FloatLiteral class is greater than another instance."""
        return self._value32 > other

    def __le__(self, other: Any) -> bool:
        """Return if one instance of the FloatLiteral class is less than or equal to another instance."""
        return self._value32 <= other

    def __ge__(self, other: Any) -> bool:
        """Return if one instance of the FloatLiteral class is greater than or equal to another instance."""
        return self._value32 >= other

    def __hash__(self) -> int:
        """Return a hashed representation of the FloatLiteral class."""
        return hash(self._value32)

    @singledispatchmethod
    def to(self, type_var: IcebergType) -> Literal:  # type: ignore
        raise TypeError(f"Cannot convert FloatLiteral into {type_var}")

    @to.register(FloatType)
    def _(self, _: FloatType) -> Literal[float]:
        return self

    @to.register(DoubleType)
    def _(self, _: DoubleType) -> Literal[float]:
        return DoubleLiteral(self.value)

    @to.register(DecimalType)
    def _(self, type_var: DecimalType) -> Literal[Decimal]:
        return DecimalLiteral(Decimal(self.value).quantize(Decimal((0, (1,), -type_var.scale)), rounding=ROUND_HALF_UP))

__eq__(other)

Return the equality of two instances of the FloatLiteral class.

Source code in pyiceberg/expressions/literals.py
def __eq__(self, other: Any) -> bool:
    """Return the equality of two instances of the FloatLiteral class."""
    return self._value32 == other

__ge__(other)

Return if one instance of the FloatLiteral class is greater than or equal to another instance.

Source code in pyiceberg/expressions/literals.py
def __ge__(self, other: Any) -> bool:
    """Return if one instance of the FloatLiteral class is greater than or equal to another instance."""
    return self._value32 >= other

__gt__(other)

Return if one instance of the FloatLiteral class is greater than another instance.

Source code in pyiceberg/expressions/literals.py
def __gt__(self, other: Any) -> bool:
    """Return if one instance of the FloatLiteral class is greater than another instance."""
    return self._value32 > other

__hash__()

Return a hashed representation of the FloatLiteral class.

Source code in pyiceberg/expressions/literals.py
def __hash__(self) -> int:
    """Return a hashed representation of the FloatLiteral class."""
    return hash(self._value32)

__le__(other)

Return if one instance of the FloatLiteral class is less than or equal to another instance.

Source code in pyiceberg/expressions/literals.py
def __le__(self, other: Any) -> bool:
    """Return if one instance of the FloatLiteral class is less than or equal to another instance."""
    return self._value32 <= other

__lt__(other)

Return if one instance of the FloatLiteral class is less than another instance.

Source code in pyiceberg/expressions/literals.py
def __lt__(self, other: Any) -> bool:
    """Return if one instance of the FloatLiteral class is less than another instance."""
    return self._value32 < other

Literal

Bases: Generic[L], ABC

Literal which has a value and can be converted between types.

Source code in pyiceberg/expressions/literals.py
class Literal(Generic[L], ABC):
    """Literal which has a value and can be converted between types."""

    _value: L

    def __init__(self, value: L, value_type: Type[L]):
        if value is None or not isinstance(value, value_type):
            raise TypeError(f"Invalid literal value: {value!r} (not a {value_type})")
        if isinstance(value, float) and isnan(value):
            raise ValueError("Cannot create expression literal from NaN.")
        self._value = value

    @property
    def value(self) -> L:
        return self._value

    @singledispatchmethod
    @abstractmethod
    def to(self, type_var: IcebergType) -> Literal[L]: ...  # pragma: no cover

    def __repr__(self) -> str:
        """Return the string representation of the Literal class."""
        return f"{type(self).__name__}({self.value!r})"

    def __str__(self) -> str:
        """Return the string representation of the Literal class."""
        return str(self.value)

    def __hash__(self) -> int:
        """Return a hashed representation of the Literal class."""
        return hash(self.value)

    def __eq__(self, other: Any) -> bool:
        """Return the equality of two instances of the Literal class."""
        if not isinstance(other, Literal):
            return False
        return self.value == other.value

    def __ne__(self, other: Any) -> bool:
        """Return the inequality of two instances of the Literal class."""
        return not self.__eq__(other)

    def __lt__(self, other: Any) -> bool:
        """Return if one instance of the Literal class is less than another instance."""
        return self.value < other.value

    def __gt__(self, other: Any) -> bool:
        """Return if one instance of the Literal class is greater than another instance."""
        return self.value > other.value

    def __le__(self, other: Any) -> bool:
        """Return if one instance of the Literal class is less than or equal to another instance."""
        return self.value <= other.value

    def __ge__(self, other: Any) -> bool:
        """Return if one instance of the Literal class is greater than or equal to another instance."""
        return self.value >= other.value

__eq__(other)

Return the equality of two instances of the Literal class.

Source code in pyiceberg/expressions/literals.py
def __eq__(self, other: Any) -> bool:
    """Return the equality of two instances of the Literal class."""
    if not isinstance(other, Literal):
        return False
    return self.value == other.value

__ge__(other)

Return if one instance of the Literal class is greater than or equal to another instance.

Source code in pyiceberg/expressions/literals.py
def __ge__(self, other: Any) -> bool:
    """Return if one instance of the Literal class is greater than or equal to another instance."""
    return self.value >= other.value

__gt__(other)

Return if one instance of the Literal class is greater than another instance.

Source code in pyiceberg/expressions/literals.py
def __gt__(self, other: Any) -> bool:
    """Return if one instance of the Literal class is greater than another instance."""
    return self.value > other.value

__hash__()

Return a hashed representation of the Literal class.

Source code in pyiceberg/expressions/literals.py
def __hash__(self) -> int:
    """Return a hashed representation of the Literal class."""
    return hash(self.value)

__le__(other)

Return if one instance of the Literal class is less than or equal to another instance.

Source code in pyiceberg/expressions/literals.py
def __le__(self, other: Any) -> bool:
    """Return if one instance of the Literal class is less than or equal to another instance."""
    return self.value <= other.value

__lt__(other)

Return if one instance of the Literal class is less than another instance.

Source code in pyiceberg/expressions/literals.py
def __lt__(self, other: Any) -> bool:
    """Return if one instance of the Literal class is less than another instance."""
    return self.value < other.value

__ne__(other)

Return the inequality of two instances of the Literal class.

Source code in pyiceberg/expressions/literals.py
def __ne__(self, other: Any) -> bool:
    """Return the inequality of two instances of the Literal class."""
    return not self.__eq__(other)

__repr__()

Return the string representation of the Literal class.

Source code in pyiceberg/expressions/literals.py
def __repr__(self) -> str:
    """Return the string representation of the Literal class."""
    return f"{type(self).__name__}({self.value!r})"

__str__()

Return the string representation of the Literal class.

Source code in pyiceberg/expressions/literals.py
def __str__(self) -> str:
    """Return the string representation of the Literal class."""
    return str(self.value)

StringLiteral

Bases: Literal[str]

Source code in pyiceberg/expressions/literals.py
class StringLiteral(Literal[str]):
    def __init__(self, value: str) -> None:
        super().__init__(value, str)

    @singledispatchmethod
    def to(self, type_var: IcebergType) -> Literal:  # type: ignore
        raise TypeError(f"Cannot convert StringLiteral into {type_var}")

    @to.register(StringType)
    def _(self, _: StringType) -> Literal[str]:
        return self

    @to.register(IntegerType)
    def _(self, type_var: IntegerType) -> Literal[int]:
        try:
            number = int(float(self.value))

            if IntegerType.max < number:
                return IntAboveMax()
            elif IntegerType.min > number:
                return IntBelowMin()
            return LongLiteral(number)
        except ValueError as e:
            raise ValueError(f"Could not convert {self.value} into a {type_var}") from e

    @to.register(LongType)
    def _(self, type_var: LongType) -> Literal[int]:
        try:
            long_value = int(float(self.value))
            if LongType.max < long_value:
                return LongAboveMax()
            elif LongType.min > long_value:
                return LongBelowMin()
            else:
                return LongLiteral(long_value)
        except (TypeError, ValueError) as e:
            raise ValueError(f"Could not convert {self.value} into a {type_var}") from e

    @to.register(DateType)
    def _(self, type_var: DateType) -> Literal[int]:
        try:
            return DateLiteral(date_str_to_days(self.value))
        except (TypeError, ValueError) as e:
            raise ValueError(f"Could not convert {self.value} into a {type_var}") from e

    @to.register(TimeType)
    def _(self, type_var: TimeType) -> Literal[int]:
        try:
            return TimeLiteral(time_str_to_micros(self.value))
        except (TypeError, ValueError) as e:
            raise ValueError(f"Could not convert {self.value} into a {type_var}") from e

    @to.register(TimestampType)
    def _(self, _: TimestampType) -> Literal[int]:
        return TimestampLiteral(timestamp_to_micros(self.value))

    @to.register(TimestamptzType)
    def _(self, _: TimestamptzType) -> Literal[int]:
        return TimestampLiteral(timestamptz_to_micros(self.value))

    @to.register(UUIDType)
    def _(self, _: UUIDType) -> Literal[bytes]:
        return UUIDLiteral(UUID(self.value).bytes)

    @to.register(DecimalType)
    def _(self, type_var: DecimalType) -> Literal[Decimal]:
        dec = Decimal(self.value)
        scale = abs(int(dec.as_tuple().exponent))
        if type_var.scale == scale:
            return DecimalLiteral(dec)
        else:
            raise ValueError(f"Could not convert {self.value} into a {type_var}, scales differ {type_var.scale} <> {scale}")

    @to.register(BooleanType)
    def _(self, type_var: BooleanType) -> Literal[bool]:
        value_upper = self.value.upper()
        if value_upper in ["TRUE", "FALSE"]:
            return BooleanLiteral(value_upper == "TRUE")
        else:
            raise ValueError(f"Could not convert {self.value} into a {type_var}")

    def __repr__(self) -> str:
        """Return the string representation of the StringLiteral class."""
        return f"literal({repr(self.value)})"

__repr__()

Return the string representation of the StringLiteral class.

Source code in pyiceberg/expressions/literals.py
def __repr__(self) -> str:
    """Return the string representation of the StringLiteral class."""
    return f"literal({repr(self.value)})"

literal(value)

Construct an Iceberg Literal based on Python primitive data type.

Parameters:

Name Type Description Default
value Python primitive type

the value to be associated with literal.

required
Example

from pyiceberg.expressions.literals import literal.

literal(123) LongLiteral(123)

Source code in pyiceberg/expressions/literals.py
def literal(value: L) -> Literal[L]:
    """
    Construct an Iceberg Literal based on Python primitive data type.

    Args:
        value (Python primitive type): the value to be associated with literal.

    Example:
        from pyiceberg.expressions.literals import literal.
        >>> literal(123)
        LongLiteral(123)
    """
    if isinstance(value, float):
        return DoubleLiteral(value)  # type: ignore
    elif isinstance(value, bool):
        return BooleanLiteral(value)
    elif isinstance(value, int):
        return LongLiteral(value)
    elif isinstance(value, str):
        return StringLiteral(value)
    elif isinstance(value, UUID):
        return UUIDLiteral(value.bytes)  # type: ignore
    elif isinstance(value, bytes):
        return BinaryLiteral(value)
    elif isinstance(value, Decimal):
        return DecimalLiteral(value)
    else:
        raise TypeError(f"Invalid literal value: {repr(value)}")