SQLY supports user-defined functions (UDFs) to extend its capabilities by creating reusable logic for queries.
Users can create custom functions with parameters and return types.
udf:
name: calculate_tax
parameters:
- name: price
type: float
- name: tax_rate
type: float
return_type: float
body: |
return price * tax_rateThis defines a calculate_tax function to compute tax based on a given price and tax rate.
UDFs can be called in SQLY queries to process data dynamically.
query:
select: [product_id, calculate_tax(price, 0.07)]
from: productsThis applies the calculate_tax function to compute tax for each product dynamically.
SQLY supports advanced UDF capabilities, including conditional logic and iteration.
udf:
name: discount_price
parameters:
- name: price
type: float
return_type: float
body: |
if price > 100:
return price * 0.9
else:
return priceThis function applies a discount for prices above 100.
SQLY allows integration with external libraries or APIs within UDFs.
udf:
name: get_exchange_rate
parameters:
- name: currency
type: string
return_type: float
external:
type: REST
endpoint: "https://api.exchangerate.com/latest"
method: GET
query_params:
base: "USD"
target: "currency"This function fetches exchange rates from an external API.
- Define UDFs with parameters and return types.
- Use UDFs dynamically in queries.
- Implement conditional logic within functions.
- Register external functions to call APIs or libraries.