From 59b725b0d9f6660f719f5be3a9143e0a1ff42674 Mon Sep 17 00:00:00 2001 From: Daniel Henneberger Date: Sat, 9 Nov 2024 17:20:39 -0800 Subject: [PATCH] Create translation function library Signed-off-by: Daniel Henneberger --- sqrl-calcite/pom.xml | 6 ++ .../calcite/DialectCallConverter.java | 3 +- .../com/datasqrl/calcite/QueryPlanner.java | 21 +++++- .../flink/sql/calcite/FlinkDialect.java | 1 - .../translations/DialectSqlTranslation.java | 10 ++- .../translations/PostgresSqlTranslation.java | 5 +- .../translations/SnowflakeSqlTranslation.java | 3 +- .../translations/TextSearchTranslation.java | 7 +- sqrl-flink-lib/pom.xml | 7 ++ sqrl-flink-lib/sqrl-translate-common/pom.xml | 41 +++++++++++ .../dialect/ExtendedPostgresSqlDialect.java | 16 ++--- .../dialect/ExtendedSnowflakeSqlDialect.java | 5 +- .../calcite/dialect/PostgresConformance.java | 0 .../calcite/function/RuleTransform.java | 4 +- .../function/translations/SqlTranslation.java | 4 +- .../function/util/ServiceLoaderDiscovery.java | 71 +++++++++++++++++++ .../json/postgres/JsonExtractTranslation.java | 14 ++-- 17 files changed, 177 insertions(+), 41 deletions(-) rename {sqrl-planner => sqrl-calcite}/src/main/java/com/datasqrl/engine/stream/flink/sql/calcite/FlinkDialect.java (94%) create mode 100644 sqrl-flink-lib/sqrl-translate-common/pom.xml rename {sqrl-calcite => sqrl-flink-lib/sqrl-translate-common}/src/main/java/com/datasqrl/calcite/dialect/ExtendedPostgresSqlDialect.java (90%) rename {sqrl-calcite => sqrl-flink-lib/sqrl-translate-common}/src/main/java/com/datasqrl/calcite/dialect/ExtendedSnowflakeSqlDialect.java (90%) rename {sqrl-calcite => sqrl-flink-lib/sqrl-translate-common}/src/main/java/com/datasqrl/calcite/dialect/PostgresConformance.java (100%) rename {sqrl-calcite => sqrl-flink-lib/sqrl-translate-common}/src/main/java/com/datasqrl/calcite/function/RuleTransform.java (75%) rename {sqrl-calcite => sqrl-flink-lib/sqrl-translate-common}/src/main/java/com/datasqrl/function/translations/SqlTranslation.java (81%) create mode 100644 sqrl-flink-lib/sqrl-translate-common/src/main/java/com/datasqrl/function/util/ServiceLoaderDiscovery.java diff --git a/sqrl-calcite/pom.xml b/sqrl-calcite/pom.xml index 7f33ce02b..6afc3c271 100644 --- a/sqrl-calcite/pom.xml +++ b/sqrl-calcite/pom.xml @@ -64,6 +64,12 @@ com.datasqrl sqrl-jdbc-1.19 + + com.datasqrl + sqrl-translate-common + 0.5.9-SNAPSHOT + compile + diff --git a/sqrl-calcite/src/main/java/com/datasqrl/calcite/DialectCallConverter.java b/sqrl-calcite/src/main/java/com/datasqrl/calcite/DialectCallConverter.java index ead035a3c..34314dd0b 100644 --- a/sqrl-calcite/src/main/java/com/datasqrl/calcite/DialectCallConverter.java +++ b/sqrl-calcite/src/main/java/com/datasqrl/calcite/DialectCallConverter.java @@ -18,6 +18,7 @@ import org.apache.calcite.rex.RexCall; import org.apache.calcite.rex.RexNode; import org.apache.calcite.rex.RexShuttle; +import org.apache.calcite.sql.SqlDialect; import org.apache.calcite.sql.SqlOperator; import org.apache.calcite.tools.Programs; @@ -31,7 +32,7 @@ public DialectCallConverter(RelOptPlanner planner) { this.planner = planner; } - public RelNode convert(Dialect dialect, RelNode relNode) { + public RelNode convert(SqlDialect dialect, RelNode relNode) { Map transforms = extractFunctionTransforms(relNode); List rules = new ArrayList<>(); diff --git a/sqrl-calcite/src/main/java/com/datasqrl/calcite/QueryPlanner.java b/sqrl-calcite/src/main/java/com/datasqrl/calcite/QueryPlanner.java index 22b2c437c..b058fa5c4 100644 --- a/sqrl-calcite/src/main/java/com/datasqrl/calcite/QueryPlanner.java +++ b/sqrl-calcite/src/main/java/com/datasqrl/calcite/QueryPlanner.java @@ -8,9 +8,13 @@ import com.datasqrl.calcite.convert.SqlNodeToString; import com.datasqrl.calcite.convert.SqlNodeToString.SqlStrings; import com.datasqrl.calcite.convert.SqlToStringFactory; +import com.datasqrl.calcite.dialect.ExtendedPostgresSqlDialect; +import com.datasqrl.calcite.dialect.ExtendedSnowflakeSqlDialect; import com.datasqrl.calcite.schema.ExpandTableMacroRule.ExpandTableMacroConfig; import com.datasqrl.calcite.schema.sql.SqlBuilders.SqlSelectBuilder; import com.datasqrl.canonicalizer.ReservedName; +import com.datasqrl.engine.stream.flink.sql.calcite.FlinkDialect; +import com.datasqrl.jdbc.SqrlPostgresDialect; import com.datasqrl.parse.SqrlParserImpl; import com.datasqrl.util.DataContextImpl; import java.util.Arrays; @@ -270,7 +274,22 @@ public RelNode runStage(OptimizationStage stage, RelNode relNode) { public RelNode convertRelToDialect(Dialect dialect, RelNode relNode) { return new DialectCallConverter(planner) - .convert(dialect, relNode); + .convert(dialectToSqlDialect(dialect), relNode); + } + + //temporary until we remove the Dialect enum + private SqlDialect dialectToSqlDialect(Dialect dialect) { + switch (dialect){ + case SQRL: + case CALCITE: + case POSTGRES: + return ExtendedPostgresSqlDialect.DEFAULT; + case FLINK: + return FlinkDialect.DEFAULT; + case SNOWFLAKE: + return ExtendedSnowflakeSqlDialect.DEFAULT; + } + throw new RuntimeException("Unknown dialect:" + dialect); } public static SqlNodes relToSql(Dialect dialect, RelNode relNode) { diff --git a/sqrl-planner/src/main/java/com/datasqrl/engine/stream/flink/sql/calcite/FlinkDialect.java b/sqrl-calcite/src/main/java/com/datasqrl/engine/stream/flink/sql/calcite/FlinkDialect.java similarity index 94% rename from sqrl-planner/src/main/java/com/datasqrl/engine/stream/flink/sql/calcite/FlinkDialect.java rename to sqrl-calcite/src/main/java/com/datasqrl/engine/stream/flink/sql/calcite/FlinkDialect.java index 32af1d61e..bef33f08a 100644 --- a/sqrl-planner/src/main/java/com/datasqrl/engine/stream/flink/sql/calcite/FlinkDialect.java +++ b/sqrl-calcite/src/main/java/com/datasqrl/engine/stream/flink/sql/calcite/FlinkDialect.java @@ -3,7 +3,6 @@ import org.apache.calcite.avatica.util.Casing; import org.apache.calcite.rex.RexCall; import org.apache.calcite.sql.SqlDialect; -import org.apache.calcite.sql.validate.SqlConformance; import org.apache.flink.sql.parser.validate.FlinkSqlConformance; public class FlinkDialect extends SqlDialect { diff --git a/sqrl-calcite/src/main/java/com/datasqrl/function/translations/DialectSqlTranslation.java b/sqrl-calcite/src/main/java/com/datasqrl/function/translations/DialectSqlTranslation.java index dcef9b78c..ae54602b1 100644 --- a/sqrl-calcite/src/main/java/com/datasqrl/function/translations/DialectSqlTranslation.java +++ b/sqrl-calcite/src/main/java/com/datasqrl/function/translations/DialectSqlTranslation.java @@ -1,17 +1,15 @@ package com.datasqrl.function.translations; import com.datasqrl.calcite.Dialect; +import lombok.AllArgsConstructor; import lombok.Getter; +import org.apache.calcite.sql.SqlDialect; import org.apache.calcite.sql.SqlOperator; @Getter +@AllArgsConstructor public abstract class DialectSqlTranslation implements SqlTranslation { - private final Dialect dialect; + private final SqlDialect sqlDialect; private final SqlOperator operator; - - public DialectSqlTranslation(Dialect dialect, SqlOperator operator) { - this.dialect = dialect; - this.operator = operator; - } } diff --git a/sqrl-calcite/src/main/java/com/datasqrl/function/translations/PostgresSqlTranslation.java b/sqrl-calcite/src/main/java/com/datasqrl/function/translations/PostgresSqlTranslation.java index 4f4e8f91b..18eceb739 100644 --- a/sqrl-calcite/src/main/java/com/datasqrl/function/translations/PostgresSqlTranslation.java +++ b/sqrl-calcite/src/main/java/com/datasqrl/function/translations/PostgresSqlTranslation.java @@ -1,14 +1,13 @@ package com.datasqrl.function.translations; -import com.datasqrl.calcite.Dialect; -import lombok.AllArgsConstructor; import lombok.Getter; import org.apache.calcite.sql.SqlOperator; +import org.apache.calcite.sql.dialect.PostgresqlSqlDialect; @Getter public abstract class PostgresSqlTranslation extends DialectSqlTranslation { public PostgresSqlTranslation(SqlOperator operator) { - super(Dialect.POSTGRES, operator); + super(PostgresqlSqlDialect.DEFAULT, operator); } } diff --git a/sqrl-calcite/src/main/java/com/datasqrl/function/translations/SnowflakeSqlTranslation.java b/sqrl-calcite/src/main/java/com/datasqrl/function/translations/SnowflakeSqlTranslation.java index 3517a5208..cbc16ff6c 100644 --- a/sqrl-calcite/src/main/java/com/datasqrl/function/translations/SnowflakeSqlTranslation.java +++ b/sqrl-calcite/src/main/java/com/datasqrl/function/translations/SnowflakeSqlTranslation.java @@ -1,12 +1,13 @@ package com.datasqrl.function.translations; import com.datasqrl.calcite.Dialect; +import com.datasqrl.calcite.dialect.ExtendedSnowflakeSqlDialect; import lombok.Getter; import org.apache.calcite.sql.SqlOperator; @Getter public abstract class SnowflakeSqlTranslation extends DialectSqlTranslation { public SnowflakeSqlTranslation(SqlOperator operator) { - super(Dialect.SNOWFLAKE, operator); + super(ExtendedSnowflakeSqlDialect.DEFAULT, operator); } } diff --git a/sqrl-calcite/src/main/java/com/datasqrl/function/translations/TextSearchTranslation.java b/sqrl-calcite/src/main/java/com/datasqrl/function/translations/TextSearchTranslation.java index 742ff6b21..3c5822633 100644 --- a/sqrl-calcite/src/main/java/com/datasqrl/function/translations/TextSearchTranslation.java +++ b/sqrl-calcite/src/main/java/com/datasqrl/function/translations/TextSearchTranslation.java @@ -5,6 +5,7 @@ import com.datasqrl.calcite.convert.SimpleCallTransform.SimpleCallTransformConfig; import com.datasqrl.calcite.convert.SimplePredicateTransform; import com.datasqrl.calcite.convert.SimplePredicateTransform.SimplePredicateTransformConfig; +import com.datasqrl.calcite.dialect.ExtendedPostgresSqlDialect; import com.datasqrl.calcite.function.RuleTransform; import com.datasqrl.canonicalizer.Name; import com.datasqrl.function.PgSpecificOperatorTable; @@ -18,16 +19,18 @@ import org.apache.calcite.rex.RexCall; import org.apache.calcite.rex.RexLiteral; import org.apache.calcite.rex.RexNode; +import org.apache.calcite.sql.SqlDialect; import org.apache.calcite.sql.SqlKind; import org.apache.calcite.sql.SqlOperator; +import org.apache.calcite.sql.dialect.PostgresqlSqlDialect; import org.apache.calcite.sql.fun.SqlStdOperatorTable; @AutoService(RuleTransform.class) public class TextSearchTranslation implements RuleTransform { @Override - public List transform(Dialect dialect, SqlOperator operator) { - if (dialect != Dialect.POSTGRES) { + public List transform(SqlDialect dialect, SqlOperator operator) { + if (!dialect.getClass().isAssignableFrom(ExtendedPostgresSqlDialect.class)) { return List.of(); } return List.of( diff --git a/sqrl-flink-lib/pom.xml b/sqrl-flink-lib/pom.xml index 0c1c9c959..66d265fe7 100644 --- a/sqrl-flink-lib/pom.xml +++ b/sqrl-flink-lib/pom.xml @@ -31,6 +31,7 @@ sqrl-flexible-csv sqrl-name sqrl-errors + sqrl-translate-common Parent pom @@ -107,6 +108,12 @@ ${project.version} + + com.datasqrl + sqrl-jdbc-1.19 + ${project.version} + + com.datasqrl sqrl-json diff --git a/sqrl-flink-lib/sqrl-translate-common/pom.xml b/sqrl-flink-lib/sqrl-translate-common/pom.xml new file mode 100644 index 000000000..b966f7c2e --- /dev/null +++ b/sqrl-flink-lib/sqrl-translate-common/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + + com.datasqrl + sqrl-flink-lib + 0.5.9-SNAPSHOT + + + sqrl-translate-common + + + 11 + 11 + UTF-8 + + + + + org.apache.calcite + calcite-core + 1.32.0 + provided + + + org.apache.flink + flink-table-planner_2.12 + 1.19.1 + provided + + + + com.datasqrl + sqrl-jdbc-1.19 + ${project.version} + provided + + + \ No newline at end of file diff --git a/sqrl-calcite/src/main/java/com/datasqrl/calcite/dialect/ExtendedPostgresSqlDialect.java b/sqrl-flink-lib/sqrl-translate-common/src/main/java/com/datasqrl/calcite/dialect/ExtendedPostgresSqlDialect.java similarity index 90% rename from sqrl-calcite/src/main/java/com/datasqrl/calcite/dialect/ExtendedPostgresSqlDialect.java rename to sqrl-flink-lib/sqrl-translate-common/src/main/java/com/datasqrl/calcite/dialect/ExtendedPostgresSqlDialect.java index eb13325e5..1929dcde1 100644 --- a/sqrl-calcite/src/main/java/com/datasqrl/calcite/dialect/ExtendedPostgresSqlDialect.java +++ b/sqrl-flink-lib/sqrl-translate-common/src/main/java/com/datasqrl/calcite/dialect/ExtendedPostgresSqlDialect.java @@ -1,12 +1,9 @@ package com.datasqrl.calcite.dialect; -import static org.apache.calcite.sql.SqlKind.COLLECTION_TABLE; - -import com.datasqrl.calcite.Dialect; import com.datasqrl.function.translations.SqlTranslation; +import com.datasqrl.function.util.ServiceLoaderDiscovery; import com.datasqrl.type.JdbcTypeSerializer; -import com.datasqrl.util.ServiceLoaderDiscovery; import java.util.Map; import java.util.stream.Collectors; import org.apache.calcite.avatica.util.Casing; @@ -16,19 +13,18 @@ import org.apache.calcite.sql.SqlCall; import org.apache.calcite.sql.SqlDataTypeSpec; import org.apache.calcite.sql.SqlDialect; +import org.apache.calcite.sql.SqlKind; import org.apache.calcite.sql.SqlWriter; import org.apache.calcite.sql.dialect.PostgresqlSqlDialect; -import org.apache.calcite.sql.fun.SqlCollectionTableOperator; import org.apache.calcite.sql.parser.SqlParserPos; import org.apache.calcite.sql.type.SqlTypeName; import org.apache.calcite.sql.validate.SqlConformance; -import org.apache.calcite.sql.validate.SqlConformanceEnum; import org.apache.flink.table.planner.plan.schema.RawRelDataType; public class ExtendedPostgresSqlDialect extends PostgresqlSqlDialect { public static final Map translationMap = ServiceLoaderDiscovery.getAll(SqlTranslation.class) - .stream().filter(f->f.getDialect() == Dialect.POSTGRES) + .stream().filter(f->f.getSqlDialect().getClass().isAssignableFrom(ExtendedPostgresSqlDialect.class)) .collect(Collectors.toMap(f->f.getOperator().getName().toLowerCase(), f->f)); public static final ExtendedPostgresSqlDialect.Context DEFAULT_CONTEXT; @@ -37,7 +33,7 @@ public class ExtendedPostgresSqlDialect extends PostgresqlSqlDialect { static { DEFAULT_CONTEXT = SqlDialect.EMPTY_CONTEXT.withDatabaseProduct(DatabaseProduct.POSTGRESQL) .withIdentifierQuoteString("\"").withUnquotedCasing(Casing.TO_LOWER) - .withDataTypeSystem(POSTGRESQL_TYPE_SYSTEM) + .withDataTypeSystem(PostgresqlSqlDialect.POSTGRESQL_TYPE_SYSTEM) .withConformance(new PostgresConformance()); DEFAULT = new ExtendedPostgresSqlDialect(DEFAULT_CONTEXT); } @@ -51,7 +47,7 @@ public ExtendedPostgresSqlDialect(Context context) { private static Map getForeignCastSpecs() { Map jdbcTypeSerializer = ServiceLoaderDiscovery.getAll(JdbcTypeSerializer.class) .stream() - .filter(f->f.getDialectId().equalsIgnoreCase(Dialect.POSTGRES.name())) + .filter(f->f.getDialectId().equalsIgnoreCase("POSTGRES")) .collect(Collectors.toMap(JdbcTypeSerializer::getConversionClass, JdbcTypeSerializer::dialectTypeName)); return jdbcTypeSerializer; @@ -133,7 +129,7 @@ public SqlDataTypeSpec getCastSpec(RelDataType type) { @Override public void unparseCall(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) { - if (call.getOperator().getKind() == COLLECTION_TABLE) { //skip FROM TABLE(..) call + if (call.getOperator().getKind() == SqlKind.COLLECTION_TABLE) { //skip FROM TABLE(..) call unparseCall(writer, (SqlCall)call.getOperandList().get(0), leftPrec, rightPrec); return; } diff --git a/sqrl-calcite/src/main/java/com/datasqrl/calcite/dialect/ExtendedSnowflakeSqlDialect.java b/sqrl-flink-lib/sqrl-translate-common/src/main/java/com/datasqrl/calcite/dialect/ExtendedSnowflakeSqlDialect.java similarity index 90% rename from sqrl-calcite/src/main/java/com/datasqrl/calcite/dialect/ExtendedSnowflakeSqlDialect.java rename to sqrl-flink-lib/sqrl-translate-common/src/main/java/com/datasqrl/calcite/dialect/ExtendedSnowflakeSqlDialect.java index 2cbf3cd3d..c116462d8 100644 --- a/sqrl-calcite/src/main/java/com/datasqrl/calcite/dialect/ExtendedSnowflakeSqlDialect.java +++ b/sqrl-flink-lib/sqrl-translate-common/src/main/java/com/datasqrl/calcite/dialect/ExtendedSnowflakeSqlDialect.java @@ -1,8 +1,7 @@ package com.datasqrl.calcite.dialect; -import com.datasqrl.calcite.Dialect; import com.datasqrl.function.translations.SqlTranslation; -import com.datasqrl.util.ServiceLoaderDiscovery; +import com.datasqrl.function.util.ServiceLoaderDiscovery; import java.util.Map; import java.util.stream.Collectors; import org.apache.calcite.avatica.util.Casing; @@ -14,7 +13,7 @@ public class ExtendedSnowflakeSqlDialect extends SnowflakeSqlDialect { public static final Map translationMap = ServiceLoaderDiscovery .getAll(SqlTranslation.class) - .stream().filter(f->f.getDialect() == Dialect.SNOWFLAKE) + .stream().filter(f->f.getSqlDialect().getClass().isAssignableFrom(ExtendedSnowflakeSqlDialect.class)) .collect(Collectors.toMap(f->f.getOperator().getName().toLowerCase(), f->f)); public static final SqlDialect.Context DEFAULT_CONTEXT; diff --git a/sqrl-calcite/src/main/java/com/datasqrl/calcite/dialect/PostgresConformance.java b/sqrl-flink-lib/sqrl-translate-common/src/main/java/com/datasqrl/calcite/dialect/PostgresConformance.java similarity index 100% rename from sqrl-calcite/src/main/java/com/datasqrl/calcite/dialect/PostgresConformance.java rename to sqrl-flink-lib/sqrl-translate-common/src/main/java/com/datasqrl/calcite/dialect/PostgresConformance.java diff --git a/sqrl-calcite/src/main/java/com/datasqrl/calcite/function/RuleTransform.java b/sqrl-flink-lib/sqrl-translate-common/src/main/java/com/datasqrl/calcite/function/RuleTransform.java similarity index 75% rename from sqrl-calcite/src/main/java/com/datasqrl/calcite/function/RuleTransform.java rename to sqrl-flink-lib/sqrl-translate-common/src/main/java/com/datasqrl/calcite/function/RuleTransform.java index 615acda89..1f0701eb2 100644 --- a/sqrl-calcite/src/main/java/com/datasqrl/calcite/function/RuleTransform.java +++ b/sqrl-flink-lib/sqrl-translate-common/src/main/java/com/datasqrl/calcite/function/RuleTransform.java @@ -1,7 +1,7 @@ package com.datasqrl.calcite.function; -import com.datasqrl.calcite.Dialect; import org.apache.calcite.plan.RelRule; +import org.apache.calcite.sql.SqlDialect; import org.apache.calcite.sql.SqlOperator; import java.util.List; @@ -12,7 +12,7 @@ public interface RuleTransform { * may not be the same operator your 'this' since it may undergo delegation so it is passed * as a parameter. */ - List transform(Dialect dialect, SqlOperator operator /* todo engine capabilities*/); + List transform(SqlDialect dialect, SqlOperator operator /* todo engine capabilities*/); String getRuleOperatorName(); } diff --git a/sqrl-calcite/src/main/java/com/datasqrl/function/translations/SqlTranslation.java b/sqrl-flink-lib/sqrl-translate-common/src/main/java/com/datasqrl/function/translations/SqlTranslation.java similarity index 81% rename from sqrl-calcite/src/main/java/com/datasqrl/function/translations/SqlTranslation.java rename to sqrl-flink-lib/sqrl-translate-common/src/main/java/com/datasqrl/function/translations/SqlTranslation.java index 95ceb4eb4..4f8f4a8e8 100644 --- a/sqrl-calcite/src/main/java/com/datasqrl/function/translations/SqlTranslation.java +++ b/sqrl-flink-lib/sqrl-translate-common/src/main/java/com/datasqrl/function/translations/SqlTranslation.java @@ -1,12 +1,12 @@ package com.datasqrl.function.translations; -import com.datasqrl.calcite.Dialect; import org.apache.calcite.sql.SqlCall; +import org.apache.calcite.sql.SqlDialect; import org.apache.calcite.sql.SqlOperator; import org.apache.calcite.sql.SqlWriter; public interface SqlTranslation { - Dialect getDialect(); + SqlDialect getSqlDialect(); SqlOperator getOperator(); void unparse(SqlCall call, SqlWriter writer, int leftPrec, int rightPrec); } diff --git a/sqrl-flink-lib/sqrl-translate-common/src/main/java/com/datasqrl/function/util/ServiceLoaderDiscovery.java b/sqrl-flink-lib/sqrl-translate-common/src/main/java/com/datasqrl/function/util/ServiceLoaderDiscovery.java new file mode 100644 index 000000000..4c2132521 --- /dev/null +++ b/sqrl-flink-lib/sqrl-translate-common/src/main/java/com/datasqrl/function/util/ServiceLoaderDiscovery.java @@ -0,0 +1,71 @@ +package com.datasqrl.function.util; + +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import java.util.ArrayList; +import java.util.List; +import lombok.NonNull; + +import java.util.Optional; +import java.util.ServiceLoader; +import java.util.function.Function; +import java.util.function.Predicate; +import lombok.SneakyThrows; + +public class ServiceLoaderDiscovery { + + private static final Cache, List> cache = CacheBuilder.newBuilder().maximumSize(10_000) + .build(); + + public static Optional findFirst(@NonNull Class clazz, + @NonNull Predicate condition) { + List services = getAll(clazz); + for (L service : services) { + if (condition.test(service)) { + return Optional.of(service); + } + } + return Optional.empty(); + } + + @SneakyThrows + public static List getAll(Class clazz) { + return (List) cache.get(clazz, () -> { + List loaded = new ArrayList<>(); + ServiceLoader.load(clazz).forEach(loaded::add); + return loaded; + }); + } + + public static L get(@NonNull Class clazz, @NonNull Predicate condition, + @NonNull List identifiers) { + return findFirst(clazz, condition).orElseThrow( + () -> new RuntimeException("Could not find server loader class:" + clazz.getName())); + } + + public static Optional findFirst(@NonNull Class clazz, @NonNull Function key, + @NonNull String value) { + return findFirst(clazz, l -> key.apply(l).equalsIgnoreCase(value)); + } + + public static L get(@NonNull Class clazz, @NonNull Function key, + @NonNull String value) { + return findFirst(clazz, key, value) + .orElseThrow(() -> new RuntimeException("Could not find server loader class:" + clazz.getName())); + } + + public static Optional findFirst(@NonNull Class clazz, + @NonNull Function key1, @NonNull String value1, @NonNull Function key2, + @NonNull String value2) { + return findFirst(clazz, + l -> key1.apply(l).equalsIgnoreCase(value1) && key2.apply(l).equalsIgnoreCase(value2)); + } + + public static L get(@NonNull Class clazz, @NonNull Function key1, + @NonNull String value1, @NonNull Function key2, @NonNull String value2) { + return findFirst(clazz, key1, value1, key2, value2).orElseThrow( + () -> new RuntimeException("Could not find server loader class:" + clazz.getName())); + } + + +} diff --git a/sqrl-planner/src/main/java/com/datasqrl/functions/json/postgres/JsonExtractTranslation.java b/sqrl-planner/src/main/java/com/datasqrl/functions/json/postgres/JsonExtractTranslation.java index dbfa6d003..660d3896d 100644 --- a/sqrl-planner/src/main/java/com/datasqrl/functions/json/postgres/JsonExtractTranslation.java +++ b/sqrl-planner/src/main/java/com/datasqrl/functions/json/postgres/JsonExtractTranslation.java @@ -3,23 +3,19 @@ import static com.datasqrl.function.CalciteFunctionUtil.lightweightOp; import static com.datasqrl.function.PgSpecificOperatorTable.JsonToString; -import com.datasqrl.calcite.Dialect; -import com.datasqrl.calcite.convert.SimpleCallTransform; import com.datasqrl.calcite.convert.SimpleCallTransform.SimpleCallTransformConfig; +import com.datasqrl.calcite.dialect.ExtendedPostgresSqlDialect; import com.datasqrl.calcite.function.RuleTransform; -import com.datasqrl.calcite.type.TypeFactory; import com.google.auto.service.AutoService; import java.util.ArrayList; import java.util.List; import org.apache.calcite.plan.RelRule; import org.apache.calcite.rel.type.RelDataType; -import org.apache.calcite.rel.type.RelDataTypeFactory; -import org.apache.calcite.rex.RexLiteral; import org.apache.calcite.rex.RexNode; +import org.apache.calcite.sql.SqlDialect; import org.apache.calcite.sql.SqlFunction; -import org.apache.calcite.sql.SqlJsonEmptyOrError; -import org.apache.calcite.sql.SqlJsonValueEmptyOrErrorBehavior; import org.apache.calcite.sql.SqlOperator; +import org.apache.calcite.sql.dialect.PostgresqlSqlDialect; import org.apache.calcite.sql.fun.SqlStdOperatorTable; import org.apache.calcite.sql.type.SqlTypeName; @@ -33,8 +29,8 @@ public class JsonExtractTranslation implements RuleTransform { "jsonb_path_query_first"); @Override - public List transform(Dialect dialect, SqlOperator operator) { - if (dialect == Dialect.POSTGRES) { + public List transform(SqlDialect dialect, SqlOperator operator) { + if (dialect.getClass().isAssignableFrom(ExtendedPostgresSqlDialect.class)) { return postgresTransform(operator); } return List.of();