Copilot commented on code in PR #10402:
URL: https://github.com/apache/seatunnel/pull/10402#discussion_r2791765007


##########
seatunnel-api/src/main/java/org/apache/seatunnel/api/metalake/TableSchemaDiscoverer.java:
##########
@@ -0,0 +1,239 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.seatunnel.api.metalake;
+
+import org.apache.seatunnel.shade.com.fasterxml.jackson.databind.JsonNode;
+import 
org.apache.seatunnel.shade.com.google.common.annotations.VisibleForTesting;
+import org.apache.seatunnel.shade.org.apache.commons.lang3.StringUtils;
+
+import org.apache.seatunnel.api.configuration.ReadonlyConfig;
+import org.apache.seatunnel.api.options.ConnectorCommonOptions;
+import org.apache.seatunnel.api.options.EnvCommonOptions;
+import org.apache.seatunnel.api.options.table.CatalogOptions;
+import org.apache.seatunnel.api.options.table.ColumnOptions;
+import org.apache.seatunnel.api.options.table.FieldOptions;
+import org.apache.seatunnel.api.options.table.TableIdentifierOptions;
+import org.apache.seatunnel.api.options.table.TableSchemaOptions;
+import org.apache.seatunnel.api.table.catalog.CatalogTable;
+import org.apache.seatunnel.api.table.catalog.CatalogTableUtil;
+import org.apache.seatunnel.api.table.catalog.TableIdentifier;
+import org.apache.seatunnel.api.table.catalog.TablePath;
+import org.apache.seatunnel.api.table.catalog.TableSchema;
+import org.apache.seatunnel.api.table.factory.TableSourceFactoryContext;
+import org.apache.seatunnel.common.constants.MetaLakeType;
+import org.apache.seatunnel.common.exception.SeaTunnelRuntimeException;
+
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+
+import static 
org.apache.seatunnel.api.table.schema.exception.SchemaEvolutionErrorCode.GET_META_LAKE_TABLE_SCHEMA_FAILED;
+
+@Slf4j
+public class TableSchemaDiscoverer implements AutoCloseable {
+
+    private final ReadonlyConfig envOptions;
+    private final ReadonlyConfig sourceOptions;
+    private final String catalogName;
+    private MetalakeClient metalakeClient;
+    private final MetaLakeTableSchemaConvertor metaLakeTableSchemaConvertor;
+
+    public TableSchemaDiscoverer(TableSourceFactoryContext context, String 
catalogName) {
+        this.envOptions = context.getEnvOptions();
+        this.sourceOptions = context.getOptions();
+        this.catalogName = catalogName;
+        if (enableMetaLakeClient(context.getOptions())) {
+            this.metalakeClient = 
MetaLakeFactory.createClient(getMetaLakeType());
+        }
+        this.metaLakeTableSchemaConvertor = 
MetaLakeFactory.createTypeMapper(getMetaLakeType());
+    }
+
+    @VisibleForTesting
+    protected TableSchemaDiscoverer(
+            ReadonlyConfig envOptions,
+            ReadonlyConfig sourceOptions,
+            String catalogName,
+            MetalakeClient metalakeClient,
+            MetaLakeTableSchemaConvertor convertor) {
+        this.envOptions = envOptions;
+        this.sourceOptions = sourceOptions;
+        this.catalogName = catalogName;
+        this.metalakeClient = metalakeClient;
+        this.metaLakeTableSchemaConvertor = convertor;
+    }
+
+    public List<CatalogTable> discoverTableSchemas() {
+        // schema
+        if 
(sourceOptions.getOptional(ConnectorCommonOptions.SCHEMA).isPresent()) {
+            return 
Collections.singletonList(discoverTableSchema(sourceOptions));
+        }
+        // table_config
+        if 
(sourceOptions.getOptional(TableSchemaOptions.TABLE_CONFIGS).isPresent()) {
+            return sourceOptions.get(TableSchemaOptions.TABLE_CONFIGS).stream()
+                    .map(ReadonlyConfig::fromMap)
+                    .map(this::discoverTableSchema)
+                    .collect(Collectors.toList());
+        }
+        // table_list
+        if (sourceOptions.getOptional(CatalogOptions.TABLE_LIST).isPresent()) {
+            return sourceOptions.get(CatalogOptions.TABLE_LIST).stream()
+                    .map(ReadonlyConfig::fromMap)
+                    .map(this::discoverTableSchema)
+                    .collect(Collectors.toList());
+        }
+        return 
Collections.singletonList(CatalogTableUtil.buildSimpleTextTable());
+    }
+
+    private CatalogTable discoverTableSchema(ReadonlyConfig sourceOptions) {
+        final Map<String, Object> schemaMap = 
sourceOptions.get(ConnectorCommonOptions.SCHEMA);
+        ReadonlyConfig schemaConfig = ReadonlyConfig.fromMap(schemaMap);
+        // fields or columns
+        if (schemaConfig.getOptional(ColumnOptions.COLUMNS).isPresent()
+                || sourceOptions.getOptional(FieldOptions.FIELDS).isPresent()) 
{
+            return discoverTableSchemaFromConfig(sourceOptions);
+        }

Review Comment:
   `discoverTableSchema` unconditionally reads `ConnectorCommonOptions.SCHEMA` 
from `sourceOptions` and then checks 
`sourceOptions.getOptional(FieldOptions.FIELDS)`. In the provided 
configs/tests, `fields` lives inside the `schema { ... }` block, so this 
condition will never trigger and the method will incorrectly fall back to 
`buildSimpleTextTable()`.
   
   Consider (1) handling the case where `schema` is absent (return simple text 
table or config-derived table), and (2) checking 
`schemaConfig.getOptional(FieldOptions.FIELDS)` (and/or 
`schemaConfig.getOptional(ColumnOptions.COLUMNS)`) instead of looking at 
`sourceOptions` for `FIELDS`.



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

Reply via email to