Skip to content

Commit

Permalink
[CALCITE-6290] Incorrect return type for BigQuery TRUNC
Browse files Browse the repository at this point in the history
  • Loading branch information
tanclary committed Mar 4, 2024
1 parent c546d4a commit b0f7419
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ static RelDataType deriveTypeSplit(SqlOperatorBinding operatorBinding,
.withName("CEIL_BIG_QUERY")
.withReturnTypeInference(ReturnTypes.ARG0_EXCEPT_INTEGER_NULLABLE);

/** The "FLOOR(value)" function. Identical to the stadnard <code>FLOOR</code> function
/** The "FLOOR(value)" function. Identical to the standard <code>FLOOR</code> function
* except the return type should be a double if the operand is an integer. */
@LibraryOperator(libraries = {BIG_QUERY})
public static final SqlFunction FLOOR_BIG_QUERY = new SqlFloorFunction(SqlKind.FLOOR)
Expand Down Expand Up @@ -2160,10 +2160,13 @@ private static RelDataType deriveTypeMapFromEntries(SqlOperatorBinding opBinding
@LibraryOperator(libraries = {BIG_QUERY, SPARK})
public static final SqlFunction POW =
SqlStdOperatorTable.POWER.withName("POW");


/** The "TRUNC(numeric1 [, integer2])" function. Identical to the standard <code>TRUNCATE</code>
* function except the return type should be a double if numeric1 is an integer. */
@LibraryOperator(libraries = {BIG_QUERY})
public static final SqlFunction TRUNC =
SqlStdOperatorTable.TRUNCATE.withName("TRUNC");
SqlStdOperatorTable.TRUNCATE.withName("TRUNC")
.withReturnTypeInference(ReturnTypes.ARG0_EXCEPT_INTEGER_NULLABLE);

/** Infix "::" cast operator used by PostgreSQL, for example
* {@code '100'::INTEGER}. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,20 @@ private static String toSql(RelNode root, SqlDialect dialect,
+ "created_thing\nFROM foodmart.product");
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6290">[CALCITE-6290]
* Incorrect return type for BigQuery TRUNC</a>. */
@Test void testBigQueryTruncPreservesCast() {
final String query = "SELECT CAST(TRUNC(3) AS BIGINT) "
+ "as created_thing\n FROM `foodmart`.`product`";
final SqlParser.Config parserConfig =
BigQuerySqlDialect.DEFAULT.configureParser(SqlParser.config());
final Sql sql = fixture()
.withBigQuery().withLibrary(SqlLibrary.BIG_QUERY).parserConfig(parserConfig);
sql.withSql(query).ok("SELECT CAST(TRUNC(3) AS INT64) AS "
+ "created_thing\nFROM foodmart.product");
}

@Test void testSelectLiteralAgg() {
final Function<RelBuilder, RelNode> relFn = b -> b
.scan("EMP")
Expand Down

0 comments on commit b0f7419

Please sign in to comment.