From c67446372c9353436ba0fa085a5865bf33960a1e Mon Sep 17 00:00:00 2001 From: torcato Date: Mon, 29 Apr 2024 10:29:07 +0200 Subject: [PATCH 01/17] wip --- .../snapi/truffle/builtin/jdbc/Jdbc.java | 29 + .../ast/io/jdbc/BinaryReadJdbcQuery.java | 11 +- .../ast/io/jdbc/BoolReadJdbcQuery.java | 21 +- .../truffle/ast/io/jdbc/JdbcQuery.java | 502 +++++++++++------- 4 files changed, 366 insertions(+), 197 deletions(-) diff --git a/snapi-truffle/src/main/java/raw/compiler/snapi/truffle/builtin/jdbc/Jdbc.java b/snapi-truffle/src/main/java/raw/compiler/snapi/truffle/builtin/jdbc/Jdbc.java index aff7a1ff2..19bd024af 100644 --- a/snapi-truffle/src/main/java/raw/compiler/snapi/truffle/builtin/jdbc/Jdbc.java +++ b/snapi-truffle/src/main/java/raw/compiler/snapi/truffle/builtin/jdbc/Jdbc.java @@ -83,4 +83,33 @@ static private ProgramExpressionNode columnReader(String colName, Type t, RawLan }; return new ProgramExpressionNode(lang, frameDescriptor, node); } + + static private ProgramExpressionNode columnReaderByIndex(int index, Type t, , RawLanguage lang) { + FrameDescriptor frameDescriptor = new FrameDescriptor(); + ExpressionNode node = switch (t){ + case Rql2TypeWithProperties r when r.props().contains(tryable) -> { + ProgramExpressionNode inner = columnReaderByIndex(index,r.cloneAndRemoveProp(tryable),lang); + yield new TryableReadJdbcQuery(inner, colName); + } + case Rql2TypeWithProperties r when r.props().contains(nullable) -> { + ProgramExpressionNode inner = columnReader(colName,r.cloneAndRemoveProp(nullable),lang); + yield new NullableReadJdbcQuery(inner, colName); + } + case Rql2ByteType ignored -> new ByteReadJdbcQuery(colName); + case Rql2ShortType ignored -> new ShortReadJdbcQuery(colName); + case Rql2IntType ignored -> new IntReadJdbcQuery(colName); + case Rql2LongType ignored -> new LongReadJdbcQuery(colName); + case Rql2FloatType ignored -> new FloatReadJdbcQuery(colName); + case Rql2DoubleType ignored -> new DoubleReadJdbcQuery(colName); + case Rql2DecimalType ignored -> new DecimalReadJdbcQuery(colName); + case Rql2StringType ignored -> new StringReadJdbcQuery(colName); + case Rql2DateType ignored -> new DateReadJdbcQuery(colName); + case Rql2TimeType ignored -> new TimeReadJdbcQuery(colName); + case Rql2TimestampType ignored -> new TimestampReadJdbcQuery(colName); + case Rql2BoolType ignored -> new BoolReadJdbcQuery(colName); + case Rql2BinaryType ignored -> new BinaryReadJdbcQuery(colName); + default -> throw new RawTruffleInternalErrorException(); + }; + return new ProgramExpressionNode(lang, frameDescriptor, node); + } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/BinaryReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/BinaryReadJdbcQuery.java index ef0c9bc5e..7af6c9ff0 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/BinaryReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/BinaryReadJdbcQuery.java @@ -20,10 +20,13 @@ @NodeInfo(shortName = "Jdbc.BinaryRead") public class BinaryReadJdbcQuery extends ExpressionNode { - private final String idx; + private final String colName; - public BinaryReadJdbcQuery(String idx) { - this.idx = idx; + private final int index; + + public BinaryReadJdbcQuery(String colName, int index) { + this.colName = colName; + this.index = index; } public Object executeGeneric(VirtualFrame frame) { @@ -34,6 +37,6 @@ public Object executeGeneric(VirtualFrame frame) { public final BinaryObject executeBinary(VirtualFrame frame) { Object[] args = frame.getArguments(); JdbcQuery rs = (JdbcQuery) args[0]; - return new BinaryObject(rs.getBytes(idx, this)); + return new BinaryObject(rs.getBytes(index, colName, this)); } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/BoolReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/BoolReadJdbcQuery.java index 100cb4d5d..08223fc51 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/BoolReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/BoolReadJdbcQuery.java @@ -19,15 +19,18 @@ @NodeInfo(shortName = "Jdbc.BoolRead") public class BoolReadJdbcQuery extends ExpressionNode { - private final String idx; + private final String colName; - public BoolReadJdbcQuery(String idx) { - this.idx = idx; - } + private final int index; - public Object executeGeneric(VirtualFrame frame) { - Object[] args = frame.getArguments(); - JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getBool(idx, this); - } + public BoolReadJdbcQuery(String colname, int index) { + this.colName = colname; + this.index = index; + } + + public Object executeGeneric(VirtualFrame frame) { + Object[] args = frame.getArguments(); + JdbcQuery rs = (JdbcQuery) args[0]; + return rs.getBool(index, colName, this); + } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/JdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/JdbcQuery.java index 09a55ba0c..9e417bcb4 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/JdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/JdbcQuery.java @@ -14,10 +14,12 @@ import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; import com.oracle.truffle.api.nodes.Node; + import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; + import raw.client.api.LocationDescription; import raw.runtime.truffle.runtime.exceptions.rdbms.JdbcExceptionHandler; import raw.runtime.truffle.runtime.exceptions.rdbms.JdbcReaderRawTruffleException; @@ -31,188 +33,320 @@ public class JdbcQuery { - private final Connection connection; - private final ResultSet rs; - private final JdbcExceptionHandler exceptionHandler; - private final String url; - - @TruffleBoundary - public JdbcQuery( - LocationDescription locationDescription, - String query, - SourceContext context, - JdbcExceptionHandler exceptionHandler) { - this.exceptionHandler = exceptionHandler; - this.url = locationDescription.url(); - try { - connection = JdbcLocationProvider.build(locationDescription, context).getJdbcConnection(); - PreparedStatement stmt; - int fetchSize = context.settings().getInt("raw.runtime.rdbms.fetch-size"); - try { - stmt = connection.prepareStatement(query); - stmt.setFetchSize(fetchSize); - rs = stmt.executeQuery(); - } catch (SQLException e) { - throw exceptionHandler.rewrite(e, this); - } - } catch (RawException e) { - // exceptions due to location errors (e.g. connection failures) are turned into runtime - // exceptions. - throw new JdbcReaderRawTruffleException(e.getMessage(), this, e, null); - } - } - - @TruffleBoundary - public void close() { - if (rs != null) { - try { - rs.close(); - connection.close(); - } catch (SQLException ignored) { - } - } - } - - @TruffleBoundary - public boolean next() { - try { - return rs.next(); - } catch (SQLException e) { - throw exceptionHandler.rewrite(e, this); - } - } - - @TruffleBoundary - byte getByte(String colName, Node node) { - try { - return rs.getByte(colName); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - short getShort(String colName, Node node) { - try { - return rs.getShort(colName); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - int getInt(String colName, Node node) { - try { - return rs.getInt(colName); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - long getLong(String colName, Node node) { - try { - return rs.getLong(colName); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - float getFloat(String colName, Node node) { - try { - return rs.getFloat(colName); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - double getDouble(String colName, Node node) { - try { - return rs.getDouble(colName); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - DecimalObject getDecimal(String colName, Node node) { - try { - return new DecimalObject(rs.getBigDecimal(colName)); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - String getString(String colName, Node node) { - try { - return rs.getString(colName); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - boolean getBool(String colName, Node node) { - try { - return rs.getBoolean(colName); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - DateObject getDate(String colName, Node node) { - try { - java.sql.Date sqlDate = rs.getDate(colName); - return new DateObject(sqlDate.toLocalDate()); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - TimeObject getTime(String colName, Node node) { - try { - java.sql.Time sqlTime = rs.getTime(colName); - return new TimeObject(sqlTime.toLocalTime()); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - TimestampObject getTimestamp(String colName, Node node) { - try { - java.sql.Timestamp sqlTimestamp = rs.getTimestamp(colName); - return new TimestampObject(sqlTimestamp.toLocalDateTime()); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - byte[] getBytes(String colName, Node node) { - try { - return rs.getBytes(colName); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - boolean isNull(String colName, Node node) { - try { - rs.getObject(colName); - return rs.wasNull(); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - public String location() { - return url; - } + private final Connection connection; + private final ResultSet rs; + private final JdbcExceptionHandler exceptionHandler; + private final String url; + + @TruffleBoundary + public JdbcQuery( + LocationDescription locationDescription, + String query, + SourceContext context, + JdbcExceptionHandler exceptionHandler) { + this.exceptionHandler = exceptionHandler; + this.url = locationDescription.url(); + try { + connection = JdbcLocationProvider.build(locationDescription, context).getJdbcConnection(); + PreparedStatement stmt; + int fetchSize = context.settings().getInt("raw.runtime.rdbms.fetch-size"); + try { + stmt = connection.prepareStatement(query); + stmt.setFetchSize(fetchSize); + rs = stmt.executeQuery(); + } catch (SQLException e) { + throw exceptionHandler.rewrite(e, this); + } + } catch (RawException e) { + // exceptions due to location errors (e.g. connection failures) are turned into runtime + // exceptions. + throw new JdbcReaderRawTruffleException(e.getMessage(), this, e, null); + } + } + + @TruffleBoundary + public void close() { + if (rs != null) { + try { + rs.close(); + connection.close(); + } catch (SQLException ignored) { + } + } + } + + @TruffleBoundary + public boolean next() { + try { + return rs.next(); + } catch (SQLException e) { + throw exceptionHandler.rewrite(e, this); + } + } + + @TruffleBoundary + byte getByte(String colName, Node node) { + try { + return rs.getByte(colName); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + byte getByte(int idx, String colName, Node node) { + try { + return rs.getByte(idx); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + short getShort(String colName, Node node) { + try { + return rs.getShort(colName); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + short getShort(int idx, String colName, Node node) { + try { + return rs.getShort(idx); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + + @TruffleBoundary + int getInt(String colName, Node node) { + try { + return rs.getInt(colName); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + int getInt(int idx, String colName, Node node) { + try { + return rs.getInt(idx); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + long getLong(String colName, Node node) { + try { + return rs.getLong(colName); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + long getLong(int idx, String colName, Node node) { + try { + return rs.getLong(idx); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + float getFloat(String colName, Node node) { + try { + return rs.getFloat(colName); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + float getFloat(int idx, String colName, Node node) { + try { + return rs.getFloat(idx); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + double getDouble(String colName, Node node) { + try { + return rs.getDouble(colName); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + double getDouble(int idx, String colName, Node node) { + try { + return rs.getDouble(idx); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + DecimalObject getDecimal(String colName, Node node) { + try { + return new DecimalObject(rs.getBigDecimal(colName)); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + DecimalObject getDecimal(int idx, String colName, Node node) { + try { + return new DecimalObject(rs.getBigDecimal(idx)); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + String getString(String colName, Node node) { + try { + return rs.getString(colName); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + String getString(int idx, String colName, Node node) { + try { + return rs.getString(idx); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + boolean getBool(String colName, Node node) { + try { + return rs.getBoolean(colName); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + boolean getBool(int idx, String colName, Node node) { + try { + return rs.getBoolean(idx); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + DateObject getDate(String colName, Node node) { + try { + java.sql.Date sqlDate = rs.getDate(colName); + return new DateObject(sqlDate.toLocalDate()); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + DateObject getDate(int idx, String colName, Node node) { + try { + java.sql.Date sqlDate = rs.getDate(idx); + return new DateObject(sqlDate.toLocalDate()); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + TimeObject getTime(String colName, Node node) { + try { + java.sql.Time sqlTime = rs.getTime(colName); + return new TimeObject(sqlTime.toLocalTime()); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + TimeObject getTime(int idx, String colName, Node node) { + try { + java.sql.Time sqlTime = rs.getTime(idx); + return new TimeObject(sqlTime.toLocalTime()); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + + @TruffleBoundary + TimestampObject getTimestamp(String colName, Node node) { + try { + java.sql.Timestamp sqlTimestamp = rs.getTimestamp(colName); + return new TimestampObject(sqlTimestamp.toLocalDateTime()); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + TimestampObject getTimestamp(int idx, String colName, Node node) { + try { + java.sql.Timestamp sqlTimestamp = rs.getTimestamp(idx); + return new TimestampObject(sqlTimestamp.toLocalDateTime()); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + byte[] getBytes(String colName, Node node) { + try { + return rs.getBytes(colName); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + byte[] getBytes(int idx, String colName, Node node) { + try { + return rs.getBytes(idx); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + boolean isNull(String colName, Node node) { + try { + rs.getObject(colName); + return rs.wasNull(); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + boolean isNull(int idx, String colName, Node node) { + try { + rs.getObject(idx); + return rs.wasNull(); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + public String location() { + return url; + } } From a7c6de0abbb61fdd7379f8208a47b6b6eaf744db Mon Sep 17 00:00:00 2001 From: torcato Date: Mon, 29 Apr 2024 11:04:19 +0200 Subject: [PATCH 02/17] Adding index to read classes --- .../ast/io/jdbc/BinaryReadJdbcQuery.java | 1 - .../ast/io/jdbc/BoolReadJdbcQuery.java | 1 - .../ast/io/jdbc/ByteReadJdbcQuery.java | 28 +++++++++-------- .../ast/io/jdbc/DateReadJdbcQuery.java | 28 +++++++++-------- .../ast/io/jdbc/DecimalReadJdbcQuery.java | 28 +++++++++-------- .../ast/io/jdbc/DoubleReadJdbcQuery.java | 28 +++++++++-------- .../ast/io/jdbc/FloatReadJdbcQuery.java | 28 +++++++++-------- .../truffle/ast/io/jdbc/IntReadJdbcQuery.java | 10 +++--- .../ast/io/jdbc/LongReadJdbcQuery.java | 28 +++++++++-------- .../ast/io/jdbc/NullableReadJdbcQuery.java | 12 ++++--- .../ast/io/jdbc/ShortReadJdbcQuery.java | 10 +++--- .../ast/io/jdbc/StringReadJdbcQuery.java | 10 +++--- .../ast/io/jdbc/TimeReadJdbcQuery.java | 10 +++--- .../ast/io/jdbc/TimestampReadJdbcQuery.java | 28 +++++++++-------- .../ast/io/jdbc/TryableReadJdbcQuery.java | 31 ++++++++++--------- 15 files changed, 153 insertions(+), 128 deletions(-) diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/BinaryReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/BinaryReadJdbcQuery.java index 7af6c9ff0..24979f4ae 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/BinaryReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/BinaryReadJdbcQuery.java @@ -21,7 +21,6 @@ public class BinaryReadJdbcQuery extends ExpressionNode { private final String colName; - private final int index; public BinaryReadJdbcQuery(String colName, int index) { diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/BoolReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/BoolReadJdbcQuery.java index 08223fc51..0ce2a4c9d 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/BoolReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/BoolReadJdbcQuery.java @@ -20,7 +20,6 @@ public class BoolReadJdbcQuery extends ExpressionNode { private final String colName; - private final int index; public BoolReadJdbcQuery(String colname, int index) { diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/ByteReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/ByteReadJdbcQuery.java index e1c766348..375baaf04 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/ByteReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/ByteReadJdbcQuery.java @@ -19,20 +19,22 @@ @NodeInfo(shortName = "Jdbc.ByteRead") public class ByteReadJdbcQuery extends ExpressionNode { - private final String idx; + private final String colName; + private final int index; - public ByteReadJdbcQuery(String idx) { - this.idx = idx; - } + public ByteReadJdbcQuery(String colname, int idx) { + this.colName = colname; + this.index = idx; + } - public Object executeGeneric(VirtualFrame frame) { - return this.executeByte(frame); - } + public Object executeGeneric(VirtualFrame frame) { + return this.executeByte(frame); + } - @Override - public final byte executeByte(VirtualFrame frame) { - Object[] args = frame.getArguments(); - JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getByte(idx, this); - } + @Override + public final byte executeByte(VirtualFrame frame) { + Object[] args = frame.getArguments(); + JdbcQuery rs = (JdbcQuery) args[0]; + return rs.getByte(index, colName, this); + } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DateReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DateReadJdbcQuery.java index ace38901c..95fee8f1b 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DateReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DateReadJdbcQuery.java @@ -20,20 +20,22 @@ @NodeInfo(shortName = "Jdbc.DateRead") public class DateReadJdbcQuery extends ExpressionNode { - private final String idx; + private final String colName; + private final int index; - public DateReadJdbcQuery(String idx) { - this.idx = idx; - } + public DateReadJdbcQuery(String colName, int idx) { + this.index = idx; + this.colName = colName; + } - public Object executeGeneric(VirtualFrame frame) { - return this.executeDate(frame); - } + public Object executeGeneric(VirtualFrame frame) { + return this.executeDate(frame); + } - @Override - public final DateObject executeDate(VirtualFrame frame) { - Object[] args = frame.getArguments(); - JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getDate(idx, this); - } + @Override + public final DateObject executeDate(VirtualFrame frame) { + Object[] args = frame.getArguments(); + JdbcQuery rs = (JdbcQuery) args[0]; + return rs.getDate(index, colName, this); + } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DecimalReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DecimalReadJdbcQuery.java index e12c47ac8..a9daa89a0 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DecimalReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DecimalReadJdbcQuery.java @@ -20,20 +20,22 @@ @NodeInfo(shortName = "Jdbc.DecimalRead") public class DecimalReadJdbcQuery extends ExpressionNode { - private final String idx; + private final String colName; + private final int index; - public DecimalReadJdbcQuery(String idx) { - this.idx = idx; - } + public DecimalReadJdbcQuery(String colName, int idx) { + this.colName = colName; + this.index = idx; + } - public Object executeGeneric(VirtualFrame frame) { - return this.executeDecimal(frame); - } + public Object executeGeneric(VirtualFrame frame) { + return this.executeDecimal(frame); + } - @Override - public final DecimalObject executeDecimal(VirtualFrame frame) { - Object[] args = frame.getArguments(); - JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getDecimal(idx, this); - } + @Override + public final DecimalObject executeDecimal(VirtualFrame frame) { + Object[] args = frame.getArguments(); + JdbcQuery rs = (JdbcQuery) args[0]; + return rs.getDecimal(index, colName, this); + } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DoubleReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DoubleReadJdbcQuery.java index 6056973ee..1cce48659 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DoubleReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DoubleReadJdbcQuery.java @@ -19,20 +19,22 @@ @NodeInfo(shortName = "Jdbc.DoubleRead") public class DoubleReadJdbcQuery extends ExpressionNode { - private final String idx; + private final String colName; + private final int index; - public DoubleReadJdbcQuery(String idx) { - this.idx = idx; - } + public DoubleReadJdbcQuery(String colName, int idx) { + this.colName = colName; + this.index = idx; + } - public Object executeGeneric(VirtualFrame frame) { - return this.executeDouble(frame); - } + public Object executeGeneric(VirtualFrame frame) { + return this.executeDouble(frame); + } - @Override - public final double executeDouble(VirtualFrame frame) { - Object[] args = frame.getArguments(); - JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getDouble(idx, this); - } + @Override + public final double executeDouble(VirtualFrame frame) { + Object[] args = frame.getArguments(); + JdbcQuery rs = (JdbcQuery) args[0]; + return rs.getDouble(index, colName, this); + } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/FloatReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/FloatReadJdbcQuery.java index 1f0fc803d..8ab0e0b69 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/FloatReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/FloatReadJdbcQuery.java @@ -19,20 +19,22 @@ @NodeInfo(shortName = "Jdbc.FloatRead") public class FloatReadJdbcQuery extends ExpressionNode { - private final String idx; + private final String colName; + private final int index; - public FloatReadJdbcQuery(String idx) { - this.idx = idx; - } + public FloatReadJdbcQuery(String colName, int idx) { + this.colName = colName; + this.index = idx; + } - public Object executeGeneric(VirtualFrame frame) { - return this.executeFloat(frame); - } + public Object executeGeneric(VirtualFrame frame) { + return this.executeFloat(frame); + } - @Override - public final float executeFloat(VirtualFrame frame) { - Object[] args = frame.getArguments(); - JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getFloat(idx, this); - } + @Override + public final float executeFloat(VirtualFrame frame) { + Object[] args = frame.getArguments(); + JdbcQuery rs = (JdbcQuery) args[0]; + return rs.getFloat(index, colName, this); + } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/IntReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/IntReadJdbcQuery.java index f1b2df0ea..451500c4f 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/IntReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/IntReadJdbcQuery.java @@ -19,10 +19,12 @@ @NodeInfo(shortName = "Jdbc.IntRead") public class IntReadJdbcQuery extends ExpressionNode { - private final String idx; + private final int index; + private final String colName; - public IntReadJdbcQuery(String idx) { - this.idx = idx; + public IntReadJdbcQuery(String colname, int idx) { + this.colName = colname; + this.index = idx; } public Object executeGeneric(VirtualFrame frame) { @@ -33,6 +35,6 @@ public Object executeGeneric(VirtualFrame frame) { public final int executeInt(VirtualFrame frame) { Object[] args = frame.getArguments(); JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getInt(idx, this); + return rs.getInt(index, colName, this); } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/LongReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/LongReadJdbcQuery.java index 77e40a566..fac0cf5af 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/LongReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/LongReadJdbcQuery.java @@ -19,20 +19,22 @@ @NodeInfo(shortName = "Jdbc.LongRead") public class LongReadJdbcQuery extends ExpressionNode { - private final String idx; + private final int index; + private final String colName; - public LongReadJdbcQuery(String idx) { - this.idx = idx; - } + public LongReadJdbcQuery(String colName, int idx) { + this.colName = colName; + this.index = idx; + } - public Object executeGeneric(VirtualFrame frame) { - return this.executeLong(frame); - } + public Object executeGeneric(VirtualFrame frame) { + return this.executeLong(frame); + } - @Override - public final long executeLong(VirtualFrame frame) { - Object[] args = frame.getArguments(); - JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getLong(idx, this); - } + @Override + public final long executeLong(VirtualFrame frame) { + Object[] args = frame.getArguments(); + JdbcQuery rs = (JdbcQuery) args[0]; + return rs.getLong(index, colName, this); + } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/NullableReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/NullableReadJdbcQuery.java index fc165f786..de2e9cd4d 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/NullableReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/NullableReadJdbcQuery.java @@ -23,17 +23,19 @@ public class NullableReadJdbcQuery extends ExpressionNode { @Child private DirectCallNode innerParse; - private final String idx; + private final int index; + private final String colName; - public NullableReadJdbcQuery(ProgramExpressionNode innerParse, String idx) { + public NullableReadJdbcQuery(ProgramExpressionNode innerParse, String colName, int idx) { this.innerParse = DirectCallNode.create(innerParse.getCallTarget()); - this.idx = idx; + this.colName = colName; + this.index = idx; } public Object executeGeneric(VirtualFrame frame) { Object[] args = frame.getArguments(); JdbcQuery rs = (JdbcQuery) args[0]; - if (rs.isNull(idx, this)) return NullObject.INSTANCE; - else return innerParse.call(rs, idx); + if (rs.isNull(index, colName, this)) return NullObject.INSTANCE; + else return innerParse.call(rs, index, colName); } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/ShortReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/ShortReadJdbcQuery.java index 8a1df5acc..f25995195 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/ShortReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/ShortReadJdbcQuery.java @@ -19,10 +19,12 @@ @NodeInfo(shortName = "Jdbc.ShortRead") public class ShortReadJdbcQuery extends ExpressionNode { - private final String idx; + private final String colName; + private final int index; - public ShortReadJdbcQuery(String idx) { - this.idx = idx; + public ShortReadJdbcQuery(String colName, int idx) { + this.colName = colName; + this.index = idx; } public Object executeGeneric(VirtualFrame frame) { @@ -33,6 +35,6 @@ public Object executeGeneric(VirtualFrame frame) { public final short executeShort(VirtualFrame frame) { Object[] args = frame.getArguments(); JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getShort(idx, this); + return rs.getShort(index, colName, this); } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/StringReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/StringReadJdbcQuery.java index 0d58c02c6..1bd6a3498 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/StringReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/StringReadJdbcQuery.java @@ -19,10 +19,12 @@ @NodeInfo(shortName = "StringReadJdbcQuery") public class StringReadJdbcQuery extends ExpressionNode { - private final String idx; + private final String colName; + private final int index; - public StringReadJdbcQuery(String idx) { - this.idx = idx; + public StringReadJdbcQuery(String colName, int idx) { + this.colName = colName; + this.index = idx; } public Object executeGeneric(VirtualFrame frame) { @@ -33,6 +35,6 @@ public Object executeGeneric(VirtualFrame frame) { public final String executeString(VirtualFrame frame) { Object[] args = frame.getArguments(); JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getString(idx, this); + return rs.getString(index, colName, this); } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TimeReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TimeReadJdbcQuery.java index ec29a6d62..9d3163d51 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TimeReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TimeReadJdbcQuery.java @@ -20,10 +20,12 @@ @NodeInfo(shortName = "Time.DateRead") public class TimeReadJdbcQuery extends ExpressionNode { - private final String idx; + private final String colName; + private final int index; - public TimeReadJdbcQuery(String idx) { - this.idx = idx; + public TimeReadJdbcQuery(String colName, int idx) { + this.colName = colName; + this.index = idx; } public Object executeGeneric(VirtualFrame frame) { @@ -34,6 +36,6 @@ public Object executeGeneric(VirtualFrame frame) { public final TimeObject executeTime(VirtualFrame frame) { Object[] args = frame.getArguments(); JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getTime(idx, this); + return rs.getTime(index, colName, this); } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TimestampReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TimestampReadJdbcQuery.java index 3ca0408d7..bb348e22c 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TimestampReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TimestampReadJdbcQuery.java @@ -20,20 +20,22 @@ @NodeInfo(shortName = "Timestamp.DateRead") public class TimestampReadJdbcQuery extends ExpressionNode { - private final String idx; + private final String colName; + private final int index; - public TimestampReadJdbcQuery(String idx) { - this.idx = idx; - } + public TimestampReadJdbcQuery(String colName, int idx) { + this.colName = colName; + this.index = idx; + } - public Object executeGeneric(VirtualFrame frame) { - return this.executeTimestamp(frame); - } + public Object executeGeneric(VirtualFrame frame) { + return this.executeTimestamp(frame); + } - @Override - public final TimestampObject executeTimestamp(VirtualFrame frame) { - Object[] args = frame.getArguments(); - JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getTimestamp(idx, this); - } + @Override + public final TimestampObject executeTimestamp(VirtualFrame frame) { + Object[] args = frame.getArguments(); + JdbcQuery rs = (JdbcQuery) args[0]; + return rs.getTimestamp(index, colName, this); + } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TryableReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TryableReadJdbcQuery.java index a490f5c34..982b66677 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TryableReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TryableReadJdbcQuery.java @@ -23,21 +23,24 @@ @NodeInfo(shortName = "TryableReadJdbcQuery") public class TryableReadJdbcQuery extends ExpressionNode { - @Child private DirectCallNode innerParse; - private final String idx; + @Child + private DirectCallNode innerParse; + private final String colName; + private final int index; - public TryableReadJdbcQuery(ProgramExpressionNode innerParse, String idx) { - this.innerParse = DirectCallNode.create(innerParse.getCallTarget()); - this.idx = idx; - } + public TryableReadJdbcQuery(ProgramExpressionNode innerParse, String colName, int idx) { + this.innerParse = DirectCallNode.create(innerParse.getCallTarget()); + this.colName = colName; + this.index = idx; + } - public Object executeGeneric(VirtualFrame frame) { - Object[] args = frame.getArguments(); - JdbcQuery rs = (JdbcQuery) args[0]; - try { - return innerParse.call(rs, idx); - } catch (RawTruffleRuntimeException e) { - return new ErrorObject(e.getMessage()); + public Object executeGeneric(VirtualFrame frame) { + Object[] args = frame.getArguments(); + JdbcQuery rs = (JdbcQuery) args[0]; + try { + return innerParse.call(rs, index, colName); + } catch (RawTruffleRuntimeException e) { + return new ErrorObject(e.getMessage()); + } } - } } From e27084afade75aebb210ca58eb925a1ffae03ca6 Mon Sep 17 00:00:00 2001 From: torcato Date: Mon, 29 Apr 2024 11:38:26 +0200 Subject: [PATCH 03/17] wip --- .../truffle/builtin/jdbc/AttrWithIndex.java | 13 ++ .../snapi/truffle/builtin/jdbc/Jdbc.java | 144 ++++++++---------- 2 files changed, 76 insertions(+), 81 deletions(-) create mode 100644 snapi-truffle/src/main/java/raw/compiler/snapi/truffle/builtin/jdbc/AttrWithIndex.java diff --git a/snapi-truffle/src/main/java/raw/compiler/snapi/truffle/builtin/jdbc/AttrWithIndex.java b/snapi-truffle/src/main/java/raw/compiler/snapi/truffle/builtin/jdbc/AttrWithIndex.java new file mode 100644 index 000000000..c0b8c952e --- /dev/null +++ b/snapi-truffle/src/main/java/raw/compiler/snapi/truffle/builtin/jdbc/AttrWithIndex.java @@ -0,0 +1,13 @@ +package raw.compiler.snapi.truffle.builtin.jdbc; + +import raw.compiler.rql2.source.Rql2AttrType; + +class AttrWithIndex { + public final int index; + public final Rql2AttrType attr; + + public AttrWithIndex(int index, Rql2AttrType attr) { + this.index = index; + this.attr = attr; + } +} \ No newline at end of file diff --git a/snapi-truffle/src/main/java/raw/compiler/snapi/truffle/builtin/jdbc/Jdbc.java b/snapi-truffle/src/main/java/raw/compiler/snapi/truffle/builtin/jdbc/Jdbc.java index 19bd024af..1f25f4bc0 100644 --- a/snapi-truffle/src/main/java/raw/compiler/snapi/truffle/builtin/jdbc/Jdbc.java +++ b/snapi-truffle/src/main/java/raw/compiler/snapi/truffle/builtin/jdbc/Jdbc.java @@ -26,90 +26,72 @@ import raw.runtime.truffle.runtime.exceptions.rdbms.JdbcExceptionHandler; import scala.collection.JavaConverters; +import java.util.stream.IntStream; +import java.util.stream.Stream; + public class Jdbc { - static public JdbcQueryNode query( - ExpressionNode location, - ExpressionNode query, - Type t, - JdbcExceptionHandler exceptionHandler, - RawLanguage lang) { - Rql2IterableType iterableType = (Rql2IterableType) t; - Rql2RecordType recordType = (Rql2RecordType) iterableType.innerType(); - assert iterableType.props().isEmpty(); - assert recordType.props().isEmpty(); - FrameDescriptor frameDescriptor = new FrameDescriptor(); - ProgramExpressionNode[] columnParsers = - JavaConverters.asJavaCollection(recordType.atts()).stream().map(a -> (Rql2AttrType) a) - .map(att -> columnReader(att.idn(), att.tipe(), lang)) - .toArray(ProgramExpressionNode[]::new); - RecordReadJdbcQuery recordParser = - new RecordReadJdbcQuery( - columnParsers, - JavaConverters.asJavaCollection(recordType.atts()).stream().map(a -> (Rql2AttrType) a).toArray(Rql2AttrType[]::new)); - return new JdbcQueryNode( - location, - query, - new ProgramExpressionNode(lang, frameDescriptor, recordParser), - exceptionHandler); - } - static private ProgramExpressionNode columnReader(String colName, Type t, RawLanguage lang) { - FrameDescriptor frameDescriptor = new FrameDescriptor(); - ExpressionNode node = switch (t){ - case Rql2TypeWithProperties r when r.props().contains(tryable) -> { - ProgramExpressionNode inner = columnReader(colName,r.cloneAndRemoveProp(tryable),lang); - yield new TryableReadJdbcQuery(inner, colName); - } - case Rql2TypeWithProperties r when r.props().contains(nullable) -> { - ProgramExpressionNode inner = columnReader(colName,r.cloneAndRemoveProp(nullable),lang); - yield new NullableReadJdbcQuery(inner, colName); - } - case Rql2ByteType ignored -> new ByteReadJdbcQuery(colName); - case Rql2ShortType ignored -> new ShortReadJdbcQuery(colName); - case Rql2IntType ignored -> new IntReadJdbcQuery(colName); - case Rql2LongType ignored -> new LongReadJdbcQuery(colName); - case Rql2FloatType ignored -> new FloatReadJdbcQuery(colName); - case Rql2DoubleType ignored -> new DoubleReadJdbcQuery(colName); - case Rql2DecimalType ignored -> new DecimalReadJdbcQuery(colName); - case Rql2StringType ignored -> new StringReadJdbcQuery(colName); - case Rql2DateType ignored -> new DateReadJdbcQuery(colName); - case Rql2TimeType ignored -> new TimeReadJdbcQuery(colName); - case Rql2TimestampType ignored -> new TimestampReadJdbcQuery(colName); - case Rql2BoolType ignored -> new BoolReadJdbcQuery(colName); - case Rql2BinaryType ignored -> new BinaryReadJdbcQuery(colName); - default -> throw new RawTruffleInternalErrorException(); - }; - return new ProgramExpressionNode(lang, frameDescriptor, node); - } + static public JdbcQueryNode query( + ExpressionNode location, + ExpressionNode query, + Type t, + JdbcExceptionHandler exceptionHandler, + RawLanguage lang) { + Rql2IterableType iterableType = (Rql2IterableType) t; + Rql2RecordType recordType = (Rql2RecordType) iterableType.innerType(); + assert iterableType.props().isEmpty(); + assert recordType.props().isEmpty(); + + FrameDescriptor frameDescriptor = new FrameDescriptor(); + + Stream attrWithIndexStream = IntStream.range(0, recordType.atts().size()) + .mapToObj(i -> new AttrWithIndex(i, recordType.atts().apply(i))); + + ProgramExpressionNode[] columnParsers = attrWithIndexStream + .map(att -> columnReader(att.index, att.attr.idn(), att.attr.tipe(), lang)) + .toArray(ProgramExpressionNode[]::new); + + // Should we reuse the attrWithIndexStream? + RecordReadJdbcQuery recordParser = + new RecordReadJdbcQuery( + columnParsers, + JavaConverters.asJavaCollection(recordType.atts()).stream().map(a -> (Rql2AttrType) a).toArray(Rql2AttrType[]::new)); + return new JdbcQueryNode( + location, + query, + new ProgramExpressionNode(lang, frameDescriptor, recordParser), + exceptionHandler); + } + + static private ProgramExpressionNode columnReader(int index, String colName, Type t, RawLanguage lang) { + FrameDescriptor frameDescriptor = new FrameDescriptor(); + ExpressionNode node = switch (t) { + case Rql2TypeWithProperties r when r.props().contains(tryable) -> { + ProgramExpressionNode inner = columnReader(index, colName, r.cloneAndRemoveProp(tryable), lang); + yield new TryableReadJdbcQuery(inner, colName, index); + } + case Rql2TypeWithProperties r when r.props().contains(nullable) -> { + ProgramExpressionNode inner = columnReader(index, colName, r.cloneAndRemoveProp(nullable), lang); + yield new NullableReadJdbcQuery(inner, colName, index); + } + case Rql2ByteType ignored -> new ByteReadJdbcQuery(colName, index); + case Rql2ShortType ignored -> new ShortReadJdbcQuery(colName, index); + case Rql2IntType ignored -> new IntReadJdbcQuery(colName, index); + case Rql2LongType ignored -> new LongReadJdbcQuery(colName, index); + case Rql2FloatType ignored -> new FloatReadJdbcQuery(colName, index); + case Rql2DoubleType ignored -> new DoubleReadJdbcQuery(colName, index); + case Rql2DecimalType ignored -> new DecimalReadJdbcQuery(colName, index); + case Rql2StringType ignored -> new StringReadJdbcQuery(colName, index); + case Rql2DateType ignored -> new DateReadJdbcQuery(colName, index); + case Rql2TimeType ignored -> new TimeReadJdbcQuery(colName, index); + case Rql2TimestampType ignored -> new TimestampReadJdbcQuery(colName, index); + case Rql2BoolType ignored -> new BoolReadJdbcQuery(colName, index); + case Rql2BinaryType ignored -> new BinaryReadJdbcQuery(colName, index); + default -> throw new RawTruffleInternalErrorException(); + }; + return new ProgramExpressionNode(lang, frameDescriptor, node); + } - static private ProgramExpressionNode columnReaderByIndex(int index, Type t, , RawLanguage lang) { - FrameDescriptor frameDescriptor = new FrameDescriptor(); - ExpressionNode node = switch (t){ - case Rql2TypeWithProperties r when r.props().contains(tryable) -> { - ProgramExpressionNode inner = columnReaderByIndex(index,r.cloneAndRemoveProp(tryable),lang); - yield new TryableReadJdbcQuery(inner, colName); - } - case Rql2TypeWithProperties r when r.props().contains(nullable) -> { - ProgramExpressionNode inner = columnReader(colName,r.cloneAndRemoveProp(nullable),lang); - yield new NullableReadJdbcQuery(inner, colName); - } - case Rql2ByteType ignored -> new ByteReadJdbcQuery(colName); - case Rql2ShortType ignored -> new ShortReadJdbcQuery(colName); - case Rql2IntType ignored -> new IntReadJdbcQuery(colName); - case Rql2LongType ignored -> new LongReadJdbcQuery(colName); - case Rql2FloatType ignored -> new FloatReadJdbcQuery(colName); - case Rql2DoubleType ignored -> new DoubleReadJdbcQuery(colName); - case Rql2DecimalType ignored -> new DecimalReadJdbcQuery(colName); - case Rql2StringType ignored -> new StringReadJdbcQuery(colName); - case Rql2DateType ignored -> new DateReadJdbcQuery(colName); - case Rql2TimeType ignored -> new TimeReadJdbcQuery(colName); - case Rql2TimestampType ignored -> new TimestampReadJdbcQuery(colName); - case Rql2BoolType ignored -> new BoolReadJdbcQuery(colName); - case Rql2BinaryType ignored -> new BinaryReadJdbcQuery(colName); - default -> throw new RawTruffleInternalErrorException(); - }; - return new ProgramExpressionNode(lang, frameDescriptor, node); - } } From 20d727e26d2b504bac62adc5ce7e4cf192082b5f Mon Sep 17 00:00:00 2001 From: torcato Date: Mon, 29 Apr 2024 14:30:06 +0200 Subject: [PATCH 04/17] wip --- .../truffle/builtin/jdbc/AttrWithIndex.java | 28 +- .../ast/io/jdbc/BoolReadJdbcQuery.java | 22 +- .../ast/io/jdbc/ByteReadJdbcQuery.java | 30 +- .../ast/io/jdbc/DateReadJdbcQuery.java | 30 +- .../ast/io/jdbc/DecimalReadJdbcQuery.java | 30 +- .../ast/io/jdbc/DoubleReadJdbcQuery.java | 30 +- .../ast/io/jdbc/FloatReadJdbcQuery.java | 30 +- .../truffle/ast/io/jdbc/JdbcQuery.java | 632 +++++++++--------- .../ast/io/jdbc/LongReadJdbcQuery.java | 30 +- .../ast/io/jdbc/TimestampReadJdbcQuery.java | 30 +- .../ast/io/jdbc/TryableReadJdbcQuery.java | 33 +- 11 files changed, 466 insertions(+), 459 deletions(-) diff --git a/snapi-truffle/src/main/java/raw/compiler/snapi/truffle/builtin/jdbc/AttrWithIndex.java b/snapi-truffle/src/main/java/raw/compiler/snapi/truffle/builtin/jdbc/AttrWithIndex.java index c0b8c952e..9f460874a 100644 --- a/snapi-truffle/src/main/java/raw/compiler/snapi/truffle/builtin/jdbc/AttrWithIndex.java +++ b/snapi-truffle/src/main/java/raw/compiler/snapi/truffle/builtin/jdbc/AttrWithIndex.java @@ -1,13 +1,25 @@ +/* + * Copyright 2023 RAW Labs S.A. + * + * Use of this software is governed by the Business Source License + * included in the file licenses/BSL.txt. + * + * As of the Change Date specified in that file, in accordance with + * the Business Source License, use of this software will be governed + * by the Apache License, Version 2.0, included in the file + * licenses/APL.txt. + */ + package raw.compiler.snapi.truffle.builtin.jdbc; import raw.compiler.rql2.source.Rql2AttrType; class AttrWithIndex { - public final int index; - public final Rql2AttrType attr; - - public AttrWithIndex(int index, Rql2AttrType attr) { - this.index = index; - this.attr = attr; - } -} \ No newline at end of file + public final int index; + public final Rql2AttrType attr; + + public AttrWithIndex(int index, Rql2AttrType attr) { + this.index = index; + this.attr = attr; + } +} diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/BoolReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/BoolReadJdbcQuery.java index 0ce2a4c9d..d0dfd57f4 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/BoolReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/BoolReadJdbcQuery.java @@ -19,17 +19,17 @@ @NodeInfo(shortName = "Jdbc.BoolRead") public class BoolReadJdbcQuery extends ExpressionNode { - private final String colName; - private final int index; + private final String colName; + private final int index; - public BoolReadJdbcQuery(String colname, int index) { - this.colName = colname; - this.index = index; - } + public BoolReadJdbcQuery(String colname, int index) { + this.colName = colname; + this.index = index; + } - public Object executeGeneric(VirtualFrame frame) { - Object[] args = frame.getArguments(); - JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getBool(index, colName, this); - } + public Object executeGeneric(VirtualFrame frame) { + Object[] args = frame.getArguments(); + JdbcQuery rs = (JdbcQuery) args[0]; + return rs.getBool(index, colName, this); + } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/ByteReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/ByteReadJdbcQuery.java index 375baaf04..812116feb 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/ByteReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/ByteReadJdbcQuery.java @@ -19,22 +19,22 @@ @NodeInfo(shortName = "Jdbc.ByteRead") public class ByteReadJdbcQuery extends ExpressionNode { - private final String colName; - private final int index; + private final String colName; + private final int index; - public ByteReadJdbcQuery(String colname, int idx) { - this.colName = colname; - this.index = idx; - } + public ByteReadJdbcQuery(String colname, int idx) { + this.colName = colname; + this.index = idx; + } - public Object executeGeneric(VirtualFrame frame) { - return this.executeByte(frame); - } + public Object executeGeneric(VirtualFrame frame) { + return this.executeByte(frame); + } - @Override - public final byte executeByte(VirtualFrame frame) { - Object[] args = frame.getArguments(); - JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getByte(index, colName, this); - } + @Override + public final byte executeByte(VirtualFrame frame) { + Object[] args = frame.getArguments(); + JdbcQuery rs = (JdbcQuery) args[0]; + return rs.getByte(index, colName, this); + } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DateReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DateReadJdbcQuery.java index 95fee8f1b..5c5caeed1 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DateReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DateReadJdbcQuery.java @@ -20,22 +20,22 @@ @NodeInfo(shortName = "Jdbc.DateRead") public class DateReadJdbcQuery extends ExpressionNode { - private final String colName; - private final int index; + private final String colName; + private final int index; - public DateReadJdbcQuery(String colName, int idx) { - this.index = idx; - this.colName = colName; - } + public DateReadJdbcQuery(String colName, int idx) { + this.index = idx; + this.colName = colName; + } - public Object executeGeneric(VirtualFrame frame) { - return this.executeDate(frame); - } + public Object executeGeneric(VirtualFrame frame) { + return this.executeDate(frame); + } - @Override - public final DateObject executeDate(VirtualFrame frame) { - Object[] args = frame.getArguments(); - JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getDate(index, colName, this); - } + @Override + public final DateObject executeDate(VirtualFrame frame) { + Object[] args = frame.getArguments(); + JdbcQuery rs = (JdbcQuery) args[0]; + return rs.getDate(index, colName, this); + } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DecimalReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DecimalReadJdbcQuery.java index a9daa89a0..a223262cb 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DecimalReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DecimalReadJdbcQuery.java @@ -20,22 +20,22 @@ @NodeInfo(shortName = "Jdbc.DecimalRead") public class DecimalReadJdbcQuery extends ExpressionNode { - private final String colName; - private final int index; + private final String colName; + private final int index; - public DecimalReadJdbcQuery(String colName, int idx) { - this.colName = colName; - this.index = idx; - } + public DecimalReadJdbcQuery(String colName, int idx) { + this.colName = colName; + this.index = idx; + } - public Object executeGeneric(VirtualFrame frame) { - return this.executeDecimal(frame); - } + public Object executeGeneric(VirtualFrame frame) { + return this.executeDecimal(frame); + } - @Override - public final DecimalObject executeDecimal(VirtualFrame frame) { - Object[] args = frame.getArguments(); - JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getDecimal(index, colName, this); - } + @Override + public final DecimalObject executeDecimal(VirtualFrame frame) { + Object[] args = frame.getArguments(); + JdbcQuery rs = (JdbcQuery) args[0]; + return rs.getDecimal(index, colName, this); + } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DoubleReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DoubleReadJdbcQuery.java index 1cce48659..c0eb75fba 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DoubleReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DoubleReadJdbcQuery.java @@ -19,22 +19,22 @@ @NodeInfo(shortName = "Jdbc.DoubleRead") public class DoubleReadJdbcQuery extends ExpressionNode { - private final String colName; - private final int index; + private final String colName; + private final int index; - public DoubleReadJdbcQuery(String colName, int idx) { - this.colName = colName; - this.index = idx; - } + public DoubleReadJdbcQuery(String colName, int idx) { + this.colName = colName; + this.index = idx; + } - public Object executeGeneric(VirtualFrame frame) { - return this.executeDouble(frame); - } + public Object executeGeneric(VirtualFrame frame) { + return this.executeDouble(frame); + } - @Override - public final double executeDouble(VirtualFrame frame) { - Object[] args = frame.getArguments(); - JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getDouble(index, colName, this); - } + @Override + public final double executeDouble(VirtualFrame frame) { + Object[] args = frame.getArguments(); + JdbcQuery rs = (JdbcQuery) args[0]; + return rs.getDouble(index, colName, this); + } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/FloatReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/FloatReadJdbcQuery.java index 8ab0e0b69..e481d1341 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/FloatReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/FloatReadJdbcQuery.java @@ -19,22 +19,22 @@ @NodeInfo(shortName = "Jdbc.FloatRead") public class FloatReadJdbcQuery extends ExpressionNode { - private final String colName; - private final int index; + private final String colName; + private final int index; - public FloatReadJdbcQuery(String colName, int idx) { - this.colName = colName; - this.index = idx; - } + public FloatReadJdbcQuery(String colName, int idx) { + this.colName = colName; + this.index = idx; + } - public Object executeGeneric(VirtualFrame frame) { - return this.executeFloat(frame); - } + public Object executeGeneric(VirtualFrame frame) { + return this.executeFloat(frame); + } - @Override - public final float executeFloat(VirtualFrame frame) { - Object[] args = frame.getArguments(); - JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getFloat(index, colName, this); - } + @Override + public final float executeFloat(VirtualFrame frame) { + Object[] args = frame.getArguments(); + JdbcQuery rs = (JdbcQuery) args[0]; + return rs.getFloat(index, colName, this); + } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/JdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/JdbcQuery.java index 9e417bcb4..8015e64d2 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/JdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/JdbcQuery.java @@ -14,12 +14,10 @@ import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; import com.oracle.truffle.api.nodes.Node; - import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; - import raw.client.api.LocationDescription; import raw.runtime.truffle.runtime.exceptions.rdbms.JdbcExceptionHandler; import raw.runtime.truffle.runtime.exceptions.rdbms.JdbcReaderRawTruffleException; @@ -33,320 +31,318 @@ public class JdbcQuery { - private final Connection connection; - private final ResultSet rs; - private final JdbcExceptionHandler exceptionHandler; - private final String url; - - @TruffleBoundary - public JdbcQuery( - LocationDescription locationDescription, - String query, - SourceContext context, - JdbcExceptionHandler exceptionHandler) { - this.exceptionHandler = exceptionHandler; - this.url = locationDescription.url(); - try { - connection = JdbcLocationProvider.build(locationDescription, context).getJdbcConnection(); - PreparedStatement stmt; - int fetchSize = context.settings().getInt("raw.runtime.rdbms.fetch-size"); - try { - stmt = connection.prepareStatement(query); - stmt.setFetchSize(fetchSize); - rs = stmt.executeQuery(); - } catch (SQLException e) { - throw exceptionHandler.rewrite(e, this); - } - } catch (RawException e) { - // exceptions due to location errors (e.g. connection failures) are turned into runtime - // exceptions. - throw new JdbcReaderRawTruffleException(e.getMessage(), this, e, null); - } - } - - @TruffleBoundary - public void close() { - if (rs != null) { - try { - rs.close(); - connection.close(); - } catch (SQLException ignored) { - } - } - } - - @TruffleBoundary - public boolean next() { - try { - return rs.next(); - } catch (SQLException e) { - throw exceptionHandler.rewrite(e, this); - } - } - - @TruffleBoundary - byte getByte(String colName, Node node) { - try { - return rs.getByte(colName); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - byte getByte(int idx, String colName, Node node) { - try { - return rs.getByte(idx); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - short getShort(String colName, Node node) { - try { - return rs.getShort(colName); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - short getShort(int idx, String colName, Node node) { - try { - return rs.getShort(idx); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - - @TruffleBoundary - int getInt(String colName, Node node) { - try { - return rs.getInt(colName); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - int getInt(int idx, String colName, Node node) { - try { - return rs.getInt(idx); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - long getLong(String colName, Node node) { - try { - return rs.getLong(colName); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - long getLong(int idx, String colName, Node node) { - try { - return rs.getLong(idx); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - float getFloat(String colName, Node node) { - try { - return rs.getFloat(colName); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - float getFloat(int idx, String colName, Node node) { - try { - return rs.getFloat(idx); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - double getDouble(String colName, Node node) { - try { - return rs.getDouble(colName); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - double getDouble(int idx, String colName, Node node) { - try { - return rs.getDouble(idx); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - DecimalObject getDecimal(String colName, Node node) { - try { - return new DecimalObject(rs.getBigDecimal(colName)); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - DecimalObject getDecimal(int idx, String colName, Node node) { - try { - return new DecimalObject(rs.getBigDecimal(idx)); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - String getString(String colName, Node node) { - try { - return rs.getString(colName); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - String getString(int idx, String colName, Node node) { - try { - return rs.getString(idx); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - boolean getBool(String colName, Node node) { - try { - return rs.getBoolean(colName); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - boolean getBool(int idx, String colName, Node node) { - try { - return rs.getBoolean(idx); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - DateObject getDate(String colName, Node node) { - try { - java.sql.Date sqlDate = rs.getDate(colName); - return new DateObject(sqlDate.toLocalDate()); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - DateObject getDate(int idx, String colName, Node node) { - try { - java.sql.Date sqlDate = rs.getDate(idx); - return new DateObject(sqlDate.toLocalDate()); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - TimeObject getTime(String colName, Node node) { - try { - java.sql.Time sqlTime = rs.getTime(colName); - return new TimeObject(sqlTime.toLocalTime()); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - TimeObject getTime(int idx, String colName, Node node) { - try { - java.sql.Time sqlTime = rs.getTime(idx); - return new TimeObject(sqlTime.toLocalTime()); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - - @TruffleBoundary - TimestampObject getTimestamp(String colName, Node node) { - try { - java.sql.Timestamp sqlTimestamp = rs.getTimestamp(colName); - return new TimestampObject(sqlTimestamp.toLocalDateTime()); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - TimestampObject getTimestamp(int idx, String colName, Node node) { - try { - java.sql.Timestamp sqlTimestamp = rs.getTimestamp(idx); - return new TimestampObject(sqlTimestamp.toLocalDateTime()); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - byte[] getBytes(String colName, Node node) { - try { - return rs.getBytes(colName); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - byte[] getBytes(int idx, String colName, Node node) { - try { - return rs.getBytes(idx); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - boolean isNull(String colName, Node node) { - try { - rs.getObject(colName); - return rs.wasNull(); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - @TruffleBoundary - boolean isNull(int idx, String colName, Node node) { - try { - rs.getObject(idx); - return rs.wasNull(); - } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); - } - } - - public String location() { - return url; - } + private final Connection connection; + private final ResultSet rs; + private final JdbcExceptionHandler exceptionHandler; + private final String url; + + @TruffleBoundary + public JdbcQuery( + LocationDescription locationDescription, + String query, + SourceContext context, + JdbcExceptionHandler exceptionHandler) { + this.exceptionHandler = exceptionHandler; + this.url = locationDescription.url(); + try { + connection = JdbcLocationProvider.build(locationDescription, context).getJdbcConnection(); + PreparedStatement stmt; + int fetchSize = context.settings().getInt("raw.runtime.rdbms.fetch-size"); + try { + stmt = connection.prepareStatement(query); + stmt.setFetchSize(fetchSize); + rs = stmt.executeQuery(); + } catch (SQLException e) { + throw exceptionHandler.rewrite(e, this); + } + } catch (RawException e) { + // exceptions due to location errors (e.g. connection failures) are turned into runtime + // exceptions. + throw new JdbcReaderRawTruffleException(e.getMessage(), this, e, null); + } + } + + @TruffleBoundary + public void close() { + if (rs != null) { + try { + rs.close(); + connection.close(); + } catch (SQLException ignored) { + } + } + } + + @TruffleBoundary + public boolean next() { + try { + return rs.next(); + } catch (SQLException e) { + throw exceptionHandler.rewrite(e, this); + } + } + + @TruffleBoundary + byte getByte(String colName, Node node) { + try { + return rs.getByte(colName); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + byte getByte(int idx, String colName, Node node) { + try { + return rs.getByte(idx); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + short getShort(String colName, Node node) { + try { + return rs.getShort(colName); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + short getShort(int idx, String colName, Node node) { + try { + return rs.getShort(idx); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + int getInt(String colName, Node node) { + try { + return rs.getInt(colName); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + int getInt(int idx, String colName, Node node) { + try { + return rs.getInt(idx); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + long getLong(String colName, Node node) { + try { + return rs.getLong(colName); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + long getLong(int idx, String colName, Node node) { + try { + return rs.getLong(idx); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + float getFloat(String colName, Node node) { + try { + return rs.getFloat(colName); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + float getFloat(int idx, String colName, Node node) { + try { + return rs.getFloat(idx); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + double getDouble(String colName, Node node) { + try { + return rs.getDouble(colName); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + double getDouble(int idx, String colName, Node node) { + try { + return rs.getDouble(idx); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + DecimalObject getDecimal(String colName, Node node) { + try { + return new DecimalObject(rs.getBigDecimal(colName)); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + DecimalObject getDecimal(int idx, String colName, Node node) { + try { + return new DecimalObject(rs.getBigDecimal(idx)); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + String getString(String colName, Node node) { + try { + return rs.getString(colName); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + String getString(int idx, String colName, Node node) { + try { + return rs.getString(idx); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + boolean getBool(String colName, Node node) { + try { + return rs.getBoolean(colName); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + boolean getBool(int idx, String colName, Node node) { + try { + return rs.getBoolean(idx); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + DateObject getDate(String colName, Node node) { + try { + java.sql.Date sqlDate = rs.getDate(colName); + return new DateObject(sqlDate.toLocalDate()); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + DateObject getDate(int idx, String colName, Node node) { + try { + java.sql.Date sqlDate = rs.getDate(idx); + return new DateObject(sqlDate.toLocalDate()); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + TimeObject getTime(String colName, Node node) { + try { + java.sql.Time sqlTime = rs.getTime(colName); + return new TimeObject(sqlTime.toLocalTime()); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + TimeObject getTime(int idx, String colName, Node node) { + try { + java.sql.Time sqlTime = rs.getTime(idx); + return new TimeObject(sqlTime.toLocalTime()); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + TimestampObject getTimestamp(String colName, Node node) { + try { + java.sql.Timestamp sqlTimestamp = rs.getTimestamp(colName); + return new TimestampObject(sqlTimestamp.toLocalDateTime()); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + TimestampObject getTimestamp(int idx, String colName, Node node) { + try { + java.sql.Timestamp sqlTimestamp = rs.getTimestamp(idx); + return new TimestampObject(sqlTimestamp.toLocalDateTime()); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + byte[] getBytes(String colName, Node node) { + try { + return rs.getBytes(colName); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + byte[] getBytes(int idx, String colName, Node node) { + try { + return rs.getBytes(idx); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + boolean isNull(String colName, Node node) { + try { + rs.getObject(colName); + return rs.wasNull(); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + @TruffleBoundary + boolean isNull(int idx, String colName, Node node) { + try { + rs.getObject(idx); + return rs.wasNull(); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, colName, node); + } + } + + public String location() { + return url; + } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/LongReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/LongReadJdbcQuery.java index fac0cf5af..a59d3c8e7 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/LongReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/LongReadJdbcQuery.java @@ -19,22 +19,22 @@ @NodeInfo(shortName = "Jdbc.LongRead") public class LongReadJdbcQuery extends ExpressionNode { - private final int index; - private final String colName; + private final int index; + private final String colName; - public LongReadJdbcQuery(String colName, int idx) { - this.colName = colName; - this.index = idx; - } + public LongReadJdbcQuery(String colName, int idx) { + this.colName = colName; + this.index = idx; + } - public Object executeGeneric(VirtualFrame frame) { - return this.executeLong(frame); - } + public Object executeGeneric(VirtualFrame frame) { + return this.executeLong(frame); + } - @Override - public final long executeLong(VirtualFrame frame) { - Object[] args = frame.getArguments(); - JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getLong(index, colName, this); - } + @Override + public final long executeLong(VirtualFrame frame) { + Object[] args = frame.getArguments(); + JdbcQuery rs = (JdbcQuery) args[0]; + return rs.getLong(index, colName, this); + } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TimestampReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TimestampReadJdbcQuery.java index bb348e22c..6925f3fc6 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TimestampReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TimestampReadJdbcQuery.java @@ -20,22 +20,22 @@ @NodeInfo(shortName = "Timestamp.DateRead") public class TimestampReadJdbcQuery extends ExpressionNode { - private final String colName; - private final int index; + private final String colName; + private final int index; - public TimestampReadJdbcQuery(String colName, int idx) { - this.colName = colName; - this.index = idx; - } + public TimestampReadJdbcQuery(String colName, int idx) { + this.colName = colName; + this.index = idx; + } - public Object executeGeneric(VirtualFrame frame) { - return this.executeTimestamp(frame); - } + public Object executeGeneric(VirtualFrame frame) { + return this.executeTimestamp(frame); + } - @Override - public final TimestampObject executeTimestamp(VirtualFrame frame) { - Object[] args = frame.getArguments(); - JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getTimestamp(index, colName, this); - } + @Override + public final TimestampObject executeTimestamp(VirtualFrame frame) { + Object[] args = frame.getArguments(); + JdbcQuery rs = (JdbcQuery) args[0]; + return rs.getTimestamp(index, colName, this); + } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TryableReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TryableReadJdbcQuery.java index 982b66677..9e29a6f90 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TryableReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TryableReadJdbcQuery.java @@ -23,24 +23,23 @@ @NodeInfo(shortName = "TryableReadJdbcQuery") public class TryableReadJdbcQuery extends ExpressionNode { - @Child - private DirectCallNode innerParse; - private final String colName; - private final int index; + @Child private DirectCallNode innerParse; + private final String colName; + private final int index; - public TryableReadJdbcQuery(ProgramExpressionNode innerParse, String colName, int idx) { - this.innerParse = DirectCallNode.create(innerParse.getCallTarget()); - this.colName = colName; - this.index = idx; - } + public TryableReadJdbcQuery(ProgramExpressionNode innerParse, String colName, int idx) { + this.innerParse = DirectCallNode.create(innerParse.getCallTarget()); + this.colName = colName; + this.index = idx; + } - public Object executeGeneric(VirtualFrame frame) { - Object[] args = frame.getArguments(); - JdbcQuery rs = (JdbcQuery) args[0]; - try { - return innerParse.call(rs, index, colName); - } catch (RawTruffleRuntimeException e) { - return new ErrorObject(e.getMessage()); - } + public Object executeGeneric(VirtualFrame frame) { + Object[] args = frame.getArguments(); + JdbcQuery rs = (JdbcQuery) args[0]; + try { + return innerParse.call(rs, index, colName); + } catch (RawTruffleRuntimeException e) { + return new ErrorObject(e.getMessage()); } + } } From 6e5c6b4b4e16754a25728e5e5435f71a30323e9a Mon Sep 17 00:00:00 2001 From: torcato Date: Mon, 29 Apr 2024 15:28:14 +0200 Subject: [PATCH 05/17] Removing colname --- .../snapi/truffle/builtin/jdbc/Jdbc.java | 38 ++++++------- .../ast/io/jdbc/BinaryReadJdbcQuery.java | 6 +- .../ast/io/jdbc/BoolReadJdbcQuery.java | 6 +- .../ast/io/jdbc/ByteReadJdbcQuery.java | 6 +- .../ast/io/jdbc/DateReadJdbcQuery.java | 6 +- .../ast/io/jdbc/DecimalReadJdbcQuery.java | 6 +- .../ast/io/jdbc/DoubleReadJdbcQuery.java | 6 +- .../ast/io/jdbc/FloatReadJdbcQuery.java | 6 +- .../truffle/ast/io/jdbc/IntReadJdbcQuery.java | 6 +- .../truffle/ast/io/jdbc/JdbcQuery.java | 56 +++++++++---------- .../ast/io/jdbc/LongReadJdbcQuery.java | 6 +- .../ast/io/jdbc/NullableReadJdbcQuery.java | 8 +-- .../ast/io/jdbc/ShortReadJdbcQuery.java | 6 +- .../ast/io/jdbc/StringReadJdbcQuery.java | 6 +- .../ast/io/jdbc/TimeReadJdbcQuery.java | 6 +- .../ast/io/jdbc/TimestampReadJdbcQuery.java | 6 +- .../ast/io/jdbc/TryableReadJdbcQuery.java | 6 +- .../rdbms/JdbcExceptionHandler.java | 11 ++++ 18 files changed, 89 insertions(+), 108 deletions(-) diff --git a/snapi-truffle/src/main/java/raw/compiler/snapi/truffle/builtin/jdbc/Jdbc.java b/snapi-truffle/src/main/java/raw/compiler/snapi/truffle/builtin/jdbc/Jdbc.java index 1f25f4bc0..8c48afd0b 100644 --- a/snapi-truffle/src/main/java/raw/compiler/snapi/truffle/builtin/jdbc/Jdbc.java +++ b/snapi-truffle/src/main/java/raw/compiler/snapi/truffle/builtin/jdbc/Jdbc.java @@ -50,7 +50,7 @@ static public JdbcQueryNode query( .mapToObj(i -> new AttrWithIndex(i, recordType.atts().apply(i))); ProgramExpressionNode[] columnParsers = attrWithIndexStream - .map(att -> columnReader(att.index, att.attr.idn(), att.attr.tipe(), lang)) + .map(att -> columnReader(att.index, att.attr.tipe(), lang)) .toArray(ProgramExpressionNode[]::new); // Should we reuse the attrWithIndexStream? @@ -65,30 +65,30 @@ static public JdbcQueryNode query( exceptionHandler); } - static private ProgramExpressionNode columnReader(int index, String colName, Type t, RawLanguage lang) { + static private ProgramExpressionNode columnReader(int index, Type t, RawLanguage lang) { FrameDescriptor frameDescriptor = new FrameDescriptor(); ExpressionNode node = switch (t) { case Rql2TypeWithProperties r when r.props().contains(tryable) -> { - ProgramExpressionNode inner = columnReader(index, colName, r.cloneAndRemoveProp(tryable), lang); - yield new TryableReadJdbcQuery(inner, colName, index); + ProgramExpressionNode inner = columnReader(index, r.cloneAndRemoveProp(tryable), lang); + yield new TryableReadJdbcQuery(inner, index); } case Rql2TypeWithProperties r when r.props().contains(nullable) -> { - ProgramExpressionNode inner = columnReader(index, colName, r.cloneAndRemoveProp(nullable), lang); - yield new NullableReadJdbcQuery(inner, colName, index); + ProgramExpressionNode inner = columnReader(index, r.cloneAndRemoveProp(nullable), lang); + yield new NullableReadJdbcQuery(inner, index); } - case Rql2ByteType ignored -> new ByteReadJdbcQuery(colName, index); - case Rql2ShortType ignored -> new ShortReadJdbcQuery(colName, index); - case Rql2IntType ignored -> new IntReadJdbcQuery(colName, index); - case Rql2LongType ignored -> new LongReadJdbcQuery(colName, index); - case Rql2FloatType ignored -> new FloatReadJdbcQuery(colName, index); - case Rql2DoubleType ignored -> new DoubleReadJdbcQuery(colName, index); - case Rql2DecimalType ignored -> new DecimalReadJdbcQuery(colName, index); - case Rql2StringType ignored -> new StringReadJdbcQuery(colName, index); - case Rql2DateType ignored -> new DateReadJdbcQuery(colName, index); - case Rql2TimeType ignored -> new TimeReadJdbcQuery(colName, index); - case Rql2TimestampType ignored -> new TimestampReadJdbcQuery(colName, index); - case Rql2BoolType ignored -> new BoolReadJdbcQuery(colName, index); - case Rql2BinaryType ignored -> new BinaryReadJdbcQuery(colName, index); + case Rql2ByteType ignored -> new ByteReadJdbcQuery( index); + case Rql2ShortType ignored -> new ShortReadJdbcQuery( index); + case Rql2IntType ignored -> new IntReadJdbcQuery( index); + case Rql2LongType ignored -> new LongReadJdbcQuery( index); + case Rql2FloatType ignored -> new FloatReadJdbcQuery( index); + case Rql2DoubleType ignored -> new DoubleReadJdbcQuery( index); + case Rql2DecimalType ignored -> new DecimalReadJdbcQuery( index); + case Rql2StringType ignored -> new StringReadJdbcQuery( index); + case Rql2DateType ignored -> new DateReadJdbcQuery( index); + case Rql2TimeType ignored -> new TimeReadJdbcQuery( index); + case Rql2TimestampType ignored -> new TimestampReadJdbcQuery( index); + case Rql2BoolType ignored -> new BoolReadJdbcQuery( index); + case Rql2BinaryType ignored -> new BinaryReadJdbcQuery( index); default -> throw new RawTruffleInternalErrorException(); }; return new ProgramExpressionNode(lang, frameDescriptor, node); diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/BinaryReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/BinaryReadJdbcQuery.java index 24979f4ae..22780b723 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/BinaryReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/BinaryReadJdbcQuery.java @@ -20,11 +20,9 @@ @NodeInfo(shortName = "Jdbc.BinaryRead") public class BinaryReadJdbcQuery extends ExpressionNode { - private final String colName; private final int index; - public BinaryReadJdbcQuery(String colName, int index) { - this.colName = colName; + public BinaryReadJdbcQuery(int index) { this.index = index; } @@ -36,6 +34,6 @@ public Object executeGeneric(VirtualFrame frame) { public final BinaryObject executeBinary(VirtualFrame frame) { Object[] args = frame.getArguments(); JdbcQuery rs = (JdbcQuery) args[0]; - return new BinaryObject(rs.getBytes(index, colName, this)); + return new BinaryObject(rs.getBytes(index, this)); } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/BoolReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/BoolReadJdbcQuery.java index d0dfd57f4..a1f34ddbf 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/BoolReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/BoolReadJdbcQuery.java @@ -19,17 +19,15 @@ @NodeInfo(shortName = "Jdbc.BoolRead") public class BoolReadJdbcQuery extends ExpressionNode { - private final String colName; private final int index; - public BoolReadJdbcQuery(String colname, int index) { - this.colName = colname; + public BoolReadJdbcQuery(int index) { this.index = index; } public Object executeGeneric(VirtualFrame frame) { Object[] args = frame.getArguments(); JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getBool(index, colName, this); + return rs.getBool(index, this); } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/ByteReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/ByteReadJdbcQuery.java index 812116feb..b718179e1 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/ByteReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/ByteReadJdbcQuery.java @@ -19,11 +19,9 @@ @NodeInfo(shortName = "Jdbc.ByteRead") public class ByteReadJdbcQuery extends ExpressionNode { - private final String colName; private final int index; - public ByteReadJdbcQuery(String colname, int idx) { - this.colName = colname; + public ByteReadJdbcQuery(int idx) { this.index = idx; } @@ -35,6 +33,6 @@ public Object executeGeneric(VirtualFrame frame) { public final byte executeByte(VirtualFrame frame) { Object[] args = frame.getArguments(); JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getByte(index, colName, this); + return rs.getByte(index, this); } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DateReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DateReadJdbcQuery.java index 5c5caeed1..daee16510 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DateReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DateReadJdbcQuery.java @@ -20,12 +20,10 @@ @NodeInfo(shortName = "Jdbc.DateRead") public class DateReadJdbcQuery extends ExpressionNode { - private final String colName; private final int index; - public DateReadJdbcQuery(String colName, int idx) { + public DateReadJdbcQuery(int idx) { this.index = idx; - this.colName = colName; } public Object executeGeneric(VirtualFrame frame) { @@ -36,6 +34,6 @@ public Object executeGeneric(VirtualFrame frame) { public final DateObject executeDate(VirtualFrame frame) { Object[] args = frame.getArguments(); JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getDate(index, colName, this); + return rs.getDate(index, this); } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DecimalReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DecimalReadJdbcQuery.java index a223262cb..c4f2d383e 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DecimalReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DecimalReadJdbcQuery.java @@ -20,11 +20,9 @@ @NodeInfo(shortName = "Jdbc.DecimalRead") public class DecimalReadJdbcQuery extends ExpressionNode { - private final String colName; private final int index; - public DecimalReadJdbcQuery(String colName, int idx) { - this.colName = colName; + public DecimalReadJdbcQuery(int idx) { this.index = idx; } @@ -36,6 +34,6 @@ public Object executeGeneric(VirtualFrame frame) { public final DecimalObject executeDecimal(VirtualFrame frame) { Object[] args = frame.getArguments(); JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getDecimal(index, colName, this); + return rs.getDecimal(index, this); } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DoubleReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DoubleReadJdbcQuery.java index c0eb75fba..4b78bd874 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DoubleReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/DoubleReadJdbcQuery.java @@ -19,11 +19,9 @@ @NodeInfo(shortName = "Jdbc.DoubleRead") public class DoubleReadJdbcQuery extends ExpressionNode { - private final String colName; private final int index; - public DoubleReadJdbcQuery(String colName, int idx) { - this.colName = colName; + public DoubleReadJdbcQuery(int idx) { this.index = idx; } @@ -35,6 +33,6 @@ public Object executeGeneric(VirtualFrame frame) { public final double executeDouble(VirtualFrame frame) { Object[] args = frame.getArguments(); JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getDouble(index, colName, this); + return rs.getDouble(index, this); } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/FloatReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/FloatReadJdbcQuery.java index e481d1341..f8f815780 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/FloatReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/FloatReadJdbcQuery.java @@ -19,11 +19,9 @@ @NodeInfo(shortName = "Jdbc.FloatRead") public class FloatReadJdbcQuery extends ExpressionNode { - private final String colName; private final int index; - public FloatReadJdbcQuery(String colName, int idx) { - this.colName = colName; + public FloatReadJdbcQuery(int idx) { this.index = idx; } @@ -35,6 +33,6 @@ public Object executeGeneric(VirtualFrame frame) { public final float executeFloat(VirtualFrame frame) { Object[] args = frame.getArguments(); JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getFloat(index, colName, this); + return rs.getFloat(index, this); } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/IntReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/IntReadJdbcQuery.java index 451500c4f..e45e76b08 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/IntReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/IntReadJdbcQuery.java @@ -20,10 +20,8 @@ public class IntReadJdbcQuery extends ExpressionNode { private final int index; - private final String colName; - public IntReadJdbcQuery(String colname, int idx) { - this.colName = colname; + public IntReadJdbcQuery(int idx) { this.index = idx; } @@ -35,6 +33,6 @@ public Object executeGeneric(VirtualFrame frame) { public final int executeInt(VirtualFrame frame) { Object[] args = frame.getArguments(); JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getInt(index, colName, this); + return rs.getInt(index, this); } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/JdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/JdbcQuery.java index 8015e64d2..140ddfa44 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/JdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/JdbcQuery.java @@ -92,11 +92,11 @@ byte getByte(String colName, Node node) { } @TruffleBoundary - byte getByte(int idx, String colName, Node node) { + byte getByte(int idx, Node node) { try { return rs.getByte(idx); } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); + throw exceptionHandler.columnParseError(e, idx, rs, node); } } @@ -110,11 +110,11 @@ short getShort(String colName, Node node) { } @TruffleBoundary - short getShort(int idx, String colName, Node node) { + short getShort(int idx, Node node) { try { return rs.getShort(idx); } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); + throw exceptionHandler.columnParseError(e, idx, rs, node); } } @@ -128,11 +128,11 @@ int getInt(String colName, Node node) { } @TruffleBoundary - int getInt(int idx, String colName, Node node) { + int getInt(int idx, Node node) { try { return rs.getInt(idx); } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); + throw exceptionHandler.columnParseError(e, idx, rs, node); } } @@ -146,11 +146,11 @@ long getLong(String colName, Node node) { } @TruffleBoundary - long getLong(int idx, String colName, Node node) { + long getLong(int idx, Node node) { try { return rs.getLong(idx); } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); + throw exceptionHandler.columnParseError(e, idx, rs, node); } } @@ -164,11 +164,11 @@ float getFloat(String colName, Node node) { } @TruffleBoundary - float getFloat(int idx, String colName, Node node) { + float getFloat(int idx, Node node) { try { return rs.getFloat(idx); } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); + throw exceptionHandler.columnParseError(e, idx, rs, node); } } @@ -182,11 +182,11 @@ float getFloat(int idx, String colName, Node node) { } @TruffleBoundary - double getDouble(int idx, String colName, Node node) { + double getDouble(int idx, Node node) { try { return rs.getDouble(idx); } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); + throw exceptionHandler.columnParseError(e, idx, rs, node); } } @@ -200,11 +200,11 @@ DecimalObject getDecimal(String colName, Node node) { } @TruffleBoundary - DecimalObject getDecimal(int idx, String colName, Node node) { + DecimalObject getDecimal(int idx, Node node) { try { return new DecimalObject(rs.getBigDecimal(idx)); } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); + throw exceptionHandler.columnParseError(e, idx, rs, node); } } @@ -218,11 +218,11 @@ String getString(String colName, Node node) { } @TruffleBoundary - String getString(int idx, String colName, Node node) { + String getString(int idx, Node node) { try { return rs.getString(idx); } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); + throw exceptionHandler.columnParseError(e, idx, rs, node); } } @@ -236,11 +236,11 @@ boolean getBool(String colName, Node node) { } @TruffleBoundary - boolean getBool(int idx, String colName, Node node) { + boolean getBool(int idx, Node node) { try { return rs.getBoolean(idx); } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); + throw exceptionHandler.columnParseError(e, idx, rs, node); } } @@ -255,12 +255,12 @@ DateObject getDate(String colName, Node node) { } @TruffleBoundary - DateObject getDate(int idx, String colName, Node node) { + DateObject getDate(int idx, Node node) { try { java.sql.Date sqlDate = rs.getDate(idx); return new DateObject(sqlDate.toLocalDate()); } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); + throw exceptionHandler.columnParseError(e, idx, rs, node); } } @@ -275,12 +275,12 @@ TimeObject getTime(String colName, Node node) { } @TruffleBoundary - TimeObject getTime(int idx, String colName, Node node) { + TimeObject getTime(int idx, Node node) { try { java.sql.Time sqlTime = rs.getTime(idx); return new TimeObject(sqlTime.toLocalTime()); } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); + throw exceptionHandler.columnParseError(e, idx, rs, node); } } @@ -295,12 +295,12 @@ TimestampObject getTimestamp(String colName, Node node) { } @TruffleBoundary - TimestampObject getTimestamp(int idx, String colName, Node node) { + TimestampObject getTimestamp(int idx, Node node) { try { java.sql.Timestamp sqlTimestamp = rs.getTimestamp(idx); return new TimestampObject(sqlTimestamp.toLocalDateTime()); } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); + throw exceptionHandler.columnParseError(e, idx, rs, node); } } @@ -314,11 +314,11 @@ byte[] getBytes(String colName, Node node) { } @TruffleBoundary - byte[] getBytes(int idx, String colName, Node node) { + byte[] getBytes(int idx, Node node) { try { return rs.getBytes(idx); } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); + throw exceptionHandler.columnParseError(e, idx, rs, node); } } @@ -333,12 +333,12 @@ boolean isNull(String colName, Node node) { } @TruffleBoundary - boolean isNull(int idx, String colName, Node node) { + boolean isNull(int idx, Node node) { try { rs.getObject(idx); return rs.wasNull(); } catch (SQLException e) { - throw exceptionHandler.columnParseError(e, colName, node); + throw exceptionHandler.columnParseError(e, idx, rs, node); } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/LongReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/LongReadJdbcQuery.java index a59d3c8e7..7e083ed29 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/LongReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/LongReadJdbcQuery.java @@ -20,10 +20,8 @@ public class LongReadJdbcQuery extends ExpressionNode { private final int index; - private final String colName; - public LongReadJdbcQuery(String colName, int idx) { - this.colName = colName; + public LongReadJdbcQuery(int idx) { this.index = idx; } @@ -35,6 +33,6 @@ public Object executeGeneric(VirtualFrame frame) { public final long executeLong(VirtualFrame frame) { Object[] args = frame.getArguments(); JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getLong(index, colName, this); + return rs.getLong(index, this); } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/NullableReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/NullableReadJdbcQuery.java index de2e9cd4d..4e58b8f25 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/NullableReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/NullableReadJdbcQuery.java @@ -24,18 +24,16 @@ public class NullableReadJdbcQuery extends ExpressionNode { @Child private DirectCallNode innerParse; private final int index; - private final String colName; - public NullableReadJdbcQuery(ProgramExpressionNode innerParse, String colName, int idx) { + public NullableReadJdbcQuery(ProgramExpressionNode innerParse, int idx) { this.innerParse = DirectCallNode.create(innerParse.getCallTarget()); - this.colName = colName; this.index = idx; } public Object executeGeneric(VirtualFrame frame) { Object[] args = frame.getArguments(); JdbcQuery rs = (JdbcQuery) args[0]; - if (rs.isNull(index, colName, this)) return NullObject.INSTANCE; - else return innerParse.call(rs, index, colName); + if (rs.isNull(index, this)) return NullObject.INSTANCE; + else return innerParse.call(rs, index); } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/ShortReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/ShortReadJdbcQuery.java index f25995195..0f9225a66 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/ShortReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/ShortReadJdbcQuery.java @@ -19,11 +19,9 @@ @NodeInfo(shortName = "Jdbc.ShortRead") public class ShortReadJdbcQuery extends ExpressionNode { - private final String colName; private final int index; - public ShortReadJdbcQuery(String colName, int idx) { - this.colName = colName; + public ShortReadJdbcQuery(int idx) { this.index = idx; } @@ -35,6 +33,6 @@ public Object executeGeneric(VirtualFrame frame) { public final short executeShort(VirtualFrame frame) { Object[] args = frame.getArguments(); JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getShort(index, colName, this); + return rs.getShort(index, this); } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/StringReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/StringReadJdbcQuery.java index 1bd6a3498..94e949849 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/StringReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/StringReadJdbcQuery.java @@ -19,11 +19,9 @@ @NodeInfo(shortName = "StringReadJdbcQuery") public class StringReadJdbcQuery extends ExpressionNode { - private final String colName; private final int index; - public StringReadJdbcQuery(String colName, int idx) { - this.colName = colName; + public StringReadJdbcQuery(int idx) { this.index = idx; } @@ -35,6 +33,6 @@ public Object executeGeneric(VirtualFrame frame) { public final String executeString(VirtualFrame frame) { Object[] args = frame.getArguments(); JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getString(index, colName, this); + return rs.getString(index, this); } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TimeReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TimeReadJdbcQuery.java index 9d3163d51..14099d56a 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TimeReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TimeReadJdbcQuery.java @@ -20,11 +20,9 @@ @NodeInfo(shortName = "Time.DateRead") public class TimeReadJdbcQuery extends ExpressionNode { - private final String colName; private final int index; - public TimeReadJdbcQuery(String colName, int idx) { - this.colName = colName; + public TimeReadJdbcQuery(int idx) { this.index = idx; } @@ -36,6 +34,6 @@ public Object executeGeneric(VirtualFrame frame) { public final TimeObject executeTime(VirtualFrame frame) { Object[] args = frame.getArguments(); JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getTime(index, colName, this); + return rs.getTime(index, this); } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TimestampReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TimestampReadJdbcQuery.java index 6925f3fc6..5017fdd4b 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TimestampReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TimestampReadJdbcQuery.java @@ -20,11 +20,9 @@ @NodeInfo(shortName = "Timestamp.DateRead") public class TimestampReadJdbcQuery extends ExpressionNode { - private final String colName; private final int index; - public TimestampReadJdbcQuery(String colName, int idx) { - this.colName = colName; + public TimestampReadJdbcQuery(int idx) { this.index = idx; } @@ -36,6 +34,6 @@ public Object executeGeneric(VirtualFrame frame) { public final TimestampObject executeTimestamp(VirtualFrame frame) { Object[] args = frame.getArguments(); JdbcQuery rs = (JdbcQuery) args[0]; - return rs.getTimestamp(index, colName, this); + return rs.getTimestamp(index, this); } } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TryableReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TryableReadJdbcQuery.java index 9e29a6f90..398c6fe6c 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TryableReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/TryableReadJdbcQuery.java @@ -24,12 +24,10 @@ public class TryableReadJdbcQuery extends ExpressionNode { @Child private DirectCallNode innerParse; - private final String colName; private final int index; - public TryableReadJdbcQuery(ProgramExpressionNode innerParse, String colName, int idx) { + public TryableReadJdbcQuery(ProgramExpressionNode innerParse, int idx) { this.innerParse = DirectCallNode.create(innerParse.getCallTarget()); - this.colName = colName; this.index = idx; } @@ -37,7 +35,7 @@ public Object executeGeneric(VirtualFrame frame) { Object[] args = frame.getArguments(); JdbcQuery rs = (JdbcQuery) args[0]; try { - return innerParse.call(rs, index, colName); + return innerParse.call(rs, index); } catch (RawTruffleRuntimeException e) { return new ErrorObject(e.getMessage()); } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/runtime/exceptions/rdbms/JdbcExceptionHandler.java b/snapi-truffle/src/main/java/raw/runtime/truffle/runtime/exceptions/rdbms/JdbcExceptionHandler.java index d1ebe7349..386edd4c8 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/runtime/exceptions/rdbms/JdbcExceptionHandler.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/runtime/exceptions/rdbms/JdbcExceptionHandler.java @@ -13,6 +13,7 @@ package raw.runtime.truffle.runtime.exceptions.rdbms; import com.oracle.truffle.api.nodes.Node; +import java.sql.ResultSet; import java.sql.SQLException; import raw.runtime.truffle.ast.io.jdbc.JdbcQuery; import raw.runtime.truffle.runtime.exceptions.RawTruffleRuntimeException; @@ -23,6 +24,16 @@ public RawTruffleRuntimeException rewrite(SQLException e, JdbcQuery rs, Node loc return new JdbcReaderRawTruffleException(e.getMessage(), rs, e, location); } + public RawTruffleRuntimeException columnParseError( + SQLException e, int colIndex, ResultSet rs, Node location) { + try { + String colName = rs.getMetaData().getColumnName(colIndex); + return columnParseError(e, colName, location); + } catch (SQLException e2) { + return new JdbcParserRawTruffleException(e2.getMessage(), e2, location); + } + } + public RawTruffleRuntimeException columnParseError( SQLException e, String colName, Node location) { return new JdbcParserRawTruffleException( From 643a9b0c6ae388ec5f5d2b2ecd8c56a7a1ccf4de Mon Sep 17 00:00:00 2001 From: torcato Date: Mon, 29 Apr 2024 17:53:38 +0200 Subject: [PATCH 06/17] Fixing +1 issue with sql resultset --- .../truffle/ast/io/jdbc/JdbcQuery.java | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/JdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/JdbcQuery.java index 140ddfa44..9ff9c5a86 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/JdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/JdbcQuery.java @@ -94,7 +94,7 @@ byte getByte(String colName, Node node) { @TruffleBoundary byte getByte(int idx, Node node) { try { - return rs.getByte(idx); + return rs.getByte(idx + 1); } catch (SQLException e) { throw exceptionHandler.columnParseError(e, idx, rs, node); } @@ -112,7 +112,7 @@ short getShort(String colName, Node node) { @TruffleBoundary short getShort(int idx, Node node) { try { - return rs.getShort(idx); + return rs.getShort(idx + 1); } catch (SQLException e) { throw exceptionHandler.columnParseError(e, idx, rs, node); } @@ -130,7 +130,7 @@ int getInt(String colName, Node node) { @TruffleBoundary int getInt(int idx, Node node) { try { - return rs.getInt(idx); + return rs.getInt(idx + 1); } catch (SQLException e) { throw exceptionHandler.columnParseError(e, idx, rs, node); } @@ -148,7 +148,7 @@ long getLong(String colName, Node node) { @TruffleBoundary long getLong(int idx, Node node) { try { - return rs.getLong(idx); + return rs.getLong(idx + 1); } catch (SQLException e) { throw exceptionHandler.columnParseError(e, idx, rs, node); } @@ -166,7 +166,7 @@ float getFloat(String colName, Node node) { @TruffleBoundary float getFloat(int idx, Node node) { try { - return rs.getFloat(idx); + return rs.getFloat(idx + 1); } catch (SQLException e) { throw exceptionHandler.columnParseError(e, idx, rs, node); } @@ -184,7 +184,7 @@ float getFloat(int idx, Node node) { @TruffleBoundary double getDouble(int idx, Node node) { try { - return rs.getDouble(idx); + return rs.getDouble(idx + 1); } catch (SQLException e) { throw exceptionHandler.columnParseError(e, idx, rs, node); } @@ -202,7 +202,7 @@ DecimalObject getDecimal(String colName, Node node) { @TruffleBoundary DecimalObject getDecimal(int idx, Node node) { try { - return new DecimalObject(rs.getBigDecimal(idx)); + return new DecimalObject(rs.getBigDecimal(idx + 1)); } catch (SQLException e) { throw exceptionHandler.columnParseError(e, idx, rs, node); } @@ -220,7 +220,7 @@ String getString(String colName, Node node) { @TruffleBoundary String getString(int idx, Node node) { try { - return rs.getString(idx); + return rs.getString(idx + 1); } catch (SQLException e) { throw exceptionHandler.columnParseError(e, idx, rs, node); } @@ -238,7 +238,7 @@ boolean getBool(String colName, Node node) { @TruffleBoundary boolean getBool(int idx, Node node) { try { - return rs.getBoolean(idx); + return rs.getBoolean(idx + 1); } catch (SQLException e) { throw exceptionHandler.columnParseError(e, idx, rs, node); } @@ -257,7 +257,7 @@ DateObject getDate(String colName, Node node) { @TruffleBoundary DateObject getDate(int idx, Node node) { try { - java.sql.Date sqlDate = rs.getDate(idx); + java.sql.Date sqlDate = rs.getDate(idx + 1); return new DateObject(sqlDate.toLocalDate()); } catch (SQLException e) { throw exceptionHandler.columnParseError(e, idx, rs, node); @@ -277,7 +277,7 @@ TimeObject getTime(String colName, Node node) { @TruffleBoundary TimeObject getTime(int idx, Node node) { try { - java.sql.Time sqlTime = rs.getTime(idx); + java.sql.Time sqlTime = rs.getTime(idx + 1); return new TimeObject(sqlTime.toLocalTime()); } catch (SQLException e) { throw exceptionHandler.columnParseError(e, idx, rs, node); @@ -297,7 +297,7 @@ TimestampObject getTimestamp(String colName, Node node) { @TruffleBoundary TimestampObject getTimestamp(int idx, Node node) { try { - java.sql.Timestamp sqlTimestamp = rs.getTimestamp(idx); + java.sql.Timestamp sqlTimestamp = rs.getTimestamp(idx + 1); return new TimestampObject(sqlTimestamp.toLocalDateTime()); } catch (SQLException e) { throw exceptionHandler.columnParseError(e, idx, rs, node); @@ -316,7 +316,7 @@ byte[] getBytes(String colName, Node node) { @TruffleBoundary byte[] getBytes(int idx, Node node) { try { - return rs.getBytes(idx); + return rs.getBytes(idx + 1); } catch (SQLException e) { throw exceptionHandler.columnParseError(e, idx, rs, node); } @@ -335,7 +335,7 @@ boolean isNull(String colName, Node node) { @TruffleBoundary boolean isNull(int idx, Node node) { try { - rs.getObject(idx); + rs.getObject(idx + 1); return rs.wasNull(); } catch (SQLException e) { throw exceptionHandler.columnParseError(e, idx, rs, node); From 2bd1a5c42fe515a2d55b0185fb9ef24cc63ee987 Mon Sep 17 00:00:00 2001 From: torcato Date: Mon, 29 Apr 2024 17:54:10 +0200 Subject: [PATCH 07/17] Adding test for the issue --- .../truffle/regressions/RD10855Test.scala | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala diff --git a/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala b/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala new file mode 100644 index 000000000..426f59513 --- /dev/null +++ b/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala @@ -0,0 +1,36 @@ +/* + * Copyright 2024 RAW Labs S.A. + * + * Use of this software is governed by the Business Source License + * included in the file licenses/BSL.txt. + * + * As of the Change Date specified in that file, in accordance with + * the Business Source License, use of this software will be governed + * by the Apache License, Version 2.0, included in the file + * licenses/APL.txt. + */ + +package raw.compiler.rql2.tests.truffle.regressions + +import raw.compiler.rql2.truffle.TruffleCompilerTestContext +import raw.creds.jdbc.RDBMSTestCreds + +class RD10855Test extends TruffleCompilerTestContext with RDBMSTestCreds { + + val pgSchema = "rdbmstest" + val pgTable = "tbl1" + + test( + s"""PostgreSQL.InferAndRead("${pgsqlCreds.database}", "$pgSchema", "$pgTable", + | host = "${pgsqlCreds.host}", username = "${pgsqlCreds.username.get}", password = "${pgsqlCreds.password.get}")""".stripMargin + ) { it => + it should evaluateTo( + """[ + | {a: 1, b: 1, c: 1.5, d: 1.5, x: "x1", y: "y1"}, + | {a: 2, b: 2, c: 2.2, d: 2.2, x: "x2", y: "y2"}, + | {a: 3, b: null, c: 3.3, d: null, x: "x3", y: null} + |]""".stripMargin + ) + } + +} From bc01b32d5822fe76c744a6ab4d0e220f7156ffbc Mon Sep 17 00:00:00 2001 From: torcato Date: Tue, 30 Apr 2024 14:16:13 +0200 Subject: [PATCH 08/17] wip --- .../tests/truffle/regressions/RD10855Test.scala | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala b/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala index 426f59513..d14896eed 100644 --- a/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala +++ b/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala @@ -33,4 +33,17 @@ class RD10855Test extends TruffleCompilerTestContext with RDBMSTestCreds { ) } + test( + s"""PostgreSQL.Read("${pgsqlCreds.database}", "$pgSchema", "$pgTable", type collection(record(a: int, b: int, c: double, d: double, x: string, y: string)), + | host = "${pgsqlCreds.host}", username = "${pgsqlCreds.username.get}", password = "${pgsqlCreds.password.get}")""".stripMargin + ) { it => + it should evaluateTo( + """[ + | {a: 1, b: 1, c: 1.5, d: 1.5, x: "x1", y: "y1"}, + | {a: 2, b: 2, c: 2.2, d: 2.2, x: "x2", y: "y2"}, + | {a: 3, b: null, c: 3.3, d: null, x: "x3", y: null} + |]""".stripMargin + ) + } + } From c9d2ed7704e70560f4e2ac534064faa1af61cebb Mon Sep 17 00:00:00 2001 From: torcato Date: Tue, 30 Apr 2024 15:13:54 +0200 Subject: [PATCH 09/17] Adding undefined support for jdbc --- .../truffle/regressions/RD10855Test.scala | 51 +++++++++++++++++++ .../builtin/SqlTableExtensionHelper.scala | 1 + .../inferrer/local/jdbc/JdbcInferrer.scala | 19 ++----- .../local/jdbc/JdbcTypeToSourceType.scala | 23 ++++----- .../snapi/truffle/builtin/jdbc/Jdbc.java | 3 +- .../ast/io/jdbc/UndefinedReadJdbcQuery.java | 29 +++++++++++ 6 files changed, 98 insertions(+), 28 deletions(-) create mode 100644 snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/UndefinedReadJdbcQuery.java diff --git a/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala b/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala index d14896eed..213bba46c 100644 --- a/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala +++ b/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala @@ -46,4 +46,55 @@ class RD10855Test extends TruffleCompilerTestContext with RDBMSTestCreds { ) } +// table skippable_types has unsupported types +// Column | Type +// --------+-----------------------+- +// a | integer | +// b | character varying(10) | +// c | smallint | +// d | bigint | +// e | numeric(20,4) | +// f | macaddr | +// g | cidr | + test( + s"""PostgreSQL.Read( + | "${pgsqlCreds.database}", + | "$pgSchema", + | "skippable_types", + | type collection(record( + | a: int, + | b: string, + | c: int, + | d: long, + | e: double, + | f: undefined, + | g: undefined)), + | host = "${pgsqlCreds.host}", + | username = "${pgsqlCreds.username.get}", + | password = "${pgsqlCreds.password.get}" + |)""".stripMargin + ) { it => + it should evaluateTo( + """[ + | {a: 1, b: "hello", c: 2, d: 3, e: 4.4, f: null, g: null} + |]""".stripMargin + ) + } + + test( + s"""PostgreSQL.InferAndRead( + | "${pgsqlCreds.database}", + | "$pgSchema", + | "skippable_types", + | host = "${pgsqlCreds.host}", + | username = "${pgsqlCreds.username.get}", + | password = "${pgsqlCreds.password.get}" + |)""".stripMargin + ) { it => + it should evaluateTo( + """[ + | {a: 1, b: "hello", c: 2, d: 3, e: Decimal.From(4.4), f: null, g: null} + |]""".stripMargin + ) + } } diff --git a/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/SqlTableExtensionHelper.scala b/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/SqlTableExtensionHelper.scala index ee540dea0..c3c1278f8 100644 --- a/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/SqlTableExtensionHelper.scala +++ b/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/SqlTableExtensionHelper.scala @@ -45,6 +45,7 @@ trait SqlTableExtensionHelper extends EntryExtensionHelper { case _: Rql2TimeType => Right(x) case _: Rql2TimestampType => Right(x) case _: Rql2BinaryType => Right(x) + case _: Rql2UndefinedType => Right(x) case _ => Left(Seq(UnsupportedType(x.tipe, x.tipe, None))) } } diff --git a/snapi-frontend/src/main/scala/raw/inferrer/local/jdbc/JdbcInferrer.scala b/snapi-frontend/src/main/scala/raw/inferrer/local/jdbc/JdbcInferrer.scala index 1417c9adb..f3bae2f3d 100644 --- a/snapi-frontend/src/main/scala/raw/inferrer/local/jdbc/JdbcInferrer.scala +++ b/snapi-frontend/src/main/scala/raw/inferrer/local/jdbc/JdbcInferrer.scala @@ -13,9 +13,8 @@ package raw.inferrer.local.jdbc import java.sql.ResultSetMetaData - import com.typesafe.scalalogging.StrictLogging -import raw.inferrer.api.{SourceAttrType, SourceCollectionType, SourceRecordType, SourceType} +import raw.inferrer.api.{SourceAttrType, SourceCollectionType, SourceNullType, SourceRecordType, SourceType} import raw.sources.jdbc.api.{JdbcLocation, JdbcTableLocation} import scala.collection.mutable @@ -44,26 +43,16 @@ class JdbcInferrer extends JdbcTypeToSourceType with StrictLogging { } private def getTypeFromResultSetMetadata(res: ResultSetMetaData): SourceType = { - val columns = mutable.ListBuffer[SourceAttrType]() - - val incompatible = mutable.HashMap[String, String]() - (1 to res.getColumnCount).foreach { n => + val columns = (1 to res.getColumnCount).map { n => val columnName = res.getColumnName(n) val columnType = res.getColumnType(n) val nullability = res.isNullable(n) jdbcColumnToSourceType(columnType, nullability) match { - case Some(t) => columns += SourceAttrType(columnName, t) - case None => incompatible(columnName) = res.getColumnTypeName(n) + case Some(t) => SourceAttrType(columnName, t) + case None => SourceAttrType(columnName, SourceNullType()) } } - - // We are removing from the data incompatible types. It would be nice to show warning to the user - if (incompatible.nonEmpty) { - val errorColumns = incompatible.toSeq.map { case (name, typeName) => s"$name: $typeName" }.mkString(", ") - logger.warn(s"columns have unsupported types: $errorColumns") - } - SourceCollectionType(SourceRecordType(columns.to, nullable = false), nullable = false) } diff --git a/snapi-frontend/src/main/scala/raw/inferrer/local/jdbc/JdbcTypeToSourceType.scala b/snapi-frontend/src/main/scala/raw/inferrer/local/jdbc/JdbcTypeToSourceType.scala index b0dc2dd0b..b0118178b 100644 --- a/snapi-frontend/src/main/scala/raw/inferrer/local/jdbc/JdbcTypeToSourceType.scala +++ b/snapi-frontend/src/main/scala/raw/inferrer/local/jdbc/JdbcTypeToSourceType.scala @@ -26,6 +26,7 @@ import raw.inferrer.api.{ SourceIntType, SourceIntervalType, SourceLongType, + SourceNullType, SourceRecordType, SourceShortType, SourceStringType, @@ -71,23 +72,21 @@ trait JdbcTypeToSourceType { protected def tableMetadataToSourceType(tableMetadata: TableMetadata): SourceType = { val TableMetadata(tableColumns, _) = tableMetadata - val columns = mutable.ListBuffer[SourceAttrType]() - val unsupportedColumns = mutable.ListBuffer[String]() - tableColumns.foreach { + + val columns: Vector[SourceAttrType] = tableColumns.map { case TableColumn(columnName, columnType) => columnType match { case JdbcColumnType(jdbcType, jdbcNullability) => jdbcColumnToSourceType(jdbcType, jdbcNullability) match { - case Some(t) => columns += SourceAttrType(columnName, t) - case None => unsupportedColumns.append(columnName) + case Some(t) => SourceAttrType(columnName, t) + case None => SourceAttrType(columnName, SourceNullType()) } - case NativeIntervalType(nullable) => columns += SourceAttrType(columnName, SourceIntervalType(nullable)) - case UnsupportedColumnType => unsupportedColumns.append(columnName) + // This UnsupportedColumnType is just used in this match, how do we get one? + case NativeIntervalType(nullable) => SourceAttrType(columnName, SourceIntervalType(nullable)) + case UnsupportedColumnType => SourceAttrType(columnName, SourceNullType()) } - } - - if (tableColumns.isEmpty && unsupportedColumns.isEmpty) throw new InferrerException("could not find table") - else if (columns.isEmpty) throw new InferrerException("table has only unsupported column types") + }.toVector - SourceCollectionType(SourceRecordType(columns.to, nullable = false), nullable = false) + if (tableColumns.isEmpty) throw new InferrerException("could not find table") + SourceCollectionType(SourceRecordType(columns, nullable = false), nullable = false) } } diff --git a/snapi-truffle/src/main/java/raw/compiler/snapi/truffle/builtin/jdbc/Jdbc.java b/snapi-truffle/src/main/java/raw/compiler/snapi/truffle/builtin/jdbc/Jdbc.java index 8c48afd0b..5148622a8 100644 --- a/snapi-truffle/src/main/java/raw/compiler/snapi/truffle/builtin/jdbc/Jdbc.java +++ b/snapi-truffle/src/main/java/raw/compiler/snapi/truffle/builtin/jdbc/Jdbc.java @@ -88,7 +88,8 @@ static private ProgramExpressionNode columnReader(int index, Type t, RawLanguage case Rql2TimeType ignored -> new TimeReadJdbcQuery( index); case Rql2TimestampType ignored -> new TimestampReadJdbcQuery( index); case Rql2BoolType ignored -> new BoolReadJdbcQuery( index); - case Rql2BinaryType ignored -> new BinaryReadJdbcQuery( index); + case Rql2BinaryType ignored -> new BinaryReadJdbcQuery(index); + case Rql2UndefinedType ignored -> new UndefinedReadJdbcQuery( index); default -> throw new RawTruffleInternalErrorException(); }; return new ProgramExpressionNode(lang, frameDescriptor, node); diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/UndefinedReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/UndefinedReadJdbcQuery.java new file mode 100644 index 000000000..749661a22 --- /dev/null +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/UndefinedReadJdbcQuery.java @@ -0,0 +1,29 @@ +/* + * Copyright 2024 RAW Labs S.A. + * + * Use of this software is governed by the Business Source License + * included in the file licenses/BSL.txt. + * + * As of the Change Date specified in that file, in accordance with + * the Business Source License, use of this software will be governed + * by the Apache License, Version 2.0, included in the file + * licenses/APL.txt. + */ + +package raw.runtime.truffle.ast.io.jdbc; + +import com.oracle.truffle.api.frame.VirtualFrame; +import raw.runtime.truffle.ExpressionNode; +import raw.runtime.truffle.runtime.primitives.NullObject; + +public class UndefinedReadJdbcQuery extends ExpressionNode { + private final int index; + + public UndefinedReadJdbcQuery(int idx) { + this.index = idx; + } + + public Object executeGeneric(VirtualFrame frame) { + return NullObject.INSTANCE; + } +} From d7fa390a7a066296f4ff1ecbb42b8e151eea67de Mon Sep 17 00:00:00 2001 From: torcato Date: Tue, 30 Apr 2024 16:23:07 +0200 Subject: [PATCH 10/17] wip --- .../truffle/regressions/RD10855Test.scala | 26 ------------------- .../inferrer/local/jdbc/JdbcInferrer.scala | 4 ++- .../truffle/ast/io/jdbc/JdbcQuery.java | 9 +++++++ .../ast/io/jdbc/UndefinedReadJdbcQuery.java | 7 +++-- 4 files changed, 17 insertions(+), 29 deletions(-) diff --git a/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala b/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala index 213bba46c..a4a8f0e58 100644 --- a/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala +++ b/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala @@ -20,32 +20,6 @@ class RD10855Test extends TruffleCompilerTestContext with RDBMSTestCreds { val pgSchema = "rdbmstest" val pgTable = "tbl1" - test( - s"""PostgreSQL.InferAndRead("${pgsqlCreds.database}", "$pgSchema", "$pgTable", - | host = "${pgsqlCreds.host}", username = "${pgsqlCreds.username.get}", password = "${pgsqlCreds.password.get}")""".stripMargin - ) { it => - it should evaluateTo( - """[ - | {a: 1, b: 1, c: 1.5, d: 1.5, x: "x1", y: "y1"}, - | {a: 2, b: 2, c: 2.2, d: 2.2, x: "x2", y: "y2"}, - | {a: 3, b: null, c: 3.3, d: null, x: "x3", y: null} - |]""".stripMargin - ) - } - - test( - s"""PostgreSQL.Read("${pgsqlCreds.database}", "$pgSchema", "$pgTable", type collection(record(a: int, b: int, c: double, d: double, x: string, y: string)), - | host = "${pgsqlCreds.host}", username = "${pgsqlCreds.username.get}", password = "${pgsqlCreds.password.get}")""".stripMargin - ) { it => - it should evaluateTo( - """[ - | {a: 1, b: 1, c: 1.5, d: 1.5, x: "x1", y: "y1"}, - | {a: 2, b: 2, c: 2.2, d: 2.2, x: "x2", y: "y2"}, - | {a: 3, b: null, c: 3.3, d: null, x: "x3", y: null} - |]""".stripMargin - ) - } - // table skippable_types has unsupported types // Column | Type // --------+-----------------------+- diff --git a/snapi-frontend/src/main/scala/raw/inferrer/local/jdbc/JdbcInferrer.scala b/snapi-frontend/src/main/scala/raw/inferrer/local/jdbc/JdbcInferrer.scala index f3bae2f3d..e3191a861 100644 --- a/snapi-frontend/src/main/scala/raw/inferrer/local/jdbc/JdbcInferrer.scala +++ b/snapi-frontend/src/main/scala/raw/inferrer/local/jdbc/JdbcInferrer.scala @@ -50,7 +50,9 @@ class JdbcInferrer extends JdbcTypeToSourceType with StrictLogging { val nullability = res.isNullable(n) jdbcColumnToSourceType(columnType, nullability) match { case Some(t) => SourceAttrType(columnName, t) - case None => SourceAttrType(columnName, SourceNullType()) + case None => + logger.warn(s"Unsupported column type $columnType for column $columnName") + SourceAttrType(columnName, SourceNullType()) } } SourceCollectionType(SourceRecordType(columns.to, nullable = false), nullable = false) diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/JdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/JdbcQuery.java index 9ff9c5a86..0406bfce8 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/JdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/JdbcQuery.java @@ -36,6 +36,15 @@ public class JdbcQuery { private final JdbcExceptionHandler exceptionHandler; private final String url; + @TruffleBoundary + public String getDatabaseType(int index, Node node) { + try { + return rs.getMetaData().getColumnTypeName(index + 1); + } catch (SQLException e) { + throw exceptionHandler.columnParseError(e, index, rs, node); + } + } + @TruffleBoundary public JdbcQuery( LocationDescription locationDescription, diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/UndefinedReadJdbcQuery.java b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/UndefinedReadJdbcQuery.java index 749661a22..184b0211e 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/UndefinedReadJdbcQuery.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/ast/io/jdbc/UndefinedReadJdbcQuery.java @@ -14,7 +14,7 @@ import com.oracle.truffle.api.frame.VirtualFrame; import raw.runtime.truffle.ExpressionNode; -import raw.runtime.truffle.runtime.primitives.NullObject; +import raw.runtime.truffle.runtime.primitives.ErrorObject; public class UndefinedReadJdbcQuery extends ExpressionNode { private final int index; @@ -24,6 +24,9 @@ public UndefinedReadJdbcQuery(int idx) { } public Object executeGeneric(VirtualFrame frame) { - return NullObject.INSTANCE; + Object[] args = frame.getArguments(); + JdbcQuery rs = (JdbcQuery) args[0]; + return new ErrorObject( + String.format("skipping column of type %s", rs.getDatabaseType(index, this))); } } From 31f9b21fa2ff836bdea7cf65031269f40d910990 Mon Sep 17 00:00:00 2001 From: torcato Date: Tue, 30 Apr 2024 17:28:35 +0200 Subject: [PATCH 11/17] wip --- .../truffle/regressions/RD10855Test.scala | 78 ++++++++++--------- 1 file changed, 42 insertions(+), 36 deletions(-) diff --git a/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala b/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala index a4a8f0e58..e6d99a38c 100644 --- a/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala +++ b/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala @@ -17,53 +17,39 @@ import raw.creds.jdbc.RDBMSTestCreds class RD10855Test extends TruffleCompilerTestContext with RDBMSTestCreds { - val pgSchema = "rdbmstest" - val pgTable = "tbl1" - -// table skippable_types has unsupported types -// Column | Type -// --------+-----------------------+- -// a | integer | -// b | character varying(10) | -// c | smallint | -// d | bigint | -// e | numeric(20,4) | -// f | macaddr | -// g | cidr | + // The column y is of type varchar, but we are setting to undefined + // when the value is not null it shows the error "skipping column of type ..." test( s"""PostgreSQL.Read( - | "${pgsqlCreds.database}", - | "$pgSchema", - | "skippable_types", - | type collection(record( - | a: int, - | b: string, - | c: int, - | d: long, - | e: double, - | f: undefined, - | g: undefined)), - | host = "${pgsqlCreds.host}", - | username = "${pgsqlCreds.username.get}", - | password = "${pgsqlCreds.password.get}" - |)""".stripMargin + | "${pgsqlCreds.database}", + | "rdbmstest", + | "tbl1", + | type collection(record(a: int, b: int, c: double, d: double, x: string, y: undefined)), + | host = "${pgsqlCreds.host}", + | username = "${pgsqlCreds.username.get}", + | password = "${pgsqlCreds.password.get}" + |)""".stripMargin ) { it => it should evaluateTo( """[ - | {a: 1, b: "hello", c: 2, d: 3, e: 4.4, f: null, g: null} + | {a: 1, b: 1, c: 1.5, d: 1.5, x: "x1", y: "skipping column of type varchar"}, + | {a: 2, b: 2, c: 2.2, d: 2.2, x: "x2", y: "skipping column of type varchar"}, + | {a: 3, b: null, c: 3.3, d: null, x: "x3", y: null} |]""".stripMargin ) } + // table skippable_types has unsupported types, field f: macaddr, field g: cidr + // but are null so no errors will be shown test( s"""PostgreSQL.InferAndRead( - | "${pgsqlCreds.database}", - | "$pgSchema", - | "skippable_types", - | host = "${pgsqlCreds.host}", - | username = "${pgsqlCreds.username.get}", - | password = "${pgsqlCreds.password.get}" - |)""".stripMargin + | "${pgsqlCreds.database}", + | "rdbmstest", + | "skippable_types", + | host = "${pgsqlCreds.host}", + | username = "${pgsqlCreds.username.get}", + | password = "${pgsqlCreds.password.get}" + |)""".stripMargin ) { it => it should evaluateTo( """[ @@ -71,4 +57,24 @@ class RD10855Test extends TruffleCompilerTestContext with RDBMSTestCreds { |]""".stripMargin ) } + + // Retrieving columns with repeated names + val ttt = "\"\"\"" + test(s"""PostgreSQL.InferAndQuery( + | "${pgsqlCreds.database}", + | ${ttt}SELECT t1.a, t2.a, t1.b, t2.x as b + | FROM rdbmstest.tbl1 t1, rdbmstest.tbl1 t2 + | WHERE t1.a = t2.a $ttt, + | host = "${pgsqlCreds.host}", + | username = "${pgsqlCreds.username.get}", + | password = "${pgsqlCreds.password.get}" + |)""".stripMargin) { it => + it should evaluateTo( + """[ + | {a: 1, a: 1, b: 1, b: "x1"}, + | {a: 2, a: 2, b: 2, b: "x2"}, + | {a: 3, a: 3, b: null, b: "x3"} + |]""".stripMargin + ) + } } From e4ffc60088fa557cae0ecf001af97de9f2ce9eb0 Mon Sep 17 00:00:00 2001 From: torcato Date: Wed, 1 May 2024 09:17:03 +0200 Subject: [PATCH 12/17] wip --- .../truffle/regressions/RD10855Test.scala | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala b/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala index e6d99a38c..e4ee309ba 100644 --- a/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala +++ b/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala @@ -39,6 +39,27 @@ class RD10855Test extends TruffleCompilerTestContext with RDBMSTestCreds { ) } + // As we are getting items by index, we can rename the columns, as long as the types are correct + test( + s"""PostgreSQL.Read( + | "${pgsqlCreds.database}", + | "rdbmstest", + | "tbl1", + | type collection(record(e: int, f: int, g: double, h: double, xx: string, yy: string)), + | host = "${pgsqlCreds.host}", + | username = "${pgsqlCreds.username.get}", + | password = "${pgsqlCreds.password.get}" + |)""".stripMargin + ) { it => + it should evaluateTo( + """[ + | {e: 1, f: 1, g: 1.5, h: 1.5, xx: "x1", yy: "y1"}, + | {e: 2, f: 2, g: 2.2, h: 2.2, xx: "x2", yy: "y2"}, + | {e: 3, f: null, g: 3.3, h: null, xx: "x3", yy: null} + |]""".stripMargin + ) + } + // table skippable_types has unsupported types, field f: macaddr, field g: cidr // but are null so no errors will be shown test( From eeae1dce2ee08ec91b23ba02d5e07e98509cc92d Mon Sep 17 00:00:00 2001 From: torcato Date: Wed, 1 May 2024 10:05:45 +0200 Subject: [PATCH 13/17] wip --- .../client/rql2/truffle/Rql2JsonWriter.scala | 1 - .../truffle/regressions/RD10855Test.scala | 39 ++++++++++++++++++- .../raw/compiler/rql2/Rql2TypeUtils.scala | 4 +- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/snapi-client/src/main/scala/raw/client/rql2/truffle/Rql2JsonWriter.scala b/snapi-client/src/main/scala/raw/client/rql2/truffle/Rql2JsonWriter.scala index e7a8714a3..093b7288a 100644 --- a/snapi-client/src/main/scala/raw/client/rql2/truffle/Rql2JsonWriter.scala +++ b/snapi-client/src/main/scala/raw/client/rql2/truffle/Rql2JsonWriter.scala @@ -140,7 +140,6 @@ class Rql2JsonWriter(os: OutputStream) { val index = v.invokeMember("getIndex").asInt() val actualValue = v.invokeMember("getValue") writeValue(actualValue, tipes(index).asInstanceOf[Rql2TypeWithProperties]) - case _ => throw new RuntimeException("unsupported type") } } diff --git a/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala b/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala index e4ee309ba..c061c5367 100644 --- a/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala +++ b/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala @@ -79,8 +79,8 @@ class RD10855Test extends TruffleCompilerTestContext with RDBMSTestCreds { ) } - // Retrieving columns with repeated names val ttt = "\"\"\"" + // Retrieving columns with repeated names test(s"""PostgreSQL.InferAndQuery( | "${pgsqlCreds.database}", | ${ttt}SELECT t1.a, t2.a, t1.b, t2.x as b @@ -98,4 +98,41 @@ class RD10855Test extends TruffleCompilerTestContext with RDBMSTestCreds { |]""".stripMargin ) } + + // Interval type is not supported, so its skipped + test(s"""PostgreSQL.InferAndQuery( + | "${pgsqlCreds.database}", + | "SELECT * FROM rdbmstest.test_types", + | host = "${pgsqlCreds.host}", + | username = "${pgsqlCreds.username.get}", + | password = "${pgsqlCreds.password.get}" + |)""".stripMargin) { it => + it should evaluateTo( + """[ + | { + | smallint1: 1, + | integer1: 2, + | bigint1: 3, + | decimal1: Decimal.From(4.4), + | real1: 5.5f, + | double1: 6.6, + | money1: 7.7, + | varchar1: "string", + | char1: "string ", + | text1: "string", + | boolean1: true, + | timestamp1: Timestamp.Build(1975, 6, 23, 1, 2, seconds=3), + | date1: Date.Build(1975, 6, 23), + | time1: Time.Build(1, 2, seconds=3), + | interval1: "skipping column of type interval", + | bytea1: "eyJhIjogMTIzNCwgImIiOiBbImhlbGxvIiwgIndvcmxkIl19", + | bit1: null, + | integer_array1: null, + | smallserial1: 0, + | serial1: 0, + | bigserial1: 0 + | } + |]""".stripMargin + ) + } } diff --git a/snapi-frontend/src/main/scala/raw/compiler/rql2/Rql2TypeUtils.scala b/snapi-frontend/src/main/scala/raw/compiler/rql2/Rql2TypeUtils.scala index 7333e56b1..4d66cd169 100644 --- a/snapi-frontend/src/main/scala/raw/compiler/rql2/Rql2TypeUtils.scala +++ b/snapi-frontend/src/main/scala/raw/compiler/rql2/Rql2TypeUtils.scala @@ -98,7 +98,9 @@ trait Rql2TypeUtils { ) case SourceBinaryType(n) => wrapProps(Rql2BinaryType(), n || makeNullable, makeTryable) case _: SourceNothingType => wrapProps(Rql2UndefinedType(), makeNullable, makeTryable) - case _: SourceNullType => wrapProps(Rql2UndefinedType(), true, makeTryable) + // (CTM) Null type will be an error if we find a non null value, so we have to make it triable also. + // This is because we use it to skipp unsupported types in databases + case _: SourceNullType => wrapProps(Rql2UndefinedType(), true, true) case _: SourceAnyType => AnyType() case SourceOrType(ors) => val options = ors From f05da941cc67397364d5e17ecb79537a3183dda7 Mon Sep 17 00:00:00 2001 From: torcato Date: Wed, 1 May 2024 14:47:13 +0200 Subject: [PATCH 14/17] wip --- .../compiler/rql2/tests/truffle/regressions/RD10767Test.scala | 2 ++ .../compiler/rql2/tests/truffle/regressions/RD10855Test.scala | 2 ++ 2 files changed, 4 insertions(+) diff --git a/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10767Test.scala b/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10767Test.scala index 4eacb6eef..3ff932b08 100644 --- a/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10767Test.scala +++ b/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10767Test.scala @@ -15,7 +15,9 @@ package raw.compiler.rql2.tests.truffle.regressions import raw.client.api.{GetProgramDescriptionSuccess, ProgramEnvironment} import raw.compiler.rql2.truffle.TruffleCompilerTestContext import raw.compiler.utils.SnapiInterpolator +import raw.testing.tags.TruffleTests +@TruffleTests class RD10767Test extends TruffleCompilerTestContext { private val data = tempFile(""" |[ diff --git a/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala b/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala index c061c5367..6d341b6cc 100644 --- a/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala +++ b/snapi-client/src/test/scala/raw/compiler/rql2/tests/truffle/regressions/RD10855Test.scala @@ -14,7 +14,9 @@ package raw.compiler.rql2.tests.truffle.regressions import raw.compiler.rql2.truffle.TruffleCompilerTestContext import raw.creds.jdbc.RDBMSTestCreds +import raw.testing.tags.TruffleTests +@TruffleTests class RD10855Test extends TruffleCompilerTestContext with RDBMSTestCreds { // The column y is of type varchar, but we are setting to undefined From 2f1e8ce50eca445b38fd7f6e8cd7e7f0f4f05561 Mon Sep 17 00:00:00 2001 From: cesar Date: Wed, 5 Jun 2024 10:15:03 +0200 Subject: [PATCH 15/17] wip --- .../compiler/rql2/builtin/MySQLPackage.scala | 63 +++++++++++++++++-- .../compiler/rql2/builtin/OraclePackage.scala | 25 +++++++- .../builtin/SqlTableExtensionHelper.scala | 39 +++++++++++- .../snapi/truffle/builtin/jdbc/Jdbc.java | 2 - 4 files changed, 117 insertions(+), 12 deletions(-) diff --git a/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/MySQLPackage.scala b/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/MySQLPackage.scala index c21efe38d..9ee62ca0d 100644 --- a/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/MySQLPackage.scala +++ b/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/MySQLPackage.scala @@ -12,6 +12,7 @@ package raw.compiler.rql2.builtin +import com.sun.jdi.BooleanValue import raw.compiler.base.errors.ErrorCompilerMessage import raw.compiler.base.source.{AnythingType, BaseNode, Type} import raw.compiler.common.source._ @@ -19,7 +20,14 @@ import raw.compiler.rql2.api._ import raw.compiler.rql2.ProgramContext import raw.compiler.rql2.source._ import raw.client.api._ -import raw.inferrer.api.{SqlQueryInputFormatDescriptor, SqlTableInputFormatDescriptor} +import raw.inferrer.api.{ + SourceAttrType, + SourceCollectionType, + SourceNullType, + SourceRecordType, + SqlQueryInputFormatDescriptor, + SqlTableInputFormatDescriptor +} class MySQLPackage extends PackageExtension { @@ -76,6 +84,18 @@ class MySQLInferAndReadEntry extends SugarEntryExtension with SqlTableExtensionH description = """The database user password. Can only to be used together with 'host' and 'username' arguments.""".stripMargin, isOptional = true + ), + ParamDoc( + "byIndex", + typeDoc = TypeDoc(List("bool")), + """Fetch the fields of the database by index instead of by name""".stripMargin, + isOptional = true + ), + ParamDoc( + "skipUnsupportedType", + typeDoc = TypeDoc(List("bool")), + """will remove unsupported types from the output of the query automatically""".stripMargin, + isOptional = true ) ), examples = List(ExampleDoc("""MySQL.InferAndRead("database", "table")""")), @@ -98,6 +118,8 @@ class MySQLInferAndReadEntry extends SugarEntryExtension with SqlTableExtensionH case "port" => Right(ValueParam(Rql2IntType())) case "username" => Right(ValueParam(Rql2StringType())) case "password" => Right(ValueParam(Rql2StringType())) + case "byIndex" => Right(ValueParam(Rql2BoolType())) + case "skipUnsupportedType" => Right(ValueParam(Rql2BoolType())) } } @@ -126,9 +148,9 @@ class MySQLInferAndReadEntry extends SugarEntryExtension with SqlTableExtensionH for ( inferrerProperties <- getTableInferrerProperties(mandatoryArgs, optionalArgs, MySqlVendor()); inputFormatDescriptor <- programContext.infer(inferrerProperties); - SqlTableInputFormatDescriptor(_, _, _, _, tipe) = inputFormatDescriptor + t <- resolveInferType(inputFormatDescriptor, optionalArgs) ) yield { - inferTypeToRql2Type(tipe, false, false) + t } } } @@ -183,6 +205,12 @@ class MySQLReadEntry extends SugarEntryExtension with SqlTableExtensionHelper { description = """The database user password. Can only to be used together with 'host' and 'username' arguments.""".stripMargin, isOptional = true + ), + ParamDoc( + "byIndex", + typeDoc = TypeDoc(List("bool")), + """Fetch the fields of the database by index instead of by name""".stripMargin, + isOptional = true ) ), examples = @@ -207,6 +235,7 @@ class MySQLReadEntry extends SugarEntryExtension with SqlTableExtensionHelper { case "port" => Right(ExpParam(Rql2IntType())) case "username" => Right(ExpParam(Rql2StringType())) case "password" => Right(ExpParam(Rql2StringType())) + case "byIndex" => Right(ExpParam(Rql2BoolType())) } } @@ -288,6 +317,18 @@ class MySQLInferAndQueryEntry extends SugarEntryExtension with SqlTableExtension description = """The database user password. Can only to be used together with 'host' and 'username' arguments.""".stripMargin, isOptional = true + ), + ParamDoc( + "byIndex", + typeDoc = TypeDoc(List("bool")), + """Fetch the fields of the database by index instead of by name""".stripMargin, + isOptional = true + ), + ParamDoc( + "skipUnsupportedType", + typeDoc = TypeDoc(List("bool")), + """will remove unsupported types from the output of the query automatically""".stripMargin, + isOptional = true ) ), examples = List(ExampleDoc("""MySQL.InferAndQuery("database", "SELECT * FROM table")""")), @@ -310,6 +351,8 @@ class MySQLInferAndQueryEntry extends SugarEntryExtension with SqlTableExtension case "port" => Right(ValueParam(Rql2IntType())) case "username" => Right(ValueParam(Rql2StringType())) case "password" => Right(ValueParam(Rql2StringType())) + case "byIndex" => Right(ValueParam(Rql2BoolType())) + case "skipUnsupportedType" => Right(ValueParam(Rql2BoolType())) } } @@ -321,9 +364,10 @@ class MySQLInferAndQueryEntry extends SugarEntryExtension with SqlTableExtension for ( inferrerProperties <- getQueryInferrerProperties(mandatoryArgs, optionalArgs, MySqlVendor()); inputFormatDescriptor <- programContext.infer(inferrerProperties); - SqlQueryInputFormatDescriptor(_, _, tipe) = inputFormatDescriptor + SqlQueryInputFormatDescriptor(_, _, tipe) = inputFormatDescriptor; + t <- resolveInferType(inferTypeToRql2Type(tipe, false, false), optionalArgs) ) yield { - inferTypeToRql2Type(tipe, false, false) + t } } @@ -396,6 +440,12 @@ class MySQLQueryEntry extends EntryExtension with SqlTableExtensionHelper { description = """The database user password. Can only to be used together with 'host' and 'username' arguments.""".stripMargin, isOptional = true + ), + ParamDoc( + "byIndex", + typeDoc = TypeDoc(List("bool")), + """Fetch the fields of the database by index instead of by name""".stripMargin, + isOptional = true ) ), examples = List( @@ -413,7 +463,7 @@ class MySQLQueryEntry extends EntryExtension with SqlTableExtensionHelper { else Right(ExpParam(Rql2StringType())) } - override def optionalParams: Option[Set[String]] = Some(Set("host", "username", "port", "password")) + override def optionalParams: Option[Set[String]] = Some(Set("host", "username", "port", "password", "byIndex")) override def getOptionalParam(prevMandatoryArgs: Seq[Arg], idn: String): Either[String, Param] = { idn match { @@ -421,6 +471,7 @@ class MySQLQueryEntry extends EntryExtension with SqlTableExtensionHelper { case "port" => Right(ExpParam(Rql2IntType())) case "username" => Right(ExpParam(Rql2StringType())) case "password" => Right(ExpParam(Rql2StringType())) + case "byIndex" => Right(ExpParam(Rql2BoolType())) } } override def returnTypeErrorList( diff --git a/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/OraclePackage.scala b/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/OraclePackage.scala index 1ec783a00..4b022c23a 100644 --- a/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/OraclePackage.scala +++ b/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/OraclePackage.scala @@ -94,6 +94,18 @@ class OracleInferAndReadEntry extends SugarEntryExtension with SqlTableExtension description = """The database user password. Can only to be used together with 'host' and 'username' arguments.""".stripMargin, isOptional = true + ), + ParamDoc( + "byIndex", + typeDoc = TypeDoc(List("bool")), + """Fetch the fields of the database by index instead of by name""".stripMargin, + isOptional = true + ), + ParamDoc( + "skipUnsupportedType", + typeDoc = TypeDoc(List("bool")), + """will remove unsupported types from the output of the query automatically""".stripMargin, + isOptional = true ) ), examples = List(ExampleDoc("""Oracle.InferAndRead("database", "schema", "table")""")), @@ -116,6 +128,8 @@ class OracleInferAndReadEntry extends SugarEntryExtension with SqlTableExtension case "port" => Right(ValueParam(Rql2IntType())) case "username" => Right(ValueParam(Rql2StringType())) case "password" => Right(ValueParam(Rql2StringType())) + case "byIndex" => Right(ValueParam(Rql2BoolType())) + case "skipUnsupportedType" => Right(ValueParam(Rql2BoolType())) } } @@ -145,9 +159,9 @@ class OracleInferAndReadEntry extends SugarEntryExtension with SqlTableExtension for ( inferrerProperties <- getTableInferrerProperties(mandatoryArgs, optionalArgs, OracleVendor()); inputFormatDescriptor <- programContext.infer(inferrerProperties); - SqlTableInputFormatDescriptor(_, _, _, _, tipe) = inputFormatDescriptor + t <- resolveInferType(inputFormatDescriptor, optionalArgs) ) yield { - inferTypeToRql2Type(tipe, false, false) + t } } } @@ -207,6 +221,12 @@ class OracleReadEntry extends SugarEntryExtension with SqlTableExtensionHelper { description = """The database user password. Can only to be used together with 'host' and 'username' arguments.""".stripMargin, isOptional = true + ), + ParamDoc( + "byIndex", + typeDoc = TypeDoc(List("bool")), + """Fetch the fields of the database by index instead of by name""".stripMargin, + isOptional = true ) ), examples = List( @@ -232,6 +252,7 @@ class OracleReadEntry extends SugarEntryExtension with SqlTableExtensionHelper { case "port" => Right(ExpParam(Rql2IntType())) case "username" => Right(ExpParam(Rql2StringType())) case "password" => Right(ExpParam(Rql2StringType())) + case "byIndex" => Right(ExpParam(Rql2BoolType())) } } diff --git a/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/SqlTableExtensionHelper.scala b/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/SqlTableExtensionHelper.scala index c3c1278f8..c582b69ed 100644 --- a/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/SqlTableExtensionHelper.scala +++ b/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/SqlTableExtensionHelper.scala @@ -14,9 +14,14 @@ package raw.compiler.rql2.builtin import raw.compiler.base.errors.UnsupportedType import raw.compiler.base.source.Type -import raw.compiler.rql2.api.{Arg, EntryExtensionHelper, StringValue, ValueArg} +import raw.compiler.rql2.api.{Arg, BoolValue, EntryExtensionHelper, StringValue, ValueArg} import raw.compiler.rql2.source._ -import raw.inferrer.api.{SqlQueryInferrerProperties, SqlTableInferrerProperties} +import raw.inferrer.api.{ + InputFormatDescriptor, + SqlQueryInferrerProperties, + SqlTableInferrerProperties, + SqlTableInputFormatDescriptor +} import raw.client.api._ import scala.collection.mutable @@ -114,4 +119,34 @@ trait SqlTableExtensionHelper extends EntryExtensionHelper { } } + protected def resolveInferType( + desc: InputFormatDescriptor, + optionalArgs: Seq[(String, Arg)] + ): Either[String, Type] = { + val skipUnsupported = optionalArgs + .find(_._1 == "skipUnsupportedType").exists(x => x._2.asInstanceOf[BoolValue].v) + + val byIndex = optionalArgs + .find(_._1 == "byIndex").exists(x => x._2.asInstanceOf[BoolValue].v) + + val SqlTableInputFormatDescriptor(_, _, _, _, inferType) = desc + if (skipUnsupported && byIndex) { + return Left("cannot use both skipUnsupportedType and byIndex options at the same time") + } + + val tipe = inferTypeToRql2Type(inferType, false, false) + tipe match { + case Rql2IterableType(Rql2RecordType(atts, p1), p2) if skipUnsupported => + val filtered = atts.filter(x => + x.tipe match { + case _: Rql2UndefinedType => false + case _ => true + } + ) + Right(Rql2IterableType(Rql2RecordType(filtered, p1), p2)) + case _ => Right(tipe) + + } + } + } diff --git a/snapi-truffle/src/main/java/raw/compiler/snapi/truffle/builtin/jdbc/Jdbc.java b/snapi-truffle/src/main/java/raw/compiler/snapi/truffle/builtin/jdbc/Jdbc.java index 5148622a8..5fae1e792 100644 --- a/snapi-truffle/src/main/java/raw/compiler/snapi/truffle/builtin/jdbc/Jdbc.java +++ b/snapi-truffle/src/main/java/raw/compiler/snapi/truffle/builtin/jdbc/Jdbc.java @@ -31,8 +31,6 @@ public class Jdbc { - - static public JdbcQueryNode query( ExpressionNode location, ExpressionNode query, From 59f6f695ad53a82af27ab9141997a89976092dd5 Mon Sep 17 00:00:00 2001 From: torcato Date: Wed, 5 Jun 2024 12:33:50 +0200 Subject: [PATCH 16/17] Adding new args to other RDBMS packages --- .../compiler/rql2/builtin/MySQLPackage.scala | 3 +- .../compiler/rql2/builtin/OraclePackage.scala | 25 ++++++++- .../rql2/builtin/PostgreSQLPackage.scala | 43 ++++++++++++++-- .../rql2/builtin/SQLServerPackage.scala | 50 ++++++++++++++++-- .../rql2/builtin/SnowflakePackage.scala | 51 ++++++++++++++++++- .../inferrer/local/jdbc/JdbcInferrer.scala | 5 +- 6 files changed, 160 insertions(+), 17 deletions(-) diff --git a/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/MySQLPackage.scala b/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/MySQLPackage.scala index 9ee62ca0d..b593d053d 100644 --- a/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/MySQLPackage.scala +++ b/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/MySQLPackage.scala @@ -364,8 +364,7 @@ class MySQLInferAndQueryEntry extends SugarEntryExtension with SqlTableExtension for ( inferrerProperties <- getQueryInferrerProperties(mandatoryArgs, optionalArgs, MySqlVendor()); inputFormatDescriptor <- programContext.infer(inferrerProperties); - SqlQueryInputFormatDescriptor(_, _, tipe) = inputFormatDescriptor; - t <- resolveInferType(inferTypeToRql2Type(tipe, false, false), optionalArgs) + t <- resolveInferType(inputFormatDescriptor, optionalArgs) ) yield { t } diff --git a/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/OraclePackage.scala b/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/OraclePackage.scala index 4b022c23a..17582e6bc 100644 --- a/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/OraclePackage.scala +++ b/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/OraclePackage.scala @@ -338,6 +338,18 @@ class OracleInferAndQueryEntry extends SugarEntryExtension with SqlTableExtensio description = """The database user password. Can only to be used together with 'host' and 'username' arguments.""".stripMargin, isOptional = true + ), + ParamDoc( + "byIndex", + typeDoc = TypeDoc(List("bool")), + """Fetch the fields of the database by index instead of by name""".stripMargin, + isOptional = true + ), + ParamDoc( + "skipUnsupportedType", + typeDoc = TypeDoc(List("bool")), + """will remove unsupported types from the output of the query automatically""".stripMargin, + isOptional = true ) ), examples = List(ExampleDoc("""Oracle.InferAndQuery("database", "SELECT * FROM schema.table")""")), @@ -360,6 +372,8 @@ class OracleInferAndQueryEntry extends SugarEntryExtension with SqlTableExtensio case "port" => Right(ValueParam(Rql2IntType())) case "username" => Right(ValueParam(Rql2StringType())) case "password" => Right(ValueParam(Rql2StringType())) + case "byIndex" => Right(ValueParam(Rql2BoolType())) + case "skipUnsupportedType" => Right(ValueParam(Rql2BoolType())) } } @@ -371,9 +385,9 @@ class OracleInferAndQueryEntry extends SugarEntryExtension with SqlTableExtensio for ( inferrerProperties <- getQueryInferrerProperties(mandatoryArgs, optionalArgs, OracleVendor()); inputFormatDescriptor <- programContext.infer(inferrerProperties); - SqlQueryInputFormatDescriptor(_, _, tipe) = inputFormatDescriptor + t <- resolveInferType(inputFormatDescriptor, optionalArgs) ) yield { - inferTypeToRql2Type(tipe, false, false) + t } } @@ -446,6 +460,12 @@ class OracleQueryEntry extends EntryExtension with SqlTableExtensionHelper { description = """The database user password. Can only to be used together with 'host' and 'username' arguments.""".stripMargin, isOptional = true + ), + ParamDoc( + "byIndex", + typeDoc = TypeDoc(List("bool")), + """Fetch the fields of the database by index instead of by name""".stripMargin, + isOptional = true ) ), examples = List( @@ -473,6 +493,7 @@ class OracleQueryEntry extends EntryExtension with SqlTableExtensionHelper { case "port" => Right(ExpParam(Rql2IntType())) case "username" => Right(ExpParam(Rql2StringType())) case "password" => Right(ExpParam(Rql2StringType())) + case "byIndex" => Right(ExpParam(Rql2BoolType())) } } diff --git a/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/PostgreSQLPackage.scala b/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/PostgreSQLPackage.scala index 13212a8b6..5a99ec612 100644 --- a/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/PostgreSQLPackage.scala +++ b/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/PostgreSQLPackage.scala @@ -93,6 +93,18 @@ class PostgreSQLInferAndReadEntry extends SugarEntryExtension with SqlTableExten description = """The database user password. Can only to be used together with 'host' and 'username' arguments.""".stripMargin, isOptional = true + ), + ParamDoc( + "byIndex", + typeDoc = TypeDoc(List("bool")), + """Fetch the fields of the database by index instead of by name""".stripMargin, + isOptional = true + ), + ParamDoc( + "skipUnsupportedType", + typeDoc = TypeDoc(List("bool")), + """will remove unsupported types from the output of the query automatically""".stripMargin, + isOptional = true ) ), examples = List(ExampleDoc("""PostgreSQL.InferAndRead("database", "schema", "table")""")), @@ -116,6 +128,8 @@ class PostgreSQLInferAndReadEntry extends SugarEntryExtension with SqlTableExten case "port" => Right(ValueParam(Rql2IntType())) case "username" => Right(ValueParam(Rql2StringType())) case "password" => Right(ValueParam(Rql2StringType())) + case "byIndex" => Right(ValueParam(Rql2BoolType())) + case "skipUnsupportedType" => Right(ValueParam(Rql2BoolType())) } } @@ -145,9 +159,9 @@ class PostgreSQLInferAndReadEntry extends SugarEntryExtension with SqlTableExten for ( inferrerProperties <- getTableInferrerProperties(mandatoryArgs, optionalArgs, PgSqlVendor()); inputFormatDescriptor <- programContext.infer(inferrerProperties); - SqlTableInputFormatDescriptor(_, _, _, _, tipe) = inputFormatDescriptor + t <- resolveInferType(inputFormatDescriptor, optionalArgs) ) yield { - inferTypeToRql2Type(tipe, false, false) + t } } } @@ -207,6 +221,12 @@ class PostgreSQLReadEntry extends SugarEntryExtension with SqlTableExtensionHelp description = """The database user password. Can only to be used together with 'host' and 'username' arguments.""".stripMargin, isOptional = true + ), + ParamDoc( + "byIndex", + typeDoc = TypeDoc(List("bool")), + """Fetch the fields of the database by index instead of by name""".stripMargin, + isOptional = true ) ), examples = List( @@ -235,6 +255,7 @@ class PostgreSQLReadEntry extends SugarEntryExtension with SqlTableExtensionHelp case "port" => Right(ExpParam(Rql2IntType())) case "username" => Right(ExpParam(Rql2StringType())) case "password" => Right(ExpParam(Rql2StringType())) + case "byIndex" => Right(ExpParam(Rql2BoolType())) } } @@ -318,6 +339,18 @@ class PostgreSQLInferAndQueryEntry extends SugarEntryExtension with SqlTableExte description = """The database user password. Can only to be used together with 'host' and 'username' arguments.""".stripMargin, isOptional = true + ), + ParamDoc( + "byIndex", + typeDoc = TypeDoc(List("bool")), + """Fetch the fields of the database by index instead of by name""".stripMargin, + isOptional = true + ), + ParamDoc( + "skipUnsupportedType", + typeDoc = TypeDoc(List("bool")), + """will remove unsupported types from the output of the query automatically""".stripMargin, + isOptional = true ) ), examples = List(ExampleDoc("""PostgreSQL.InferAndQuery("database", "SELECT * FROM schema.table")""")), @@ -341,6 +374,8 @@ class PostgreSQLInferAndQueryEntry extends SugarEntryExtension with SqlTableExte case "port" => Right(ValueParam(Rql2IntType())) case "username" => Right(ValueParam(Rql2StringType())) case "password" => Right(ValueParam(Rql2StringType())) + case "byIndex" => Right(ValueParam(Rql2BoolType())) + case "skipUnsupportedType" => Right(ValueParam(Rql2BoolType())) } } @@ -352,9 +387,9 @@ class PostgreSQLInferAndQueryEntry extends SugarEntryExtension with SqlTableExte for ( inferrerProperties <- getQueryInferrerProperties(mandatoryArgs, optionalArgs, PgSqlVendor()); inputFormatDescriptor <- programContext.infer(inferrerProperties); - SqlQueryInputFormatDescriptor(_, _, tipe) = inputFormatDescriptor + t <- resolveInferType(inputFormatDescriptor, optionalArgs) ) yield { - inferTypeToRql2Type(tipe, false, false) + t } } diff --git a/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/SQLServerPackage.scala b/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/SQLServerPackage.scala index bbd0a8232..ab35652d7 100644 --- a/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/SQLServerPackage.scala +++ b/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/SQLServerPackage.scala @@ -94,6 +94,18 @@ class SQLServerInferAndReadEntry extends SugarEntryExtension with SqlTableExtens description = """The database user password. Can only to be used together with 'host' and 'username' arguments.""".stripMargin, isOptional = true + ), + ParamDoc( + "byIndex", + typeDoc = TypeDoc(List("bool")), + """Fetch the fields of the database by index instead of by name""".stripMargin, + isOptional = true + ), + ParamDoc( + "skipUnsupportedType", + typeDoc = TypeDoc(List("bool")), + """will remove unsupported types from the output of the query automatically""".stripMargin, + isOptional = true ) ), examples = List(ExampleDoc("""SQLServer.InferAndRead("database", "schema", "table")""")), @@ -117,6 +129,8 @@ class SQLServerInferAndReadEntry extends SugarEntryExtension with SqlTableExtens case "port" => Right(ValueParam(Rql2IntType())) case "username" => Right(ValueParam(Rql2StringType())) case "password" => Right(ValueParam(Rql2StringType())) + case "byIndex" => Right(ValueParam(Rql2BoolType())) + case "skipUnsupportedType" => Right(ValueParam(Rql2BoolType())) } } @@ -146,9 +160,9 @@ class SQLServerInferAndReadEntry extends SugarEntryExtension with SqlTableExtens for ( inferrerProperties <- getTableInferrerProperties(mandatoryArgs, optionalArgs, SqlServerVendor()); inputFormatDescriptor <- programContext.infer(inferrerProperties); - SqlTableInputFormatDescriptor(_, _, _, _, tipe) = inputFormatDescriptor + t <- resolveInferType(inputFormatDescriptor, optionalArgs) ) yield { - inferTypeToRql2Type(tipe, false, false) + t } } } @@ -208,6 +222,12 @@ class SQLServerReadEntry extends SugarEntryExtension with SqlTableExtensionHelpe description = """The database user password. Can only to be used together with 'host' and 'username' arguments.""".stripMargin, isOptional = true + ), + ParamDoc( + "byIndex", + typeDoc = TypeDoc(List("bool")), + """Fetch the fields of the database by index instead of by name""".stripMargin, + isOptional = true ) ), examples = List( @@ -236,6 +256,7 @@ class SQLServerReadEntry extends SugarEntryExtension with SqlTableExtensionHelpe case "port" => Right(ExpParam(Rql2IntType())) case "username" => Right(ExpParam(Rql2StringType())) case "password" => Right(ExpParam(Rql2StringType())) + case "byIndex" => Right(ExpParam(Rql2BoolType())) } } @@ -320,6 +341,18 @@ class SQLServerInferAndQueryEntry extends SugarEntryExtension with SqlTableExten description = """The database user password. Can only to be used together with 'host' and 'username' arguments.""".stripMargin, isOptional = true + ), + ParamDoc( + "byIndex", + typeDoc = TypeDoc(List("bool")), + """Fetch the fields of the database by index instead of by name""".stripMargin, + isOptional = true + ), + ParamDoc( + "skipUnsupportedType", + typeDoc = TypeDoc(List("bool")), + """will remove unsupported types from the output of the query automatically""".stripMargin, + isOptional = true ) ), examples = List(ExampleDoc("""SQLServer.InferAndQuery("database", "SELECT * FROM schema.table")""")), @@ -343,6 +376,8 @@ class SQLServerInferAndQueryEntry extends SugarEntryExtension with SqlTableExten case "port" => Right(ValueParam(Rql2IntType())) case "username" => Right(ValueParam(Rql2StringType())) case "password" => Right(ValueParam(Rql2StringType())) + case "byIndex" => Right(ValueParam(Rql2BoolType())) + case "skipUnsupportedType" => Right(ValueParam(Rql2BoolType())) } } @@ -354,9 +389,9 @@ class SQLServerInferAndQueryEntry extends SugarEntryExtension with SqlTableExten for ( inferrerProperties <- getQueryInferrerProperties(mandatoryArgs, optionalArgs, SqlServerVendor()); inputFormatDescriptor <- programContext.infer(inferrerProperties); - SqlQueryInputFormatDescriptor(_, _, tipe) = inputFormatDescriptor + t <- resolveInferType(inputFormatDescriptor, optionalArgs) ) yield { - inferTypeToRql2Type(tipe, false, false) + t } } @@ -428,6 +463,12 @@ class SQLServerQueryEntry extends EntryExtension with SqlTableExtensionHelper { description = """The database user password. Can only to be used together with 'host' and 'username' arguments.""".stripMargin, isOptional = true + ), + ParamDoc( + "byIndex", + typeDoc = TypeDoc(List("bool")), + """Fetch the fields of the database by index instead of by name""".stripMargin, + isOptional = true ) ), examples = List( @@ -456,6 +497,7 @@ class SQLServerQueryEntry extends EntryExtension with SqlTableExtensionHelper { case "port" => Right(ExpParam(Rql2IntType())) case "username" => Right(ExpParam(Rql2StringType())) case "password" => Right(ExpParam(Rql2StringType())) + case "byIndex" => Right(ExpParam(Rql2BoolType())) } } diff --git a/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/SnowflakePackage.scala b/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/SnowflakePackage.scala index 123c4fa89..507d7bf92 100644 --- a/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/SnowflakePackage.scala +++ b/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/SnowflakePackage.scala @@ -97,6 +97,18 @@ class SnowflakeInferAndReadEntry extends SugarEntryExtension with SqlTableExtens typeDoc = TypeDoc(List("list")), description = """Extra connection options.""".stripMargin, isOptional = true + ), + ParamDoc( + "byIndex", + typeDoc = TypeDoc(List("bool")), + """Fetch the fields of the database by index instead of by name""".stripMargin, + isOptional = true + ), + ParamDoc( + "skipUnsupportedType", + typeDoc = TypeDoc(List("bool")), + """will remove unsupported types from the output of the query automatically""".stripMargin, + isOptional = true ) ), examples = List(ExampleDoc("""Snowflake.InferAndRead("database", "schema", "table")""")), @@ -237,6 +249,12 @@ class SnowflakeReadEntry extends SugarEntryExtension with SqlTableExtensionHelpe typeDoc = TypeDoc(List("list")), description = """Extra connection options.""".stripMargin, isOptional = true + ), + ParamDoc( + "byIndex", + typeDoc = TypeDoc(List("bool")), + """Fetch the fields of the database by index instead of by name""".stripMargin, + isOptional = true ) ), examples = List( @@ -276,6 +294,7 @@ class SnowflakeReadEntry extends SugarEntryExtension with SqlTableExtensionHelpe ) ) ) + case "byIndex" => Right(ExpParam(Rql2BoolType())) } } @@ -365,6 +384,18 @@ class SnowflakeInferAndQueryEntry extends SugarEntryExtension with SqlTableExten typeDoc = TypeDoc(List("list")), description = """Extra connection options.""".stripMargin, isOptional = true + ), + ParamDoc( + "byIndex", + typeDoc = TypeDoc(List("bool")), + """Fetch the fields of the database by index instead of by name""".stripMargin, + isOptional = true + ), + ParamDoc( + "skipUnsupportedType", + typeDoc = TypeDoc(List("bool")), + """will remove unsupported types from the output of the query automatically""".stripMargin, + isOptional = true ) ), examples = List(ExampleDoc("""Snowflake.InferAndQuery("database", "SELECT * FROM schema.table")""")), @@ -399,6 +430,8 @@ class SnowflakeInferAndQueryEntry extends SugarEntryExtension with SqlTableExten ) ) ) + case "byIndex" => Right(ValueParam(Rql2BoolType())) + case "skipUnsupportedType" => Right(ValueParam(Rql2BoolType())) } } @@ -410,9 +443,9 @@ class SnowflakeInferAndQueryEntry extends SugarEntryExtension with SqlTableExten for ( inferrerProperties <- getQueryInferrerProperties(mandatoryArgs, optionalArgs, SnowflakeVendor()); inputFormatDescriptor <- programContext.infer(inferrerProperties); - SqlQueryInputFormatDescriptor(_, _, tipe) = inputFormatDescriptor + t <- resolveInferType(inputFormatDescriptor, optionalArgs) ) yield { - inferTypeToRql2Type(tipe, makeNullable = false, makeTryable = false) + t } } @@ -500,6 +533,19 @@ class SnowflakeQueryEntry extends EntryExtension with SqlTableExtensionHelper { typeDoc = TypeDoc(List("list")), description = """Extra connection options.""".stripMargin, isOptional = true + ), + ParamDoc( + "password", + typeDoc = TypeDoc(List("string")), + description = + """The database user password. Can only to be used together with 'host' and 'username' arguments.""".stripMargin, + isOptional = true + ), + ParamDoc( + "byIndex", + typeDoc = TypeDoc(List("bool")), + """Fetch the fields of the database by index instead of by name""".stripMargin, + isOptional = true ) ), examples = List( @@ -539,6 +585,7 @@ class SnowflakeQueryEntry extends EntryExtension with SqlTableExtensionHelper { ) ) ) + case "byIndex" => Right(ExpParam(Rql2BoolType())) } } diff --git a/snapi-frontend/src/main/scala/raw/inferrer/local/jdbc/JdbcInferrer.scala b/snapi-frontend/src/main/scala/raw/inferrer/local/jdbc/JdbcInferrer.scala index e3191a861..61490ad2d 100644 --- a/snapi-frontend/src/main/scala/raw/inferrer/local/jdbc/JdbcInferrer.scala +++ b/snapi-frontend/src/main/scala/raw/inferrer/local/jdbc/JdbcInferrer.scala @@ -12,12 +12,11 @@ package raw.inferrer.local.jdbc -import java.sql.ResultSetMetaData import com.typesafe.scalalogging.StrictLogging -import raw.inferrer.api.{SourceAttrType, SourceCollectionType, SourceNullType, SourceRecordType, SourceType} +import raw.inferrer.api._ import raw.sources.jdbc.api.{JdbcLocation, JdbcTableLocation} -import scala.collection.mutable +import java.sql.ResultSetMetaData class JdbcInferrer extends JdbcTypeToSourceType with StrictLogging { From 1828ff96a7f4e80855e15f91882dab873db2f400 Mon Sep 17 00:00:00 2001 From: torcato Date: Wed, 5 Jun 2024 13:39:04 +0200 Subject: [PATCH 17/17] Filtering optional args while desugaring --- .../main/scala/raw/compiler/rql2/builtin/MySQLPackage.scala | 6 +++--- .../scala/raw/compiler/rql2/builtin/SQLServerPackage.scala | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/MySQLPackage.scala b/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/MySQLPackage.scala index b593d053d..0b2ba87bb 100644 --- a/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/MySQLPackage.scala +++ b/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/MySQLPackage.scala @@ -133,7 +133,7 @@ class MySQLInferAndReadEntry extends SugarEntryExtension with SqlTableExtensionH val db = FunAppArg(StringConst(getStringValue(mandatoryArgs(0))), None) val table = FunAppArg(StringConst(getStringValue(mandatoryArgs(1))), None) val readType = FunAppArg(TypeExp(t), None) - val optArgs = optionalArgs.map { case (idn, ValueArg(StringValue(s), _)) => FunAppArg(StringConst(s), Some(idn)) } + val optArgs = optionalArgs.filter(x => x._1 != "skipUnsupportedType").map { case (idn, ValueArg(StringValue(s), _)) => FunAppArg(StringConst(s), Some(idn)) } FunApp( Proj(PackageIdnExp("MySQL"), "Read"), Vector(db, table, readType) ++ optArgs @@ -259,7 +259,7 @@ class MySQLReadEntry extends SugarEntryExtension with SqlTableExtensionHelper { val db = FunAppArg(mandatoryArgs.head.asInstanceOf[ExpArg].e, None) val table = FunAppArg(mandatoryArgs(1).asInstanceOf[ExpArg].e, None) val tipe = FunAppArg(TypeExp(mandatoryArgs(2).asInstanceOf[TypeArg].t), None) - val optArgs = optionalArgs.map { case (idn, ExpArg(e, _)) => FunAppArg(e, Some(idn)) } + val optArgs = optionalArgs.map { case (idn, ValueArg(StringValue(s), _)) => FunAppArg(StringConst(s), Some(idn)) } // MySql needs the table name to be quoted with backticks def quoted(e: Exp) = BinaryExp(Plus(), BinaryExp(Plus(), StringConst("`"), e), StringConst("`")) @@ -380,7 +380,7 @@ class MySQLInferAndQueryEntry extends SugarEntryExtension with SqlTableExtension val db = FunAppArg(StringConst(getStringValue(mandatoryArgs(0))), None) val query = FunAppArg(StringConst(getStringValue(mandatoryArgs(1))), None) val readType = FunAppArg(TypeExp(t), None) - val optArgs = optionalArgs.map { case (idn, ValueArg(StringValue(s), _)) => FunAppArg(StringConst(s), Some(idn)) } + val optArgs = optionalArgs.filter(x => x._1 != "skipUnsupportedType").map { case (idn, ValueArg(StringValue(s), _)) => FunAppArg(StringConst(s), Some(idn)) } FunApp( Proj(PackageIdnExp("MySQL"), "Query"), Vector(db, query, readType) ++ optArgs diff --git a/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/SQLServerPackage.scala b/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/SQLServerPackage.scala index ab35652d7..ce64c850f 100644 --- a/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/SQLServerPackage.scala +++ b/snapi-frontend/src/main/scala/raw/compiler/rql2/builtin/SQLServerPackage.scala @@ -145,7 +145,7 @@ class SQLServerInferAndReadEntry extends SugarEntryExtension with SqlTableExtens val schema = FunAppArg(StringConst(getStringValue(mandatoryArgs(1))), None) val table = FunAppArg(StringConst(getStringValue(mandatoryArgs(2))), None) val readType = FunAppArg(TypeExp(t), None) - val optArgs = optionalArgs.map { case (idn, ValueArg(StringValue(s), _)) => FunAppArg(StringConst(s), Some(idn)) } + val optArgs = optionalArgs.filter(x => x._1 != "skipUnsupportedType").map { case (idn, ValueArg(StringValue(s), _)) => FunAppArg(StringConst(s), Some(idn)) } FunApp( Proj(PackageIdnExp("SQLServer"), "Read"), Vector(db, schema, table, readType) ++ optArgs @@ -405,7 +405,7 @@ class SQLServerInferAndQueryEntry extends SugarEntryExtension with SqlTableExten val db = FunAppArg(StringConst(getStringValue(mandatoryArgs(0))), None) val query = FunAppArg(StringConst(getStringValue(mandatoryArgs(1))), None) val readType = FunAppArg(TypeExp(t), None) - val optArgs = optionalArgs.map { case (idn, ValueArg(StringValue(s), _)) => FunAppArg(StringConst(s), Some(idn)) } + val optArgs = optionalArgs.filter(x => x._1 != "skipUnsupportedType").map { case (idn, ValueArg(StringValue(s), _)) => FunAppArg(StringConst(s), Some(idn)) } FunApp( Proj(PackageIdnExp("SQLServer"), "Query"), Vector(db, query, readType) ++ optArgs