@object-ui/core 0.3.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. package/.turbo/turbo-build.log +4 -0
  2. package/CHANGELOG.md +11 -0
  3. package/dist/actions/ActionRunner.d.ts +228 -4
  4. package/dist/actions/ActionRunner.js +397 -45
  5. package/dist/actions/TransactionManager.d.ts +193 -0
  6. package/dist/actions/TransactionManager.js +410 -0
  7. package/dist/actions/index.d.ts +2 -1
  8. package/dist/actions/index.js +2 -1
  9. package/dist/adapters/ApiDataSource.d.ts +69 -0
  10. package/dist/adapters/ApiDataSource.js +293 -0
  11. package/dist/adapters/ValueDataSource.d.ts +55 -0
  12. package/dist/adapters/ValueDataSource.js +287 -0
  13. package/dist/adapters/index.d.ts +3 -0
  14. package/dist/adapters/index.js +5 -2
  15. package/dist/adapters/resolveDataSource.d.ts +40 -0
  16. package/dist/adapters/resolveDataSource.js +59 -0
  17. package/dist/data-scope/DataScopeManager.d.ts +127 -0
  18. package/dist/data-scope/DataScopeManager.js +229 -0
  19. package/dist/data-scope/index.d.ts +10 -0
  20. package/dist/data-scope/index.js +10 -0
  21. package/dist/evaluator/ExpressionCache.d.ts +101 -0
  22. package/dist/evaluator/ExpressionCache.js +135 -0
  23. package/dist/evaluator/ExpressionEvaluator.d.ts +30 -2
  24. package/dist/evaluator/ExpressionEvaluator.js +60 -16
  25. package/dist/evaluator/FormulaFunctions.d.ts +58 -0
  26. package/dist/evaluator/FormulaFunctions.js +350 -0
  27. package/dist/evaluator/index.d.ts +4 -2
  28. package/dist/evaluator/index.js +4 -2
  29. package/dist/index.d.ts +14 -7
  30. package/dist/index.js +13 -9
  31. package/dist/query/index.d.ts +6 -0
  32. package/dist/query/index.js +6 -0
  33. package/dist/query/query-ast.d.ts +32 -0
  34. package/dist/query/query-ast.js +268 -0
  35. package/dist/registry/PluginScopeImpl.d.ts +80 -0
  36. package/dist/registry/PluginScopeImpl.js +243 -0
  37. package/dist/registry/PluginSystem.d.ts +66 -0
  38. package/dist/registry/PluginSystem.js +142 -0
  39. package/dist/registry/Registry.d.ts +83 -4
  40. package/dist/registry/Registry.js +113 -7
  41. package/dist/registry/WidgetRegistry.d.ts +120 -0
  42. package/dist/registry/WidgetRegistry.js +275 -0
  43. package/dist/theme/ThemeEngine.d.ts +82 -0
  44. package/dist/theme/ThemeEngine.js +400 -0
  45. package/dist/theme/index.d.ts +8 -0
  46. package/dist/theme/index.js +8 -0
  47. package/dist/validation/index.d.ts +9 -0
  48. package/dist/validation/index.js +9 -0
  49. package/dist/validation/validation-engine.d.ts +88 -0
  50. package/dist/validation/validation-engine.js +428 -0
  51. package/dist/validation/validators/index.d.ts +16 -0
  52. package/dist/validation/validators/index.js +16 -0
  53. package/dist/validation/validators/object-validation-engine.d.ts +118 -0
  54. package/dist/validation/validators/object-validation-engine.js +538 -0
  55. package/package.json +14 -5
  56. package/src/actions/ActionRunner.ts +577 -55
  57. package/src/actions/TransactionManager.ts +521 -0
  58. package/src/actions/__tests__/ActionRunner.params.test.ts +134 -0
  59. package/src/actions/__tests__/ActionRunner.test.ts +711 -0
  60. package/src/actions/__tests__/TransactionManager.test.ts +447 -0
  61. package/src/actions/index.ts +2 -1
  62. package/src/adapters/ApiDataSource.ts +349 -0
  63. package/src/adapters/ValueDataSource.ts +332 -0
  64. package/src/adapters/__tests__/ApiDataSource.test.ts +418 -0
  65. package/src/adapters/__tests__/ValueDataSource.test.ts +325 -0
  66. package/src/adapters/__tests__/resolveDataSource.test.ts +144 -0
  67. package/src/adapters/index.ts +6 -1
  68. package/src/adapters/resolveDataSource.ts +79 -0
  69. package/src/builder/__tests__/schema-builder.test.ts +235 -0
  70. package/src/data-scope/DataScopeManager.ts +269 -0
  71. package/src/data-scope/__tests__/DataScopeManager.test.ts +211 -0
  72. package/src/data-scope/index.ts +16 -0
  73. package/src/evaluator/ExpressionCache.ts +192 -0
  74. package/src/evaluator/ExpressionEvaluator.ts +61 -16
  75. package/src/evaluator/FormulaFunctions.ts +398 -0
  76. package/src/evaluator/__tests__/ExpressionCache.test.ts +135 -0
  77. package/src/evaluator/__tests__/ExpressionContext.test.ts +110 -0
  78. package/src/evaluator/__tests__/FormulaFunctions.test.ts +447 -0
  79. package/src/evaluator/index.ts +4 -2
  80. package/src/index.ts +14 -10
  81. package/src/query/__tests__/query-ast.test.ts +211 -0
  82. package/src/query/__tests__/window-functions.test.ts +275 -0
  83. package/src/query/index.ts +7 -0
  84. package/src/query/query-ast.ts +341 -0
  85. package/src/registry/PluginScopeImpl.ts +259 -0
  86. package/src/registry/PluginSystem.ts +161 -0
  87. package/src/registry/Registry.ts +136 -8
  88. package/src/registry/WidgetRegistry.ts +316 -0
  89. package/src/registry/__tests__/PluginSystem.test.ts +226 -0
  90. package/src/registry/__tests__/Registry.test.ts +293 -0
  91. package/src/registry/__tests__/WidgetRegistry.test.ts +321 -0
  92. package/src/registry/__tests__/plugin-scope-integration.test.ts +283 -0
  93. package/src/theme/ThemeEngine.ts +452 -0
  94. package/src/theme/__tests__/ThemeEngine.test.ts +606 -0
  95. package/src/theme/index.ts +22 -0
  96. package/src/validation/__tests__/object-validation-engine.test.ts +567 -0
  97. package/src/validation/__tests__/schema-validator.test.ts +118 -0
  98. package/src/validation/__tests__/validation-engine.test.ts +102 -0
  99. package/src/validation/index.ts +10 -0
  100. package/src/validation/validation-engine.ts +520 -0
  101. package/src/validation/validators/index.ts +25 -0
  102. package/src/validation/validators/object-validation-engine.ts +722 -0
  103. package/tsconfig.tsbuildinfo +1 -1
  104. package/vitest.config.ts +2 -0
  105. package/src/adapters/index.d.ts +0 -8
  106. package/src/adapters/index.js +0 -10
  107. package/src/builder/schema-builder.d.ts +0 -294
  108. package/src/builder/schema-builder.js +0 -503
  109. package/src/index.d.ts +0 -13
  110. package/src/index.js +0 -16
  111. package/src/registry/Registry.d.ts +0 -56
  112. package/src/registry/Registry.js +0 -43
  113. package/src/types/index.d.ts +0 -19
  114. package/src/types/index.js +0 -8
  115. package/src/utils/filter-converter.d.ts +0 -57
  116. package/src/utils/filter-converter.js +0 -100
  117. package/src/validation/schema-validator.d.ts +0 -94
  118. package/src/validation/schema-validator.js +0 -278
@@ -0,0 +1,275 @@
1
+ /**
2
+ * ObjectUI
3
+ * Copyright (c) 2024-present ObjectStack Inc.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ /**
10
+ * @object-ui/core - Window Function Tests
11
+ *
12
+ * Tests for ObjectStack Spec v2.0.1 window function support
13
+ */
14
+
15
+ import { describe, it, expect } from 'vitest';
16
+ import { QueryASTBuilder } from '../query-ast';
17
+ import type { WindowNode, WindowFunction } from '@object-ui/types';
18
+
19
+ describe('QueryASTBuilder - Window Functions', () => {
20
+ const builder = new QueryASTBuilder();
21
+
22
+ describe('buildWindow', () => {
23
+ it('should build a simple row_number window function', () => {
24
+ const config = {
25
+ function: 'row_number' as WindowFunction,
26
+ alias: 'row_num',
27
+ partitionBy: ['department'],
28
+ orderBy: [{ field: 'salary', direction: 'desc' as const }],
29
+ };
30
+
31
+ // @ts-expect-error - testing private method
32
+ const result: WindowNode = builder.buildWindow(config);
33
+
34
+ expect(result).toMatchObject({
35
+ type: 'window',
36
+ function: 'row_number',
37
+ alias: 'row_num',
38
+ });
39
+
40
+ expect(result.partitionBy).toHaveLength(1);
41
+ expect(result.partitionBy![0]).toMatchObject({
42
+ type: 'field',
43
+ name: 'department',
44
+ });
45
+
46
+ expect(result.orderBy).toHaveLength(1);
47
+ expect(result.orderBy![0]).toMatchObject({
48
+ field: { type: 'field', name: 'salary' },
49
+ direction: 'desc',
50
+ });
51
+ });
52
+
53
+ it('should build a rank window function with multiple partition fields', () => {
54
+ const config = {
55
+ function: 'rank' as WindowFunction,
56
+ alias: 'rank_val',
57
+ partitionBy: ['department', 'location'],
58
+ orderBy: [
59
+ { field: 'performance_score', direction: 'desc' as const },
60
+ { field: 'tenure_years', direction: 'desc' as const },
61
+ ],
62
+ };
63
+
64
+ // @ts-expect-error - testing private method
65
+ const result: WindowNode = builder.buildWindow(config);
66
+
67
+ expect(result).toMatchObject({
68
+ type: 'window',
69
+ function: 'rank',
70
+ alias: 'rank_val',
71
+ });
72
+
73
+ expect(result.partitionBy).toHaveLength(2);
74
+ expect(result.orderBy).toHaveLength(2);
75
+ });
76
+
77
+ it('should build a lag window function with offset and default value', () => {
78
+ const config = {
79
+ function: 'lag' as WindowFunction,
80
+ field: 'revenue',
81
+ alias: 'prev_month_revenue',
82
+ partitionBy: ['product_id'],
83
+ orderBy: [{ field: 'month', direction: 'asc' as const }],
84
+ offset: 1,
85
+ defaultValue: 0,
86
+ };
87
+
88
+ // @ts-expect-error - testing private method
89
+ const result: WindowNode = builder.buildWindow(config);
90
+
91
+ expect(result).toMatchObject({
92
+ type: 'window',
93
+ function: 'lag',
94
+ alias: 'prev_month_revenue',
95
+ offset: 1,
96
+ });
97
+
98
+ expect(result.field).toMatchObject({
99
+ type: 'field',
100
+ name: 'revenue',
101
+ });
102
+
103
+ expect(result.defaultValue).toMatchObject({
104
+ type: 'literal',
105
+ value: 0,
106
+ data_type: 'number',
107
+ });
108
+ });
109
+
110
+ it('should build a lead window function', () => {
111
+ const config = {
112
+ function: 'lead' as WindowFunction,
113
+ field: 'sales',
114
+ alias: 'next_day_sales',
115
+ orderBy: [{ field: 'date', direction: 'asc' as const }],
116
+ offset: 1,
117
+ };
118
+
119
+ // @ts-expect-error - testing private method
120
+ const result: WindowNode = builder.buildWindow(config);
121
+
122
+ expect(result).toMatchObject({
123
+ type: 'window',
124
+ function: 'lead',
125
+ alias: 'next_day_sales',
126
+ offset: 1,
127
+ });
128
+ });
129
+
130
+ it('should build aggregate window functions (sum, avg, count)', () => {
131
+ const sumConfig = {
132
+ function: 'sum' as WindowFunction,
133
+ field: 'amount',
134
+ alias: 'running_total',
135
+ orderBy: [{ field: 'date', direction: 'asc' as const }],
136
+ frame: {
137
+ unit: 'rows' as const,
138
+ start: 'unbounded_preceding' as const,
139
+ end: 'current_row' as const,
140
+ },
141
+ };
142
+
143
+ // @ts-expect-error - testing private method
144
+ const result: WindowNode = builder.buildWindow(sumConfig);
145
+
146
+ expect(result).toMatchObject({
147
+ type: 'window',
148
+ function: 'sum',
149
+ alias: 'running_total',
150
+ });
151
+
152
+ expect(result.field).toMatchObject({
153
+ type: 'field',
154
+ name: 'amount',
155
+ });
156
+
157
+ expect(result.frame).toEqual({
158
+ unit: 'rows',
159
+ start: 'unbounded_preceding',
160
+ end: 'current_row',
161
+ });
162
+ });
163
+
164
+ it('should build first_value window function', () => {
165
+ const config = {
166
+ function: 'first_value' as WindowFunction,
167
+ field: 'price',
168
+ alias: 'first_price',
169
+ partitionBy: ['product_category'],
170
+ orderBy: [{ field: 'created_at', direction: 'asc' as const }],
171
+ };
172
+
173
+ // @ts-expect-error - testing private method
174
+ const result: WindowNode = builder.buildWindow(config);
175
+
176
+ expect(result).toMatchObject({
177
+ type: 'window',
178
+ function: 'first_value',
179
+ alias: 'first_price',
180
+ });
181
+ });
182
+
183
+ it('should build last_value window function', () => {
184
+ const config = {
185
+ function: 'last_value' as WindowFunction,
186
+ field: 'status',
187
+ alias: 'latest_status',
188
+ partitionBy: ['customer_id'],
189
+ orderBy: [{ field: 'updated_at', direction: 'desc' as const }],
190
+ };
191
+
192
+ // @ts-expect-error - testing private method
193
+ const result: WindowNode = builder.buildWindow(config);
194
+
195
+ expect(result).toMatchObject({
196
+ type: 'window',
197
+ function: 'last_value',
198
+ alias: 'latest_status',
199
+ });
200
+ });
201
+
202
+ it('should handle window function without partition by', () => {
203
+ const config = {
204
+ function: 'row_number' as WindowFunction,
205
+ alias: 'global_row_num',
206
+ orderBy: [{ field: 'created_at', direction: 'asc' as const }],
207
+ };
208
+
209
+ // @ts-expect-error - testing private method
210
+ const result: WindowNode = builder.buildWindow(config);
211
+
212
+ expect(result.partitionBy).toBeUndefined();
213
+ expect(result.orderBy).toBeDefined();
214
+ });
215
+
216
+ it('should handle window function with frame specification', () => {
217
+ const config = {
218
+ function: 'avg' as WindowFunction,
219
+ field: 'temperature',
220
+ alias: 'moving_avg_3days',
221
+ orderBy: [{ field: 'date', direction: 'asc' as const }],
222
+ frame: {
223
+ unit: 'rows' as const,
224
+ start: { type: 'preceding' as const, offset: 2 },
225
+ end: 'current_row' as const,
226
+ },
227
+ };
228
+
229
+ // @ts-expect-error - testing private method
230
+ const result: WindowNode = builder.buildWindow(config);
231
+
232
+ expect(result.frame).toEqual({
233
+ unit: 'rows',
234
+ start: { type: 'preceding', offset: 2 },
235
+ end: 'current_row',
236
+ });
237
+ });
238
+
239
+ it('should build dense_rank window function', () => {
240
+ const config = {
241
+ function: 'dense_rank' as WindowFunction,
242
+ alias: 'dense_rank_val',
243
+ partitionBy: ['team'],
244
+ orderBy: [{ field: 'score', direction: 'desc' as const }],
245
+ };
246
+
247
+ // @ts-expect-error - testing private method
248
+ const result: WindowNode = builder.buildWindow(config);
249
+
250
+ expect(result).toMatchObject({
251
+ type: 'window',
252
+ function: 'dense_rank',
253
+ alias: 'dense_rank_val',
254
+ });
255
+ });
256
+
257
+ it('should build percent_rank window function', () => {
258
+ const config = {
259
+ function: 'percent_rank' as WindowFunction,
260
+ alias: 'percentile',
261
+ partitionBy: ['class'],
262
+ orderBy: [{ field: 'exam_score', direction: 'desc' as const }],
263
+ };
264
+
265
+ // @ts-expect-error - testing private method
266
+ const result: WindowNode = builder.buildWindow(config);
267
+
268
+ expect(result).toMatchObject({
269
+ type: 'window',
270
+ function: 'percent_rank',
271
+ alias: 'percentile',
272
+ });
273
+ });
274
+ });
275
+ });
@@ -0,0 +1,7 @@
1
+ /**
2
+ * @object-ui/core - Query Module
3
+ *
4
+ * Phase 3.3: Query AST builder and utilities
5
+ */
6
+
7
+ export * from './query-ast.js';
@@ -0,0 +1,341 @@
1
+ /**
2
+ * ObjectUI - Query AST Builder
3
+ * Phase 3.3: QuerySchema AST implementation
4
+ * ObjectStack Spec v2.0.1: Window functions support
5
+ */
6
+
7
+ import type {
8
+ QueryAST,
9
+ QuerySchema,
10
+ SelectNode,
11
+ FromNode,
12
+ WhereNode,
13
+ JoinNode,
14
+ GroupByNode,
15
+ OrderByNode,
16
+ LimitNode,
17
+ OffsetNode,
18
+ AggregateNode,
19
+ WindowNode,
20
+ WindowFunction,
21
+ WindowFrame,
22
+ WindowConfig,
23
+ FieldNode,
24
+ LiteralNode,
25
+ OperatorNode,
26
+ LogicalOperator,
27
+ AdvancedFilterSchema,
28
+ AdvancedFilterCondition,
29
+ QuerySortConfig,
30
+ JoinConfig,
31
+ AggregationConfig,
32
+ } from '@object-ui/types';
33
+
34
+ /**
35
+ * Query AST Builder - Converts QuerySchema to AST
36
+ */
37
+ export class QueryASTBuilder {
38
+ build(query: QuerySchema): QueryAST {
39
+ const ast: QueryAST = {
40
+ select: this.buildSelect(query),
41
+ from: this.buildFrom(query),
42
+ };
43
+
44
+ if (query.filter) {
45
+ ast.where = this.buildWhere(query.filter);
46
+ }
47
+
48
+ if (query.joins && query.joins.length > 0) {
49
+ ast.joins = query.joins.map(join => this.buildJoin(join));
50
+ }
51
+
52
+ if (query.group_by && query.group_by.length > 0) {
53
+ ast.group_by = this.buildGroupBy(query.group_by);
54
+ }
55
+
56
+ if (query.sort && query.sort.length > 0) {
57
+ ast.order_by = this.buildOrderBy(query.sort);
58
+ }
59
+
60
+ if (query.limit !== undefined) {
61
+ ast.limit = this.buildLimit(query.limit);
62
+ }
63
+
64
+ if (query.offset !== undefined) {
65
+ ast.offset = this.buildOffset(query.offset);
66
+ }
67
+
68
+ return ast;
69
+ }
70
+
71
+ private buildSelect(query: QuerySchema): SelectNode {
72
+ const fields: (FieldNode | AggregateNode | WindowNode)[] = [];
73
+
74
+ if (query.fields && query.fields.length > 0) {
75
+ fields.push(...query.fields.map(field => this.buildField(field)));
76
+ } else if (!query.aggregations || query.aggregations.length === 0) {
77
+ // Only add '*' if there are no aggregations
78
+ fields.push(this.buildField('*'));
79
+ }
80
+
81
+ if (query.aggregations && query.aggregations.length > 0) {
82
+ fields.push(...query.aggregations.map(agg => this.buildAggregation(agg)));
83
+ }
84
+
85
+ // Add window functions (ObjectStack Spec v2.0.1)
86
+ if (query.windows && query.windows.length > 0) {
87
+ fields.push(...query.windows.map(win => this.buildWindow(win)));
88
+ }
89
+
90
+ return {
91
+ type: 'select',
92
+ fields,
93
+ distinct: false,
94
+ };
95
+ }
96
+
97
+ private buildFrom(query: QuerySchema): FromNode {
98
+ return {
99
+ type: 'from',
100
+ table: query.object,
101
+ };
102
+ }
103
+
104
+ private buildWhere(filter: AdvancedFilterSchema): WhereNode {
105
+ return {
106
+ type: 'where',
107
+ condition: this.buildFilterCondition(filter),
108
+ };
109
+ }
110
+
111
+ private buildFilterCondition(filter: AdvancedFilterSchema): OperatorNode {
112
+ const operator = filter.operator || 'and';
113
+ const operands: (OperatorNode | FieldNode | LiteralNode)[] = [];
114
+
115
+ if (filter.conditions && filter.conditions.length > 0) {
116
+ operands.push(...filter.conditions.map(cond => this.buildCondition(cond)));
117
+ }
118
+
119
+ if (filter.groups && filter.groups.length > 0) {
120
+ operands.push(...filter.groups.map(group => this.buildFilterCondition(group)));
121
+ }
122
+
123
+ return {
124
+ type: 'operator',
125
+ operator: operator as LogicalOperator,
126
+ operands,
127
+ };
128
+ }
129
+
130
+ private buildCondition(condition: AdvancedFilterCondition): OperatorNode {
131
+ const field = this.buildField(condition.field);
132
+
133
+ // Map filter operators to comparison operators
134
+ const operatorMap: Record<string, string> = {
135
+ 'equals': '=',
136
+ 'not_equals': '!=',
137
+ 'greater_than': '>',
138
+ 'greater_than_or_equal': '>=',
139
+ 'less_than': '<',
140
+ 'less_than_or_equal': '<=',
141
+ 'contains': 'contains',
142
+ 'not_contains': 'contains',
143
+ 'starts_with': 'starts_with',
144
+ 'ends_with': 'ends_with',
145
+ 'like': 'like',
146
+ 'ilike': 'ilike',
147
+ 'in': 'in',
148
+ 'not_in': 'not_in',
149
+ 'is_null': 'is_null',
150
+ 'is_not_null': 'is_not_null',
151
+ 'between': 'between',
152
+ };
153
+
154
+ const operator = operatorMap[condition.operator] || '=';
155
+
156
+ // Handle special operators
157
+ if (operator === 'between' && condition.values && condition.values.length === 2) {
158
+ return {
159
+ type: 'operator',
160
+ operator: 'between' as any,
161
+ operands: [
162
+ field,
163
+ this.buildLiteral(condition.values[0]),
164
+ this.buildLiteral(condition.values[1])
165
+ ],
166
+ };
167
+ }
168
+
169
+ if ((operator === 'in' || operator === 'not_in') && condition.values) {
170
+ return {
171
+ type: 'operator',
172
+ operator: operator as any,
173
+ operands: [field, ...condition.values.map(v => this.buildLiteral(v))],
174
+ };
175
+ }
176
+
177
+ if (operator === 'is_null' || operator === 'is_not_null') {
178
+ return {
179
+ type: 'operator',
180
+ operator: operator as any,
181
+ operands: [field],
182
+ };
183
+ }
184
+
185
+ // Standard binary operator
186
+ const value = this.buildLiteral(condition.value);
187
+ return {
188
+ type: 'operator',
189
+ operator: operator as any,
190
+ operands: [field, value],
191
+ };
192
+ }
193
+
194
+ private buildJoin(join: JoinConfig): JoinNode {
195
+ const onCondition: OperatorNode = {
196
+ type: 'operator',
197
+ operator: '=',
198
+ operands: [
199
+ this.buildField(join.on.local_field),
200
+ this.buildField(join.on.foreign_field, join.alias || join.object),
201
+ ],
202
+ };
203
+
204
+ return {
205
+ type: 'join',
206
+ join_type: join.type,
207
+ table: join.object,
208
+ alias: join.alias,
209
+ on: onCondition,
210
+ };
211
+ }
212
+
213
+ private buildGroupBy(fields: string[]): GroupByNode {
214
+ return {
215
+ type: 'group_by',
216
+ fields: fields.map(field => this.buildField(field)),
217
+ };
218
+ }
219
+
220
+ private buildOrderBy(sorts: QuerySortConfig[]): OrderByNode {
221
+ return {
222
+ type: 'order_by',
223
+ fields: sorts.map(sort => ({
224
+ field: this.buildField(sort.field),
225
+ direction: sort.order,
226
+ })),
227
+ };
228
+ }
229
+
230
+ private buildLimit(limit: number): LimitNode {
231
+ return {
232
+ type: 'limit',
233
+ value: limit,
234
+ };
235
+ }
236
+
237
+ private buildOffset(offset: number): OffsetNode {
238
+ return {
239
+ type: 'offset',
240
+ value: offset,
241
+ };
242
+ }
243
+
244
+ private buildField(field: string, table?: string): FieldNode {
245
+ const parts = field.split('.');
246
+
247
+ if (parts.length === 2) {
248
+ return {
249
+ type: 'field',
250
+ table: parts[0],
251
+ name: parts[1],
252
+ };
253
+ }
254
+
255
+ return {
256
+ type: 'field',
257
+ table,
258
+ name: field,
259
+ };
260
+ }
261
+
262
+ private buildLiteral(value: any): LiteralNode {
263
+ let dataType: 'string' | 'number' | 'boolean' | 'date' | 'null' = 'string';
264
+
265
+ if (value === null || value === undefined) {
266
+ dataType = 'null';
267
+ } else if (typeof value === 'number') {
268
+ dataType = 'number';
269
+ } else if (typeof value === 'boolean') {
270
+ dataType = 'boolean';
271
+ } else if (value instanceof Date) {
272
+ dataType = 'date';
273
+ }
274
+
275
+ return {
276
+ type: 'literal',
277
+ value,
278
+ data_type: dataType,
279
+ };
280
+ }
281
+
282
+ private buildAggregation(agg: AggregationConfig): AggregateNode {
283
+ return {
284
+ type: 'aggregate',
285
+ function: agg.function,
286
+ field: agg.field ? this.buildField(agg.field) : undefined,
287
+ alias: agg.alias,
288
+ distinct: agg.distinct,
289
+ };
290
+ }
291
+
292
+ /**
293
+ * Build window function node (ObjectStack Spec v2.0.1)
294
+ */
295
+ private buildWindow(config: WindowConfig): WindowNode {
296
+ const node: WindowNode = {
297
+ type: 'window',
298
+ function: config.function,
299
+ alias: config.alias,
300
+ };
301
+
302
+ if (config.field) {
303
+ node.field = this.buildField(config.field);
304
+ }
305
+
306
+ if (config.partitionBy && config.partitionBy.length > 0) {
307
+ node.partitionBy = config.partitionBy.map(field => this.buildField(field));
308
+ }
309
+
310
+ if (config.orderBy && config.orderBy.length > 0) {
311
+ node.orderBy = config.orderBy.map(sort => ({
312
+ field: this.buildField(sort.field),
313
+ direction: sort.direction,
314
+ }));
315
+ }
316
+
317
+ if (config.frame) {
318
+ node.frame = config.frame;
319
+ }
320
+
321
+ if (config.offset !== undefined) {
322
+ node.offset = config.offset;
323
+ }
324
+
325
+ if (config.defaultValue !== undefined) {
326
+ node.defaultValue = this.buildLiteral(config.defaultValue);
327
+ }
328
+
329
+ return node;
330
+ }
331
+
332
+ optimize(ast: QueryAST): QueryAST {
333
+ return ast;
334
+ }
335
+ }
336
+
337
+ export const defaultQueryASTBuilder = new QueryASTBuilder();
338
+
339
+ export function buildQueryAST(query: QuerySchema): QueryAST {
340
+ return defaultQueryASTBuilder.build(query);
341
+ }