Skip to content

Commit

Permalink
HCK-7465: Add clustering options to the Materialized view (#130)
Browse files Browse the repository at this point in the history
* HCK-7465: Add clustering options to Materialized view

* HCK-7465: update formatting

* HCK-7465: resolve comments
  • Loading branch information
Nightlngale authored Aug 8, 2024
1 parent 1ef3b77 commit cbef87a
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion forward_engineering/configs/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ 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 ${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
11 changes: 10 additions & 1 deletion forward_engineering/ddlProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,15 +530,23 @@ module.exports = (baseProvider, options, app) => {
indent: '',
});

const clustering = viewData.materialized
? keyHelper.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)}\n` : '',
select_statement: selectStatement,
tag: tagStatement ? tagStatement + '\n' : '',
clustering,
});
},

Expand Down Expand Up @@ -877,6 +885,7 @@ module.exports = (baseProvider, options, app) => {
secure: firstTab.secure,
materialized: firstTab.materialized,
fullName,
clusteringKey: firstTab.clusteringKey,
viewTags: firstTab.viewTags ?? [],
};
},
Expand Down
1 change: 1 addition & 0 deletions forward_engineering/helpers/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ module.exports = app => {
getEntityName,
getFullName,
getDbName,
addQuotes,
getGroupItemsByCompMode,
};
};
38 changes: 38 additions & 0 deletions forward_engineering/helpers/keyHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const _ = require('lodash');

module.exports = app => {
const { clean } = app.require('@hackolade/ddl-fe-utils').general;
const { addQuotes } = require('./general')(app);

const mapProperties = (jsonSchema, iteratee) => {
return Object.entries(jsonSchema.properties).map(iteratee);
Expand Down Expand Up @@ -131,7 +132,44 @@ module.exports = app => {
];
};

/**
* @typedef {{ isActivated: boolean, name: string }} ClusteringKey
* @param {{ clusteringKey: ClusteringKey[], isParentActivated: boolean }} clusteringKeyArgs
* @returns {string}
*/
const getClusteringKey = ({ clusteringKey, isParentActivated }) => {
if (!Array.isArray(clusteringKey) || clusteringKey.length === 0) {
return '';
}

const mapName = ({ name }) => addQuotes(name);

const activated = clusteringKey
.filter(key => key.isActivated)
.map(mapName)
.join(', ');
const deactivated = clusteringKey
.filter(key => !key.isActivated)
.map(mapName)
.join(', ');

if (!isParentActivated) {
return `CLUSTER BY (${clusteringKey.map(mapName).join(', ')})\n`;
}

if (activated.length === 0) {
return `// CLUSTER BY (${deactivated})\n`;
}

if (deactivated.length === 0) {
return `CLUSTER BY (${activated})\n`;
}

return `CLUSTER BY (${activated}) //${deactivated}\n`;
};

return {
getTableKeyConstraints,
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 cbef87a

Please sign in to comment.