def get_table_from_alter_table(line, alter_expr): """ Parse the content and return full qualified schema.table from the line if schema provided, else return the table name. Fact: if schema name or table name contains any special chars, each should be double quoted already in dump file. """ dot_separator_idx = line.find('.') last_double_quote_idx = line.rfind('"') has_schema_table_fmt = True if dot_separator_idx != -1 else False has_special_chars = True if last_double_quote_idx != -1 else False if not has_schema_table_fmt and not has_special_chars: line[len(alter_expr):].split()[0] elif has_schema_table_fmt and not has_special_chars: full_table_name = line[len(alter_expr):].split()[0] _, table = split_fqn(full_table_name) return table elif not has_schema_table_fmt and has_special_chars: return line[len(alter_expr) : last_double_quote_idx + 1] else: if dot_separator_idx < last_double_quote_idx: # table name is double quoted full_table_name = line[len(alter_expr) : last_double_idx + 1] else: # only schema name double quoted ending_space_idx = line.find(' ', dot_separator_idx) full_table_name = line[len(alter_expr) : ending_space_idx] _, table = split_fqn(full_table_name) return table
def get_table_from_alter_table(line, alter_expr): """ Parse the content and return full qualified schema.table from the line if schema provided, else return the table name. Fact: if schema name or table name contains any special chars, each should be double quoted already in dump file. """ dot_separator_idx = line.find('.') last_double_quote_idx = line.rfind('"') has_schema_table_fmt = True if dot_separator_idx != -1 else False has_special_chars = True if last_double_quote_idx != -1 else False if not has_schema_table_fmt and not has_special_chars: line[len(alter_expr):].split()[0] elif has_schema_table_fmt and not has_special_chars: full_table_name = line[len(alter_expr):].split()[0] _, table = split_fqn(full_table_name) return table elif not has_schema_table_fmt and has_special_chars: return line[len(alter_expr):last_double_quote_idx + 1] else: if dot_separator_idx < last_double_quote_idx: # table name is double quoted full_table_name = line[len(alter_expr):last_double_idx + 1] else: # only schema name double quoted ending_space_idx = line.find(' ', dot_separator_idx) full_table_name = line[len(alter_expr):ending_space_idx] _, table = split_fqn(full_table_name) return table
def check_table(schema, line, search_str, dump_tables, schema_level_restore_list=None, is_rule=False): if schema_level_restore_list and schema in schema_level_restore_list: return True if dump_tables: try: comp_set = set() start = line.index(search_str) + len(search_str) if is_rule: # cut the line nicely based on extra keyword for create rule statement # in case [WHERE condition] clause contains any special chars, cut before WHERE end = locate_unquoted_keyword(line, extra_rule_keyword[0]) if end == -1: end = locate_unquoted_keyword(line, extra_rule_keyword[1]) line = line[:end] dot_separator_idx = line.find('.') last_double_quote_idx = line.rfind('"') has_schema_table_fmt = True if dot_separator_idx != -1 else False has_special_chars = True if last_double_quote_idx != -1 else False if not has_schema_table_fmt and not has_special_chars: table = line[start:].split()[0] elif has_schema_table_fmt and not has_special_chars: full_table_name = line[start:].split()[0] _, table = split_fqn(full_table_name) elif not has_schema_table_fmt and has_special_chars: table = line[start:last_double_quote_idx + 1] else: if dot_separator_idx < last_double_quote_idx: # table name is double quoted full_table_name = line[start:last_double_idx + 1] else: # only schema name double quoted ending_space_idx = line.find(' ', dot_separator_idx) full_table_name = line[start:ending_space_idx] _, table = split_fqn(full_table_name) table = checkAndRemoveEnclosingDoubleQuote(table) table = removeEscapingDoubleQuoteInSQLString(table, False) comp_set.add((schema, table)) if comp_set.issubset(dump_tables): return True return False except: return False else: return False
def check_table(schema, line, search_str, dump_tables, schema_level_restore_list=None, is_rule=False): if schema_level_restore_list and schema in schema_level_restore_list: return True if dump_tables: try: comp_set = set() start = line.index(search_str) + len(search_str) if is_rule: # cut the line nicely based on extra keyword for create rule statement # in case [WHERE condition] clause contains any special chars, cut before WHERE end = locate_unquoted_keyword(line, extra_rule_keyword[0]) if end == -1: end = locate_unquoted_keyword(line, extra_rule_keyword[1]) line = line[:end] dot_separator_idx = line.find(".") last_double_quote_idx = line.rfind('"') has_schema_table_fmt = True if dot_separator_idx != -1 else False has_special_chars = True if last_double_quote_idx != -1 else False if not has_schema_table_fmt and not has_special_chars: table = line[start:].split()[0] elif has_schema_table_fmt and not has_special_chars: full_table_name = line[start:].split()[0] _, table = split_fqn(full_table_name) elif not has_schema_table_fmt and has_special_chars: table = line[start : last_double_quote_idx + 1] else: if dot_separator_idx < last_double_quote_idx: # table name is double quoted full_table_name = line[start : last_double_idx + 1] else: # only schema name double quoted ending_space_idx = line.find(" ", dot_separator_idx) full_table_name = line[start:ending_space_idx] _, table = split_fqn(full_table_name) table = checkAndRemoveEnclosingDoubleQuote(table) table = removeEscapingDoubleQuoteInSQLString(table, False) comp_set.add((schema, table)) if comp_set.issubset(dump_tables): return True return False except: return False else: return False
def check_dropped_table(line, dump_tables, schema_level_restore_list, drop_table_expr): """ check if table to drop is valid (can be dropped from schema level restore) """ temp = line[len(drop_table_expr):].strip()[:-1] (schema, table) = split_fqn(temp) schema = removeEscapingDoubleQuoteInSQLString(checkAndRemoveEnclosingDoubleQuote(schema), False) table = removeEscapingDoubleQuoteInSQLString(checkAndRemoveEnclosingDoubleQuote(table), False) if (schema_level_restore_list and schema in schema_level_restore_list) or ((schema, table) in dump_tables): return True return False
def get_table_schema_set(filename): """ filename: file with true schema and table name (none escaped), don't strip white space on schema and table name in case it's part of the name """ dump_schemas = set() dump_tables = set() with open(filename) as fd: contents = fd.read() tables = contents.splitlines() for t in tables: schema, table = split_fqn(t) dump_tables.add((schema, table)) dump_schemas.add(schema) return (dump_schemas, dump_tables)