|
| 1 | +package expressions |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/efritz/gostgres/internal/execution/engine/serialization" |
| 8 | + "github.com/efritz/gostgres/internal/execution/scan" |
| 9 | + "github.com/efritz/gostgres/internal/shared/impls" |
| 10 | + "github.com/efritz/gostgres/internal/shared/rows" |
| 11 | +) |
| 12 | + |
| 13 | +type Query interface { |
| 14 | + Serialize(w serialization.IndentWriter) |
| 15 | + Optimize() |
| 16 | + Scanner(ctx impls.Context) (scan.Scanner, error) |
| 17 | +} |
| 18 | + |
| 19 | +// |
| 20 | +// |
| 21 | +// |
| 22 | + |
| 23 | +type existsSubqueryExpression struct { |
| 24 | + subquery Query |
| 25 | +} |
| 26 | + |
| 27 | +func NewExistsSubqueryExpression(subquery Query) impls.Expression { |
| 28 | + return existsSubqueryExpression{ |
| 29 | + subquery: subquery, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func (e existsSubqueryExpression) String() string { return "" } // TODO |
| 34 | +func (e existsSubqueryExpression) Equal(other impls.Expression) bool { return false } // TODO |
| 35 | +func (e existsSubqueryExpression) Children() []impls.Expression { return nil } // TODO |
| 36 | +func (e existsSubqueryExpression) Fold() impls.Expression { return e } // TODO |
| 37 | +func (e existsSubqueryExpression) Map(f func(impls.Expression) impls.Expression) impls.Expression { |
| 38 | + return e |
| 39 | +} // TODO |
| 40 | + |
| 41 | +func (e existsSubqueryExpression) ValueFrom(ctx impls.Context, row rows.Row) (any, error) { |
| 42 | + // TODO - want to pull this out and do optimization before the enclosing query is executed |
| 43 | + // TODO - need to pull out serialization into separate parts in the explain output as well |
| 44 | + ctx.Log("Optimizing subquery") |
| 45 | + e.subquery.Optimize() |
| 46 | + |
| 47 | + var buf bytes.Buffer |
| 48 | + w := serialization.NewIndentWriter(&buf) |
| 49 | + e.subquery.Serialize(w) |
| 50 | + ctx.Log("%s", buf.String()) |
| 51 | + |
| 52 | + ctx.Log("Preparing subquery") |
| 53 | + |
| 54 | + s, err := e.subquery.Scanner(ctx) |
| 55 | + if err != nil { |
| 56 | + return nil, err |
| 57 | + } |
| 58 | + |
| 59 | + ctx.Log("Scanning subquery") |
| 60 | + |
| 61 | + rowx, err := s.Scan() |
| 62 | + if err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + |
| 66 | + fmt.Printf("? %#v\n", rowx) |
| 67 | + panic("OH NO") |
| 68 | +} |
| 69 | + |
| 70 | +// |
| 71 | +// |
| 72 | +// |
| 73 | + |
| 74 | +type anySubqueryExpression struct { |
| 75 | + expression impls.Expression |
| 76 | + op string // TODO |
| 77 | + subquery Query |
| 78 | +} |
| 79 | + |
| 80 | +func NewAnySubqueryExpression(expression impls.Expression, op string, subquery Query) impls.Expression { |
| 81 | + return anySubqueryExpression{ |
| 82 | + expression: expression, |
| 83 | + op: op, |
| 84 | + subquery: subquery, |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func (e anySubqueryExpression) String() string { return "" } // TODO |
| 89 | +func (e anySubqueryExpression) Equal(other impls.Expression) bool { return false } // TODO |
| 90 | +func (e anySubqueryExpression) Children() []impls.Expression { return nil } // TODO |
| 91 | +func (e anySubqueryExpression) Fold() impls.Expression { return e } // TODO |
| 92 | +func (e anySubqueryExpression) Map(f func(impls.Expression) impls.Expression) impls.Expression { |
| 93 | + return e |
| 94 | +} // TODO |
| 95 | + |
| 96 | +func (e anySubqueryExpression) ValueFrom(ctx impls.Context, row rows.Row) (any, error) { |
| 97 | + return nil, fmt.Errorf("unimplemented") // TODO |
| 98 | +} |
| 99 | + |
| 100 | +// |
| 101 | +// |
| 102 | +// |
| 103 | + |
| 104 | +type allSubqueryExpression struct { |
| 105 | + expression impls.Expression |
| 106 | + op string // TODO |
| 107 | + subquery Query |
| 108 | +} |
| 109 | + |
| 110 | +func NewAllSubqueryExpression(expression impls.Expression, op string, subquery Query) impls.Expression { |
| 111 | + return allSubqueryExpression{ |
| 112 | + expression: expression, |
| 113 | + op: op, |
| 114 | + subquery: subquery, |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +func (e allSubqueryExpression) String() string { return "" } // TODO |
| 119 | +func (e allSubqueryExpression) Equal(other impls.Expression) bool { return false } // TODO |
| 120 | +func (e allSubqueryExpression) Children() []impls.Expression { return nil } // TODO |
| 121 | +func (e allSubqueryExpression) Fold() impls.Expression { return e } // TODO |
| 122 | +func (e allSubqueryExpression) Map(f func(impls.Expression) impls.Expression) impls.Expression { |
| 123 | + return e |
| 124 | +} // TODO |
| 125 | + |
| 126 | +func (e allSubqueryExpression) ValueFrom(ctx impls.Context, row rows.Row) (any, error) { |
| 127 | + return nil, fmt.Errorf("unimplemented") // TODO |
| 128 | +} |
| 129 | + |
| 130 | +// |
| 131 | +// |
| 132 | +// |
| 133 | + |
| 134 | +type opSubqueryExpression struct { |
| 135 | + expression impls.Expression |
| 136 | + op string // TODO |
| 137 | + subquery Query |
| 138 | +} |
| 139 | + |
| 140 | +func NewOpSubqueryExpression(expression impls.Expression, op string, subquery Query) impls.Expression { |
| 141 | + return opSubqueryExpression{ |
| 142 | + expression: expression, |
| 143 | + op: op, |
| 144 | + subquery: subquery, |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +func (e opSubqueryExpression) String() string { return "" } // TODO |
| 149 | +func (e opSubqueryExpression) Equal(other impls.Expression) bool { return false } // TODO |
| 150 | +func (e opSubqueryExpression) Children() []impls.Expression { return nil } // TODO |
| 151 | +func (e opSubqueryExpression) Fold() impls.Expression { return e } // TODO |
| 152 | +func (e opSubqueryExpression) Map(f func(impls.Expression) impls.Expression) impls.Expression { |
| 153 | + return e |
| 154 | +} // TODO |
| 155 | + |
| 156 | +func (e opSubqueryExpression) ValueFrom(ctx impls.Context, row rows.Row) (any, error) { |
| 157 | + return nil, fmt.Errorf("unimplemented") // TODO |
| 158 | +} |
0 commit comments