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-6876] Druid Adapter support more functions #4227

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public class DruidQuery extends AbstractRelNode implements BindableRel {
/**
* Provides a standard list of supported Calcite operators that can be converted to
* Druid Expressions. This can be used as is or re-adapted based on underline
* engine operator syntax.
* engine operator syntax. Methods can refer to https://druid.apache.org/docs/latest/querying/
*/
public static final List<DruidSqlOperatorConverter> DEFAULT_OPERATORS_LIST =
ImmutableList.<DruidSqlOperatorConverter>builder()
Expand All @@ -120,6 +120,11 @@ public class DruidQuery extends AbstractRelNode implements BindableRel {
.add(new DirectOperatorConversion(SqlStdOperatorTable.SIN, "sin"))
.add(new DirectOperatorConversion(SqlStdOperatorTable.COS, "cos"))
.add(new DirectOperatorConversion(SqlStdOperatorTable.TAN, "tan"))
.add(new DirectOperatorConversion(SqlStdOperatorTable.COT, "cot"))
Copy link
Member

Choose a reason for hiding this comment

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

Could you please add a link to the druid function documentation for this method, at least to let users know which version it is, thank you

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, Thanks~ @caicancai

.add(new DirectOperatorConversion(SqlStdOperatorTable.ASIN, "asin"))
.add(new DirectOperatorConversion(SqlStdOperatorTable.ACOS, "acos"))
.add(new DirectOperatorConversion(SqlStdOperatorTable.ATAN, "atan"))
.add(new DirectOperatorConversion(SqlStdOperatorTable.ATAN2, "atan2"))
.add(new DirectOperatorConversion(SqlStdOperatorTable.CASE, "case_searched"))
.add(new DirectOperatorConversion(SqlStdOperatorTable.CHAR_LENGTH, "strlen"))
.add(new DirectOperatorConversion(SqlStdOperatorTable.CHARACTER_LENGTH, "strlen"))
Expand Down
24 changes: 24 additions & 0 deletions druid/src/test/java/org/apache/calcite/test/DruidAdapterIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -3969,6 +3969,30 @@ private void testCountWithApproxDistinct(boolean approx, String sql, String expe
+ "filter=[AND(>(SIN($91), 0.9129452507276277E0), >(COS($90), 0.40808206181339196E0), =(FLOOR(TAN($91)), 2.0E0), "
+ "<(ABS(-(TAN($91), /(SIN($91), COS($91)))), 1.0E-6))], "
+ "groups=[{}], aggs=[[COUNT()]])");

final String sql1 = "SELECT COUNT(*) FROM " + FOODMART_TABLE + " WHERE "
+ "COT(\"store_cost\") > COT(20) AND ASIN(\"store_sales\") > ASIN(1) "
+ "AND FLOOR(ACOS(\"store_cost\")) = 2 "
+ "AND ABS(ATAN(\"store_cost\") - ATAN(\"store_cost\") / ATAN(\"store_cost\")) < 10e-7";
sql(sql1, FOODMART)
.returnsOrdered("EXPR$0=0")
.explainContains("PLAN=EnumerableInterpreter\n"
+ " DruidQuery(table=[[foodmart, foodmart]], "
+ "intervals=[[1900-01-09T00:00:00.000Z/2992-01-10T00:00:00.000Z]], "
+ "filter=[AND(>(COT($91), 0.4469951089489167E0), >(ASIN($90), 1.5707963267948966E0), =(FLOOR(ACOS($91)), 2.0E0), "
+ "<(ABS(-(ATAN($91), /(ATAN($91), ATAN($91)))), 1.0E-6))], "
+ "groups=[{}], aggs=[[COUNT()]])");

final String sql2 = "SELECT COUNT(*) FROM " + FOODMART_TABLE + " WHERE "
+ "FLOOR(ATAN2(\"store_cost\", \"store_cost\")) = 2 ";
sql(sql2, FOODMART)
.returnsOrdered("EXPR$0=0")
Copy link
Contributor

Choose a reason for hiding this comment

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

I assume this result has been validated in some way.

Copy link
Member Author

@xuzifu666 xuzifu666 Mar 6, 2025

Choose a reason for hiding this comment

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

Yes,I had test the ut with Druid Adapter dataset(https://github.com/zabetak/calcite-druid-dataset) in docker env and result is passed. @mihaibudiu

Copy link
Member

Choose a reason for hiding this comment

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

I guess the question is about the results, not the test, something like comparing the result here with the output of a few major DBs like postgres or Oracle, could you do that and share @xuzifu666 please?

Copy link
Member Author

@xuzifu666 xuzifu666 Mar 7, 2025

Choose a reason for hiding this comment

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

@asolimando OK,I had test the result from Oracle,the result like follow which keep the same with Druid:
ORACLE:
select COT(10) from test_db.new_table nt ORDER BY id limit 1;
1.5423510453569202

select ASIN(0.1) from test_db.new_table nt ORDER BY id limit 1;
0.1001674211615598

select ACOS(0.1) from test_db.new_table nt ORDER BY id limit 1;
1.4706289056333368

select ATAN(10) from test_db.new_table nt ORDER BY id limit 1;
1.4711276743037347

select ATAN2(10, 20) from test_db.new_table nt ORDER BY id limit 1;
0.4636476090008061

select DEGREES(10) from test_db.new_table nt ORDER BY id limit 1;
572.9577951308232

Druid:
select COT(10)
1.5423510453569202

select ASIN(0.1)
0.1001674211615598

select ACOS(0.1)
1.4706289056333368

select ATAN(10)
1.4711276743037347

select ATAN2(10, 20)
0.4636476090008061

select DEGREES(10)
572.9577951308232

.explainContains("PLAN=EnumerableInterpreter\n"
+ " DruidQuery(table=[[foodmart, foodmart]], "
+ "intervals=[[1900-01-09T00:00:00.000Z/2992-01-10T00:00:00.000Z]], "
+ "filter=[=(FLOOR(ATAN2($91, $91)), 2.0E0)], "
+ "groups=[{}], aggs=[[COUNT()]])");

}

@Test void testCastLiteralToTimestamp() {
Expand Down