Skip to content

Commit

Permalink
Determine type for interleaved columns
Browse files Browse the repository at this point in the history
This determines the least type which includes all the columns being
interleaved as the type of the target column.
  • Loading branch information
DavePearce committed Oct 29, 2024
1 parent 6898249 commit 00168e8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
10 changes: 9 additions & 1 deletion pkg/binfile/computation.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,16 @@ func addInterleavedComputation(c *jsonInterleavedComputation, index uint,
target_id := asColumn(c.Target)
dst_col := columns[target_id]
dst_hnd := asHandle(dst_col.Handle)
// Initially assume bottom type
var dst_type sc.Type = sc.NewUintType(0)
// Ensure each column's types included
for i := range sources {
src_col := schema.Columns().Nth(sources[i])
// Update the column type
dst_type = sc.Join(dst_type, src_col.Type())
}
// Finally, add the sorted permutation assignment
schema.AddAssignment(assignment.NewInterleaving(ctx, dst_hnd.column, sources))
schema.AddAssignment(assignment.NewInterleaving(ctx, dst_hnd.column, sources, dst_type))
// Update allocation information.
colmap[target_id] = index
//
Expand Down
2 changes: 1 addition & 1 deletion pkg/hir/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ func (p *hirParser) parseInterleavingDeclaration(l *sexp.List) error {
sources[i] = cid
}
// Add assignment
p.env.AddAssignment(assignment.NewInterleaving(ctx, sexpTarget.Value, sources))
p.env.AddAssignment(assignment.NewInterleaving(ctx, sexpTarget.Value, sources, &sc.FieldType{}))
// Done
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/schema/assignment/interleave.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ type Interleaving struct {
}

// NewInterleaving constructs a new interleaving assignment.
func NewInterleaving(context tr.Context, name string, sources []uint) *Interleaving {
func NewInterleaving(context tr.Context, name string, sources []uint, datatype sc.Type) *Interleaving {
// Update multiplier
context = context.Multiply(uint(len(sources)))
// Fixme: determine interleaving type
target := sc.NewColumn(context, name, &sc.FieldType{})
target := sc.NewColumn(context, name, datatype)

return &Interleaving{target, sources}
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/schema/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,20 @@ func (p *FieldType) Accept(val fr.Element) bool {
func (p *FieldType) String() string {
return "𝔽"
}

// Join compute the Least Upper Bound of two types. For example, the lub of u16
// and u128 is u128, etc.
func Join(lhs Type, rhs Type) Type {
if lhs.AsField() != nil || rhs.AsField() != nil {
return &FieldType{}
}
//
uLhs := lhs.AsUint()
uRhs := rhs.AsUint()
//
if uLhs.nbits >= uRhs.nbits {
return uLhs
}
//
return uRhs
}

0 comments on commit 00168e8

Please sign in to comment.