Skip to content

Commit

Permalink
RD-9317 text output doesn't cope with nullable strings (#56)
Browse files Browse the repository at this point in the history
- Enabled another test for text type
- Fixed the issue by reusing BinaryWriter
  • Loading branch information
alexzerntev authored Aug 8, 2023
1 parent 57d42fd commit e182db3
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ import raw.runtime.truffle.ast.expressions.unary._
import raw.runtime.truffle.ast.io.binary.BinaryWriterNode
import raw.runtime.truffle.ast.io.csv.writer.{CsvIterableWriterNode, CsvListWriterNode}
import raw.runtime.truffle.ast.io.json.writer.JsonWriterNode
import raw.runtime.truffle.ast.io.text.TextWriterNode
import raw.runtime.truffle.ast.local._
import raw.runtime.truffle.runtime.generator.GeneratorLibrary
import raw.runtime.truffle.runtime.iterable.IterableLibrary
Expand Down Expand Up @@ -310,7 +309,7 @@ class Rql2TruffleCompiler(implicit compilerContext: CompilerContext)
new ProgramStatementNode(
RawLanguage.getCurrentContext.getLanguage,
frameDescriptor,
new TextWriterNode(bodyExpNode, writer)
new BinaryWriterNode(bodyExpNode, writer)
)
case _ => throw new CompilerException("unsupported type")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import raw.runtime.truffle.ast.expressions.builtin.binary_package.{
BinaryFromStringNodeGen,
BinaryReadNodeGen
}
import raw.runtime.truffle.ast.io.{NullableStreamWriterNode, TryableStreamWriterNode}
import raw.runtime.truffle.ast.io.binary.BinaryBytesWriterNode
import raw.runtime.truffle.ast.io.binary.{BinaryBytesWriterNode, NullableBinaryWriterNode, TryableBinaryWriterNode}
import raw.runtime.truffle.{ExpressionNode, RawLanguage}

class TruffleFromStringBinaryEntryExtension extends FromStringBinaryEntryExtension with TruffleShortEntryExtension {
Expand Down Expand Up @@ -56,12 +55,12 @@ object TruffleBinaryWriter {
// Tryable binary: wrap the inner writer (plain or nullable) in a tryable writer that throws in case of failure.
val innerType = t.cloneAndRemoveProp(Rql2IsTryableTypeProperty()).asInstanceOf[Rql2BinaryType]
val innerWriter = TruffleBinaryWriter(innerType)
new ProgramStatementNode(lang, frameDescriptor, new TryableStreamWriterNode(innerWriter))
new ProgramStatementNode(lang, frameDescriptor, new TryableBinaryWriterNode(innerWriter))
} else {
// Nullable binary: wrap the inner writer (plain) in a nullable writer that writes nothing if the value is null.
val innerType = t.cloneAndRemoveProp(Rql2IsNullableTypeProperty()).asInstanceOf[Rql2BinaryType]
val innerWriter = TruffleBinaryWriter(innerType)
new ProgramStatementNode(lang, frameDescriptor, new NullableStreamWriterNode(innerWriter))
new ProgramStatementNode(lang, frameDescriptor, new NullableBinaryWriterNode(innerWriter))
}

}
Expand All @@ -75,12 +74,12 @@ object TruffleBinaryWriter {
// Tryable binary: wrap the inner writer (plain or nullable) in a tryable writer that throws in case of failure.
val innerType = t.cloneAndRemoveProp(Rql2IsTryableTypeProperty()).asInstanceOf[Rql2StringType]
val innerWriter = TruffleBinaryWriter(innerType)
new ProgramStatementNode(lang, frameDescriptor, new TryableStreamWriterNode(innerWriter))
new ProgramStatementNode(lang, frameDescriptor, new TryableBinaryWriterNode(innerWriter))
} else {
// Nullable binary: wrap the inner writer (plain) in a nullable writer that writes nothing if the value is null.
val innerType = t.cloneAndRemoveProp(Rql2IsNullableTypeProperty()).asInstanceOf[Rql2StringType]
val innerWriter = TruffleBinaryWriter(innerType)
new ProgramStatementNode(lang, frameDescriptor, new NullableStreamWriterNode(innerWriter))
new ProgramStatementNode(lang, frameDescriptor, new NullableBinaryWriterNode(innerWriter))
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ import raw.testing.tags.TruffleTests
@TruffleTests class RD5412TruffleTest extends TruffleCompilerTestContext with RD5412Test

// 'text' format not supported
// @TruffleTests class RD5971TruffleTest extends TruffleCompilerTestContext with RD5971Test
@TruffleTests class RD5971TruffleTest extends TruffleCompilerTestContext with RD5971Test
@TruffleTests class RD4445TruffleTest extends TruffleCompilerTestContext with RD4445Test

// XML
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ private void doWrite(OutputStream os, byte[] binaryData) {
@Override
public void executeVoid(VirtualFrame frame) {
Object[] args = frame.getArguments();
byte[] binaryData = (byte[]) args[0];
byte[] binaryData;
if (args[0] instanceof byte[]) {
binaryData = (byte[]) args[0];
} else {
binaryData = ((String) args[0]).getBytes();
}
OutputStream output = (OutputStream) args[1];
doWrite(output, binaryData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* licenses/APL.txt.
*/

package raw.runtime.truffle.ast.io;
package raw.runtime.truffle.ast.io.binary;

import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.DirectCallNode;
Expand All @@ -22,15 +22,15 @@
import java.io.OutputStream;

@NodeInfo(shortName = "Binary.NullableWrite")
public class NullableStreamWriterNode extends StatementNode {
public class NullableBinaryWriterNode extends StatementNode {

@Child
private DirectCallNode innerWriter;

@Child
private OptionLibrary options = OptionLibrary.getFactory().createDispatched(1);

public NullableStreamWriterNode(ProgramStatementNode innerWriter) {
public NullableBinaryWriterNode(ProgramStatementNode innerWriter) {
this.innerWriter = DirectCallNode.create(innerWriter.getCallTarget());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* licenses/APL.txt.
*/

package raw.runtime.truffle.ast.io;
package raw.runtime.truffle.ast.io.binary;

import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.DirectCallNode;
Expand All @@ -23,15 +23,15 @@
import java.io.OutputStream;

@NodeInfo(shortName = "Binary.TryableWrite")
public class TryableStreamWriterNode extends StatementNode {
public class TryableBinaryWriterNode extends StatementNode {

@Child
private DirectCallNode innerWriter;

@Child
private TryableLibrary tryables = TryableLibrary.getFactory().createDispatched(1);

public TryableStreamWriterNode(ProgramStatementNode innerWriter) {
public TryableBinaryWriterNode(ProgramStatementNode innerWriter) {
this.innerWriter = DirectCallNode.create(innerWriter.getCallTarget());
}

Expand Down

This file was deleted.

0 comments on commit e182db3

Please sign in to comment.