Skip to content

Commit

Permalink
Override JDBC type for MEASURE columns
Browse files Browse the repository at this point in the history
  • Loading branch information
tjbanghart committed Mar 24, 2023
1 parent f8f8dc7 commit ba8bb5c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
11 changes: 10 additions & 1 deletion core/src/main/java/org/apache/calcite/jdbc/CalciteMetaImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;

import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -458,12 +459,20 @@ public Enumerable<MetaColumn> columns(final MetaTable table_) {
instanceof RelDataTypeFactoryImpl.JavaType)
? field.getType().getPrecision()
: -1;
// MEASURE is a special case. We want to surface the type returned
// after aggregation rather than its default java.sql.Type of `OTHER(1111)`
final int jdbcOrdinal =
Optional
.ofNullable(field.getType().getMeasureElementType())
.map(RelDataType::getSqlTypeName)
.map(SqlTypeName::getJdbcOrdinal)
.orElse(field.getType().getSqlTypeName().getJdbcOrdinal());
return new MetaColumn(
table.tableCat,
table.tableSchem,
table.tableName,
field.getName(),
field.getType().getSqlTypeName().getJdbcOrdinal(),
jdbcOrdinal,
field.getType().getFullTypeString(),
precision,
field.getType().getSqlTypeName().allowsScale()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ public interface RelDataType {
*/
@Nullable RelDataType getValueType();

/**
* Gets the element type if this type is a measure, otherwise null.
*
* @return canonical type descriptor for the value used in the measure
*/
default @Nullable RelDataType getMeasureElementType() {
return null;
}

/**
* Gets this type's character set, or null if this type cannot carry a
* character set or has no character set defined.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,18 @@
* into an expression before use.
*/
public class MeasureSqlType extends ApplySqlType {
private final RelDataType elementType;
/** Private constructor. */
private MeasureSqlType(RelDataType elementType, boolean isNullable) {
super(SqlTypeName.MEASURE, isNullable, ImmutableList.of(elementType));
this.elementType = elementType;
computeDigest();
}

@Override public RelDataType getMeasureElementType() {
return elementType;
}

/** Creates a MeasureSqlType. */
static MeasureSqlType create(RelDataType elementType) {
return new MeasureSqlType(elementType, elementType.isNullable());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8966,8 +8966,8 @@ void testTimestampDiff(boolean coercionEnabled) {
f.checkScalar("FORMAT_DATE('%x', DATE '2008-12-25')",
"12/25/08",
"VARCHAR(2000) NOT NULL");
f.checkScalar("FORMAT_DATE('The date is: %x', DATE '2008-12-25')",
"The date is: 12/25/08",
f.checkScalar("FORMAT_DATE('The date is: %x', DATE '2008-12-01')",
"The date is: 12/01/08",
"VARCHAR(2000) NOT NULL");
}

Expand Down

0 comments on commit ba8bb5c

Please sign in to comment.