Exemple #1
0
 def sorted_group_by(
     self,
     *keys,
     values: Optional[Iterable] = None,
     as_pairs: bool = False,
 ) -> StreamInterface:
     if as_pairs:
         return super().sorted_group_by(*keys, values=values, as_pairs=True)
     else:
         output_struct = FlatStruct([])
         for f in list(keys) + list(values):
             if isinstance(f, ARRAY_TYPES):
                 field_name = get_name(f[0])
             else:
                 field_name = get_name(f)
             if f in values:
                 field_type = FieldType.Tuple
             elif isinstance(f, FieldInterface) or hasattr(f, 'get_type'):
                 field_type = f.get_type()
             else:
                 field_type = AUTO
             output_struct.append_field(field_name, field_type)
         return super().sorted_group_by(*keys,
                                        values=values,
                                        as_pairs=False,
                                        output_struct=output_struct)
Exemple #2
0
 def __init__(
     self,
     data: Row,
     struct: Union[Row, StructInterface],
     check: bool = True,
 ):
     if not isinstance(struct, StructInterface):
         struct = FlatStruct(struct)
     self._struct = struct
     if check:
         data = self._structure_row(data, struct)
     super().__init__(data=data, name='-')
Exemple #3
0
 def get_output_struct(self) -> StructInterface:
     input_struct = self.get_input_struct()
     output_columns = self.get_output_columns()
     types = {
         f: t
         for f, t in input_struct.get_types_dict().items()
         if f in output_columns
     }
     struct = FlatStruct(output_columns).set_types(types)
     assert isinstance(struct, FlatStruct)
     struct.validate_about(input_struct, ignore_moved=True)
     return struct
Exemple #4
0
 def get_struct_from_database(
     self,
     types: AutoLinks = AUTO,
     set_struct: bool = False,
     skip_missing: bool = False,
     verbose: AutoBool = AUTO,
 ) -> StructInterface:
     struct = FlatStruct(self.describe_table(verbose=verbose))
     if struct.is_empty() and not skip_missing:
         raise ValueError(
             'Can not get struct for non-existing table {}'.format(self))
     if Auto.is_defined(types):
         struct.set_types(types, inplace=True)
     if set_struct:
         self.set_struct(struct, inplace=True)
     return struct
Exemple #5
0
 def set_struct(self, struct: GeneralizedStruct,
                inplace: bool) -> Optional[Native]:
     if isinstance(struct, StructInterface) or struct is None:
         pass
     elif isinstance(struct, ARRAY_TYPES):
         if max([isinstance(f, ARRAY_TYPES) for f in struct]):
             struct = FlatStruct(struct)
         else:
             struct = FlatStruct.get_struct_detected_by_title_row(struct)
     elif struct == AUTO:
         struct = self.get_struct_from_database()
     else:
         message = 'struct must be StructInterface or tuple with fields_description (got {})'.format(
             type(struct))
         raise TypeError(message)
     return super().set_struct(struct, inplace=inplace)