This is an automated email from the ASF dual-hosted git repository.

amashenkov pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/ignite-3.git


The following commit(s) were added to refs/heads/main by this push:
     new 6351e34c5b IGNITE-22393 Fix incorrect BinaryMode in FieldAccessor for 
column data (#3892)
6351e34c5b is described below

commit 6351e34c5baa320c27187252cae90c50286894d7
Author: Andrew V. Mashenkov <[email protected]>
AuthorDate: Mon Jun 10 15:20:27 2024 +0300

    IGNITE-22393 Fix incorrect BinaryMode in FieldAccessor for column data 
(#3892)
---
 .../org/apache/ignite/table/mapper/Mapper.java     | 14 -------------
 .../ignite/internal/marshaller/FieldAccessor.java  | 21 +++++++++++++++-----
 .../schema/marshaller/KvMarshallerTest.java        | 23 +++++++++++++++++-----
 .../schema/marshaller/RecordMarshallerTest.java    | 22 +++++++++++++++++----
 4 files changed, 52 insertions(+), 28 deletions(-)

diff --git 
a/modules/api/src/main/java/org/apache/ignite/table/mapper/Mapper.java 
b/modules/api/src/main/java/org/apache/ignite/table/mapper/Mapper.java
index 7ed141e7a1..64800ef068 100644
--- a/modules/api/src/main/java/org/apache/ignite/table/mapper/Mapper.java
+++ b/modules/api/src/main/java/org/apache/ignite/table/mapper/Mapper.java
@@ -24,8 +24,6 @@ import java.time.LocalTime;
 import java.util.BitSet;
 import java.util.Objects;
 import java.util.UUID;
-import java.util.function.Function;
-import org.apache.ignite.table.Tuple;
 
 /**
  * Mapper interface defines marshaller methods for mapping class field names 
to table columns.
@@ -116,18 +114,6 @@ public interface Mapper<T> {
         return builder(type).map(Objects.requireNonNull(fieldName), 
Objects.requireNonNull(columnName), fieldColumnPairs).build();
     }
 
-    /**
-     * Adds a manual functional mapping for an object and a row represented by 
a tuple.
-     *
-     * @param objectToRow Object to tuple function.
-     * @param rowToObject Tuple to object function.
-     * @return {@code this} for chaining.
-     */
-    static <O> Mapper<O> of(Function<O, Tuple> objectToRow, Function<Tuple, O> 
rowToObject) {
-        // TODO: implement custom user mapping 
https://issues.apache.org/jira/browse/IGNITE-16116
-        throw new UnsupportedOperationException("Not implemented yet.");
-    }
-
     /**
      * Creates a mapper builder for objects of a given class.
      *
diff --git 
a/modules/marshaller-common/src/main/java/org/apache/ignite/internal/marshaller/FieldAccessor.java
 
b/modules/marshaller-common/src/main/java/org/apache/ignite/internal/marshaller/FieldAccessor.java
index 8340ac4be1..bec0e86ccf 100644
--- 
a/modules/marshaller-common/src/main/java/org/apache/ignite/internal/marshaller/FieldAccessor.java
+++ 
b/modules/marshaller-common/src/main/java/org/apache/ignite/internal/marshaller/FieldAccessor.java
@@ -76,14 +76,14 @@ abstract class FieldAccessor {
                 validateColumnType(col, field.getType());
             }
 
-            BinaryMode mode = BinaryMode.forClass(field.getType());
+            BinaryMode fieldAccessMode = BinaryMode.forClass(field.getType());
             MethodHandles.Lookup lookup = MethodHandles.privateLookupIn(type, 
MethodHandles.lookup());
 
             VarHandle varHandle = lookup.unreflectVarHandle(field);
 
-            assert mode != null : "Invalid mode for type: " + field.getType();
+            assert fieldAccessMode != null : "Invalid fieldAccessMode for 
type: " + field.getType();
 
-            switch (mode) {
+            switch (fieldAccessMode) {
                 case P_BOOLEAN:
                     return new BooleanPrimitiveAccessor(varHandle, colIdx);
 
@@ -123,10 +123,10 @@ abstract class FieldAccessor {
                 case DATETIME:
                 case TIMESTAMP:
                 case POJO:
-                    return new ReferenceFieldAccessor(varHandle, colIdx, mode, 
col.scale(), typeConverter);
+                    return new ReferenceFieldAccessor(varHandle, colIdx, 
col.type(), col.scale(), typeConverter);
 
                 default:
-                    assert false : "Invalid mode " + mode;
+                    assert false : "Invalid field access mode " + 
fieldAccessMode;
             }
 
             throw new IllegalArgumentException("Failed to create accessor for 
field [name=" + field.getName() + ']');
@@ -820,5 +820,16 @@ abstract class FieldAccessor {
                 throw new IllegalArgumentException(e);
             }
         }
+
+        @Override
+        Object value(Object obj) {
+            Object value = get(Objects.requireNonNull(obj));
+
+            try {
+                return typeConverter == null ? value : 
typeConverter.toColumnType(value);
+            } catch (Exception e) {
+                throw new IllegalArgumentException(e);
+            }
+        }
     }
 }
diff --git 
a/modules/schema/src/test/java/org/apache/ignite/internal/schema/marshaller/KvMarshallerTest.java
 
b/modules/schema/src/test/java/org/apache/ignite/internal/schema/marshaller/KvMarshallerTest.java
index 1e9ba96c4a..4cc5e820d3 100644
--- 
a/modules/schema/src/test/java/org/apache/ignite/internal/schema/marshaller/KvMarshallerTest.java
+++ 
b/modules/schema/src/test/java/org/apache/ignite/internal/schema/marshaller/KvMarshallerTest.java
@@ -96,6 +96,7 @@ import org.apache.ignite.internal.type.NativeTypeSpec;
 import org.apache.ignite.internal.type.NativeTypes;
 import org.apache.ignite.internal.util.ObjectFactory;
 import org.apache.ignite.table.mapper.Mapper;
+import org.apache.ignite.table.mapper.TypeConverter;
 import org.junit.jupiter.api.Assumptions;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.DynamicNode;
@@ -283,7 +284,7 @@ public class KvMarshallerTest {
 
         Mapper<TestObject> valMapper = Mapper.builder(TestObject.class)
                 .map("longCol", "col1")
-                .map("stringCol", "col3")
+                .map("dateCol", "col3", new TestTypeConverter())
                 .build();
 
         KvMarshaller<TestKeyObject, TestObject> marshaller = 
factory.create(schema, keyMapper, valMapper);
@@ -937,7 +938,7 @@ public class KvMarshallerTest {
 
             obj.longCol = rnd.nextLong();
             obj.longCol2 = rnd.nextLong();
-            obj.stringCol = IgniteTestUtils.randomString(rnd, 100);
+            obj.dateCol = LocalDate.now();
 
             return obj;
         }
@@ -946,7 +947,7 @@ public class KvMarshallerTest {
 
         private Long longCol2;
 
-        private String stringCol;
+        private LocalDate dateCol;
 
         @Override
         public boolean equals(Object o) {
@@ -962,12 +963,12 @@ public class KvMarshallerTest {
 
             return longCol == that.longCol
                     && Objects.equals(longCol2, that.longCol2)
-                    && Objects.equals(stringCol, that.stringCol);
+                    && Objects.equals(dateCol, that.dateCol);
         }
 
         @Override
         public int hashCode() {
-            return Objects.hash(longCol, longCol2, stringCol);
+            return Objects.hash(longCol, longCol2, dateCol);
         }
     }
 
@@ -1150,4 +1151,16 @@ public class KvMarshallerTest {
             return result;
         }
     }
+
+    private static class TestTypeConverter implements TypeConverter<LocalDate, 
String> {
+        @Override
+        public String toColumnType(LocalDate val) {
+            return val.toString();
+        }
+
+        @Override
+        public LocalDate toObjectType(String val) {
+            return LocalDate.parse(val);
+        }
+    }
 }
diff --git 
a/modules/schema/src/test/java/org/apache/ignite/internal/schema/marshaller/RecordMarshallerTest.java
 
b/modules/schema/src/test/java/org/apache/ignite/internal/schema/marshaller/RecordMarshallerTest.java
index 2520d5599f..beac708ecc 100644
--- 
a/modules/schema/src/test/java/org/apache/ignite/internal/schema/marshaller/RecordMarshallerTest.java
+++ 
b/modules/schema/src/test/java/org/apache/ignite/internal/schema/marshaller/RecordMarshallerTest.java
@@ -48,6 +48,7 @@ import com.facebook.presto.bytecode.ParameterizedType;
 import com.facebook.presto.bytecode.Variable;
 import com.facebook.presto.bytecode.expression.BytecodeExpressions;
 import java.lang.reflect.Field;
+import java.time.LocalDate;
 import java.util.Arrays;
 import java.util.EnumSet;
 import java.util.List;
@@ -72,6 +73,7 @@ import 
org.apache.ignite.internal.testframework.IgniteTestUtils;
 import org.apache.ignite.internal.type.NativeTypes;
 import org.apache.ignite.internal.util.ObjectFactory;
 import org.apache.ignite.table.mapper.Mapper;
+import org.apache.ignite.table.mapper.TypeConverter;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.params.ParameterizedTest;
@@ -180,7 +182,7 @@ public class RecordMarshallerTest {
         Mapper<TestObject> mapper = Mapper.builder(TestObject.class)
                 .map("id", "key")
                 .map("intCol", "col1")
-                .map("stringCol", "col3")
+                .map("dateCol", "col3", new TestTypeConverter())
                 .build();
 
         RecordMarshaller<TestObject> marshaller = factory.create(schema, 
mapper);
@@ -442,7 +444,7 @@ public class RecordMarshallerTest {
 
         private Long longCol2;
 
-        private String stringCol;
+        private LocalDate dateCol;
 
         static TestObject randomObject(Random rnd) {
             final TestObject obj = new TestObject();
@@ -450,7 +452,7 @@ public class RecordMarshallerTest {
             obj.id = rnd.nextLong();
             obj.intCol = rnd.nextInt();
             obj.longCol2 = rnd.nextLong();
-            obj.stringCol = IgniteTestUtils.randomString(rnd, 100);
+            obj.dateCol = LocalDate.now();
 
             return obj;
         }
@@ -470,7 +472,7 @@ public class RecordMarshallerTest {
             return id == that.id
                     && intCol == that.intCol
                     && Objects.equals(longCol2, that.longCol2)
-                    && Objects.equals(stringCol, that.stringCol);
+                    && Objects.equals(dateCol, that.dateCol);
         }
 
         @Override
@@ -582,4 +584,16 @@ public class RecordMarshallerTest {
             return Objects.hash(primLongCol);
         }
     }
+
+    private static class TestTypeConverter implements TypeConverter<LocalDate, 
String> {
+        @Override
+        public String toColumnType(LocalDate val) {
+            return val.toString();
+        }
+
+        @Override
+        public LocalDate toObjectType(String val) {
+            return LocalDate.parse(val);
+        }
+    }
 }

Reply via email to