@malloydata/db-snowflake 0.0.375 → 0.0.377
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 +144 -228
- package/dist/snowflake_connection.js.map +1 -1
- package/dist/snowflake_connection.spec.js +179 -14
- package/dist/snowflake_connection.spec.js.map +1 -1
- package/dist/snowflake_sample_strategy.spec.js +97 -0
- package/dist/snowflake_sample_strategy.spec.js.map +1 -0
- package/dist/snowflake_table_name.d.ts +19 -0
- package/dist/snowflake_table_name.js +80 -0
- package/dist/snowflake_table_name.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.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 +219 -15
- package/src/snowflake_connection.ts +218 -262
- package/src/snowflake_sample_strategy.spec.ts +130 -0
- package/src/snowflake_table_name.ts +94 -0
- package/src/snowflake_variant_schema.spec.ts +188 -0
- package/src/snowflake_variant_schema.ts +301 -0
- package/dist/snowflake_executor.spec.js +0 -89
- package/dist/snowflake_executor.spec.js.map +0 -1
- package/dist/snowflake_setup.spec.js +0 -76
- package/dist/snowflake_setup.spec.js.map +0 -1
- package/src/snowflake_executor.spec.ts +0 -103
- package/src/snowflake_setup.spec.ts +0 -56
- /package/dist/{snowflake_executor.spec.d.ts → snowflake_sample_strategy.spec.d.ts} +0 -0
- /package/dist/{snowflake_setup.spec.d.ts → snowflake_variant_schema.spec.d.ts} +0 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright Contributors to the Malloy project
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// Parses Snowflake table references of the form
|
|
7
|
+
// [database.][schema.]table
|
|
8
|
+
// where each part is either a bare identifier (case-insensitive, stored
|
|
9
|
+
// upper-cased in Snowflake's catalog) or a double-quoted identifier
|
|
10
|
+
// (case-sensitive, with `""` as the in-string escape for a literal `"`).
|
|
11
|
+
//
|
|
12
|
+
// The parser exists so the INFORMATION_SCHEMA.TABLES size probe can compare
|
|
13
|
+
// `table_schema` / `table_name` against the correct catalog value:
|
|
14
|
+
// bare names must be upper-cased, quoted names must be passed through
|
|
15
|
+
// verbatim. The old `split('.')` + regex approach got this wrong for
|
|
16
|
+
// quoted names and names with embedded dots.
|
|
17
|
+
|
|
18
|
+
import {TinyParser} from '@malloydata/malloy/internal';
|
|
19
|
+
|
|
20
|
+
export interface SnowflakeIdentPart {
|
|
21
|
+
/** Normalized catalog value — suitable for a SQL string literal. */
|
|
22
|
+
literal: string;
|
|
23
|
+
/** Re-emission form — suitable for a SQL identifier position. */
|
|
24
|
+
sql: string;
|
|
25
|
+
quoted: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface ParsedSnowflakeTableName {
|
|
29
|
+
database?: SnowflakeIdentPart;
|
|
30
|
+
schema?: SnowflakeIdentPart;
|
|
31
|
+
table: SnowflakeIdentPart;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
class SnowflakeTableNameParser extends TinyParser {
|
|
35
|
+
constructor(input: string) {
|
|
36
|
+
super(input, {
|
|
37
|
+
space: /^\s+/,
|
|
38
|
+
char: /^\./,
|
|
39
|
+
qstr: /^"(?:[^"]|"")*"/,
|
|
40
|
+
ident: /^[A-Za-z_][A-Za-z0-9_$]*/,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
parts(): SnowflakeIdentPart[] {
|
|
45
|
+
const out: SnowflakeIdentPart[] = [this.readPart()];
|
|
46
|
+
while (this.match('.')) {
|
|
47
|
+
out.push(this.readPart());
|
|
48
|
+
}
|
|
49
|
+
if (!this.eof()) {
|
|
50
|
+
throw this.parseError(`Unexpected ${this.peek().type}`);
|
|
51
|
+
}
|
|
52
|
+
return out;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
private readPart(): SnowflakeIdentPart {
|
|
56
|
+
const quoted = this.match('qstr');
|
|
57
|
+
if (quoted) {
|
|
58
|
+
// qstr strips outer quotes; `""` inside is the Snowflake escape.
|
|
59
|
+
const literal = quoted.text.replace(/""/g, '"');
|
|
60
|
+
return {
|
|
61
|
+
literal,
|
|
62
|
+
sql: `"${literal.replace(/"/g, '""')}"`,
|
|
63
|
+
quoted: true,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
const ident = this.match('ident');
|
|
67
|
+
if (ident) {
|
|
68
|
+
const literal = ident.text.toUpperCase();
|
|
69
|
+
return {literal, sql: literal, quoted: false};
|
|
70
|
+
}
|
|
71
|
+
throw this.parseError('Expected identifier');
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Parse a Snowflake table reference into 1-3 identifier parts. Returns
|
|
77
|
+
* undefined when the input is not a well-formed `[db.][schema.]table`
|
|
78
|
+
* reference — callers should treat that as "unknown shape" and skip
|
|
79
|
+
* metadata-driven optimizations rather than guessing.
|
|
80
|
+
*/
|
|
81
|
+
export function parseSnowflakeTableName(
|
|
82
|
+
src: string
|
|
83
|
+
): ParsedSnowflakeTableName | undefined {
|
|
84
|
+
let parts: SnowflakeIdentPart[];
|
|
85
|
+
try {
|
|
86
|
+
parts = new SnowflakeTableNameParser(src).parts();
|
|
87
|
+
} catch {
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
if (parts.length < 1 || parts.length > 3) return undefined;
|
|
91
|
+
if (parts.length === 1) return {table: parts[0]};
|
|
92
|
+
if (parts.length === 2) return {schema: parts[0], table: parts[1]};
|
|
93
|
+
return {database: parts[0], schema: parts[1], table: parts[2]};
|
|
94
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*
|
|
3
|
-
* Copyright 2023 Google LLC
|
|
4
|
-
*
|
|
5
|
-
* Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
-
* a copy of this software and associated documentation files
|
|
7
|
-
* (the "Software"), to deal in the Software without restriction,
|
|
8
|
-
* including without limitation the rights to use, copy, modify, merge,
|
|
9
|
-
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
10
|
-
* and to permit persons to whom the Software is furnished to do so,
|
|
11
|
-
* subject to the following conditions:
|
|
12
|
-
*
|
|
13
|
-
* The above copyright notice and this permission notice shall be
|
|
14
|
-
* included in all copies or substantial portions of the Software.
|
|
15
|
-
*
|
|
16
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
-
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
-
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
19
|
-
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
20
|
-
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
-
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
22
|
-
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
-
*/
|
|
24
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
-
const snowflake_executor_1 = require("./snowflake_executor");
|
|
26
|
-
const test_1 = require("@malloydata/malloy/test");
|
|
27
|
-
const [describe] = (0, test_1.describeIfDatabaseAvailable)(['snowflake']);
|
|
28
|
-
class SnowflakeExecutorTestSetup {
|
|
29
|
-
constructor(executor) {
|
|
30
|
-
this.executor_ = executor;
|
|
31
|
-
}
|
|
32
|
-
async runBatch(sqlText) {
|
|
33
|
-
let ret = [];
|
|
34
|
-
await (async () => {
|
|
35
|
-
const rows = await this.executor_.batch(sqlText);
|
|
36
|
-
return rows;
|
|
37
|
-
})().then((rows) => {
|
|
38
|
-
ret = rows;
|
|
39
|
-
});
|
|
40
|
-
return ret;
|
|
41
|
-
}
|
|
42
|
-
async runStreaming(sqlText, queryOptions) {
|
|
43
|
-
const rows = [];
|
|
44
|
-
await (async () => {
|
|
45
|
-
for await (const row of await this.executor_.stream(sqlText, queryOptions)) {
|
|
46
|
-
rows.push(row);
|
|
47
|
-
}
|
|
48
|
-
})();
|
|
49
|
-
return rows;
|
|
50
|
-
}
|
|
51
|
-
async done() {
|
|
52
|
-
await this.executor_.done();
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
describe('db:SnowflakeExecutor', () => {
|
|
56
|
-
let db;
|
|
57
|
-
let query;
|
|
58
|
-
beforeAll(() => {
|
|
59
|
-
const connOptions = snowflake_executor_1.SnowflakeExecutor.getConnectionOptionsFromEnv() ||
|
|
60
|
-
snowflake_executor_1.SnowflakeExecutor.getConnectionOptionsFromToml();
|
|
61
|
-
const executor = new snowflake_executor_1.SnowflakeExecutor(connOptions);
|
|
62
|
-
db = new SnowflakeExecutorTestSetup(executor);
|
|
63
|
-
query = `
|
|
64
|
-
select
|
|
65
|
-
*
|
|
66
|
-
from
|
|
67
|
-
(
|
|
68
|
-
values
|
|
69
|
-
(1, 'one'),
|
|
70
|
-
(2, 'two'),
|
|
71
|
-
(3, 'three'),
|
|
72
|
-
(4, 'four'),
|
|
73
|
-
(5, 'five')
|
|
74
|
-
);
|
|
75
|
-
`;
|
|
76
|
-
});
|
|
77
|
-
afterAll(async () => {
|
|
78
|
-
await db.done();
|
|
79
|
-
});
|
|
80
|
-
it('verifies batch execute', async () => {
|
|
81
|
-
const rows = await db.runBatch(query);
|
|
82
|
-
expect(rows.length).toBe(5);
|
|
83
|
-
});
|
|
84
|
-
it('verifies stream iterable', async () => {
|
|
85
|
-
const rows = await db.runStreaming(query, { rowLimit: 2 });
|
|
86
|
-
expect(rows.length).toBe(2);
|
|
87
|
-
});
|
|
88
|
-
});
|
|
89
|
-
//# sourceMappingURL=snowflake_executor.spec.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"snowflake_executor.spec.js","sourceRoot":"","sources":["../src/snowflake_executor.spec.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;;AAEH,6DAAuD;AAEvD,kDAAoE;AAEpE,MAAM,CAAC,QAAQ,CAAC,GAAG,IAAA,kCAA2B,EAAC,CAAC,WAAW,CAAC,CAAC,CAAC;AAE9D,MAAM,0BAA0B;IAE9B,YAAY,QAA2B;QACrC,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC;IAC5B,CAAC;IAED,KAAK,CAAC,QAAQ,CAAC,OAAe;QAC5B,IAAI,GAAG,GAAc,EAAE,CAAC;QACxB,MAAM,CAAC,KAAK,IAAI,EAAE;YAChB,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;YACjD,OAAO,IAAI,CAAC;QACd,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,IAAe,EAAE,EAAE;YAC5B,GAAG,GAAG,IAAI,CAAC;QACb,CAAC,CAAC,CAAC;QACH,OAAO,GAAG,CAAC;IACb,CAAC;IAED,KAAK,CAAC,YAAY,CAAC,OAAe,EAAE,YAA4B;QAC9D,MAAM,IAAI,GAAc,EAAE,CAAC;QAC3B,MAAM,CAAC,KAAK,IAAI,EAAE;YAChB,IAAI,KAAK,EAAE,MAAM,GAAG,IAAI,MAAM,IAAI,CAAC,SAAS,CAAC,MAAM,CACjD,OAAO,EACP,YAAY,CACb,EAAE,CAAC;gBACF,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YACjB,CAAC;QACH,CAAC,CAAC,EAAE,CAAC;QACL,OAAO,IAAI,CAAC;IACd,CAAC;IAED,KAAK,CAAC,IAAI;QACR,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC;IAC9B,CAAC;CACF;AAED,QAAQ,CAAC,sBAAsB,EAAE,GAAG,EAAE;IACpC,IAAI,EAA8B,CAAC;IACnC,IAAI,KAAa,CAAC;IAElB,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,WAAW,GACf,sCAAiB,CAAC,2BAA2B,EAAE;YAC/C,sCAAiB,CAAC,4BAA4B,EAAE,CAAC;QACnD,MAAM,QAAQ,GAAG,IAAI,sCAAiB,CAAC,WAAW,CAAC,CAAC;QACpD,EAAE,GAAG,IAAI,0BAA0B,CAAC,QAAQ,CAAC,CAAC;QAC9C,KAAK,GAAG;;;;;;;;;;;;KAYP,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,QAAQ,CAAC,KAAK,IAAI,EAAE;QAClB,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC;IAClB,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,wBAAwB,EAAE,KAAK,IAAI,EAAE;QACtC,MAAM,IAAI,GAAG,MAAM,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QACtC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAC9B,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,0BAA0B,EAAE,KAAK,IAAI,EAAE;QACxC,MAAM,IAAI,GAAG,MAAM,EAAE,CAAC,YAAY,CAAC,KAAK,EAAE,EAAC,QAAQ,EAAE,CAAC,EAAC,CAAC,CAAC;QACzD,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAC9B,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|