kunwp1 commented on code in PR #4268: URL: https://github.com/apache/texera/pull/4268#discussion_r3103082791
########## amber/src/main/python/pyamber/__init__.py: ########## Review Comment: I don't think you have to update this file. Can you revert this? ########## frontend/src/app/workspace/service/code-editor/ui-udf-parameters-parser.service.ts: ########## @@ -0,0 +1,435 @@ +/** + * 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. + */ + +import { Injectable } from "@angular/core"; +import { + AttributeType, + JavaAttributeTypeName, + PythonAttributeTypeName, + SchemaAttribute, + JAVA_ATTRIBUTE_TYPE_NAMES, + PYTHON_ATTRIBUTE_TYPE_NAMES, +} from "../../types/workflow-compiling.interface"; + +export interface UiUdfParameter { + attribute: SchemaAttribute; + value: string; +} + +type ParserAttributeTypeToken = JavaAttributeTypeName | PythonAttributeTypeName; + +const ATTRIBUTE_TYPE_TOKEN_TO_CANONICAL: Readonly<Record<ParserAttributeTypeToken, AttributeType>> = { + STRING: "string", + INTEGER: "integer", + INT: "integer", + LONG: "long", + DOUBLE: "double", + BOOLEAN: "boolean", + BOOL: "boolean", + TIMESTAMP: "timestamp", + BINARY: "binary", + LARGE_BINARY: "large_binary", +}; + +const JAVA_ATTRIBUTE_TYPE_NAME_SET = new Set<string>(JAVA_ATTRIBUTE_TYPE_NAMES); +const PYTHON_ATTRIBUTE_TYPE_NAME_SET = new Set<string>(PYTHON_ATTRIBUTE_TYPE_NAMES); +const SUPPORTED_UI_PARAMETER_ATTRIBUTE_TYPES = new Set<AttributeType>([ + "string", + "integer", + "long", + "double", + "boolean", + "timestamp", +]); + +@Injectable({ providedIn: "root" }) +export class UiUdfParametersParserService { + private static readonly SUPPORTED_CLASSES = [ + "ProcessTupleOperator", + "ProcessBatchOperator", + "ProcessTableOperator", + "GenerateOperator", + ]; + + parse(code: string): UiUdfParameter[] { + if (!code) { + return []; + } + + const sanitizedCode = this.stripCommentsAndDocstrings(code); + const classPattern = UiUdfParametersParserService.SUPPORTED_CLASSES.join("|"); + const classRegex = new RegExp( + `class\\s+(${classPattern})\\s*\\([^)]*\\)\\s*:[\\s\\S]*?(?=\\nclass\\s+\\w+\\s*\\(|$)`, + "g" + ); + + const parsed: UiUdfParameter[] = []; + const existingNames = new Set<string>(); + + let classMatch: RegExpExecArray | null; + while ((classMatch = classRegex.exec(sanitizedCode)) !== null) { + const classBlock = classMatch[0]; + + for (const args of this.extractUiParameterArgumentLists(classBlock)) { + const argumentTokens = this.tokenizeArguments(args); + const extracted = this.extractParameter(argumentTokens); + if (!extracted || existingNames.has(extracted.attribute.attributeName)) { + continue; + } + + existingNames.add(extracted.attribute.attributeName); + parsed.push(extracted); + } + } + + return parsed; + } + + private stripCommentsAndDocstrings(code: string): string { Review Comment: The logics you implemented here looks too trivial. Did you check if there are existing third-party libraries that does this? ########## amber/src/main/python/pytexera/__init__.py: ########## @@ -53,4 +54,7 @@ "Iterator", "Optional", "Union", + "Dict", Review Comment: Remove Dict and Any ########## frontend/src/app/workspace/types/workflow-compiling.interface.ts: ########## @@ -70,8 +70,50 @@ export type CompilationStateInfo = Readonly< operatorErrors: Readonly<Record<string, WorkflowFatalError>>; } >; + // possible types of an attribute -export type AttributeType = "string" | "integer" | "double" | "boolean" | "long" | "timestamp" | "binary"; // schema: an array of attribute names and types +// Canonical frontend / JSON schema names +export type AttributeType = + | "string" + | "integer" + | "long" + | "double" + | "boolean" + | "timestamp" + | "binary" + | "large_binary"; + +// Java enum constant names (AttributeType.java) +export const JAVA_ATTRIBUTE_TYPE_NAMES = [ Review Comment: Why does this PR include JAVA_ATTRIBUTE_TYPE_NAMES? Isn't this PR only for Python UDF? ########## common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/visualization/wordCloud/WordCloudOpDesc.scala: ########## Review Comment: Revert unnecessary changes. ########## amber/src/main/python/core/models/schema/attribute_type.py: ########## @@ -78,6 +78,45 @@ class AttributeType(Enum): } +FROM_STRING_PARSER_MAPPING = { + AttributeType.STRING: str, + AttributeType.INT: lambda v: ( + 0 if v is None or (isinstance(v, str) and v.strip() == "") else int(v) + ), + AttributeType.LONG: lambda v: ( + 0 if v is None or (isinstance(v, str) and v.strip() == "") else int(v) + ), + AttributeType.DOUBLE: lambda v: ( + 0.0 if v is None or (isinstance(v, str) and v.strip() == "") else float(v) + ), + AttributeType.BOOL: lambda v: ( + False + if v is None or (isinstance(v, str) and v.strip() == "") + else str(v).strip().lower() in ("true", "1", "yes") Review Comment: How did you come up with these three values ("true", "1", "yes")? Do you think it's okay to return false if the value is 2 for example? ########## common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/udf/python/DualInputPortsPythonUDFOpDescV2.scala: ########## @@ -34,11 +34,25 @@ class DualInputPortsPythonUDFOpDescV2 extends LogicalOp { required = true, defaultValue = "# Choose from the following templates:\n" + + "# \n" + + "# UiParameter notes:\n" + + "# - A UiParameter is a user-editable value exposed in the property panel and read from your Python code.\n" + + "# - Define UiParameter values in open() and then use them later in your UDF methods.\n" + + "# - Active UiParameter calls appear in the property panel; commented-out calls are ignored.\n" + + "# - Supported UiParameter types are STRING, INT/LONG, DOUBLE, BOOL, and TIMESTAMP.\n" + "# \n" + "# from pytexera import *\n" + "# \n" + "# class ProcessTupleOperator(UDFOperatorV2):\n" + - "# \n" + + "# @overrides\n" + Review Comment: You might only need one copy of the comment. ########## frontend/src/app/common/formly/collab-wrapper/collab-wrapper/collab-wrapper.component.css: ########## Review Comment: Revert this change. -- 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]
