@@ -3,14 +3,14 @@ import type { AllowedFunctionName, ConditionOperand } from "./schema.js"
33
44/**
55 * Extracts a value from a nested row structure
6- * @param nestedRow The nested row structure
6+ * @param namespacedRow The nested row structure
77 * @param columnRef The column reference (may include table.column format)
88 * @param mainTableAlias The main table alias to check first for columns without table reference
99 * @param joinedTableAlias The joined table alias to check second for columns without table reference
1010 * @returns The extracted value or undefined if not found
1111 */
12- export function extractValueFromNestedRow (
13- nestedRow : Record < string , unknown > ,
12+ export function extractValueFromNamespacedRow (
13+ namespacedRow : Record < string , unknown > ,
1414 columnRef : string ,
1515 mainTableAlias ?: string ,
1616 joinedTableAlias ?: string
@@ -20,7 +20,7 @@ export function extractValueFromNestedRow(
2020 const [ tableAlias , colName ] = columnRef . split ( `.` ) as [ string , string ]
2121
2222 // Get the table data
23- const tableData = nestedRow [ tableAlias ] as
23+ const tableData = namespacedRow [ tableAlias ] as
2424 | Record < string , unknown >
2525 | null
2626 | undefined
@@ -34,16 +34,19 @@ export function extractValueFromNestedRow(
3434 return value
3535 } else {
3636 // If no table is specified, first try to find in the main table if provided
37- if ( mainTableAlias && nestedRow [ mainTableAlias ] ) {
38- const mainTableData = nestedRow [ mainTableAlias ] as Record < string , unknown >
37+ if ( mainTableAlias && namespacedRow [ mainTableAlias ] ) {
38+ const mainTableData = namespacedRow [ mainTableAlias ] as Record <
39+ string ,
40+ unknown
41+ >
3942 if ( typeof mainTableData === `object` && columnRef in mainTableData ) {
4043 return mainTableData [ columnRef ]
4144 }
4245 }
4346
4447 // Then try the joined table if provided
45- if ( joinedTableAlias && nestedRow [ joinedTableAlias ] ) {
46- const joinedTableData = nestedRow [ joinedTableAlias ] as Record <
48+ if ( joinedTableAlias && namespacedRow [ joinedTableAlias ] ) {
49+ const joinedTableData = namespacedRow [ joinedTableAlias ] as Record <
4750 string ,
4851 unknown
4952 >
@@ -53,7 +56,7 @@ export function extractValueFromNestedRow(
5356 }
5457
5558 // If not found in main or joined table, try to find the column in any table
56- for ( const [ _tableAlias , tableData ] of Object . entries ( nestedRow ) ) {
59+ for ( const [ _tableAlias , tableData ] of Object . entries ( namespacedRow ) ) {
5760 if (
5861 tableData &&
5962 typeof tableData === `object` &&
@@ -69,17 +72,17 @@ export function extractValueFromNestedRow(
6972/**
7073 * Evaluates an operand against a nested row structure
7174 */
72- export function evaluateOperandOnNestedRow (
73- nestedRow : Record < string , unknown > ,
75+ export function evaluateOperandOnNamespacedRow (
76+ namespacedRow : Record < string , unknown > ,
7477 operand : ConditionOperand ,
7578 mainTableAlias ?: string ,
7679 joinedTableAlias ?: string
7780) : unknown {
7881 // Handle column references
7982 if ( typeof operand === `string` && operand . startsWith ( `@` ) ) {
8083 const columnRef = operand . substring ( 1 )
81- return extractValueFromNestedRow (
82- nestedRow ,
84+ return extractValueFromNamespacedRow (
85+ namespacedRow ,
8386 columnRef ,
8487 mainTableAlias ,
8588 joinedTableAlias
@@ -92,17 +95,17 @@ export function evaluateOperandOnNestedRow(
9295
9396 if ( typeof colRef === `string` ) {
9497 // First try to extract from nested row structure
95- const nestedValue = extractValueFromNestedRow (
96- nestedRow ,
98+ const nestedValue = extractValueFromNamespacedRow (
99+ namespacedRow ,
97100 colRef ,
98101 mainTableAlias ,
99102 joinedTableAlias
100103 )
101104
102105 // If not found in nested structure, check if it's a direct property of the row
103106 // This is important for HAVING clauses that reference aggregated values
104- if ( nestedValue === undefined && colRef in nestedRow ) {
105- return nestedRow [ colRef ]
107+ if ( nestedValue === undefined && colRef in namespacedRow ) {
108+ return namespacedRow [ colRef ]
106109 }
107110
108111 return nestedValue
@@ -121,15 +124,15 @@ export function evaluateOperandOnNestedRow(
121124 // If the arguments are a reference or another expression, evaluate them first
122125 const evaluatedArgs = Array . isArray ( args )
123126 ? args . map ( ( arg ) =>
124- evaluateOperandOnNestedRow (
125- nestedRow ,
127+ evaluateOperandOnNamespacedRow (
128+ namespacedRow ,
126129 arg as ConditionOperand ,
127130 mainTableAlias ,
128131 joinedTableAlias
129132 )
130133 )
131- : evaluateOperandOnNestedRow (
132- nestedRow ,
134+ : evaluateOperandOnNamespacedRow (
135+ namespacedRow ,
133136 args as ConditionOperand ,
134137 mainTableAlias ,
135138 joinedTableAlias
0 commit comments