Skip to content

Commit

Permalink
feat: check that VirtualTableScan field names correspond to schema
Browse files Browse the repository at this point in the history
  • Loading branch information
vbarua committed Jul 18, 2024
1 parent ada8d0b commit 8bc5997
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
13 changes: 12 additions & 1 deletion core/src/main/java/io/substrait/relation/VirtualTableScan.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ public abstract class VirtualTableScan extends AbstractReadRel {
@Value.Check
protected void check() {
var names = getInitialSchema().names();

assert names.size()
== NamedFieldCountingTypeVisitor.countNames(this.getInitialSchema().struct());
var rows = getRows();

assert rows.size() > 0
&& names.stream().noneMatch(s -> s == null)
&& rows.stream().noneMatch(r -> r == null)
&& rows.stream()
.allMatch(r -> r.getType().accept(new NamedFieldCountingTypeVisitor()) == names.size());
.allMatch(r -> NamedFieldCountingTypeVisitor.countNames(r.getType()) == names.size());
}

@Override
Expand All @@ -44,6 +47,14 @@ public static ImmutableVirtualTableScan.Builder builder() {

private static class NamedFieldCountingTypeVisitor
implements TypeVisitor<Integer, RuntimeException> {

private static final NamedFieldCountingTypeVisitor VISITOR =
new NamedFieldCountingTypeVisitor();

private static Integer countNames(Type type) {
return type.accept(VISITOR);
}

@Override
public Integer visit(Type.Bool type) throws RuntimeException {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ void check() {
R.struct(
R.STRING,
R.struct(R.STRING, R.STRING),
R.list(R.STRING),
R.map(R.STRING, R.STRING))))
R.list(R.struct(R.STRING)),
R.map(R.struct(R.STRING), R.struct(R.STRING)))))
.addRows(
struct(
false,
Expand Down

0 comments on commit 8bc5997

Please sign in to comment.