From 192313d33f4ae0aea6b5b1bfd7ca32ac855ba3a3 Mon Sep 17 00:00:00 2001 From: Loc Nguyen <113083148+thailoc-dev@users.noreply.github.com> Date: Thu, 1 Oct 2026 22:30:31 +0700 Subject: [PATCH] fix(mongodb): complete setwindowfields densify and fill bodies --- apps/desktop/src/lib/mongo/mongoCompletion.ts | 57 +++++++++++++ .../src/lib/mongo/mongoCompletionTables.ts | 72 ++++++++++++++++- packages/app-tests/mongoCompletion.test.ts | 81 +++++++++++++++++++ 3 files changed, 209 insertions(+), 1 deletion(-) diff --git a/apps/desktop/src/lib/mongo/mongoCompletion.ts b/apps/desktop/src/lib/mongo/mongoCompletion.ts index 83cb560b22..22eaa23073 100644 --- a/apps/desktop/src/lib/mongo/mongoCompletion.ts +++ b/apps/desktop/src/lib/mongo/mongoCompletion.ts @@ -17,6 +17,7 @@ import { TOP_LEVEL_QUERY_OPERATORS, UPDATE_OPERATORS, VALUE_SNIPPETS, + WINDOW_FUNCTION_OPERATORS, mongoOperatorItemType, type MongoOperatorSpec, } from "@/lib/mongo/mongoCompletionTables"; @@ -51,6 +52,7 @@ export type MongoCompletionMode = | "pushModifier" | "expression" | "accumulator" + | "windowOperator" | "stage" | "stageOption" | "methodOption" @@ -318,6 +320,8 @@ const STAGE_OPTION_VALUE_MODES: Record(); + const deduped: MongoOperatorSpec[] = []; + for (const list of lists) { + for (const spec of list) { + if (seen.has(spec.label)) continue; + seen.add(spec.label); + deduped.push(spec); + } + } + return deduped; +} + +export const WINDOW_FUNCTION_OPERATORS: MongoOperatorSpec[] = [...dedupeSpecsByLabel(WINDOW_OPERATORS, ACCUMULATORS), ...specs([WINDOW_SPEC])]; + export const EXPRESSION_OPERATORS: MongoOperatorSpec[] = specs([ // Conditional ["$cond", "Returns one of two values from a condition", "$cond: { if: ${}, then: ${}, else: ${} }"], @@ -416,6 +447,15 @@ export const OPERATOR_SUB_KEYS: Record = { ["backwards", "Compare secondary differences from the end", "backwards: false"], ["normalization", "Normalize text to Unicode NFD first", "normalization: false"], ]), + range: specs([ + ["step", "Step size to densify by", "step: 1"], + ["unit", "Time unit for date densification", 'unit: "${unit}"'], + ["bounds", "Boundary condition for the range", 'bounds: "${full}"'], + ]), + fillOutput: specs([ + ["value", "Value or expression to fill with", "value: ${}"], + ["method", "Fill method", 'method: "${linear}"'], + ]), }; const BSON_TYPE_ALIASES: Spec[] = [ @@ -510,6 +550,25 @@ export const ENUM_VALUES: Record = { ["4", "Also punctuation, with alternate: shifted", "4"], ["5", "Identical, including code points", "5"], ]), + unit: specs([ + ["millisecond", "Densify by milliseconds", '"millisecond"'], + ["second", "Densify by seconds", '"second"'], + ["minute", "Densify by minutes", '"minute"'], + ["hour", "Densify by hours", '"hour"'], + ["day", "Densify by days", '"day"'], + ["week", "Densify by weeks", '"week"'], + ["month", "Densify by months", '"month"'], + ["quarter", "Densify by quarters", '"quarter"'], + ["year", "Densify by years", '"year"'], + ]), + bounds: specs([ + ["full", "Densify across entire range", '"full"'], + ["partition", "Densify within each partition", '"partition"'], + ]), + fillMethod: specs([ + ["linear", "Linear interpolation between surrounding values", '"linear"'], + ["locf", "Last observation carried forward", '"locf"'], + ]), }; export const BULK_WRITE_OPERATIONS: MongoOperatorSpec[] = specs([ @@ -710,6 +769,17 @@ export const STAGE_OPTION_KEYS: Record = { ["includeLocs", "Field holding the matched location", 'includeLocs: "${location}"'], ]), $replaceRoot: specs([["newRoot", "Expression producing the new root document", 'newRoot: "$${field}"']]), + $densify: specs([ + ["field", "Field whose values will be densified", 'field: "${field}"'], + ["partitionByFields", "Fields that partition the documents", 'partitionByFields: ["${field}"]'], + ["range", "Range specification to densify", 'range: { step: 1, bounds: "${full}" }'], + ]), + $fill: specs([ + ["partitionBy", "Expression to partition by", 'partitionBy: "$${field}"'], + ["partitionByFields", "Fields that partition the documents", 'partitionByFields: ["${field}"]'], + ["sortBy", "Sort order for evaluating values", "sortBy: { ${field}: 1 }"], + ["output", "Fields to fill", "output: { ${field}: { value: ${} } }"], + ]), }; /** Literal values that are worth completing in value position. */ diff --git a/packages/app-tests/mongoCompletion.test.ts b/packages/app-tests/mongoCompletion.test.ts index 9c2430c270..ac94fdfdc2 100644 --- a/packages/app-tests/mongoCompletion.test.ts +++ b/packages/app-tests/mongoCompletion.test.ts @@ -17,6 +17,7 @@ import { STAGE_OPTION_KEYS, UPDATE_OPERATORS, VALUE_SNIPPETS, + WINDOW_OPERATORS, } from "../../apps/desktop/src/lib/mongo/mongoCompletionTables.ts"; import { parseMongoCommand } from "../../apps/desktop/src/lib/mongo/mongoShellCommand.ts"; @@ -1098,6 +1099,8 @@ test("every suggested sub-document key and enumerated value parses in the positi collation: (body) => `db.users.find({}).collation({ ${body} })`, timeseries: (body) => `db.createCollection("x", { timeseries: { ${body} } })`, clusteredIndex: (body) => `db.createCollection("x", { clusteredIndex: { ${body} } })`, + range: (body) => `db.users.aggregate([{ $densify: { field: "t", range: { ${body} } } }])`, + fillOutput: (body) => `db.users.aggregate([{ $fill: { output: { score: { ${body} } } } }])`, }; for (const [operator, keys] of Object.entries(OPERATOR_SUB_KEYS)) { const build = keyCommands[operator]; @@ -1123,6 +1126,9 @@ test("every suggested sub-document key and enumerated value parses in the positi validationLevel: (value) => `db.createCollection("x", { validationLevel: ${value} })`, validationAction: (value) => `db.createCollection("x", { validationAction: ${value} })`, granularity: (value) => `db.createCollection("x", { timeseries: { timeField: "t", granularity: ${value} } })`, + unit: (value) => `db.users.aggregate([{ $densify: { field: "t", range: { step: 1, unit: ${value} } } }])`, + bounds: (value) => `db.users.aggregate([{ $densify: { field: "t", range: { step: 1, bounds: ${value} } } }])`, + fillMethod: (value) => `db.users.aggregate([{ $fill: { output: { score: { method: ${value} } } } }])`, }; for (const [enumKey, values] of Object.entries(ENUM_VALUES)) { const build = valueCommands[enumKey]; @@ -1221,6 +1227,7 @@ test("snippet templates use placeholder syntax CodeMirror actually honours", () ...PUSH_MODIFIERS, ...PIPELINE_STAGES, ...ACCUMULATORS, + ...WINDOW_OPERATORS, ...EXPRESSION_OPERATORS, ...VALUE_SNIPPETS, ...EXTENDED_JSON_VALUES, @@ -1279,6 +1286,80 @@ test("infers dotted MongoDB fields from sampled documents", () => { assert.ok(inferred.find((field) => field.name === "tags.label" && field.type === "string")); }); +test("completes $setWindowFields stage options, sortBy key map, and output window operators", () => { + assert.deepEqual(labels("db.users.aggregate([{ $setWindowFields: { "), ["output", "partitionBy", "sortBy"]); + assert.deepEqual(labels("db.users.aggregate([{ $setWindowFields: { partitionBy: '", { fields }), ["$_id", "$createdAt", "$name", "$profile.email"]); + assert.deepEqual(labels("db.users.aggregate([{ $setWindowFields: { sortBy: { ", { fields }), ["_id", "createdAt", "name", "profile.email"]); + assert.deepEqual(labels("db.users.aggregate([{ $setWindowFields: { sortBy: { name: ", { fields }), ["-1", "1"]); + assert.deepEqual(labels("db.users.aggregate([{ $setWindowFields: { output: { ", { fields }), ["_id", "createdAt", "name", "profile.email"]); + + const windowOps = labels("db.users.aggregate([{ $setWindowFields: { output: { r: { $"); + const expectedOperators = [ + "$rank", + "$denseRank", + "$documentNumber", + "$shift", + "$expMovingAvg", + "$derivative", + "$integral", + "$covariancePop", + "$covarianceSamp", + "$locf", + "$linearFill", + "$sum", + "$avg", + "$min", + "$max", + "$count", + "$first", + "$last", + "$push", + "$addToSet", + "$stdDevPop", + "$stdDevSamp", + "$top", + "$bottom", + "$topN", + "$bottomN", + "$firstN", + "$lastN", + "$maxN", + "$minN", + "$median", + "$percentile", + ]; + for (const op of expectedOperators) { + assert.ok(windowOps.includes(op), op); + } + assert.equal(windowOps.includes("window"), false); + + const bareOutputBody = labels("db.users.aggregate([{ $setWindowFields: { output: { r: { "); + assert.ok(bareOutputBody.includes("window")); + assert.ok(bareOutputBody.includes("$rank")); + + const windowPrefix = labels("db.users.aggregate([{ $setWindowFields: { output: { r: { win"); + assert.ok(windowPrefix.includes("window")); +}); + +test("completes $densify and $fill stage options and value modes", () => { + assert.deepEqual(labels("db.users.aggregate([{ $densify: { "), ["field", "partitionByFields", "range"]); + assert.deepEqual(labels("db.users.aggregate([{ $densify: { field: '", { fields }), ["_id", "createdAt", "name", "profile.email"]); + assert.deepEqual(labels("db.users.aggregate([{ $densify: { partitionByFields: ['", { fields }), ["_id", "createdAt", "name", "profile.email"]); + assert.deepEqual(labels("db.users.aggregate([{ $densify: { range: { "), ["bounds", "step", "unit"]); + assert.ok(labels("db.users.aggregate([{ $densify: { range: { unit: '").includes("hour")); + assert.ok(labels("db.users.aggregate([{ $densify: { range: { bounds: '").includes("full")); + + assert.deepEqual(labels("db.users.aggregate([{ $fill: { "), ["output", "partitionBy", "partitionByFields", "sortBy"]); + assert.deepEqual(labels("db.users.aggregate([{ $fill: { partitionBy: '", { fields }), ["$_id", "$createdAt", "$name", "$profile.email"]); + assert.deepEqual(labels("db.users.aggregate([{ $fill: { partitionByFields: ['", { fields }), ["_id", "createdAt", "name", "profile.email"]); + assert.deepEqual(labels("db.users.aggregate([{ $fill: { sortBy: { ", { fields }), ["_id", "createdAt", "name", "profile.email"]); + assert.deepEqual(labels("db.users.aggregate([{ $fill: { sortBy: { name: ", { fields }), ["-1", "1"]); + assert.deepEqual(labels("db.users.aggregate([{ $fill: { output: { ", { fields }), ["_id", "createdAt", "name", "profile.email"]); + assert.deepEqual(labels("db.users.aggregate([{ $fill: { output: { amount: { "), ["method", "value"]); + assert.deepEqual(labels("db.users.aggregate([{ $fill: { output: { amount: { method: '"), ["linear", "locf"]); + assert.deepEqual(labels("db.users.aggregate([{ $fill: { output: { amount: { value: '", { fields }), ["$_id", "$createdAt", "$name", "$profile.email"]); +}); + test("completes joined-collection fields inside $lookup, $graphLookup and $unionWith", () => { // $lookup foreignField targets joined collection const lookupForeign = getMongoCompletionContext('db.users.aggregate([{ $lookup: { from: "orders", localField: "name", foreignField: "', 'db.users.aggregate([{ $lookup: { from: "orders", localField: "name", foreignField: "'.length);