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 1 commit
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
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 @@ -33,6 +33,7 @@
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.type.BasicSqlType;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.util.RelToSqlConverterUtil;

import org.checkerframework.checker.nullness.qual.Nullable;

Expand Down Expand Up @@ -203,6 +204,9 @@ private static SqlDataTypeSpec createSqlDataTypeSpecByName(String typeAlias,
call.operand(1).unparse(writer, 0, 0);
writer.endList(frame);
break;
case PI:
RelToSqlConverterUtil.unparsePI(writer, call, leftPrec, rightPrec);
break;
default:
super.unparseCall(writer, call, leftPrec, rightPrec);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ public HiveSqlDialect(Context context) {
case TRIM:
RelToSqlConverterUtil.unparseHiveTrim(writer, call, leftPrec, rightPrec);
break;
case PI:
RelToSqlConverterUtil.unparsePI(writer, call, leftPrec, rightPrec);
break;
default:
super.unparseCall(writer, call, leftPrec, rightPrec);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.apache.calcite.sql.type.OperandTypes;
import org.apache.calcite.sql.type.ReturnTypes;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.util.RelToSqlConverterUtil;

import org.checkerframework.checker.nullness.qual.Nullable;

Expand Down Expand Up @@ -172,7 +173,9 @@ public MssqlSqlDialect(Context context) {
}
unparseFloor(writer, call);
break;

case PI:
RelToSqlConverterUtil.unparsePI(writer, call, leftPrec, rightPrec);
break;
default:
super.unparseCall(writer, call, leftPrec, rightPrec);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.apache.calcite.sql.type.OperandTypes;
import org.apache.calcite.sql.type.ReturnTypes;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.util.RelToSqlConverterUtil;

import com.google.common.collect.ImmutableList;

Expand Down Expand Up @@ -242,7 +243,6 @@ public MysqlSqlDialect(Context context) {

unparseFloor(writer, call);
break;

case WITHIN_GROUP:
final List<SqlNode> operands = call.getOperandList();
if (operands.size() <= 0 || operands.get(0).getKind() != SqlKind.LISTAGG) {
Expand All @@ -252,11 +252,12 @@ public MysqlSqlDialect(Context context) {
unparseListAggCall(writer, (SqlCall) operands.get(0),
operands.size() == 2 ? operands.get(1) : null, leftPrec, rightPrec);
break;

case LISTAGG:
unparseListAggCall(writer, call, null, leftPrec, rightPrec);
break;

case PI:
RelToSqlConverterUtil.unparsePI(writer, call, leftPrec, rightPrec);
break;
default:
super.unparseCall(writer, call, leftPrec, rightPrec);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.util.RelToSqlConverterUtil;

import com.google.common.collect.ImmutableList;

Expand Down Expand Up @@ -177,6 +178,9 @@ public PostgresqlSqlDialect(Context context) {
timeUnitNode.getParserPosition());
SqlFloorFunction.unparseDatetimeFunction(writer, call2, "DATE_TRUNC", false);
break;
case PI:
RelToSqlConverterUtil.unparsePI(writer, call, leftPrec, rightPrec);
break;
default:
super.unparseCall(writer, call, leftPrec, rightPrec);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ private static void unparseUsingLimit(SqlWriter writer, @Nullable SqlNode offset
case MAP_VALUE_CONSTRUCTOR:
unparseMapValue(writer, call, leftPrec, rightPrec);
break;
case PI:
RelToSqlConverterUtil.unparsePI(writer, call, leftPrec, rightPrec);
break;
default:
// Current impl is same with Postgresql.
PostgresqlSqlDialect.DEFAULT.unparseCall(writer, call, leftPrec, rightPrec);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.type.BasicSqlType;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.util.RelToSqlConverterUtil;

import org.checkerframework.checker.nullness.qual.Nullable;

Expand Down Expand Up @@ -139,6 +140,9 @@ public SparkSqlDialect(SqlDialect.Context context) {
case POSITION:
SqlUtil.unparseFunctionSyntax(SqlStdOperatorTable.POSITION, writer, call, false);
break;
case PI:
RelToSqlConverterUtil.unparsePI(writer, call, leftPrec, rightPrec);
break;
default:
super.unparseCall(writer, call, leftPrec, rightPrec);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1815,7 +1815,7 @@ public class SqlStdOperatorTable extends ReflectiveSqlOperatorTable {

/** The {@code PI} function. */
public static final SqlFunction PI =
SqlBasicFunction.create("PI", ReturnTypes.DOUBLE, OperandTypes.NILADIC,
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)
.withSyntax(SqlSyntax.FUNCTION_ID);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,27 @@ public static void unparseSparkArrayAndMap(SqlWriter writer,
writer.endList(frame);
}

/**
* Unparses PI function without parentheses.
*
* <p>For example :
*
* <blockquote><pre>
* SELECT PI &rarr; SELECT PI()
* </pre></blockquote>
*
* @param writer writer
* @param call the call
*/
public static void unparsePI(
SqlWriter writer,
SqlCall call,
int leftPrec,
int rightPrec) {
final SqlWriter.Frame trimFrame = writer.startFunCall(call.getKind().name());
writer.endFunCall(trimFrame);
}

/**
* Unparses TRIM function with value as space.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,29 @@ 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
* When unparse PI function without parentheses. */
@Test void testPiFunction() {
String query = "select PI";
final String expected = "SELECT PI AS \"PI\"\nFROM (VALUES (0)) AS \"t\" (\"ZERO\")";
final String expectedHive = "SELECT PI() `PI`";
final String expectedSpark = "SELECT PI() `PI`\nFROM (VALUES (0)) `t` (`ZERO`)";
final String expectedMssql = "SELECT PI() AS [PI]\nFROM (VALUES (0)) AS [t] ([ZERO])";
final String expectedMysql = "SELECT PI() AS `PI`";
final String expectedClickHouse = "SELECT PI() AS `PI`";
final String expectedPresto = "SELECT PI() AS \"PI\"\nFROM (VALUES (0)) AS \"t\" (\"ZERO\")";
final String expectedOracle = "SELECT PI \"PI\"\nFROM \"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 testPivotToSqlFromProductTable() {
String query = "select * from (\n"
+ " select \"shelf_width\", \"net_weight\", \"product_id\"\n"
Expand Down
Loading