Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const groupNode = (node: Node.GROUP, collections: Collections) => {
case 'BY__':
break
case 'COLUMN': {
columns.push(columnNode(n, collections))
columns.push(columnNode(n, collections, true))
break
}
default:
Expand Down
6 changes: 3 additions & 3 deletions packages/tanstack-db-sql-interpreter/src/ast/orderClause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ const orderExpressionNode = (
switch (n.name) {
case 'ORDER_COLUMN':
return {
...columnNode(n.children[0], collections),
...columnNode(n.children[0], collections, true),
type: 'asc',
} as const
case 'ORDER_ASC':
return {
...columnNode(n.children[0], collections),
...columnNode(n.children[0], collections, true),
type: 'asc',
} as const
case 'ORDER_DESC':
return {
...columnNode(n.children[0], collections),
...columnNode(n.children[0], collections, true),
type: 'desc',
} as const
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { groupNode } from './groupClause.ts'
import { limitNode } from './limitClause.ts'
import { orderNode } from './orderClause.ts'
import { selectNode } from './selectClause.ts'
import { expressionNode } from './shared/expression/expression.ts'
import { applyExpression } from './shared/expression/apply.ts'
import { expressionNode } from './shared/expression/expression.ts'
import { stringifyExpression } from './shared/expression/stringify.ts'

export const selectStatementNode = (
Expand Down
8 changes: 6 additions & 2 deletions packages/tanstack-db-sql-interpreter/src/ast/shared/column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import {
import { defaultSwitchNodeError } from '../../util/error.ts'
import type { Collections } from '../../util/types.ts'

export const columnNode = (node: Node.COLUMN, collections: Collections) => {
export const columnNode = (
node: Node.COLUMN,
collections: Collections,
includeSelected: boolean = false,
) => {
const n = node.children[0]
switch (n.name) {
case 'COLUMN_NAME__': {
const column = n.value
return findColumnFromTables(collections, column)
return findColumnFromTables(collections, column, includeSelected)
}
case 'TABLE_COLUMN_NAME': {
const table = n.children[0].value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export const expressionNode = (
case 'COLUMN': {
return {
type: 'column',
column: columnNode(n, collections),
column: columnNode(n, collections, true),
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/tanstack-db-sql-interpreter/src/util/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ export const collectionProperties = (
export const findColumnFromTables = (
collections: Collections,
column: string,
includeSelected: boolean = false,
) => {
const tableNames = Object.keys(collections)
for (const table of tableNames) {
const properties = collectionProperties(collections, table)
if (properties.includes(column)) return { table, column }
}
if (includeSelected) return { table: '$selected', column }
throw new LiveQuerySqlError(
`Cannot find column '${column}' in tables: '${tableNames.join(`', '`)}'`,
)
Expand Down