Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CALCITE-6325] Add LOG function (enabled in Mysql and Spark library) #3789

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

caicancai
Copy link
Member

@caicancai caicancai commented May 5, 2024

@@ -512,8 +512,7 @@ public enum BuiltInMethod {
SAFE_DIVIDE(SqlFunctions.class, "safeDivide", double.class, double.class),
SAFE_MULTIPLY(SqlFunctions.class, "safeMultiply", double.class, double.class),
SAFE_SUBTRACT(SqlFunctions.class, "safeSubtract", double.class, double.class),
LOG(SqlFunctions.class, "log", long.class, long.class),
LOG2(SqlFunctions.class, "log2", long.class),
LOG(SqlFunctions.class, "log", long.class, long.class, int.class),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this requires an enum instead of a numeric flag?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tanclary I'm sorry that it took so long to improve it. If you have time, please review this PR.

Copy link

sonarcloud bot commented May 6, 2024


map.put(LN, new LogImplementor());
map.put(LOG, new LogImplementor());
map.put(LOG10, new LogImplementor());

map.put(LOG_MYSQLSPARK, new LogMysqlSparkImplementor());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SQL function name that you are registering is LOG, which is the same as what is added on line 651. The equals and hashCode methods used for the key is from the class SqlOperator and will only use the name+kind.

The result is that you overwrite the map entry from line 651. One way around this is set LOG_MYSQLSPARK to a new anonymous inner subclass of SqlBasicFunction. You would need to increase visibility of the SqlBasicFunction constructor.

LOG_MYSQLSPARK = new SqlBasicFunction(...) {};

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@normanj-bitquill Yes, you are right, so I use the name LOGMYSQLSPARK. The method you mentioned is something I didn't expect. Let me try it.
Thank you for your reply

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue isn't with the static variable name, it is with the value of name in the SqlBasicFunction object. Both LOG and LOG_MYSQLSPARK use the same value for the name (and for kind).

If you debug at this point, you can see if it is replacing or adding a new entry.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix,

  public static final SqlFunction LOG_MYSQL =
      SqlBasicFunction.create(SqlKind.LOG,
          ReturnTypes.DOUBLE_FORCE_NULLABLE,
          OperandTypes.NUMERIC_OPTIONAL_NUMERIC);

Changing the parameter list can fix this situation

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@normanj-bitquill I'm very sorry for the late reply. I've been very busy recently. You are correct, but I have a question, why the test can pass

Thanks for reminding me

@@ -211,6 +211,7 @@
import static org.apache.calcite.sql.fun.SqlLibraryOperators.LOG2;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.LOGICAL_AND;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.LOGICAL_OR;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.LOG_MYSQLSPARK;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is strange we use LOG_MYSQLSPARK. May be LOG_SPARK or LOG_MYSQL. we will add the libraries label in SqlLibraryOperators to show the function can work in which database.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is LOG_MYSQL OK?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bigquery's log function and MySQL's log function are inconsistent in log(0)

@@ -2214,6 +2222,7 @@ private static RelDataType deriveTypeMapFromEntries(SqlOperatorBinding opBinding
OperandTypes.NUMERIC,
SqlFunctionCategory.NUMERIC);


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete the blank line.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -2789,7 +2789,8 @@ In the following:
| f s | LEN(string) | Equivalent to `CHAR_LENGTH(string)`
| b f s | LENGTH(string) | Equivalent to `CHAR_LENGTH(string)`
| h s | LEVENSHTEIN(string1, string2) | Returns the Levenshtein distance between *string1* and *string2*
| b | LOG(numeric1 [, numeric2 ]) | Returns the logarithm of *numeric1* to base *numeric2*, or base e if *numeric2* is not present
| b p | LOG(numeric1 [, numeric2 ]) | Returns the logarithm of *numeric1* to base *numeric2*, or base e if *numeric2* is not present
| m s | LOG(numeric1 [, numeric2 ]) | Returns the logarithm of *numeric1* to base *numeric2*, or base e if *numeric2* is not present
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to add a description to illustrate the difference before LOG.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link

sonarcloud bot commented Jun 9, 2024

@caicancai
Copy link
Member Author

caicancai commented Jun 17, 2024

@YiwenWu @NobiGo PATL, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants