def _handle_join_subqueries(self, join: JoinBase) -> QueryInfo: info = QueryInfo( InternalTransformer( join.get_tables(), join.get_table_map(), self._column_name_map, self._column_to_table_name, self._table_name_map, self._alias_registry, ), ) info.add_table(join) info.add_column(Column(name="*")) return info
def handle_filtering( self, ibis_table: TableExpr, where_expr: Tree, internal_transformer: InternalTransformer, ): """ Returns frame with appropriately selected and named columns :param ibis_table: Ibis expression table to manipulate :param where_expr: Syntax tree containing where clause :param internal_transformer: Transformer to transform the where clauses :return: Filtered TableExpr """ if where_expr is not None: where_expression: WhereExpression = internal_transformer.transform( where_expr) return ibis_table.filter(where_expression.value.get_value()) return ibis_table
def _handle_having_expressions( self, having_expr: Tree, internal_transformer: InternalTransformer, table: TableExpr, aggregates: Dict[str, Aggregate], group_column_names: List[str], ): having = None if having_expr: having = internal_transformer.transform( having_expr.children[0]).value if having is not None and not aggregates: for column in table.columns: if column not in group_column_names: raise InvalidQueryException( self.format_column_needs_agg_or_group_msg(column)) return having
def handle_join( self, join: JoinBase, columns: List[Value], internal_transformer: InternalTransformer, ) -> TableExpr: """ Return the table expr resulting from the join :param join: :param columns: List of all column values :return: """ result: TableExpr = None all_columns: List[Value] = [] left_table = join.left_table right_table = join.right_table if self._columns_have_select_star(columns): all_columns = self._get_all_join_columns_handle_duplicates( left_table, right_table, join) left_ibis_table = left_table.get_table_expr() right_ibis_table = right_table.get_table_expr() if isinstance(join, Join): compiled_condition: Value = internal_transformer.transform( join.join_condition) result = left_ibis_table.join( right_ibis_table, predicates=compiled_condition.get_value(), how=join.join_type, ) if isinstance(join, CrossJoin): result = ibis.cross_join(left_ibis_table, right_ibis_table) if all_columns: return result[all_columns] return result
def select(self, *select_expressions: Tuple[Tree]) -> QueryInfo: """ Forms the final sequence of methods that will be executed :param select_expressions: :return: """ tables: List[Token] = [] having_expr = None where_expr = None for select_expression in select_expressions: if isinstance(select_expression, Tree): if select_expression.data == "from_expression": table_object = select_expression.children[0] if isinstance(table_object, JoinBase): tables += [ table_object.right_table, table_object.left_table, ] elif (isinstance(table_object, Tree) and table_object.data == "cross_join_expression"): cross_join: CrossJoin = table_object.children[0] tables += [ cross_join.right_table, cross_join.left_table, ] else: tables.append(table_object) elif select_expression.data == "having_expr": having_expr = select_expression elif select_expression.data == "where_expr": where_expr = select_expression internal_transformer = InternalTransformer( tables, # type: ignore self._table_map, self._column_name_map, self._column_to_table_name, self._table_name_map, self._alias_registry, ) select_expressions_no_boolean_clauses: List[Union[str, Tree]] = [] distinct = False for select_expression in select_expressions: if isinstance(select_expression, Tree) and select_expression.data not in ( "having_expr", "where_expr", ): select_expressions_no_boolean_clauses.append(select_expression) if (isinstance(select_expression, Token) and select_expression.value.lower() == "distinct"): distinct = True return QueryInfo( internal_transformer, select_expressions_no_boolean_clauses, having_expr=having_expr, where_expr=where_expr, distinct=distinct, )