Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions packages/cubejs-snowflake-driver/src/SnowflakeDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,9 @@ export class SnowflakeDriver extends BaseDriver implements DriverInterface {
const [schema, name] = table.split('.');
const columns = await this.query<{
COLUMN_NAME: string,
DATA_TYPE: string
DATA_TYPE: string,
NUMERIC_PRECISION: number | null,
NUMERIC_SCALE: number | null
}[]>(
`SELECT COLUMNS.COLUMN_NAME,
CASE
Expand All @@ -708,7 +710,9 @@ export class SnowflakeDriver extends BaseDriver implements DriverInterface {
COLUMNS.DATA_TYPE = 'NUMBER'
THEN 'int'
ELSE COLUMNS.DATA_TYPE
END as DATA_TYPE
END as DATA_TYPE,
COLUMNS.NUMERIC_PRECISION,
COLUMNS.NUMERIC_SCALE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = ${this.param(0)} AND
Expand All @@ -718,10 +722,24 @@ export class SnowflakeDriver extends BaseDriver implements DriverInterface {
);
return columns.map(c => ({
name: c.COLUMN_NAME,
type: this.toGenericType(c.DATA_TYPE),
type: this.formatColumnType(c.DATA_TYPE, c.NUMERIC_PRECISION, c.NUMERIC_SCALE),
}));
}

/**
* Formats column type with precision and scale for NUMBER/DECIMAL types.
*/
private formatColumnType(dataType: string, precision: number | null, scale: number | null): string {
const genericType = this.toGenericType(dataType);

// For decimal types, include precision and scale if available
if (genericType === 'decimal' && precision !== null && scale !== null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be better to check also for precision > 0 and scale > 0 here....

return `decimal(${precision}, ${scale})`;
}

return genericType;
}

/**
* Returns export options clause.
*/
Expand Down Expand Up @@ -942,9 +960,16 @@ export class SnowflakeDriver extends BaseDriver implements DriverInterface {
};
if (column.isNumber()) {
// @ts-ignore
if (column.getScale() === 0) {
const scale = column.getScale();
// @ts-ignore
const precision = column.getPrecision();

if (scale === 0) {
type.type = 'int';
} else if (column.getScale() && column.getScale() <= 10) {
} else if (scale !== null && precision !== null) {
// Include precision and scale for decimal types
type.type = `decimal(${precision}, ${scale})`;
} else if (scale && scale <= 10) {
type.type = 'decimal';
} else {
type.type = this.toGenericType(column.getType());
Expand Down
Loading