Skip to content

Commit

Permalink
feat: Transpile EXCEPT and REPLACE clauses
Browse files Browse the repository at this point in the history
- 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 <andreas@manticore-projects.com>
  • Loading branch information
manticore-projects committed Jun 10, 2024
1 parent 9a2692f commit 469af1c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/main/java/ai/starlake/transpiler/JSQLExpressionTranspiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
21 changes: 21 additions & 0 deletions src/test/resources/ai/starlake/transpiler/any/except_clause.sql
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 469af1c

Please sign in to comment.