def order_query(statement, order, natural_order=None, labels=None):
 order = (order or [])
 labels = (labels or {})
 natural_order = (natural_order or [])
 final_order = OrderedDict()
 columns = OrderedDict(zip(labels, statement.columns))
 if (SPLIT_DIMENSION_NAME in statement.columns):
  split_column = sql.expression.column(SPLIT_DIMENSION_NAME)
  final_order[SPLIT_DIMENSION_NAME] = split_column
 for (attribute, direction) in order:
  attribute = str(attribute)
  column = order_column(columns[attribute], direction)
  if (attribute not in final_order):
   final_order[attribute] = column
 for (name, column) in columns.items():
  if ((name in natural_order) and (name not in order_by)):
   final_order[name] = order_column(column, natural_order[name])
 statement = statement.order_by(*final_order.values())
 return statement

def order_query ( statement , order , natural_order = None , labels = None ) : order = ( order or [ ] ) labels = ( labels or { } ) natural_order = ( natural_order or [ ] ) final_order = OrderedDict ( ) columns = OrderedDict ( zip ( labels , statement . columns ) ) if ( SPLIT_DIMENSION_NAME in statement . columns ) : split_column = sql . expression . column ( SPLIT_DIMENSION_NAME ) final_order [ SPLIT_DIMENSION_NAME ] = split_column for ( attribute , direction ) in order : attribute = str ( attribute ) column = order_column ( columns [ attribute ] , direction ) if ( attribute not in final_order ) : final_order [ attribute ] = column for ( name , column ) in columns . items ( ) : if ( ( name in natural_order ) and ( name not in order_by ) ) : final_order [ name ] = order_column ( column , natural_order [ name ] ) statement = statement . order_by ( * final_order . values ( ) ) return statement
returns a sql statement which is ordered according to the order .