def replace_subqueries(ex,tables,table_ctor):
  "this mutates passed in ex (any BaseX), replacing nested selects with their (flattened) output"
  # http://www.postgresql.org/docs/9.1/static/sql-expressions.html#SQL-SYNTAX-SCALAR-SUBQUERIES
  # see here for subquery conditions that *do* use multi-rows. ug. http://www.postgresql.org/docs/9.1/static/functions-subquery.html
  if isinstance(ex,sqparse2.SelectX):
    old_tables, ex.tables = ex.tables, [] # we *don't* recurse into tables because selects in here get transformed into tables
  for path in treepath.sub_slots(ex, lambda x:isinstance(x,sqparse2.SelectX)):
    ex[path] = sqparse2.Literal(flatten_scalar(run_select(ex[path], tables, table_ctor)))
  if isinstance(ex,sqparse2.SelectX): ex.tables = old_tables
  return ex
def replace_subqueries ( ex , tables , table_ctor ) : if isinstance ( ex , sqparse2 . SelectX ) : old_tables , ex . tables = ex . tables , [ ] for path in treepath . sub_slots ( ex , lambda x : isinstance ( x , sqparse2 . SelectX ) ) : ex [ path ] = sqparse2 . Literal ( flatten_scalar ( run_select ( ex [ path ] , tables , table_ctor ) ) ) if isinstance ( ex , sqparse2 . SelectX ) : ex . tables = old_tables return ex
this mutates passed in ex (any BaseX), replacing nested selects with their (flattened) output