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-6566] JDBC adapter should generate PI function with parenthe… #3956

Open
wants to merge 5 commits 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
12 changes: 6 additions & 6 deletions babel/src/test/resources/sql/redshift.iq
Original file line number Diff line number Diff line change
Expand Up @@ -1476,7 +1476,7 @@ EXPR$0
2
!ok

select -abs(-pi);
select -abs(-pi());
EXPR$0
-3.141592653589793
!ok
Expand Down Expand Up @@ -1522,7 +1522,7 @@ EXPR$0
-10
!ok

select ceil(pi);
select ceil(pi());
EXPR$0
4.0
!ok
Expand All @@ -1546,7 +1546,7 @@ EXPR$0
!ok

# DEGREES
select degrees(pi);
select degrees(pi());
EXPR$0
180.0
!ok
Expand Down Expand Up @@ -1629,17 +1629,17 @@ SELECT "RANDOM"()
!explain-validated-on calcite

# ROUND
select round(pi);
select round(pi());
EXPR$0
3.0
!ok

select round(pi, 2);
select round(pi(), 2);
EXPR$0
3.14
!ok

select round(-pi, 2);
select round(-pi(), 2);
EXPR$0
-3.14
!ok
Expand Down
11 changes: 11 additions & 0 deletions core/src/main/java/org/apache/calcite/sql/SqlBasicFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ public static SqlBasicFunction create(String name, SqlKind kind,
SqlFunctionCategory.SYSTEM, call -> SqlMonotonicity.NOT_MONOTONIC, false);
}

/** Creates a {@code SqlBasicFunction} whose name is the same as its kind. */
public static SqlBasicFunction create(SqlKind kind,
SqlReturnTypeInference returnTypeInference,
SqlOperandTypeChecker operandTypeChecker,
SqlFunctionCategory category) {
return new SqlBasicFunction(kind.name(), kind,
SqlSyntax.FUNCTION, true, returnTypeInference, null,
OperandHandlers.DEFAULT, operandTypeChecker, 0,
category, call -> SqlMonotonicity.NOT_MONOTONIC, false);
}

/** Creates a {@code SqlBasicFunction} whose name is the same as its kind
* and whose category {@link SqlFunctionCategory#SYSTEM}. */
public static SqlBasicFunction create(SqlKind kind,
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/java/org/apache/calcite/sql/SqlKind.java
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,9 @@ public enum SqlKind {
/** {@code CEIL} function. */
CEIL,

/** ${code PI} function. */
PI,

/** {@code TRIM} function. */
TRIM,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ public OracleSqlDialect(Context context) {
timeUnitNode.getParserPosition());
SqlFloorFunction.unparseDatetimeFunction(writer, call2, "TRUNC", true);
break;

case PI:
writer.sep(call.getOperator().getName());
break;
default:
super.unparseCall(writer, call, leftPrec, rightPrec);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import org.apache.calcite.sql.SqlSetOperator;
import org.apache.calcite.sql.SqlSetSemanticsTableOperator;
import org.apache.calcite.sql.SqlSpecialOperator;
import org.apache.calcite.sql.SqlSyntax;
import org.apache.calcite.sql.SqlTumbleTableFunction;
import org.apache.calcite.sql.SqlUnnestOperator;
import org.apache.calcite.sql.SqlUtil;
Expand Down Expand Up @@ -1815,9 +1814,8 @@ public class SqlStdOperatorTable extends ReflectiveSqlOperatorTable {

/** The {@code PI} function. */
public static final SqlFunction PI =
SqlBasicFunction.create("PI", ReturnTypes.DOUBLE, OperandTypes.NILADIC,
SqlFunctionCategory.NUMERIC)
.withSyntax(SqlSyntax.FUNCTION_ID);
SqlBasicFunction.create(SqlKind.PI, ReturnTypes.DOUBLE, OperandTypes.NILADIC,
Copy link
Member

Choose a reason for hiding this comment

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

Does this have to be changed to SqlKind.PI?
The others look good

SqlFunctionCategory.NUMERIC);

/** {@code FIRST} function to be used within {@code MATCH_RECOGNIZE}. */
public static final SqlFunction FIRST =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,53 @@ private static String toSql(RelNode root, SqlDialect dialect,
.withStarRocks().ok(expectedStarRocks);
}

/** Test case for <a href="https://issues.apache.org/jira/browse/CALCITE-6566">[CALCITE-6566]</a>
* JDBC adapter should generate PI function with parentheses in most dialects. */
@Test void testPiFunction() {
String query = "select PI()";
final String expected = "SELECT PI()\n"
+ "FROM (VALUES (0)) AS \"t\" (\"ZERO\")";
final String expectedHive = "SELECT PI()";
final String expectedSpark = "SELECT PI()\n"
+ "FROM (VALUES (0)) `t` (`ZERO`)";
final String expectedMssql = "SELECT PI()\n"
+ "FROM (VALUES (0)) AS [t] ([ZERO])";
final String expectedMysql = "SELECT PI()";
final String expectedClickHouse = "SELECT PI()";
final String expectedPresto = "SELECT PI()\n"
+ "FROM (VALUES (0)) AS \"t\" (\"ZERO\")";
final String expectedOracle = "SELECT PI\n"
+ "FROM \"DUAL\"";
sql(query)
.ok(expected)
.withHive().ok(expectedHive)
.withSpark().ok(expectedSpark)
.withMssql().ok(expectedMssql)
.withMysql().ok(expectedMysql)
.withClickHouse().ok(expectedClickHouse)
.withPresto().ok(expectedPresto)
.withOracle().ok(expectedOracle);
}

@Test void testNiladicCurrentDateFunction() {
String query = "select CURRENT_DATE";
final String expected = "SELECT CURRENT_DATE AS \"CURRENT_DATE\"\n"
+ "FROM (VALUES (0)) AS \"t\" (\"ZERO\")";
final String expectedPostgresql = "SELECT CURRENT_DATE AS \"CURRENT_DATE\"\n"
+ "FROM (VALUES (0)) AS \"t\" (\"ZERO\")";
final String expectedSpark = "SELECT CURRENT_DATE `CURRENT_DATE`\n"
+ "FROM (VALUES (0)) `t` (`ZERO`)";
final String expectedMysql = "SELECT CURRENT_DATE AS `CURRENT_DATE`";
final String expectedOracle = "SELECT CURRENT_DATE \"CURRENT_DATE\"\n"
+ "FROM \"DUAL\"";
sql(query)
.ok(expected)
.withPostgresql().ok(expectedPostgresql)
.withSpark().ok(expectedSpark)
.withMysql().ok(expectedMysql)
.withOracle().ok(expectedOracle);
}

@Test void testPivotToSqlFromProductTable() {
String query = "select * from (\n"
+ " select \"shelf_width\", \"net_weight\", \"product_id\"\n"
Expand Down
6 changes: 3 additions & 3 deletions core/src/test/resources/sql/misc.iq
Original file line number Diff line number Diff line change
Expand Up @@ -1815,9 +1815,9 @@ from (values 1, 2, 3, 4, 5) as t(i);
!ok

# PI function
values pi;
values pi();
+-------------------+
| PI |
| EXPR$0 |
+-------------------+
| 3.141592653589793 |
+-------------------+
Expand All @@ -1826,7 +1826,7 @@ values pi;
!ok

# DEGREES function
values (degrees(pi), degrees(-pi / 2));
values (degrees(pi()), degrees(-pi() / 2));
+--------+--------+
| EXPR$0 | EXPR$1 |
+--------+--------+
Expand Down
Loading