11import { DbFieldType } from '@teable/core' ;
2+ import type { ISelectFormulaConversionContext } from '../../../features/record/query-builder/sql-conversion.visitor' ;
23import { SelectQueryAbstract } from '../select-query.abstract' ;
34
45/**
@@ -8,6 +9,17 @@ import { SelectQueryAbstract } from '../select-query.abstract';
89 * mutable functions and have different optimization strategies.
910 */
1011export class SelectQueryPostgres extends SelectQueryAbstract {
12+ private get tableAlias ( ) : string | undefined {
13+ const ctx = this . context as ISelectFormulaConversionContext | undefined ;
14+ return ctx ?. tableAlias ;
15+ }
16+
17+ private qualifySystemColumn ( column : string ) : string {
18+ const quoted = `"${ column } "` ;
19+ const alias = this . tableAlias ;
20+ return alias ? `"${ alias } ".${ quoted } ` : quoted ;
21+ }
22+
1123 private toNumericSafe ( expr : string ) : string {
1224 // Safely coerce any scalar to a floating-point number:
1325 // - Strip everything except digits, sign, decimal point
@@ -16,6 +28,10 @@ export class SelectQueryPostgres extends SelectQueryAbstract {
1628 return `NULLIF(REGEXP_REPLACE((${ expr } )::text, '[^0-9.+-]', '', 'g'), '')::double precision` ;
1729 }
1830
31+ private coalesceNumeric ( expr : string ) : string {
32+ return `COALESCE(${ this . toNumericSafe ( expr ) } , 0)` ;
33+ }
34+
1935 private isEmptyStringLiteral ( value : string ) : boolean {
2036 return value . trim ( ) === "''" ;
2137 }
@@ -220,12 +236,23 @@ export class SelectQueryPostgres extends SelectQueryAbstract {
220236 }
221237 // Numeric Functions
222238 sum ( params : string [ ] ) : string {
223- // In SELECT context, we can use window functions and aggregates more freely
224- return `SUM(${ this . joinParams ( params ) } )` ;
239+ if ( params . length === 0 ) {
240+ return '0' ;
241+ }
242+
243+ const terms = params . map ( ( param ) => this . coalesceNumeric ( param ) ) ;
244+ if ( terms . length === 1 ) {
245+ return terms [ 0 ] ;
246+ }
247+ return `(${ terms . join ( ' + ' ) } )` ;
225248 }
226249
227250 average ( params : string [ ] ) : string {
228- return `AVG(${ this . joinParams ( params ) } )` ;
251+ if ( params . length === 0 ) {
252+ return '0' ;
253+ }
254+ const numerator = this . sum ( params ) ;
255+ return `(${ numerator } ) / ${ params . length } ` ;
229256 }
230257
231258 max ( params : string [ ] ) : string {
@@ -488,7 +515,7 @@ export class SelectQueryPostgres extends SelectQueryAbstract {
488515
489516 lastModifiedTime ( ) : string {
490517 // This would typically reference a system column
491- return `" __last_modified_time"` ;
518+ return this . qualifySystemColumn ( ' __last_modified_time' ) ;
492519 }
493520
494521 minute ( date : string ) : string {
@@ -539,7 +566,7 @@ export class SelectQueryPostgres extends SelectQueryAbstract {
539566
540567 createdTime ( ) : string {
541568 // This would typically reference a system column
542- return `" __created_time"` ;
569+ return this . qualifySystemColumn ( ' __created_time' ) ;
543570 }
544571
545572 // Logical Functions
@@ -680,12 +707,12 @@ export class SelectQueryPostgres extends SelectQueryAbstract {
680707 // System Functions
681708 recordId ( ) : string {
682709 // This would typically reference the primary key
683- return ` __id` ;
710+ return this . qualifySystemColumn ( ' __id' ) ;
684711 }
685712
686713 autoNumber ( ) : string {
687714 // This would typically reference an auto-increment column
688- return ` __auto_number` ;
715+ return this . qualifySystemColumn ( ' __auto_number' ) ;
689716 }
690717
691718 textAll ( value : string ) : string {
0 commit comments