Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Directly write varchar slices to JSON #24300

Merged
merged 2 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public void encode(JsonGenerator generator, ConnectorSession session, Block bloc
return;
}
Slice slice = VARCHAR.getSlice(block, position);
generator.writeString(slice.toStringUtf8());
generator.writeUTF8String(slice.byteArray(), slice.byteArrayOffset(), slice.length());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.server.protocol.spooling;
package io.trino.server.protocol;

import com.google.common.collect.ImmutableList;
import io.trino.client.Column;
import io.trino.client.QueryDataDecoder;
import io.trino.client.Row;
import io.trino.client.spooling.DataAttributes;
import io.trino.client.spooling.encoding.JsonQueryDataDecoder;
import io.trino.server.protocol.OutputColumn;
import io.trino.server.protocol.spooling.QueryDataEncoder;
import io.trino.server.protocol.spooling.encoding.JsonQueryDataEncoder;
import io.trino.spi.Page;
import io.trino.spi.block.ArrayBlockBuilder;
Expand All @@ -37,12 +37,12 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static io.airlift.slice.Slices.utf8Slice;
import static io.trino.SessionTestUtils.TEST_SESSION;
Expand All @@ -56,7 +56,7 @@
import static io.trino.block.BlockAssertions.createTinyintsBlock;
import static io.trino.block.BlockAssertions.createTypedLongsBlock;
import static io.trino.server.protocol.ProtocolUtil.createColumn;
import static io.trino.server.protocol.spooling.TestJsonQueryDataEncoding.TypedColumn.typed;
import static io.trino.server.protocol.TestJsonEncodingUtils.TypedColumn.typed;
import static io.trino.spi.type.BigintType.BIGINT;
import static io.trino.spi.type.BooleanType.BOOLEAN;
import static io.trino.spi.type.DoubleType.DOUBLE;
Expand All @@ -68,10 +68,11 @@
import static io.trino.spi.type.VarcharType.VARCHAR;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class TestJsonQueryDataEncoding
public class TestJsonEncodingUtils
{
protected QueryDataDecoder createDecoder(List<Column> columns)
{
Expand Down Expand Up @@ -291,10 +292,27 @@ public void testVarcharSerialization()
throws IOException
{
List<TypedColumn> columns = ImmutableList.of(typed("col0", VARCHAR));
Page page = page(createStringsBlock("ala", "ma", "kota", "a", "kot", "ma", "ale", ""));
Page page = page(createStringsBlock("ala", "ma", "kota", "a", "kot", "ma", "ale", "", null));

assertThat(roundTrip(columns, page, "[[\"ala\"],[\"ma\"],[\"kota\"],[\"a\"],[\"kot\"],[\"ma\"],[\"ale\"],[\"\"]]"))
.isEqualTo(column("ala", "ma", "kota", "a", "kot", "ma", "ale", ""));
assertThat(roundTrip(columns, page, "[[\"ala\"],[\"ma\"],[\"kota\"],[\"a\"],[\"kot\"],[\"ma\"],[\"ale\"],[\"\"],[null]]"))
wendigo marked this conversation as resolved.
Show resolved Hide resolved
.isEqualTo(column("ala", "ma", "kota", "a", "kot", "ma", "ale", "", null));
}

@Test
public void testVarcharUtf8Serialization()
throws IOException
{
List<TypedColumn> columns = ImmutableList.of(typed("col0", VARCHAR));
Page page = page(createStringsBlock(
"数据应用",
"\"quoted\"",
"zażółć gęślą jaźń",
"\0\0\0", // garbage in, garbage out
"\r\t\n",
"\uD83E\uDD83"));

assertThat(roundTrip(columns, page, "[[\"数据应用\"],[\"\\\"quoted\\\"\"],[\"zażółć gęślą jaźń\"],[\"\\u0000\\u0000\\u0000\"],[\"\\r\\t\\n\"],[\"🦃\"]]"))
.isEqualTo(column("数据应用", "\"quoted\"", "zażółć gęślą jaźń", "\0\0\0", "\r\t\n", "🦃"));
}

@Test
Expand Down Expand Up @@ -564,8 +582,13 @@ private static Page page(Block... blocks)
private static <T> List<List<T>> column(T... values)
{
return Arrays.stream(values)
.map(List::of)
.collect(toImmutableList());
// Allow nulls in values
.map(value -> {
List<T> list = new ArrayList<>();
list.add(value);
return list;
})
.collect(toList());
}

private static <T> List<T> array(T... values)
Expand Down
Loading