dianfu commented on code in PR #28843:
URL: https://github.com/apache/flink/pull/28843#discussion_r3688499726
##########
flink-python/pyflink/dataframe/datatype.py:
##########
@@ -49,7 +76,55 @@ def __eq__(self, other: object) -> bool:
@PublicEvolving()
def __hash__(self) -> int:
- return hash(str(self._table_data_type))
+ return hash(repr(self._table_data_type))
+
+ @PublicEvolving()
+ def not_null(self) -> "DataType":
+ """
+ Return a non-nullable version of this data type.
+
+ .. versionadded:: 2.4.0
+ """
+ return DataType(self._table_data_type.not_null())
+
+ @PublicEvolving()
+ def nullable(self) -> "DataType":
+ """
+ Return a nullable version of this data type.
+
+ .. versionadded:: 2.4.0
+ """
+ return DataType(self._table_data_type.nullable())
+
+ @classmethod
+ @PublicEvolving()
+ def int8(cls) -> "DataType":
Review Comment:
`pf.lit(1, DataType.int8())`, `int16()`, and `float32()` raise Java
`ValidationException`s because Py4J sends Integer/Double rather than
Byte/Short/Float. Date/time/timestamp, list, and struct literals fail
similarly. Extend the literal conversion path for these types and add
end-to-end factory/literal tests.
##########
flink-python/pyflink/dataframe/datatype.py:
##########
@@ -81,5 +189,260 @@ def string(cls) -> "DataType":
"""
return cls(DataTypes.STRING())
+ @classmethod
+ @PublicEvolving()
+ def fixed_size_string(cls, length: int) -> "DataType":
+ """
+ Create a fixed-length character string type.
+
+ :param length: Number of characters.
+
+ .. versionadded:: 2.4.0
+ """
+ return cls(DataTypes.CHAR(length))
+
+ @classmethod
+ @PublicEvolving()
+ def binary(cls) -> "DataType":
+ """
+ Create a variable-length binary string type.
+
+ .. versionadded:: 2.4.0
+ """
+ return cls(DataTypes.BYTES())
+
+ @classmethod
+ @PublicEvolving()
+ def fixed_size_binary(cls, length: int) -> "DataType":
+ """
+ Create a fixed-length binary string type.
+
+ :param length: Number of bytes.
+
+ .. versionadded:: 2.4.0
+ """
+ return cls(DataTypes.BINARY(length))
+
+ @classmethod
+ @PublicEvolving()
+ def bool(cls) -> "DataType":
+ """
+ Create a boolean type.
+
+ .. versionadded:: 2.4.0
+ """
+ return cls(DataTypes.BOOLEAN())
+
+ @classmethod
+ @PublicEvolving()
+ def null(cls) -> "DataType":
+ """
+ Create a null type.
+
+ .. versionadded:: 2.4.0
+ """
+ return cls(DataTypes.NULL())
+
+ @classmethod
+ @PublicEvolving()
+ def date(cls) -> "DataType":
+ """
+ Create a date type.
+
+ .. versionadded:: 2.4.0
+ """
+ return cls(DataTypes.DATE())
+
+ @classmethod
+ @PublicEvolving()
+ def time(cls, precision: int = 0) -> "DataType":
+ """
+ Create a time type.
+
+ :param precision: Number of fractional-second digits.
+
+ .. versionadded:: 2.4.0
+ """
+ return cls(DataTypes.TIME(precision))
+
+ @classmethod
+ @PublicEvolving()
+ def timestamp(cls, precision: int = 6) -> "DataType":
+ """
+ Create a timestamp type without a time zone.
+
+ :param precision: Number of fractional-second digits.
+
+ .. versionadded:: 2.4.0
+ """
+ return cls(DataTypes.TIMESTAMP(precision))
+
+ @classmethod
+ @PublicEvolving()
+ def timestamp_ltz(cls, precision: int = 6) -> "DataType":
+ """
+ Create a timestamp type with a local time zone.
+
+ :param precision: Number of fractional-second digits.
+
+ .. versionadded:: 2.4.0
+ """
+ return cls(DataTypes.TIMESTAMP_LTZ(precision))
+
+ @classmethod
+ @PublicEvolving()
+ def list(cls, dtype: "DataType") -> "DataType":
+ """
+ Create a list type.
+
+ :param dtype: Type of each list element.
+
+ .. versionadded:: 2.4.0
+ """
+ return cls(DataTypes.ARRAY(dtype._to_table_data_type()))
+
+ @classmethod
+ @PublicEvolving()
+ def map(cls, key_type: "DataType", value_type: "DataType") -> "DataType":
+ """
+ Create a map type.
+
+ :param key_type: Type of each map key.
+ :param value_type: Type of each map value.
Review Comment:
ditto, it would be great to add an example.
##########
flink-python/pyflink/dataframe/datatype.py:
##########
@@ -81,5 +189,260 @@ def string(cls) -> "DataType":
"""
return cls(DataTypes.STRING())
+ @classmethod
+ @PublicEvolving()
+ def fixed_size_string(cls, length: int) -> "DataType":
+ """
+ Create a fixed-length character string type.
+
+ :param length: Number of characters.
+
+ .. versionadded:: 2.4.0
+ """
+ return cls(DataTypes.CHAR(length))
+
+ @classmethod
+ @PublicEvolving()
+ def binary(cls) -> "DataType":
+ """
+ Create a variable-length binary string type.
+
+ .. versionadded:: 2.4.0
+ """
+ return cls(DataTypes.BYTES())
+
+ @classmethod
+ @PublicEvolving()
+ def fixed_size_binary(cls, length: int) -> "DataType":
+ """
+ Create a fixed-length binary string type.
+
+ :param length: Number of bytes.
+
+ .. versionadded:: 2.4.0
+ """
+ return cls(DataTypes.BINARY(length))
+
+ @classmethod
+ @PublicEvolving()
+ def bool(cls) -> "DataType":
+ """
+ Create a boolean type.
+
+ .. versionadded:: 2.4.0
+ """
+ return cls(DataTypes.BOOLEAN())
+
+ @classmethod
+ @PublicEvolving()
+ def null(cls) -> "DataType":
+ """
+ Create a null type.
+
+ .. versionadded:: 2.4.0
+ """
+ return cls(DataTypes.NULL())
+
+ @classmethod
+ @PublicEvolving()
+ def date(cls) -> "DataType":
+ """
+ Create a date type.
+
+ .. versionadded:: 2.4.0
+ """
+ return cls(DataTypes.DATE())
+
+ @classmethod
+ @PublicEvolving()
+ def time(cls, precision: int = 0) -> "DataType":
+ """
+ Create a time type.
+
+ :param precision: Number of fractional-second digits.
+
+ .. versionadded:: 2.4.0
+ """
+ return cls(DataTypes.TIME(precision))
+
+ @classmethod
+ @PublicEvolving()
+ def timestamp(cls, precision: int = 6) -> "DataType":
+ """
+ Create a timestamp type without a time zone.
+
+ :param precision: Number of fractional-second digits.
+
+ .. versionadded:: 2.4.0
+ """
+ return cls(DataTypes.TIMESTAMP(precision))
+
+ @classmethod
+ @PublicEvolving()
+ def timestamp_ltz(cls, precision: int = 6) -> "DataType":
+ """
+ Create a timestamp type with a local time zone.
+
+ :param precision: Number of fractional-second digits.
+
+ .. versionadded:: 2.4.0
+ """
+ return cls(DataTypes.TIMESTAMP_LTZ(precision))
+
+ @classmethod
+ @PublicEvolving()
+ def list(cls, dtype: "DataType") -> "DataType":
+ """
+ Create a list type.
+
+ :param dtype: Type of each list element.
+
+ .. versionadded:: 2.4.0
+ """
+ return cls(DataTypes.ARRAY(dtype._to_table_data_type()))
+
+ @classmethod
+ @PublicEvolving()
+ def map(cls, key_type: "DataType", value_type: "DataType") -> "DataType":
+ """
+ Create a map type.
+
+ :param key_type: Type of each map key.
+ :param value_type: Type of each map value.
+
+ .. versionadded:: 2.4.0
+ """
+ return cls(
+ DataTypes.MAP(
+ key_type._to_table_data_type(),
+ value_type._to_table_data_type(),
+ )
+ )
+
+ @classmethod
+ @PublicEvolving()
+ def struct(
+ cls,
+ fields: Union[
+ Dict[str, "DataType"],
+ List[Tuple[str, "DataType"]],
+ ],
+ ) -> "DataType":
+ """
+ Create a struct type with named fields.
+
+ ``fields`` may be an insertion-ordered dictionary or a list of name
and type pairs.
+
+ :param fields: Field names and their data types.
+
+ Example::
+
+ >>> import pyflink.dataframe as pf
+ >>> person_type = pf.DataType.struct({
+ ... "name": pf.DataType.string(),
+ ... "age": pf.DataType.int32(),
Review Comment:
Also add an example when the type of fields is an list.
##########
flink-python/docs/reference/pyflink.dataframe/datatype.rst:
##########
@@ -34,5 +34,25 @@ Example::
:toctree: api/
DataType
+ DataType.not_null
+ DataType.nullable
Review Comment:
What about moving these two APIs to the end of this list?
##########
flink-python/pyflink/dataframe/datatype.py:
##########
@@ -81,5 +189,260 @@ def string(cls) -> "DataType":
"""
return cls(DataTypes.STRING())
+ @classmethod
+ @PublicEvolving()
+ def fixed_size_string(cls, length: int) -> "DataType":
+ """
+ Create a fixed-length character string type.
+
+ :param length: Number of characters.
+
+ .. versionadded:: 2.4.0
+ """
+ return cls(DataTypes.CHAR(length))
+
+ @classmethod
+ @PublicEvolving()
+ def binary(cls) -> "DataType":
+ """
+ Create a variable-length binary string type.
+
+ .. versionadded:: 2.4.0
+ """
+ return cls(DataTypes.BYTES())
+
+ @classmethod
+ @PublicEvolving()
+ def fixed_size_binary(cls, length: int) -> "DataType":
+ """
+ Create a fixed-length binary string type.
+
+ :param length: Number of bytes.
+
+ .. versionadded:: 2.4.0
+ """
+ return cls(DataTypes.BINARY(length))
+
+ @classmethod
+ @PublicEvolving()
+ def bool(cls) -> "DataType":
+ """
+ Create a boolean type.
+
+ .. versionadded:: 2.4.0
+ """
+ return cls(DataTypes.BOOLEAN())
+
+ @classmethod
+ @PublicEvolving()
+ def null(cls) -> "DataType":
+ """
+ Create a null type.
+
+ .. versionadded:: 2.4.0
+ """
+ return cls(DataTypes.NULL())
+
+ @classmethod
+ @PublicEvolving()
+ def date(cls) -> "DataType":
+ """
+ Create a date type.
+
+ .. versionadded:: 2.4.0
+ """
+ return cls(DataTypes.DATE())
+
+ @classmethod
+ @PublicEvolving()
+ def time(cls, precision: int = 0) -> "DataType":
+ """
+ Create a time type.
+
+ :param precision: Number of fractional-second digits.
+
+ .. versionadded:: 2.4.0
+ """
+ return cls(DataTypes.TIME(precision))
+
+ @classmethod
+ @PublicEvolving()
+ def timestamp(cls, precision: int = 6) -> "DataType":
+ """
+ Create a timestamp type without a time zone.
+
+ :param precision: Number of fractional-second digits.
+
+ .. versionadded:: 2.4.0
+ """
+ return cls(DataTypes.TIMESTAMP(precision))
+
+ @classmethod
+ @PublicEvolving()
+ def timestamp_ltz(cls, precision: int = 6) -> "DataType":
+ """
+ Create a timestamp type with a local time zone.
+
+ :param precision: Number of fractional-second digits.
+
+ .. versionadded:: 2.4.0
+ """
+ return cls(DataTypes.TIMESTAMP_LTZ(precision))
+
+ @classmethod
+ @PublicEvolving()
+ def list(cls, dtype: "DataType") -> "DataType":
+ """
+ Create a list type.
+
+ :param dtype: Type of each list element.
Review Comment:
It would be great to add an example.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]