Skip to content

Commit fce98ea

Browse files
Antony BaileyAntony Bailey
authored andcommitted
add udf and json arrays
1 parent 6f170e0 commit fce98ea

File tree

3 files changed

+214
-0
lines changed

3 files changed

+214
-0
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,26 @@ Work with geospatial data to find points within a radius, bounding boxes, and di
4040

4141
Integrate predictive models for forecasting and anomaly detection within queries.
4242

43+
### 🔹 [JSON & Arrays](json_arrays.md)
44+
45+
Filter and manipulate JSON fields and array data types.
46+
47+
### 🔹 [User-defined Functions (UDFs)](user_defined_functions.md)
48+
49+
Define and use custom functions to extend SQLY’s capabilities.
50+
51+
### 🔹 [Performance Optimization](performance.md)
52+
53+
Learn techniques to optimize query execution and improve efficiency.
54+
55+
### 🔹 [Advanced Debugging](debugging.md)
56+
57+
Enable query tracing, logging, and error handling for efficient debugging.
58+
59+
### 🔹 [SQLY Extensions](extensions.md)
60+
61+
Explore custom functions, external data sources, and event-driven triggers.
62+
4363
## 🚀 Getting Started
4464

4565
To start using SQLY, check out the [Basics](basics.md) section and explore examples.

json_arrays.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# JSON & Arrays in SQLY
2+
3+
## 📖 Introduction
4+
5+
SQLY provides powerful support for querying and manipulating JSON data and array structures, enabling efficient handling of semi-structured data.
6+
7+
---
8+
9+
## 📦 Querying JSON Fields
10+
11+
SQLY allows extracting and filtering data from JSON objects.
12+
13+
### ✅ Example 1: Extract a JSON Field
14+
15+
```yaml
16+
json_query:
17+
select: [customer_id, profile.name]
18+
from: customers
19+
where:
20+
profile.age: "> 30"
21+
```
22+
23+
This extracts the `name` field from the `profile` JSON object and filters customers older than 30.
24+
25+
### ✅ Example 2: Filter by a JSON Array Value
26+
27+
```yaml
28+
json_query:
29+
select: [order_id, items]
30+
from: orders
31+
where:
32+
items[*].category: "electronics"
33+
```
34+
35+
This retrieves orders containing items in the `electronics` category.
36+
37+
---
38+
39+
## 📑 Modifying JSON Data
40+
41+
SQLY enables updates and modifications within JSON structures.
42+
43+
### ✅ Example 3: Update a JSON Field
44+
45+
```yaml
46+
json_modify:
47+
update: customers
48+
set:
49+
profile.status: "VIP"
50+
where:
51+
customer_id: 123
52+
```
53+
54+
This updates the `status` field inside the `profile` JSON object.
55+
56+
---
57+
58+
## 📂 Working with Arrays
59+
60+
Arrays are supported for querying and filtering within SQLY.
61+
62+
### ✅ Example 4: Check If a Value Exists in an Array
63+
64+
```yaml
65+
array_query:
66+
select: [user_id, roles]
67+
from: users
68+
where:
69+
roles: contains("admin")
70+
```
71+
72+
This retrieves users who have `admin` in their `roles` array.
73+
74+
### ✅ Example 5: Unnest an Array
75+
76+
```yaml
77+
array_query:
78+
select: [user_id, unnest(permissions)]
79+
from: users
80+
```
81+
82+
This expands the `permissions` array into individual rows.
83+
84+
---
85+
86+
## 📌 Summary
87+
88+
- Query JSON fields using dot notation.
89+
- Filter JSON arrays with wildcard `[*]`.
90+
- Modify JSON data using structured updates.
91+
- Work with arrays using `contains()` and `unnest()`.

user_defined_functions.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# User-defined Functions (UDFs) in SQLY
2+
3+
## 📖 Introduction
4+
5+
SQLY supports user-defined functions (UDFs) to extend its capabilities by creating reusable logic for queries.
6+
7+
---
8+
9+
## 🔧 Defining a User-defined Function
10+
11+
Users can create custom functions with parameters and return types.
12+
13+
### ✅ Example 1: Define a Simple Function
14+
15+
```yaml
16+
udf:
17+
name: calculate_tax
18+
parameters:
19+
- name: price
20+
type: float
21+
- name: tax_rate
22+
type: float
23+
return_type: float
24+
body: |
25+
return price * tax_rate
26+
```
27+
28+
This defines a `calculate_tax` function to compute tax based on a given price and tax rate.
29+
30+
---
31+
32+
## 📌 Using UDFs in Queries
33+
34+
UDFs can be called in SQLY queries to process data dynamically.
35+
36+
### ✅ Example 2: Apply a UDF in a Query
37+
38+
```yaml
39+
query:
40+
select: [product_id, calculate_tax(price, 0.07)]
41+
from: products
42+
```
43+
44+
This applies the `calculate_tax` function to compute tax for each product dynamically.
45+
46+
---
47+
48+
## 🏗️ Advanced UDF Features
49+
50+
SQLY supports advanced UDF capabilities, including conditional logic and iteration.
51+
52+
### ✅ Example 3: UDF with Conditional Logic
53+
54+
```yaml
55+
udf:
56+
name: discount_price
57+
parameters:
58+
- name: price
59+
type: float
60+
return_type: float
61+
body: |
62+
if price > 100:
63+
return price * 0.9
64+
else:
65+
return price
66+
```
67+
68+
This function applies a discount for prices above 100.
69+
70+
---
71+
72+
## 🚀 Registering External Functions
73+
74+
SQLY allows integration with external libraries or APIs within UDFs.
75+
76+
### ✅ Example 4: Call an External API in a UDF
77+
78+
```yaml
79+
udf:
80+
name: get_exchange_rate
81+
parameters:
82+
- name: currency
83+
type: string
84+
return_type: float
85+
external:
86+
type: REST
87+
endpoint: "https://api.exchangerate.com/latest"
88+
method: GET
89+
query_params:
90+
base: "USD"
91+
target: "currency"
92+
```
93+
94+
This function fetches exchange rates from an external API.
95+
96+
---
97+
98+
## 📌 Summary
99+
100+
- Define UDFs with parameters and return types.
101+
- Use UDFs dynamically in queries.
102+
- Implement conditional logic within functions.
103+
- Register external functions to call APIs or libraries.

0 commit comments

Comments
 (0)