Skip to content

Commit f42fc3e

Browse files
committed
Allow IntSequenceTable to take a non-constant argument.
1 parent 7b8fb20 commit f42fc3e

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

memory/lookup_squence_table.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var _ sql.TableNode = LookupSequenceTable{}
1818
// LookupSequenceTable is a variation of IntSequenceTable that supports lookups and implements sql.TableNode
1919
type LookupSequenceTable struct {
2020
IntSequenceTable
21+
length int64
2122
}
2223

2324
func (s LookupSequenceTable) UnderlyingTable() sql.Table {
@@ -29,7 +30,15 @@ func (s LookupSequenceTable) NewInstance(ctx *sql.Context, db sql.Database, args
2930
if err != nil {
3031
return nil, err
3132
}
32-
return LookupSequenceTable{newIntSequenceTable.(IntSequenceTable)}, nil
33+
lenExp, ok := args[1].(*expression.Literal)
34+
if !ok {
35+
return nil, fmt.Errorf("sequence table expects arguments to be literal expressions")
36+
}
37+
length, _, err := types.Int64.Convert(ctx, lenExp.Value())
38+
if err != nil {
39+
return nil, err
40+
}
41+
return LookupSequenceTable{newIntSequenceTable.(IntSequenceTable), length.(int64)}, nil
3342
}
3443

3544
func (s LookupSequenceTable) String() string {
@@ -82,21 +91,21 @@ func (s LookupSequenceTable) Description() string {
8291

8392
// Partitions is a sql.Table interface function that returns a partition of the data. This data has a single partition.
8493
func (s LookupSequenceTable) Partitions(ctx *sql.Context) (sql.PartitionIter, error) {
85-
return sql.PartitionsToPartitionIter(&sequencePartition{min: 0, max: int64(s.Len) - 1}), nil
94+
return sql.PartitionsToPartitionIter(&sequencePartition{min: 0, max: s.length - 1}), nil
8695
}
8796

8897
// PartitionRows is a sql.Table interface function that takes a partition and returns all rows in that partition.
8998
// This table has a partition for just schema changes, one for just data changes, and one for both.
9099
func (s LookupSequenceTable) PartitionRows(ctx *sql.Context, partition sql.Partition) (sql.RowIter, error) {
91100
sp, ok := partition.(*sequencePartition)
92101
if !ok {
93-
return &SequenceTableFnRowIter{i: 0, n: s.Len}, nil
102+
return &SequenceTableFnRowIter{i: 0, n: s.length}, nil
94103
}
95104
min := int64(0)
96105
if sp.min > min {
97106
min = sp.min
98107
}
99-
max := int64(s.Len) - 1
108+
max := int64(s.length) - 1
100109
if sp.max < max {
101110
max = sp.max
102111
}

memory/sequence_table.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var _ sql.ExecSourceRel = IntSequenceTable{}
1919
type IntSequenceTable struct {
2020
db sql.Database
2121
name string
22-
Len int64
22+
Len sql.Expression
2323
}
2424

2525
func (s IntSequenceTable) NewInstance(ctx *sql.Context, db sql.Database, args []sql.Expression) (sql.Node, error) {
@@ -34,15 +34,11 @@ func (s IntSequenceTable) NewInstance(ctx *sql.Context, db sql.Database, args []
3434
if !ok {
3535
return nil, fmt.Errorf("sequence table expects 1st argument to be column name")
3636
}
37-
lenExp, ok := args[1].(*expression.Literal)
38-
if !ok {
39-
return nil, fmt.Errorf("sequence table expects arguments to be literal expressions")
37+
lenExp := args[1]
38+
if !sql.IsNumberType(lenExp.Type()) {
39+
return nil, fmt.Errorf("sequence table expects length argument to be a number")
4040
}
41-
length, _, err := types.Int64.Convert(ctx, lenExp.Value())
42-
if !ok {
43-
return nil, fmt.Errorf("%w; sequence table expects 2nd argument to be a sequence length integer", err)
44-
}
45-
return IntSequenceTable{db: db, name: name, Len: length.(int64)}, nil
41+
return IntSequenceTable{db: db, name: name, Len: lenExp}, nil
4642
}
4743

4844
func (s IntSequenceTable) Resolved() bool {
@@ -85,8 +81,20 @@ func (s IntSequenceTable) Children() []sql.Node {
8581
return []sql.Node{}
8682
}
8783

88-
func (s IntSequenceTable) RowIter(_ *sql.Context, _ sql.Row) (sql.RowIter, error) {
89-
rowIter := &SequenceTableFnRowIter{i: 0, n: s.Len}
84+
func (s IntSequenceTable) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
85+
iterLen, err := s.Len.Eval(ctx, row)
86+
if err != nil {
87+
return nil, err
88+
}
89+
iterLenVal, ok, err := types.Int64.Convert(ctx, iterLen)
90+
if err != nil {
91+
return nil, err
92+
}
93+
if !ok {
94+
return nil, fmt.Errorf("sequence table expects integer argument")
95+
}
96+
97+
rowIter := &SequenceTableFnRowIter{i: 0, n: iterLenVal.(int64)}
9098
return rowIter, nil
9199
}
92100

0 commit comments

Comments
 (0)