fix(redshift): prune Spectrum partitions for binary integer binds (#7912)

This commit is contained in:
Vũ Thế Huy authored and GitHub committed 2026-11-11 13:06:00 -04:00
1 parent 21cfcea3dd
commit 9f8593a062
6 files changed
+194 -31

No files matched your search

@@ -23,6 +23,7 @@ import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.postgresql.PGStatement;
@@ -680,9 +681,10 @@ class RedshiftSpectrumIntegrationTest {
}
@ParameterizedTest
@ValueSource(booleans = {false, true})
@CsvSource({"false,false", "true,false", "true,true"})
@Timeout(120)
void readsOnlyThePartitionsTheWhereSelectsOnTheExtendedProtocolAndInATransaction(boolean bindParameters) throws Exception {
void readsOnlyThePartitionsTheWhereSelectsOnTheExtendedProtocolAndInATransaction(
boolean bindParameters, boolean binaryTransfer) throws Exception {
bucket = "spectrum-pruning-ext-" + System.nanoTime();
s3Service.createBucket(bucket, "us-east-1");
s3Service.putObject(bucket, "events/day=1/part-1.csv", "1,10\n2,20\n".getBytes(StandardCharsets.UTF_8),
@@ -730,7 +732,8 @@ class RedshiftSpectrumIntegrationTest {
}
}
String select = bindParameters ? "SELECT SUM(amount) FROM " + schemaName + ".events WHERE day = ?" : selectDayOne;
try (Connection extended = DriverManager.getConnection(baseUrl + "&preferQueryMode=extended&binaryTransfer=false",
String extendedUrl = baseUrl + "&preferQueryMode=extended" + (binaryTransfer ? "" : "&binaryTransfer=false");
try (Connection extended = DriverManager.getConnection(extendedUrl,
"admin", "Secret123");
PreparedStatement query = extended.prepareStatement(select)) {
query.unwrap(PGStatement.class).setPrepareThreshold(1);
@@ -1,6 +1,8 @@
package io.floci.aws.services.redshift.proxy;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@@ -8,6 +10,7 @@ import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
@@ -23,6 +26,45 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
class PostgresWireDecoderTest {
@ParameterizedTest
@CsvSource({"21,2,-32768", "21,2,32767", "23,4,-2147483648", "23,4,2147483647", "23,4,0",
"20,8,-9223372036854775808", "20,8,9223372036854775807", "23,4,1"})
void decodesBinaryIntegersUsingTheirDeclaredType(int oid, int width, long value) throws IOException {
byte[] encoded = Arrays.copyOfRange(ByteBuffer.allocate(8).putLong(value).array(), 8 - width, 8);
byte[] body = concat(cString("p"), cString("s"), int16(1), int16(1), int16(1),
int32(width), encoded, int16(0));
assertEquals(List.of(Long.toString(value)), parameters(body, List.of(oid)));
}
@Test
void decodesMixedFormatsWithoutGuessingUnsupportedBinaryTypesOrNullValues() throws IOException {
byte[] body = concat(cString("p"), cString("s"), int16(4), int16(0), int16(1), int16(1), int16(1),
int16(4), int32(2), new byte[]{'1', '2'}, int32(2), new byte[]{-1, -7},
int32(4), int32(42), int32(-1), int16(0));
assertEquals(Arrays.asList("12", "-7", null, null), parameters(body, List.of(23, 21, 25, 20)));
}
@Test
void binaryValuesWithUnknownTypesOrWrongWidthsRemainUnknown() throws IOException {
byte[] body = concat(cString("p"), cString("s"), int16(1), int16(1), int16(3),
int32(3), new byte[]{0, 0, 1}, int32(4), int32(2), int32(4), int32(3), int16(0));
assertEquals(Arrays.asList(null, null, null), parameters(body, List.of(23, 0, 17)));
assertEquals(Arrays.asList(null, null, null), parameters(body, List.of()));
}
@Test
void truncatedBinaryIntegerLeavesTheBindUnknown() throws IOException {
byte[] body = concat(cString("p"), cString("s"), int16(1), int16(1), int16(1), int32(4), new byte[]{0, 1});
assertEquals(List.of(), parameters(body, List.of(23)));
}
@Test
void decodesOnlyCompleteBackendParameterDescriptions() throws IOException {
assertEquals(List.of(23, 20), PostgresWireDecoder.decodeParameterDescription(concat(int16(2), int32(23), int32(20))));
assertThrows(IOException.class, () -> PostgresWireDecoder.decodeParameterDescription(concat(int16(2), int32(23))));
assertThrows(IOException.class, () -> PostgresWireDecoder.decodeParameterDescription(concat(int16(0), int32(23))));
}
@Test
void decodesParseNameSqlAndParameterTypeOids() throws IOException {
byte[] packet = frame('P', concat(
@@ -107,6 +149,11 @@ class PostgresWireDecoderTest {
return decoder.decodeBindParameters(decoder.nextMessage());
}
private static List<String> parameters(byte[] body, List<Integer> typeOids) throws IOException {
PostgresWireDecoder decoder = new PostgresWireDecoder(new ByteArrayInputStream(frame('B', body)));
return decoder.decodeBindParameters(decoder.nextMessage(), typeOids);
}
private static PostgresWireDecoder.BindMessage decodeBind(byte[] body) throws IOException {
PostgresWireDecoder decoder = new PostgresWireDecoder(new ByteArrayInputStream(frame('B', body)));
return decoder.decodeBind(decoder.nextMessage());
@@ -11,6 +11,8 @@ import org.jboss.logging.Logger;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.mockito.Mockito;
import java.io.ByteArrayOutputStream;
@@ -22,6 +24,7 @@ import java.net.Socket;
import java.net.SocketTimeoutException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
@@ -550,9 +553,10 @@ class RedshiftInterceptingBridgeTest {
assertEquals('T', fromBridge.read());
}
@Test
@ParameterizedTest
@CsvSource({"false,23", "true,23", "true,0", "true,-1"})
@Timeout(20)
void preparesExternalQueriesWithoutValuesAtParseAndWithEachBindBeforeForwarding() throws Exception {
void preparesExternalQueriesWithoutValuesAtParseAndWithEachBindBeforeForwarding(boolean binary, int oid) throws Exception {
String sql = "SELECT SUM(amount) FROM lake.events WHERE day = $1";
SpectrumInterceptor interceptor = inlineMock(SpectrumInterceptor.class);
SpectrumQueryPreparation preparation = mock(SpectrumQueryPreparation.class);
@@ -562,7 +566,8 @@ class RedshiftInterceptingBridgeTest {
testClientEnd.setSoTimeout(5000);
testBackendEnd.setSoTimeout(5000);
InputStream response = testClientEnd.getInputStream();
byte[] parse = PostgresWireDecoder.encodeParse(new PostgresWireDecoder.ParseMessage("s", sql, List.of(23)), sql);
List<Integer> types = oid < 0 ? List.of() : List.of(oid);
byte[] parse = PostgresWireDecoder.encodeParse(new PostgresWireDecoder.ParseMessage("s", sql, types), sql);
testClientEnd.getOutputStream().write(parse);
testClientEnd.getOutputStream().flush();
@@ -573,13 +578,33 @@ class RedshiftInterceptingBridgeTest {
assertEquals('1', response.read());
skipFrameBody(response);
if (oid <= 0) {
byte[] describe = frame('D', concat(new byte[]{'S'}, cString("s")));
testClientEnd.getOutputStream().write(describe);
testClientEnd.getOutputStream().flush();
assertArrayEquals(describe, nextForwarded().toPacketBytes());
}
for (String day : List.of("1", "2")) {
byte[] value = day.getBytes(StandardCharsets.UTF_8);
byte[] bind = frame('B', concat(cString(""), cString("s"), new byte[]{0, 0, 0, 1},
byte[] value = binary ? ByteBuffer.allocate(4).putInt(Integer.parseInt(day)).array()
: day.getBytes(StandardCharsets.UTF_8);
byte[] formats = binary ? new byte[]{0, 1, 0, 1, 0, 1} : new byte[]{0, 0, 0, 1};
byte[] bind = frame('B', concat(cString(""), cString("s"), formats,
ByteBuffer.allocate(4).putInt(value.length).array(), value, new byte[]{0, 0}));
testClientEnd.getOutputStream().write(bind);
testClientEnd.getOutputStream().flush();
assertEquals('H', nextForwarded().type());
if (oid <= 0 && day.equals("1")) {
// Bind is already queued, but the backend has not yet replied to Describe.
byte[] description = frame('t', concat(new byte[]{0, 1}, ByteBuffer.allocate(4).putInt(23).array()));
testBackendEnd.getOutputStream().write(description, 0, 7);
testBackendEnd.getOutputStream().flush();
testBackendEnd.getOutputStream().write(description, 7, description.length - 7);
writeBackendFrame('n', new byte[0]);
assertArrayEquals(description, new PostgresWireDecoder(response).nextMessage().toPacketBytes());
assertEquals('n', response.read());
skipFrameBody(response);
}
assertArrayEquals(bind, nextForwarded().toPacketBytes());
verify(preparation).prepare(eq(sql), any(), any(), eq(List.of(day)));
writeBackendFrame('2', new byte[0]);
@@ -588,6 +613,32 @@ class RedshiftInterceptingBridgeTest {
}
verify(preparation, never()).prepare(anyString(), any(), any());
verify(preparation, times(3)).prepare(eq(sql), any(), any(), any());
if (binary && oid == 0) {
byte[] close = frame('C', concat(new byte[]{'S'}, cString("s")));
testClientEnd.getOutputStream().write(close);
testClientEnd.getOutputStream().flush();
assertArrayEquals(close, nextForwarded().toPacketBytes());
writeBackendFrame('3', new byte[0]);
assertEquals('3', response.read());
skipFrameBody(response);
testClientEnd.getOutputStream().write(parse);
testClientEnd.getOutputStream().flush();
assertEquals('H', nextForwarded().type());
assertArrayEquals(parse, nextForwarded().toPacketBytes());
writeBackendFrame('1', new byte[0]);
assertEquals('1', response.read());
skipFrameBody(response);
byte[] bind = frame('B', concat(cString(""), cString("s"), new byte[]{0, 1, 0, 1, 0, 1},
ByteBuffer.allocate(4).putInt(4).array(), ByteBuffer.allocate(4).putInt(3).array(), new byte[]{0, 0}));
testClientEnd.getOutputStream().write(bind);
testClientEnd.getOutputStream().flush();
assertEquals('H', nextForwarded().type());
assertArrayEquals(bind, nextForwarded().toPacketBytes());
verify(preparation).prepare(eq(sql), any(), any(), eq(Collections.singletonList(null)));
writeBackendFrame('2', new byte[0]);
assertEquals('2', response.read());
skipFrameBody(response);
}
}
@Test