Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.8k
fix: table comparsion query for date type column#4649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Aries0d0f
wants to merge
4
commits into
simstudioai:staging
from
Aries0d0f:fix/table-comparsion-query-for-date-type-column
Uh oh!
There was an error while loading. Please reload this page.
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d2bb947
fix: broken date/timestamp comparisons in table filter
Aries0d0f f4f1f5e
test: Update test for verify date column type handling.
Aries0d0f baf6585
fix: wire schema columns through all buildFilterClause call sites
Aries0d0f daa1047
fix: pass schema columns to buildSortClause in queryRows
Aries0d0f File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1435,7 +1435,7 @@ export async function queryRows( | ||
| let whereClause = baseConditions | ||
| if (filter && Object.keys(filter).length > 0) { | ||
| const filterClause = buildFilterClause(filter, tableName) | ||
| const filterClause = buildFilterClause(filter, tableName, options.columns) | ||
cursor[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if (filterClause) { | ||
| whereClause = and(baseConditions, filterClause) | ||
| } | ||
| @@ -1453,7 +1453,7 @@ export async function queryRows( | ||
| // Build ORDER BY clause (default to position ASC for stable ordering) | ||
| let orderByClause | ||
| if (sort && Object.keys(sort).length > 0) { | ||
| orderByClause = buildSortClause(sort, tableName) | ||
| orderByClause = buildSortClause(sort, tableName, options.columns) | ||
| } | ||
| // Execute query | ||
| @@ -1818,7 +1818,7 @@ export async function updateRowsByFilter( | ||
| ): Promise<BulkOperationResult> { | ||
| const tableName = USER_TABLE_ROWS_SQL_NAME | ||
| const filterClause = buildFilterClause(data.filter, tableName) | ||
| const filterClause = buildFilterClause(data.filter, tableName, (table.schema as TableSchema).columns) | ||
| if (!filterClause) { | ||
| throw new Error('Filter is required for bulk update') | ||
| } | ||
| @@ -2119,17 +2119,19 @@ async function recompactPositions(tableId: string, trx: DbTransaction, minDelete | ||
| * Deletes multiple rows matching a filter. | ||
| * | ||
| * @param data - Bulk delete data | ||
| * @param table - Table definition used to emit correct SQL casts in filter expressions | ||
| * @param requestId - Request ID for logging | ||
| * @returns Bulk operation result | ||
| */ | ||
| export async function deleteRowsByFilter( | ||
| data: BulkDeleteData, | ||
| table: TableDefinition, | ||
| requestId: string | ||
| ): Promise<BulkOperationResult> { | ||
| const tableName = USER_TABLE_ROWS_SQL_NAME | ||
| // Build filter clause | ||
| const filterClause = buildFilterClause(data.filter, tableName) | ||
| const filterClause = buildFilterClause(data.filter, tableName, (table.schema as TableSchema).columns) | ||
| if (!filterClause) { | ||
| throw new Error('Filter is required for bulk delete') | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -64,8 +64,13 @@ const ALLOWED_OPERATORS = new Set([ | ||
| * // Logical operators | ||
| * buildFilterClause({ $or: [{ status: 'active' }, { verified: true }] }, 'user_table_rows') | ||
| */ | ||
| export function buildFilterClause(filter: Filter, tableName: string): SQL | undefined { | ||
| export function buildFilterClause( | ||
| filter: Filter, | ||
| tableName: string, | ||
| columns?: ColumnDefinition[] | ||
| ): SQL | undefined { | ||
cursor[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| const conditions: SQL[] = [] | ||
| const columnTypeMap = new Map(columns?.map((col) => [col.name, col.type])) | ||
| for (const [field, condition] of Object.entries(filter)) { | ||
| if (condition === undefined) { | ||
| @@ -75,7 +80,7 @@ export function buildFilterClause(filter: Filter, tableName: string): SQL | unde | ||
| // This represents a case where the filter is a logical OR of multiple filters | ||
| // e.g. { $or: [{ status: 'active' }, { status: 'pending' }] } | ||
| if (field === '$or' && Array.isArray(condition)) { | ||
| const orClause = buildLogicalClause(condition as Filter[], tableName, 'OR') | ||
| const orClause = buildLogicalClause(condition as Filter[], tableName, 'OR', columns) | ||
| if (orClause) { | ||
| conditions.push(orClause) | ||
| } | ||
| @@ -85,7 +90,7 @@ export function buildFilterClause(filter: Filter, tableName: string): SQL | unde | ||
| // This represents a case where the filter is a logical AND of multiple filters | ||
| // e.g. { $and: [{ status: 'active' }, { status: 'pending' }] } | ||
| if (field === '$and' && Array.isArray(condition)) { | ||
| const andClause = buildLogicalClause(condition as Filter[], tableName, 'AND') | ||
| const andClause = buildLogicalClause(condition as Filter[], tableName, 'AND', columns) | ||
| if (andClause) { | ||
| conditions.push(andClause) | ||
| } | ||
| @@ -103,7 +108,8 @@ export function buildFilterClause(filter: Filter, tableName: string): SQL | unde | ||
| const fieldConditions = buildFieldCondition( | ||
| tableName, | ||
| field, | ||
| condition as JsonValue | ConditionOperators | ||
| condition as JsonValue | ConditionOperators, | ||
| columnTypeMap.get(field) | ||
| ) | ||
| conditions.push(...fieldConditions) | ||
| } | ||
| @@ -208,7 +214,8 @@ function validateOperator(operator: string): void { | ||
| function buildFieldCondition( | ||
| tableName: string, | ||
| field: string, | ||
| condition: JsonValue | ConditionOperators | ||
| condition: JsonValue | ConditionOperators, | ||
| columnType?: string | ||
| ): SQL[] { | ||
| validateFieldName(field) | ||
| @@ -231,19 +238,19 @@ function buildFieldCondition( | ||
| break | ||
| case '$gt': | ||
| conditions.push(buildComparisonClause(tableName, field, '>', value as number)) | ||
| conditions.push(buildComparisonClause(tableName, field, '>', value as number | string, columnType)) | ||
| break | ||
| case '$gte': | ||
| conditions.push(buildComparisonClause(tableName, field, '>=', value as number)) | ||
| conditions.push(buildComparisonClause(tableName, field, '>=', value as number | string, columnType)) | ||
| break | ||
| case '$lt': | ||
| conditions.push(buildComparisonClause(tableName, field, '<', value as number)) | ||
| conditions.push(buildComparisonClause(tableName, field, '<', value as number | string, columnType)) | ||
| break | ||
| case '$lte': | ||
| conditions.push(buildComparisonClause(tableName, field, '<=', value as number)) | ||
| conditions.push(buildComparisonClause(tableName, field, '<=', value as number | string, columnType)) | ||
| break | ||
| case '$in': | ||
| @@ -312,11 +319,12 @@ function buildFieldCondition( | ||
| function buildLogicalClause( | ||
| subFilters: Filter[], | ||
| tableName: string, | ||
| operator: 'OR' | 'AND' | ||
| operator: 'OR' | 'AND', | ||
| columns?: ColumnDefinition[] | ||
| ): SQL | undefined { | ||
| const clauses: SQL[] = [] | ||
| for (const subFilter of subFilters) { | ||
| const clause = buildFilterClause(subFilter, tableName) | ||
| const clause = buildFilterClause(subFilter, tableName, columns) | ||
| if (clause) { | ||
| clauses.push(clause) | ||
| } | ||
| @@ -334,15 +342,24 @@ function buildContainmentClause(tableName: string, field: string, value: JsonVal | ||
| return sql`${sql.raw(`${tableName}.data`)} @> ${jsonObj}::jsonb` | ||
| } | ||
| /** Builds numeric comparison: `(data->>'field')::numeric <op> value` (cannot use GIN index) */ | ||
| /** | ||
| * Builds a range comparison: `(data->>'field')::<type> <op> value` (cannot use GIN index). | ||
| * Uses `::timestamp` when `columnType === 'date'`, otherwise `::numeric`. | ||
| */ | ||
| function buildComparisonClause( | ||
| tableName: string, | ||
| field: string, | ||
| operator: '>' | '>=' | '<' | '<=', | ||
| value: number | ||
| value: number | string, | ||
| columnType?: string | ||
| ): SQL { | ||
| const escapedField = field.replace(/'/g, "''") | ||
| return sql`(${sql.raw(`${tableName}.data->>'${escapedField}'`)})::numeric ${sql.raw(operator)} ${value}` | ||
| const extract = sql.raw(`${tableName}.data->>'${escapedField}'`) | ||
| const isDate = columnType === 'date' | ||
| if (isDate) { | ||
| return sql`(${extract})::timestamp ${sql.raw(operator)} ${value}::timestamp` | ||
| } | ||
| return sql`(${extract})::numeric ${sql.raw(operator)} ${value}` | ||
| } | ||
| /** Escapes LIKE/ILIKE wildcard characters so they match literally */ | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getRawSqlStringrelies on undocumented drizzle internalsThe helper reads
queryChunksfrom the drizzleSQLobject, which is not part of drizzle-orm's public API. The inner shape ({ value?: string[] }) also doesn't match the actual exportedStringChunk/Paramtypes in drizzle — a minor drizzle version bump could silently change the internal structure, causing these assertions to always pass (fallback toJSON.stringify) without actually testing the cast.