troels commented on a change in pull request #8178:
URL: https://github.com/apache/arrow/pull/8178#discussion_r489773087



##########
File path: cpp/src/arrow/table_builder.cc
##########
@@ -62,7 +62,21 @@ Status RecordBatchBuilder::Flush(bool reset_builders,
     }
     length = fields[i]->length();
   }
-  *batch = RecordBatch::Make(schema_, length, std::move(fields));
+
+  // For certain types like dictionaries, types may not be fully
+  // determined before we have flushed. Make sure that the RecordBatch
+  // gets the correct types in schema.
+  // See: #ARROW-9969
+  std::shared_ptr<Schema> schema(schema_);
+  std::vector<std::shared_ptr<Field>> schema_fields(schema->fields());

Review comment:
       I've changed the logic so that the vector might be modified, and now it 
need to be copied.

##########
File path: cpp/src/arrow/table_builder_test.cc
##########
@@ -151,4 +151,31 @@ TEST_F(TestRecordBatchBuilder, InvalidFieldLength) {
   ASSERT_RAISES(Invalid, builder->Flush(&dummy));
 }
 
+// In #ARROW-9969 dictionary types were not updated
+// in schema when the index width grew.
+TEST_F(TestRecordBatchBuilder, DictionaryTypes) {
+  const int num_rows = static_cast<int>(UINT8_MAX) + 2;
+  std::vector<std::string> f0_values;
+  std::vector<bool> is_valid(num_rows, true);
+  for (int i = 0; i < num_rows; i++) {
+    f0_values.push_back(std::to_string(i));
+  }
+
+  auto f0 = field("f0", dictionary(int8(), utf8()));
+
+  auto schema = ::arrow::schema({f0});
+
+  std::unique_ptr<RecordBatchBuilder> builder;
+  ASSERT_OK(RecordBatchBuilder::Make(schema, pool_, &builder));
+
+  auto b0 = builder->GetFieldAs<StringDictionaryBuilder>(0);
+
+  AppendValues<StringDictionaryBuilder, std::string>(b0, f0_values, is_valid);
+
+  std::shared_ptr<RecordBatch> batch;
+  ASSERT_OK(builder->Flush(&batch));
+
+  
ASSERT_TRUE(batch->column(0)->type()->Equals(batch->schema()->field(0)->type()));

Review comment:
       Ok




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to