Skip to content

Commit

Permalink
Make first unique key as primary key if possible. Fix 0000 year problem.
Browse files Browse the repository at this point in the history
  • Loading branch information
neoremind committed May 21, 2020
1 parent 6eee34a commit 0751900
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public TableReaderImpl(String ibdFilePath, TableDef tableDef, KeyComparator keyC
this.ibdFilePath = ibdFilePath;
this.tableDef = tableDef;
this.keyComparator = keyComparator;
this.tableDef.validate();
this.tableDef.prepare();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,11 @@ public Class<?> typeClass() {

@Override
public Short readFrom(SliceInput input, Column column) {
return (short) (input.readUnsignedByte() + 1900);
int b = input.readUnsignedByte();
if (b == 0) {
return (short) 0;
}
return (short) (b + 1900);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import lombok.Data;
Expand All @@ -37,6 +38,8 @@
import static com.alibaba.innodb.java.reader.schema.KeyMeta.Type.FOREIGN_KEY;
import static com.alibaba.innodb.java.reader.schema.KeyMeta.Type.FULLTEXT_KEY;
import static com.alibaba.innodb.java.reader.schema.KeyMeta.Type.PRIMARY_KEY;
import static com.alibaba.innodb.java.reader.schema.KeyMeta.Type.UNIQUE_INDEX;
import static com.alibaba.innodb.java.reader.schema.KeyMeta.Type.UNIQUE_KEY;
import static com.alibaba.innodb.java.reader.schema.KeyMeta.Type.isValidSk;
import static com.alibaba.innodb.java.reader.util.Utils.sanitize;
import static com.google.common.base.Preconditions.checkArgument;
Expand Down Expand Up @@ -125,8 +128,9 @@ public TableDef() {
this.variableLengthColumnList = new ArrayList<>();
}

public void validate() {
public void prepare() {
checkState(CollectionUtils.isNotEmpty(columnList), "No column is specified");
makeFirstUniqueKeyAsPrimaryKeyIfPossible();
}

public boolean containsVariableLengthColumn() {
Expand Down Expand Up @@ -491,6 +495,22 @@ public static Column createRowIdColumn() {
return new Column().setName(COLUMN_ROW_ID).setType(ColumnType.ROW_ID).setNullable(false);
}

/**
* If no primary key provided, make first unique key as primary key.
*/
private void makeFirstUniqueKeyAsPrimaryKeyIfPossible() {
if (primaryKeyMeta == null) {
Predicate<KeyMeta> predicate = k -> k.getType() == UNIQUE_KEY || k.getType() == UNIQUE_INDEX;
if (secondaryKeyMetaList != null) {
if (secondaryKeyMetaList.stream().anyMatch(predicate)) {
KeyMeta pkMeta = secondaryKeyMetaList.stream().filter(predicate).findFirst().get();
setPrimaryKeyColumns(pkMeta.getKeyColumnNames());
secondaryKeyMetaList.remove(pkMeta);
}
}
}
}

@Data
public class Field {
private int ordinal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,17 @@ public void testYearDateColumnMysql80() {
public Consumer<List<GenericRecord>> expected() {
return recordList -> {

assertThat(recordList.size(), is(6));
assertThat(recordList.size(), is(8));

List<Object[]> expected = Arrays.asList(
new Object[]{1, (short) 1901, "1900-01-01"},
new Object[]{2, (short) 1999, "1901-12-31"},
new Object[]{3, (short) 1969, "1969-10-02"},
new Object[]{4, (short) 2020, "2020-12-31"},
new Object[]{5, (short) 2100, "0069-01-10"},
new Object[]{6, (short) 2155, "0001-01-01"}
new Object[]{1, (short) 0, "2100-11-11"},
new Object[]{2, (short) 2001, "2155-01-01"},
new Object[]{3, (short) 1901, "1900-01-01"},
new Object[]{4, (short) 1999, "1901-12-31"},
new Object[]{5, (short) 1969, "1969-10-02"},
new Object[]{6, (short) 2020, "2020-12-31"},
new Object[]{7, (short) 2100, "0069-01-10"},
new Object[]{8, (short) 2155, "0001-01-01"}
);

for (int i = 0; i < recordList.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,4 +515,61 @@ public void testTableFullyQualifiedName() {
assertThat(tableDef.getFullyQualifiedName(), is("test.tb02"));
}

@Test
public void testMakeUniqueKeyAsPrimaryKey() {
String sql = "CREATE TABLE `type_newdecimaltest59` (\n"
+ " `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,\n"
+ " `d` decimal(65,30) DEFAULT NULL,\n"
+ " UNIQUE KEY `id` (`id`)\n"
+ ") ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=utf8";
TableDef tableDef = TableDefUtil.covertToTableDef(sql);
// have to prepare
tableDef.prepare();
System.out.println(tableDef);

assertThat(tableDef.getName(), is("type_newdecimaltest59"));
assertThat(tableDef.getFullyQualifiedName(), is("type_newdecimaltest59"));
assertThat(tableDef.getDefaultCharset(), is("utf8"));
assertThat(tableDef.getDefaultJavaCharset(), is("UTF-8"));
assertThat(tableDef.getCollation(), is("utf8_general_ci"));
assertThat(tableDef.isCollationCaseSensitive(), is(false));

List<Column> columnList = tableDef.getColumnList();
assertThat(columnList.size(), is(2));
assertThat(tableDef.getColumnNum(), is(2));

assertThat(columnList.get(0).getOrdinal(), is(0));
assertThat(columnList.get(0).getName(), is("id"));
assertThat(columnList.get(0).getType(), is(ColumnType.UNSIGNED_BIGINT));
assertThat(columnList.get(0).getFullType(), is("bigint(20) UNSIGNED"));
assertThat(columnList.get(0).getLength(), is(20));
assertThat(columnList.get(0).getPrecision(), is(0));
assertThat(columnList.get(0).getScale(), is(0));
assertThat(columnList.get(0).isPrimaryKey(), is(false));
assertThat(columnList.get(0).isNullable(), is(false));

assertThat(columnList.get(1).getOrdinal(), is(1));
assertThat(columnList.get(1).getName(), is("d"));
assertThat(columnList.get(1).getType(), is(ColumnType.DECIMAL));
assertThat(columnList.get(1).getFullType(), is("decimal(65,30)"));
assertThat(columnList.get(1).getLength(), is(0));
assertThat(columnList.get(1).getPrecision(), is(65));
assertThat(columnList.get(1).getScale(), is(30));
assertThat(columnList.get(1).isPrimaryKey(), is(false));
assertThat(columnList.get(1).isNullable(), is(true));

assertThat(tableDef.getPrimaryKeyColumns(), is(ImmutableList.of(columnList.get(0))));
assertThat(tableDef.getPrimaryKeyColumnNum(), is(1));
assertThat(tableDef.getPrimaryKeyColumnNames(), is(ImmutableList.of("id")));
assertThat(tableDef.getPrimaryKeyVarLenColumns(), is(ImmutableList.of()));
assertThat(tableDef.getPrimaryKeyVarLenColumnNames(), is(ImmutableList.of()));
assertThat(tableDef.isColumnPrimaryKey(columnList.get(0)), is(true));
for (int i = 1; i < 10; i++) {
assertThat(tableDef.isColumnPrimaryKey(columnList.get(1)), is(false));
}

assertThat(tableDef.getSecondaryKeyMetaList().size(), is(0));
assertThat(tableDef.getSecondaryKeyMetaMap().size(), is(0));
}

}
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ CREATE TABLE `tb16`
PRIMARY KEY (`id`))
ENGINE=InnoDB;

insert into tb16 values(null, 0, '2100-11-11');
insert into tb16 values(null, 1, '2155-01-01');
insert into tb16 values(null, 1901, '1900-01-01');
insert into tb16 values(null, 1999, '1901-12-31');
insert into tb16 values(null, 1969, '1969-10-02');
Expand Down
Binary file not shown.
Binary file not shown.

0 comments on commit 0751900

Please sign in to comment.