You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by "YiwenWu (via GitHub)" <gi...@apache.org> on 2024/07/02 17:10:07 UTC
Re: [PR] [CALCITE-6312] Add LOG function (enabled in PostgreSQL library) [calcite]
YiwenWu commented on code in PR #3839:
URL: https://github.com/apache/calcite/pull/3839#discussion_r1662509026
##########
core/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java:
##########
@@ -4202,67 +4205,43 @@ private static class LogicalNotImplementor extends AbstractRexCallImplementor {
* appropriate base (i.e. base e for LN).
*/
private static class LogImplementor extends AbstractRexCallImplementor {
- LogImplementor() {
+ private final SqlLibrary library;
+ LogImplementor(SqlLibrary library) {
super("log", NullPolicy.STRICT, true);
+ this.library = library;
}
@Override Expression implementSafe(final RexToLixTranslator translator,
final RexCall call, final List<Expression> argValueList) {
- return Expressions.call(BuiltInMethod.LOG.method, args(call, argValueList));
- }
-
- private static List<Expression> args(RexCall call,
- List<Expression> argValueList) {
- Expression operand0 = argValueList.get(0);
- final Expressions.FluentList<Expression> list = Expressions.list(operand0);
- switch (call.getOperator().getName()) {
- case "LOG":
- if (argValueList.size() == 2) {
- return list.append(argValueList.get(1)).append(Expressions.constant(0));
- }
- // fall through
- case "LN":
- return list.append(Expressions.constant(Math.exp(1))).append(Expressions.constant(0));
- case "LOG10":
- return list.append(Expressions.constant(BigDecimal.TEN)).append(Expressions.constant(0));
- default:
- throw new AssertionError("Operator not found: " + call.getOperator());
+ if (library == SqlLibrary.MYSQL) {
+ return Expressions.call(BuiltInMethod.LOG.method, args(call, argValueList, 1));
+ } else if (library == SqlLibrary.POSTGRESQL) {
+ return Expressions.call(BuiltInMethod.LOG.method, args(call, argValueList, 2));
+ } else {
+ return Expressions.call(BuiltInMethod.LOG.method, args(call, argValueList, 0));
}
}
- }
-
- /** Implementor for the {@code LN}, {@code LOG}, {@code LOG2} and {@code LOG10} operators
- * on Mysql and Spark library
- *
- * <p>Handles all logarithm functions using log rules to determine the
- * appropriate base (i.e. base e for LN).
- */
- private static class LogMysqlImplementor extends AbstractRexCallImplementor {
- LogMysqlImplementor() {
- super("log", NullPolicy.STRICT, true);
- }
-
- @Override Expression implementSafe(final RexToLixTranslator translator,
- final RexCall call, final List<Expression> argValueList) {
- return Expressions.call(BuiltInMethod.LOG.method, args(call, argValueList));
- }
private static List<Expression> args(RexCall call,
- List<Expression> argValueList) {
+ List<Expression> argValueList, int flag) {
Expression operand0 = argValueList.get(0);
final Expressions.FluentList<Expression> list = Expressions.list(operand0);
switch (call.getOperator().getName()) {
case "LOG":
if (argValueList.size() == 2) {
- return list.append(argValueList.get(1)).append(Expressions.constant(1));
+ return list.append(argValueList.get(1)).append(Expressions.constant(flag));
+ }
+ if (flag == 2) {
Review Comment:
Whether to consider BigQuery case, flag=0 ?
##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -7045,6 +7049,33 @@ void checkRegexpExtract(SqlOperatorFixture f0, FunctionAlias functionAlias) {
f0.forEachLibrary(list(SqlLibrary.MYSQL, SqlLibrary.SPARK), consumer);
}
+ /** Test case for
+ * <a href="https://issues.apache.org/jira/browse/CALCITE-6321">[CALCITE-6321]
+ * Add LOG function (enabled in PostgreSQL library)</a>. */
+ @Test void testPostgresLogFunc() {
+ final SqlOperatorFixture f0 = fixture()
+ .setFor(SqlLibraryOperators.LOG_POSTGRES, VmName.EXPAND);
+ f0.checkFails("^log(100, 10)^",
+ "No match found for function signature LOG\\(<NUMERIC>, <NUMERIC>\\)", false);
+ final SqlOperatorFixture f = f0.withLibrary(SqlLibrary.POSTGRESQL);
+ f.checkScalarApprox("log(10, 10)", "DOUBLE NOT NULL",
+ isWithin(1.0, 0.000001));
+ f.checkScalarApprox("log(64, 8)", "DOUBLE NOT NULL",
+ isWithin(2.0, 0.000001));
Review Comment:
Recommend to set a different value range from existing `testLogFunc`, or use `f.checkScalar` directly.
--
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: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org