def _select(self, tree, allow_select_scan):
tablename = tree.table<EOL>desc = self.describe(tablename, require=True)<EOL>kwargs = {}<EOL>if tree.consistent:<EOL><INDENT>kwargs["<STR_LIT>"] = True<EOL><DEDENT>visitor = Visitor(self.reserved_words)<EOL>selection = SelectionExpression.from_selection(tree.attrs)<EOL>if selection.is_count:<EOL><INDENT>kwargs["<STR_LIT>"] = "<STR_LIT>"<EOL><DEDENT>if tree.keys_in:<EOL><INDENT>if tree.limit:<EOL><INDENT>raise SyntaxError("<STR_LIT>")<EOL><DEDENT>elif tree.using:<EOL><INDENT>raise SyntaxError("<STR_LIT>")<EOL><DEDENT>elif tree.order:<EOL><INDENT>raise SyntaxError("<STR_LIT>")<EOL><DEDENT>elif tree.where:<EOL><INDENT>raise SyntaxError("<STR_LIT>")<EOL><DEDENT>keys = list(self._iter_where_in(tree))<EOL>kwargs["<STR_LIT>"] = selection.build(visitor)<EOL>kwargs["<STR_LIT>"] = visitor.attribute_names<EOL>return self.connection.batch_get(tablename, keys=keys, **kwargs)<EOL><DEDENT>if tree.limit:<EOL><INDENT>if tree.scan_limit:<EOL><INDENT>kwargs["<STR_LIT>"] = Limit(<EOL>scan_limit=resolve(tree.scan_limit[<NUM_LIT:2>]),<EOL>item_limit=resolve(tree.limit[<NUM_LIT:1>]),<EOL>strict=True,<EOL>)<EOL><DEDENT>else:<EOL><INDENT>kwargs["<STR_LIT>"] = Limit(item_limit=resolve(tree.limit[<NUM_LIT:1>]), strict=True)<EOL><DEDENT><DEDENT>elif tree.scan_limit:<EOL><INDENT>kwargs["<STR_LIT>"] = Limit(scan_limit=resolve(tree.scan_limit[<NUM_LIT:2>]))<EOL><DEDENT>(action, query_kwargs, index) = self._build_query(desc, tree, visitor)<EOL>if action == "<STR_LIT>" and not allow_select_scan:<EOL><INDENT>raise SyntaxError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>order_by = None<EOL>if tree.order_by:<EOL><INDENT>order_by = tree.order_by[<NUM_LIT:0>]<EOL><DEDENT>reverse = tree.order == "<STR_LIT>"<EOL>if tree.order:<EOL><INDENT>if action == "<STR_LIT>" and not tree.order_by:<EOL><INDENT>raise SyntaxError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>if action == "<STR_LIT>":<EOL><INDENT>if order_by is None or order_by == index.range_key:<EOL><INDENT>kwargs["<STR_LIT>"] = reverse<EOL><DEDENT><DEDENT><DEDENT>kwargs.update(query_kwargs)<EOL>fetch_attrs_after = False<EOL>if index is not None and not index.projects_all_attributes(<EOL>selection.all_fields<EOL>):<EOL><INDENT>kwargs["<STR_LIT>"] = [<EOL>visitor.get_field(a) for a in desc.primary_key_attributes<EOL>]<EOL>fetch_attrs_after = True<EOL><DEDENT>else:<EOL><INDENT>kwargs["<STR_LIT>"] = selection.build(visitor)<EOL><DEDENT>kwargs["<STR_LIT>"] = visitor.expression_values<EOL>kwargs["<STR_LIT>"] = visitor.attribute_names<EOL>method = getattr(self.connection, action + "<STR_LIT:2>")<EOL>result = method(tablename, **kwargs)<EOL>if fetch_attrs_after:<EOL><INDENT>if not isinstance(result, list):<EOL><INDENT>result = list(result)<EOL><DEDENT>if not result:<EOL><INDENT>return result<EOL><DEDENT>visitor = Visitor(self.reserved_words)<EOL>kwargs = {"<STR_LIT>": [desc.primary_key(item) for item in result]}<EOL>kwargs["<STR_LIT>"] = selection.build(visitor)<EOL>kwargs["<STR_LIT>"] = visitor.attribute_names<EOL>result = self.connection.batch_get(tablename, **kwargs)<EOL><DEDENT>def order(items):<EOL><INDENT>"""<STR_LIT>"""<EOL>if order_by is None:<EOL><INDENT>return items<EOL><DEDENT>if index is None or order_by != index.range_key:<EOL><INDENT>if not isinstance(items, list):<EOL><INDENT>items = list(items)<EOL><DEDENT>items.sort(key=lambda x: x.get(order_by), reverse=reverse)<EOL><DEDENT>return items<EOL><DEDENT>if tree.save_file:<EOL><INDENT>if selection.is_count:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>count = <NUM_LIT:0><EOL>result = order(selection.convert(item, True) for item in result)<EOL>filename = tree.save_file[<NUM_LIT:0>]<EOL>if filename[<NUM_LIT:0>] in ['<STR_LIT:">', "<STR_LIT:'>"]:<EOL><INDENT>filename = unwrap(filename)<EOL><DEDENT>if not isinstance(result, list):<EOL><INDENT>result = list(result)<EOL><DEDENT>remainder, ext = os.path.splitext(filename)<EOL>if ext.lower() in ["<STR_LIT>", "<STR_LIT>"]:<EOL><INDENT>ext = os.path.splitext(remainder)[<NUM_LIT:1>]<EOL>opened = gzip.open(filename, "<STR_LIT:wb>")<EOL><DEDENT>else:<EOL><INDENT>opened = open(filename, "<STR_LIT:wb>")<EOL><DEDENT>if ext.lower() == "<STR_LIT>":<EOL><INDENT>if selection.all_keys:<EOL><INDENT>headers = selection.all_keys<EOL><DEDENT>else:<EOL><INDENT>result = list(result)<EOL>all_headers = set()<EOL>for item in result:<EOL><INDENT>all_headers.update(item.keys())<EOL><DEDENT>headers = list(all_headers)<EOL><DEDENT>with opened as ofile:<EOL><INDENT>writer = csv.DictWriter(<EOL>ofile, fieldnames=headers, extrasaction="<STR_LIT:ignore>"<EOL>)<EOL>writer.writeheader()<EOL>for item in result:<EOL><INDENT>count += <NUM_LIT:1><EOL>writer.writerow(item)<EOL><DEDENT><DEDENT><DEDENT>elif ext.lower() == "<STR_LIT>":<EOL><INDENT>with opened as ofile:<EOL><INDENT>for item in result:<EOL><INDENT>count += <NUM_LIT:1><EOL>ofile.write(self._encoder.encode(item))<EOL>ofile.write("<STR_LIT:\n>")<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>with opened as ofile:<EOL><INDENT>for item in result:<EOL><INDENT>count += <NUM_LIT:1><EOL>pickle.dump(item, ofile)<EOL><DEDENT><DEDENT><DEDENT>return count<EOL><DEDENT>elif not selection.is_count:<EOL><INDENT>result = order(selection.convert(item) for item in result)<EOL><DEDENT>return result<EOL>
Run a SELECT statement