forked from secretflow/scql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logical_node.go
210 lines (177 loc) · 4.9 KB
/
logical_node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright 2023 Ant Group Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package translator
import (
"fmt"
"github.com/secretflow/scql/pkg/expression"
"github.com/secretflow/scql/pkg/interpreter/ccl"
"github.com/secretflow/scql/pkg/interpreter/graph"
"github.com/secretflow/scql/pkg/parser/ast"
"github.com/secretflow/scql/pkg/planner/core"
"github.com/secretflow/scql/pkg/types"
)
// logicalNode is a wrapper on planner/core:LogicalPlan.
// It adds secure computation info such as visible parties, etc.
type logicalNode interface {
// Methods of core.LogicalPlan
ID() int
Type() string
LP() core.LogicalPlan
Children() []logicalNode
Schema() *expression.Schema
OutputNames() types.NameSlice
String() string
IntoOpt() *ast.SelectIntoOption
InsertTableOpt() *core.InsertTableOption
// buildCCL builds the column control list of logical node's table
buildCCL(*ccl.Context, *ccl.ColumnTracer) error
// CCL returns the column control list of the logical node's table
CCL() map[int64]*ccl.CCL
// VisibleParty returns a list of parties that can see the logical node's table
VisibleParty() []string
// DataSourceParty returns a list of parties that logical node's table comes from
DataSourceParty() []string
SetResultTableWithDTypeCheck([]*graph.Tensor) error
ResultTable() []*graph.Tensor
FindTensorByColumnId(int64) (*graph.Tensor, error)
}
type baseNode struct {
lp core.LogicalPlan
children []logicalNode
rt []*graph.Tensor
ccl map[int64]*ccl.CCL
dataSourceParties []string
}
func (n *baseNode) String() string {
return core.ToString(n.lp)
}
func (n *baseNode) ID() int {
return n.lp.ID()
}
func (n *baseNode) Type() string {
return n.lp.TP()
}
func (n *baseNode) Schema() *expression.Schema {
return n.lp.Schema()
}
func (n *baseNode) OutputNames() types.NameSlice {
return n.lp.OutputNames()
}
func (n *baseNode) Children() []logicalNode {
return n.children
}
func (n *baseNode) IntoOpt() *ast.SelectIntoOption {
return n.lp.IntoOpt()
}
func (n *baseNode) InsertTableOpt() *core.InsertTableOption {
return n.lp.InsertTableOpt()
}
func (n *baseNode) VisibleParty() []string {
var ccls []*ccl.CCL
for _, cc := range n.CCL() {
ccls = append(ccls, cc)
}
partyCodes := ccl.ExtractPartyCodes(ccls)
var visibleParties []string
for _, party := range partyCodes {
visible := true
for _, cc := range n.CCL() {
if !cc.IsVisibleFor(party) {
visible = false
break
}
}
if visible {
visibleParties = append(visibleParties, party)
}
}
return visibleParties
}
func (n *baseNode) DataSourceParty() []string {
return n.dataSourceParties
}
func (n *baseNode) SetResultTableWithDTypeCheck(rt []*graph.Tensor) error {
// check tensor date type matches the logical node's column data type
for i, t := range rt {
col := n.Schema().Columns[i]
expectTp, err := convertDataType(col.RetType)
if err != nil {
return fmt.Errorf("fail to set result table: %v", err)
}
if !t.SkipDTypeCheck && t.DType != expectTp {
return fmt.Errorf("fail to set result table: tensor %v type(=%v) doesn't match scheme type(=%v) in node %v ",
t.UniqueName(), t.DType, expectTp, n.Type())
}
t.CC = n.ccl[col.UniqueID]
}
n.rt = rt
return nil
}
func (n *baseNode) ResultTable() []*graph.Tensor {
return n.rt
}
func (n *baseNode) CCL() map[int64]*ccl.CCL {
return n.ccl
}
func (n *baseNode) buildCCL(colTracer *ccl.ColumnTracer) error {
return fmt.Errorf("please implement this function in child")
}
func (n *baseNode) FindTensorByColumnId(id int64) (*graph.Tensor, error) {
if n.rt == nil || len(n.rt) != len(n.Schema().Columns) {
return nil, fmt.Errorf("findTensorByColumnId: invalid result table %v", n.rt)
}
for i, col := range n.Schema().Columns {
if col.UniqueID == id {
return n.rt[i], nil
}
}
return nil, fmt.Errorf("findTensorByColumnId: column id %v doesn't exists", id)
}
func (n *baseNode) LP() core.LogicalPlan {
return n.lp
}
type JoinNode struct {
baseNode
// record child data source party
childDataSourceParties [][]string
}
type SelectionNode struct {
baseNode
condVis *ccl.CCL
}
type DataSourceNode struct {
baseNode
originCCL map[string]*ccl.CCL
}
type ProjectionNode struct {
baseNode
}
type ApplyNode struct {
baseNode
}
type AggregationNode struct {
baseNode
}
type UnionAllNode struct {
baseNode
}
type LimitNode struct {
baseNode
}
type SortNode struct {
baseNode
}
type WindowNode struct {
baseNode
}