Skip to content

Commit 2f0ad04

Browse files
rYamal4AXEPOH
authored andcommitted
DBTOOLS-1742 fixed clickhouse not null
1 parent 30abd2f commit 2f0ad04

20 files changed

Lines changed: 188 additions & 133 deletions

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1919

2020
### Fixed
2121

22+
- Fixed code generation for columns with Nullable values in ClickHouse.
23+
2224
### Removed
2325

2426
- Removed support for MS SQL versions below 2017.

CHANGELOG.ru.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
### Исправлено
2121

22+
- Исправлена генерация кода для колонок c Nullable значениями в ClickHouse.
23+
2224
### Удалено
2325

2426
- Прекращена поддержка версий MS SQL ниже 2017.

src/main/antlr4/org/pgcodekeeper/core/parsers/antlr/ch/generated/CHParser.g4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,7 @@ data_type
10761076
| NCHAR VARYING
10771077
| NESTED LPAREN identifier data_type (COMMA identifier data_type)* RPAREN
10781078
| NOTHING
1079-
| NULLABLE LPAREN data_type RPAREN
1079+
| NULLABLE LPAREN nullable_data_type=data_type RPAREN
10801080
| NUMERIC
10811081
| NVARCHAR
10821082
| OBJECT_TYPE LPAREN STRING_LITERAL RPAREN

src/main/java/org/pgcodekeeper/core/parsers/antlr/ch/statement/ChParserAbstract.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,9 @@ protected ChColumn getColumn(Table_column_defContext column) {
5858
List<ParserRuleContext> ids = getIdentifiers(column.qualified_name());
5959
var col = new ChColumn(QNameParser.getFirstName(ids));
6060
Data_type_exprContext typeExpr = column.data_type_expr();
61-
if (typeExpr.data_type() != null) {
62-
col.setType(getFullCtxText(typeExpr.data_type()));
63-
col.setNotNull(isColumnNotNullable(typeExpr));
64-
61+
var dataType = typeExpr.data_type();
62+
if (dataType != null) {
63+
setDataType(col, dataType);
6564
var defType = typeExpr.table_column_property_expr();
6665
if (defType != null) {
6766
if (defType.DEFAULT() != null) {
@@ -78,8 +77,8 @@ protected ChColumn getColumn(Table_column_defContext column) {
7877
}
7978
}
8079
}
81-
if (column.not_null() != null) {
82-
col.setNotNull(column.not_null().NOT() != null);
80+
if (isNullable(column.not_null()) || isNullable(typeExpr.not_null())) {
81+
col.setNotNull(false);
8382
}
8483
if (column.comment_expr() != null) {
8584
col.setComment(column.comment_expr().STRING_LITERAL().getText());
@@ -95,9 +94,16 @@ protected ChColumn getColumn(Table_column_defContext column) {
9594
return col;
9695
}
9796

98-
private boolean isColumnNotNullable(Data_type_exprContext typeExpr) {
99-
return (typeExpr.not_null() != null && typeExpr.not_null().NOT() != null)
100-
|| typeExpr.data_type().NULLABLE() == null;
97+
private boolean isNullable(Not_nullContext notNullContext) {
98+
return notNullContext != null && notNullContext.NOT() == null;
99+
}
100+
101+
protected void setDataType(ChColumn col, Data_typeContext dataType) {
102+
if (dataType.NULLABLE() != null) {
103+
dataType = dataType.nullable_data_type;
104+
col.setNotNull(false);
105+
}
106+
col.setType(getFullCtxText(dataType));
101107
}
102108

103109
protected ChEngine getEnginePart(Engine_clauseContext engineClause) {

src/main/java/org/pgcodekeeper/core/parsers/antlr/ch/statement/CreateChDictionary.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void parseObject() {
6767
public void parseObject(ChDictionary dictionary) {
6868
for (var attrCtx : ctx.dictionary_attr_def()) {
6969
var col = new ChColumn(attrCtx.identifier().getText());
70-
col.setType(getFullCtxText(attrCtx.data_type()));
70+
setDataType(col, attrCtx.data_type());
7171
if (attrCtx.DEFAULT() != null) {
7272
col.setDefaultType("DEFAULT");
7373
col.setDefaultValue(getFullCtxText(attrCtx.literal()));

src/main/java/org/pgcodekeeper/core/schema/ch/ChColumn.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
* codecs, and different default types.
3535
*/
3636
public final class ChColumn extends AbstractColumn {
37-
3837
private String defaultType;
3938

4039
private String ttl;
@@ -48,6 +47,7 @@ public final class ChColumn extends AbstractColumn {
4847
*/
4948
public ChColumn(String name) {
5049
super(name);
50+
setNotNull(true);
5151
}
5252

5353
public void setDefaultType(String defaultType) {
@@ -75,12 +75,7 @@ public String getFullDefinition() {
7575
var sb = new StringBuilder();
7676
sb.append(ChDiffUtils.quoteName(name));
7777

78-
if (type != null) {
79-
sb.append(' ').append(type);
80-
if (notNull) {
81-
sb.append(" NOT NULL");
82-
}
83-
}
78+
appendType(sb);
8479
appendColumnOptions(sb);
8580
return sb.toString();
8681
}
@@ -92,13 +87,24 @@ public void getCreationSQL(SQLScript script) {
9287
appendIfNotExists(sb, script.getSettings());
9388
sb.append(ChDiffUtils.quoteName(name));
9489

95-
if (type != null) {
96-
sb.append(' ').append(type);
97-
}
90+
appendType(sb);
9891
appendColumnOptions(sb);
9992
script.addStatement(sb);
10093
}
10194

95+
private void appendType(StringBuilder sb) {
96+
if (type == null) {
97+
return;
98+
}
99+
100+
sb.append(' ');
101+
if (notNull) {
102+
sb.append(type);
103+
} else {
104+
sb.append("Nullable(").append(type).append(")");
105+
}
106+
}
107+
102108
private void appendColumnOptions(StringBuilder sb) {
103109
if (defaultType != null) {
104110
sb.append(' ').append(defaultType);
@@ -135,7 +141,7 @@ public ObjectState appendAlterSQL(PgStatement newCondition, SQLScript script) {
135141
int startSize = script.getSize();
136142
ChColumn newColumn = (ChColumn) newCondition;
137143

138-
compareTypes(newColumn.type, script);
144+
compareTypes(newColumn, script);
139145
compareDefaults(newColumn, script);
140146
compareCodecs(newColumn.codecs, script);
141147
compareTtl(newColumn.ttl, newColumn.type, script);
@@ -154,13 +160,14 @@ public void getDropSQL(SQLScript script, boolean optionExists) {
154160
script.addStatement(sb);
155161
}
156162

157-
private void compareTypes(String newType, SQLScript script) {
158-
if (type.equals(newType)) {
163+
private void compareTypes(ChColumn newColumn, SQLScript script) {
164+
if (Objects.equals(type, newColumn.type) && notNull == newColumn.notNull) {
159165
return;
160166
}
167+
161168
StringBuilder sb = new StringBuilder();
162169
appendAlterColumn(sb, script.getSettings());
163-
sb.append(' ').append(newType);
170+
newColumn.appendType(sb);
164171
script.addStatement(sb);
165172
}
166173

src/test/resources/org/pgcodekeeper/core/it/depcies/ch/add_func_func_1_usr_func_1_diff.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
CREATE VIEW default.v1
44
(
5-
`s` UInt8 NOT NULL
5+
`s` UInt8
66
)
77
AS SELECT 1 AS s;
88

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
CREATE TABLE default.t2
22
(
3-
`col1` Int64 NOT NULL,
4-
`col2` Int64 NOT NULL
3+
`col1` Int64,
4+
`col2` Int64
55
)
66
ENGINE = MergeTree
77
ORDER BY col1
88
SETTINGS index_granularity = 8192;
99

10-
CREATE FUNCTION test_qual AS (x) -> default.t2;
10+
CREATE FUNCTION test_qual AS (x) -> default.t2;

src/test/resources/org/pgcodekeeper/core/it/depcies/ch/ch_mat_view_deps_change_table_usr_t1_diff.sql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ DROP TABLE default.t1_1;
66

77
CREATE TABLE default.t1_1
88
(
9-
`d` DateTime NOT NULL DEFAULT 1,
10-
`a` UInt32 NOT NULL,
11-
`c` Int64 NOT NULL
9+
`d` DateTime DEFAULT 1,
10+
`a` UInt32,
11+
`c` Int64
1212
)
1313
ENGINE = MergeTree
1414
PRIMARY KEY tuple()
@@ -18,8 +18,8 @@ SETTINGS index_granularity = 8192;
1818

1919
CREATE MATERIALIZED VIEW default.v1
2020
(
21-
`col1` Int64 NOT NULL,
22-
`col2` Int64 NOT NULL
21+
`col1` Int64,
22+
`col2` Int64
2323
)
2424
ENGINE = Memory
2525
AS SELECT A + B AS col1, C AS col2

src/test/resources/org/pgcodekeeper/core/it/depcies/ch/ch_view_deps_change_table_usr_t1_diff.sql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ DROP TABLE default.t1_1;
66

77
CREATE TABLE default.t1_1
88
(
9-
`d` DateTime NOT NULL DEFAULT 1,
10-
`a` UInt32 NOT NULL,
11-
`c` Int64 NOT NULL
9+
`d` DateTime DEFAULT 1,
10+
`a` UInt32,
11+
`c` Int64
1212
)
1313
ENGINE = MergeTree
1414
PRIMARY KEY tuple()
@@ -18,8 +18,8 @@ SETTINGS index_granularity = 8192;
1818

1919
CREATE VIEW default.v1
2020
(
21-
`col1` Int64 NOT NULL,
22-
`col2` Int64 NOT NULL
21+
`col1` Int64,
22+
`col2` Int64
2323
)
2424
AS SELECT A + B AS col1, C AS col2
2525
FROM (

0 commit comments

Comments
 (0)