html_url,issue_url,id,node_id,user,created_at,updated_at,author_association,body,reactions,issue,performed_via_github_app
https://github.com/simonw/datasette/issues/1293#issuecomment-899915829,https://api.github.com/repos/simonw/datasette/issues/1293,899915829,IC_kwDOBm6k_c41o6A1,9599,2021-08-17T01:02:35Z,2021-08-17T01:02:35Z,OWNER,"New approach: this time I'm building a simplified executor for the bytecode operations themselves.
```python
def execute_operations(operations, max_iterations = 100, trace=None):
trace = trace or (lambda *args: None)
registers: Dict[int, Any] = {}
cursors: Dict[int, Tuple[str, Dict]] = {}
instruction_pointer = 0
iterations = 0
result_row = None
while True:
iterations += 1
if iterations > max_iterations:
break
operation = operations[instruction_pointer]
trace(instruction_pointer, dict(operation))
opcode = operation[""opcode""]
if opcode == ""Init"":
if operation[""p2""] != 0:
instruction_pointer = operation[""p2""]
continue
else:
instruction_pointer += 1
continue
elif opcode == ""Goto"":
instruction_pointer = operation[""p2""]
continue
elif opcode == ""Halt"":
break
elif opcode == ""OpenRead"":
cursors[operation[""p1""]] = (""database_table"", {
""rootpage"": operation[""p2""],
""connection"": operation[""p3""],
})
elif opcode == ""OpenEphemeral"":
cursors[operation[""p1""]] = (""ephemeral"", {
""num_columns"": operation[""p2""],
""index_keys"": [],
})
elif opcode == ""MakeRecord"":
registers[operation[""p3""]] = (""MakeRecord"", {
""registers"": list(range(operation[""p1""] + operation[""p2""]))
})
elif opcode == ""IdxInsert"":
record = registers[operation[""p2""]]
cursors[operation[""p1""]][1][""index_keys""].append(record)
elif opcode == ""Rowid"":
registers[operation[""p2""]] = (""rowid"", {
""table"": operation[""p1""]
})
elif opcode == ""Sequence"":
registers[operation[""p2""]] = (""sequence"", {
""next_from_cursor"": operation[""p1""]
})
elif opcode == ""Column"":
registers[operation[""p3""]] = (""column"", {
""cursor"": operation[""p1""],
""column_offset"": operation[""p2""]
})
elif opcode == ""ResultRow"":
p1 = operation[""p1""]
p2 = operation[""p2""]
trace(""ResultRow: "", list(range(p1, p1 + p2)), registers)
result_row = [registers.get(i) for i in range(p1, p1 + p2)]
elif opcode == ""Integer"":
registers[operation[""p2""]] = (""Integer"", operation[""p1""])
elif opcode == ""String8"":
registers[operation[""p2""]] = (""String"", operation[""p4""])
instruction_pointer += 1
return {""registers"": registers, ""cursors"": cursors, ""result_row"": result_row}
```
Results are promising!
```
execute_operations(db.execute(""explain select 'hello', 55, rowid, * from searchable"").fetchall())
{'registers': {1: ('String', 'hello'),
2: ('Integer', 55),
3: ('rowid', {'table': 0}),
4: ('rowid', {'table': 0}),
5: ('column', {'cursor': 0, 'column_offset': 1}),
6: ('column', {'cursor': 0, 'column_offset': 2}),
7: ('column', {'cursor': 0, 'column_offset': 3})},
'cursors': {0: ('database_table', {'rootpage': 32, 'connection': 0})},
'result_row': [('String', 'hello'),
('Integer', 55),
('rowid', {'table': 0}),
('rowid', {'table': 0}),
('column', {'cursor': 0, 'column_offset': 1}),
('column', {'cursor': 0, 'column_offset': 2}),
('column', {'cursor': 0, 'column_offset': 3})]}
```
Here's what happens with a union across three tables:
```
execute_operations(db.execute(f""""""
explain select data as content from binary_data
union
select pk as content from complex_foreign_keys
union
select name as content from facet_cities
""""""}).fetchall())
{'registers': {1: ('column', {'cursor': 4, 'column_offset': 0}),
2: ('MakeRecord', {'registers': [0, 1, 2, 3]}),
3: ('column', {'cursor': 0, 'column_offset': 1}),
4: ('column', {'cursor': 3, 'column_offset': 0})},
'cursors': {3: ('ephemeral',
{'num_columns': 1,
'index_keys': [('MakeRecord', {'registers': [0, 1]}),
('MakeRecord', {'registers': [0, 1]}),
('MakeRecord', {'registers': [0, 1, 2, 3]})]}),
2: ('database_table', {'rootpage': 44, 'connection': 0}),
4: ('database_table', {'rootpage': 24, 'connection': 0}),
0: ('database_table', {'rootpage': 42, 'connection': 0})},
'result_row': [('column', {'cursor': 3, 'column_offset': 0})]}
```
Note how the result_row refers to cursor 3, which is an ephemeral table which had three different sets of `MakeRecord` index keys assigned to it - indicating that the output column is NOT from the same underlying table source.","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",849978964,