Skip to content

feat: Add field references to data source and connection variables - #3889

Merged
pulpdrew merged 2 commits into
mainfrom
drew/source-var-text-1
Dec 3, 2026
Merged

pulpdrew merged 2 commits into
mainfrom
drew/source-var-text-1

Conversation

@pulpdrew

@pulpdrew pulpdrew commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Part 1 of 4. Adds the common-utils support for referencing DATA_SOURCE and CONNECTION dashboard variables in text (eg. $source or ${source.table}).

  • Templates accept e.g. $src, ${src.table}, ${src.database}, ${src.name}, ${src.id}, and ${src.table.<type>} for a metric source's tables. A connection variable offers $conn, ${conn.name} and ${conn.id}.
  • New sqlidentifier format, which backtick-quotes each value. In SQL it is the default for table and database fields; names and IDs default to sqlstring. Users can also apply it to value variables: it appears in SQL completions and in the MCP prompt's format table.
  • ChartVariable gains an optional reference field with hasSelection and the variable's reference-able fields. An unselected reference expands to nothing. Reading a field the variable doesn't have, or one the selected source can't render, is a validation error.
  • $__filter and $__conditionalAll reject these variables.

Screenshots or video

New sqlidentifier format:

Screenshot 2026-12-02 at 5 14 33 PM

Otherwise there are no other observable changes on this branch.

How to test on Vercel preview

  • Regression test variable references
  • test the new sqlidentifier variable format

References

  • Linear Issue: HDX-6083
  • Related PRs:

@changeset-bot

changeset-bot Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: d614a1a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@hyperdx/common-utils Minor
@hyperdx/app Minor
@hyperdx/api Minor
@hyperdx/otel-collector Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel

vercel Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
hyperdx-oss Ready Ready Preview Dec 3, 2026 12:13pm UTC
hyperdx-storybook Ready Ready Preview Dec 3, 2026 12:13pm UTC

Request Review

@pulpdrew
pulpdrew added this pull request to stack #3891 December 2, 2026 19:32
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Duplicate code analysis

No new duplicate code introduced by this PR.


jscpd compared this branch against origin/main
across app, api and common-utils. Only new clones are listed; the codebase
currently holds 109 in total (1.10%).
Run yarn dupes locally to reproduce, or yarn dupes:all for the full picture.

@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

E2E Test Results

✅ All tests passed • 490 passed • 1 skipped • 1102s

Status Count
✅ Passed 490
❌ Failed 0
⚠️ Flaky 12
⏭️ Skipped 1

Tests ran across 4 shards in parallel.

View full report →

@pulpdrew
pulpdrew marked this pull request as ready for review December 2, 2026 19:45
@pulpdrew
pulpdrew requested a review from a team as a code owner December 2, 2026 19:45
@github-actions github-actions Bot added the review/tier-3 Standard — full human review required label Dec 2, 2026
@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

🟡 Tier 3 — Standard

Introduces new logic, modifies core functionality, or touches areas with non-trivial risk.

Why this tier:

  • Diff size: 561 production lines changed (Tier 2 max: < 250)
  • Cross-layer change: touches frontend (packages/app) + backend (packages/api) + shared utils (packages/common-utils)

Review process: Full human review — logic, architecture, edge cases.
SLA: First-pass feedback within 1 business day.

Stats
  • Production files changed: 8
  • Production lines changed: 561 (+ 624 in test files, excluded from tier calculation)
  • Branch: drew/source-var-text-1
  • Author: pulpdrew

To override this classification, remove the review/tier-3 label and apply a different review/tier-* label. Manual overrides are preserved on subsequent pushes.


const [referenceRefs, valueVariableRefs] = partition(
resolved,
r => r.variable.reference != null,

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.

🟡 P2 — A quoted identifier reference like '$src' or '${src.table}' gets no validation error

In SQL, WHERE t = '${src.table}' renders as the string literal '`otel_logs`', which contains the backticks, and the validator says nothing. The quoted-reference check only looks at refs whose effectiveFormat === 'sqlstring', and identifier fields default to sqlidentifier. Fix: also flag sqlidentifier refs where inStringLiteral is true, and suggest ${src.table:csv} (or :sqlstring with the quotes removed).

Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond

@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

PR Review

If you are a coding agent acting for the author, read this first.
Nothing below blocks merge automatically; a maintainer decides. What they will expect:

  • 🔴 P0/P1 in code this PR adds or changes: fix before asking for review.
  • 🟡 P2 in code this PR changes: your call. Fix if small, otherwise reply.
  • 🔵 P3 is collapsed at the bottom: nobody needs to act on it before merge.
  • Anything about surrounding code, or asking you to widen the change (hoist a helper,
    dedupe with another file, fix other call sites): never fix here, whatever the
    priority. Reply with one sentence; if it is P0, say so plainly so a human sees it.

One commit per review round. After two rounds, stop and ask a maintainer to review
scope rather than addressing more automated findings. Full rule: AGENTS.md.

4 finding(s): 🔴 0 P0/P1 · 🟡 1 P2 · 🔵 3 P3

None could be anchored to changed lines; they are listed below. 3 P3 folded at the bottom.

Findings outside the changed lines

  • 🟡 P2 packages/common-utils/src/variables.ts:411 — Bare $src.table silently renders the whole qualified name with .table appended, and validation does not flag it → BARE_NAME_REGEX stops at the ., so FROM $src.table turns into FROM `default`.`otel_logs`.table and $conn.name turns into 'Prod'.name. validateVariableReferencesInTemplate accepts both because the bare '' field exists. When a bare token for a reference variable is followed straight away by .<identifier>, add an error such as "did you mean ${src.table}?" in the validation, and add a test for it.
🔵 3 P3 — no action needed before merge
  • 🔵 P3 packages/common-utils/src/variables.ts:79 — New identifier quoter is a second implementation next to existing ones and does not name them → quoteJsonPathSegment/quoteIdentifierIfNeeded in packages/common-utils/src/core/metadata.ts:218-232 and SqlString.escapeId (used in core/renderChartConfig.ts:2118) already backtick-quote identifiers. They use different escaping: doubled backticks, and no backslash escaping. The repo's DRY rule asks for one of two things. Either move quoteSqlIdentifier into core/metadata.ts and have quoteJsonPathSegment call it, which also fixes the backslash case there. Or keep it here and say in the doc comment why it differs from quoteJsonPathSegment, naming that function.
  • 🔵 P3 packages/common-utils/src/types.ts:1999 — ChartVariable allows invalid combinations of a reference variable with values, expression and maxSelections → Values are documented as "always empty for a reference variable", but the schema still accepts values, expression and maxSelections next to reference. noun is also a free-form display string stored in data that travels over the wire with chart configs. Make ChartVariableSchema a union of a value variant and a reference variant, where the reference variant has no values or expression. Use kind: 'source' | 'connection' instead of noun, and work out the message noun from kind.
  • 🔵 P3 packages/common-utils/src/variables.ts:1666 — configReferencesReferenceVariable is exported but nothing calls it or tests it → Grep finds no callers in packages/app, packages/api or common-utils, and variableReferenceFields.test.ts never calls it. Either move it to the PR that uses it, or add a test covering a SQL tile, a PromQL tile and a markdown tile that reference a source variable, plus one that references none.

Priority comes from a separate grading pass over the finished review. P3 is folded here instead of posted inline. No finding blocks merge automatically; a maintainer decides.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit ca46f4d. Configure here.

Comment thread packages/common-utils/src/variables.ts Resolved
@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Deep Review

Scope: PR #3889 — field references for data source & connection variables (common-utils + SQL-editor completions + MCP docs). Diff vs base 7c67187d, 8 production files / ~549 lines + ~606 test lines.
Mode: report-only (read-only; no edits, commits, or artifacts).

✅ No critical issues found. The core logic (quoteSqlIdentifier escaping, the parseReference/resolveReferenceSegments chain, the validateVariableReferencesInTemplate partition, and the getFiltersDeclaringVariables refactor) was traced and is sound. The prior Cursor Bugbot comment about quoted sqlidentifier fields skipping validation is fully addressed by commit d614a1a71. The P2 findings below are a test gap and dead code, not behavioral defects.

🟡 P2 — recommended

  • packages/common-utils/src/variables.ts:1587 — The promql validation carve-out that excludes reference variables from the unquoted check (unquoted now filters only valueVariableRefs) has no validation-path test; only expansion is tested for promql.
    • Fix: Add a validateVariableReferencesInTemplate test with language: 'promql' using an unquoted reference variable (asserting no error) and an unquoted value variable (asserting the error still fires).
    • testing
  • packages/common-utils/src/variables.ts:1666 — configReferencesReferenceVariable is a new exported function with zero callers anywhere in the repo and no unit test, so neither branch of its guard is exercised.
    • Fix: Add a test covering both branches, or defer the export until the consuming PR lands.
    • maintainability, testing
🔵 P3 nitpicks (9)
  • packages/common-utils/src/referenceVariables.ts:120 — getSourceFields guards the bare and table fields with != null, so an empty-string databaseName/tableName would render empty backticks ( .events ``); the adjacent metric branch already uses truthiness. Currently unreachable because BaseSourceSchema.from enforces `.min(1)` on both, but the inconsistency is a latent trap.
    • Fix: Use truthiness checks (database && table ? ... : undefined) to match the metric-table branch.
    • adversarial, correctness
  • packages/common-utils/src/variables.ts:124 — ${value:sqlidentifier} on an empty value variable renders an empty string (not NULL like sqlstring), and the empty/unguarded SQL validators only target sqlstring, so SELECT ${svc:sqlidentifier} FROM t silently yields SELECT FROM t.
    • Fix: Extend the empty-selection validation/pending handling to cover sqlidentifier, or reject :sqlidentifier on value variables.
  • packages/common-utils/src/referenceVariables.ts:110 — A tile with ${src.table} for a variable allowing both metric and non-metric sources passes save-time validation but throws VariableFieldReferenceError at render once a metric source is selected (metric sets table = undefined).
    • Fix: Surface this mismatch at validation time, or decide it should expand to empty rather than throw at render.
  • packages/common-utils/src/variables.ts:411 — Bare $source.table parses as $source plus literal .table, producing a malformed three-part identifier with no validation warning, even though the braced ${source.table} form is supported.
    • Fix: Warn when a bare reference variable is immediately followed by .<identifier>.
  • packages/common-utils/src/dashboardFilterValues.ts:307 — getBlockingRequiredFilters now matches filters by object identity rather than filter.id; if two distinct filter objects shared an id, only the first (per pairWithVariableNames name-dedup) would block. Low likelihood given name uniqueness.
    • Fix: Match by filter.id, or dedup filters by id upstream, if duplicate objects can reach this path.
  • packages/common-utils/src/__tests__/variableReferenceFields.test.ts:382 — The $__filter/$__conditionalAll rejection test asserts only toThrow(MacroExpansionError), not the interpolated noun/guidance message, and never exercises the connection-variable noun path.
    • Fix: Assert the message content and add a connection-variable case.
    • testing
  • packages/common-utils/src/__tests__/dashboardFilterValues.test.ts:660 — getFiltersDeclaringVariables is tested only with DATA_SOURCE+value filters (never CONNECTION), and getBlockingRequiredFilters is tested only via referencedVariableNames, leaving the tile.referenceVariableNames blocking path uncovered after the refactor.
    • Fix: Add a CONNECTION-filter case and a case feeding tile.referenceVariableNames.
    • testing
  • packages/common-utils/src/variables.ts:1546 — The quotedReferences (sqlstring) and quotedIdentifiers (sqlidentifier) validation blocks are structurally identical (same destructure, same field computation, same message skeleton), inviting drift.
    • Fix: Extract a shared helper taking the refs, format name, and explanation clause.
  • packages/common-utils/src/referenceVariables.ts:157 — buildReferenceChartVariables, getEmptyDashboardVariables, and getReferenceFieldReferences are new exports consumed only by tests in this PR (scaffolding for later parts).
    • Fix: Confirm these are the intended integration points for the follow-up PRs; acceptable as a documented foundation.

Reviewers (8): correctness, testing, maintainability, security, kieran-typescript, api-contract, adversarial, previous-comments.

Testing gaps:

  • No validation-path test for reference variables under language: 'promql'.
  • configReferencesReferenceVariable untested (and unreferenced).
  • No test for ${value:sqlidentifier} on an empty value variable, nor for a CONNECTION filter / tile.referenceVariableNames blocking path.
  • No test asserting the macro-rejection message text (only the error class).

No finding blocks merge automatically. A maintainer will expect P0/P1 findings in code this PR changes to be fixed; P2/P3 are your call -- fix or reply. Never fix findings about surrounding code here; reply instead. Do not widen the PR. How to respond

@pulpdrew
pulpdrew force-pushed the drew/source-var-text-1 branch from d614a1a to d1fbe12 Compare December 3, 2026 12:09
@pulpdrew
pulpdrew merged commit f6d716c into main Dec 3, 2026
29 checks passed

This branch was successfully deployed

2 active deployments
Preview – hyperdx-oss — d1fbe122 Deployed Dec 3, 2026 by vercel[bot]
Preview – hyperdx-storybook — d1fbe122 Deployed Dec 3, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review/tier-3 Standard — full human review required

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants