From 2530b6d2b17fbba23fb6e9a7fd283ea932376939 Mon Sep 17 00:00:00 2001 From: bmg13 Date: Fri, 23 Aug 2024 16:09:20 +0100 Subject: [PATCH 1/4] Support filter expressions for numberic types (double, float and long). --- .../sql/translation/JsonFieldTranslator.java | 16 +++++++++ .../translation/JsonFieldTranslatorTest.java | 33 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/extensions/common/sql/sql-core/src/main/java/org/eclipse/edc/sql/translation/JsonFieldTranslator.java b/extensions/common/sql/sql-core/src/main/java/org/eclipse/edc/sql/translation/JsonFieldTranslator.java index b250fc8b750..941565e57b3 100644 --- a/extensions/common/sql/sql-core/src/main/java/org/eclipse/edc/sql/translation/JsonFieldTranslator.java +++ b/extensions/common/sql/sql-core/src/main/java/org/eclipse/edc/sql/translation/JsonFieldTranslator.java @@ -44,6 +44,10 @@ public String getLeftOperand(List path, Class type) { statementBuilder.append(" ->> '%s'".formatted(path.get(length - 1))); var statement = statementBuilder.toString(); + return checkStatementByType(type, statement); + } + + private String checkStatementByType(Class type, String statement) { if (type.equals(Boolean.class)) { return format("(%s)::boolean", statement); } @@ -52,6 +56,18 @@ public String getLeftOperand(List path, Class type) { return format("(%s)::integer", statement); } + if (type.equals(Double.class)) { + return format("(%s)::double", statement); + } + + if (type.equals(Float.class)) { + return format("(%s)::float", statement); + } + + if (type.equals(Long.class)) { + return format("(%s)::long", statement); + } + return statement; } diff --git a/extensions/common/sql/sql-core/src/test/java/org/eclipse/edc/sql/translation/JsonFieldTranslatorTest.java b/extensions/common/sql/sql-core/src/test/java/org/eclipse/edc/sql/translation/JsonFieldTranslatorTest.java index a22750f078e..8a03770a9bd 100644 --- a/extensions/common/sql/sql-core/src/test/java/org/eclipse/edc/sql/translation/JsonFieldTranslatorTest.java +++ b/extensions/common/sql/sql-core/src/test/java/org/eclipse/edc/sql/translation/JsonFieldTranslatorTest.java @@ -97,6 +97,39 @@ void shouldParseWhereClause_whenRightOperandIsInteger() { assertThat(result.parameters()).containsExactly(100); } + @Test + void shouldParseWhereClause_whenRightOperandIsDouble() { + var operator = new SqlOperator("=", Object.class); + var criterion = criterion("json.field", "=", 100.0); + + var result = translator.toWhereClause(PathItem.parse("field"), criterion, operator); + + assertThat(result.sql()).isEqualTo("(column_name ->> 'field')::double = ?"); + assertThat(result.parameters()).containsExactly(100.0); + } + + @Test + void shouldParseWhereClause_whenRightOperandIsFloat() { + var operator = new SqlOperator("=", Object.class); + var criterion = criterion("json.field", "=", 100.0F); + + var result = translator.toWhereClause(PathItem.parse("field"), criterion, operator); + + assertThat(result.sql()).isEqualTo("(column_name ->> 'field')::float = ?"); + assertThat(result.parameters()).containsExactly(100.0F); + } + + @Test + void shouldParseWhereClause_whenRightOperandIsLong() { + var operator = new SqlOperator("=", Object.class); + var criterion = criterion("json.field", "=", 100L); + + var result = translator.toWhereClause(PathItem.parse("field"), criterion, operator); + + assertThat(result.sql()).isEqualTo("(column_name ->> 'field')::long = ?"); + assertThat(result.parameters()).containsExactly(100L); + } + @Test void shouldConvertToJsonB_whenOperatorIsContains() { var operator = new SqlOperator("??", Object.class); From 0fad11b7c969f6165ec089753596ebff5f8576ab Mon Sep 17 00:00:00 2001 From: bmg13 Date: Mon, 26 Aug 2024 11:21:50 +0100 Subject: [PATCH 2/4] Support filter expressions for more java primitive types. --- .../sql/translation/JsonFieldTranslator.java | 39 +++++++++++-------- .../translation/JsonFieldTranslatorTest.java | 22 +++++++++++ 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/extensions/common/sql/sql-core/src/main/java/org/eclipse/edc/sql/translation/JsonFieldTranslator.java b/extensions/common/sql/sql-core/src/main/java/org/eclipse/edc/sql/translation/JsonFieldTranslator.java index 941565e57b3..a4fb7a5cca8 100644 --- a/extensions/common/sql/sql-core/src/main/java/org/eclipse/edc/sql/translation/JsonFieldTranslator.java +++ b/extensions/common/sql/sql-core/src/main/java/org/eclipse/edc/sql/translation/JsonFieldTranslator.java @@ -47,6 +47,22 @@ public String getLeftOperand(List path, Class type) { return checkStatementByType(type, statement); } + @Override + public WhereClause toWhereClause(List path, Criterion criterion, SqlOperator operator) { + var leftOperand = getLeftOperand(path, criterion.getOperandRight().getClass()); + + var amendedLeftOperand = Optional.of(leftOperand) + .filter(it -> operator.representation().equals("??")) + .map(it -> it.replace("->>", "->")) + .map("(%s)::jsonb"::formatted) + .orElse(leftOperand); + + return new WhereClause( + "%s %s %s".formatted(amendedLeftOperand, operator.representation(), toValuePlaceholder(criterion)), + toParameters(criterion) + ); + } + private String checkStatementByType(Class type, String statement) { if (type.equals(Boolean.class)) { return format("(%s)::boolean", statement); @@ -68,23 +84,14 @@ private String checkStatementByType(Class type, String statement) { return format("(%s)::long", statement); } - return statement; - } - - @Override - public WhereClause toWhereClause(List path, Criterion criterion, SqlOperator operator) { - var leftOperand = getLeftOperand(path, criterion.getOperandRight().getClass()); + if (type.equals(Byte.class)) { + return format("(%s)::byte", statement); + } - var amendedLeftOperand = Optional.of(leftOperand) - .filter(it -> operator.representation().equals("??")) - .map(it -> it.replace("->>", "->")) - .map("(%s)::jsonb"::formatted) - .orElse(leftOperand); + if (type.equals(Short.class)) { + return format("(%s)::short", statement); + } - return new WhereClause( - "%s %s %s".formatted(amendedLeftOperand, operator.representation(), toValuePlaceholder(criterion)), - toParameters(criterion) - ); + return statement; } - } diff --git a/extensions/common/sql/sql-core/src/test/java/org/eclipse/edc/sql/translation/JsonFieldTranslatorTest.java b/extensions/common/sql/sql-core/src/test/java/org/eclipse/edc/sql/translation/JsonFieldTranslatorTest.java index 8a03770a9bd..65de74baee1 100644 --- a/extensions/common/sql/sql-core/src/test/java/org/eclipse/edc/sql/translation/JsonFieldTranslatorTest.java +++ b/extensions/common/sql/sql-core/src/test/java/org/eclipse/edc/sql/translation/JsonFieldTranslatorTest.java @@ -130,6 +130,28 @@ void shouldParseWhereClause_whenRightOperandIsLong() { assertThat(result.parameters()).containsExactly(100L); } + @Test + void shouldParseWhereClause_whenRightOperandIsByte() { + var operator = new SqlOperator("=", Object.class); + var criterion = criterion("json.field", "=", (byte) 1); + + var result = translator.toWhereClause(PathItem.parse("field"), criterion, operator); + + assertThat(result.sql()).isEqualTo("(column_name ->> 'field')::byte = ?"); + assertThat(result.parameters()).containsExactly((byte) 1); + } + + @Test + void shouldParseWhereClause_whenRightOperandIsShort() { + var operator = new SqlOperator("=", Object.class); + var criterion = criterion("json.field", "=", (short) 1); + + var result = translator.toWhereClause(PathItem.parse("field"), criterion, operator); + + assertThat(result.sql()).isEqualTo("(column_name ->> 'field')::short = ?"); + assertThat(result.parameters()).containsExactly((short) 1); + } + @Test void shouldConvertToJsonB_whenOperatorIsContains() { var operator = new SqlOperator("??", Object.class); From 69aa80817ee65f51b0f93284e7093ca715db5c77 Mon Sep 17 00:00:00 2001 From: bmg13 Date: Mon, 26 Aug 2024 11:39:44 +0100 Subject: [PATCH 3/4] Remove Byte Type Cast Support. --- .../edc/sql/translation/JsonFieldTranslator.java | 4 ---- .../edc/sql/translation/JsonFieldTranslatorTest.java | 11 ----------- 2 files changed, 15 deletions(-) diff --git a/extensions/common/sql/sql-core/src/main/java/org/eclipse/edc/sql/translation/JsonFieldTranslator.java b/extensions/common/sql/sql-core/src/main/java/org/eclipse/edc/sql/translation/JsonFieldTranslator.java index a4fb7a5cca8..726ea8b3f8f 100644 --- a/extensions/common/sql/sql-core/src/main/java/org/eclipse/edc/sql/translation/JsonFieldTranslator.java +++ b/extensions/common/sql/sql-core/src/main/java/org/eclipse/edc/sql/translation/JsonFieldTranslator.java @@ -84,10 +84,6 @@ private String checkStatementByType(Class type, String statement) { return format("(%s)::long", statement); } - if (type.equals(Byte.class)) { - return format("(%s)::byte", statement); - } - if (type.equals(Short.class)) { return format("(%s)::short", statement); } diff --git a/extensions/common/sql/sql-core/src/test/java/org/eclipse/edc/sql/translation/JsonFieldTranslatorTest.java b/extensions/common/sql/sql-core/src/test/java/org/eclipse/edc/sql/translation/JsonFieldTranslatorTest.java index 65de74baee1..5f39d4a5bf2 100644 --- a/extensions/common/sql/sql-core/src/test/java/org/eclipse/edc/sql/translation/JsonFieldTranslatorTest.java +++ b/extensions/common/sql/sql-core/src/test/java/org/eclipse/edc/sql/translation/JsonFieldTranslatorTest.java @@ -130,17 +130,6 @@ void shouldParseWhereClause_whenRightOperandIsLong() { assertThat(result.parameters()).containsExactly(100L); } - @Test - void shouldParseWhereClause_whenRightOperandIsByte() { - var operator = new SqlOperator("=", Object.class); - var criterion = criterion("json.field", "=", (byte) 1); - - var result = translator.toWhereClause(PathItem.parse("field"), criterion, operator); - - assertThat(result.sql()).isEqualTo("(column_name ->> 'field')::byte = ?"); - assertThat(result.parameters()).containsExactly((byte) 1); - } - @Test void shouldParseWhereClause_whenRightOperandIsShort() { var operator = new SqlOperator("=", Object.class); From 8196e469c2dd48874989ac12188bbcb6757909cc Mon Sep 17 00:00:00 2001 From: bmg13 Date: Mon, 26 Aug 2024 13:38:35 +0100 Subject: [PATCH 4/4] Changes from PR. --- .../org/eclipse/edc/sql/translation/JsonFieldTranslator.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extensions/common/sql/sql-core/src/main/java/org/eclipse/edc/sql/translation/JsonFieldTranslator.java b/extensions/common/sql/sql-core/src/main/java/org/eclipse/edc/sql/translation/JsonFieldTranslator.java index 726ea8b3f8f..ae1ec6803ed 100644 --- a/extensions/common/sql/sql-core/src/main/java/org/eclipse/edc/sql/translation/JsonFieldTranslator.java +++ b/extensions/common/sql/sql-core/src/main/java/org/eclipse/edc/sql/translation/JsonFieldTranslator.java @@ -44,7 +44,7 @@ public String getLeftOperand(List path, Class type) { statementBuilder.append(" ->> '%s'".formatted(path.get(length - 1))); var statement = statementBuilder.toString(); - return checkStatementByType(type, statement); + return createStatementForType(type, statement); } @Override @@ -63,7 +63,7 @@ public WhereClause toWhereClause(List path, Criterion criterion, SqlOp ); } - private String checkStatementByType(Class type, String statement) { + private String createStatementForType(Class type, String statement) { if (type.equals(Boolean.class)) { return format("(%s)::boolean", statement); }