mihaibudiu commented on code in PR #3668:
URL: https://github.com/apache/calcite/pull/3668#discussion_r2135328409


##########
core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java:
##########
@@ -1511,6 +1513,21 @@ public static SqlNode toSql(RexLiteral literal) {
       // Create a string without specifying a charset
       return SqlLiteral.createCharString((String) 
castNonNull(literal.getValue2()), POS);
     }
+    case GEOMETRY: {
+      Geometry geom = castNonNull(literal.getValueAs(Geometry.class));
+      switch (typeName) {
+      case CHAR:
+      case VARCHAR:
+        return SqlLiteral.createCharString(
+            SpatialTypeUtils.asEwkt(geom), POS);
+      case BINARY:
+      case VARBINARY:
+        return SqlLiteral.createBinaryString(
+            SpatialTypeUtils.asWkbArray(geom), POS);
+      default:
+        return SqlLiteral.createNull(POS);

Review Comment:
   why is this null?
   



##########
core/src/main/java/org/apache/calcite/rel/type/RelDataTypeFactory.java:
##########
@@ -65,6 +65,15 @@ public interface RelDataTypeFactory {
    */
   RelDataType createJavaType(Class clazz);
 
+  /**
+   * Creates a type that corresponds to a Java class, with a given family.
+   *
+   * @param clazz the Java class used to define the type
+   * @param family The family of the type, or null to infer

Review Comment:
   type families are a very confusing concept in Calcite, I think they should 
be deprecated.
   



##########
core/src/test/java/org/apache/calcite/util/PostgisGeometryDecoderTest.java:
##########
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * 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.
+ */
+package org.apache.calcite.util;
+
+import org.junit.jupiter.api.Test;
+import org.locationtech.jts.geom.Geometry;
+import org.locationtech.jts.geom.Point;
+import org.locationtech.jts.io.WKTWriter;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Tests for the {@link PostgisGeometryDecoder} class. These tests are based
+ * on the values retrieved from PostGIS using the JDBC driver for PostgreSQL.
+ */
+class PostgisGeometryDecoderTest {
+
+  private static final String POINT = 
"0101000020E6100000000000000000F03F000000"
+      + "0000000040";
+
+  private static final String LINESTRING = 
"0102000020E610000003000000000000000"
+      + 
"00000000000000000000000000000000000F03F000000000000F03F000000000000004"
+      + "00000000000000040";
+
+  private static final String POLYGON = 
"0103000020E610000002000000050000000000"
+      + 
"0000000000000000000000000000000000000000F03F00000000000000000000000000"
+      + 
"00F03F000000000000F03F0000000000000000000000000000F03F0000000000000000"
+      + 
"000000000000000005000000000000000000E03F000000000000E03F000000000000E0"
+      + 
"3F333333333333E33F333333333333E33F333333333333E33F333333333333E33F0000"
+      + "00000000E03F000000000000E03F000000000000E03F";
+
+  private static final String MULTIPOINT = 
"0104000020E610000002000000010100000"
+      + 
"0000000000000000000000000000000000101000000000000000000F03F00000000000"
+      + "0F03F";
+
+  private static final String MULTILINESTRING = 
"0105000020E6100000020000000102"
+      + 
"0000000200000000000000000000000000000000000000000000000000F03F00000000"
+      + 
"0000F03F01020000000200000000000000000000400000000000000040000000000000"
+      + "08400000000000000840";
+
+  private static final String MULTIPOLYGON = 
"0106000020E6100000020000000103000"
+      + 
"000010000000500000000000000000000000000000000000000000000000000F03F000"
+      + 
"0000000000000000000000000F03F000000000000F03F0000000000000000000000000"
+      + 
"000F03F000000000000000000000000000000000103000000010000000500000000000"
+      + 
"0000000004000000000000000400000000000000840000000000000004000000000000"
+      + 
"0084000000000000008400000000000000040000000000000084000000000000000400"
+      + "000000000000040";
+
+  private static final String GEOMETRYCOLLECTION = 
"0107000020E6100000020000000"
+      + 
"1010000000000000000000000000000000000000001020000000200000000000000000"
+      + "0F03F000000000000F03F00000000000000400000000000000040";
+
+  @Test void decodeNull() {
+    Geometry geometry = PostgisGeometryDecoder.decode((String) null);
+    assertEquals(null, geometry);
+  }
+
+  @Test void decodePoint() {
+    Geometry geometry = PostgisGeometryDecoder.decode(POINT);
+    assertTrue(geometry instanceof Point);
+    assertEquals("POINT (1 2)", new WKTWriter().write(geometry));
+  }
+
+  @Test void decodeLineString() {
+    Geometry geometry = PostgisGeometryDecoder.decode(LINESTRING);
+    assertTrue(geometry instanceof org.locationtech.jts.geom.LineString);
+    assertEquals("LINESTRING (0 0, 1 1, 2 2)", new 
WKTWriter().write(geometry));
+  }
+
+  @Test void decodePolygon() {
+    Geometry geometry = PostgisGeometryDecoder.decode(POLYGON);
+    assertTrue(geometry instanceof org.locationtech.jts.geom.Polygon);
+    assertEquals(
+        "POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0), (0.5 0.5, 0.5 0.6, 0.6 0.6, 0.6 
0.5, 0.5 0.5))",
+        new WKTWriter().write(geometry));
+  }
+
+  @Test void decodeMultiPoint() {
+    Geometry geometry = PostgisGeometryDecoder.decode(MULTIPOINT);
+    assertTrue(geometry instanceof org.locationtech.jts.geom.MultiPoint);
+    assertEquals(
+        "MULTIPOINT ((0 0), (1 1))",
+        new WKTWriter().write(geometry));
+  }
+
+  @Test void decodeMultiLineString() {
+    Geometry geometry = PostgisGeometryDecoder.decode(MULTILINESTRING);
+    assertTrue(geometry instanceof org.locationtech.jts.geom.MultiLineString);
+    assertEquals(
+        "MULTILINESTRING ((0 0, 1 1), (2 2, 3 3))",
+        new WKTWriter().write(geometry));
+  }
+
+  @Test void decodeMultiPolygon() {
+    Geometry geometry = PostgisGeometryDecoder.decode(MULTIPOLYGON);
+    assertTrue(geometry instanceof org.locationtech.jts.geom.MultiPolygon);
+    assertEquals(
+        "MULTIPOLYGON (((0 0, 1 0, 1 1, 0 1, 0 0)), ((2 2, 3 2, 3 3, 2 3, 2 
2)))",
+        new WKTWriter().write(geometry));
+  }
+
+  @Test void decodeGeometryCollection() {

Review Comment:
   how about a few negative tests, where the decoding cannot happen?



##########
core/src/main/java/org/apache/calcite/rel/type/RelDataTypeFactory.java:
##########
@@ -65,6 +65,15 @@ public interface RelDataTypeFactory {
    */
   RelDataType createJavaType(Class clazz);
 
+  /**
+   * Creates a type that corresponds to a Java class, with a given family.
+   *
+   * @param clazz the Java class used to define the type
+   * @param family The family of the type, or null to infer

Review Comment:
   but perhaps this PR is not the right place to have this conversation.



##########
core/src/main/java/org/apache/calcite/sql/type/SqlTypeAssignmentRule.java:
##########
@@ -193,6 +193,8 @@ private SqlTypeAssignmentRule(
     rule.add(SqlTypeName.GEOMETRY);
     rule.add(SqlTypeName.CHAR);
     rule.add(SqlTypeName.VARCHAR);
+    rule.add(SqlTypeName.BINARY);

Review Comment:
   these (and a few other similar changes) look like bug fixes that would be 
nice to have even before we can merge the full PR.  



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to