This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Run this in your instance that doesn't have a direction connection to the DB | |
import sqlalchemy | |
from sshtunnel import SSHTunnelForwarder | |
import pandas as pd | |
bastion_url = "ec2-xxxxx.us-west-1.compute.amazonaws.com" # The EC2 instance from which you can connect to RDS | |
host_prod = "my-db-instance.us-west-1.rds.amazonaws.com" # RDS instance URL | |
username = "postgres" # DB username |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from b_tree_index import BTreeIndex | |
import pprint | |
if __name__ == "__main__": | |
tbl = [ | |
{"id": 1, "col_a": 7, "col_b": "A", "TID": 101}, | |
{"id": 2, "col_a": 7, "col_b": "B", "TID": 102}, | |
{"id": 3, "col_a": 25, "col_b": "C", "TID": 103}, | |
{"id": 4, "col_a": 30, "col_b": "D", "TID": 104}, | |
{"id": 5, "col_a": 4, "col_b": "E", "TID": 105}, | |
{"id": 6, "col_a": 55, "col_b": "F", "TID": 106}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DoublyLinkedNode(object): | |
def __init__(self, value: BPlusTreeNode) -> None: | |
""""Init a doubly-linked list node | |
Params: | |
value (BPlusTreeNode) - The value of the node as a B+ tree node, specifically leaf node | |
""" | |
if(type(value) != BPlusTreeNode): | |
print("The value of the node needs to be a BPlusTreeNode") | |
else: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def lookup_using_hash_index(self, value: str) -> list: | |
""" Return all the rows where the indexed column contains a value | |
Params: | |
value (str) - The condition | |
Returns: | |
rows (list) - The matching rows from our table | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create_hash_index(self) -> None: | |
""" Creates a hash index of our data based on the column | |
""" | |
self.hash_table = dict() | |
for row in self.table: | |
hsh, buck = self.hash_function(row[self.on]) | |
if(self.hash_table.get(buck, None) is None): | |
# Empty bucket, insert TID | |
self.hash_table[buck] = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HashIndex(object): | |
def __init__(self, data: List, col: str) -> None: | |
""" Init the hash index class | |
Params: | |
data (list) - Our data as a list of dicts | |
col (str) - The column (dict key) to create an index on | |
""" | |
self.table = data | |
self.on = col |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def query(): | |
if(accept("SELECT")): | |
if(columns()): | |
if(accept("FROM")): | |
if(name()): | |
if(terminal(False)): | |
show_tree(tree) | |
elif(accept("WHERE")): | |
if(condition_list()): | |
if(terminal()): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def columns(): | |
if(accept("all_cols", False)): | |
return True | |
else: | |
if(accept("name")): | |
if(accept("punctuation", False)): | |
columns() # Recursively call self till next token is no longer a punctuation or name | |
else: | |
return True | |
else: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def term(): | |
if(accept("integer", False) or accept("float", False) or accept("name", False)): | |
return True | |
def terminal(): | |
if(accept("terminal")): | |
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def query(): | |
""" <query> ::= "SELECT " <columns> " FROM " <name> <terminal> | "SELECT " <columns> " FROM " <name> " WHERE " <conditionList> <terminal> | |
""" | |
pass | |
def columns(): | |
""" <columns> ::= (<name> ", ")+ | "*" | |
Accepts: | |
- * |
NewerOlder