Skip to content
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
18 changes: 17 additions & 1 deletion plan/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ type Builder interface {
// Deprecated: Use VirtualTableFromExpr(...).Remap() instead.
VirtualTableFromExprRemap(fieldNames []string, remap []int32, values ...expr.VirtualTableExpressionValue) (*VirtualTableReadRel, error)
VirtualTableFromExpr(fieldNames []string, values ...expr.VirtualTableExpressionValue) (*VirtualTableReadRel, error)
EmptyVirtualTable(fieldNames []string, types []types.Type) (*VirtualTableReadRel, error)
IcebergTableFromMetadataFile(metadataURI string, snapshot IcebergSnapshot, schema types.NamedStruct) (*IcebergTableReadRel, error)
// Deprecated: Use Sort(...).Remap() instead.
SortRemap(input Rel, remap []int32, sorts ...expr.SortField) (*SortRel, error)
Expand Down Expand Up @@ -568,7 +569,7 @@ func (b *builder) VirtualTableFromExpr(fieldNames []string, values ...expr.Virtu

func (b *builder) VirtualTableFromExprRemap(fieldNames []string, remap []int32, values ...expr.VirtualTableExpressionValue) (*VirtualTableReadRel, error) {
if len(values) == 0 {
return nil, fmt.Errorf("%w: must provide at least one set of values for virtual table", substraitgo.ErrInvalidRel)
return nil, fmt.Errorf("%w: must provide at least one set of values. Consider EmptyVirtualTable to construct rowless Virtual Table", substraitgo.ErrInvalidArg)
}

nfields := len(fieldNames)
Expand Down Expand Up @@ -619,6 +620,21 @@ func (b *builder) VirtualTable(fields []string, values ...expr.StructLiteralValu
return b.VirtualTableRemap(fields, nil, values...)
}

func (b *builder) EmptyVirtualTable(fieldNames []string, typeList []types.Type) (*VirtualTableReadRel, error) {
baseSchema := types.NamedStruct{
Names: fieldNames,
Struct: types.StructType{
Nullability: types.NullabilityRequired,
Types: typeList,
},
}
return &VirtualTableReadRel{
baseReadRel: baseReadRel{
baseSchema: baseSchema,
},
}, nil
}

func (b *builder) IcebergTableFromMetadataFile(metadataURI string, snapshot IcebergSnapshot, schema types.NamedStruct) (*IcebergTableReadRel, error) {
tableType := &Direct{}
tableType.MetadataUri = metadataURI
Expand Down
2 changes: 1 addition & 1 deletion plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ func RelFromProto(rel *proto.Rel, reg expr.ExtensionRegistry) (Rel, error) {
}
case *proto.ReadRel_VirtualTable_:
if len(readType.VirtualTable.Values) > 0 && len(readType.VirtualTable.Expressions) > 0 {
return nil, fmt.Errorf("VirtualTable Value can't have both liternal and expression")
return nil, fmt.Errorf("VirtualTable cannot declare both Values and Expressions")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opportunistic error message clarification

}
var values []expr.VirtualTableExpressionValue
for _, v := range readType.VirtualTable.Values {
Expand Down
62 changes: 42 additions & 20 deletions plan/plan_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1629,7 +1629,7 @@ func TestSetRelations(t *testing.T) {
checkRoundTrip(t, expectedJSON, p)
}

func TestEmptyVirtualTable(t *testing.T) {
func TestColumnlessVirtualTable(t *testing.T) {
const expectedJSON = `{
` + versionStruct + `,
"relations": [
Expand All @@ -1647,24 +1647,7 @@ func TestEmptyVirtualTable(t *testing.T) {
"expressions": [
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{}
{}
]
}
}
Expand All @@ -1676,7 +1659,7 @@ func TestEmptyVirtualTable(t *testing.T) {

b := plan.NewBuilderDefault()

virtual, err := b.VirtualTable(nil, make([]expr.StructLiteralValue, 20)...)
virtual, err := b.VirtualTable(nil, make([]expr.StructLiteralValue, 3)...)
require.NoError(t, err)

p, err := b.Plan(virtual, []string{})
Expand All @@ -1685,6 +1668,45 @@ func TestEmptyVirtualTable(t *testing.T) {
checkRoundTrip(t, expectedJSON, p)
}

func TestEmptyVirtualTable(t *testing.T) {
const expectedJSON = `{
` + versionStruct + `,
"relations": [
{
"root": {
"input": {
"read": {
"common": {"direct":{}},
"baseSchema": {
"names": ["i"],
"struct": {
"types": [
{"i32": {"nullability": "NULLABILITY_REQUIRED"}}
],
"nullability": "NULLABILITY_REQUIRED"
}
},
"virtualTable": {}
}
},
"names": ["i"]
}
}
]
}`

b := plan.NewBuilderDefault()

i32Type := types.Int32Type{Nullability: types.NullabilityRequired}
virtual, err := b.EmptyVirtualTable([]string{"i"}, []types.Type{&i32Type})
require.NoError(t, err)

p, err := b.Plan(virtual, []string{"i"})
require.NoError(t, err)

checkRoundTrip(t, expectedJSON, p)
}

func TestSetRelErrors(t *testing.T) {
b := plan.NewBuilderDefault()

Expand Down
Loading