JoinToParentPK implements AISValidation { @Override public void validate(AkibanInformationSchema ais, AISValidationOutput output) { for (Join join : ais.getJoins().values()) { if (join.getParent().getPrimaryKey() == null) { output.reportFailure(new AISValidationFailure( new JoinParentNoExplicitPK (join.getParent().getName()))); continue; } TableIndex parentPK= join.getParent().getPrimaryKey().getIndex(); if (parentPK.getKeyColumns().size() != join.getJoinColumns().size()) { output.reportFailure(new AISValidationFailure( new JoinColumnMismatchException (join.getJoinColumns().size(), join.getChild().getName(), join.getParent().getName(), parentPK.getKeyColumns().size()))); continue; } Iterator<JoinColumn> joinColumns = join.getJoinColumns().iterator(); for (IndexColumn parentPKColumn : parentPK.getKeyColumns()) { JoinColumn joinColumn = joinColumns.next(); if (parentPKColumn.getColumn() != joinColumn.getParent()) { output.reportFailure(new AISValidationFailure ( new JoinToWrongColumnsException ( join.getChild().getName(), joinColumn.getParent().getName(), parentPK.getTable().getName(), parentPKColumn.getColumn().getName()))); } } } }  @Override void validate(AkibanInformationSchema ais, AISValidationOutput output);  }
@Test public void joinOneColumnValid() { builder.table("j1").colInt("c1").colInt("c2").pk("c1").joinTo("t1").on("c2", "c1"); Assert.assertEquals(0, builder.unvalidatedAIS().validate(validations).failures().size()); }
@Test public void joinTwoColumnValid() { builder.table("j2").colInt("c1").colString("c2", 10).pk("c1").joinTo("t2").on("c1", "c1").and("c2", "c2"); Assert.assertEquals(0, builder.unvalidatedAIS().validate(validations).failures().size()); }
@Test public void joinNoPKFailed() { builder.table("j3").colInt("c1").joinTo("t3").on("c1", "c1"); Collection<AISValidationFailure> failures = builder.unvalidatedAIS().validate(validations).failures(); Assert.assertEquals(1, failures.size()); AISValidationFailure fail = failures.iterator().next(); Assert.assertEquals(ErrorCode.JOIN_PARENT_NO_PK, fail.errorCode()); }
@Test public void joinOneToTwoMismatch() { builder.table("j4").colInt("c1").joinTo("t2").on("c1", "c1"); Collection<AISValidationFailure> failures = builder.unvalidatedAIS().validate(validations).failures(); Assert.assertEquals(1, failures.size()); AISValidationFailure fail = failures.iterator().next(); Assert.assertEquals(ErrorCode.JOIN_COLUMN_MISMATCH, fail.errorCode()); }
@Test public void joinTwoToOneMismatch() { builder.table("j5").colInt("c1").colString("c2", 10).joinTo("t1").on("c1","c1").and("c2", "c2"); Collection<AISValidationFailure> failures = builder.unvalidatedAIS().validate(validations).failures(); Assert.assertEquals(1, failures.size()); AISValidationFailure fail = failures.iterator().next(); Assert.assertEquals(ErrorCode.JOIN_COLUMN_MISMATCH, fail.errorCode()); }
@Test public void joinColumnsMismatch () { builder.table("j6").colInt("c1").colString("c2", 10).joinTo("t2").on("c2", "c1").and("c1", "c2"); Collection<AISValidationFailure> failures = builder.unvalidatedAIS().validate(validations).failures(); Assert.assertEquals(2, failures.size()); }
@Test public void joinOrderMismatch() { builder.table("j7").colInt("c1").colString("c2", 10).joinTo("t2").on("c2", "c2").and("c1", "c1"); Collection<AISValidationFailure> failures = builder.unvalidatedAIS().validate(validations).failures(); Assert.assertEquals(2, failures.size()); }
@Test public void joinToNonPKColumns() { builder.table("j8").colInt("c1").colString("c2", 10).joinTo("t1").on("c2", "c2"); Collection<AISValidationFailure> failures = builder.unvalidatedAIS().validate(validations).failures(); Assert.assertEquals(1, failures.size()); AISValidationFailure fail = failures.iterator().next(); Assert.assertEquals(ErrorCode.JOIN_TO_WRONG_COLUMNS, fail.errorCode()); }