Light, fluffy, and always free - The AWS Local Emulator alternative
fix(rds-data): fixing empty array response from statement (#2880)
Anders Wasen46283f320927
diff --git a/compatibility-tests/sdk-test-go/tests/rds_data_test.go b/compatibility-tests/sdk-test-go/tests/rds_data_test.goindex 435502bdd1..61fc230222 100644--- a/compatibility-tests/sdk-test-go/tests/rds_data_test.go+++ b/compatibility-tests/sdk-test-go/tests/rds_data_test.go@@ -131,6 +131,7 @@ func TestRdsDataApiGoSdkV1(t *testing.T) {})require.NoError(t, err)assert.Equal(t, int64(1), awsV1.Int64Value(insertOut.NumberOfRecordsUpdated))+ assert.Nil(t, insertOut.Records, "AWS omits records for a statement that returns no result set")inTxRead, err := dataSvc.ExecuteStatement(&rdsdataservice.ExecuteStatementInput{ResourceArn: awsV1.String(resourceArn),@@ -570,6 +571,7 @@ func TestRdsDataApiGoSdkPostgresV1(t *testing.T) {})require.NoError(t, err)assert.Equal(t, int64(1), awsV1.Int64Value(insertOut.NumberOfRecordsUpdated))+ assert.Nil(t, insertOut.Records, "AWS omits records for a statement that returns no result set")inTxRead, err := dataSvc.ExecuteStatement(&rdsdataservice.ExecuteStatementInput{ResourceArn: awsV1.String(resourceArn),@@ -989,6 +991,7 @@ func TestRdsDataApiGoSdkV2(t *testing.T) {})require.NoError(t, err)assert.Equal(t, int64(1), insertOut.NumberOfRecordsUpdated)+ assert.Nil(t, insertOut.Records, "AWS omits records for a statement that returns no result set")readInTx, err := dataSvc.ExecuteStatement(ctx, &rdsdata.ExecuteStatementInput{ResourceArn: awsV2.String(resourceArn),@@ -1242,6 +1245,7 @@ func TestRdsDataApiGoSdkPostgresV2(t *testing.T) {})require.NoError(t, err)assert.Equal(t, int64(1), insertOut.NumberOfRecordsUpdated)+ assert.Nil(t, insertOut.Records, "AWS omits records for a statement that returns no result set")readInTx, err := dataSvc.ExecuteStatement(ctx, &rdsdata.ExecuteStatementInput{ResourceArn: awsV2.String(resourceArn),diff --git a/docs/services/rds-data.md b/docs/services/rds-data.mdindex 32ca91a6cf..0f38db3e24 100644--- a/docs/services/rds-data.md+++ b/docs/services/rds-data.md@@ -35,7 +35,7 @@ The deprecated `ExecuteSql` operation is recognized at `POST /ExecuteSql` and re- Transaction requests validate `resourceArn` against the active transaction resource. Floci resolves accepted ARN aliases to the local resource before comparing transaction identity.- MySQL, MariaDB, and PostgreSQL resources are supported. Aurora PostgreSQL resources resolve to the same PostgreSQL execution path.- SQL is sent directly to the local database engine through JDBC. `SqlParameter` binding is supported for all engines: named `:placeholder` markers are rewritten to positional JDBC bind parameters and executed through a `PreparedStatement`. Colons inside string literals, quoted/backtick identifiers, comments, PostgreSQL `::` casts, and PostgreSQL dollar-quoted strings are left untouched. A placeholder used more than once binds its value at each position. Supported value variants are `stringValue`, `booleanValue`, `longValue`, `doubleValue`, `blobValue`, and `isNull`; `typeHint` values `DECIMAL`, `TIMESTAMP`, `DATE`, `TIME`, `UUID`, and `JSON` are honored. `arrayValue` parameters are not supported yet and return `BadRequestException`, as do malformed `parameters` payloads (not a JSON array, or an entry missing `name`).-- Result records include Data API field variants such as `stringValue`, `longValue`, `blobValue`, `booleanValue`, `doubleValue`, and `isNull`.+- Result records include Data API field variants such as `stringValue`, `longValue`, `blobValue`, `booleanValue`, `doubleValue`, and `isNull`. `records` is returned only for statements that produce a result set — empty when the query matched no rows — and is omitted entirely from the response to a statement that reports an update count, as AWS does. An `INSERT`, `UPDATE`, `DELETE`, or DDL statement therefore answers with `numberOfRecordsUpdated` and `generatedFields` and no `records` field at all.- `includeResultMetadata` returns the full AWS `ColumnMetadata` shape per column: `label`, `name`, `type`, `typeName`, `tableName`, `schemaName`, `nullable`, `precision`, `scale`, `isSigned`, `isCaseSensitive`, `isCurrency`, `isAutoIncrement`, and `arrayBaseColumnType`. `label` is the result-set label (the alias when the query aliases a column) and `name` is the underlying column name, falling back to the label for computed columns — clients that hydrate rows by `label` need it present. `type` is a JDBC type code and `typeName` is the engine's own type name (for example `4` and `int4` for a PostgreSQL integer), both taken from the driver so they match the engine actually serving the query. `arrayBaseColumnType` is always `0` because array columns are not mapped to `arrayValue` fields yet.- `ExecuteStatement` returns `generatedFields` for statements that report an update count on MySQL and MariaDB, carrying the auto-increment keys the engine generated. As on Aurora PostgreSQL, PostgreSQL resources always return an empty list — use a `RETURNING` clause to read generated values. `generatedFields` is omitted for statements that return a result set.- `BatchExecuteStatement` runs the statement once per entry in `parameterSets` through a JDBC batch and returns one `updateResults` entry per set, each carrying the `generatedFields` that set produced under the same engine rules as `ExecuteStatement` — a set inserting several rows reports one field per generated key. On MySQL and MariaDB the sets are executed one at a time so each entry owns its own keys, because `getGeneratedKeys()` after a JDBC batch reports every key the batch generated with nothing tying a key back to the set that produced it. It accepts `transactionId`, so a batch can take part in a Data API transaction; without one the batch commits automatically. An absent or empty `parameterSets` runs nothing and returns an empty `updateResults`: AWS runs the statement once per parameter set provided and points a caller who wants a single parameterless execution at one empty set (`[[]]`, which runs once here) or at `ExecuteStatement`. A statement that returns rows rather than an update count — a `SELECT`, or a CTE ending in one — is rejected with `BadRequestException` before any set runs, because a batch reports one `UpdateResult` per set and has nowhere to carry a result set, and AWS documents the operation as taking a DML statement. The check reads the driver's description of the prepared statement, so nothing executes first, and a DML statement that also reports rows through a PostgreSQL `RETURNING` clause is left alone: the batch still performs its writes, and `generatedFields` is empty on PostgreSQL anyway.diff --git a/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataService.java b/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataService.javaindex 55c6e648b6..147364c269 100644--- a/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataService.java+++ b/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataService.java@@ -410,6 +410,19 @@ public class RdsDataService implements Resettable {};}+ /**+ * The {@code ExecuteStatement} response for {@code statement}.+ *+ * <p>{@code records} and {@code generatedFields} are mutually exclusive on+ * the wire, and each is omitted rather than sent empty when it does not+ * apply. AWS answers a statement that produced a result set with+ * {@code records} — an empty array when the query matched no rows — and no+ * {@code generatedFields}; a statement that produced an update count gets+ * {@code generatedFields} and no {@code records} at all. Sending+ * {@code "records": []} for an update would make {@code hasRecords()} on+ * the AWS SDK models report true, and read as an empty result set to any+ * caller that tests the field for presence.+ */private ObjectNode buildResponse(Statement statement, boolean hasResultSet, boolean includeMetadata,DatabaseEngine engine) throws SQLException {ObjectNode response = objectMapper.createObjectNode();@@ -424,7 +437,6 @@ public class RdsDataService implements Resettable {response.put("numberOfRecordsUpdated", 0L);} else {int updateCount = statement.getUpdateCount();- response.set("records", objectMapper.createArrayNode());response.put("numberOfRecordsUpdated", Math.max(updateCount, 0));response.set("generatedFields", generatedFields(statement, engine));}diff --git a/src/test/java/io/github/hectorvent/floci/services/rdsdata/RdsDataServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/rdsdata/RdsDataServiceTest.javaindex 93581594d8..2e0763cbde 100644--- a/src/test/java/io/github/hectorvent/floci/services/rdsdata/RdsDataServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/rdsdata/RdsDataServiceTest.java@@ -127,6 +127,40 @@ class RdsDataServiceTest {assertTrue(generatedFields.get(0).get("longValue").asLong() > 0);}+ @Test+ void omitsRecordsForStatementsThatProduceNoResultSet() throws Exception {+ TestHarness harness = new TestHarness();+ harness.createTables();++ for (String sql : new String[] {+ "create table data_api_omit(id bigint primary key)",+ "insert into data_api_items(id, title) values ('omit', 'Omit')",+ "update data_api_items set title = 'Renamed' where id = 'omit'",+ "delete from data_api_items where id = 'omit'",+ "update data_api_items set title = 'Nobody' where id = 'no-such-row'"}) {+ ObjectNode response = harness.service.executeStatement(harness.request(sql), REGION);++ assertFalse(response.has("records"), sql);+ assertFalse(response.has("columnMetadata"), sql);+ assertTrue(response.has("generatedFields"), sql);+ assertTrue(response.has("numberOfRecordsUpdated"), sql);+ }+ }++ @Test+ void reportsEmptyRecordsForQueriesThatMatchNoRows() throws Exception {+ TestHarness harness = new TestHarness();+ harness.createTables();++ ObjectNode response = harness.service.executeStatement(+ harness.request("select title from data_api_items where id = 'no-such-row'"), REGION);++ ArrayNode records = (ArrayNode) response.get("records");+ assertTrue(records.isEmpty());+ assertEquals(0L, response.get("numberOfRecordsUpdated").asLong());+ assertFalse(response.has("generatedFields"));+ }+@Testvoid batchExecuteStatementRunsEveryParameterSet() throws Exception {TestHarness harness = new TestHarness();