Skip to content

fix(selfhost): translate SQLite julianday() for the Postgres dialect - #9838

Merged
loopover-orb[bot] merged 1 commit into
JSONbored:mainfrom
shin-core:fix/pg-dialect-julianday-9648
Jul 29, 2026
Merged

fix(selfhost): translate SQLite julianday() for the Postgres dialect#9838
loopover-orb[bot] merged 1 commit into
JSONbored:mainfrom
shin-core:fix/pg-dialect-julianday-9648

Conversation

@shin-core

Copy link
Copy Markdown
Contributor

What & why

translateFunctions (src/selfhost/pg-dialect.ts) rewrites date(), strftime(), datetime(), json_extract() and more for the self-host Postgres dialect — but not julianday(). So submitter-reputation.ts's avgMergeMs column,

(julianday(pr.merged_at) - julianday(pr.created_at)) * 86400000

reached Postgres as a function julianday(...) does not exist error, swallowed by the fail-safe read — the same class of silent self-host gap #8171 kept closing.

The fix

Map julianday(<expr>) to its Julian Day number:

(EXTRACT(EPOCH FROM (<expr>)::timestamptz) / 86400.0 + 2440587.5)
  • ::timestamptz matches the sibling date()/strftime() rules, so the TEXT ISO timestamps the app writes are read identically.
  • The + 2440587.5 offset cancels in the subtraction, so (julianday(a) - julianday(b)) * 86400000 still yields milliseconds exactly as before.

Drift guard

A new test scans every src/** SQL string literal (TS comments stripped first, so English prose like "…their total (…)" or a review.exclude_paths glob isn't mistaken for SQL) for the ten SQLite-only scalar functions — julianday, unixepoch, json_group_array, json_array_length, group_concat, printf, iif, glob, randomblob, total — and fails if any survives translateSql untranslated. A companion positive test asserts the detection helper flags a synthetic untranslated call (unixepoch(...)), so the guard is not trivially green.

Tests (test/unit/selfhost-pg-dialect.test.ts)

  • julianday() translates (single, whitespaced, and the exact avgMergeMs two-call expression → no julianday left, ms arithmetic preserved) — fails on main.
  • Regression: the listSubmitterCohortRows statement text has no remaining julianday after translateSqlfails on main.
  • The drift guard: no src/** SQL literal uses an untranslated SQLite-only function (flags submitter-reputation.ts: julianday on main), and its helper catches a synthetic unixepoch(.

Validation

  • npm run typecheck green; the pg-dialect suite green.
  • Diff coverage on pg-dialect.ts is 100% (the added rule).
  • git diff --check <base> HEAD clean; diff is two files, no schema/migration change.

Closes #9648

`translateFunctions` rewrote date()/strftime()/json_extract() for self-host
Postgres but not `julianday()`, so submitter-reputation.ts's avgMergeMs column
(`(julianday(merged_at) - julianday(created_at)) * 86400000`) reached Postgres as
a "function julianday does not exist" error, swallowed by the fail-safe read.

Add a rule mapping `julianday(<expr>)` to its Julian Day NUMBER,
`(EXTRACT(EPOCH FROM (<expr>)::timestamptz) / 86400.0 + 2440587.5)`, using
::timestamptz like the sibling date()/strftime() rules so the TEXT ISO timestamps
the app writes are read identically. The +2440587.5 offset cancels in the
subtraction, so the `(a - b) * 86400000` still yields milliseconds.

Add a drift guard: a test that scans every src/** SQL string literal (comments
stripped) for the ten SQLite-only scalar functions (julianday, unixepoch,
json_group_array, json_array_length, group_concat, printf, iif, glob, randomblob,
total) and fails if any survives translateSql untranslated — plus a positive test
that its detection helper flags a synthetic untranslated call, so the guard is not
trivially green.

Closes JSONbored#9648
@shin-core
shin-core requested a review from JSONbored as a code owner July 29, 2026 12:57
@loopover-orb

loopover-orb Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Warning

⏸️ LoopOver review result - manual review recommended

Review updated: 2026-07-29 13:02:50 UTC

2 files · 1 AI reviewer · no blockers · CI green · clean

⏸️ Suggested Action - Manual Review

Review summary
This PR adds a `julianday()` → Postgres translation rule in `translateFunctions` (pg-dialect.ts:79-82), converting it to a Julian Day number via `EXTRACT(EPOCH FROM (...)::timestamptz) / 86400.0 + 2440587.5`, which correctly preserves the millisecond arithmetic in `submitter-reputation.ts`'s `avgMergeMs` column since the added offset cancels in subtraction. The regex and surrounding style match the existing `date()`/`strftime()` translation rules, and the accompanying drift-guard test scans all `src/**` SQL literals for untranslated SQLite-only functions, which is a genuinely useful regression guard beyond the immediate fix. The Julian Day formula (unix epoch offset 2440587.5) is the standard correct constant, and the new tests cover the exact production expression from `submitter-reputation.ts`.

Nits — 3 non-blocking
  • The non-greedy `[^)]+?` capture in the new `julianday` regex (pg-dialect.ts:82) will mis-parse a nested-paren argument like `julianday(datetime('now'))`, same pre-existing limitation as the `date()`/`json_extract` single-level rules — worth a one-line comment noting the same caveat applies here, or confirm no such nested call exists in the codebase today.
  • The drift-guard's `SQLITE_ONLY_FUNCTIONS` list is a hardcoded snapshot; consider a short comment noting it must be updated whenever a new dialect rule is added, so the guard doesn't quietly go stale.
  • Consider asserting in a test that the julianday regex's non-greedy capture behaves reasonably (or is documented as unsupported) for a nested-call argument, mirroring the caveat already implicit in the `date()` rule.

Decision drivers

  • ✅ Code review — No blockers (1 reviewer)
  • ✅ Gate result — Passing (No configured blocker found.)
Context & advisory signals — never blocks the verdict
Signal Result Evidence
Linked issue ✅ Linked #9648
Related work ✅ No active overlap found No same-issue or scoped active PR overlap found.
Change scope ✅ 20/20 Low review scope from cached public metadata (1 linked issue).
Validation posture ❌ 5/25 Preflight is holding this PR: the review lane is unavailable, so it is not ready for automated review.
Contributor workload ✅ 10/10 Author activity: 71 registered-repo PR(s), 52 merged, 0 issue(s).
Contributor context ✅ Confirmed Gittensor contributor shin-core; Gittensor profile; 71 PR(s), 0 issue(s).
Improvement ✅ Minor risk: clean · value: minor · LLM: moderate
Linked issue satisfaction

Addressed
The diff adds the julianday() translation rule to translateFunctions matching the exact required Postgres expression and ::timestamptz pattern, and implements the drift guard as a vitest test scanning src/** string literals (with a companion test proving the detection helper flags an untranslated function), plus the required regression test for the avgMergeMs expression — covering all four listed

Review context
  • Author: shin-core
  • Role context: outside_contributor
  • Public audience mode: oss maintainer
  • Lane context: Repository is registered but has no active allocation in the current snapshot.
  • Public profile languages: not available
  • Official Gittensor activity: 71 PR(s), 0 issue(s).
  • PR-specific overlap: none found.
Contributor next steps
  • Start here: Await review-lane availability.
  • Then work through the remaining 1 step in the Signals table above.
Signal definitions
  • Related work = same linked issue, overlapping active PRs, or title/path similarity.
  • Change scope = cached public metadata such as size labels, draft state, and review-burden hints.
  • Validation posture = whether the PR provides enough public validation/test evidence for maintainer review.
  • Contributor workload = public contributor activity and cleanup pressure, not a repo-wide quality failure.
  • Contributor context = public GitHub/Gittensor identity context; non-Gittensor status is not a blocker.
🧪 Chat with LoopOver

Ask LoopOver a question about this PR directly in a comment — grounded only in the same cached, public-safe facts shown above, never a new claim.

  • @loopover ask &lt;question&gt; answers contribution-quality Q&A with source citations and freshness.
  • @loopover chat &lt;question&gt; answers in natural prose from cached decision-pack facts via local inference (maintainer/collaborator; read-only).
  • A plain-language @loopover mention with a real question is routed to the closest matching read-only command automatically — no exact syntax required.

Full command reference: https://loopover.ai/docs/loopover-commands

🧪 Experimental — new and may change.

🟩 Safe / merged · 🟦 Advisory · 🟨 Held for review · 🟥 Blocked / closed


💰 Earn for open-source contributions like this. Gittensor lets GitHub contributors earn for the work they already do — register to start earning →.

Checked by LoopOver, a quiet PR intelligence layer for OSS maintainers.

  • Re-run LoopOver review

@superagent-security

Copy link
Copy Markdown
Contributor

Superagent didn't find any vulnerabilities or security issues in this PR.

@loopover-orb loopover-orb Bot added the gittensor:bug Gittensor-scored bug fix — scores a 0.05x multiplier. label Jul 29, 2026
@codecov

codecov Bot commented Jul 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.06%. Comparing base (503eb0c) to head (64eff4d).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9838      +/-   ##
==========================================
+ Coverage   79.03%   79.06%   +0.02%     
==========================================
  Files         280      281       +1     
  Lines       58366    58445      +79     
  Branches     6697     6714      +17     
==========================================
+ Hits        46128    46207      +79     
  Misses      11955    11955              
  Partials      283      283              
Flag Coverage Δ
backend 100.00% <ø> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/selfhost/pg-dialect.ts 100.00% <ø> (ø)

@loopover-orb loopover-orb Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LoopOver approves — the gate is satisfied and CI is green.

@loopover-orb
loopover-orb Bot merged commit 7f6e42b into JSONbored:main Jul 29, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gittensor:bug Gittensor-scored bug fix — scores a 0.05x multiplier.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

selfhost(pg): julianday() is never translated

1 participant