diff --git a/nodejs/__test__/nested_blob.test.ts b/nodejs/__test__/nested_blob.test.ts index 508b57c77..cc5d2e37a 100644 --- a/nodejs/__test__/nested_blob.test.ts +++ b/nodejs/__test__/nested_blob.test.ts @@ -210,6 +210,77 @@ describe('nested blobs with blobMode "bytes"', () => { }, ); + it("reads quoted SQL column references as blob bytes", async () => { + const query = table.query().where("id = 1").select({ + coverBytes: "`cover`", + imageBytes: "info.image", + details: "`info`", + literal: '"cover"', + caption: "info.caption", + }); + const [description] = await query.toArray({ blobMode: "descriptions" }); + expect(Number(description.coverBytes.size)).toBe(5); + expect(Number(description.imageBytes.size)).toBe(5); + + const [row] = await query.toArray({ blobMode: "bytes" }); + expect(row.coverBytes).toBeInstanceOf(Uint8Array); + expect(Buffer.from(row.coverBytes).toString()).toBe("cover"); + expect(Buffer.from(row.imageBytes).toString()).toBe("hello"); + expect(Buffer.from(row.details.image).toString()).toBe("hello"); + expect(row.caption).toBe("first"); + expect(row.literal).toBe("cover"); + }); + + it.each(["info.`image`", "`info`.image", "`info`.`image`"])( + "reads a nested Blob projected with %s", + async (source) => { + const query = table + .query() + .where("id = 1") + .select({ "payload.image": source, literal: '"info.image"' }); + const [description] = await query.toArray({ blobMode: "descriptions" }); + expect(Number(description["payload.image"].size)).toBe(5); + + const [row] = await query.toArray({ blobMode: "bytes" }); + expect(row["payload.image"]).toBeInstanceOf(Uint8Array); + expect(Buffer.from(row["payload.image"])).toEqual(Buffer.from("hello")); + expect(row.literal).toBe("info.image"); + }, + ); + + it("leaves a quoted component containing a dot as a descriptor", async () => { + const db = await connect(tmpDir.name); + const schema = new Schema([ + new Field( + "info", + new Struct([ + blob("image.ext"), + new Field("image", new Struct([blob("ext")]), true), + ]), + true, + ), + ]); + const dotted = await db.createTable( + "dotted_blobs", + [ + { + info: { + "image.ext": Buffer.from("literal"), + image: { ext: Buffer.from("nested") }, + }, + }, + ], + { schema }, + ); + const [row] = await dotted + .query() + .select({ literal: "info.`image.ext`", nested: "info.`image`.ext" }) + .toArray({ blobMode: "bytes" }); + + expect(Number(row.literal.size)).toBe(7); + expect(Buffer.from(row.nested).toString()).toBe("nested"); + }); + it("materializes dotted aliases independently of nested fields", async () => { const [row] = await table .query() diff --git a/nodejs/lancedb/query.ts b/nodejs/lancedb/query.ts index a309eb359..8cf06999e 100644 --- a/nodejs/lancedb/query.ts +++ b/nodejs/lancedb/query.ts @@ -82,6 +82,28 @@ interface BlobSource { source: string; } +// Normalize direct SQL column references for Blob lookup only. Backtick +// quoting is applied per identifier, and string literals are not column names. +const SQL_COLUMN_COMPONENT = + /`(?:[^`]|``)*`|[\p{ID_Start}_][\p{ID_Continue}$]*/gu; +const SQL_COLUMN_PATH = new RegExp( + `^(?:${SQL_COLUMN_COMPONENT.source})(?:\\s*\\.\\s*(?:${SQL_COLUMN_COMPONENT.source}))*$`, + "u", +); + +function blobProjectionSource(sql: string): string { + const column = sql.trim(); + if (!column.includes("`") || !SQL_COLUMN_PATH.test(column)) { + return sql; + } + + const parts = Array.from(column.matchAll(SQL_COLUMN_COMPONENT), ([part]) => + part.startsWith("`") ? part.slice(1, -1).replaceAll("``", "`") : part, + ); + // The Blob lookup uses dots as separators and cannot distinguish literal dots. + return parts.some((part) => part.includes(".")) ? sql : parts.join("."); +} + function blobSources( blobColumns: string[], projection: [string, string][] | undefined, @@ -482,7 +504,7 @@ export class QueryBase< sources.push([name, value.inner.columnName() ?? sql]); } else if (typeof value === "string") { columnTuples.push([name, value]); - sources.push([name, value]); + sources.push([name, blobProjectionSource(value)]); } else { throw new TypeError( `select() values must be SQL strings or Expr objects, got ${typeof value} for "${name}"`,