-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[DRAFT] feat: add new string-based pipeline expressions #16149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+699
−0
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -897,6 +897,199 @@ def if_error(self, then_value: Expression | CONSTANT_TYPE) -> "Expression": | |||||
| "if_error", [self, self._cast_to_expr_or_convert_to_constant(then_value)] | ||||||
| ) | ||||||
|
|
||||||
| @expose_as_static | ||||||
| def regex_find(self, pattern: Expression | CONSTANT_TYPE) -> "Expression": | ||||||
| """Creates an expression that returns the first substring that matches the specified regex pattern. | ||||||
|
|
||||||
| Example: | ||||||
| >>> # Get the first match of email | ||||||
| >>> Field.of("text").regex_find(r"\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,}\\b") | ||||||
|
|
||||||
| Args: | ||||||
| pattern: The regular expression pattern to search for. | ||||||
|
|
||||||
| Returns: | ||||||
| A new `Expression` finding the regex substring. | ||||||
| """ | ||||||
| return FunctionExpression( | ||||||
| "regex_find", [self, self._cast_to_expr_or_convert_to_constant(pattern)] | ||||||
| ) | ||||||
|
|
||||||
| @expose_as_static | ||||||
| def regex_find_all(self, pattern: Expression | CONSTANT_TYPE) -> "Expression": | ||||||
| """Creates an expression that returns all substrings that match the specified regex pattern. | ||||||
|
|
||||||
| Example: | ||||||
| >>> # Get all hashtags | ||||||
| >>> Field.of("post").regex_find_all(r"#[a-zA-Z]+\\b") | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The regex pattern in this example has an incorrectly escaped word boundary. In a Python raw string (
Suggested change
|
||||||
|
|
||||||
| Args: | ||||||
| pattern: The regular expression pattern to search for. | ||||||
|
|
||||||
| Returns: | ||||||
| A new `Expression` finding all regex substrings. | ||||||
| """ | ||||||
| return FunctionExpression( | ||||||
| "regex_find_all", [self, self._cast_to_expr_or_convert_to_constant(pattern)] | ||||||
| ) | ||||||
|
|
||||||
| @expose_as_static | ||||||
| def string_split(self, delimiter: Expression | CONSTANT_TYPE) -> "Expression": | ||||||
| """Creates an expression that splits a string by a delimiter. | ||||||
|
|
||||||
| Example: | ||||||
| >>> # Split by comma | ||||||
| >>> Field.of("tags").string_split(",") | ||||||
|
|
||||||
| Args: | ||||||
| delimiter: The delimiter to split by. | ||||||
|
|
||||||
| Returns: | ||||||
| A new `Expression` representing the split string. | ||||||
| """ | ||||||
| return FunctionExpression( | ||||||
| "split", [self, self._cast_to_expr_or_convert_to_constant(delimiter)], infix_name_override="string_split" | ||||||
| ) | ||||||
|
|
||||||
| @expose_as_static | ||||||
| def string_repeat(self, repetition: Expression | CONSTANT_TYPE) -> "Expression": | ||||||
| """Creates an expression that repeats a string a specified number of times. | ||||||
|
|
||||||
| Example: | ||||||
| >>> # Repeat the string 3 times | ||||||
| >>> Field.of("name").string_repeat(3) | ||||||
|
|
||||||
| Args: | ||||||
| repetition: The number of times to repeat the string. | ||||||
|
|
||||||
| Returns: | ||||||
| A new `Expression` representing the repeated string. | ||||||
| """ | ||||||
| return FunctionExpression( | ||||||
| "string_repeat", | ||||||
| [self, self._cast_to_expr_or_convert_to_constant(repetition)], | ||||||
| ) | ||||||
|
|
||||||
| @expose_as_static | ||||||
| def string_replace_all( | ||||||
| self, | ||||||
| search: Expression | CONSTANT_TYPE, | ||||||
| replacement: Expression | CONSTANT_TYPE, | ||||||
| ) -> "Expression": | ||||||
| """Creates an expression that replaces all occurrences of a search value with a replacement value. | ||||||
|
|
||||||
| Example: | ||||||
| >>> # Replace 'user' with 'admin' | ||||||
| >>> Field.of("role").string_replace_all("user", "admin") | ||||||
|
|
||||||
| Args: | ||||||
| search: The string to search for. | ||||||
| replacement: The string to replace it with. | ||||||
|
|
||||||
| Returns: | ||||||
| A new `Expression` representing the string with all replacements made. | ||||||
| """ | ||||||
| return FunctionExpression( | ||||||
| "string_replace_all", | ||||||
| [ | ||||||
| self, | ||||||
| self._cast_to_expr_or_convert_to_constant(search), | ||||||
| self._cast_to_expr_or_convert_to_constant(replacement), | ||||||
| ], | ||||||
| ) | ||||||
|
|
||||||
| @expose_as_static | ||||||
| def string_replace_one( | ||||||
| self, | ||||||
| search: Expression | CONSTANT_TYPE, | ||||||
| replacement: Expression | CONSTANT_TYPE, | ||||||
| ) -> "Expression": | ||||||
| """Creates an expression that replaces the first occurrence of a search value with a replacement value. | ||||||
|
|
||||||
| Example: | ||||||
| >>> # Replace first 'apple' with 'orange' | ||||||
| >>> Field.of("fruits").string_replace_one("apple", "orange") | ||||||
|
|
||||||
| Args: | ||||||
| search: The string to search for. | ||||||
| replacement: The string to replace it with. | ||||||
|
|
||||||
| Returns: | ||||||
| A new `Expression` representing the string with the first replacement made. | ||||||
| """ | ||||||
| return FunctionExpression( | ||||||
| "string_replace_one", | ||||||
| [ | ||||||
| self, | ||||||
| self._cast_to_expr_or_convert_to_constant(search), | ||||||
| self._cast_to_expr_or_convert_to_constant(replacement), | ||||||
| ], | ||||||
| ) | ||||||
|
|
||||||
| @expose_as_static | ||||||
| def string_index_of(self, search: Expression | CONSTANT_TYPE) -> "Expression": | ||||||
| """Creates an expression that returns the index of a search value in a string. | ||||||
|
|
||||||
| Example: | ||||||
| >>> # Get the index of 'target' | ||||||
| >>> Field.of("text").string_index_of("target") | ||||||
|
|
||||||
| Args: | ||||||
| search: The string to search for. | ||||||
|
|
||||||
| Returns: | ||||||
| A new `Expression` representing the index of the string. | ||||||
| """ | ||||||
| return FunctionExpression( | ||||||
| "string_index_of", [self, self._cast_to_expr_or_convert_to_constant(search)] | ||||||
| ) | ||||||
|
|
||||||
| @expose_as_static | ||||||
| def ltrim( | ||||||
| self, values_to_trim: Expression | CONSTANT_TYPE | None = None | ||||||
| ) -> "Expression": | ||||||
| """Creates an expression that removes leading whitespace (or specified characters) from a string. | ||||||
|
|
||||||
| Example: | ||||||
| >>> # Trim leading spaces | ||||||
| >>> Field.of("text").ltrim() | ||||||
| >>> # Trim specific character | ||||||
| >>> Field.of("text").ltrim("-") | ||||||
|
|
||||||
| Args: | ||||||
| values_to_trim: The substring or expression defining characters to trim. Defaults to None (trims whitespace). | ||||||
|
|
||||||
| Returns: | ||||||
| A new `Expression` representing the left-trimmed string. | ||||||
| """ | ||||||
| args = [self] | ||||||
| if values_to_trim is not None: | ||||||
| args.append(self._cast_to_expr_or_convert_to_constant(values_to_trim)) | ||||||
| return FunctionExpression("ltrim", args) | ||||||
|
|
||||||
| @expose_as_static | ||||||
| def rtrim( | ||||||
| self, values_to_trim: Expression | CONSTANT_TYPE | None = None | ||||||
| ) -> "Expression": | ||||||
| """Creates an expression that removes trailing whitespace (or specified characters) from a string. | ||||||
|
|
||||||
| Example: | ||||||
| >>> # Trim trailing spaces | ||||||
| >>> Field.of("text").rtrim() | ||||||
| >>> # Trim specific character | ||||||
| >>> Field.of("text").rtrim("-") | ||||||
|
|
||||||
| Args: | ||||||
| values_to_trim: The substring or expression defining characters to trim. Defaults to None (trims whitespace). | ||||||
|
|
||||||
| Returns: | ||||||
| A new `Expression` representing the right-trimmed string. | ||||||
| """ | ||||||
| args = [self] | ||||||
| if values_to_trim is not None: | ||||||
| args.append(self._cast_to_expr_or_convert_to_constant(values_to_trim)) | ||||||
| return FunctionExpression("rtrim", args) | ||||||
|
|
||||||
| @expose_as_static | ||||||
| def exists(self) -> "BooleanExpression": | ||||||
| """Creates an expression that checks if a field exists in the document. | ||||||
|
|
||||||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The regex pattern in the example has a few issues:
\\b) and the literal dot (\\.). In a Python raw string (r"..."), a single backslash is sufficient (\band\.).[A-Z|a-z]for the top-level domain is likely intended to be[A-Za-z]. Inside[],|is treated as a literal character.Here is a corrected version of the example.