Skip to content

Add enum column support to relational server #3074

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

Merged
merged 3 commits into from
Feb 11, 2025
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
2 changes: 1 addition & 1 deletion docs/ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Users performing online updates are encouraged to update from [4.0.559.4](#40559
* **Performance** Improvement 4 [(Issue #NNN)](https://github.com/FoundationDB/fdb-record-layer/issues/NNN)
* **Performance** Improvement 5 [(Issue #NNN)](https://github.com/FoundationDB/fdb-record-layer/issues/NNN)
* **Feature** Feature 1 [(Issue #NNN)](https://github.com/FoundationDB/fdb-record-layer/issues/NNN)
* **Feature** Feature 2 [(Issue #NNN)](https://github.com/FoundationDB/fdb-record-layer/issues/NNN)
* **Feature** Add enum column support to relational server [(Issue #3073)](https://github.com/FoundationDB/fdb-record-layer/issues/3073)
* **Feature** Feature 3 [(Issue #NNN)](https://github.com/FoundationDB/fdb-record-layer/issues/NNN)
* **Feature** Feature 4 [(Issue #NNN)](https://github.com/FoundationDB/fdb-record-layer/issues/NNN)
* **Feature** Feature 5 [(Issue #NNN)](https://github.com/FoundationDB/fdb-record-layer/issues/NNN)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,11 @@ public Object getObject(int oneBasedColumn) throws SQLException {
int index = PositionalIndex.toProtobuf(oneBasedColumn);
Column column = this.delegate.getRow(rowIndex).getColumns().getColumn(index);
return column == null || !column.hasBinary() ? null : column.getBinary().toByteArray();
case Types.OTHER:
// Probably an enum, it's not clear exactly how we should handle this, but we currently only have one
// thing which appears as OTHER
o = getString(oneBasedColumn);
Comment on lines +324 to +326
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is mostly in line as to what we have in other parts of the system (in DirectAccessAPI etc.) but we should formally decide as to how we want to treat our ENUMs. I think most of the DBs actually map it to either a STRING or INTEGER externally and do an implicit cast internally, but they loose some bits of type info in the way to do so.

break;
default:
throw new SQLException("Unsupported type " + type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,21 @@ private static Column toColumn(RelationalStruct relationalStruct, int oneBasedIn
column = toColumn(relationalStruct.wasNull() ? null : d,
(a, b) -> a == null ? b.clearDouble() : b.setDouble(a));
break;
case Types.OTHER:
final Object object = relationalStruct.getObject(oneBasedIndex);
if (object instanceof String) {
// an enum. Enums are effectively just strings with a constraint, but this is how some other
// databases support them.
column = toColumn(relationalStruct.wasNull() ? null : (String) object,
(value, protobuf) -> value == null ? protobuf.clearString() : protobuf.setString(value));
break;
}
if (object == null) {
column = toColumn(null, (value, protobuf) -> protobuf.clearString());
break;
}
throw new SQLException("java.sql.Type=" + columnType + " not supported with " + object.getClass(),
ErrorCode.UNSUPPORTED_OPERATION.getErrorCode());
default:
throw new SQLException("java.sql.Type=" + columnType + " not supported",
ErrorCode.UNSUPPORTED_OPERATION.getErrorCode());
Expand Down
4 changes: 0 additions & 4 deletions yaml-tests/src/test/java/YamlIntegrationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,6 @@ void arrays(YamlTest.Runner runner) throws Exception {
}

@TestTemplate
@ExcludeYamlTestConfig(
value = YamlTestConfigFilters.DO_NOT_USE_JDBC,
reason = "JDBC does not support enums")
public void insertEnum(YamlTest.Runner runner) throws Exception {
runner.runYamsql("insert-enum.yamsql");
}
Expand Down Expand Up @@ -303,7 +300,6 @@ public void recursiveCte(YamlTest.Runner runner) throws Exception {
}

@TestTemplate
@ExcludeYamlTestConfig(value = YamlTestConfigFilters.DO_NOT_USE_JDBC, reason = "JDBC does not support enums")
public void enumTest(YamlTest.Runner runner) throws Exception {
runner.runYamsql("enum.yamsql");
}
Expand Down
4 changes: 3 additions & 1 deletion yaml-tests/src/test/resources/enum.yamsql
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

---
options:
supported_version: !current_version
---
schema_template:
CREATE TYPE AS ENUM MOOD ( 'JOYFUL', 'HAPPY', 'RELAXED', 'INDIFFERENT', 'CONFUSED', 'SAD', 'ANXIOUS', 'ANGRY' )
Expand Down
4 changes: 3 additions & 1 deletion yaml-tests/src/test/resources/insert-enum.yamsql
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

---
options:
supported_version: !current_version
---
schema_template:
CREATE TYPE AS ENUM "WHATEVER" ( 'OWNING', 'WEAK', 'VALIDATING' )
Expand Down
Loading