Skip to content

Commit 23b9040

Browse files
committed
add data and code
1 parent 1c9049c commit 23b9040

File tree

8 files changed

+2074
-0
lines changed

8 files changed

+2074
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.DS_Store
2+
config.py
3+
images/*
4+
__pycache__/
5+
*.py[cod]

README.md

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# SQL Examples
2+
3+
This repository contains a collection of SQL examples that demonstrate various types of queries. The purpose of this repository is to serve as a reference for users looking to understand and learn different SQL query patterns.
4+
5+
## Structure
6+
7+
The repository is structured as follows:
8+
9+
sql-examples/
10+
├── data/
11+
│ ├── example1.sql
12+
│ ├── example2.sql
13+
│ └── ...
14+
├── lib/
15+
│ └── database.py
16+
├── output.sql
17+
└── README.md
18+
19+
20+
- The `data/` directory contains a collection of `.sql` files, each representing a specific SQL query examples for a given table. These files are meant to showcase different query patterns and serve as a starting point for building more complex queries.
21+
22+
- The `lib/` directory contains the `database.py` module, which provides methods for generating new SQL queries. These methods can be used to create additional `.sql` files in the `data/` directory.
23+
24+
- The `output.sql` file is a log file generated by the `database.py` module. It records the SQL queries generated by the program.
25+
26+
## Contribution
27+
28+
Contributions to this repository are welcome! If you have a new SQL query example or improvements to existing examples, please feel free to submit a pull request. Make sure to follow the guidelines in the [CONTRIBUTING.md](CONTRIBUTING.md) file.
29+
30+
## License
31+
32+
This repository is licensed under the [MIT License](LICENSE). You are free to use, modify, and distribute the code and examples as per the terms of the license.
33+
34+
We hope you find this repository helpful in exploring different SQL query patterns and generating queries for your own projects. If you have any questions or feedback, please don't hesitate to reach out.
35+
36+
Happy querying!
37+
38+
## Example: Collatz Sequence Table
39+
To create and populate the Collatz sequences in FeatureBase Cloud, get the token from Cloud first:
40+
41+
```
42+
python3 fb_token.py
43+
```
44+
45+
Put this token, along with the path of the endpoint in a file called 'config.py'. Reference 'config.py.sample' for the format.
46+
47+
Note that the `/query/sql` needs to be stripped from the end of the URL you use in the config file.
48+
49+
The result of a run will be a table called `collatz_flotz` in your account.
50+
51+
## Possible Queries
52+
It would be interesting to build a query that traverses graphs.
53+
54+
More information: https://www.quantamagazine.org/why-mathematicians-still-cant-solve-the-collatz-conjecture-20200922/

collatz.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import random
2+
import time
3+
from lib import database
4+
5+
try:
6+
query = "CREATE TABLE collatz_flotz (_id id, prev_set idset, next_set id);"
7+
result = database.featurebase_query({"sql": query})
8+
except Exception as ex:
9+
print(ex)
10+
11+
values = ""
12+
for x in range(1,30000):
13+
# print("running %s" % x)
14+
15+
# the proof is we foolishly believe this will exit
16+
while True:
17+
# set what we are, currently
18+
prev_set = x
19+
20+
# check if we are odd or even
21+
is_odd = x % 2
22+
23+
# run the algo and update our number
24+
if is_odd:
25+
x = (x * 3) + 1
26+
else:
27+
x = int(x / 2)
28+
29+
# query for if we have the newly calculated number already
30+
query = "SELECT next_set FROM collatz_flotz WHERE _id = %s" % x
31+
result = database.featurebase_query({"sql": query})
32+
33+
# we already have the next number, so we add x to the prev_set set and exit loop
34+
if result.get('data', None):
35+
_next_set = result.get('data')[0][0]
36+
37+
if values != "":
38+
values = values + ","
39+
40+
values = values + "(%s, [%s], %s)" % (x, prev_set, _next_set)
41+
query = "INSERT INTO collatz_flotz (_id, prev_set, next_set) VALUES %s;" % values
42+
result = database.featurebase_query({"sql": query})
43+
values = ""
44+
break
45+
else:
46+
# we don't have the next number, so we set it
47+
is_odd = x % 2
48+
49+
if is_odd:
50+
next_set = (x * 3) + 1
51+
else:
52+
next_set = int(x / 2)
53+
54+
if values != "":
55+
values = values + ","
56+
57+
values = values + "(%s, [%s], %s)" % (x, prev_set, next_set)
58+
if x == 1:
59+
query = "INSERT INTO collatz_flotz (_id, prev_set, next_set) VALUES %s;" % values
60+
result = database.featurebase_query({"sql": query})
61+
values = ""
62+
break

config.py.sample

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# config
2+
dev = "True"
3+
4+
# tokens
5+
featurebase_token = "TOKEN"
6+
7+
# featurebase (please ensure "/query/sql" is stripped from the URL)
8+
featurebase_endpoint = "https://query.featurebase.com/v2/databases/xxxx-xxxx"

0 commit comments

Comments
 (0)