zachjsh commented on code in PR #13165: URL: https://github.com/apache/druid/pull/13165#discussion_r1012236575
########## extensions-core/druid-catalog/src/main/java/org/apache/druid/catalog/http/TableEditor.java: ########## @@ -0,0 +1,369 @@ +/* + * 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.druid.catalog.http; + +import com.google.common.base.Strings; +import org.apache.druid.catalog.CatalogException; +import org.apache.druid.catalog.http.MoveColumn.Position; +import org.apache.druid.catalog.http.TableEditRequest.DropColumns; +import org.apache.druid.catalog.http.TableEditRequest.HideColumns; +import org.apache.druid.catalog.http.TableEditRequest.UnhideColumns; +import org.apache.druid.catalog.http.TableEditRequest.UpdateColumns; +import org.apache.druid.catalog.http.TableEditRequest.UpdateProperties; +import org.apache.druid.catalog.model.ColumnSpec; +import org.apache.druid.catalog.model.TableDefn; +import org.apache.druid.catalog.model.TableId; +import org.apache.druid.catalog.model.TableMetadata; +import org.apache.druid.catalog.model.TableSpec; +import org.apache.druid.catalog.model.table.AbstractDatasourceDefn; +import org.apache.druid.catalog.storage.CatalogStorage; +import org.apache.druid.java.util.common.IAE; +import org.apache.druid.utils.CollectionUtils; + +import javax.ws.rs.core.Response; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class TableEditor +{ + private final CatalogStorage catalog; + private final TableId id; + private final TableEditRequest editRequest; + + public TableEditor( + final CatalogStorage catalog, + final TableId id, + final TableEditRequest editRequest + ) + { + this.catalog = catalog; + this.id = id; + this.editRequest = editRequest; + } + + public long go() throws CatalogException + { + if (editRequest instanceof HideColumns) { + return hideColumns(((HideColumns) editRequest).columns); + } else if (editRequest instanceof UnhideColumns) { + return unHideColumns(((UnhideColumns) editRequest).columns); + } else if (editRequest instanceof DropColumns) { + return dropColumns(((DropColumns) editRequest).columns); + } else if (editRequest instanceof UpdateProperties) { + return updateProperties(((UpdateProperties) editRequest).properties); + } else if (editRequest instanceof UpdateColumns) { + return updateColumns(((UpdateColumns) editRequest).columns); + } else if (editRequest instanceof MoveColumn) { + return moveColumn(((MoveColumn) editRequest)); + } else { + // More of a server error: if we can deserialize the request, + // we should know how to perform that request. + throw CatalogException.badRequest( + "Unknown edit request: %s", + editRequest.getClass().getSimpleName() + ); + } + } + + private long hideColumns(List<String> columns) throws CatalogException + { + if (CollectionUtils.isNullOrEmpty(columns)) { + return 0; + } + return catalog.tables().updateProperties( + id, + table -> applyHiddenColumns(table, columns) + ); + } + + /** + * Given the existing set of properties, which may contain a list of hidden columns, + * perform the update action to add the requested new columns (if they don't yet exist). + * + * @return revised properties with the revised hidden columns list after applying + * the requested changes + */ + private TableSpec applyHiddenColumns(TableMetadata table, List<String> columns) throws CatalogException + { + if (!AbstractDatasourceDefn.isDatasource(table.spec().type())) { + throw CatalogException.badRequest("hideColumns is supported only for data source specs"); + } + TableSpec spec = table.spec(); + if (columns.isEmpty()) { + return null; + } + Map<String, Object> props = spec.properties(); + @SuppressWarnings("unchecked") + List<String> hiddenColumns = (List<String>) props.get(AbstractDatasourceDefn.HIDDEN_COLUMNS_PROPERTY); + if (hiddenColumns == null) { + hiddenColumns = Collections.emptyList(); + } + Set<String> existing = new HashSet<>(hiddenColumns); + List<String> revised = new ArrayList<>(hiddenColumns); + for (String col : columns) { + if (!existing.contains(col)) { + revised.add(col); + existing.add(col); + } + } + if (revised.size() == hiddenColumns.size()) { + // Nothing changed + return null; + } + Map<String, Object> revisedProps = new HashMap<>(props); + revisedProps.put(AbstractDatasourceDefn.HIDDEN_COLUMNS_PROPERTY, revised); + return spec.withProperties(revisedProps); + } + + private long unHideColumns(List<String> columns) throws CatalogException + { + if (CollectionUtils.isNullOrEmpty(columns)) { + return 0; + } + return catalog.tables().updateProperties( + id, + table -> applyUnhideColumns(table, columns) + ); + } + + /** + * Given the existing set of properties, which may contain a list of hidden columns, + * perform the update action remove the requested columns (if they exist). + * + * @return revised properties with the revised hidden columns list after applying + * the requested changes + */ + private TableSpec applyUnhideColumns(TableMetadata table, List<String> columns) throws CatalogException + { + final TableSpec existingSpec = table.spec(); + if (!AbstractDatasourceDefn.isDatasource(existingSpec.type())) { + throw CatalogException.badRequest("hideColumns is supported only for data source specs"); + } + + final Map<String, Object> props = existingSpec.properties(); + @SuppressWarnings("unchecked") + List<String> hiddenColumns = (List<String>) props.get(AbstractDatasourceDefn.HIDDEN_COLUMNS_PROPERTY); + if (hiddenColumns == null || columns.isEmpty()) { + return null; Review Comment: Will this cause all other properties for table to be overwritten to null? I thought we'd just want to return the original properties in this case? If returning null indicates no change to properties should be made, we should update the relevant javadocs. Also good to annotate with `@Nullable`. Similarly for all other functions that return null in this pr. -- 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]
