(query: str)
| 34 | # strip_query, reformat_query and replace values |
| 35 | # were implemented by Yu Tao for processing CoSQL |
| 36 | def strip_query(query: str) -> Tuple[List[str], List[str]]: |
| 37 | query_keywords, all_values = [], [] |
| 38 | |
| 39 | # then replace all stuff enclosed by "" with a numerical value to get it marked as {VALUE} |
| 40 | |
| 41 | # Tao's implementation is commented out here. |
| 42 | """ |
| 43 | str_1 = re.findall("\"[^\"]*\"", query) |
| 44 | str_2 = re.findall("\'[^\']*\'", query) |
| 45 | values = str_1 + str_2 |
| 46 | """ |
| 47 | |
| 48 | toks = sqlparse.parse(query)[0].flatten() |
| 49 | values = [t.value for t in toks if t.ttype == sqlparse.tokens.Literal.String.Single or t.ttype == sqlparse.tokens.Literal.String.Symbol] |
| 50 | |
| 51 | |
| 52 | for val in values: |
| 53 | all_values.append(val) |
| 54 | query = query.replace(val.strip(), VALUE_NUM_SYMBOL) |
| 55 | |
| 56 | query_tokenized = query.split() |
| 57 | float_nums = re.findall("[-+]?\d*\.\d+", query) |
| 58 | all_values += [qt for qt in query_tokenized if qt in float_nums] |
| 59 | query_tokenized = [VALUE_NUM_SYMBOL if qt in float_nums else qt for qt in query_tokenized] |
| 60 | |
| 61 | query = " ".join(query_tokenized) |
| 62 | int_nums = [i.strip() for i in re.findall("[^tT]\d+", query)] |
| 63 | |
| 64 | all_values += [qt for qt in query_tokenized if qt in int_nums] |
| 65 | query_tokenized = [VALUE_NUM_SYMBOL if qt in int_nums else qt for qt in query_tokenized] |
| 66 | # print int_nums, query, query_tokenized |
| 67 | |
| 68 | for tok in query_tokenized: |
| 69 | if "." in tok: |
| 70 | table = re.findall("[Tt]\d+\.", tok) |
| 71 | if len(table) > 0: |
| 72 | to = tok.replace(".", " . ").split() |
| 73 | to = [t.lower() for t in to if len(t) > 0] |
| 74 | query_keywords.extend(to) |
| 75 | else: |
| 76 | query_keywords.append(tok.lower()) |
| 77 | |
| 78 | elif len(tok) > 0: |
| 79 | query_keywords.append(tok.lower()) |
| 80 | return query_keywords, all_values |
| 81 | |
| 82 | |
| 83 | def reformat_query(query: str) -> str: |
no test coverage detected