Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/cubejs-dremio-driver/driver/DremioQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ class DremioQuery extends BaseQuery {
templates.functions.DATEDIFF = 'DATE_DIFF(DATE, DATE_TRUNC(\'{{ date_part }}\', {{ args[1] }}), DATE_TRUNC(\'{{ date_part }}\', {{ args[2] }}))';
templates.functions.STRING_AGG = 'LISTAGG({% if distinct %}DISTINCT {% endif %}{{ args_concat }})';
templates.expressions.interval_single_date_part = 'CAST({{ num }} as INTERVAL {{ date_part }})';
templates.expressions.like = '{{ expr }} {% if negated %}NOT {% endif %}LIKE {{ pattern }}{% if default_escape %} ESCAPE \'\\\'{% endif %}';
delete templates.expressions.ilike;
templates.quotes.identifiers = '"';
return templates;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/cubejs-duckdb-driver/src/DuckDBQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export class DuckDBQuery extends BaseQuery {
templates.functions.LEAST = 'LEAST({{ args_concat }})';
templates.functions.GREATEST = 'GREATEST({{ args_concat }})';
templates.functions.STRING_AGG = 'STRING_AGG({% if distinct %}DISTINCT {% endif %}{{ args[0] }}, COALESCE({{ args[1] }}, \'\'))';
templates.expressions.like = '{{ expr }} {% if negated %}NOT {% endif %}LIKE {{ pattern }}{% if default_escape %} ESCAPE \'\\\'{% endif %}';
templates.expressions.ilike = '{{ expr }} {% if negated %}NOT {% endif %}ILIKE {{ pattern }}{% if default_escape %} ESCAPE \'\\\'{% endif %}';
return templates;
}

Expand Down
10 changes: 9 additions & 1 deletion packages/cubejs-schema-compiler/src/adapter/BaseFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,15 @@ export class BaseFilter extends BaseDimension {
}

public isWildcardOperator() {
return this.camelizeOperator === 'contains' || this.camelizeOperator === 'notContains';
// All LIKE-based operators need wildcard chars escaped in user values
return (
this.camelizeOperator === 'contains' ||
this.camelizeOperator === 'notContains' ||
this.camelizeOperator === 'startsWith' ||
this.camelizeOperator === 'notStartsWith' ||
this.camelizeOperator === 'endsWith' ||
this.camelizeOperator === 'notEndsWith'
);
}

public filterParams() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ export class MssqlQuery extends BaseQuery {
delete templates.functions.STRING_AGG;
// PERCENTILE_CONT works but requires PARTITION BY
delete templates.functions.PERCENTILECONT;
templates.expressions.like = '{{ expr }} {% if negated %}NOT {% endif %}LIKE {{ pattern }}{% if default_escape %} ESCAPE \'\\\'{% endif %}';
delete templates.expressions.ilike;
// MSSQL uses + for string concatenation instead of ||
templates.expressions.concat_strings = '{{ strings | join(\' + \' ) }}';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export class PrestodbQuery extends BaseQuery {
templates.expressions.binary = '{% if op == \'||\' %}' +
'(CAST({{ left }} AS VARCHAR) || CAST({{ right }} AS VARCHAR))' +
'{% else %}({{ left }} {{ op }} {{ right }}){% endif %}';
templates.expressions.like = '{{ expr }} {% if negated %}NOT {% endif %}LIKE {{ pattern }}{% if default_escape %} ESCAPE \'\\\'{% endif %}';
delete templates.expressions.ilike;
templates.types.string = 'VARCHAR';
templates.types.float = 'REAL';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ export class SnowflakeQuery extends BaseQuery {
templates.expressions.extract = 'EXTRACT({{ date_part }} FROM {{ expr }})';
templates.expressions.interval = 'INTERVAL \'{{ interval }}\'';
templates.expressions.timestamp_literal = '\'{{ value }}\'::timestamp_tz';
templates.expressions.like = '{{ expr }} {% if negated %}NOT {% endif %}LIKE {{ pattern }}{% if default_escape %} ESCAPE \'\\\\\'{% endif %}';
templates.expressions.ilike = '{{ expr }} {% if negated %}NOT {% endif %}ILIKE {{ pattern }}{% if default_escape %} ESCAPE \'\\\\\'{% endif %}';
Comment thread
MazterQyou marked this conversation as resolved.
templates.operators.is_not_distinct_from = 'IS NOT DISTINCT FROM';
templates.join_types.full = 'FULL';
delete templates.types.interval;
Expand Down
47 changes: 37 additions & 10 deletions rust/cubesql/cubesql/src/compile/engine/df/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use datafusion::{
error::{DataFusionError, Result},
logical_plan::{
plan::Extension, replace_col, Column, DFSchema, DFSchemaRef, Expr, GroupingSet, JoinType,
LogicalPlan, UserDefinedLogicalNode,
LogicalPlan, Operator, UserDefinedLogicalNode,
},
physical_plan::{aggregates::AggregateFunction, functions::BuiltinScalarFunction},
scalar::ScalarValue,
Expand Down Expand Up @@ -1859,15 +1859,42 @@ impl WrappedSelectNode {
subqueries,
)
.await?;
let resulting_sql = sql_generator
.get_sql_templates()
.binary_expr(left, op.to_string(), right)
.map_err(|e| {
DataFusionError::Internal(format!(
"Can't generate SQL for binary expr: {}",
e
))
})?;
let resulting_sql = match op {
Operator::Like => sql_generator.get_sql_templates().like_expr(
LikeType::Like,
left,
false,
right,
None,
),
Operator::NotLike => sql_generator.get_sql_templates().like_expr(
LikeType::Like,
left,
true,
right,
None,
),
Operator::ILike => sql_generator.get_sql_templates().like_expr(
LikeType::ILike,
left,
false,
right,
None,
),
Operator::NotILike => sql_generator.get_sql_templates().like_expr(
LikeType::ILike,
left,
true,
right,
None,
),
_ => sql_generator
.get_sql_templates()
.binary_expr(left, op.to_string(), right),
}
.map_err(|e| {
DataFusionError::Internal(format!("Can't generate SQL for binary expr: {}", e))
})?;
Comment thread
MazterQyou marked this conversation as resolved.
Ok((resulting_sql, sql_query))
}
// Expr::AnyExpr { .. } => {}
Expand Down
106 changes: 106 additions & 0 deletions rust/cubesql/cubesql/src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3389,6 +3389,43 @@ limit
}]),
None,
),
// LIKE with `\` as escape character: `\_` and `\%` must be
// resolved to literal characters, not preserved in the filter
// value, and the resulting filter must use `equals` since the
// unescaped pattern contains no wildcards.
(
r"customer_gender LIKE 'fem\_ale'".to_string(),
Some(vec![V1LoadRequestQueryFilterItem {
member: Some("KibanaSampleDataEcommerce.customer_gender".to_string()),
operator: Some("equals".to_string()),
values: Some(vec!["fem_ale".to_string()]),
or: None,
and: None,
}]),
None,
),
(
r"customer_gender LIKE 'fem\%ale%'".to_string(),
Some(vec![V1LoadRequestQueryFilterItem {
member: Some("KibanaSampleDataEcommerce.customer_gender".to_string()),
operator: Some("startsWith".to_string()),
values: Some(vec!["fem%ale".to_string()]),
or: None,
and: None,
}]),
None,
),
(
r"customer_gender NOT LIKE '%fem\_ale'".to_string(),
Some(vec![V1LoadRequestQueryFilterItem {
member: Some("KibanaSampleDataEcommerce.customer_gender".to_string()),
operator: Some("notEndsWith".to_string()),
values: Some(vec!["fem_ale".to_string()]),
or: None,
and: None,
}]),
None,
),
// Segment
(
"is_male = true".to_string(),
Expand Down Expand Up @@ -11397,6 +11434,75 @@ ORDER BY "source"."str0" ASC
)
}

#[tokio::test]
async fn test_cube_scan_like_escaped_chars_filter() {
init_testing_logger();

let logical_plan = convert_select_to_query_plan(
r#"
SELECT customer_gender
FROM "public"."KibanaSampleDataEcommerce"
WHERE customer_gender LIKE '%fem\_ale%'
GROUP BY 1
"#
.to_string(),
DatabaseProtocol::PostgreSQL,
)
.await
.as_logical_plan();

assert_eq!(
logical_plan.find_cube_scan().request,
V1LoadRequestQuery {
measures: Some(vec![]),
dimensions: Some(vec!["KibanaSampleDataEcommerce.customer_gender".to_string()]),
segments: Some(vec![]),
order: Some(vec![]),
filters: Some(vec![V1LoadRequestQueryFilterItem {
member: Some("KibanaSampleDataEcommerce.customer_gender".to_string()),
operator: Some("contains".to_string()),
values: Some(vec!["fem_ale".to_string()]),
or: None,
and: None,
}]),
..Default::default()
}
);
}

#[tokio::test]
async fn test_sql_push_down_like_with_escape() {
if !Rewriter::sql_push_down_enabled() {
return;
}
init_testing_logger();

let query_plan = convert_select_to_query_plan_customized(
r#"
SELECT customer_gender
FROM "public"."KibanaSampleDataEcommerce"
WHERE
LOWER(customer_gender) <> 'a'
AND customer_gender LIKE 'foo\%bar'
GROUP BY 1
"#
.to_string(),
DatabaseProtocol::PostgreSQL,
vec![(
"expressions/like".to_string(),
"{{ expr }} {% if negated %}NOT {% endif %}LIKE {{ pattern }}\
{% if default_escape %} ESCAPE '\\\\'{% endif %}"
.to_string(),
)],
)
.await;

let logical_plan = query_plan.as_logical_plan();
let sql = logical_plan.find_cube_scan_wrapped_sql().wrapped_sql.sql;
assert!(sql.contains(" LIKE "));
assert!(sql.contains(" ESCAPE "));
}
Comment thread
MazterQyou marked this conversation as resolved.

#[tokio::test]
async fn test_thoughtspot_exclude_single_filter() {
if !Rewriter::sql_push_down_enabled() {
Expand Down
Loading
Loading