-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVectorization.py
More file actions
24 lines (20 loc) · 916 Bytes
/
Vectorization.py
File metadata and controls
24 lines (20 loc) · 916 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import torch
# 递归构建查询计划树的特征向量
def construct_feature_vector(query_plan_tree):
def encode_sub_plan(sub_plan):
encoded = []
node_type_id = int(sub_plan.get("Node Type ID", 0))
encoded.append(node_type_id)
encoded.append((sub_plan.get("Total Cost", 0) / 1000000))
encoded.append((sub_plan.get("Plan Rows", 0)))
for sub_sub_plan in sub_plan.get("Plans", []):
encoded.extend(encode_sub_plan(sub_sub_plan))
return encoded
encoded_sequence = []
encoded_sequence.extend(encode_sub_plan(query_plan_tree))
MAX_LENGTH = 30 # 例如
if len(encoded_sequence) < MAX_LENGTH:
encoded_sequence += [0] * (MAX_LENGTH - len(encoded_sequence))
elif len(encoded_sequence) > MAX_LENGTH:
encoded_sequence = encoded_sequence[:MAX_LENGTH]
return torch.tensor(encoded_sequence, dtype=torch.float32)