Skip to content

Commit

Permalink
a few less snowlake errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mtoy-googly-moogly committed Nov 20, 2024
1 parent 9a0ae97 commit 0c03f46
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
39 changes: 31 additions & 8 deletions packages/malloy/src/dialect/snowflake/snowflake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,10 +458,34 @@ ${indent(sql)}
malloyTypeToSQLType(malloyType: AtomicTypeDef): string {
if (malloyType.type === 'number') {
if (malloyType.numberType === 'integer') {
return 'integer';
return 'INTEGER';
} else {
return 'double';
return 'DOUBLE';
}
} else if (
malloyType.type === 'record' ||
(malloyType.type === 'array' &&
malloyType.elementTypeDef.type === 'record_element')
) {
const sqlFields = malloyType.fields.reduce((ret, f) => {
if (isAtomic(f)) {
const name = f.as ?? f.name;
const oneSchema = `${this.sqlMaybeQuoteIdentifier(
name
)} ${this.malloyTypeToSQLType(f)}`;
ret.push(oneSchema);
}
return ret;
}, [] as string[]);
const recordScehma = `OBJECT(${sqlFields.join(',')})`;
return malloyType.type === 'record'
? recordScehma
: `ARRAY(${recordScehma})`;
} else if (
malloyType.type === 'array' &&
malloyType.elementTypeDef.type !== 'record_element'
) {
return `ARRAY(${this.malloyTypeToSQLType(malloyType.elementTypeDef)})`;
}
return malloyType.type;
}
Expand Down Expand Up @@ -496,23 +520,22 @@ ${indent(sql)}

sqlLiteralRecord(lit: RecordLiteralNode): string {
const rowVals: string[] = [];
const rowTypes: string[] = [];
for (const f of lit.typeDef.fields) {
if (isAtomic(f)) {
const name = f.as ?? f.name;
const propName = `'${name}'`;
const propVal = lit.kids[name].sql ?? 'internal-error-record-literal';
rowVals.push(`${propName}:${propVal}`);
rowTypes.push(
`${this.sqlMaybeQuoteIdentifier(name)} ${this.malloyTypeToSQLType(f)}`
);
}
}
return `{${rowVals.join(',')}}::OBJECT(${rowTypes.join(',')})`;
return `{${rowVals.join(',')}}::${this.malloyTypeToSQLType(lit.typeDef)}`;
}

sqlLiteralArray(lit: ArrayLiteralNode): string {
const array = lit.kids.values.map(val => val.sql);
return `[${array.join(',')}]`;
const arraySchema = `[${array.join(',')}]`;
return lit.typeDef.elementTypeDef.type === 'record_element'
? `${arraySchema}::${this.malloyTypeToSQLType(lit.typeDef)}`
: arraySchema;
}
}
6 changes: 5 additions & 1 deletion test/src/databases/all/compound-atomic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe.each(runtimes.runtimeList)(
'mysql': 'JSON_LENGTH',
'snowflake': 'ARRAY_SIZE',
};
const empty = `${databaseName}.sql("SELECT 0")`;
const empty = `${databaseName}.sql("SELECT 0 as z")`;
function arraySelectVal(...val: Number[]): string {
const literal: ArrayLiteralNode = {
node: 'arrayLiteral',
Expand Down Expand Up @@ -408,3 +408,7 @@ describe.each(runtimes.runtimeList)(
});
}
);

afterAll(async () => {
await runtimes.closeAll();
});

0 comments on commit 0c03f46

Please sign in to comment.