Skip to content

Commit b1606da

Browse files
authored
feat(cubesql): Support SQL push down for more functions (#7406)
* feat(cubesql): Add `PI` function * chore(cubesql): Add `GREATEST` function stub * fix(cubesql): Fix `IS NOT NULL` push down * feat(cubesql): Support SQL push down for more functions
1 parent ae6dd84 commit b1606da

File tree

12 files changed

+48
-15
lines changed

12 files changed

+48
-15
lines changed

packages/cubejs-backend-native/Cargo.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cubejs-schema-compiler/src/adapter/BaseQuery.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2455,11 +2455,15 @@ class BaseQuery {
24552455
EXP: 'EXP({{ args_concat }})',
24562456
LN: 'LN({{ args_concat }})',
24572457
LOG: 'LOG({{ args_concat }})',
2458+
DLOG10: 'LOG10({{ args_concat }})',
24582459
PI: 'PI()',
24592460
POWER: 'POWER({{ args_concat }})',
24602461
SIN: 'SIN({{ args_concat }})',
24612462
TAN: 'TAN({{ args_concat }})',
24622463
REPEAT: 'REPEAT({{ args_concat }})',
2464+
NULLIF: 'NULLIF({{ args_concat }})',
2465+
ROUND: 'ROUND({{ args_concat }})',
2466+
GREATEST: 'GREATEST({{ args_concat }})',
24632467

24642468
STDDEV: 'STDDEV_SAMP({{ args_concat }})',
24652469
SUBSTR: 'SUBSTRING({{ args_concat }})',
@@ -2475,7 +2479,7 @@ class BaseQuery {
24752479
expressions: {
24762480
column_aliased: '{{expr}} {{quoted_alias}}',
24772481
case: 'CASE{% if expr %}{{ expr }} {% endif %}{% for when, then in when_then %} WHEN {{ when }} THEN {{ then }}{% endfor %}{% if else_expr %} ELSE {{ else_expr }}{% endif %} END',
2478-
is_null: '{{ expr }} {% if negate %}NOT {% endif %}IS NULL',
2482+
is_null: '{{ expr }} IS {% if negate %}NOT {% endif %}NULL',
24792483
binary: '({{ left }} {{ op }} {{ right }})',
24802484
sort: '{{ expr }} {% if asc %}ASC{% else %}DESC{% endif %}{% if nulls_first %} NULLS FIRST{% endif %}',
24812485
cast: 'CAST({{ expr }} AS {{ data_type }})',

packages/cubejs-schema-compiler/src/adapter/BigqueryQuery.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ export class BigqueryQuery extends BaseQuery {
150150
templates.quotes.identifiers = '`';
151151
templates.quotes.escape = '\\`';
152152
templates.functions.DATETRUNC = 'DATETIME_TRUNC(CAST({{ args[1] }} AS DATETIME), {{ date_part }})';
153+
templates.functions.LOG = 'LOG({{ args_concat }}{% if args[1] is undefined %}, 10{% endif %})';
153154
templates.expressions.binary = '{% if op == \'%\' %}MOD({{ left }}, {{ right }}){% else %}({{ left }} {{ op }} {{ right }}){% endif %}';
154155
templates.expressions.interval = 'INTERVAL {{ interval }}';
155156
templates.expressions.extract = 'EXTRACT({% if date_part == \'DOW\' %}DAYOFWEEK{% elif date_part == \'DOY\' %}DAYOFYEAR{% else %}{{ date_part }}{% endif %} FROM {{ expr }})';

packages/cubejs-schema-compiler/src/adapter/RedshiftQuery.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export class RedshiftQuery extends PostgresQuery {
1717
...super.sqlTemplates(),
1818
COVAR_POP: undefined,
1919
COVAR_SAMP: undefined,
20+
DLOG10: 'LOG(10, {{ args_concat }})',
2021
};
2122
}
2223
}

packages/cubejs-schema-compiler/src/adapter/SnowflakeQuery.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ export class SnowflakeQuery extends BaseQuery {
5454
const templates = super.sqlTemplates();
5555
templates.functions.DATETRUNC = 'DATE_TRUNC({{ args_concat }})';
5656
templates.functions.DATEPART = 'DATE_PART({{ args_concat }})';
57+
templates.functions.LOG = 'LOG({% if args[1] is undefined %}10, {% endif %}{{ args_concat }})';
58+
templates.functions.DLOG10 = 'LOG(10, {{ args_concat }})';
5759
templates.expressions.extract = 'EXTRACT({{ date_part }} FROM {{ expr }})';
5860
templates.expressions.interval = 'INTERVAL \'{{ interval }}\'';
5961
return templates;

rust/cubesql/Cargo.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/cubesql/cubesql/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ homepage = "https://cube.dev"
1010

1111
[dependencies]
1212
arc-swap = "1"
13-
datafusion = { git = 'https://github.com/cube-js/arrow-datafusion.git', rev = "be967a63967f23bdd946a45925d07af0a11dcc39", default-features = false, features = ["regex_expressions", "unicode_expressions"] }
13+
datafusion = { git = 'https://github.com/cube-js/arrow-datafusion.git', rev = "3fb79c32defa4ec39ccf658b6185704d1980f228", default-features = false, features = ["regex_expressions", "unicode_expressions"] }
1414
anyhow = "1.0"
1515
thiserror = "1.0"
1616
cubeclient = { path = "../cubeclient" }

rust/cubesql/cubesql/src/compile/engine/udf.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3634,6 +3634,7 @@ pub fn register_fun_stubs(mut ctx: SessionContext) -> SessionContext {
36343634
rettyp = Int64,
36353635
vol = Volatile
36363636
);
3637+
register_fun_stub!(udf, "greatest", argc = 2);
36373638
register_fun_stub!(
36383639
udf,
36393640
"has_any_column_privilege",

rust/cubesql/cubesql/src/compile/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19629,4 +19629,18 @@ ORDER BY \"COUNT(count)\" DESC"
1962919629
.sql
1963019630
.contains("NOT IN ("));
1963119631
}
19632+
19633+
#[tokio::test]
19634+
async fn test_pi() -> Result<(), CubeError> {
19635+
insta::assert_snapshot!(
19636+
"pi",
19637+
execute_query(
19638+
"SELECT PI() AS PI".to_string(),
19639+
DatabaseProtocol::PostgreSQL
19640+
)
19641+
.await?
19642+
);
19643+
19644+
Ok(())
19645+
}
1963219646
}

rust/cubesql/cubesql/src/compile/rewrite/language.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ macro_rules! variant_field_struct {
356356
BuiltinScalarFunction::MD5 => "MD5",
357357
BuiltinScalarFunction::NullIf => "NullIf",
358358
BuiltinScalarFunction::OctetLength => "OctetLength",
359+
BuiltinScalarFunction::Pi => "Pi",
359360
BuiltinScalarFunction::Random => "Random",
360361
BuiltinScalarFunction::RegexpReplace => "RegexpReplace",
361362
BuiltinScalarFunction::Repeat => "Repeat",

0 commit comments

Comments
 (0)