Skip to content

feat(snowflake-driver): Support execute sql via custom procedure #9579

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
20 changes: 20 additions & 0 deletions packages/cubejs-backend-shared/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1625,6 +1625,26 @@ const variables: Record<string, (...args: any) => any> = {
return false;
}
},

snowflakeExecutionProcedure: ({
dataSource
}: {
dataSource: string,
}) => (
process.env[
keyByDataSource('CUBEJS_DB_SNOWFLAKE_EXECUTION_PROCEDURE', dataSource)
]
),

snowflakeInformationSchemaProcedure: ({
dataSource
}: {
dataSource: string,
}) => (
process.env[
keyByDataSource('CUBEJS_DB_SNOWFLAKE_INFORMATION_SCHEMA_PROCEDURE', dataSource)
]
),
/** ****************************************************************
* Presto Driver *
***************************************************************** */
Expand Down
33 changes: 33 additions & 0 deletions packages/cubejs-snowflake-driver/src/SnowflakeDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@
executionTimeout?: number,
identIgnoreCase?: boolean,
application: string,
snowflakeExecutionProcedure?: string,
snowflakeInformationSchemaProcedure?: string,
readOnly?: boolean,

/**
Expand Down Expand Up @@ -208,6 +210,8 @@
'CUBEJS_DB_SNOWFLAKE_PRIVATE_KEY_PATH',
'CUBEJS_DB_SNOWFLAKE_PRIVATE_KEY_PASS',
'CUBEJS_DB_SNOWFLAKE_QUOTED_IDENTIFIERS_IGNORE_CASE',
'CUBEJS_DB_SNOWFLAKE_EXECUTION_PROCEDURE',
'CUBEJS_DB_SNOWFLAKE_INFORMATION_SCHEMA_PROCEDURE',
];
}

Expand Down Expand Up @@ -296,6 +300,8 @@
identIgnoreCase: getEnv('snowflakeQuotedIdentIgnoreCase', { dataSource }),
exportBucketCsvEscapeSymbol: getEnv('dbExportBucketCsvEscapeSymbol', { dataSource }),
application: 'CubeDev_Cube',
snowflakeExecutionProcedure: getEnv('snowflakeExecutionProcedure', { dataSource }),
snowflakeInformationSchemaProcedure: getEnv('snowflakeInformationSchemaProcedure', { dataSource }),
...config
};
}
Expand Down Expand Up @@ -879,6 +885,24 @@
values?: unknown[],
rehydrate: boolean = true
): Promise<R> {
if (
this.config.snowflakeExecutionProcedure &&
query.toUpperCase().startsWith('SELECT')
) {
const escapedQuery = query.replace(/'/g, "\\'");

Check failure

Code scanning / CodeQL

Incomplete string escaping or encoding High

This does not escape backslash characters in the input.

Copilot Autofix

AI 7 days ago

To fix the issue, we need to ensure that backslashes in the query string are escaped before escaping single quotes. This can be achieved by first replacing all backslashes (\) with double backslashes (\\), and then replacing all single quotes (') with escaped single quotes (\'). This ensures that both backslashes and single quotes are properly escaped.

The best way to implement this fix is to chain two replace operations on the query string:

  1. Replace all backslashes (\) with double backslashes (\\).
  2. Replace all single quotes (') with escaped single quotes (\').

This change should be applied to line 892 in the execute method.


Suggested changeset 1
packages/cubejs-snowflake-driver/src/SnowflakeDriver.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/packages/cubejs-snowflake-driver/src/SnowflakeDriver.ts b/packages/cubejs-snowflake-driver/src/SnowflakeDriver.ts
--- a/packages/cubejs-snowflake-driver/src/SnowflakeDriver.ts
+++ b/packages/cubejs-snowflake-driver/src/SnowflakeDriver.ts
@@ -891,3 +891,3 @@
     ) {
-      const escapedQuery = query.replace(/'/g, "\\'");
+      const escapedQuery = query.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
       const serializedBinds = `ARRAY_CONSTRUCT(${(values ?? [])
EOF
@@ -891,3 +891,3 @@
) {
const escapedQuery = query.replace(/'/g, "\\'");
const escapedQuery = query.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
const serializedBinds = `ARRAY_CONSTRUCT(${(values ?? [])
Copilot is powered by AI and may make mistakes. Always verify output.
const serializedBinds = `ARRAY_CONSTRUCT(${(values ?? [])
.map((v) =>
typeof v === 'string'
? `'${String(v).replace(/'/g, "''")}'`
: v === null || v === undefined
? "NULL"
: v
)
.join(", ")})`;

query = `CALL ${this.config.snowflakeExecutionProcedure}('${escapedQuery}', ${serializedBinds})`;
}

return new Promise((resolve, reject) => connection.execute({
sqlText: query,
binds: <string[] | undefined>values,
Expand Down Expand Up @@ -908,6 +932,15 @@
}

public informationSchemaQuery() {
if (this.config.snowflakeInformationSchemaProcedure) {
return `
SELECT COLUMN_NAME as "column_name",
TABLE_NAME as "table_name",
TABLE_SCHEMA as "table_schema",
DATA_TYPE as "data_type"
FROM TABLE(${this.config.snowflakeInformationSchemaProcedure}())`;
}

return `
SELECT COLUMNS.COLUMN_NAME as "column_name",
COLUMNS.TABLE_NAME as "table_name",
Expand Down
Loading