You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by "snuyanzin (via GitHub)" <gi...@apache.org> on 2024/03/05 12:04:42 UTC

Re: [PR] [CALCITE-6015] AssertionError during optimization of EXTRACT expression [calcite]

snuyanzin commented on code in PR #3435:
URL: https://github.com/apache/calcite/pull/3435#discussion_r1512708973


##########
core/src/main/java/org/apache/calcite/sql/fun/SqlExtractFunction.java:
##########
@@ -83,8 +88,102 @@ public SqlExtractFunction(String name) {
     //    startUnit = EPOCH and timeFrameName = 'MINUTE15'.
     //
     // If the latter, check that timeFrameName is valid.
-    validator.validateTimeFrame(
-        (SqlIntervalQualifier) call.getOperandList().get(0));
+    SqlIntervalQualifier qualifier = call.operand(0);
+    validator.validateTimeFrame(qualifier);
+    TimeUnitRange range = qualifier.timeUnitRange;
+
+    RelDataType type = validator.getValidatedNodeTypeIfKnown(call.operand(1));
+    if (type == null) {
+      return;
+    }
+
+    SqlTypeName typeName = type.getSqlTypeName();
+    boolean legal;
+    switch (range) {
+    case YEAR:
+    case MONTH:
+    case ISOYEAR:
+    case QUARTER:
+    case DECADE:
+    case CENTURY:
+    case MILLENNIUM:
+      legal = new ImmutableList.Builder<SqlTypeName>()
+          .add(SqlTypeName.DATE)
+          .add(SqlTypeName.TIMESTAMP)
+          .add(SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE)
+          .addAll(SqlTypeName.YEAR_INTERVAL_TYPES)
+          .build()
+          .contains(typeName);
+      break;
+    case WEEK:
+    case DOW:
+    case ISODOW:
+    case DOY:
+      legal = new ImmutableList.Builder<SqlTypeName>()

Review Comment:
   should we extract the result of the builder here and others into constants (may be sets instead of lists) to avoid building it each time we come here?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org