Skip to content
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

feat: add support to NOT LIKE operator #29384

Merged
merged 9 commits into from
Jul 8, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const BINARY_OPERATORS = [
'<=',
'ILIKE',
'LIKE',
'NOT LIKE',
'REGEX',
'TEMPORAL_RANGE',
] as const;
Expand Down
59 changes: 34 additions & 25 deletions superset/models/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@
from superset.db_engine_specs import BaseEngineSpec
from superset.models.core import Database


config = app.config
logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -200,7 +199,8 @@
for u in cls.__table_args__ # type: ignore
if isinstance(u, UniqueConstraint)
]
unique.extend({c.name} for c in cls.__table__.columns if c.unique) # type: ignore
unique.extend(
{c.name} for c in cls.__table__.columns if c.unique) # type: ignore
return unique

@classmethod
Expand Down Expand Up @@ -1028,7 +1028,7 @@
_("Db engine did not return all queried columns")
)
if len(df.columns) > len(labels_expected):
df = df.iloc[:, 0 : len(labels_expected)]
df = df.iloc[:, 0: len(labels_expected)]

Check warning on line 1031 in superset/models/helpers.py

View check run for this annotation

Codecov / codecov/patch

superset/models/helpers.py#L1031

Added line #L1031 was not covered by tests
df.columns = labels_expected
return df

Expand Down Expand Up @@ -1200,9 +1200,9 @@
target_generic_type == utils.GenericDataType.NUMERIC
and operator
not in {
utils.FilterOperator.ILIKE,
utils.FilterOperator.LIKE,
}
utils.FilterOperator.ILIKE,
utils.FilterOperator.LIKE,
}
):
# For backwards compatibility and edge cases
# where a column data type might have changed
Expand Down Expand Up @@ -1448,7 +1448,8 @@
col = self.make_sqla_column_compatible(col, label)
return col

def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-many-branches,too-many-statements
def get_sqla_query(
# pylint: disable=too-many-arguments,too-many-locals,too-many-branches,too-many-statements
self,
apply_fetch_values_predicate: bool = False,
columns: Optional[list[Column]] = None,
Expand Down Expand Up @@ -1819,8 +1820,8 @@
if (
col_advanced_data_type != ""
and feature_flag_manager.is_feature_enabled(
"ENABLE_ADVANCED_DATA_TYPES"
)
"ENABLE_ADVANCED_DATA_TYPES"
)
and col_advanced_data_type in ADVANCED_DATA_TYPES
):
values = eq if is_list_target else [eq] # type: ignore
Expand Down Expand Up @@ -1873,9 +1874,9 @@
if (
op
not in {
utils.FilterOperator.EQUALS.value,
rusackas marked this conversation as resolved.
Show resolved Hide resolved
utils.FilterOperator.NOT_EQUALS.value,
}
utils.FilterOperator.EQUALS.value,
utils.FilterOperator.NOT_EQUALS.value,
}
and eq is None
):
raise QueryObjectValidationError(
Expand Down Expand Up @@ -1907,6 +1908,13 @@
where_clause_and.append(sqla_col.like(eq))
else:
where_clause_and.append(sqla_col.ilike(eq))
elif op in {
utils.FilterOperator.NOT_LIKE.value
}:
if target_generic_type != GenericDataType.STRING:
sqla_col = sa.cast(sqla_col, sa.String)

Check warning on line 1915 in superset/models/helpers.py

View check run for this annotation

Codecov / codecov/patch

superset/models/helpers.py#L1914-L1915

Added lines #L1914 - L1915 were not covered by tests

where_clause_and.append(sqla_col.not_like(eq))

Check warning on line 1917 in superset/models/helpers.py

View check run for this annotation

Codecov / codecov/patch

superset/models/helpers.py#L1917

Added line #L1917 was not covered by tests
elif (
op == utils.FilterOperator.TEMPORAL_RANGE.value
and isinstance(eq, str)
Expand Down Expand Up @@ -2112,21 +2120,22 @@

filter_columns = [flt.get("col") for flt in filter] if filter else []
rejected_filter_columns = [
col
for col in filter_columns
if col
and not is_adhoc_column(col)
and col not in self.column_names
and col not in applied_template_filters
] + rejected_adhoc_filters_columns
col
rusackas marked this conversation as resolved.
Show resolved Hide resolved
for col in filter_columns
if col
and not is_adhoc_column(col)
and col not in self.column_names
and col not in applied_template_filters
] + rejected_adhoc_filters_columns

applied_filter_columns = [
col
for col in filter_columns
if col
and not is_adhoc_column(col)
and (col in self.column_names or col in applied_template_filters)
] + applied_adhoc_filters_columns
col
for col in filter_columns
if col
and not is_adhoc_column(col)
and (
col in self.column_names or col in applied_template_filters)
] + applied_adhoc_filters_columns

return SqlaQuery(
applied_template_filters=applied_template_filters,
Expand Down
1 change: 1 addition & 0 deletions superset/utils/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ class FilterOperator(StrEnum):
GREATER_THAN_OR_EQUALS = ">="
LESS_THAN_OR_EQUALS = "<="
LIKE = "LIKE"
NOT_LIKE = "NOT LIKE"
ILIKE = "ILIKE"
IS_NULL = "IS NULL"
IS_NOT_NULL = "IS NOT NULL"
Expand Down
Loading