Skip to content

Commit

Permalink
[CALCITE-6239] Add a postgis dialect that supports ST functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bchapuis committed Feb 2, 2024
1 parent 2aabf21 commit 4869424
Show file tree
Hide file tree
Showing 9 changed files with 649 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.apache.calcite.rex.RexUtil;
import org.apache.calcite.rex.RexWindow;
import org.apache.calcite.rex.RexWindowBound;
import org.apache.calcite.runtime.SpatialTypeUtils;
import org.apache.calcite.sql.JoinType;
import org.apache.calcite.sql.SqlAggFunction;
import org.apache.calcite.sql.SqlBasicCall;
Expand Down Expand Up @@ -112,6 +113,7 @@

import org.checkerframework.checker.initialization.qual.UnknownInitialization;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.locationtech.jts.geom.Geometry;

import java.math.BigDecimal;
import java.util.AbstractList;
Expand Down Expand Up @@ -1445,6 +1447,10 @@ public static SqlNode toSql(RexLiteral literal) {
// Create a string without specifying a charset
return SqlLiteral.createCharString((String) castNonNull(literal.getValue2()), POS);
}
case GEO: {
return SqlLiteral.createCharString(
SpatialTypeUtils.asEwkt(castNonNull(literal.getValueAs(Geometry.class))), POS);
}
case NUMERIC:
case EXACT_NUMERIC: {
if (SqlTypeName.APPROX_TYPES.contains(typeName)) {
Expand Down Expand Up @@ -1488,6 +1494,7 @@ public static SqlNode toSql(RexLiteral literal) {
default:
break;
}

// fall through
default:
throw new AssertionError(literal + ": " + typeName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@
import com.google.common.collect.Interners;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.locationtech.jts.geom.Geometry;

import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.AbstractList;
Expand Down Expand Up @@ -106,9 +108,10 @@ private static RelDataType keyToType(Key key) {
.put(Float.class, SqlTypeFamily.APPROXIMATE_NUMERIC)
.put(double.class, SqlTypeFamily.APPROXIMATE_NUMERIC)
.put(Double.class, SqlTypeFamily.APPROXIMATE_NUMERIC)
.put(java.sql.Date.class, SqlTypeFamily.DATE)
.put(Date.class, SqlTypeFamily.DATE)
.put(Time.class, SqlTypeFamily.TIME)
.put(Timestamp.class, SqlTypeFamily.TIMESTAMP)
.put(Geometry.class, SqlTypeFamily.GEO)
.build();

protected final RelDataTypeSystem typeSystem;
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/java/org/apache/calcite/sql/SqlDialect.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ public static DatabaseProduct getProduct(
return DatabaseProduct.NEOVIEW;
} else if (upperProductName.contains("POSTGRE")) {
return DatabaseProduct.POSTGRESQL;
} else if (upperProductName.contains("POSTGIS")) {
return DatabaseProduct.POSTGIS;
} else if (upperProductName.contains("SQL SERVER")) {
return DatabaseProduct.MSSQL;
} else if (upperProductName.contains("SYBASE")) {
Expand Down Expand Up @@ -1383,6 +1385,7 @@ public enum DatabaseProduct {
INTERBASE("Interbase", null, NullCollation.HIGH),
PHOENIX("Phoenix", "\"", NullCollation.HIGH),
POSTGRESQL("PostgreSQL", "\"", NullCollation.HIGH),
POSTGIS("Postgis", "\"", NullCollation.HIGH),
PRESTO("Presto", "\"", NullCollation.LAST),
NETEZZA("Netezza", "\"", NullCollation.HIGH),
INFOBRIGHT("Infobright", "`", NullCollation.HIGH),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.apache.calcite.sql.dialect.OracleSqlDialect;
import org.apache.calcite.sql.dialect.ParaccelSqlDialect;
import org.apache.calcite.sql.dialect.PhoenixSqlDialect;
import org.apache.calcite.sql.dialect.PostgisSqlDialect;
import org.apache.calcite.sql.dialect.PostgresqlSqlDialect;
import org.apache.calcite.sql.dialect.PrestoSqlDialect;
import org.apache.calcite.sql.dialect.RedshiftSqlDialect;
Expand Down Expand Up @@ -222,6 +223,8 @@ public class SqlDialectFactoryImpl implements SqlDialectFactory {
return PhoenixSqlDialect.DEFAULT;
case POSTGRESQL:
return PostgresqlSqlDialect.DEFAULT;
case POSTGIS:
return PostgisSqlDialect.DEFAULT;
case PRESTO:
return PrestoSqlDialect.DEFAULT;
case REDSHIFT:
Expand Down
Loading

0 comments on commit 4869424

Please sign in to comment.