Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DAT-17990] Add tblProperties functionality to createTable #153

Merged
merged 3 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class CreateTableChangeDatabricks extends CreateTableChange {
private String tableLocation;
private String clusterColumns;
private String partitionColumns;
private ExtendedTableProperties extendedTableProperties;


@Override
Expand Down Expand Up @@ -66,7 +67,17 @@ protected CreateTableStatement generateCreateTableStatement() {
ctas.setTableLocation(this.getTableLocation());
ctas.setClusterColumns(this.getClusterColumns());
ctas.setPartitionColumns(this.getPartitionColumns());
ctas.setExtendedTableProperties(this.getExtendedTableProperties());

return ctas;
}

@DatabaseChangeProperty
public ExtendedTableProperties getExtendedTableProperties() {
return extendedTableProperties;
}

public void setExtendedTableProperties(ExtendedTableProperties extendedTableProperties) {
this.extendedTableProperties = extendedTableProperties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CreateTableStatementDatabricks extends CreateTableStatement {

Expand All @@ -16,6 +15,8 @@ public class CreateTableStatementDatabricks extends CreateTableStatement {

private ArrayList<String> partitionColumns;

private ExtendedTableProperties extendedTableProperties;


public CreateTableStatementDatabricks(String catalogName, String schemaName, String tableName) {
super(catalogName, schemaName, tableName);
Expand Down Expand Up @@ -55,5 +56,11 @@ public void setClusterColumns (String clusterColumns) {
this.clusterColumns = new ArrayList<>(Arrays.asList(clusterColumns.split("\\s*,\\s*")));
}

public ExtendedTableProperties getExtendedTableProperties() {
return extendedTableProperties;
}

public void setExtendedTableProperties(ExtendedTableProperties extendedTableProperties) {
this.extendedTableProperties = extendedTableProperties;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package liquibase.ext.databricks.change.createTable;

import liquibase.serializer.AbstractLiquibaseSerializable;

public class ExtendedTableProperties extends AbstractLiquibaseSerializable{
private String tableLocation;
private String tblProperties;

@Override
public String getSerializedObjectName() {
return "extendedTableProperties";
}

@Override
public String getSerializedObjectNamespace() {
return "http://www.liquibase.org/xml/ns/databricks";
}

public String getTableLocation() {
return tableLocation;
}

public void setTableLocation(String tableLocation) {
this.tableLocation = tableLocation;
}

public String getTblProperties() {
return tblProperties;
}

public void setTblProperties(String tblProperties) {
this.tblProperties = tblProperties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import liquibase.sqlgenerator.SqlGeneratorChain;
import liquibase.statement.core.CreateTableStatement;
import liquibase.structure.DatabaseObject;
import liquibase.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
vitaliimak marked this conversation as resolved.
Show resolved Hide resolved

import java.util.ArrayList;

Expand Down Expand Up @@ -46,14 +46,16 @@ public Sql[] generateSql(CreateTableStatement statement, Database database, SqlG
if (statement instanceof CreateTableStatementDatabricks) {
CreateTableStatementDatabricks thisStatement = (CreateTableStatementDatabricks) statement;

if ((!StringUtil.isEmpty(thisStatement.getTableFormat()))) {
if ((!StringUtils.isEmpty(thisStatement.getTableFormat()))) {
finalsql += " USING " + thisStatement.getTableFormat();
} else if (thisStatement.getExtendedTableProperties() != null && StringUtils.isNoneEmpty(thisStatement.getExtendedTableProperties().getTblProperties())) {
finalsql += " TBLPROPERTIES (" + thisStatement.getExtendedTableProperties().getTblProperties() + ")";
} else {
finalsql += " USING delta TBLPROPERTIES('delta.feature.allowColumnDefaults' = 'supported', 'delta.columnMapping.mode' = 'name', 'delta.enableDeletionVectors' = true)";
}

// Databricks can decide to have tables live in a particular location. If null, Databricks will handle the location automatically in DBFS
if (!StringUtil.isEmpty(thisStatement.getTableLocation())) {
if (!StringUtils.isEmpty(thisStatement.getTableLocation())) {
finalsql += " LOCATION '" + thisStatement.getTableLocation() + "'";
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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 id="1" author="oleh">
<createTable tableName="test_table">
<column name="test_id" type="int">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="test_column" type="varchar(50)">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>

<changeSet id="2" author="as">
<createTable tableName="test_table_properties">
<column name="test_id" type="int">
<constraints primaryKey="true" nullable="false"/>
</column>
<databricks:extendedTableProperties tblProperties="'external.location'='s3://mybucket/mytable','this.is.my.key'=12,'this.is.my.key2'=true"/>
</createTable>
</changeSet>
</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
CREATE TABLE main.liquibase_harness_test_ds.test_table (test_id INT NOT NULL, test_column VARCHAR(50) NOT NULL, CONSTRAINT PK_TEST_TABLE PRIMARY KEY (test_id)) USING delta TBLPROPERTIES('delta.feature.allowColumnDefaults' = 'supported', 'delta.columnMapping.mode' = 'name', 'delta.enableDeletionVectors' = true)
CREATE TABLE main.liquibase_harness_test_ds.test_table (test_id INT NOT NULL, test_column VARCHAR(50) NOT NULL, CONSTRAINT PK_TEST_TABLE PRIMARY KEY (test_id)) USING delta TBLPROPERTIES('delta.feature.allowColumnDefaults' = 'supported', 'delta.columnMapping.mode' = 'name', 'delta.enableDeletionVectors' = true)
CREATE TABLE main.liquibase_harness_test_ds.test_table_properties (test_id INT NOT NULL, CONSTRAINT PK_TEST_TABLE_PROPERTIES PRIMARY KEY (test_id)) TBLPROPERTIES ('external.location'='s3://mybucket/mytable','this.is.my.key'=12,'this.is.my.key2'=true)
Loading