Skip to content

Optimize surql builder#122

Open
aguspdana wants to merge 1 commit intomainfrom
agus/query-builder
Open

Optimize surql builder#122
aguspdana wants to merge 1 commit intomainfrom
agus/query-builder

Conversation

@aguspdana
Copy link
Collaborator

@aguspdana aguspdana commented Feb 27, 2026

Important

Optimize SurQL builder by refining projections, filters, and logging, and update tests accordingly.

  • Behavior:
    • Add logging for batchedMutation and errors in runSURQLMutation in run.ts.
    • Add logging for batchedQuery in query.ts.
  • Projections:
    • Modify buildProjection in buildLogical.ts to handle constant and computed fields by skipping them.
    • Update buildSimpleFieldProjection to return undefined for constant and computed fields.
    • Add resultCardinality and fieldCardinality to ProjectionField types in logical.ts.
  • Filters:
    • Add cardinality and oppositeCardinality to filters in buildLogical.ts and optimize.ts.
    • Prioritize future_bi_ref and bi_ref filters in optimizeSource in optimize.ts.
  • Misc:
    • Update processResults.ts to convert DateTime to Date in transformResultObject.
    • Fix test expectations in basic.ts and errors.ts to match new error messages.

This description was created by Ellipsis for 24a59da. You can customize this summary. It will automatically update as commits are pushed.

Summary by cubic

Optimized the SURQL query builder by introducing precise ref types and cardinality handling to generate faster, more accurate queries. Added helpful logging and result normalization.

  • Performance

    • Split ref types into ref/future_ref and nested_ref/nested_future_ref with resultCardinality vs fieldCardinality, used across logical, builder, and optimizer.
    • Added biref/future_biref filters and prioritized traversal conversion to pick faster relationship paths.
    • Smarter ref projections: correct FROM path and array::first based on cardinality; flex refs supported.
  • Bug Fixes

    • Skip constant/computed fields in projections.
    • Normalize results: convert SurrealDB DateTime to JS Date; treat empty ref arrays as null.
    • Log batched queries/mutations on errors; updated tests to reflect new error messages.

Written for commit 24a59da. Summary will update on new commits.

Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

Important

Looks good to me! 👍

Reviewed everything up to 24a59da in 14 seconds. Click for details.
  • Reviewed 736 lines of code in 9 files
  • Skipped 0 files when reviewing.
  • Skipped posting 0 draft comments. View those below.
  • Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.

Workflow ID: wflow_NMbcbrdfwbsZUCBq

You can customize Ellipsis by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

2 issues found across 9 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="src/stateMachine/query/surql2/buildSurql.ts">

<violation number="1" location="src/stateMachine/query/surql2/buildSurql.ts:96">
P2: FutureRefField with fieldCardinality === 'ONE' still uses `$this.<path>[*]`, which treats single-valued link fields as arrays and changes the query shape. This should be keyed only on fieldCardinality, not on `field.type`.</violation>

<violation number="2" location="src/stateMachine/query/surql2/buildSurql.ts:114">
P2: NestedFutureRefField with fieldCardinality === 'ONE' still uses `$this.<path>[*]`, treating single-valued nested link fields as arrays. Align this condition with fieldCardinality so ONE uses `$this.<path>` regardless of nested_ref vs nested_future_ref.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

lines.push(buildProjection(field.projection, level + 1, mutParams));
const filter = field.filter ? buildFilter(field.filter, mutParams) : undefined;
lines.push(indent(`FROM $this.${esc(field.path)}[*]`, level + 1));
if (field.fieldCardinality === 'ONE' && field.type === 'nested_ref') {
Copy link

@cubic-dev-ai cubic-dev-ai bot Feb 27, 2026

Choose a reason for hiding this comment

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

P2: NestedFutureRefField with fieldCardinality === 'ONE' still uses $this.<path>[*], treating single-valued nested link fields as arrays. Align this condition with fieldCardinality so ONE uses $this.<path> regardless of nested_ref vs nested_future_ref.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/stateMachine/query/surql2/buildSurql.ts, line 114:

<comment>NestedFutureRefField with fieldCardinality === 'ONE' still uses `$this.<path>[*]`, treating single-valued nested link fields as arrays. Align this condition with fieldCardinality so ONE uses `$this.<path>` regardless of nested_ref vs nested_future_ref.</comment>

<file context>
@@ -87,26 +89,33 @@ const buildDataFieldProjection = (field: DataField, level: number) => {
   lines.push(buildProjection(field.projection, level + 1, mutParams));
   const filter = field.filter ? buildFilter(field.filter, mutParams) : undefined;
-  lines.push(indent(`FROM $this.${esc(field.path)}[*]`, level + 1));
+  if (field.fieldCardinality === 'ONE' && field.type === 'nested_ref') {
+    lines.push(indent(`FROM $this.${esc(field.path)}`, level + 1));
+  } else {
</file context>
Suggested change
if (field.fieldCardinality === 'ONE' && field.type === 'nested_ref') {
if (field.fieldCardinality === 'ONE') {
Fix with Cubic

Comment on lines +96 to +98
const subQuery = field.fieldCardinality === 'ONE' && field.type === 'ref'
? `SELECT VALUE record::id(id) FROM $this.${escapedPath}`
: `SELECT VALUE record::id(id) FROM $this.${escapedPath}[*]`;
Copy link

@cubic-dev-ai cubic-dev-ai bot Feb 27, 2026

Choose a reason for hiding this comment

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

P2: FutureRefField with fieldCardinality === 'ONE' still uses $this.<path>[*], which treats single-valued link fields as arrays and changes the query shape. This should be keyed only on fieldCardinality, not on field.type.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/stateMachine/query/surql2/buildSurql.ts, line 96:

<comment>FutureRefField with fieldCardinality === 'ONE' still uses `$this.<path>[*]`, which treats single-valued link fields as arrays and changes the query shape. This should be keyed only on fieldCardinality, not on `field.type`.</comment>

<file context>
@@ -87,26 +89,33 @@ const buildDataFieldProjection = (field: DataField, level: number) => {
   const escapedAlias = esc(alias || path);
-  if (cardinality === 'ONE') {
-    return indent(`array::first(SELECT VALUE record::id(id) FROM $this.${escapedPath}[*]) AS ${escapedAlias}`, level);
+  const subQuery = field.fieldCardinality === 'ONE' && field.type === 'ref'
+    ? `SELECT VALUE record::id(id) FROM $this.${escapedPath}`
+    : `SELECT VALUE record::id(id) FROM $this.${escapedPath}[*]`;
</file context>
Suggested change
const subQuery = field.fieldCardinality === 'ONE' && field.type === 'ref'
? `SELECT VALUE record::id(id) FROM $this.${escapedPath}`
: `SELECT VALUE record::id(id) FROM $this.${escapedPath}[*]`;
const subQuery = field.fieldCardinality === 'ONE'
? `SELECT VALUE record::id(id) FROM $this.${escapedPath}`
: `SELECT VALUE record::id(id) FROM $this.${escapedPath}[*]`;
Fix with Cubic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant