Skip to content

Commit 0af4891

Browse files
committed
fix(tables): type-aware SQL casts for range filters on date columns
1 parent 0c1167d commit 0af4891

10 files changed

Lines changed: 425 additions & 280 deletions

File tree

apps/sim/app/api/table/[tableId]/export/route.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ export const GET = withRouteHandler(async (request: NextRequest, { params }: Rou
6262
let firstJsonRow = true
6363
while (true) {
6464
const result = await queryRows(
65-
tableId,
66-
table.workspaceId,
65+
table,
6766
{ limit: EXPORT_BATCH_SIZE, offset, includeTotal: false },
6867
requestId
6968
)

apps/sim/app/api/table/[tableId]/rows/route.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,14 @@ export const GET = withRouteHandler(
266266
eq(userTableRows.workspaceId, validated.workspaceId),
267267
]
268268

269+
const schema = table.schema as TableSchema
270+
269271
if (validated.filter) {
270-
const filterClause = buildFilterClause(validated.filter as Filter, USER_TABLE_ROWS_SQL_NAME)
272+
const filterClause = buildFilterClause(
273+
validated.filter as Filter,
274+
USER_TABLE_ROWS_SQL_NAME,
275+
schema.columns
276+
)
271277
if (filterClause) {
272278
baseConditions.push(filterClause)
273279
}
@@ -286,7 +292,6 @@ export const GET = withRouteHandler(
286292
.where(and(...baseConditions))
287293

288294
if (validated.sort) {
289-
const schema = table.schema as TableSchema
290295
const sortClause = buildSortClause(validated.sort, USER_TABLE_ROWS_SQL_NAME, schema.columns)
291296
if (sortClause) {
292297
query = query.orderBy(sortClause) as typeof query
@@ -388,14 +393,14 @@ export const PUT = withRouteHandler(
388393
}
389394

390395
const result = await updateRowsByFilter(
396+
table,
391397
{
392398
tableId,
393399
filter: validated.filter as Filter,
394400
data: validated.data as RowData,
395401
limit: validated.limit,
396402
workspaceId: validated.workspaceId,
397403
},
398-
table,
399404
requestId
400405
)
401406

@@ -503,6 +508,7 @@ export const DELETE = withRouteHandler(
503508
}
504509

505510
const result = await deleteRowsByFilter(
511+
table,
506512
{
507513
tableId,
508514
filter: validated.filter as Filter,

apps/sim/app/api/v1/tables/[tableId]/rows/route.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,14 @@ export const GET = withRouteHandler(async (request: NextRequest, context: TableR
158158
eq(userTableRows.workspaceId, validated.workspaceId),
159159
]
160160

161+
const schema = table.schema as TableSchema
162+
161163
if (validated.filter) {
162-
const filterClause = buildFilterClause(validated.filter as Filter, USER_TABLE_ROWS_SQL_NAME)
164+
const filterClause = buildFilterClause(
165+
validated.filter as Filter,
166+
USER_TABLE_ROWS_SQL_NAME,
167+
schema.columns
168+
)
163169
if (filterClause) {
164170
baseConditions.push(filterClause)
165171
}
@@ -177,7 +183,6 @@ export const GET = withRouteHandler(async (request: NextRequest, context: TableR
177183
.where(and(...baseConditions))
178184

179185
if (validated.sort) {
180-
const schema = table.schema as TableSchema
181186
const sortClause = buildSortClause(validated.sort, USER_TABLE_ROWS_SQL_NAME, schema.columns)
182187
if (sortClause) {
183188
query = query.orderBy(sortClause) as typeof query
@@ -378,14 +383,14 @@ export const PUT = withRouteHandler(async (request: NextRequest, context: TableR
378383
}
379384

380385
const result = await updateRowsByFilter(
386+
table,
381387
{
382388
tableId,
383389
filter: validated.filter as Filter,
384390
data: validated.data as RowData,
385391
limit: validated.limit,
386392
workspaceId: validated.workspaceId,
387393
},
388-
table,
389394
requestId
390395
)
391396

@@ -484,6 +489,7 @@ export const DELETE = withRouteHandler(
484489
}
485490

486491
const result = await deleteRowsByFilter(
492+
table,
487493
{
488494
tableId,
489495
filter: validated.filter as Filter,

apps/sim/lib/copilot/tools/handlers/function-execute.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ async function resolveInputFiles(
6262
for (const tableId of inputTables) {
6363
if (typeof tableId !== 'string') continue
6464
const table = await getTableById(tableId)
65-
if (!table) {
65+
if (!table || table.workspaceId !== workspaceId) {
6666
logger.warn('Input table not found', { tableId })
6767
continue
6868
}
69-
const rows = await queryRows(tableId, workspaceId, {}, 'copilot-fn-exec')
69+
const rows = await queryRows(table, {}, 'copilot-fn-exec')
7070
if (!rows.rows?.length) continue
7171

7272
const allKeys = new Set<string>()

apps/sim/lib/copilot/tools/server/table/user-table.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -474,10 +474,14 @@ export const userTableServerTool: BaseServerTool<UserTableArgs, UserTableResult>
474474
return { success: false, message: 'Workspace ID is required' }
475475
}
476476

477+
const table = await getTableById(args.tableId)
478+
if (!table || table.workspaceId !== workspaceId) {
479+
return { success: false, message: `Table not found: ${args.tableId}` }
480+
}
481+
477482
const requestId = generateId().slice(0, 8)
478483
const result = await queryRows(
479-
args.tableId,
480-
workspaceId,
484+
table,
481485
{
482486
filter: args.filter,
483487
sort: args.sort,
@@ -569,21 +573,21 @@ export const userTableServerTool: BaseServerTool<UserTableArgs, UserTableResult>
569573
}
570574

571575
const table = await getTableById(args.tableId)
572-
if (!table) {
576+
if (!table || table.workspaceId !== workspaceId) {
573577
return { success: false, message: `Table not found: ${args.tableId}` }
574578
}
575579

576580
const requestId = generateId().slice(0, 8)
577581
assertNotAborted()
578582
const result = await updateRowsByFilter(
583+
table,
579584
{
580585
tableId: args.tableId,
581586
filter: args.filter,
582587
data: args.data,
583588
limit: args.limit,
584589
workspaceId,
585590
},
586-
table,
587591
requestId
588592
)
589593

@@ -605,9 +609,15 @@ export const userTableServerTool: BaseServerTool<UserTableArgs, UserTableResult>
605609
return { success: false, message: 'Workspace ID is required' }
606610
}
607611

612+
const table = await getTableById(args.tableId)
613+
if (!table || table.workspaceId !== workspaceId) {
614+
return { success: false, message: `Table not found: ${args.tableId}` }
615+
}
616+
608617
const requestId = generateId().slice(0, 8)
609618
assertNotAborted()
610619
const result = await deleteRowsByFilter(
620+
table,
611621
{
612622
tableId: args.tableId,
613623
filter: args.filter,

apps/sim/lib/copilot/tools/server/visualization/generate-visualization.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ async function collectSandboxFiles(
118118
if (inputTables?.length) {
119119
for (const tableId of inputTables) {
120120
const table = await getTableById(tableId)
121-
if (!table) {
121+
if (!table || table.workspaceId !== workspaceId) {
122122
logger.warn('Sandbox input table not found', { tableId })
123123
continue
124124
}
125-
const { rows } = await queryRows(tableId, workspaceId, { limit: 10000 }, 'sandbox-input')
125+
const { rows } = await queryRows(table, { limit: 10000 }, 'sandbox-input')
126126
const schema = table.schema as { columns: Array<{ name: string; type?: string }> }
127127
const cols = schema.columns.map((c) => c.name)
128128
const typeComment = `# types: ${schema.columns.map((c) => `${c.name}=${c.type || 'string'}`).join(', ')}`

0 commit comments

Comments
 (0)