Skip to content

Commit

Permalink
feature: support many more TEXT 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 Mar 25, 2024
1 parent 0599f38 commit 40e39f4
Show file tree
Hide file tree
Showing 8 changed files with 848 additions and 31 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ bin/

**/changelog.rst
**/javadoc.rst
**/.~lock.JSQLTranspiler.ods#
**/.~lock.JSQLTranspiler.ods#
/.idea
306 changes: 296 additions & 10 deletions src/main/java/com/manticore/transpiler/ExpressionTranspiler.java

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions src/main/java/com/manticore/transpiler/JSQLTranspiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.Statements;
import net.sf.jsqlparser.statement.select.Limit;
import net.sf.jsqlparser.statement.select.ParenthesedSelect;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.SelectItem;
import net.sf.jsqlparser.statement.select.TableFunction;
import net.sf.jsqlparser.statement.select.Top;
import net.sf.jsqlparser.util.deparser.SelectDeParser;
import org.apache.commons.cli.CommandLine;
Expand Down Expand Up @@ -416,6 +419,20 @@ public void visit(Top top) {
select.setLimit(new Limit().withRowCount(top.getExpression()));
}

public void visit(TableFunction tableFunction) {
if (tableFunction.getFunction().getName().equalsIgnoreCase("unnest")) {
PlainSelect select = new PlainSelect()
.withSelectItems(new SelectItem<>(tableFunction.getFunction(), tableFunction.getAlias()));

ParenthesedSelect parenthesedSelect =
new ParenthesedSelect().withSelect(select).withAlias(tableFunction.getAlias());

visit(parenthesedSelect);
} else {
super.visit(tableFunction);
}
}

/**
* The enum Dialect.
*/
Expand Down
Binary file modified src/main/resources/doc/JSQLTranspiler.ods
Binary file not shown.
45 changes: 45 additions & 0 deletions src/test/java/com/manticore/transpiler/DuckDBFreeze.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.manticore.transpiler;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Disabled
public class DuckDBFreeze {

@Test
@Timeout(value = 10, unit = TimeUnit.SECONDS)
void testFreeze() throws SQLException {
String sqlString =
"WITH example AS\n" + " (SELECT 'абвгд' AS characters, encode('абвгд') bytes)\n"
+ "SELECT\n" + " characters,\n" + " bit_length(characters) AS string_example,\n"
+ " bytes,\n" + " bit_length(bytes) AS bytes_example\n" + "FROM example;\n";

try (Connection connDuck = DriverManager.getConnection("jdbc:duckdb:");
Statement st = connDuck.createStatement();
ResultSet rs = st.executeQuery(sqlString);) {
Assertions.assertTrue(rs.next());
}
}

@Test
void testSalaaU() {
final String regex = "ู";
final String string = "ศูพรรณี";

final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);

Assertions.assertTrue(matcher.find());
}
}
13 changes: 11 additions & 2 deletions src/test/java/com/manticore/transpiler/JSQLTranspilerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,13 @@ static Map<File, List<SQLTest>> getSqlTestMap(File[] testFiles,
}

if (line.toLowerCase().replaceAll("\\s", "").startsWith("--provided")) {
if (test.providedSqlStr != null && test.expectedSqlStr != null
if (test.providedSqlStr != null
&& (test.expectedTally >= 0 || test.expectedResult != null)) {
LOGGER.fine("Found multiple test descriptions in " + file.getName());
// pass through tests not depending on transpiling
if (test.expectedSqlStr == null || test.expectedSqlStr.isEmpty()) {
test.expectedSqlStr = test.providedSqlStr;
}
tests.add(test);

test = new SQLTest(inputDialect, outputDialect);
Expand Down Expand Up @@ -193,6 +197,11 @@ static Map<File, List<SQLTest>> getSqlTestMap(File[] testFiles,
}

}

// pass through tests not depending on transpiling
if (test.expectedSqlStr == null || test.expectedSqlStr.isEmpty()) {
test.expectedSqlStr = test.providedSqlStr;
}
tests.add(test);

sqlMap.put(file, tests);
Expand Down Expand Up @@ -423,7 +432,7 @@ void transpile(File f, int idx, SQLTest t) throws Exception {
resultSetHelperService.setDateTimeFormat("yyyy-MM-dd HH:mm:ss");
csvWriter.setResultService(resultSetHelperService);

csvWriter.writeAll(rs, true, true, true);
csvWriter.writeAll(rs, true, false, true);
csvWriter.flush();
stringWriter.flush();
Assertions.assertThat(stringWriter.toString().trim())
Expand Down
21 changes: 3 additions & 18 deletions src/test/resources/com/manticore/transpiler/any/debug.sql
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
-- provided
SELECT
TIMESTAMP('2008-12-25 15:30:00+00') as t1,
TIMESTAMP('2008-12-25 15:30:00+00', 'America/Los_Angeles') as t2,
TIMESTAMP(DATE '2008-12-25 15:30:00+00') AS t3,
TIMESTAMP(DATE '2008-12-25 15:30:00+00', 'America/Los_Angeles') AS t4,
TIMESTAMP(DATETIME '2008-12-25 15:30:00+00') AS t5,
TIMESTAMP(DATETIME '2008-12-25 15:30:00+00', 'America/Los_Angeles') AS t6;

-- expected
SELECT
CAST('2008-12-25 15:30:00+00' AS TIMESTAMPTZ) as t1,
CAST('2008-12-25 15:30:00+00' AS TIMESTAMPTZ) AT TIME ZONE 'America/Los_Angeles' as t2,
CAST(DATE '2008-12-25 15:30:00+00' AS TIMESTAMPTZ) AS t3,
CAST(DATE '2008-12-25 15:30:00+00' AS TIMESTAMPTZ) AT TIME ZONE 'America/Los_Angeles' AS t4,
CAST(DATETIME '2008-12-25 15:30:00+00' AS TIMESTAMPTZ) AS t5,
CAST(DATETIME '2008-12-25 15:30:00+00' AS TIMESTAMPTZ) AT TIME ZONE 'America/Los_Angeles' AS t6;
SELECT ASCII('abcd') as A, ASCII('a') as B, ASCII('') as C, ASCII(NULL) as D;

-- result
"t1","t2","t3","t4","t5","t6"
"2008-12-25T15:30Z","2008-12-25 07:30:00","2008-12-25T00:00Z","2008-12-24 16:00:00","2008-12-25T08:30Z","2008-12-25 00:30:00"
"A","B","C","D"
"97","97","0",""
Loading

0 comments on commit 40e39f4

Please sign in to comment.