Skip to content
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

fix: set VirtualTableScan schema explicitly #272

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,11 @@ private VirtualTableScan newVirtualTable(ReadRel rel) {
.collect(java.util.stream.Collectors.toList()))
.build());
}
var fieldNames =
rel.getBaseSchema().getNamesList().stream().collect(java.util.stream.Collectors.toList());

var builder =
VirtualTableScan.builder()
.filter(Optional.ofNullable(rel.hasFilter() ? converter.from(rel.getFilter()) : null))
.addAllDfsNames(fieldNames)
.initialSchema(NamedStruct.fromProto(rel.getBaseSchema(), protoTypeConverter))
vbarua marked this conversation as resolved.
Show resolved Hide resolved
.rows(structLiterals);

builder
Expand Down
10 changes: 1 addition & 9 deletions core/src/main/java/io/substrait/relation/VirtualTableScan.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.substrait.relation;

import io.substrait.expression.Expression;
import io.substrait.type.NamedStruct;
import io.substrait.type.Type;
import io.substrait.type.TypeVisitor;
import java.util.List;
Expand All @@ -10,8 +9,6 @@
@Value.Immutable
public abstract class VirtualTableScan extends AbstractReadRel {

public abstract List<String> getDfsNames();
vbarua marked this conversation as resolved.
Show resolved Hide resolved

public abstract List<Expression.StructLiteral> getRows();

/**
Expand All @@ -26,7 +23,7 @@ public abstract class VirtualTableScan extends AbstractReadRel {
*/
@Value.Check
protected void check() {
var names = getDfsNames();
var names = getInitialSchema().names();
var rows = getRows();

assert rows.size() > 0
Expand All @@ -36,11 +33,6 @@ protected void check() {
.allMatch(r -> r.getType().accept(new NamedFieldCountingTypeVisitor()) == names.size());
}

@Override
public final NamedStruct getInitialSchema() {
return NamedStruct.of(getDfsNames(), (Type.Struct) getRows().get(0).getType());
vbarua marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public <O, E extends Exception> O accept(RelVisitor<O, E> visitor) throws E {
return visitor.visit(this);
Expand Down
36 changes: 25 additions & 11 deletions core/src/test/java/io/substrait/relation/VirtualTableScanTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,41 @@
import static io.substrait.expression.ExpressionCreator.struct;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

import io.substrait.TestBase;
import io.substrait.expression.Expression;
import io.substrait.type.NamedStruct;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;

class VirtualTableScanTest {
class VirtualTableScanTest extends TestBase {

@Test
void check() {
VirtualTableScan virtualTableScan =
ImmutableVirtualTableScan.builder()
.addDfsNames(
"string",
"struct",
"struct_field1",
"struct_field2",
"list",
"list_struct_field1",
"map",
"map_key_struct_field1",
"map_value_struct_field1")
.initialSchema(
NamedStruct.of(
Arrays.stream(
new String[] {
"string",
"struct",
"struct_field1",
"struct_field2",
"list",
"list_struct_field1",
"map",
"map_key_struct_field1",
"map_value_struct_field1"
})
.collect(Collectors.toList()),
R.struct(
R.STRING,
R.struct(R.STRING, R.STRING),
R.list(R.STRING),
R.map(R.STRING, R.STRING))))
.addRows(
struct(
false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.substrait.relation.ProtoRelConverter;
import io.substrait.relation.RelProtoConverter;
import io.substrait.relation.VirtualTableScan;
import io.substrait.type.NamedStruct;
import io.substrait.type.TypeCreator;
import java.io.IOException;
import java.math.BigDecimal;
Expand All @@ -25,8 +26,12 @@ public class AggregateRoundtripTest extends TestBase {
private void assertAggregateRoundtrip(Expression.AggregationInvocation invocation) {
var expression = ExpressionCreator.decimal(false, BigDecimal.TEN, 10, 2);
Expression.StructLiteral literal =
ImmutableExpression.StructLiteral.builder().from(expression).build();
var input = VirtualTableScan.builder().addRows(literal).build();
ImmutableExpression.StructLiteral.builder().addFields(expression).build();
var input =
VirtualTableScan.builder()
.initialSchema(NamedStruct.of(Arrays.asList("decimal"), R.struct(R.decimal(10, 2))))
.addRows(literal)
.build();
ExtensionCollector functionCollector = new ExtensionCollector();
var to = new RelProtoConverter(functionCollector);
var extensions = defaultExtensionCollection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ protected void verifyRoundTrip(Rel rel) {
void virtualTable() {
Rel rel =
VirtualTableScan.builder()
.initialSchema(NamedStruct.of(Collections.emptyList(), R.struct()))
.addRows(Expression.StructLiteral.builder().fields(Collections.emptyList()).build())
.commonExtension(commonExtension)
.extension(relExtension)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ void emptyScan() {
void virtualTable() {
var virtTable =
VirtualTableScan.builder()
.addAllDfsNames(Stream.of("column1", "column2").collect(Collectors.toList()))
.initialSchema(
NamedStruct.of(
Stream.of("column1", "column2").collect(Collectors.toList()),
R.struct(R.I64, R.I64)))
.addRows(
ExpressionCreator.struct(
false, ExpressionCreator.i64(false, 1), ExpressionCreator.i64(false, 2)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public Rel visit(org.apache.calcite.rel.core.Values values) {
return ExpressionCreator.struct(false, fields);
})
.collect(Collectors.toUnmodifiableList());
return VirtualTableScan.builder().addAllDfsNames(type.names()).addAllRows(structs).build();
return VirtualTableScan.builder().initialSchema(type).addAllRows(structs).build();
}

@Override
Expand Down