YannByron commented on code in PR #5201:
URL: https://github.com/apache/hudi/pull/5201#discussion_r841309398


##########
hudi-common/src/main/java/org/apache/hudi/common/table/TableSchemaResolver.java:
##########
@@ -159,23 +166,51 @@ public Schema getTableAvroSchema() throws Exception {
    * @throws Exception
    */
   public Schema getTableAvroSchema(boolean includeMetadataFields) throws 
Exception {
+    Schema schema;
     Option<Schema> schemaFromCommitMetadata = 
getTableSchemaFromCommitMetadata(includeMetadataFields);
     if (schemaFromCommitMetadata.isPresent()) {
-      return schemaFromCommitMetadata.get();
-    }
-    Option<Schema> schemaFromTableConfig = 
metaClient.getTableConfig().getTableCreateSchema();
-    if (schemaFromTableConfig.isPresent()) {
-      if (includeMetadataFields) {
-        return HoodieAvroUtils.addMetadataFields(schemaFromTableConfig.get(), 
hasOperationField);
+      schema = schemaFromCommitMetadata.get();
+    } else {
+      Option<Schema> schemaFromTableConfig = 
metaClient.getTableConfig().getTableCreateSchema();
+      if (schemaFromTableConfig.isPresent()) {
+        if (includeMetadataFields) {
+          schema = 
HoodieAvroUtils.addMetadataFields(schemaFromTableConfig.get(), 
hasOperationField);
+        } else {
+          schema = schemaFromTableConfig.get();
+        }
       } else {
-        return schemaFromTableConfig.get();
+        if (includeMetadataFields) {
+          schema = getTableAvroSchemaFromDataFile();
+        } else {
+          schema = 
HoodieAvroUtils.removeMetadataFields(getTableAvroSchemaFromDataFile());
+        }
       }
     }
-    if (includeMetadataFields) {
-      return getTableAvroSchemaFromDataFile();
-    } else {
-      return 
HoodieAvroUtils.removeMetadataFields(getTableAvroSchemaFromDataFile());
+
+    if (metaClient.getTableConfig().getDropPartitionColumnsWhenWrite()) {
+      // when hoodie.datasource.write.drop.partition.columns is true, 
partition columns can't be persisted in data files.
+      // And there are no partition schema if the schema is parsed from data 
files.
+      // Here we create partition Fields for this case, and use StringType as 
the data type.
+      Option<String[]> partitionFieldsOpt = 
metaClient.getTableConfig().getPartitionFields();
+      if (partitionFieldsOpt.isPresent() && partitionFieldsOpt.get().length != 
0) {
+        List<String> partitionFields = Arrays.asList(partitionFieldsOpt.get());
+
+        final Schema schema0 = schema;
+        boolean allPartitionColInSchema = partitionFields.stream().allMatch(
+            pt -> HoodieAvroUtils.containsFieldInSchema(schema0, pt)
+        );
+
+        if (!allPartitionColInSchema) {

Review Comment:
   i think about the case you comment above, and treat the  case on `1 out of 3 
partition fields not contained` as the impossible one. so do not deal with this 
obviously.



-- 
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: dev-unsubscr...@hudi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to