learnedcardinalities
learnedcardinalities copied to clipboard
some questions
Great work!Thanks for sharing your results! I encountered some problems during my experiments. If you have time, please answer for me. Very sorry for disturbing you.
1.We note that 10,000 queries is not a small number. We are curious as to how you generate these queries and get the exact number of rows corresponding to them. 2.Another question is, how did you convert xxx.sql to xxx.csv?
Looking forward to your reply!
Hi, I address them in my experiments like the following methods:
- First determine join tables like [title, movie_info, cast_info] and join predicates are also determined, then random generate number (maybe 1 to 3), column (like t.id or mi.movie_id), operators (like >, < or =) and operands (random number between minval and maxval for determined column) for filter predicates.
- For each row in xxx.csv,there are 4 parts splited with #, I write a program to generate csv format given a query. Hope it helpful for you.
def parse_query2csvformat(query_text):
# remove ;
query_text = query_text.replace('\n', ' ').replace(';', '').strip()
while ' ' in query_text:
query_text = query_text.replace(' ', ' ')
tables_text = [table.strip() for table in query_text.split('FROM')[1].split('WHERE')[0].split(',')]
predicates_text = ''
if len(query_text.split('WHERE')) == 2:
predicates_text = query_text.split('WHERE')[1].strip()
# parse tables
tables = []
for table_text in tables_text:
table_name = []
if 'AS' in table_text:
table_names = [table_name.strip() for table_name in table_text.split('AS')]
else:
table_names = [table_name.strip() for table_name in table_text.split(' ')]
tables.append([table_names[0], table_names[1]])
# parse predicates
brackets_num = 0
all_predicates = [] # contains join predicates and filter predicates
predicate = ''
while len(predicates_text) > 0:
if predicates_text.startswith('('):
predicate += '('
brackets_num += 1
for i in range(1, len(predicates_text)):
predicate += predicates_text[i]
if predicates_text[i] == '(':
brackets_num += 1
elif predicates_text[i] == ')':
brackets_num -= 1
if brackets_num == 0:
predicates_text = predicates_text[i + 1:].strip()
break
all_predicates.append(predicate)
predicate = ''
elif predicates_text.startswith('AND'):
predicates_text = predicates_text[3:].strip()
else:
AND_break = True
for i in range(0, len(predicates_text)):
if predicates_text[i:].strip().startswith('BETWEEN'):
AND_break = False
predicate += predicates_text[i]
if AND_break:
if predicates_text[i + 1:].strip().startswith('AND'):
break
else:
if predicates_text[i + 1:].startswith('AND'):
AND_break = True
predicates_text = predicates_text[len(predicate):].strip()
all_predicates.append(predicate)
predicate = ''
filters = []
joins = []
for predicate in all_predicates:
is_join = False
if predicate.find('(') == -1:
if predicate.find('=') != -1:
sub_parts = [sub_part.strip() for sub_part in predicate.split('=')]
if len(sub_parts) == 2 and sub_parts[0].find('.') != -1 and sub_parts[1].find('.') != -1:
tables_key = [sub_parts[0][0:sub_parts[0].find('.')], sub_parts[1][0:sub_parts[1].find('.')]]
joins.append([tables_key, predicate])
is_join = True
if not is_join:
tmp_predicate = predicate
if tmp_predicate[0] == '(':
tmp_predicate = tmp_predicate[1:]
table = tmp_predicate.split('.')[0]
filters.append([table, predicate])
tables_part = ''
for table in tables:
tables_part += '{} {},'.format(table[0], table[1])
tables_part = tables_part[:len(tables_part)-1].strip()
joins_part = ''
for join in joins:
joins_part += '{},'.format(join[1].replace(' ', ''))
joins_part = joins_part[:len(joins_part)-1].strip()
filters_part = ''
available_filters = ['>=', '<=', '>', '<', '=', 'NOT LIKE', 'LIKE']
for ft in filters:
for aft in available_filters:
if aft in ft[1]:
ft_split = ft[1].split(aft)
ft1 = ft_split[0].strip()
ft2 = aft
ft3 = ft_split[1].strip()
filters_part += '{},{},{},'.format(ft1, ft2, ft3)
break
filters_part = filters_part[:len(filters_part)-1]
true_cardinality = 0
csv_format = '{}#{}#{}#{}'.format(tables_part, joins_part, filters_part, true_cardinality)
return csv_format
def generate_queries_csv(queries, csv_filepath):
with open(csv_filepath, 'w+') as csv_file:
for query in queries:
csv_foramt = parse_query2csvformat(query)
csv_file.write(csv_foramt + '\n')
if __name__ == '__main__':
queries = open('../../queries/neurocard-job-m.sql', 'r+').readlines()
csv_filepath = 'neurocard-job-m.csv'
generate_queries_csv(queries, csv_filepath)
Thanks !!!!