mchades commented on code in PR #9794: URL: https://github.com/apache/gravitino/pull/9794#discussion_r2730016705
########## server/src/main/java/org/apache/gravitino/server/web/rest/FunctionOperations.java: ########## @@ -0,0 +1,278 @@ +/* + * 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.gravitino.server.web.rest; + +import com.codahale.metrics.annotation.ResponseMetered; +import com.codahale.metrics.annotation.Timed; +import java.util.Arrays; +import javax.inject.Inject; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.DELETE; +import javax.ws.rs.DefaultValue; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Response; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.Namespace; +import org.apache.gravitino.catalog.FunctionDispatcher; +import org.apache.gravitino.dto.function.FunctionColumnDTO; +import org.apache.gravitino.dto.function.FunctionDTO; +import org.apache.gravitino.dto.function.FunctionDefinitionDTO; +import org.apache.gravitino.dto.requests.FunctionRegisterRequest; +import org.apache.gravitino.dto.requests.FunctionUpdateRequest; +import org.apache.gravitino.dto.requests.FunctionUpdatesRequest; +import org.apache.gravitino.dto.responses.DropResponse; +import org.apache.gravitino.dto.responses.EntityListResponse; +import org.apache.gravitino.dto.responses.FunctionListResponse; +import org.apache.gravitino.dto.responses.FunctionResponse; +import org.apache.gravitino.dto.util.DTOConverters; +import org.apache.gravitino.function.Function; +import org.apache.gravitino.function.FunctionChange; +import org.apache.gravitino.function.FunctionColumn; +import org.apache.gravitino.function.FunctionDefinition; +import org.apache.gravitino.function.FunctionType; +import org.apache.gravitino.metrics.MetricNames; +import org.apache.gravitino.rel.types.Type; +import org.apache.gravitino.server.web.Utils; +import org.apache.gravitino.utils.NameIdentifierUtil; +import org.apache.gravitino.utils.NamespaceUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** REST operations for function management. */ +@Path("metalakes/{metalake}/catalogs/{catalog}/schemas/{schema}/functions") +public class FunctionOperations { + + private static final Logger LOG = LoggerFactory.getLogger(FunctionOperations.class); + + private final FunctionDispatcher dispatcher; + + @Context private HttpServletRequest httpRequest; + + @Inject + public FunctionOperations(FunctionDispatcher dispatcher) { + this.dispatcher = dispatcher; + } + + // TODO: Add authorization support for function operations + @GET + @Produces("application/vnd.gravitino.v1+json") + @Timed(name = "list-function." + MetricNames.HTTP_PROCESS_DURATION, absolute = true) + @ResponseMetered(name = "list-function", absolute = true) + public Response listFunctions( + @PathParam("metalake") String metalake, + @PathParam("catalog") String catalog, + @PathParam("schema") String schema, + @QueryParam("details") @DefaultValue("false") boolean details) { + try { + LOG.info("Received list functions request for schema: {}.{}.{}", metalake, catalog, schema); + return Utils.doAs( + httpRequest, + () -> { + Namespace namespace = NamespaceUtil.ofFunction(metalake, catalog, schema); + if (!details) { + NameIdentifier[] identifiers = dispatcher.listFunctions(namespace); + LOG.info( + "List {} function names under schema: {}.{}.{}", + identifiers.length, + metalake, + catalog, + schema); + return Utils.ok(new EntityListResponse(identifiers)); + } + + Function[] functions = dispatcher.listFunctionInfos(namespace); + FunctionDTO[] functionDTOs = + Arrays.stream(functions) + .map(DTOConverters::toDTO) + .toList() + .toArray(new FunctionDTO[0]); + LOG.info( + "List {} function definitions under schema: {}.{}.{}", + functionDTOs.length, + metalake, + catalog, + schema); + return Utils.ok(new FunctionListResponse(functionDTOs)); + }); + } catch (Exception e) { + return ExceptionHandlers.handleFunctionException(OperationType.LIST, "", schema, e); + } + } + + @POST + @Produces("application/vnd.gravitino.v1+json") + @Timed(name = "register-function." + MetricNames.HTTP_PROCESS_DURATION, absolute = true) + @ResponseMetered(name = "register-function", absolute = true) + public Response registerFunction( + @PathParam("metalake") String metalake, + @PathParam("catalog") String catalog, + @PathParam("schema") String schema, + FunctionRegisterRequest request) { + LOG.info( + "Received register function request: {}.{}.{}.{}", + metalake, + catalog, + schema, + request.getName()); + try { + return Utils.doAs( + httpRequest, + () -> { + request.validate(); + NameIdentifier ident = + NameIdentifierUtil.ofFunction(metalake, catalog, schema, request.getName()); + + FunctionDefinition[] definitions = + Arrays.stream(request.getDefinitions()) + .map(FunctionDefinitionDTO::toFunctionDefinition) + .toArray(FunctionDefinition[]::new); + + Function function; + if (request.getFunctionType() == FunctionType.TABLE) { + FunctionColumn[] returnColumns = + Arrays.stream(request.getReturnColumns()) + .map(FunctionColumnDTO::toFunctionColumn) + .toArray(FunctionColumn[]::new); + function = + dispatcher.registerFunction( + ident, + request.getComment(), + request.isDeterministic(), + returnColumns, + definitions); + } else { + Type returnType = request.getReturnType(); + function = + dispatcher.registerFunction( + ident, + request.getComment(), + request.getFunctionType(), + request.isDeterministic(), + returnType, + definitions); + } + + Response response = Utils.ok(new FunctionResponse(DTOConverters.toDTO(function))); + LOG.info( + "Function registered: {}.{}.{}.{}", metalake, catalog, schema, request.getName()); + return response; + }); + } catch (Exception e) { + return ExceptionHandlers.handleFunctionException( + OperationType.CREATE, request.getName(), schema, e); Review Comment: fixed -- 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]
