diff --git a/forward_engineering/configs/templates.js b/forward_engineering/configs/templates.js index f34ff843..7a98ecac 100644 --- a/forward_engineering/configs/templates.js +++ b/forward_engineering/configs/templates.js @@ -26,7 +26,8 @@ module.exports = { createView: 'CREATE${secure}${materialized} VIEW IF NOT EXISTS ${name} (\n' + '\t${column_list}\n' + - ')\n${copy_grants}${comment}${tag}AS ${select_statement};\n', + ')\n${copy_grants}${comment}${tag}${clustering} AS\n${select_statement};\n', + createUDF: 'CREATE${orReplace} FUNCTION ${name}(${arguments})\n\tRETURNS ${returnType}${notNull}\n\tLANGUAGE ${language}${parameters}${comment}\n\tAS ${body};\n', createProcedure: diff --git a/forward_engineering/ddlProvider.js b/forward_engineering/ddlProvider.js index 75e11b96..431bc23f 100644 --- a/forward_engineering/ddlProvider.js +++ b/forward_engineering/ddlProvider.js @@ -18,6 +18,7 @@ const { prepareCollectionStageCopyOptions, } = require('./helpers/alterScriptHelpers/common'); const { escapeString } = require('./utils/escapeString'); +const { getClusteringKey } = require('./utils/getClusteringKey'); const DEFAULT_SNOWFLAKE_SEQUENCE_START = 1; const DEFAULT_SNOWFLAKE_SEQUENCE_INCREMENT = 1; @@ -530,15 +531,23 @@ module.exports = (baseProvider, options, app) => { indent: '', }); + const clustering = viewData.materialized + ? getClusteringKey({ + clusteringKey: viewData.clusteringKey, + isParentActivated: isActivated, + }) + : undefined; + return assignTemplates(templates.createView, { secure: viewData.secure ? ' SECURE' : '', materialized: viewData.materialized ? ' MATERIALIZED' : '', name: getFullName(schemaName, viewData.name), column_list: viewColumnsToString(columnList, isActivated), copy_grants: viewData.copyGrants ? 'COPY GRANTS\n' : '', - comment: viewData.comment ? 'COMMENT=' + escapeString(scriptFormat, viewData.comment) + '\n' : '', + comment: viewData.comment ? 'COMMENT=' + escapeString(scriptFormat, viewData.comment) : '', select_statement: selectStatement, tag: tagStatement ? tagStatement + '\n' : '', + clustering, }); }, @@ -877,6 +886,7 @@ module.exports = (baseProvider, options, app) => { secure: firstTab.secure, materialized: firstTab.materialized, fullName, + clusteringKey: firstTab.clusteringKey, viewTags: firstTab.viewTags ?? [], }; }, diff --git a/forward_engineering/utils/getClusteringKey.js b/forward_engineering/utils/getClusteringKey.js new file mode 100644 index 00000000..ed1940c1 --- /dev/null +++ b/forward_engineering/utils/getClusteringKey.js @@ -0,0 +1,34 @@ +const mapName = key => `"${key.name}"`; + +const getClusteringKey = ({ clusteringKey, isParentActivated }) => { + if (!Array.isArray(clusteringKey) || clusteringKey.length === 0) { + return ''; + } + + const activated = clusteringKey + .filter(key => key.isActivated) + .map(mapName) + .join(', '); + const deActivated = clusteringKey + .filter(key => !key.isActivated) + .map(mapName) + .join(', '); + + if (!isParentActivated) { + return `\nCLUSTER BY (${clusteringKey.map(mapName).join(', ')})`; + } + + if (activated.length === 0) { + return `\n// CLUSTER BY (${deActivated})`; + } + + if (deActivated.length === 0) { + return `\nCLUSTER BY (${activated})`; + } + + return `\nCLUSTER BY (${activated}) //${deActivated}`; +}; + +module.exports = { + getClusteringKey, +}; diff --git a/properties_pane/view_level/viewLevelConfig.json b/properties_pane/view_level/viewLevelConfig.json index 5c6e01af..dfdab135 100644 --- a/properties_pane/view_level/viewLevelConfig.json +++ b/properties_pane/view_level/viewLevelConfig.json @@ -56,6 +56,23 @@ "propertyTooltip": "is Materialized View", "propertyType": "checkbox" }, + { + "propertyName": "Cluster by", + "propertyKeyword": "clusteringKey", + "dependency": { + "key": "materialized", + "value": true + }, + "propertyType": "fieldList", + "disabledItemStrategy": "default", + "abbr": "CK", + "setPrimaryKey": false, + "template": "orderedList", + "isCompositeKey": true, + "templateOptions": { + "maxFields": 4 + } + }, { "propertyName": "Secure", "propertyKeyword": "secure",