ianmcook commented on code in PR #12:
URL: https://github.com/apache/arrow-experiments/pull/12#discussion_r1518726144
##########
http/get_simple/python/server/server.py:
##########
@@ -58,8 +58,6 @@ def make_reader(schema, batches):
def generate_batches(schema, reader):
with io.BytesIO() as sink, pa.ipc.new_stream(sink, schema) as writer:
- yield sink.getvalue()
-
Review Comment:
Ah, I figured it out: if you call `new_stream()` and don't write any
batches, then it writes the schema to the sink _when the writer is closed_. So
this pre-close `yield sink.getvalue()` will always yield an empty bytestring.
Simple example to show this:
```py
import pyarrow as pa
import io
data = [pa.array([1])]
batch = pa.record_batch(data, names=['a'])
# for reference: what does the serialized schema alone look like?
batch.schema.serialize().to_pybytes()
#
b'\xff\xff\xff\xffx\x00\x00\x00\x10\x00\x00\x00\x00\x00\n\x00\x0c\x00\x06\x00\x05\x00\x08\x00\n\x00\x00\x00\x00\x01\x04\x00\x0c\x00\x00\x00\x08\x00\x08\x00\x00\x00\x04\x00\x08\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\x14\x00\x00\x00\x10\x00\x14\x00\x08\x00\x06\x00\x07\x00\x0c\x00\x00\x00\x10\x00\x10\x00\x00\x00\x00\x00\x01\x02\x10\x00\x00\x00\x1c\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00a\x00\x00\x00\x08\x00\x0c\x00\x08\x00\x07\x00\x08\x00\x00\x00\x00\x00\x00\x01@\x00\x00\x00'
# question: what's in `sink` immediately after we call `new_stream()`?
sink = io.BytesIO()
writer = pa.ipc.new_stream(sink, batch.schema)
sink.getvalue()
writer.close()
# b''
# answer: empty bytes
# question: what's in `sink` after we call `new_stream()` then close the
writer?
sink = io.BytesIO()
writer = pa.ipc.new_stream(sink, batch.schema)
writer.close()
sink.getvalue()
#
b'\xff\xff\xff\xffx\x00\x00\x00\x10\x00\x00\x00\x00\x00\n\x00\x0c\x00\x06\x00\x05\x00\x08\x00\n\x00\x00\x00\x00\x01\x04\x00\x0c\x00\x00\x00\x08\x00\x08\x00\x00\x00\x04\x00\x08\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\x14\x00\x00\x00\x10\x00\x14\x00\x08\x00\x06\x00\x07\x00\x0c\x00\x00\x00\x10\x00\x10\x00\x00\x00\x00\x00\x01\x02\x10\x00\x00\x00\x1c\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00a\x00\x00\x00\x08\x00\x0c\x00\x08\x00\x07\x00\x08\x00\x00\x00\x00\x00\x00\x01@\x00\x00\x00\xff\xff\xff\xff\x00\x00\x00\x00'
# answer: the schema and the EOS bytes
```
And it's safe to remove the pre-close `yield sink.getvalue()` because the
schema will still be written to the beginning of the sink:
```py
# question: does the schema still get written to the sink if we `seek(0)`
# and `truncate(0)` the sink immediately after calling `new_stream()`?
sink = io.BytesIO()
writer = pa.ipc.new_stream(sink, batch.schema)
sink.seek(0)
sink.truncate(0)
writer.write_batch(batch)
writer.close()
sink.getvalue()
#
b'\xff\xff\xff\xffx\x00\x00\x00\x10\x00\x00\x00\x00\x00\n\x00\x0c\x00\x06\x00\x05\x00\x08\x00\n\x00\x00\x00\x00\x01\x04\x00\x0c\x00\x00\x00\x08\x00\x08\x00\x00\x00\x04\x00\x08\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\x14\x00\x00\x00\x10\x00\x14\x00\x08\x00\x06\x00\x07\x00\x0c\x00\x00\x00\x10\x00\x10\x00\x00\x00\x00\x00\x01\x02\x10\x00\x00\x00\x1c\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00a\x00\x00\x00\x08\x00\x0c\x00\x08\x00\x07\x00\x08\x00\x00\x00\x00\x00\x00\x01@\x00\x00\x00\xff\xff\xff\xff\x88\x00\x00\x00\x14\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x16\x00\x06\x00\x05\x00\x08\x00\x0c\x00\x0c\x00\x00\x00\x00\x03\x04\x00\x18\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\n\x00\x18\x00\x0c\x00\x04\x00\x08\x00\n\x00\x00\x00<\x00\x00\x00\x10\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0
1\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\x00\x00\x00\x00'
# answer: yes
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]