code
stringlengths
17
6.64M
def _get_string_replacement(tok: Token) -> List[Token]: result = [] if ((tok.ttype == tokens.Token.Literal.String.Symbol) or (tok.ttype == tokens.Token.Literal.String.Single)): v = tok.value result.append((v[0] + v[(- 1)])) (start, end) = (1, (len(v) - 1)) for span_start in ran...
def get_possible_replacement(tok: Token, column_names: Set[str]) -> List[Token]: possible_replacement = [] for family in [AGG_OP, CMP_OP, ORDER, NUM_OP, LOGICAL_OP, LIKE_OP, IN_OP, column_names]: possible_replacement += _other_toks_same_family(tok, family) for get_family_function in [_get_int_repl...
def span_droppable(span: List[Token]) -> bool: span = [tok for tok in span if (tok.ttype != Whitespace)] if (len(span) == 0): return False if (len(span) == 1): if ((span[0].ttype == Keyword.Order) and (span[0].value.lower() == 'asc')): return False if (span[0].ttype == ...
def drop_any_span(toks: List[Token]) -> Set[str]: num_toks = len(toks) all_s = set() for span_start in range(num_toks): for span_end in range((span_start + 1), (num_toks + 1)): removed_tok = toks[span_start:span_end] if span_droppable(removed_tok): toks_left...
def rm_count_in_column(s): return re.sub('COUNT\\s?\\((.*?)\\)', 'COUNT()', s, flags=re.IGNORECASE)
def equivalent_count(s1, s2): return (rm_count_in_column(s1.strip()) == rm_count_in_column(s2.strip()))
def generate_neighbor_queries_path(sqlite_path: str, query: str) -> List[str]: (table2column2properties, _) = extract_table_column_properties_path(sqlite_path) column_names = set([column_name for table_name in table2column2properties for column_name in table2column2properties[table_name]]) sql_toks = toke...
def isint(x): try: int(x) return True except: return False
def tofloat(x): try: return float(x) except ValueError: return None
def perturb(x): if (random.random() < (1.0 / 6)): return (x + 1) elif (random.random() < (1.0 / 5)): return (x - 1) elif (random.random() < (1.0 / 4)): return (x + 2) elif (random.random() < (1.0 / 3)): return (x - 2) return x
def perturb_float(x): if (random.random() < (1.0 / 6)): return (x + 0.01) elif (random.random() < (1.0 / 5)): return (x - 0.01) elif (random.random() < (1.0 / 4)): return (x + 0.02) elif (random.random() < (1.0 / 3)): return (x - 0.02) return x
class NumberFuzzer(BaseFuzzer): def __init__(self, elements, p=0.5, max_l0=float('inf'), scale=10, unsigned=False, is_int=False, precision=0): super(NumberFuzzer, self).__init__(elements, p, max_l0) self.elements = [x for x in [tofloat(x) for x in self.elements] if (x is not None)] self.s...
class SemesterFuzzer(BaseFuzzer): def __init__(self, elements, p): super(SemesterFuzzer, self).__init__(elements, p) self.semesters = ['Fall', 'Winter', 'Spring', 'Summer'] def one_sample(self): return random.choice(self.semesters)
class AdvisingTimeFuzzer(BaseFuzzer): def rand_time_in_day(self): randtime = (start + (random.random() * timedelta(days=1))) s = randtime.strftime('%H-%M-%S') return s def __init__(self, elements, p): super(AdvisingTimeFuzzer, self).__init__(elements, p) def one_sample(s...
def represents_int(s): try: int(s) return True except ValueError: return False
def rand_string(length: int) -> str: return ''.join([random.choice(CHARSET) for _ in range(length)])
def contaminate(s: str) -> str: p = random.random() if (p < 0.2): return (s + rand_string(5)) if (p < 0.4): return (rand_string(5) + s) if (p < 0.6): return ((rand_string(3) + s) + rand_string(3)) if (p < 0.8): return s[:(- 1)] return s[1:]
class StringFuzzer(BaseFuzzer): def __init__(self, elements, p, max_l0, length=20): super(StringFuzzer, self).__init__(elements, p, max_l0) self.allow_none = (random.random() < 0.1) self.length = length self.elements = [str(e) for e in self.elements] for e in self.elements...
def random_time(): return ':'.join([random.choice(l) for l in space])
def perturb(t): nums = [int(x) for x in t.split(':')] change_digit = random.randint(0, 2) nums[change_digit] = (nums[change_digit] - 1) return ':'.join([space[i][nums[i]] for i in range(3)])
class TimeFuzzer(BaseFuzzer): def __init__(self, elements, p=0.5, max_l0=float('inf')): super(TimeFuzzer, self).__init__(elements, p, max_l0) self.elements = elements def one_sample(self): if ((random.random() > self.p) and (len(self.elements) != 0)): t = random.choice(se...
def get_values(db_name: str) -> Set[str]: values = pkl.load(open(get_value_path(db_name), 'rb')) return values
def get_schema_path(sqlite_path: str, table_name: str) -> str: (_, schema) = exec_db_path_(sqlite_path, (table_schema_query % table_name)) schema = schema[0][0] return schema
def get_unique_keys(schema: str) -> Set[str]: schema_by_list = schema.split('\n') unique_keys = set() for r in schema_by_list: if ('unique' in r.lower()): unique_keys.add(r.strip().split()[0].upper().replace('"', '').replace('`', '')) return unique_keys
def get_checked_keys(schema: str) -> Set[str]: schema_by_list = schema.split('\n') checked_keys = set() for r in schema_by_list: if (('check (' in r) or ('check(' in r)): checked_keys.add(r.strip().split()[0].upper().replace('"', '').replace('`', '')) return checked_keys
def get_table_names_path(sqlite_path: str) -> List[str]: table_names = [x[0] for x in exec_db_path_(sqlite_path, table_name_query)[1]] return table_names
def extract_table_column_properties_path(sqlite_path: str) -> Tuple[(Dict[(str, Dict[(str, Any)])], Dict[(Tuple[(str, str)], Tuple[(str, str)])])]: table_names = get_table_names_path(sqlite_path) table_name2column_properties = OrderedDict() child2parent = OrderedDict() for table_name in table_names: ...
def collapse_key(d: Dict[(str, Dict[(str, T)])]) -> Dict[(Tuple[(str, str)], T)]: result = OrderedDict() for (k1, v1) in d.items(): for (k2, v2) in v1.items(): result[(k1, k2)] = v2 return result
def process_order_helper(dep: Dict[(E, Set[E])], all: Set[E]) -> List[Set[E]]: dep_ks = set(dep.keys()) for k in dep.values(): dep_ks |= set(k) assert (len((dep_ks - all)) == 0), (dep_ks - all) order = list(my_top_sort({k: v for (k, v) in dep.items()})) if (len(order) == 0): order....
def my_top_sort(dep: Dict[(E, Set[E])]) -> List[Set[E]]: order = [] elements_left = set() for (child, parents) in dep.items(): elements_left.add(child) elements_left |= parents while (len(elements_left) != 0): level_set = set() for e in elements_left: if (e ...
def get_process_order(child2parent: Dict[(Tuple[(str, str)], Tuple[(str, str)])], table_column_properties: Dict[(Tuple[(str, str)], Dict[(str, Any)])]) -> Tuple[(List[Set[Tuple[(str, str)]]], List[Set[str]])]: all_table_column = set(table_column_properties.keys()) dep_child2parent = {c: {p} for (c, p) in chil...
def get_all_db_info_path(sqlite_path: str) -> Tuple[(Dict[(Tuple[(str, str)], Dict[(str, Any)])], Dict[(Tuple[(str, str)], Tuple[(str, str)])], Dict[(Tuple[(str, str)], List)])]: (table_name2column_properties, child2parent) = extract_table_column_properties_path(sqlite_path) table_name2content = OrderedDict()...
def get_table_size(table_column_elements: Dict[(Tuple[(str, str)], List)]) -> Dict[(str, int)]: table_name2size = OrderedDict() for (k, elements) in table_column_elements.items(): table_name = k[0] if (table_name not in table_name2size): table_name2size[table_name] = len(elements) ...
def get_primary_keys(table_column_properties: Dict[(Tuple[(str, str)], Dict[(str, Any)])]) -> Dict[(str, List[str])]: table_name2primary_keys = OrderedDict() for ((table_name, column_name), property) in table_column_properties.items(): if (table_name not in table_name2primary_keys): table_...
def get_indexing_from_db(db_path: str, shuffle=True) -> Dict[(str, List[Dict[(str, Any)]])]: (table_column_properties, _, _) = get_all_db_info_path(db_path) all_tables_names = {t_c[0] for t_c in table_column_properties} table_name2indexes = {} for table_name in all_tables_names: column_names =...
def print_table(table_name, column_names, rows): print('table:', table_name) num_cols = len(column_names) template = ' '.join((['{:20}'] * num_cols)) print(template.format(*column_names)) for row in rows: print(template.format(*[str(r) for r in row]))
def database_pprint(path): (tc2_, _, _) = get_all_db_info_path(path) table_column_names = [tc for tc in tc2_.keys()] table_names = {t_c[0] for t_c in table_column_names} for table_name in table_names: column_names = [c for (t, c) in table_column_names if (t == table_name)] elements_by_...
def get_total_size_from_indexes(table_name2indexes: Dict[(str, List[Dict[(str, Any)]])]) -> int: return sum([len(v) for (_, v) in table_name2indexes.items()])
def get_total_size_from_path(path): (_, _, table_column2elements) = get_all_db_info_path(path) return sum([v for (_, v) in get_table_size(table_column2elements).items()])
def get_db_path(db_name: str, testcase_name: Union[(str, None)]=None) -> str: sqlite_path = os.path.join(DB_DIR, db_name, ((db_name if (testcase_name is None) else testcase_name) + '.sqlite')) return sqlite_path
def get_all_dbnames() -> Set[str]: return set([db_name for db_name in os.listdir(DB_DIR)])
def get_value_path(db_name: str) -> str: return os.path.join(DB_DIR, db_name, 'values.pkl')
def get_skipped_dbnames() -> Set[str]: return {'baseball_1', 'imdb', 'restaurants'}
def orig2test(orig_db_path: str, testcase_name: str) -> str: return ((orig_db_path.replace('.sqlite', '') + testcase_name) + '.sqlite')
def permute_tuple(element: Tuple, perm: Tuple) -> Tuple: assert (len(element) == len(perm)) return tuple([element[i] for i in perm])
def unorder_row(row: Tuple) -> Tuple: return tuple(sorted(row, key=(lambda x: (str(x) + str(type(x))))))
def quick_rej(result1: List[Tuple], result2: List[Tuple], order_matters: bool) -> bool: s1 = [unorder_row(row) for row in result1] s2 = [unorder_row(row) for row in result2] if order_matters: return (s1 == s2) else: return (set(s1) == set(s2))
def multiset_eq(l1: List, l2: List) -> bool: if (len(l1) != len(l2)): return False d = defaultdict(int) for e in l1: d[e] = (d[e] + 1) for e in l2: d[e] = (d[e] - 1) if (d[e] < 0): return False return True
def get_constraint_permutation(tab1_sets_by_columns: List[Set], result2: List[Tuple]): num_cols = len(result2[0]) perm_constraints = [{i for i in range(num_cols)} for _ in range(num_cols)] if (num_cols <= 3): return product(*perm_constraints) for _ in range(20): random_tab2_row = rando...
def result_eq(result1: List[Tuple], result2: List[Tuple], order_matters: bool) -> bool: if ((len(result1) == 0) and (len(result2) == 0)): return True if (len(result1) != len(result2)): return False num_cols = len(result1[0]) if (len(result2[0]) != num_cols): return False if...
def load_sql_file(f_path: str) -> List[str]: with open(f_path, 'r') as in_file: lines = list(in_file.readlines()) lines = [l.strip().split('\t')[0] for l in lines if (len(l.strip()) > 0)] return lines
def tokenize(query: str) -> List[Token]: tokens = list([Token(t.ttype, t.value) for t in sqlparse.parse(query)[0].flatten()]) return tokens
def join_tokens(tokens: List[Token]) -> str: return ''.join([x.value for x in tokens]).strip().replace(' ', ' ')
def round_trip_test(query: str) -> None: tokens = tokenize(query) reconstructed = ''.join([token.value for token in tokens]) assert (query == reconstructed), ('Round trip test fails for string %s' % query)
def postprocess(query: str) -> str: query = query.replace('> =', '>=').replace('< =', '<=').replace('! =', '!=') return query
def strip_query(query: str) -> Tuple[(List[str], List[str])]: (query_keywords, all_values) = ([], []) '\n str_1 = re.findall(""[^"]*"", query)\n str_2 = re.findall("\'[^\']*\'", query)\n values = str_1 + str_2\n ' toks = sqlparse.parse(query)[0].flatten() values = [t.value for t in tok...
def reformat_query(query: str) -> str: query = query.strip().replace(';', '').replace('\t', '') query = ' '.join([t.value for t in tokenize(query) if (t.ttype != sqlparse.tokens.Whitespace)]) t_stars = ['t1.*', 't2.*', 't3.*', 'T1.*', 'T2.*', 'T3.*'] for ts in t_stars: query = query.replace(ts...
def replace_values(sql: str) -> Tuple[(List[str], Set[str])]: sql = sqlparse.format(sql, reindent=False, keyword_case='upper') sql = re.sub('(T\\d+\\.)\\s', '\\1', sql) (query_toks_no_value, values) = strip_query(sql) return (query_toks_no_value, set(values))
def extract_query_values(sql: str) -> Tuple[(List[str], Set[str])]: reformated = reformat_query(query=sql) (query_value_replaced, values) = replace_values(reformated) return (query_value_replaced, values)
def plugin(query_value_replaced: List[str], values_in_order: List[str]) -> str: q_length = len(query_value_replaced) query_w_values = query_value_replaced[:] value_idx = [idx for idx in range(q_length) if (query_value_replaced[idx] == VALUE_NUM_SYMBOL.lower())] assert (len(value_idx) == len(values_in_...
def plugin_all_permutations(query_value_replaced: List[str], values: Set[str]) -> Iterator[str]: num_slots = len([v for v in query_value_replaced if (v == VALUE_NUM_SYMBOL.lower())]) for values in itertools.product(*[list(values) for _ in range(num_slots)]): (yield plugin(query_value_replaced, list(va...
def get_all_preds_for_execution(gold: str, pred: str) -> Tuple[(int, Iterator[str])]: (_, gold_values) = extract_query_values(gold) (pred_query_value_replaced, _) = extract_query_values(pred) num_slots = len([v for v in pred_query_value_replaced if (v == VALUE_NUM_SYMBOL.lower())]) num_alternatives = ...
def remove_distinct(s): toks = [t.value for t in list(sqlparse.parse(s)[0].flatten())] return ''.join([t for t in toks if (t.lower() != 'distinct')])
def extract_all_comparison_from_node(node: Token) -> List[Comparison]: comparison_list = [] if hasattr(node, 'tokens'): for t in node.tokens: comparison_list.extend(extract_all_comparison_from_node(t)) if (type(node) == Comparison): comparison_list.append(node) return compa...
def extract_all_comparison(query: str) -> List[Comparison]: tree = sqlparse.parse(query)[0] comparison_list = extract_all_comparison_from_node(tree) return comparison_list
def extract_toks_from_comparison(comparison_node: Comparison) -> List[Token]: tokens = [t for t in comparison_node.tokens if (t.ttype != Whitespace)] return tokens
def extract_info_from_comparison(comparison_node: Comparison) -> Dict[(str, Any)]: tokens = extract_toks_from_comparison(comparison_node) (left, op, right) = tokens returned_dict = {'left': left, 'op': op.value, 'right': right} if (type(left) != Identifier): return returned_dict table = No...
def extract_all_comparison_from_query(query: str) -> List[Dict[(str, Any)]]: comparison_list = extract_all_comparison(query) return [extract_info_from_comparison(c) for c in comparison_list]
def rm_placeholder(s: Union[(str, None)]) -> Union[(str, None)]: if (s is None): return None return re.sub('placeholderrare', '', s, flags=re.IGNORECASE)
def typed_values_in_tuples(query): groups = in_tuple_pattern.findall(query) typed_values = [] for group in groups: if ('SELECT' in group[1].upper()): continue tab_col = (None, rm_placeholder(group[0].upper())) vals = [x.strip().replace('"', '') for x in group[1].split('...
def extract_typed_value_in_comparison_from_query(query: str) -> List[Tuple[(Tuple[(Union[(str, None)], str)], str)]]: query = re.sub('\\byear\\b', 'yearplaceholderrare', query, flags=re.IGNORECASE) query = re.sub('\\bnumber\\b', 'numberplaceholderrare', query, flags=re.IGNORECASE) query = re.sub('\\blengt...
def process_str_value(v: str) -> str: if ((len(v) > 0) and (v[0] in QUOTE_CHARS)): v = v[1:] if ((len(v) > 0) and (v[(- 1)] in QUOTE_CHARS)): v = v[:(- 1)] for c in QUOTE_CHARS: v = v.replace((c + c), c) return v
def parse_type_in_schema(schema_type: str): if ('(' in schema_type): paren_idx = schema_type.index('(') body = schema_type[:paren_idx].strip() close_paren_idx = schema_type.index(')') parameter_str = schema_type[(paren_idx + 1):close_paren_idx] arguments = [int(x) for x in ...
class Schema(): '\n Simple schema which maps table&column to a unique identifier\n ' def __init__(self, schema): self._schema = schema self._idMap = self._map(self._schema) @property def schema(self): return self._schema @property def idMap(self): retur...
def get_schema(db): "\n Get database's schema, which is a dict with table name as key\n and list of column names as value\n :param db: database path\n :return: schema dict\n " schema = {} conn = sqlite3.connect(db) cursor = conn.cursor() cursor.execute("SELECT name FROM sqlite_maste...
def get_schema_from_json(fpath): with open(fpath) as f: data = json.load(f) schema = {} for entry in data: table = str(entry['table'].lower()) cols = [str(col['column_name'].lower()) for col in entry['col_data']] schema[table] = cols return schema
def tokenize(string): string = str(string) string = string.replace("'", '"') quote_idxs = [idx for (idx, char) in enumerate(string) if (char == '"')] assert ((len(quote_idxs) % 2) == 0), 'Unexpected quote' vals = {} for i in range((len(quote_idxs) - 1), (- 1), (- 2)): qidx1 = quote_idx...
def scan_alias(toks): "Scan the index of 'as' and build the map for all alias" as_idxs = [idx for (idx, tok) in enumerate(toks) if (tok == 'as')] alias = {} for idx in as_idxs: alias[toks[(idx + 1)]] = toks[(idx - 1)] return alias
def get_tables_with_alias(schema, toks): tables = scan_alias(toks) for key in schema: assert (key not in tables), 'Alias {} has the same name in table'.format(key) tables[key] = key return tables
def parse_col(toks, start_idx, tables_with_alias, schema, default_tables=None): '\n :returns next idx, column id\n ' tok = toks[start_idx] if (tok == '*'): return ((start_idx + 1), schema.idMap[tok]) if ('.' in tok): (alias, col) = tok.split('.') key = ((tables_with_a...
def parse_col_unit(toks, start_idx, tables_with_alias, schema, default_tables=None): '\n :returns next idx, (agg_op id, col_id)\n ' idx = start_idx len_ = len(toks) isBlock = False isDistinct = False if (toks[idx] == '('): isBlock = True idx += 1 if (toks[idx] in ...
def parse_val_unit(toks, start_idx, tables_with_alias, schema, default_tables=None): idx = start_idx len_ = len(toks) isBlock = False if (toks[idx] == '('): isBlock = True idx += 1 col_unit1 = None col_unit2 = None unit_op = UNIT_OPS.index('none') (idx, col_unit1) = par...
def parse_table_unit(toks, start_idx, tables_with_alias, schema): '\n :returns next idx, table id, table name\n ' idx = start_idx len_ = len(toks) key = tables_with_alias[toks[idx]] if (((idx + 1) < len_) and (toks[(idx + 1)] == 'as')): idx += 3 else: idx += 1 ret...
def parse_value(toks, start_idx, tables_with_alias, schema, default_tables=None): idx = start_idx len_ = len(toks) isBlock = False if (toks[idx] == '('): isBlock = True idx += 1 if (toks[idx] == 'select'): (idx, val) = parse_sql(toks, idx, tables_with_alias, schema) eli...
def parse_condition(toks, start_idx, tables_with_alias, schema, default_tables=None): idx = start_idx len_ = len(toks) conds = [] while (idx < len_): (idx, val_unit) = parse_val_unit(toks, idx, tables_with_alias, schema, default_tables) not_op = False if (toks[idx] == 'not'): ...
def parse_select(toks, start_idx, tables_with_alias, schema, default_tables=None): idx = start_idx len_ = len(toks) assert (toks[idx] == 'select'), "'select' not found" idx += 1 isDistinct = False if ((idx < len_) and (toks[idx] == 'distinct')): idx += 1 isDistinct = True v...
def parse_from(toks, start_idx, tables_with_alias, schema): '\n Assume in the from clause, all table units are combined with join\n ' assert ('from' in toks[start_idx:]), "'from' not found" len_ = len(toks) idx = (toks.index('from', start_idx) + 1) default_tables = [] table_units = [] ...
def parse_where(toks, start_idx, tables_with_alias, schema, default_tables): idx = start_idx len_ = len(toks) if ((idx >= len_) or (toks[idx] != 'where')): return (idx, []) idx += 1 (idx, conds) = parse_condition(toks, idx, tables_with_alias, schema, default_tables) return (idx, conds)...
def parse_group_by(toks, start_idx, tables_with_alias, schema, default_tables): idx = start_idx len_ = len(toks) col_units = [] if ((idx >= len_) or (toks[idx] != 'group')): return (idx, col_units) idx += 1 assert (toks[idx] == 'by') idx += 1 while ((idx < len_) and (not ((toks...
def parse_order_by(toks, start_idx, tables_with_alias, schema, default_tables): idx = start_idx len_ = len(toks) val_units = [] order_type = 'asc' if ((idx >= len_) or (toks[idx] != 'order')): return (idx, val_units) idx += 1 assert (toks[idx] == 'by') idx += 1 while ((idx ...
def parse_having(toks, start_idx, tables_with_alias, schema, default_tables): idx = start_idx len_ = len(toks) if ((idx >= len_) or (toks[idx] != 'having')): return (idx, []) idx += 1 (idx, conds) = parse_condition(toks, idx, tables_with_alias, schema, default_tables) return (idx, cond...
def parse_limit(toks, start_idx): idx = start_idx len_ = len(toks) if ((idx < len_) and (toks[idx] == 'limit')): idx += 2 return (idx, int(toks[(idx - 1)])) return (idx, None)
def parse_sql(toks, start_idx, tables_with_alias, schema): isBlock = False len_ = len(toks) idx = start_idx sql = {} if (toks[idx] == '('): isBlock = True idx += 1 (from_end_idx, table_units, conds, default_tables) = parse_from(toks, start_idx, tables_with_alias, schema) sq...
def load_data(fpath): with open(fpath) as f: data = json.load(f) return data
def get_sql(schema, query): toks = tokenize(query) tables_with_alias = get_tables_with_alias(schema.schema, toks) (_, sql) = parse_sql(toks, 0, tables_with_alias, schema) return sql
def skip_semicolon(toks, start_idx): idx = start_idx while ((idx < len(toks)) and (toks[idx] == ';')): idx += 1 return idx
def get_cursor_path(sqlite_path: str): try: if (not os.path.exists(sqlite_path)): print(('Openning a new connection %s' % sqlite_path)) connection = sqlite3.connect(sqlite_path) except Exception as e: print(sqlite_path) raise e connection.text_factory = (lambda ...
def can_execute_path(sqlite_path: str, q: str) -> bool: (flag, result) = exec_db_path_(sqlite_path, q) return (flag == 'result')
def clean_tmp_f(f_prefix: str): with threadLock: for suffix in ('.in', '.out'): f_path = (f_prefix + suffix) if os.path.exists(f_path): os.unlink(f_path)
def exec_db_path(sqlite_path: str, query: str, process_id: str='', timeout: int=TIMEOUT) -> Tuple[(str, Any)]: f_prefix = None with threadLock: while ((f_prefix is None) or os.path.exists((f_prefix + '.in'))): process_id += str(time.time()) process_id += str(random.randint(0, 1...