-
Notifications
You must be signed in to change notification settings - Fork 2
feat(sql-builder): add arithmetic operations and type casts #286
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { setupIntegrationTest } from './setup'; | ||
|
|
||
| describe('integration: arithmetic operations', () => { | ||
| const { db, runtime } = setupIntegrationTest(); | ||
|
|
||
| it('add computes column + literal', async () => { | ||
| const rows = await runtime().execute( | ||
| db() | ||
| .posts.select('id') | ||
| .select('viewsPlus', (f, fns) => fns.add(f.views, 10)) | ||
| .where((f, fns) => fns.eq(f.id, 1)) | ||
| .build(), | ||
| ); | ||
| expect(rows).toHaveLength(1); | ||
| expect(rows[0]!.viewsPlus).toBe(110); | ||
| }); | ||
|
|
||
| it('sub computes column - literal', async () => { | ||
| const rows = await runtime().execute( | ||
| db() | ||
| .posts.select('id') | ||
| .select('viewsMinus', (f, fns) => fns.sub(f.views, 10)) | ||
| .where((f, fns) => fns.eq(f.id, 1)) | ||
| .build(), | ||
| ); | ||
| expect(rows[0]!.viewsMinus).toBe(90); | ||
| }); | ||
|
|
||
| it('mul computes column * literal', async () => { | ||
| const rows = await runtime().execute( | ||
| db() | ||
| .posts.select('id') | ||
| .select('viewsDouble', (f, fns) => fns.mul(f.views, 2)) | ||
| .where((f, fns) => fns.eq(f.id, 1)) | ||
| .build(), | ||
| ); | ||
| expect(rows[0]!.viewsDouble).toBe(200); | ||
| }); | ||
|
|
||
| it('div computes column / literal', async () => { | ||
| const rows = await runtime().execute( | ||
| db() | ||
| .posts.select('id') | ||
| .select('viewsHalf', (f, fns) => fns.div(f.views, 2)) | ||
| .where((f, fns) => fns.eq(f.id, 1)) | ||
| .build(), | ||
| ); | ||
| expect(rows[0]!.viewsHalf).toBe(50); | ||
| }); | ||
|
|
||
| it('mod computes column % literal', async () => { | ||
| const rows = await runtime().execute( | ||
| db() | ||
| .posts.select('id') | ||
| .select('viewsMod', (f, fns) => fns.mod(f.views, 3)) | ||
| .where((f, fns) => fns.eq(f.id, 1)) | ||
| .build(), | ||
| ); | ||
| expect(rows[0]!.viewsMod).toBe(1); | ||
| }); | ||
|
|
||
| it('arithmetic in WHERE clause filters correctly', async () => { | ||
| const rows = await runtime().execute( | ||
| db() | ||
| .posts.select('id', 'views') | ||
| .where((f, fns) => fns.gt(fns.add(f.views, 50), 150)) | ||
| .build(), | ||
| ); | ||
| expect(rows.every((r) => r.views + 50 > 150)).toBe(true); | ||
|
Comment on lines
+63
to
+70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This
🤖 Prompt for AI Agents |
||
| }); | ||
|
|
||
| it('nested arithmetic works', async () => { | ||
| const rows = await runtime().execute( | ||
| db() | ||
| .posts.select('id') | ||
| .select('computed', (f, fns) => fns.add(fns.mul(f.views, 2), 1)) | ||
| .where((f, fns) => fns.eq(f.id, 1)) | ||
| .build(), | ||
| ); | ||
| expect(rows[0]!.computed).toBe(201); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { setupIntegrationTest } from './setup'; | ||
|
|
||
| describe('integration: type cast', () => { | ||
| const { db, runtime } = setupIntegrationTest(); | ||
|
|
||
| it('cast int to text', async () => { | ||
| const rows = await runtime().execute( | ||
| db() | ||
| .posts.select('id') | ||
| .select('idText', (f, fns) => fns.cast(f.id, { codecId: 'pg/text@1', nullable: false })) | ||
| .where((f, fns) => fns.eq(f.id, 1)) | ||
| .build(), | ||
| ); | ||
| expect(rows[0]!.idText).toBe('1'); | ||
| }); | ||
|
|
||
| it('cast in WHERE clause', async () => { | ||
| const rows = await runtime().execute( | ||
| db() | ||
| .posts.select('id') | ||
| .where((f, fns) => fns.eq(fns.cast(f.id, { codecId: 'pg/text@1', nullable: false }), '1')) | ||
| .build(), | ||
| ); | ||
| expect(rows).toHaveLength(1); | ||
| expect(rows[0]!.id).toBe(1); | ||
| }); | ||
| }); |
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.
cast()should preserve source nullability.This signature lets
fns.cast(nullableExpr, { codecId: 'pg/text@1', nullable: false })type-check as non-null, and the runtime implementation copies that flag straight intoExpressionImpl.field. SQL casts do not turnNULLinto non-NULL, so the output nullability should be derived from the source expression instead of caller input.🐛 Proposed fix
🤖 Prompt for AI Agents