def repl() -> None: while True: try: user_input = input(prompt_with_color()) except (KeyboardInterrupt, EOFError): sys.exit(1) if user_input == "exit": break lexer = Lexer(program=user_input) parser = Parser(lexer=lexer) commands: List[Optional[Union[CreateTable, Insert, Select]]] = [] try: commands = list(parser.parse()) except ValueError as e: print(e) continue if len(commands) == 0: print("no command given") for command in commands: if command is None: continue try: tables = get_tables() evaluator = Evaluator(tables=tables) result = evaluator.handle_command(command) except ValueError as e: print(e) continue if isinstance(result, str): print(result) else: print_selection(result)
def test_eval_in_memory(self): user_input = "select * from family" lexer = Lexer(program=user_input) parser = Parser(lexer=lexer) commands: List[Optional[Union[CreateTable, Insert, Select]]] = [] commands = list(parser.parse()) table = Table.from_file("family", storage_type="in-memory") tables = {"family": table} evaluator = Evaluator(tables=tables) result = evaluator.handle_command(commands[0]) assert len(result) > 10 assert result[0] == ["vegard", 26]
def select() -> None: table_name = "bench_select" if not (Path(__file__).parent.parent / f"{table_name}.{VGDB_FILE_SUFFIX}").exists(): print("Creating table...") create_table(table_name) evaluator = Evaluator(tables=get_tables()) words = read_words() insert_words(evaluator, table_name, words) run_select_benchmark(table_name)
def run_insert_benchmark(table: str) -> None: create_table(table) evaluator = Evaluator(tables=get_tables()) words = read_words() n = len(words) insert_printout = f"Inserting {n} records... (to disk)" print(f"{insert_printout:<70}", end="") sys.stdout.flush() start = time.time() insert_words(evaluator, table, words) elapsed = time.time() - start print(f"{elapsed:>10.5f} seconds")
def run_in_memory_insert_benchmark(table: str) -> None: table_object = Table(name=table, columns=[("number", int), ("words", str)], storage_type="in-memory") table_object.persist() evaluator = Evaluator(tables={table: table_object}) words = read_words() n = len(words) insert_printout = f"Inserting {n} records... (in memory)" print(f"{insert_printout:<70}", end="") sys.stdout.flush() start = time.time() insert_words(evaluator, table, words) elapsed = time.time() - start print(f"{elapsed:>10.5f} seconds")
def create_table(table: str) -> None: evaluator = Evaluator(tables={}) create_command = f"CREATE TABLE {table} (number int, words text)" print(run_command(evaluator, create_command))
def time_command(sql_string: str) -> None: evaluator = Evaluator(tables=get_tables()) start = time.time() run_command(evaluator, sql_string) elapsed = time.time() - start print(f"{sql_string:<70}{elapsed:>10.5f} seconds")
def run_command(evaluator: Evaluator, sql_string: str) -> Union[str, List[List[Union[str, int]]]]: lexer = Lexer(program=sql_string) parser = Parser(lexer=lexer) commands = list(parser.parse()) return evaluator.handle_command(commands[0])