Skip to content

[Feature][transform-v2] SQL transform support schema change #8438

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

Open
wants to merge 25 commits into
base: dev
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b2639d6
[Feature][transform-v2] SQL transform support schema change
CosmosNi Jan 3, 2025
0a609a2
[Feature][transform-v2] SQL transform support schema change
CosmosNi Jan 3, 2025
36757c3
[Feature][transform-v2] SQL transform support schema change
CosmosNi Jan 6, 2025
c93e625
[Feature][transform-v2] SQL transform support schema change
CosmosNi Jan 6, 2025
7c9f250
Merge remote-tracking branch 'refs/remotes/upstream/dev' into transfo…
CosmosNi Jan 6, 2025
dd21c33
[Feature][transform-v2] SQL transform support schema change
CosmosNi Jan 6, 2025
2962de8
[Feature][transform-v2] SQL transform support schema change
CosmosNi Jan 6, 2025
cc95077
[Feature][transform-v2] SQL transform support schema change
CosmosNi Jan 7, 2025
e8ebfce
[Feature][transform-v2] SQL transform support schema change
CosmosNi Jan 7, 2025
5d1a14d
Merge remote-tracking branch 'refs/remotes/upstream/dev' into transfo…
CosmosNi Jan 7, 2025
d061b14
[Feature][connector-elasticsearch] elasticsearch support nested type
CosmosNi Jan 8, 2025
497e8eb
[Feature][transform-v2] SQL transform support schema change
CosmosNi Jan 8, 2025
537aa64
Merge remote-tracking branch 'origin/transform_sql_schema_change' int…
CosmosNi Jan 8, 2025
4dbe153
Merge remote-tracking branch 'refs/remotes/upstream/dev' into transfo…
CosmosNi Jan 8, 2025
c903b3a
[Feature][transform-v2] SQL transform support schema change
CosmosNi Jan 8, 2025
f83eff0
Merge remote-tracking branch 'upstream/dev' into transform_sql_schema…
CosmosNi Jan 11, 2025
9bbea15
[Feature][transform-v2] SQL transform support schema change
CosmosNi Jan 11, 2025
b880a18
[Feature][transform-v2] SQL transform support schema change
CosmosNi Jan 11, 2025
269ee68
[Feature][transform-v2] SQL transform support schema change
Jan 14, 2025
9c5c5c6
[Feature][transform-v2] SQL transform support schema change
Jan 14, 2025
2d17b66
[Feature][transform-v2] SQL transform support schema change
CosmosNi Jan 14, 2025
482f43b
[Feature][transform-v2] SQL transform support schema change
Jan 15, 2025
f34f675
Merge remote-tracking branch 'refs/remotes/upstream/dev' into transfo…
CosmosNi Jan 24, 2025
f20e995
[Feature][transform-v2] SQL transform support schema change
CosmosNi Jan 26, 2025
ec10c9e
Merge remote-tracking branch 'upstream/dev' into transform_sql_schema…
CosmosNi Feb 2, 2025
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
Prev Previous commit
Next Next commit
[Feature][transform-v2] SQL transform support schema change
CosmosNi committed Jan 7, 2025
commit cc9507702de91e61d7899d23185a34eb7e6042c2
Original file line number Diff line number Diff line change
@@ -59,6 +59,12 @@ public CatalogTable getProducedCatalogTable() {
return outputCatalogTable;
}

public void restProducedCatalogTable() {
synchronized (this) {
outputCatalogTable = transformCatalogTable();
}
}

@Override
public List<CatalogTable> getProducedCatalogTables() {
return Collections.singletonList(getProducedCatalogTable());
Original file line number Diff line number Diff line change
@@ -34,4 +34,6 @@ void init(
List<SeaTunnelRow> transformBySQL(SeaTunnelRow inputRow, SeaTunnelRowType outputRowType);

default void close() {}

void resetAllColumnsCount();
}
Original file line number Diff line number Diff line change
@@ -206,6 +206,13 @@ public SchemaChangeEvent mapSchemaChangeEvent(SchemaChangeEvent event) {
inputCatalogTable.getOptions(),
inputCatalogTable.getPartitionKeys(),
inputCatalogTable.getComment());
sqlEngine.init(
inputTableName,
inputCatalogTable.getTableId().getTableName(),
inputCatalogTable.getSeaTunnelRowType(),
query);
sqlEngine.resetAllColumnsCount();
restProducedCatalogTable();

if (event instanceof AlterTableColumnsEvent) {
AlterTableColumnsEvent alterTableColumnsEvent = (AlterTableColumnsEvent) event;
Original file line number Diff line number Diff line change
@@ -303,4 +303,9 @@ private int countColumnsSize(List<SelectItem<?>> selectItems) {
- allColumnsCnt;
return allColumnsCount;
}

@Override
public void resetAllColumnsCount() {
allColumnsCount = null;
}
}
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@
import org.apache.seatunnel.api.configuration.ReadonlyConfig;
import org.apache.seatunnel.api.table.catalog.CatalogTable;
import org.apache.seatunnel.api.table.catalog.CatalogTableUtil;
import org.apache.seatunnel.api.table.catalog.Column;
import org.apache.seatunnel.api.table.catalog.ConstraintKey;
import org.apache.seatunnel.api.table.catalog.PhysicalColumn;
import org.apache.seatunnel.api.table.catalog.PrimaryKey;
@@ -39,6 +40,8 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -299,7 +302,7 @@ public void testSchemaChange() {
TableSchema.builder()
.column(
PhysicalColumn.of(
"f1",
"id",
BasicType.LONG_TYPE,
null,
null,
@@ -308,92 +311,201 @@ public void testSchemaChange() {
null))
.column(
PhysicalColumn.of(
"f2",
BasicType.LONG_TYPE,
"name",
BasicType.STRING_TYPE,
null,
null,
true,
null,
null))
.column(
PhysicalColumn.of(
"f3",
"age",
BasicType.LONG_TYPE,
null,
null,
true,
null,
null))
.primaryKey(PrimaryKey.of("pk1", Arrays.asList("f1")))
.primaryKey(PrimaryKey.of("pk1", Arrays.asList("id")))
.constraintKey(
ConstraintKey.of(
ConstraintKey.ConstraintType.UNIQUE_KEY,
"uk1",
Arrays.asList(
ConstraintKey.ConstraintKeyColumn.of(
"f2",
"name",
ConstraintKey.ColumnSortType.ASC),
ConstraintKey.ConstraintKeyColumn.of(
"f3",
"age",
ConstraintKey.ColumnSortType.ASC))))
.build(),
Collections.emptyMap(),
Collections.singletonList("f2"),
Collections.singletonList("name"),
null);

ReadonlyConfig config =
ReadonlyConfig.fromMap(
new HashMap<String, Object>() {
{
put("query", "select * from dual");
}
});
List<SeaTunnelRow> result;
SQLTransform transform = new SQLTransform(config, catalogTable);
result =
transform.transformRow(
new SeaTunnelRow(
new Object[] {Integer.valueOf(1), "Cosmos", Integer.valueOf(30)}));
List<String> columnNames;
List<String> columnType;
List<String> assertNames;
List<String> assertTypes;
Object[] columnValues;
Object[] assertValue;

columnNames =
transform.getProducedCatalogTable().getTableSchema().getColumns().stream()
.map(Column::getName)
.collect(Collectors.toList());
columnType =
transform.getProducedCatalogTable().getTableSchema().getColumns().stream()
.map((e) -> e.getDataType().getSqlType().name())
.collect(Collectors.toList());
assertNames = Lists.newArrayList("id", "name", "age");
assertTypes = Lists.newArrayList("BIGINT", "STRING", "BIGINT");

columnValues = result.get(0).getFields();
assertValue = new Object[] {Integer.valueOf(1), "Cosmos", Integer.valueOf(30)};
Assertions.assertIterableEquals(columnNames, assertNames);
Assertions.assertIterableEquals(columnType, assertTypes);
Assertions.assertArrayEquals(columnValues, assertValue);

// test add column
AlterTableAddColumnEvent addColumnEvent =
AlterTableAddColumnEvent.add(
catalogTable.getTableId(),
PhysicalColumn.of("f4", BasicType.LONG_TYPE, null, null, true, null, null));
transform.mapSchemaChangeEvent(addColumnEvent);

result =
transform.transformRow(
new SeaTunnelRow(
new Object[] {
Integer.valueOf(1),
"Cosmos",
Integer.valueOf(30),
Integer.valueOf(14)
}));
columnNames =
transform.getProducedCatalogTable().getTableSchema().getColumns().stream()
.map(Column::getName)
.collect(Collectors.toList());
columnType =
transform.getProducedCatalogTable().getTableSchema().getColumns().stream()
.map((e) -> e.getDataType().getSqlType().name())
.collect(Collectors.toList());
assertNames = Lists.newArrayList("id", "name", "age", "f4");
assertTypes = Lists.newArrayList("BIGINT", "STRING", "BIGINT", "BIGINT");

columnValues = result.get(0).getFields();
assertValue =
new Object[] {
Integer.valueOf(1), "Cosmos", Integer.valueOf(30), Integer.valueOf(14)
};
Assertions.assertIterableEquals(columnNames, assertNames);
Assertions.assertIterableEquals(columnType, assertTypes);
Assertions.assertArrayEquals(columnValues, assertValue);

// test modify column
AlterTableModifyColumnEvent modifyColumnEvent =
AlterTableModifyColumnEvent.modify(
catalogTable.getTableId(),
PhysicalColumn.of("f4", BasicType.INT_TYPE, null, null, true, null, null));
PhysicalColumn.of(
"f4", BasicType.STRING_TYPE, null, null, true, null, null));
transform.mapSchemaChangeEvent(modifyColumnEvent);
result =
transform.transformRow(
new SeaTunnelRow(
new Object[] {
Integer.valueOf(1), "Cosmos", Integer.valueOf(30), "Cosmos"
}));
columnNames =
transform.getProducedCatalogTable().getTableSchema().getColumns().stream()
.map(Column::getName)
.collect(Collectors.toList());
columnType =
transform.getProducedCatalogTable().getTableSchema().getColumns().stream()
.map((e) -> e.getDataType().getSqlType().name())
.collect(Collectors.toList());
assertNames = Lists.newArrayList("id", "name", "age", "f4");
assertTypes = Lists.newArrayList("BIGINT", "STRING", "BIGINT", "STRING");

columnValues = result.get(0).getFields();
assertValue = new Object[] {Integer.valueOf(1), "Cosmos", Integer.valueOf(30), "Cosmos"};
Assertions.assertIterableEquals(columnNames, assertNames);
Assertions.assertIterableEquals(columnType, assertTypes);
Assertions.assertArrayEquals(columnValues, assertValue);

// test change column
AlterTableChangeColumnEvent changeColumnEvent =
AlterTableChangeColumnEvent.change(
catalogTable.getTableId(),
"f4",
PhysicalColumn.of("f5", BasicType.INT_TYPE, null, null, true, null, null));
transform.mapSchemaChangeEvent(changeColumnEvent);
result =
transform.transformRow(
new SeaTunnelRow(
new Object[] {
Integer.valueOf(1),
"Cosmos",
Integer.valueOf(30),
Integer.valueOf(14)
}));
columnNames =
transform.getProducedCatalogTable().getTableSchema().getColumns().stream()
.map(Column::getName)
.collect(Collectors.toList());
columnType =
transform.getProducedCatalogTable().getTableSchema().getColumns().stream()
.map((e) -> e.getDataType().getSqlType().name())
.collect(Collectors.toList());
assertNames = Lists.newArrayList("id", "name", "age", "f5");
assertTypes = Lists.newArrayList("BIGINT", "STRING", "BIGINT", "INT");

columnValues = result.get(0).getFields();
assertValue =
new Object[] {
Integer.valueOf(1), "Cosmos", Integer.valueOf(30), Integer.valueOf(14)
};
Assertions.assertIterableEquals(columnNames, assertNames);
Assertions.assertIterableEquals(columnType, assertTypes);
Assertions.assertArrayEquals(columnValues, assertValue);

// test drop column
AlterTableDropColumnEvent dropColumnEvent =
new AlterTableDropColumnEvent(catalogTable.getTableId(), "f5");
ReadonlyConfig config =
ReadonlyConfig.fromMap(
new HashMap<String, Object>() {
{
put("query", "select * from dual");
}
});
transform.mapSchemaChangeEvent(dropColumnEvent);
result =
transform.transformRow(
new SeaTunnelRow(
new Object[] {Integer.valueOf(1), "Cosmos", Integer.valueOf(30)}));
columnNames =
transform.getProducedCatalogTable().getTableSchema().getColumns().stream()
.map(Column::getName)
.collect(Collectors.toList());
columnType =
transform.getProducedCatalogTable().getTableSchema().getColumns().stream()
.map((e) -> e.getDataType().getSqlType().name())
.collect(Collectors.toList());
assertNames = Lists.newArrayList("id", "name", "age");
assertTypes = Lists.newArrayList("BIGINT", "STRING", "BIGINT");

SQLTransform transform = new SQLTransform(config, catalogTable);
AlterTableAddColumnEvent outputAddEvent =
(AlterTableAddColumnEvent) transform.mapSchemaChangeEvent(addColumnEvent);
AlterTableModifyColumnEvent outputModifyEvent =
(AlterTableModifyColumnEvent) transform.mapSchemaChangeEvent(modifyColumnEvent);
AlterTableChangeColumnEvent outputChangeEvent =
(AlterTableChangeColumnEvent) transform.mapSchemaChangeEvent(changeColumnEvent);
AlterTableDropColumnEvent outputDropEvent =
(AlterTableDropColumnEvent) transform.mapSchemaChangeEvent(dropColumnEvent);
CatalogTable outputCatalogTable = transform.getProducedCatalogTable();
Assertions.assertIterableEquals(
Arrays.asList("f1", "f2", "f3"),
Arrays.asList(outputCatalogTable.getTableSchema().getFieldNames()));
Assertions.assertIterableEquals(
Arrays.asList("f1"),
outputCatalogTable.getTableSchema().getPrimaryKey().getColumnNames());
outputCatalogTable.getTableSchema().getConstraintKeys().stream()
.forEach(
key ->
Assertions.assertIterableEquals(
Arrays.asList("f2", "f3"),
key.getColumnNames().stream()
.map(
ConstraintKey.ConstraintKeyColumn
::getColumnName)
.collect(Collectors.toList())));
Assertions.assertEquals("f4", outputAddEvent.getColumn().getName());
Assertions.assertEquals("f4", outputModifyEvent.getColumn().getName());
Assertions.assertEquals("f4", outputChangeEvent.getOldColumn());
Assertions.assertEquals("f5", outputChangeEvent.getColumn().getName());
Assertions.assertEquals("f5", outputDropEvent.getColumn());
columnValues = result.get(0).getFields();
assertValue = new Object[] {Integer.valueOf(1), "Cosmos", Integer.valueOf(30)};
Assertions.assertIterableEquals(columnNames, assertNames);
Assertions.assertIterableEquals(columnType, assertTypes);
Assertions.assertArrayEquals(columnValues, assertValue);
}
}