mirror of
https://github.com/lancedb/lancedb.git
synced 2026-12-03 07:45:17 +00:00
fix(node): materialize blobs selected with quoted SQL names (#5286)
Fixes #5285. ## Summary - Decode a backtick-quoted SQL column name for the Blob bytes lookup. Continue passing the original SQL through to query execution. - Cover direct Blob aliases and a quoted struct containing a nested Blob, alongside unchanged unquoted projections and SQL string literals. - Preserve the existing descriptor output in Embedded queries when a quoted identifier itself contains a dot. A separate projection of the actual nested Blob still returns its bytes. ## Testing - Reproduced the descriptor output with the published Node package and saw the new native Embedded regression fail on main. - 206 tests passed across the native nested Blob, Blob, expression, and query suites. Independently reran the new regression. - TypeScript build, targeted Biome checks, and `git diff --check` passed. Tests use the release-matched native module. AI assistance was used to identify, implement, and review the change. --------- Co-authored-by: Divyam Talwar <152021174+DivyamTalwar@users.noreply.github.com> Co-authored-by: Divyam Talwar <194859478+DivyamTalwar@users.noreply.github.com>
This commit is contained in:
co-authored by
Divyam Talwar
Divyam Talwar
parent
c3f005f7ff
commit
3129573909
@@ -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()
|
||||
|
||||
+23
-1
@@ -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}"`,
|
||||
|
||||
Reference in New Issue
Block a user