Skip to content

Conversation

@shuiqiangchen
Copy link
Contributor

@shuiqiangchen shuiqiangchen commented Jul 30, 2020

What is the purpose of the change

Supports basic TypeInformation including BasicTypeInfo, LocalTimeTypeInfo, PrimitiveArrayTypeInfo, RowTypeInfo.

Types.ROW()/Types.ROW_NAMED()/Types.PRIMITIVE_ARRAY() should also be supported.

Brief change log

  • Add python wrapper class for BasicTypeInformation, PrimitiveArrayTypeInfos and RowTypeInfo, etc.
  • Introduce a new type info named PickledArrayTypeInfo to indicate the type of bytes array data derived from pickled python objects.
  • Add utils to convert python typeInfo and java typeInfo.

Verifying this change

This change is already covered by existing tests, such as:

  • TypeInfoTests in test_typeinfo.py

Does this pull request potentially affect one of the following parts:

  • Dependencies (does it add or upgrade a dependency): ( no)
  • The public API, i.e., is any changed class annotated with @Public(Evolving): (no no)
  • The serializers: (no)
  • The runtime per-record code paths (performance sensitive): (no)
  • Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn/Mesos, ZooKeeper: (no)
  • The S3 file system connector: (no)

Documentation

  • Does this pull request introduce a new feature? (yes)
  • If yes, how is the feature documented? ( not documented)

@flinkbot
Copy link
Collaborator

Thanks a lot for your contribution to the Apache Flink project. I'm the @flinkbot. I help the community
to review your pull request. We will use this comment to track the progress of the review.

Automated Checks

Last check on commit 2cb0d81 (Thu Jul 30 12:09:44 UTC 2020)

Warnings:

  • No documentation files were touched! Remember to keep the Flink docs up to date!
  • This pull request references an unassigned Jira ticket. According to the code contribution guide, tickets need to be assigned before starting with the implementation work.

Mention the bot in a comment to re-run the automated checks.

Review Progress

  • ❓ 1. The [description] looks good.
  • ❓ 2. There is [consensus] that the contribution should go into to Flink.
  • ❓ 3. Needs [attention] from.
  • ❓ 4. The change fits into the overall [architecture].
  • ❓ 5. Overall code [quality] is good.

Please see the Pull Request Review Guide for a full explanation of the review process.

Details
The Bot is tracking the review progress through labels. Labels are applied according to the order of the review items. For consensus, approval by a Flink committer of PMC member is required Bot commands
The @flinkbot bot supports the following commands:

  • @flinkbot approve description to approve one or more aspects (aspects: description, consensus, architecture and quality)
  • @flinkbot approve all to approve all aspects
  • @flinkbot approve-until architecture to approve everything until architecture
  • @flinkbot attention @username1 [@username2 ..] to require somebody's attention
  • @flinkbot disapprove architecture to remove an approval you gave earlier

@flinkbot
Copy link
Collaborator

flinkbot commented Jul 30, 2020

CI report:

Bot commands The @flinkbot bot supports the following commands:
  • @flinkbot run travis re-run the last Travis build
  • @flinkbot run azure re-run the last Azure build

Copy link
Contributor

@hequn8128 hequn8128 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shuiqiangchen Thanks a lot for your PR. Left some comments below.

TypeInformation is the core class of Flink's type system. FLink requires a type information
for all types that are used as input or return type of a user function. This type information
class acts as the tool to generate serializers and comparators, and to perform semantic checks
such as whether the fields that are used as join/grouping keys actually existt.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

existt => exist

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I will revise it.

in the arrays.
a) Basic types are indivisible and are considered as a single field.
b) Arrays and collections are one field.
c) Tuples and case classes represent as many fields as the class has fields.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have case classes in PyFlink.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will remove it.

"""

@abstractmethod
def is_basic_type(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add type hint: def is_basic_type(self) -> bool:. Same for other methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, it's good to add type hints.

def is_tuple_type(self):
"""
Checks if this type information represents a Tuple type.
Tuple types are subclasses of the Java API tuples.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove this line. Tuple type is supported in Python originally. We should not simply copy Java comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, there should be differences between java and python comments.

nested fields, in the case of composite types.
The total number of fields must be at lest 1.

:return: The number of fields in this type, including its sub-fields (for composit types).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

composite types

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I'll revise it.

.PickledByteArrayTypeInfo())


class BigDecimalTypeInfo(WrapperTypeInfo):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decimal should exist in BasicTypeInfo together with BIG_INT_TYPE_INFO. The decimal type info in DataStream is different from table. The precision and scale are not fixed, i.e., not be specified during initialization.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'll remove it.

* A PickledByteArrayTypeInfo indicates that the data of this type is a generated primitive byte
* array by pickle.
*/
public class PickledByteArrayTypeInfo extends TypeInformation<byte[]> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make it a Singleton, similar to BasicTypeInfo in which provides a static final instance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, making it singleton is better.

@shuiqiangchen
Copy link
Contributor Author

Hi @hequn8128 , thank you for your comments, I have updated the pr according to your review suggestions, please have a look at it.

Copy link
Contributor

@hequn8128 hequn8128 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More comments below

field_names = [name for name in j_field_names]
return field_names

def get_field_index(self, field_name) -> int:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add type hint.

raise TypeError("Invalid element type for a primitive array.")


def from_java_type(j_type_info: JavaObject) -> TypeInformation:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Private method. Rename to _from_java_type.

raise TypeError("The java type info: %s is not supported in PyFlink currently." % j_type_info)


def is_instance_of(java_object: JavaObject, java_type: Union[JavaObject, JavaClass]) -> bool:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Private method. Rename to _is_instance_of.

PICKLED_BYTE_ARRAY = PickledBytesTypeInfo.PICKLED_BYTE_ARRAY_TYPE_INFO

@staticmethod
def ROW(types):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add type hint

return RowTypeInfo(types)

@staticmethod
def ROW_NAMED(names, types):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add type hint

from pyflink.common.typeinfo import Types, RowTypeInfo


class TypeInfoTests(unittest.TestCase):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add tests to cover TupleTypeInfo and methods in Types(Types.ROW(), Types.ROW_NAMED, etc).

Copy link
Contributor

@hequn8128 hequn8128 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shuiqiangchen Thanks a lot for the update. The code looks good overall. Some minor comments below.

TupleTypeInfo([Types.STRING(), Types.INT()]), True)

self.assertEqual(TupleTypeInfo([Types.STRING(), Types.INT()]).__str__(),
"Java Tuple2<String, Integer>")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is strange to return a string with Java for Python typeinfo. We need to regenerate the string for TupleTypeinfo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, maybe we should not directly call to toString() of the java type info object.

Comment on lines 116 to 118



Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the useless empty lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I'll clean it.

['a', 'b']), False)
self.assertEqual(RowTypeInfo([Types.STRING(),
Types.STRING()],
['a', 'b']).__str__(), "RowTypeInfo[String, String]")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also print field name for row type info.

Copy link
Contributor

@hequn8128 hequn8128 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good from my side. Will merge this if @twalthr does not have any other concerns.

Best,
Hequn

Copy link
Contributor

@hequn8128 hequn8128 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shuiqiangchen My last comments. Maybe we can remove the methods in the python TypeInformation since we don't need them now. It is always easier to add APIs than delete them. We can add these methods later when we find it is useful.

Comment on lines 50 to 86
def is_basic_type(self) -> bool:
"""
Checks if this type information represents a basic type.
Basic types are defined in BasicTypeInfo and are primitives, their boxing type, Strings ...

:return: True, if this type information describes a basic type, false otherwise.
"""
pass

@abstractmethod
def is_tuple_type(self) -> bool:
"""
Checks if this type information represents a Tuple type.

:return: True, if this type information describes a tuple type, false otherwise.
"""
pass

@abstractmethod
def get_arity(self) -> int:
"""
Gets the arity of this type - the number of fields without nesting.

:return: the number of fields in this type without nesting.
"""
pass

@abstractmethod
def get_total_fields(self) -> int:
"""
Gets the number of logical fields in this type. This includes its nested and transitively
nested fields, in the case of composite types.
The total number of fields must be at lest 1.

:return: The number of fields in this type, including its sub-fields (for composite types).
"""
pass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove these methods since we don't need them now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, I will remove them.

Comment on lines 97 to 106
def is_basic_type(self) -> bool:
return self._j_typeinfo.isBasicType()

def is_tuple_type(self) -> bool:
return self._j_typeinfo.isTupleType()

def get_arity(self) -> int:
return self._j_typeinfo.getArity()

def get_total_fields(self) -> int:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

@shuiqiangchen
Copy link
Contributor Author

@hequn8128 Thank you for your comments, I have updated the code, please take a look at it.

@hequn8128
Copy link
Contributor

hequn8128 commented Aug 4, 2020

@shuiqiangchen Thanks a lot for the update. Please check the azure test failures.

@hequn8128 hequn8128 merged commit ea9f449 into apache:master Aug 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants