shuke987 commented on code in PR #71399:
URL: https://github.com/apache/doris/pull/71399#discussion_r4794908822
##########
fe/fe-connector/fe-connector-lance/src/main/java/org/apache/doris/connector/lance/LanceConnectorMetadata.java:
##########
@@ -146,6 +150,89 @@ private static ConnectorColumn notNullColumn(String name,
String type) {
return new ConnectorColumn(name, ConnectorType.of(type), null, false,
null);
}
+ @Override
+ public String getTableComment(ConnectorSession session, String database,
String table) {
+ LanceTableMetadata current = client.loadTableMetadata(database, table,
LanceRefSelector.latest(),
+ LanceMetadataMetrics.forTable(session, database, table));
+ return tableProperties(database, table,
current).getOrDefault("comment", "");
+ }
+
+ @Override
+ public Optional<String> renderShowCreateTableDdl(ConnectorSession session,
ConnectorTableHandle handle) {
+ LanceTableHandle table = ((LanceTableHandle) handle).unpinned();
+ LanceTableMetadata current =
client.loadTableMetadata(table.getDbName(), table.getTableName(),
+ LanceRefSelector.latest(),
LanceMetadataMetrics.forTable(session, table.getDbName(),
+ table.getTableName()));
+ return Optional.of(LanceShowCreate.render(table.getDbName(),
table.getTableName(),
+ LanceTypeMapping.toColumns(current.getSchema()),
+ tableProperties(table.getDbName(), table.getTableName(),
current)));
+ }
+
+ private Map<String, String> tableProperties(String database, String table,
LanceTableMetadata metadata) {
+ Map<String, String> properties = new HashMap<>();
+ if (!client.isRest()) {
+ metadata.getSchema().getCustomMetadata().forEach((key, value) -> {
+ if (key.startsWith(LanceWriteSchema.TABLE_PROPERTY_PREFIX)) {
+
properties.put(key.substring(LanceWriteSchema.TABLE_PROPERTY_PREFIX.length()),
value);
+ }
+ });
+ }
+ properties.putAll(client.tableProperties(database, table));
+ return properties;
+ }
+
+ @Override
+ public void createDatabase(ConnectorSession session, String name,
Map<String, String> properties) {
+ ddl.createDatabase(name, properties);
+ }
+
+ @Override
+ public void dropDatabase(ConnectorSession session, String name, boolean
ifExists, boolean force) {
+ ddl.dropDatabase(name, ifExists, force);
+ }
+
+ @Override
+ public void createTable(ConnectorSession session,
ConnectorCreateTableRequest request) {
+ ddl.createTable(request);
+ }
+
+ @Override
+ public void dropTable(ConnectorSession session, ConnectorTableHandle
table) {
+ ddl.dropTable((LanceTableHandle) table);
+ }
+
+ @Override
+ public void renameTable(ConnectorSession session, ConnectorTableHandle
table, String name) {
+ ddl.renameTable((LanceTableHandle) table, name);
+ }
+
+ @Override
+ public void addColumn(ConnectorSession session, ConnectorTableHandle
table, ConnectorColumn column,
+ ConnectorColumnPosition position) {
+ ddl.addColumns(session, (LanceTableHandle) table,
Collections.singletonList(column), position);
+ }
+
+ @Override
+ public void addColumns(ConnectorSession session, ConnectorTableHandle
table, List<ConnectorColumn> columns) {
+ ddl.addColumns(session, (LanceTableHandle) table, columns, null);
+ }
+
+ @Override
+ public void dropColumn(ConnectorSession session, ConnectorTableHandle
table, String name) {
+ ddl.dropColumn(session, (LanceTableHandle) table, name);
Review Comment:
**[P1] A failed multi-clause ALTER can leave a column dropped**
On a Lance table `t(id INT, payload STRING)`, consider `ALTER TABLE t DROP
COLUMN payload, DROP COLUMN missing`. Both clauses pass FE validation for an
external table. The dispatcher calls this method once per clause, so the first
call commits a new Lance version without `payload` and refreshes the catalog.
Only the second call fails in `requireColumn`. The client receives an error for
the statement even though `payload` has already disappeared from the current
schema. [External ALTER
dispatch](https://github.com/apache/doris/blob/5fd0c1a21404cef9209c3b54d72f11dbe261f8ea/fe/fe-core/src/main/java/org/apache/doris/alter/Alter.java#L454-L510)
Please reject compound Lance column changes before the first mutation, or
submit the entire statement as one atomic operation. A regression test can
assert that both the schema and dataset version are unchanged after this
statement fails.
##########
fe/fe-connector/fe-connector-lance/src/main/java/org/apache/doris/connector/lance/LanceDdl.java:
##########
@@ -0,0 +1,221 @@
+// 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.doris.connector.lance;
+
+import org.apache.doris.connector.spi.ConnectorColumn;
+import org.apache.doris.connector.spi.ConnectorSession;
+import org.apache.doris.connector.spi.DorisConnectorException;
+import org.apache.doris.connector.spi.ddl.ConnectorColumnPosition;
+import org.apache.doris.connector.spi.ddl.ConnectorCreateTableRequest;
+
+import org.apache.arrow.vector.types.pojo.Schema;
+import org.lance.namespace.model.AddColumnsEntry;
+import org.lance.namespace.model.AlterColumnsEntry;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeSet;
+
+/** Namespace mutations; engine journaling and invalidation remain in the
generic connector bridge. */
+final class LanceDdl {
+ private final LanceCatalogClient client;
+
+ LanceDdl(LanceCatalogClient client) {
+ this.client = client;
+ }
+
+ void createDatabase(String name, Map<String, String> properties) {
+ rejectRootDatabase(name);
+ client.createDatabase(name, properties);
+ }
+
+ void dropDatabase(String name, boolean ifExists, boolean force) {
+ rejectRootDatabase(name);
+ if (!client.isRest() && force) {
+ throw new DorisConnectorException("Lance filesystem catalogs do
not support DROP DATABASE FORCE");
+ }
+ client.dropDatabase(name, ifExists, force);
+ }
+
+ void createTable(ConnectorCreateTableRequest request) {
+ if (!client.isRest() && request.getTableName().contains("$")) {
+ throw new DorisConnectorException(
+ "Lance filesystem table names must not contain the
manifest delimiter '$'");
+ }
+ if (request.getPartitionSpec() != null || request.getBucketSpec() !=
null
+ || !request.getSortOrder().isEmpty()) {
+ throw new DorisConnectorException("Lance CREATE TABLE does not
support partition, bucket or sort clauses");
+ }
+ Set<String> names = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
+ for (ConnectorColumn column : request.getColumns()) {
+ if (!names.add(column.getName())) {
+ throw new DorisConnectorException("Duplicate Lance column: " +
column.getName());
+ }
+ }
+ Map<String, String> properties = new
HashMap<>(request.getProperties());
+ if (request.getComment() != null && !request.getComment().isEmpty()) {
+ properties.put("comment", request.getComment());
+ }
+ Schema schema = LanceWriteSchema.schema(request.getColumns());
+ if (!client.isRest()) {
+ // DirectoryNamespace 12.0.0 ignores
CreateTableRequest.properties. Persist them in
+ // the initial Arrow schema so CREATE remains atomic and SHOW
CREATE can recover them.
+ Map<String, String> metadata = new HashMap<>();
+ properties.forEach((key, value) ->
metadata.put(LanceWriteSchema.TABLE_PROPERTY_PREFIX + key, value));
+ schema = new Schema(schema.getFields(), metadata);
+ }
+ client.createTable(request.getDbName(), request.getTableName(),
schema, properties);
+ }
+
+ void dropTable(LanceTableHandle table) {
+ client.dropTable(table.getDbName(), table.getTableName());
+ }
+
+ void renameTable(LanceTableHandle table, String name) {
+ if (!client.isRest()) {
+ throw new DorisConnectorException("RENAME TABLE is not supported
for Lance filesystem catalogs");
+ }
+ client.renameTable(table.getDbName(), table.getTableName(), name);
+ }
+
+ void addColumns(ConnectorSession session, LanceTableHandle table,
List<ConnectorColumn> columns,
+ ConnectorColumnPosition position) {
+ if (position != null) {
+ throw new DorisConnectorException("Lance ADD COLUMN does not
support column positions");
+ }
+ if (columns.isEmpty()) {
+ return;
+ }
+ Set<String> names = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
+ for (ConnectorColumn column : columns(session, table)) {
+ names.add(column.getName());
+ }
+ List<AddColumnsEntry> entries = new ArrayList<>();
+ for (ConnectorColumn column : columns) {
+ validateMutationColumn(column);
+ if (!column.isNullable()) {
+ throw new DorisConnectorException("Lance ADD COLUMN only
supports nullable columns");
+ }
+ if (!names.add(column.getName())) {
+ throw new DorisConnectorException(
+ "Column conflicts with an existing Lance column: " +
column.getName());
+ }
+ entries.add(LanceDdlRequests.added(column.getName(),
+ LanceWriteSchema.nullExpression(column.getType())));
+ }
+ client.addColumns(table.getDbName(), table.getTableName(), entries);
+ }
+
+ void dropColumn(ConnectorSession session, LanceTableHandle table, String
name) {
+ ConnectorColumn column = requireColumn(columns(session, table), name);
+ client.dropColumns(table.getDbName(), table.getTableName(),
Collections.singletonList(column.getName()));
Review Comment:
**[P1] DROP COLUMN can succeed on the wrong field**
A Lance table can contain ordinary `payload` and a distinct field named ``
`payload` `` with literal backticks at both ends. The latter can be created by
renaming an existing field with the Lance SDK. This SQL targets the
literal-backtick field:
```sql
ALTER TABLE t DROP COLUMN ```payload```;
```
`requireColumn` selects the intended field, but `column.getName()` is then
passed to an API that interprets it as a field path. Lance 12.0.0 treats the
two backticks as quoting syntax, resolves ordinary `payload`, and commits a
successful drop of that other field. [Lance path
parser](https://github.com/lance-format/lance/blob/cbeec97cb893a66ad6b3db87f25a57ab0093f358/rust/lance-core/src/datatypes/schema.rs#L1570)
ยท [Lance drop
implementation](https://github.com/lance-format/lance/blob/cbeec97cb893a66ad6b3db87f25a57ab0093f358/rust/lance/src/dataset/schema_evolution.rs#L1031)
Please reject field names that cannot be addressed unambiguously before
issuing the mutation, or use an API that preserves field identity. A regression
test should populate both columns and verify that the ordinary column and its
values survive.
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]