-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
496 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
database_engines: | ||
- name: Aurora | ||
functionalities: | ||
- geospatial | ||
|
||
- name: Athena | ||
functionalities: | ||
- geospatial | ||
|
||
- name: Redshift | ||
functionalities: [] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from brad.config.engine import Engine | ||
from typing import List | ||
import operator | ||
import yaml | ||
from functools import reduce | ||
|
||
|
||
class Functionality: | ||
|
||
Geospatial = "geospatial" | ||
|
||
def __init__(self, functionality_yaml="engine_functionality.yml"): | ||
|
||
# Read the YAML file | ||
with open(functionality_yaml, 'r') as yaml_file: | ||
data = yaml.load(yaml_file, Loader=yaml.FullLoader) | ||
|
||
# Initialize lists for each database engine's functionalities | ||
aurora_functionalities = [] | ||
athena_functionalities = [] | ||
redshift_functionalities = [] | ||
|
||
# Parse the data into the respective lists | ||
for engine in data['database_engines']: | ||
if engine['name'] == 'Aurora': | ||
aurora_functionalities = engine['functionalities'] | ||
elif engine['name'] == 'Athena': | ||
athena_functionalities = engine['functionalities'] | ||
elif engine['name'] == 'Redshift': | ||
redshift_functionalities = engine['functionalities'] | ||
|
||
# Convert to bitmaps | ||
engine_functionality_strings = [athena_functionalities, aurora_functionalities, redshift_functionalities] | ||
self.engine_functionalities = [Functionality.to_bitmap(f) for f in engine_functionality_strings] | ||
|
||
@staticmethod | ||
def to_bitmap(functionalities: List["Functionality"]) -> int: | ||
if len(functionalities) == 0: | ||
return 0 | ||
return reduce( | ||
# Bitwise OR | ||
operator.or_, | ||
map(lambda f: FunctionalityBitmapValues[f], functionalities), | ||
0, | ||
) | ||
|
||
def get_engine_functionalities(self) -> List[int]: | ||
""" | ||
Return a bitmap for each engine that states what functionalities the | ||
engine supports | ||
""" | ||
return self.engine_functionalities | ||
|
||
FunctionalityBitmapValues = {} | ||
FunctionalityBitmapValues[Functionality.Geospatial] = 0b1 |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Specialized functionality | ||
|
||
This directory contains helper functions that help BRAD run queries with specialized functionality (like geospatial queries). | ||
|
||
## Geospatial queries | ||
|
||
In `QueryRep`, we determine whether a query makes use of geospatial functions by determining whether PostGIS keywords appear in the query. `geospatial_keywords.yml` contains a list of the PostGIS keywords that BRAD considers. `geospatial_keywords.yml` can be updated by running `python geospatial_keywords.py`, which crawls a list of PostGIS keywords from [PostGIS' specialized functions index](https://postgis.net/docs/manual-1.5/ch08.html). | ||
|
||
Crawling the PostGIS documentation requires `requests` and `bs4`. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import requests | ||
This comment has been minimized.
Sorry, something went wrong. |
||
from bs4 import BeautifulSoup | ||
import re | ||
import yaml | ||
|
||
if __name__ == "__main__": | ||
|
||
# URL of PostGIS special functions index | ||
url = "https://postgis.net/docs/manual-1.5/ch08.html" | ||
|
||
# Send an HTTP GET request to the URL | ||
response = requests.get(url) | ||
|
||
# Check if the request was successful | ||
if response.status_code == 200: | ||
# Parse the HTML content of the page | ||
soup = BeautifulSoup(response.text, "html.parser") | ||
|
||
# Find all list items within the page | ||
list_items = soup.find_all("li") | ||
|
||
# Initialize an empty list to store the extracted keywords | ||
keywords = [] | ||
|
||
# Define a regular expression pattern to match the keywords | ||
keyword_pattern = r"^(.*?)\s-\s" | ||
|
||
# Iterate over each list item and extract the keyword | ||
for item in list_items: | ||
text = item.get_text() | ||
match = re.search(keyword_pattern, text) | ||
if match: | ||
keyword = match.group(1).strip() | ||
keywords.append(keyword) | ||
|
||
# Define the output YAML file name | ||
output_yaml_file = "postgis_keywords.yml" | ||
|
||
# Write the extracted keywords to a YAML file | ||
with open(output_yaml_file, "w") as yaml_file: | ||
yaml.dump(keywords, yaml_file, default_flow_style=False) | ||
|
||
print(f"Extracted {len(keywords)} keywords and saved to {output_yaml_file}") | ||
else: | ||
print(f"Failed to retrieve the webpage. Status code: {response.status_code}") |
Oops, something went wrong.
Move into
tools/