Skip to content

Commit e75699e

Browse files
HCK-14054: make parentheses optional in table properties (#159)
Co-authored-by: chulanovskyi-bs <56116665+chulanovskyi-bs@users.noreply.github.com>
1 parent 3ed6d34 commit e75699e

3 files changed

Lines changed: 25 additions & 19 deletions

File tree

forward_engineering/helpers/alterScriptHelpers/common.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const _ = require('lodash');
2+
const { stripParentheses } = require('../generalHelper');
23

34
const getDifferentItems = (newItems = [], oldItems = []) => {
45
const intersection = _.intersectionWith(newItems, oldItems, _.isEqual);
@@ -10,24 +11,20 @@ const getDifferentItems = (newItems = [], oldItems = []) => {
1011

1112
const hydrateTableProperties = ({ new: newItems, old: oldItems }, name, commentState) => {
1213
const hydrateProperties = properties => (properties || '').split(',').map(prop => prop.trim());
13-
const prepareProperties = properties =>
14-
properties
15-
.filter(Boolean)
16-
.map(property => property.replace(/(\S+)=([\s\S]+)/, `'$1'='$2'`))
17-
.join(',\n');
14+
const prepareProperties = properties => properties.filter(Boolean).join(', ');
1815

1916
const isCommentChanged = !_.isEqual(commentState?.new, commentState?.old);
20-
const addCommentProp = isCommentChanged && commentState?.new ? `comment=${commentState?.new}` : '';
21-
const dropCommentProp = isCommentChanged && !commentState?.new ? `comment` : '';
17+
const addCommentProp = isCommentChanged && commentState?.new ? `'comment'='${commentState?.new}'` : '';
18+
const dropCommentProp = isCommentChanged && !commentState?.new ? `'comment'` : '';
2219

2320
const preparePropertiesName = properties =>
2421
properties
2522
.filter(Boolean)
26-
.map(prop => `'${prop.replace(/(=\S+)/, '')}'`)
23+
.map(prop => `${prop.split('=')[0]}`)
2724
.join(', ');
2825

29-
const newHydrateItems = hydrateProperties(newItems);
30-
const oldHydrateItems = hydrateProperties(oldItems);
26+
const newHydrateItems = hydrateProperties(stripParentheses(newItems));
27+
const oldHydrateItems = hydrateProperties(stripParentheses(oldItems));
3128

3229
const { add, drop } = getDifferentItems(newHydrateItems, oldHydrateItems);
3330

forward_engineering/helpers/generalHelper.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,20 @@ const encodeStringLiteral = (str = '') => {
135135

136136
const isDeactivatedStatement = statement => statement.startsWith(BEFORE_DEACTIVATED_STATEMENT);
137137

138+
const stripParentheses = str => {
139+
if (typeof str !== 'string') {
140+
return str;
141+
}
142+
let result = str.trim();
143+
if (result.startsWith('(')) {
144+
result = result.slice(1);
145+
}
146+
if (result.endsWith(')')) {
147+
result = result.slice(0, -1);
148+
}
149+
return result;
150+
};
151+
138152
module.exports = {
139153
buildStatement,
140154
getName,
@@ -147,4 +161,5 @@ module.exports = {
147161
removeRedundantTrailingCommaFromStatement,
148162
encodeStringLiteral,
149163
isDeactivatedStatement,
164+
stripParentheses,
150165
};

forward_engineering/helpers/tableHelper.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const {
77
commentDeactivatedInlineKeys,
88
removeRedundantTrailingCommaFromStatement,
99
encodeStringLiteral,
10+
stripParentheses,
1011
} = require('./generalHelper');
1112
const { getColumnsStatement, getColumnStatementParts, getColumns } = require('./columnHelper');
1213
const keyHelper = require('./keyHelper');
@@ -65,7 +66,7 @@ const getCreateStatement = ({
6566
)(skewedStatement, skewedStatement)(rowFormatStatement, `ROW FORMAT ${rowFormatStatement}`)(
6667
storedAsStatement,
6768
storedAsStatement,
68-
)(location, `LOCATION "${location}"`)(tableProperties, `TBLPROPERTIES ${tableProperties}`)(
69+
)(location, `LOCATION "${location}"`)(tableProperties, `TBLPROPERTIES (${tableProperties})`)(
6970
selectStatement,
7071
`AS ${selectStatement}`,
7172
)(true, ';')();
@@ -166,13 +167,6 @@ const removePartitions = (columns, partitions) => {
166167
);
167168
};
168169

169-
const prepareTableProperties = (tableProperties = '') => {
170-
const regex = /^\((?<properties>[\s\S]*)\)$/;
171-
const match = regex.exec(tableProperties);
172-
const properties = match?.groups.properties || '';
173-
return properties.trim() ? tableProperties : '';
174-
};
175-
176170
const getSkewedKeyStatement = (skewedKeys, skewedOn, asDirectories, deactivatedColumnNames, isParentItemActivated) => {
177171
const getStatement = keysString =>
178172
`SKEWED BY (${keysString}) ON ${skewedOn} ${asDirectories ? 'STORED AS DIRECTORIES' : ''}`;
@@ -289,7 +283,7 @@ const getTableStatement = (
289283
rowFormatStatement: getRowFormat(tableData),
290284
storedAsStatement: getStoredAsStatement(tableData),
291285
location: tableData.location,
292-
tableProperties: prepareTableProperties(tableData.tableProperties),
286+
tableProperties: stripParentheses(tableData.tableProperties),
293287
selectStatement: '',
294288
isActivated: isTableActivated,
295289
ifNotExist: tableData.ifNotExist,

0 commit comments

Comments
 (0)