From 469af1cd3973718c1cfe49afad4bea97e8a57a22 Mon Sep 17 00:00:00 2001 From: Andreas Reichel Date: Mon, 10 Jun 2024 12:15:55 +0700 Subject: [PATCH] feat: Transpile `EXCEPT` and `REPLACE` clauses - rewrite `EXCEPT` into DuckDB's `EXCLUDE` - suppress `REPLACE` since DuckDB replace the Value of an existing column instead assigning an Alias Signed-off-by: Andreas Reichel --- .../transpiler/JSQLExpressionTranspiler.java | 12 +++++++++++ .../starlake/transpiler/any/except_clause.sql | 21 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 src/test/resources/ai/starlake/transpiler/any/except_clause.sql diff --git a/src/main/java/ai/starlake/transpiler/JSQLExpressionTranspiler.java b/src/main/java/ai/starlake/transpiler/JSQLExpressionTranspiler.java index c569228..ee0eb43 100644 --- a/src/main/java/ai/starlake/transpiler/JSQLExpressionTranspiler.java +++ b/src/main/java/ai/starlake/transpiler/JSQLExpressionTranspiler.java @@ -51,6 +51,7 @@ import net.sf.jsqlparser.schema.Column; import net.sf.jsqlparser.schema.Table; import net.sf.jsqlparser.statement.create.table.ColDataType; +import net.sf.jsqlparser.statement.select.AllColumns; import net.sf.jsqlparser.statement.select.OrderByElement; import net.sf.jsqlparser.statement.select.ParenthesedSelect; import net.sf.jsqlparser.statement.select.PlainSelect; @@ -1002,6 +1003,17 @@ public void visit(Column column) { } } + public void visit(AllColumns allColumns) { + if (allColumns.getReplaceExpressions() != null) { + warning("DuckDB replaces Column's content instead Column's label, so unsupported."); + allColumns.setReplaceExpressions(null); + } + + // DuckDB uses "EXCLUDE" instead "EXCEPT", because why not?! + super.visit(allColumns.getExceptColumns() != null ? allColumns.setExceptKeyword("EXCLUDE") + : allColumns); + } + @SuppressWarnings({"PMD.ExcessiveMethodLength"}) public void visit(AnalyticExpression function) { String functionName = function.getName(); diff --git a/src/test/resources/ai/starlake/transpiler/any/except_clause.sql b/src/test/resources/ai/starlake/transpiler/any/except_clause.sql new file mode 100644 index 0000000..586eb0a --- /dev/null +++ b/src/test/resources/ai/starlake/transpiler/any/except_clause.sql @@ -0,0 +1,21 @@ +-- provided +SELECT TOP 10 * EXCEPT( salesid, listid, sellerid) REPLACE( eventid AS "EVENT_ID") +FROM sales; + +-- expected +SELECT * /*WARNING: DuckDB replaces Column's content instead Column's label, so unsupported.*/ EXCLUDE( salesid, listid, sellerid) +FROM sales +LIMIT 10; + +-- result +"buyerid","eventid","dateid","qtysold","pricepaid","commission","saletime" +"21191","7872","1875","4","728.00","109.20","2008-02-18 02:36:48.0" +"11498","4337","1983","2","76.00","11.40","2008-06-06 05:00:16.0" +"17433","8647","1983","2","350.00","52.50","2008-06-06 08:26:17.0" +"19715","8647","1986","1","175.00","26.25","2008-06-09 08:38:52.0" +"14115","8240","2069","2","154.00","23.10","2008-08-31 09:17:02.0" +"24888","3375","2023","2","394.00","59.10","2008-07-16 11:59:24.0" +"7952","3375","2003","4","788.00","118.20","2008-06-26 12:56:06.0" +"19715","3375","2017","1","197.00","29.55","2008-07-10 02:12:36.0" +"29891","3375","2029","3","591.00","88.65","2008-07-22 02:23:17.0" +"10542","4769","2044","1","65.00","9.75","2008-08-06 02:51:55.0"