Feature Request: Lazy evaluation for sqlpage.fetch_with_meta() to support conditional HTTP requests
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 35/100
Hướng nghiên cứu
Start by locating the implementation and tests for sqlpage.fetch_with_meta(); the issue does not name specific files or test entry points. Read how SQL functions are registered and evaluated, then determine whether conditional or lazy fetch behavior can fit the existing execution model. Done means a supported way to avoid the external fetch when the cache condition is false, with tests demonstrating that no request is made.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Summary
Hi @lovasoa , when building a cache layer for external API calls, we need sqlpage.fetch_with_meta() to execute only when the cache is empty. Unfortunately, SQLite does not guarantee short-circuit evaluation for side-effect functions, so the fetch is executed every time regardless of the condition.
Problem
We want this pattern:
SET cached = (SELECT data FROM cache WHERE ...);
-- Only fetch if cache is empty
SET response = CASE WHEN $cached IS NULL THEN sqlpage.fetch_with_meta(...) END;
But sqlpage.fetch_with_meta() is always called, even when $cached IS NOT NULL.
What we tested
Test 1: CASE WHEN — ❌ Does NOT block evaluation
SET cached = (SELECT data FROM cache WHERE updated_at > datetime('now', '-1 hour'));
SET response = CASE
WHEN $cached IS NULL
THEN sqlpage.fetch_with_meta(json_object('method', 'GET', 'url', 'https://api.example.com/data'))
END;
Result: sqlpage.fetch_with_meta() is called EVERY time, even when $cached IS NOT NULL.
Log evidence:
Fetching https://api.example.com/data with metadata ← fetch executed!
[API] cached=HIT, response=NULL ← but cache was HIT
SQLite's CASE expression does not guarantee short-circuit evaluation for functions with side effects.
Test 2: Subquery with WHERE — ❌ Does NOT block evaluation
SET response = (
SELECT sqlpage.fetch_with_meta(json_object('method', 'GET', 'url', 'https://api.example.com/data'))
FROM (SELECT 1) AS _dummy
WHERE $cached IS NULL
);
Result: sqlpage.fetch_with_meta() is STILL called, even when WHERE would return 0 rows.
Log evidence:
Fetching https://api.example.com/data with metadata ← fetch executed!
[API] cached=HIT, response=NULL ← but cache was HIT
SQLite's query planner evaluates scalar functions in SELECT regardless of the WHERE clause.
Test 3: localhost fallback — ✅ Works (but is a hack)
SET should_fetch = CASE WHEN $cached IS NULL THEN 1 ELSE 0 END;
SET request = CASE WHEN $should_fetch = 1 THEN
json_object('method', 'GET', 'url', 'https://api.example.com/data')
ELSE
json_object('method', 'HEAD', 'url', 'http://127.0.0.1:8080/style.css')
END;
SET response = sqlpage.fetch_with_meta($request);
Result: Works! When cache is HIT, the request goes to localhost instead of the external API. No external transfer, no API quota used.
Log evidence:
Fetching http://127.0.0.1:8080/style.css with metadata ← localhost ping
[API] should_fetch=0, status=200 — cache HIT ← no external call
But this is a hack — it makes 4 unnecessary localhost HTTP requests per page load (one per API: Google, Open-Meteo, IMGW, IAU).
Why this matters
- The localhost hack adds ~4 unnecessary HTTP round-trips per page load
- It pollutes logs with
Fetching http://127.0.0.1:8080/style.cssmessages - It requires a running web server to respond to the dummy requests
- It's conceptually wrong — we're making requests we don't need
Proposed solutions
Option A: New conditional fetch function
SET response = sqlpage.fetch_if($cached IS NULL, json_object(...));
Only executes sqlpage.fetch_with_meta() when the first argument is TRUE.
Option B: Lazy evaluation for SET
SET response = LAZY sqlpage.fetch_with_meta(...);
The function is only evaluated when $response is first accessed.
Option C: Built-in cache helper
SET response = sqlpage.cached_fetch(
cache_key => 'google_rating',
cache_ttl => '24 hours',
request => json_object('method', 'GET', 'url', '...')
);
sqlpage handles caching internally.
Environment
- sqlpage version: latest (v0.44.1)
- OS: Linux (Docker)
- 4 external APIs cached: Google Places, Open-Meteo, IMGW Hydro, IAU Meteors
Workaround (current)
We use the localhost fallback pattern for all 4 APIs. It works but is not ideal.
Thank you
sqlpage is amazing — this is the only issue we've encountered in production. Great work!
- Ngôn ngữ chính
- Rust
- Star
- 2.6k
- Fork
- 175
- Merge trung bình
- 14 giờ 16 phút
- Pull request đã merge (30 ngày)
- 43
Chuẩn bị môi trường
- Có Dockerfile hoặc tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của sqlpage/SQLPage
-
enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
sqlpage/SQLPage#1218 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
There is no convenient way to reproduce issuesCó thể đã có người làm @81reap đã nhận 26 ngày trước. Đang mởenhancement
sqlpage/SQLPage#1566 · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Confusing sort interfaceCó thể đã có người làm @lovasoa đã nhận 73 ngày trước. Đang mởbug
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 20/100
sqlpage/SQLPage#1442 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Mutation Testing Results :: Dead Code + Missing TestsCó thể làm lại được @81reap đã nhận 61 ngày trước và không có pull request nào đang mở. Đang mở
sqlpage/SQLPage#1396 · 5 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của sqlpage/SQLPage
Issue tương tự
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
symposium-dev/symposium#530 ·
Maintainer thường phản hồi trong vòng 5 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
topgrade-rs/topgrade#2540 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Invalid JSON options for ngram and edge_ngram silently use defaultsCó thể đã có người làm @Tilendlesa đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
noemaforge/vimanam#153 ·
Maintainer thường phản hồi trong vòng 1 ngày