Skip to content

Commit

Permalink
Merge branch 'main' into DAT-19044
Browse files Browse the repository at this point in the history
  • Loading branch information
SvampX authored Nov 18, 2024
2 parents cea1727 + 8e00a02 commit f475ebb
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ColumnSnapshotGeneratorDatabricks extends ColumnSnapshotGenerator {

private static final String ALL_DATA_TYPES = " BIGINT | BINARY | BOOLEAN | DATE | DECIMAL| DECIMAL\\(| DOUBLE | FLOAT | INT | INTERVAL | VOID | SMALLINT | STRING | VARCHAR\\(\\d+\\) | TIMESTAMP | TIMESTAMP_NTZ | TINYINT | ARRAY<| MAP<| STRUCT<| VARIANT| OBJECT<";
private static final String DEFAULT_CLAUSE_TERMINATORS = "(?i)(\\s+COMMENT\\s+'| PRIMARY\\s+KEY | FOREIGN\\s+KEY | MASK\\s+\\w+|$|,(\\s+\\w+\\s+" + ALL_DATA_TYPES + "|\\)$)";
private static final String GENERATED_BY_DEFAULT_REGEX = "(?i)\\s+GENERATED\\s+BY\\s+DEFAULT\\s+AS\\s+IDENTITY";
private static final String GENERATED_BY_DEFAULT_REGEX = "(?i)\\s+GENERATED\\s+(BY\\s+DEFAULT|ALWAYS)\\s+AS\\s+IDENTITY";
private static final String GENERIC_DEFAULT_VALUE_REGEX = "DEFAULT\\s+(.*?)(" + DEFAULT_CLAUSE_TERMINATORS + "?))";
private static final String SANITIZE_TABLE_SPECIFICATION_REGEX = "(\\(.*?\\))\\s*(?i)(USING|OPTIONS|PARTITIONED BY|CLUSTER BY|LOCATION|TBLPROPERTIES|WITH|$|;$)";
private static final Pattern DEFAULT_VALUE_PATTERN = Pattern.compile(GENERIC_DEFAULT_VALUE_REGEX);
Expand Down Expand Up @@ -70,6 +70,7 @@ protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot
if (snapshot.getScratchData(showCreateRelatedTableQuery) != null) {
String showCreateTableStatement = (String) snapshot.getScratchData(showCreateRelatedTableQuery);
String defaultValue = extractDefaultValue(showCreateTableStatement, column.getName());
column.setAutoIncrementInformation(parseAutoIncrementInfo(showCreateTableStatement, column.getName()));
if (defaultValue != null) {
Matcher functionMatcher = FUNCTION_PATTERN.matcher(defaultValue);
if (functionMatcher.find()) {
Expand Down Expand Up @@ -119,4 +120,30 @@ private String sanitizeStatement(String createTableStatement) {
}
return sanitizedCreateTableStatement;
}

private Column.AutoIncrementInformation parseAutoIncrementInfo(String statement, String columnName) {
String findLineRegex = "(\\b"+columnName+"\\s+\\w+(\\s+NOT\\s+NULL)?\\s+GENERATED\\s+(BY\\s+DEFAULT|ALWAYS)\\s+AS\\s+IDENTITY\\s+\\(START WITH (\\d+) INCREMENT" +
" BY (\\d+)\\))";
Matcher findLineMatcher = Pattern.compile(findLineRegex).matcher(statement);
if (findLineMatcher.find()) {
if(!findLineMatcher.group(1).startsWith(columnName)){
return null;
}
return getAutoIncrementInformation(findLineMatcher);
} else {
return null;
}
}

private static Column.AutoIncrementInformation getAutoIncrementInformation(Matcher findLineMatcher) {
// GROUP 2 is potential `NOT NULL`, we don't need it.
// Also Databricks doesn't support syntax like Oracle "GENERATED BY DEFAULT ON NULL AS IDENTITY",
// so this variant not parsed and autoIncrementInformation.defaultOnNull is not used;
String generationType = findLineMatcher.group(3);
int startWith = Integer.parseInt(findLineMatcher.group(4));
int incrementBy = Integer.parseInt(findLineMatcher.group(5));
Column.AutoIncrementInformation autoIncrementInformation = new Column.AutoIncrementInformation(startWith, incrementBy);
autoIncrementInformation.setGenerationType(generationType);
return autoIncrementInformation;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<changeSet author="fl" id="1">
<createTable tableName="tableWithDefaultValues" >
<column name="longcolumn" type="long" autoIncrement="true" generationType="IDENTITY" />
<column name="longcolumn" type="long" autoIncrement="true" generationType="IDENTITY" startWith="10" incrementBy="5"/>
<column name="eventTime" type="timestamp" defaultValueComputed="CURRENT_TIMESTAMP()">
<constraints nullable="false" />
</column>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:databricks="http://www.liquibase.org/xml/ns/databricks"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd
http://www.liquibase.org/xml/ns/databricks
http://www.liquibase.org/xml/ns/databricks/liquibase-databricks-latest.xsd">

<changeSet author="oleh" id="1">
<createTable tableName="createTableWithTwoIdentityColumns" >
<column name="IdentityByDefault" type="long" autoIncrement="true" generationType="IDENTITY" startWith="10" incrementBy="5"/>
<column name="IdentityAlways" type="long" defaultValueComputed="GENERATED ALWAYS AS IDENTITY (START WITH 11 INCREMENT BY 2)"/>
</createTable>
</changeSet>

</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
"name": "longcolumn",
"type": {
"typeName": "BIGINT"
},
"autoIncrementInformation": {
"generationType": "BY DEFAULT",
"startWith": "10\\!\\{java.math.BigInteger\\}",
"incrementBy": "5\\!\\{java.math.BigInteger\\}"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"snapshot": {
"objects": {
"liquibase.structure.core.Table": [
{
"table": {
"name": "createTableWithTwoIdentityColumns"
}
}
],
"liquibase.structure.core.Column": [
{
"column": {
"name": "IdentityByDefault",
"type": {
"typeName": "BIGINT"
},
"autoIncrementInformation": {
"generationType": "BY DEFAULT",
"startWith": "10\\!\\{java.math.BigInteger\\}",
"incrementBy": "5\\!\\{java.math.BigInteger\\}"
}
}
},
{
"column": {
"name": "IdentityAlways",
"type": {
"typeName": "BIGINT"
},
"autoIncrementInformation": {
"generationType": "ALWAYS",
"startWith": "11\\!\\{java.math.BigInteger\\}",
"incrementBy": "2\\!\\{java.math.BigInteger\\}"
}
}
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CREATE TABLE main.liquibase_harness_test_ds.tableWithDefaultValues (longcolumn LONG GENERATED BY DEFAULT AS IDENTITY (START WITH 1 INCREMENT BY 1), eventTime TIMESTAMP DEFAULT current_timestamp() NOT NULL, year INT DEFAULT YEAR(CURRENT_TIMESTAMP()), eventDate date DEFAULT CAST(CURRENT_TIMESTAMP() AS DATE), eventDescription STRING DEFAULT 'default string, regular ? almost();!$#@^%[] String bigint' NOT NULL, eventShortDescription STRING DEFAULT 'short desc') USING delta TBLPROPERTIES('delta.feature.allowColumnDefaults' = 'supported', 'delta.columnMapping.mode' = 'name', 'delta.enableDeletionVectors' = true)
CREATE TABLE main.liquibase_harness_test_ds.tableWithDefaultValues (longcolumn LONG GENERATED BY DEFAULT AS IDENTITY (START WITH 10 INCREMENT BY 5), eventTime TIMESTAMP DEFAULT current_timestamp() NOT NULL, year INT DEFAULT YEAR(CURRENT_TIMESTAMP()), eventDate date DEFAULT CAST(CURRENT_TIMESTAMP() AS DATE), eventDescription STRING DEFAULT 'default string, regular ? almost();!$#@^%[] String bigint' NOT NULL, eventShortDescription STRING DEFAULT 'short desc') USING delta TBLPROPERTIES('delta.feature.allowColumnDefaults' = 'supported', 'delta.columnMapping.mode' = 'name', 'delta.enableDeletionVectors' = true)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE main.liquibase_harness_test_ds.createTableWithTwoIdentityColumns (IdentityByDefault LONG GENERATED BY DEFAULT AS IDENTITY (START WITH 10 INCREMENT BY 5), IdentityAlways LONG GENERATED ALWAYS AS IDENTITY (START WITH 11 INCREMENT BY 2)) USING delta TBLPROPERTIES('delta.feature.allowColumnDefaults' = 'supported', 'delta.columnMapping.mode' = 'name', 'delta.enableDeletionVectors' = true)

0 comments on commit f475ebb

Please sign in to comment.