twalthr commented on code in PR #26121: URL: https://github.com/apache/flink/pull/26121#discussion_r1973389883
########## flink-python/pyflink/table/resolved_expression.py: ########## @@ -0,0 +1,51 @@ +################################################################################ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +################################################################################ +from typing import List + +from pyflink.table.types import DataType, _from_java_data_type + +__all__ = ["ResolvedExpression"] + + +class ResolvedExpression(object): Review Comment: This class should extend from `Expression` ########## flink-python/pyflink/table/catalog.py: ########## @@ -1391,3 +1398,419 @@ def of(catalog_name: str, configuration: Configuration, comment: str = None): j_catalog_descriptor = gateway.jvm.org.apache.flink.table.catalog.CatalogDescriptor.of( catalog_name, configuration._j_configuration, comment) return CatalogDescriptor(j_catalog_descriptor) + + +class Column(metaclass=ABCMeta): + """ + Representation of a column in a :class:`~pyflink.table.ResolvedSchema`. + + A table column describes either a :class:`PhysicalColumn`, :class:`ComputedColumn`, or + :class:`MetadataColumn`. + """ + + def __init__(self, j_column): + self._j_column = j_column + + def __eq__(self, other): + return self.__class__ == other.__class__ and self._j_column.equals(other._j_column) + + def __hash__(self): + return self._j_column.hashCode() + + def __str__(self): + return self._j_column.toString() + + @staticmethod + def _from_j_column(j_column) -> Optional["Column"]: + """ + Returns a non-abstract column, either a :class:`PhysicalColumn`, a :class:`ComputedColumn`, + or a :class:`MetadataColumn` from an org.apache.flink.table.catalog.Column. + """ + if j_column is None: + return None + gateway = get_gateway() + JColumn = gateway.jvm.org.apache.flink.table.catalog.Column + JPhysicalColumn = gateway.jvm.org.apache.flink.table.catalog.Column.PhysicalColumn + JComputedColumn = gateway.jvm.org.apache.flink.table.catalog.Column.ComputedColumn + JMetadataColumn = gateway.jvm.org.apache.flink.table.catalog.Column.MetadataColumn + j_clz = j_column.getClass() + + if not get_java_class(JColumn).isAssignableFrom(j_clz): + raise TypeError("The input %s is not an instance of Column." % j_column) + + if get_java_class(JPhysicalColumn).isAssignableFrom(j_column.getClass()): + return PhysicalColumn(j_physical_column=j_column.getClass()) + elif get_java_class(JComputedColumn).isAssignableFrom(j_column.getClass()): + return MetaDataColumn(j_metadata_column=j_column.getClass()) + elif get_java_class(JMetadataColumn).isAssignableFrom(j_column.getClass()): + return MetaDataColumn(j_metadata_column=j_column.getClass()) + else: + return None + + @staticmethod + def physical(name: str, data_type: DataType) -> "PhysicalColumn": + """ + Creates a regular table column that represents physical data. + """ + gateway = get_gateway() + j_data_type = _to_java_data_type(data_type) + j_physical_column = gateway.jvm.org.apache.flink.table.catalog.Column.physical( + name, j_data_type + ) + return PhysicalColumn(j_physical_column) + + @staticmethod + def computed(name: str, resolved_expression: ResolvedExpression) -> "ComputedColumn": + """ + Creates a computed column that is computed from the given + :class:`~pyflink.table.ResolvedExpression`. + """ + gateway = get_gateway() + j_resolved_expression = resolved_expression + j_computed_column = gateway.jvm.org.apache.flink.table.catalog.Column.computed( + name, j_resolved_expression + ) + return ComputedColumn(j_computed_column) + + @staticmethod + def metadata( + name: str, data_type: DataType, metadata_key: Optional[str], is_virtual: bool + ) -> "MetaDataColumn": Review Comment: ```suggestion ) -> "MetadataColumn": ``` ########## flink-python/pyflink/table/tests/test_table_environment_api.py: ########## @@ -407,7 +408,7 @@ def test_from_data_stream_atomic(self): self.assertEqual("""( `f0` RAW('[B', '...') )""", - result._j_table_result.getResolvedSchema().toString()) + str(result.get_resolved_schema())) Review Comment: why was this necessary? ########## flink-python/pyflink/table/resolved_expression.py: ########## @@ -0,0 +1,51 @@ +################################################################################ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +################################################################################ +from typing import List + +from pyflink.table.types import DataType, _from_java_data_type + +__all__ = ["ResolvedExpression"] + + +class ResolvedExpression(object): + """ + Expression that has been fully resolved and validated. + + Compared to :class:`~pyflink.table.Expression`, resolved expressions do not contain unresolved + subexpressions anymore and provide an output data type for the computation result. + + Instances of this class describe a fully parameterized, immutable expression that can be + serialized and persisted. + + Resolved expression are the output of the API to the planner and are pushed from the planner + into interfaces, for example, for predicate push-down. + """ + + def __init__(self, j_resolved_expr): + self._j_resolved_expr = j_resolved_expr + + def get_output_data_type(self) -> DataType: + """ + Returns the data type of the computation result. + """ + j_data_type = self._j_resolved_expr.getOutputDataType() + return _from_java_data_type(j_data_type) + + def get_resolved_children(self) -> List["ResolvedExpression"]: Review Comment: Add `asSerializableString` and `asSummaryString` / `toString` ########## flink-python/pyflink/table/table_result.py: ########## @@ -140,9 +141,80 @@ def get_table_schema(self) -> TableSchema: :rtype: pyflink.table.TableSchema .. versionadded:: 1.11.0 + .. deprecated:: 2.1.0 + Use :func:`TableResult.get_resolved_schema` instead. """ return TableSchema(j_table_schema=self._get_java_table_schema()) + def get_resolved_schema(self) -> ResolvedSchema: + """ + Get the resolved schema of result. + + The schema of DDL, USE, EXPLAIN: Review Comment: Where does this part come from? It seems not in the Java code. Drop this? -- 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]
