in v1/src/main/java/com/google/cloud/teleport/spanner/AvroSchemaToDdlConverter.java [507:645]
public Table toTable(String tableName, Schema schema) {
if (tableName == null) {
tableName = getSpannerObjectName(schema);
}
LOG.debug("Converting to Ddl tableName {}", tableName);
Table.Builder table = Table.builder(dialect);
table.name(tableName);
for (Schema.Field f : schema.getFields()) {
Column.Builder column = table.column(f.name());
String sqlType = f.getProp(SQL_TYPE);
String expression = f.getProp(GENERATION_EXPRESSION);
String identityColumn = f.getProp(IDENTITY_COLUMN);
if (identityColumn != null && Boolean.parseBoolean(identityColumn)) {
column.isIdentityColumn(true);
if (f.getProp(SPANNER_SEQUENCE_KIND) != null) {
column.sequenceKind(f.getProp(SPANNER_SEQUENCE_KIND));
}
if (f.getProp(SPANNER_SEQUENCE_SKIP_RANGE_MIN) != null
&& f.getProp(SPANNER_SEQUENCE_SKIP_RANGE_MAX) != null) {
column
.skipRangeMin(Long.valueOf(f.getProp(SPANNER_SEQUENCE_SKIP_RANGE_MIN)))
.skipRangeMax(Long.valueOf(f.getProp(SPANNER_SEQUENCE_SKIP_RANGE_MAX)));
}
if (f.getProp(SPANNER_SEQUENCE_COUNTER_START) != null) {
column.counterStartValue(Long.valueOf(f.getProp(SPANNER_SEQUENCE_COUNTER_START)));
}
}
if (expression != null) {
// This is a generated column.
if (Strings.isNullOrEmpty(sqlType)) {
throw new IllegalArgumentException(
"Property sqlType is missing for generated column " + f.name());
}
String notNull = f.getProp(NOT_NULL);
if (notNull == null) {
throw new IllegalArgumentException(
"Property notNull is missing for generated column " + f.name());
}
column.parseType(sqlType).notNull(Boolean.parseBoolean(notNull)).generatedAs(expression);
String stored = f.getProp(STORED);
if (stored == null) {
throw new IllegalArgumentException(
"Property stored is missing for generated column " + f.name());
}
if (Boolean.parseBoolean(stored)) {
column.stored();
}
} else {
boolean nullable = false;
Schema avroType = f.schema();
if (avroType.getType() == Schema.Type.UNION) {
Schema unpacked = unpackNullable(avroType);
nullable = unpacked != null;
if (nullable) {
avroType = unpacked;
}
}
if (Strings.isNullOrEmpty(sqlType)) {
Type spannerType = inferType(avroType, true);
sqlType = SizedType.typeString(spannerType, -1, true);
}
String defaultExpression = f.getProp(DEFAULT_EXPRESSION);
column.parseType(sqlType).notNull(!nullable).defaultExpression(defaultExpression);
}
String hidden = f.getProp(HIDDEN);
if (Boolean.parseBoolean(hidden)) {
column.isHidden(true);
}
String placementKey = f.getProp(SPANNER_PLACEMENT_KEY);
if (placementKey != null) {
column.isPlacementKey(Boolean.parseBoolean(placementKey));
}
ImmutableList.Builder<String> columnOptions = ImmutableList.builder();
for (int i = 0; ; i++) {
String spannerOption = f.getProp(SPANNER_OPTION + i);
if (spannerOption == null) {
break;
}
columnOptions.add(spannerOption);
}
column.columnOptions(columnOptions.build());
column.endColumn();
}
for (int i = 0; ; i++) {
String spannerPrimaryKey = schema.getProp(SPANNER_PRIMARY_KEY + "_" + i);
if (spannerPrimaryKey == null) {
break;
}
if (spannerPrimaryKey.endsWith(" ASC")) {
String name = spannerPrimaryKey.substring(0, spannerPrimaryKey.length() - 4);
table.primaryKey().asc(unescape(name, dialect)).end();
} else if (spannerPrimaryKey.endsWith(" DESC")) {
String name = spannerPrimaryKey.substring(0, spannerPrimaryKey.length() - 5);
table.primaryKey().desc(unescape(name, dialect)).end();
} else {
throw new IllegalArgumentException("Cannot parse spannerPrimaryKey " + spannerPrimaryKey);
}
}
table.indexes(getNumberedPropsWithPrefix(schema, SPANNER_INDEX));
table.foreignKeys(getNumberedPropsWithPrefix(schema, SPANNER_FOREIGN_KEY));
table.checkConstraints(getNumberedPropsWithPrefix(schema, SPANNER_CHECK_CONSTRAINT));
// Table parent options.
String spannerParent = schema.getProp(SPANNER_PARENT);
if (!Strings.isNullOrEmpty(spannerParent)) {
table.interleaveInParent(spannerParent);
// Process the interleave type.
String spannerInterleaveType = schema.getProp(SPANNER_INTERLEAVE_TYPE);
if (!Strings.isNullOrEmpty(spannerInterleaveType)) {
table.interleaveType(
spannerInterleaveType.equals("IN") ? InterleaveType.IN : InterleaveType.IN_PARENT);
} else {
// Default to IN_PARENT for backwards compatibility with older exports.
table.interleaveType(InterleaveType.IN_PARENT);
}
// Process the on delete action.
String onDeleteAction = schema.getProp(SPANNER_ON_DELETE_ACTION);
if (onDeleteAction == null) {
// Preserve behavior for old versions of exporter that did not provide the
// spannerOnDeleteAction property.
onDeleteAction = "no action";
}
if (onDeleteAction.equals("cascade")) {
table.onDeleteCascade();
} else if (!onDeleteAction.equals("no action")) {
// This is an unknown on delete action.
throw new IllegalArgumentException("Unsupported ON DELETE action " + onDeleteAction);
}
}
return table.build();
}