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

feat: add support for current_date and current_timestamp #160

Closed
Closed
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
@@ -1,11 +1,15 @@
package io.substrait.isthmus;

import org.apache.calcite.jdbc.JavaTypeFactoryImpl;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeFactory;
import org.apache.calcite.rel.type.RelDataTypeSystem;
import org.apache.calcite.rel.type.RelDataTypeSystemImpl;
import org.apache.calcite.sql.type.SqlTypeName;

import java.lang.reflect.Type;
import java.util.function.Function;

public class SubstraitTypeSystem extends RelDataTypeSystemImpl {
static final org.slf4j.Logger logger =
org.slf4j.LoggerFactory.getLogger(SubstraitTypeSystem.class);
Expand All @@ -14,8 +18,7 @@ public class SubstraitTypeSystem extends RelDataTypeSystemImpl {

private SubstraitTypeSystem() {}

@Override
public int getMaxPrecision(final SqlTypeName typeName) {
static private int fixedTimePrecision(final SqlTypeName typeName, final int otherwise) {
switch (typeName) {
case INTERVAL_DAY:
case INTERVAL_YEAR:
Expand All @@ -25,8 +28,14 @@ public int getMaxPrecision(final SqlTypeName typeName) {
case TIMESTAMP:
case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
return 6;
default:
return otherwise;
}
return super.getMaxPrecision(typeName);
}

@Override
public int getMaxPrecision(final SqlTypeName typeName) {
return fixedTimePrecision(typeName, super.getMaxPrecision(typeName));
}

@Override
Expand All @@ -40,6 +49,14 @@ public int getMaxNumericPrecision() {
}

public static RelDataTypeFactory createTypeFactory() {
return new JavaTypeFactoryImpl(TYPE_SYSTEM);
return new JavaTypeFactoryImpl(TYPE_SYSTEM) {
@Override public RelDataType createType(Type type) {
return super.createType(type);
}

@Override public RelDataType createSqlType(SqlTypeName typeName, int precision) {
return super.createSqlType(typeName, fixedTimePrecision(typeName, precision));
}
};
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: newline maybe?

Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ public class FunctionMappings {
s(SqlStdOperatorTable.UPPER, "upper"),
s(SqlStdOperatorTable.BETWEEN),
s(SqlStdOperatorTable.IS_NOT_DISTINCT_FROM, "is_not_distinct_from"),
s(SqlStdOperatorTable.COALESCE, "coalesce"))
s(SqlStdOperatorTable.COALESCE, "coalesce"),
s(SqlStdOperatorTable.CURRENT_DATE, "current_date"),
s(SqlStdOperatorTable.CURRENT_TIMESTAMP, "current_timestamp"))
.build();

AGGREGATE_SIGS =
Expand Down
10 changes: 10 additions & 0 deletions isthmus/src/test/java/io/substrait/isthmus/CalciteCallTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ public void not() {
test("not:bool", rex.makeCall(NOT, c(false, SqlTypeName.BOOLEAN)));
}

@Test
public void currentDate() throws IOException {
test("current_date:", rex.makeCall(CURRENT_DATE));
}

@Test
public void currentTimestamp() throws IOException {
test("current_timestamp:", rex.makeCall(CURRENT_TIMESTAMP) );
}

private void test(String expectedName, RexNode call) {
test(expectedName, call, c -> {}, true);
}
Expand Down