Skip to content

Commit

Permalink
HCK-7465: Add clustering options to Materialized view
Browse files Browse the repository at this point in the history
  • Loading branch information
Nightlngale committed Aug 7, 2024
1 parent 1ef3b77 commit d79e612
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
3 changes: 2 additions & 1 deletion forward_engineering/configs/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 11 additions & 1 deletion forward_engineering/ddlProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
});
},

Expand Down Expand Up @@ -877,6 +886,7 @@ module.exports = (baseProvider, options, app) => {
secure: firstTab.secure,
materialized: firstTab.materialized,
fullName,
clusteringKey: firstTab.clusteringKey,
viewTags: firstTab.viewTags ?? [],
};
},
Expand Down
34 changes: 34 additions & 0 deletions forward_engineering/utils/getClusteringKey.js
Original file line number Diff line number Diff line change
@@ -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,
};
17 changes: 17 additions & 0 deletions properties_pane/view_level/viewLevelConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit d79e612

Please sign in to comment.