@malloydata/db-snowflake 0.0.375 → 0.0.376
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.
- package/dist/index.js +29 -2
- package/dist/index.js.map +1 -1
- package/dist/snowflake_connection.d.ts +48 -13
- package/dist/snowflake_connection.js +146 -228
- package/dist/snowflake_connection.js.map +1 -1
- package/dist/snowflake_connection.spec.js +84 -14
- package/dist/snowflake_connection.spec.js.map +1 -1
- package/dist/snowflake_sample_strategy.spec.d.ts +1 -0
- package/dist/snowflake_sample_strategy.spec.js +25 -0
- package/dist/snowflake_sample_strategy.spec.js.map +1 -0
- package/dist/snowflake_variant_schema.d.ts +43 -0
- package/dist/snowflake_variant_schema.js +203 -0
- package/dist/snowflake_variant_schema.js.map +1 -0
- package/dist/snowflake_variant_schema.spec.d.ts +1 -0
- package/dist/snowflake_variant_schema.spec.js +150 -0
- package/dist/snowflake_variant_schema.spec.js.map +1 -0
- package/package.json +2 -2
- package/src/index.ts +34 -1
- package/src/snowflake_connection.spec.ts +88 -14
- package/src/snowflake_connection.ts +220 -262
- package/src/snowflake_sample_strategy.spec.ts +43 -0
- package/src/snowflake_variant_schema.spec.ts +188 -0
- package/src/snowflake_variant_schema.ts +301 -0
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright Contributors to the Malloy project
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {SnowflakeDialect} from '@malloydata/malloy';
|
|
7
|
+
import {
|
|
8
|
+
accumulateVariantPath,
|
|
9
|
+
buildTopLevelField,
|
|
10
|
+
createVariantSchemaState,
|
|
11
|
+
mergeShape,
|
|
12
|
+
PathParser,
|
|
13
|
+
seedTopLevelShape,
|
|
14
|
+
type NestedColumn,
|
|
15
|
+
type Shape,
|
|
16
|
+
} from './snowflake_variant_schema';
|
|
17
|
+
|
|
18
|
+
describe('snowflake variant schema helper', () => {
|
|
19
|
+
const dialect = new SnowflakeDialect();
|
|
20
|
+
|
|
21
|
+
function inferField(
|
|
22
|
+
nestedColumn: NestedColumn,
|
|
23
|
+
rows: Array<{path: string; type: string}>
|
|
24
|
+
) {
|
|
25
|
+
const state = createVariantSchemaState();
|
|
26
|
+
seedTopLevelShape(state, nestedColumn);
|
|
27
|
+
for (const row of rows) {
|
|
28
|
+
accumulateVariantPath(
|
|
29
|
+
state,
|
|
30
|
+
new PathParser(row.path).segments(),
|
|
31
|
+
row.type
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
return buildTopLevelField(nestedColumn, state, dialect);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
test('reconstructs object shape from descendant-only evidence', () => {
|
|
38
|
+
expect(
|
|
39
|
+
inferField({kind: 'variant', name: 'BASE_TOUCHPOINT'}, [
|
|
40
|
+
{path: 'BASE_TOUCHPOINT.NETWORK', type: 'varchar'},
|
|
41
|
+
{path: 'BASE_TOUCHPOINT.PLATFORM', type: 'varchar'},
|
|
42
|
+
])
|
|
43
|
+
).toEqual({
|
|
44
|
+
type: 'record',
|
|
45
|
+
name: 'BASE_TOUCHPOINT',
|
|
46
|
+
join: 'one',
|
|
47
|
+
fields: [
|
|
48
|
+
{name: 'NETWORK', type: 'string'},
|
|
49
|
+
{name: 'PLATFORM', type: 'string'},
|
|
50
|
+
],
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test('degrades object-array conflict at a shared prefix', () => {
|
|
55
|
+
expect(
|
|
56
|
+
inferField({kind: 'variant', name: 'X'}, [
|
|
57
|
+
{path: 'X.Y', type: 'varchar'},
|
|
58
|
+
{path: 'X[*].Z', type: 'decimal'},
|
|
59
|
+
])
|
|
60
|
+
).toEqual({
|
|
61
|
+
type: 'sql native',
|
|
62
|
+
rawType: 'variant',
|
|
63
|
+
name: 'X',
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('builds array of records from stable descendants', () => {
|
|
68
|
+
expect(
|
|
69
|
+
inferField({kind: 'variant', name: 'ITEMS'}, [
|
|
70
|
+
{path: 'ITEMS[*].FOO', type: 'varchar'},
|
|
71
|
+
{path: 'ITEMS[*].BAR', type: 'boolean'},
|
|
72
|
+
])
|
|
73
|
+
).toEqual({
|
|
74
|
+
type: 'array',
|
|
75
|
+
name: 'ITEMS',
|
|
76
|
+
join: 'many',
|
|
77
|
+
elementTypeDef: {type: 'record_element'},
|
|
78
|
+
fields: [
|
|
79
|
+
{name: 'FOO', type: 'string'},
|
|
80
|
+
{name: 'BAR', type: 'boolean'},
|
|
81
|
+
],
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test('top-level array with no descendants becomes array of variant', () => {
|
|
86
|
+
expect(inferField({kind: 'array', name: 'DIMENSION_SET_IDS'}, [])).toEqual({
|
|
87
|
+
type: 'array',
|
|
88
|
+
name: 'DIMENSION_SET_IDS',
|
|
89
|
+
join: 'many',
|
|
90
|
+
elementTypeDef: {type: 'sql native', rawType: 'variant'},
|
|
91
|
+
fields: [
|
|
92
|
+
{name: 'value', type: 'sql native', rawType: 'variant'},
|
|
93
|
+
{
|
|
94
|
+
name: 'each',
|
|
95
|
+
type: 'sql native',
|
|
96
|
+
rawType: 'variant',
|
|
97
|
+
e: {node: 'field', path: ['value']},
|
|
98
|
+
},
|
|
99
|
+
],
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test('top-level DESCRIBE seed stays authoritative over conflicting sample', () => {
|
|
104
|
+
expect(
|
|
105
|
+
inferField({kind: 'array', name: 'DIMENSION_SET_IDS'}, [
|
|
106
|
+
{path: 'DIMENSION_SET_IDS.foo', type: 'varchar'},
|
|
107
|
+
])
|
|
108
|
+
).toEqual({
|
|
109
|
+
type: 'array',
|
|
110
|
+
name: 'DIMENSION_SET_IDS',
|
|
111
|
+
join: 'many',
|
|
112
|
+
elementTypeDef: {type: 'sql native', rawType: 'variant'},
|
|
113
|
+
fields: [
|
|
114
|
+
{name: 'value', type: 'sql native', rawType: 'variant'},
|
|
115
|
+
{
|
|
116
|
+
name: 'each',
|
|
117
|
+
type: 'sql native',
|
|
118
|
+
rawType: 'variant',
|
|
119
|
+
e: {node: 'field', path: ['value']},
|
|
120
|
+
},
|
|
121
|
+
],
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
test('top-level object with no descendants becomes opaque variant', () => {
|
|
126
|
+
expect(inferField({kind: 'object', name: 'PAYLOAD'}, [])).toEqual({
|
|
127
|
+
type: 'sql native',
|
|
128
|
+
rawType: 'variant',
|
|
129
|
+
name: 'PAYLOAD',
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test('quoted path names with punctuation are preserved', () => {
|
|
134
|
+
expect(
|
|
135
|
+
inferField({kind: 'variant', name: 'DATA'}, [
|
|
136
|
+
{path: "DATA['a.b'][*]['c[d]']", type: 'varchar'},
|
|
137
|
+
])
|
|
138
|
+
).toEqual({
|
|
139
|
+
type: 'record',
|
|
140
|
+
name: 'DATA',
|
|
141
|
+
join: 'one',
|
|
142
|
+
fields: [
|
|
143
|
+
{
|
|
144
|
+
type: 'array',
|
|
145
|
+
name: 'a.b',
|
|
146
|
+
join: 'many',
|
|
147
|
+
elementTypeDef: {type: 'record_element'},
|
|
148
|
+
fields: [{name: 'c[d]', type: 'string'}],
|
|
149
|
+
},
|
|
150
|
+
],
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
test('leaf-type conflicts degrade to variant', () => {
|
|
155
|
+
const first: Shape = {kind: 'leaf', type: 'varchar'};
|
|
156
|
+
const second: Shape = {kind: 'leaf', type: 'decimal'};
|
|
157
|
+
expect(mergeShape(first, second)).toEqual({kind: 'variant'});
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
test('scalar-vs-object at same path degrades that field only', () => {
|
|
161
|
+
// The sample emits both the scalar observation and the object
|
|
162
|
+
// observation (same path, two rows from the distinct (path, type)
|
|
163
|
+
// query). mergeShape collapses DATA.foo to variant; the parent
|
|
164
|
+
// DATA stays a record and siblings keep their types.
|
|
165
|
+
expect(
|
|
166
|
+
inferField({kind: 'variant', name: 'DATA'}, [
|
|
167
|
+
{path: 'DATA.foo', type: 'object'},
|
|
168
|
+
{path: 'DATA.foo', type: 'varchar'},
|
|
169
|
+
{path: 'DATA.foo.bar', type: 'decimal'},
|
|
170
|
+
{path: 'DATA.sib', type: 'varchar'},
|
|
171
|
+
])
|
|
172
|
+
).toEqual({
|
|
173
|
+
type: 'record',
|
|
174
|
+
name: 'DATA',
|
|
175
|
+
join: 'one',
|
|
176
|
+
fields: [
|
|
177
|
+
{type: 'sql native', rawType: 'variant', name: 'foo'},
|
|
178
|
+
{type: 'string', name: 'sib'},
|
|
179
|
+
],
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
test('variant shape is monotonic', () => {
|
|
184
|
+
expect(mergeShape({kind: 'variant'}, {kind: 'object'})).toEqual({
|
|
185
|
+
kind: 'variant',
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
});
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright Contributors to the Malloy project
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// Infers Malloy types for Snowflake VARIANT / ARRAY / OBJECT columns from
|
|
7
|
+
// sampled (path, type) evidence in two phases:
|
|
8
|
+
//
|
|
9
|
+
// 1. Accumulate. Each sampled path implies a shape for every one of its
|
|
10
|
+
// prefixes (a `.field` step implies the parent is an object; a `[*]`
|
|
11
|
+
// step implies an array; the terminal step is a leaf of the observed
|
|
12
|
+
// SQL type). `VariantSchemaState` stores a `shapes` map keyed by
|
|
13
|
+
// prefix and a `children` adjacency map. `seedTopLevelShape` pre-seeds
|
|
14
|
+
// top-level ARRAY/OBJECT from DESCRIBE as authoritative — sample rows
|
|
15
|
+
// cannot override those shapes.
|
|
16
|
+
// 2. Build. `buildTopLevelField` walks the accumulated state and emits a
|
|
17
|
+
// Malloy `FieldDef`. Any prefix whose shape is `variant` (or absent)
|
|
18
|
+
// degrades locally to `sql native` variant; stable siblings are kept.
|
|
19
|
+
//
|
|
20
|
+
// Vocabulary: `Segment` is one step in a path; `Shape` is what we've
|
|
21
|
+
// concluded a prefix must be (`object | array | leaf | variant`, where
|
|
22
|
+
// `variant` is the absorbing state); `VariantSchemaState` is the whole
|
|
23
|
+
// accumulator. All consistency decisions flow through `mergeShape`.
|
|
24
|
+
//
|
|
25
|
+
// Snowflake-specific invariant: path access on a VARIANT whose actual
|
|
26
|
+
// value doesn't match the path returns NULL rather than raising. That is
|
|
27
|
+
// what makes reconstructing an object shape from descendant-only evidence
|
|
28
|
+
// safe here — rows where the parent is a scalar still evaluate cleanly
|
|
29
|
+
// against the inferred record schema. This assumption is not portable to
|
|
30
|
+
// dialects that error on incompatible path access.
|
|
31
|
+
|
|
32
|
+
import type {AtomicTypeDef, Dialect, FieldDef} from '@malloydata/malloy';
|
|
33
|
+
import {
|
|
34
|
+
TinyParser,
|
|
35
|
+
mkArrayTypeDef,
|
|
36
|
+
mkFieldDef,
|
|
37
|
+
pathToKey,
|
|
38
|
+
} from '@malloydata/malloy/internal';
|
|
39
|
+
|
|
40
|
+
export type NestedColumnKind = 'variant' | 'array' | 'object';
|
|
41
|
+
|
|
42
|
+
export interface NestedColumn {
|
|
43
|
+
kind: NestedColumnKind;
|
|
44
|
+
name: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export type Segment = {kind: 'name'; name: string} | {kind: 'array'};
|
|
48
|
+
|
|
49
|
+
export type Shape =
|
|
50
|
+
| {kind: 'object'}
|
|
51
|
+
| {kind: 'array'}
|
|
52
|
+
| {kind: 'leaf'; type: string}
|
|
53
|
+
| {kind: 'variant'};
|
|
54
|
+
|
|
55
|
+
export type PrefixKey = string;
|
|
56
|
+
|
|
57
|
+
export interface Children {
|
|
58
|
+
elem?: PrefixKey;
|
|
59
|
+
named: Map<string, PrefixKey>;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface VariantSchemaState {
|
|
63
|
+
children: Map<PrefixKey, Children>;
|
|
64
|
+
seededTopLevels: Set<PrefixKey>;
|
|
65
|
+
shapes: Map<PrefixKey, Shape>;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export class PathParser extends TinyParser {
|
|
69
|
+
constructor(pathName: string) {
|
|
70
|
+
super(pathName, {
|
|
71
|
+
quoted: /^'(\\'|[^'])*'/,
|
|
72
|
+
array_of: /^\[\*]/,
|
|
73
|
+
char: /^[[.\]]/,
|
|
74
|
+
number: /^\d+/,
|
|
75
|
+
word: /^\w+/,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
getName() {
|
|
80
|
+
const word = this.match('word');
|
|
81
|
+
if (word) return word.text;
|
|
82
|
+
if (this.match('[')) {
|
|
83
|
+
const quotedName = this.expect('quoted');
|
|
84
|
+
this.expect(']');
|
|
85
|
+
return quotedName.text;
|
|
86
|
+
}
|
|
87
|
+
throw this.parseError('Expected column name');
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
segments(): Segment[] {
|
|
91
|
+
const segments: Segment[] = [{kind: 'name', name: this.getName()}];
|
|
92
|
+
while (!this.eof()) {
|
|
93
|
+
if (this.match('.')) {
|
|
94
|
+
segments.push({kind: 'name', name: this.expect('word').text});
|
|
95
|
+
} else if (this.match('array_of')) {
|
|
96
|
+
segments.push({kind: 'array'});
|
|
97
|
+
} else if (this.match('[')) {
|
|
98
|
+
const quoted = this.expect('quoted');
|
|
99
|
+
this.expect(']');
|
|
100
|
+
segments.push({kind: 'name', name: quoted.text});
|
|
101
|
+
} else {
|
|
102
|
+
throw this.parseError(`Unexpected ${this.peek().type}`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return segments;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function createVariantSchemaState(): VariantSchemaState {
|
|
110
|
+
return {
|
|
111
|
+
children: new Map(),
|
|
112
|
+
seededTopLevels: new Set(),
|
|
113
|
+
shapes: new Map(),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// The single consistency-policy point: any shape conflict across samples
|
|
118
|
+
// collapses to `variant`, and `variant` is absorbing (monotonic).
|
|
119
|
+
export function mergeShape(
|
|
120
|
+
existing: Shape | undefined,
|
|
121
|
+
incoming: Shape
|
|
122
|
+
): Shape {
|
|
123
|
+
if (existing === undefined) {
|
|
124
|
+
return incoming;
|
|
125
|
+
}
|
|
126
|
+
if (existing.kind === 'variant' || incoming.kind === 'variant') {
|
|
127
|
+
return {kind: 'variant'};
|
|
128
|
+
}
|
|
129
|
+
if (existing.kind !== incoming.kind) {
|
|
130
|
+
return {kind: 'variant'};
|
|
131
|
+
}
|
|
132
|
+
if (
|
|
133
|
+
existing.kind === 'leaf' &&
|
|
134
|
+
incoming.kind === 'leaf' &&
|
|
135
|
+
existing.type !== incoming.type
|
|
136
|
+
) {
|
|
137
|
+
return {kind: 'variant'};
|
|
138
|
+
}
|
|
139
|
+
return existing;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export function seedTopLevelShape(
|
|
143
|
+
state: VariantSchemaState,
|
|
144
|
+
nestedColumn: NestedColumn
|
|
145
|
+
): void {
|
|
146
|
+
const key = prefixKey([{kind: 'name', name: nestedColumn.name}]);
|
|
147
|
+
if (nestedColumn.kind === 'array' || nestedColumn.kind === 'object') {
|
|
148
|
+
state.seededTopLevels.add(key);
|
|
149
|
+
state.shapes.set(key, {kind: nestedColumn.kind});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function accumulateVariantPath(
|
|
154
|
+
state: VariantSchemaState,
|
|
155
|
+
segments: Segment[],
|
|
156
|
+
fieldType: string
|
|
157
|
+
): void {
|
|
158
|
+
if (segments.length === 0) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
const topLevelKey = prefixKey(segments.slice(0, 1));
|
|
162
|
+
const topLevelShape = state.shapes.get(topLevelKey);
|
|
163
|
+
const topLevelIncoming =
|
|
164
|
+
segments.length === 1
|
|
165
|
+
? observedTypeToShape(fieldType)
|
|
166
|
+
: nextSegmentToShape(segments[1]);
|
|
167
|
+
if (
|
|
168
|
+
state.seededTopLevels.has(topLevelKey) &&
|
|
169
|
+
topLevelShape &&
|
|
170
|
+
(topLevelShape.kind === 'array' || topLevelShape.kind === 'object') &&
|
|
171
|
+
topLevelShape.kind !== topLevelIncoming.kind
|
|
172
|
+
) {
|
|
173
|
+
// Rule 1: top-level ARRAY/OBJECT from DESCRIBE are authoritative.
|
|
174
|
+
// Ignore impossible sample rows rather than letting them override the seed.
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
let parentKey: PrefixKey | undefined;
|
|
178
|
+
for (let i = 0; i < segments.length; i++) {
|
|
179
|
+
const prefixSegments = segments.slice(0, i + 1);
|
|
180
|
+
const key = prefixKey(prefixSegments);
|
|
181
|
+
const impliedShape =
|
|
182
|
+
i === segments.length - 1
|
|
183
|
+
? observedTypeToShape(fieldType)
|
|
184
|
+
: nextSegmentToShape(segments[i + 1]);
|
|
185
|
+
state.shapes.set(key, mergeShape(state.shapes.get(key), impliedShape));
|
|
186
|
+
if (parentKey !== undefined) {
|
|
187
|
+
recordChild(state, parentKey, segments[i], key);
|
|
188
|
+
}
|
|
189
|
+
parentKey = key;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export function buildTopLevelField(
|
|
194
|
+
nestedColumn: NestedColumn,
|
|
195
|
+
state: VariantSchemaState,
|
|
196
|
+
dialect: Dialect
|
|
197
|
+
): FieldDef {
|
|
198
|
+
// Snowflake nested-schema inference follows these rules:
|
|
199
|
+
// - top-level ARRAY/OBJECT from DESCRIBE are authoritative
|
|
200
|
+
// - descendant paths imply ancestor shape
|
|
201
|
+
// - conflicting shapes degrade only that prefix to variant
|
|
202
|
+
// - every top-level nested column still produces a field
|
|
203
|
+
//
|
|
204
|
+
// Snowflake-specific semantic note: reconstructing object shape from
|
|
205
|
+
// descendant paths is safe because path access on an incompatible VARIANT
|
|
206
|
+
// value yields NULL rather than raising an error.
|
|
207
|
+
const key = prefixKey([{kind: 'name', name: nestedColumn.name}]);
|
|
208
|
+
const shape = state.shapes.get(key);
|
|
209
|
+
if (shape === undefined) {
|
|
210
|
+
// Top-level ARRAY with no usable descendants still stays queryable as
|
|
211
|
+
// array<variant>; top-level OBJECT/VARIANT degrades to opaque variant.
|
|
212
|
+
return mkFieldDef(
|
|
213
|
+
nestedColumn.kind === 'array'
|
|
214
|
+
? mkArrayTypeDef(opaqueVariantType())
|
|
215
|
+
: opaqueVariantType(),
|
|
216
|
+
nestedColumn.name
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
return mkFieldDef(buildTypeForKey(key, state, dialect), nestedColumn.name);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function buildTypeForKey(
|
|
223
|
+
key: PrefixKey,
|
|
224
|
+
state: VariantSchemaState,
|
|
225
|
+
dialect: Dialect
|
|
226
|
+
): AtomicTypeDef {
|
|
227
|
+
const shape = state.shapes.get(key);
|
|
228
|
+
if (shape === undefined || shape.kind === 'variant') {
|
|
229
|
+
return opaqueVariantType();
|
|
230
|
+
}
|
|
231
|
+
if (shape.kind === 'leaf') {
|
|
232
|
+
return dialect.sqlTypeToMalloyType(shape.type);
|
|
233
|
+
}
|
|
234
|
+
if (shape.kind === 'object') {
|
|
235
|
+
const namedChildren = state.children.get(key)?.named;
|
|
236
|
+
if (!namedChildren || namedChildren.size === 0) {
|
|
237
|
+
return opaqueVariantType();
|
|
238
|
+
}
|
|
239
|
+
return {
|
|
240
|
+
type: 'record',
|
|
241
|
+
fields: [...namedChildren.entries()].map(([childName, childKey]) =>
|
|
242
|
+
mkFieldDef(buildTypeForKey(childKey, state, dialect), childName)
|
|
243
|
+
),
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
const elemKey = state.children.get(key)?.elem;
|
|
247
|
+
const elementType = elemKey
|
|
248
|
+
? buildTypeForKey(elemKey, state, dialect)
|
|
249
|
+
: opaqueVariantType();
|
|
250
|
+
return mkArrayTypeDef(elementType);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function nextSegmentToShape(segment: Segment): Shape {
|
|
254
|
+
return segment.kind === 'array' ? {kind: 'array'} : {kind: 'object'};
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function observedTypeToShape(fieldType: string): Shape {
|
|
258
|
+
// Defensive: production callers only pass Snowflake TYPEOF() results, which
|
|
259
|
+
// are expected to be array/object or scalar leaf types, not "variant".
|
|
260
|
+
if (
|
|
261
|
+
fieldType === 'array' ||
|
|
262
|
+
fieldType === 'object' ||
|
|
263
|
+
fieldType === 'variant'
|
|
264
|
+
) {
|
|
265
|
+
return {kind: fieldType};
|
|
266
|
+
}
|
|
267
|
+
return {kind: 'leaf', type: fieldType};
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function recordChild(
|
|
271
|
+
state: VariantSchemaState,
|
|
272
|
+
parentKey: PrefixKey,
|
|
273
|
+
segment: Segment,
|
|
274
|
+
childKey: PrefixKey
|
|
275
|
+
): void {
|
|
276
|
+
let children = state.children.get(parentKey);
|
|
277
|
+
if (children === undefined) {
|
|
278
|
+
children = {named: new Map()};
|
|
279
|
+
state.children.set(parentKey, children);
|
|
280
|
+
}
|
|
281
|
+
if (segment.kind === 'array') {
|
|
282
|
+
children.elem = childKey;
|
|
283
|
+
} else {
|
|
284
|
+
children.named.set(segment.name, childKey);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function opaqueVariantType(): AtomicTypeDef {
|
|
289
|
+
return {type: 'sql native', rawType: 'variant'};
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function prefixKey(segments: Segment[]): PrefixKey {
|
|
293
|
+
const [first, ...rest] = segments;
|
|
294
|
+
if (first?.kind !== 'name') {
|
|
295
|
+
throw new Error('Snowflake schema path must start with a named segment');
|
|
296
|
+
}
|
|
297
|
+
return pathToKey(
|
|
298
|
+
first.name,
|
|
299
|
+
rest.map(segment => (segment.kind === 'array' ? '[*]' : segment.name))
|
|
300
|
+
);
|
|
301
|
+
}
|