Ejemplo n.º 1
0
def handle_request(transport, request_message):
    """
        Reads a request from a client and processes it

        If user inputs 'quit' stops the event loop
        otherwise just echoes user input
    """
    logger.debug('Receive request: --|' + str(request_message) + '|--')

    try:
        output_batch = execute_query_fetch_all(request_message)
    except Exception as e:
        logger.warn(e)
        response = Response(status=ResponseStatus.FAIL,
                            batch=None,
                            error=str(e))
    else:
        response = Response(status=ResponseStatus.SUCCESS, batch=output_batch)

    responseData = response.to_json()
    # Send data length, because response can be very large
    data = (str(len(responseData)) + '|' + responseData).encode('ascii')

    logger.debug('Response to client: --|' + str(response) + '|--\n' +
                 'Length: ' + str(len(responseData)))

    transport.write(data)

    return response
Ejemplo n.º 2
0
    def exec(self):
        """Drop table executor"""
        catalog_manager = CatalogManager()
        if len(self.node.table_refs) > 1:
            logger.exception('Drop supports only single table')
        table_ref = self.node.table_refs[0]

        if not catalog_manager.check_table_exists(
                table_ref.table.database_name, table_ref.table.table_name):
            err_msg = "Table: {} does not exsits".format(table_ref)
            if self.node.if_exists:
                logger.warn(err_msg)
            else:
                logger.exception(err_msg)

        if table_ref.table.table_obj.is_video:
            VideoStorageEngine.drop(table=table_ref.table.table_obj)
        else:
            StorageEngine.drop(table=table_ref.table.table_obj)

        success = catalog_manager.drop_dataset_metadata(
            table_ref.table.database_name, table_ref.table.table_name)

        if not success:
            err_msg = "Failed to drop {}".format(table_ref)
            logger.exception(err_msg)

        yield Batch(
            pd.DataFrame(
                {
                    "Table Successfully dropped: {}".format(
                        table_ref.table.table_name)
                },
                index=[0],
            ))
Ejemplo n.º 3
0
    def sort_orderby(self, by, sort_type=None):
        """
        in_place sort for orderby

        Args:
            by: list of column names
            sort_type: list of True/False if ASC for each column name in 'by'
                i.e [True, False] means [ASC, DESC]
        """
        # if by is None and self.identifier_column in self._frames:
        #     by = [self.identifier_column]

        if sort_type is None:
            sort_type = [True]

        if by is not None:
            for column in by:
                if column not in self._frames.columns:
                    logger.error(
                        'Can not orderby non-projected column: {}'.format(
                            column))
                    raise KeyError(
                        'Can not orderby non-projected column: {}'.format(
                            column))

            self._frames.sort_values(by,
                                     ascending=sort_type,
                                     ignore_index=True,
                                     inplace=True)
        else:
            logger.warn('Columns and Sort Type are required for orderby')
Ejemplo n.º 4
0
    def exec(self) -> Iterator[Batch]:
        if self.node.all is False:
            logger.warn('Only UNION ALL is supported now.')

        # We should have only two children
        for child in self.children:
            for batch in child.exec():
                yield batch
Ejemplo n.º 5
0
 def project(self, cols: []) -> 'Batch':
     """
     Takes as input the column list, returns the projection.
     We do a copy for now.
     """
     verfied_cols = [c for c in cols if c in self._frames]
     unknown_cols = list(set(cols) - set(verfied_cols))
     if len(unknown_cols):
         logger.warn("Unexpected columns %s\n\
                              Frames: %s" % (unknown_cols, self._frames))
     return Batch(self._frames[verfied_cols], self._identifier_column)
Ejemplo n.º 6
0
def handle_if_not_exists(table_ref: TableRef, if_not_exist=False):
    if CatalogManager().check_table_exists(table_ref.table.database_name,
                                           table_ref.table.table_name):
        err_msg = 'Table: {} already exsits'.format(table_ref)
        if if_not_exist:
            logger.warn(err_msg)
            return True
        else:
            logger.error(err_msg)
            raise RuntimeError(err_msg)
    else:
        return False
Ejemplo n.º 7
0
 def sort(self, by=None):
     """
     in_place sort
     """
     if by is None:
         if self.identifier_column in self._frames:
             by = [self.identifier_column]
         elif not self.empty():
             by = self.frames.columns[0]
         else:
             logger.warn('Sorting an empty batch')
             return
     self._frames.sort_values(by=by, ignore_index=True, inplace=True)
Ejemplo n.º 8
0
    def merge_column_wise(cls,
                          batches: ['Batch'],
                          auto_renaming=False) -> 'Batch':
        """
        Merge list of batch frames column_wise and return a new batch frame
        Arguments:
            batches: List[Batch]: lsit of batch objects to be merged
            auto_renaming: if true rename column names if required

        Returns:
            Batch: Merged batch object
        """

        if not len(batches):
            return Batch()
        frames = [batch.frames for batch in batches]
        new_frames = pd.concat(frames, axis=1, copy=False)
        if new_frames.columns.duplicated().any():
            logger.warn(
                'Duplicated column name detected {}'.format(new_frames))
        return Batch(new_frames)