Skip to content

Commit

Permalink
feat: Databricks Aggregate functions
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Reichel <andreas@manticore-projects.com>
  • Loading branch information
manticore-projects committed May 27, 2024
1 parent bc96c9f commit 662fa5a
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ public class JSQLExpressionTranspiler extends ExpressionDeParser {
{"column", "reserved"}, {"constraint", "reserved"}, {"create", "reserved"},
{"default", "reserved"}, {"deferrable", "reserved"}, {"desc", "reserved"},
{"describe", "reserved"}, {"distinct", "reserved"}, {"do", "reserved"}, {"else", "reserved"},
{"end", "reserved"}, {"except", "reserved"}, {"false", "reserved"}, {"fetch", "reserved"},
{"end", "reserved"}, {"except", "reserved"}
//, {"false", "reserved"}
, {"fetch", "reserved"},
{"for", "reserved"}, {"foreign", "reserved"}, {"from", "reserved"}, {"grant", "reserved"},
{"group", "reserved"}, {"having", "reserved"}, {"in", "reserved"}, {"initially", "reserved"},
{"intersect", "reserved"}, {"into", "reserved"}, {"lateral", "reserved"},
Expand Down Expand Up @@ -502,6 +504,12 @@ public void visit(Function function) {
function.getMultipartName().remove(0);
}

if (function.isIgnoreNullsOutside()) {
warning("RESPECT/IGNORE NULLS is not supported for non-window functions.");
function.setNullHandling(null);
function.setIgnoreNullsOutside(false);
}

Expression rewrittenExpression = null;
ExpressionList<?> parameters = function.getParameters();
TranspiledFunction f = TranspiledFunction.from(functionName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ enum TranspiledFunction {

, FROM_UNIXTIME, TO_UNIX_TIMESTAMP, MAKE_TIMESTAMP, TIMESTAMP, TO_TIMESTAMP

, ANY, APPROX_PERCENTILE

;
// @FORMATTER:ON

Expand Down Expand Up @@ -418,6 +420,16 @@ public void visit(Function function) {
rewrittenExpression = new CastExpression(parameters.get(0), "TIMESTAMP");
}
break;
case ANY:
function.setName("Any_Value");
break;
case APPROX_PERCENTILE:
function.setName("Approx_Quantile");
if (paramCount==3) {
warning("PRECISION parameter not supported");
parameters.remove(2);
}
break;
}
}
if (rewrittenExpression == null) {
Expand Down Expand Up @@ -446,7 +458,14 @@ public void visit(AnalyticExpression function) {
}

Expression rewrittenExpression = null;
// TranspiledFunction f = TranspiledFunction.from(functionName);
TranspiledFunction f = TranspiledFunction.from(functionName);
if (f != null) {
switch (f) {
case ANY:
function.setName("Any_Value");
break;
}
}
if (rewrittenExpression == null) {
super.visit(function);
} else {
Expand Down
Binary file modified src/site/sphinx/_static/JSQLTranspiler.ods
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
-- provided
SELECT any(col) AS bool FROM VALUES (true), (false), (false) AS tab(col);

-- expected
SELECT any_value(col) AS bool FROM VALUES (true), (false), (false) AS tab(col);

-- result
"bool"
"true"


-- provided
SELECT any_value(col) IGNORE NULLS FROM VALUES (NULL), (5), (20) AS tab(col);

-- expected
select any_value(col) FROM VALUES (NULL), (5), (20) AS tab(col);

-- count
1


-- provided
SELECT approx_count_distinct(col1) FILTER(WHERE col2 = 10) AS count
FROM VALUES (1, 10), (1, 10), (2, 10), (2, 10), (3, 10), (1, 12) AS tab(col1, col2);

-- result
"count"
"3"



-- provided
SELECT approx_percentile(DISTINCT col, 0.5, 100) AS percentile FROM VALUES (0), (6), (6), (7), (9), (10) AS tab(col);

-- expected
SELECT approx_quantile(DISTINCT col, 0.5) AS percentile FROM VALUES (0), (6), (6), (7), (9), (10) AS tab(col);

-- result
"percentile"
"7"


-- provided
SELECT array_agg(DISTINCT col) AS arr FROM VALUES (1), (2), (NULL), (1) AS tab(col);

-- result
"arr"
"[1, null, 2]"


-- provided
SELECT avg(col) AS avg FROM VALUES (1), (2), (NULL) AS tab(col);

-- result
"avg"
"1.50"
13 changes: 12 additions & 1 deletion src/test/resources/ai/starlake/transpiler/databricks/todo
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,15 @@ SELECT strftime( DATE '2016-04-08', 'y') AS s;

-- result
"s"
"2016"
"2016"


-- provided
SELECT approx_percentile(col, array(0.5, 0.4, 0.1), 100) AS percentile FROM VALUES (0), (1), (2), (10) AS tab(col);

-- expected
SELECT approx_quantile(col, ARRAY_VALUE(0.5, 0.4, 0.1)) AS percentile FROM VALUES (0), (1), (2), (10) AS tab(col);

-- result
"percentile"
"[2, 1, 0]"

0 comments on commit 662fa5a

Please sign in to comment.