org.apache.calcite.rel.type.RelDataTypeFamily Java Examples

The following examples show how to use org.apache.calcite.rel.type.RelDataTypeFamily. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SqlTypeUtil.java    From Bats with Apache License 2.0 6 votes vote down vote up
/** Returns whether a character data type can be implicitly converted to a
 * given family in a compare operation. */
private static boolean canConvertStringInCompare(RelDataTypeFamily family) {
  if (family instanceof SqlTypeFamily) {
    SqlTypeFamily sqlTypeFamily = (SqlTypeFamily) family;
    switch (sqlTypeFamily) {
    case DATE:
    case TIME:
    case TIMESTAMP:
    case INTERVAL_DAY_TIME:
    case INTERVAL_YEAR_MONTH:
    case NUMERIC:
    case APPROXIMATE_NUMERIC:
    case EXACT_NUMERIC:
    case INTEGER:
    case BOOLEAN:
      return true;
    }
  }
  return false;
}
 
Example #2
Source File: AccelCreateReflectionHandler.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
public static List<MeasureType> validate(String fieldName, RelDataTypeFamily dataTypeFamily, List<MeasureType> measures) {
  TypeHandler handle = null;
  if(dataTypeFamily instanceof SqlTypeFamily) {
    handle = TYPE_ALLOWANCES.get(dataTypeFamily);
  }

  if(handle == null) {
    throw UserException.validationError()
    .message("Unable to configure reflection on field %s because it was type %s.",
        fieldName,
        dataTypeFamily
      ).build(logger);
  }

  return handle.validate(fieldName, measures);
}
 
Example #3
Source File: SqlTypeFactoryImpl.java    From Bats with Apache License 2.0 6 votes vote down vote up
protected RelDataType canonize(RelDataType type) {
  type = super.canonize(type);
  if (!(type instanceof ObjectSqlType)) {
    return type;
  }
  ObjectSqlType objectType = (ObjectSqlType) type;
  if (!objectType.isNullable()) {
    objectType.setFamily(objectType);
  } else {
    objectType.setFamily(
        (RelDataTypeFamily) createTypeWithNullability(
            objectType,
            false));
  }
  return type;
}
 
Example #4
Source File: SqlTypeUtil.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Returns whether two types are scalar types of the same family, or struct types whose fields
 * are pairwise of the same family.
 *
 * @param type1 First type
 * @param type2 Second type
 * @return Whether types have the same family
 */
private static boolean isSameFamily(RelDataType type1, RelDataType type2) {
  if (type1.isStruct() != type2.isStruct()) {
    return false;
  }

  if (type1.isStruct()) {
    int n = type1.getFieldCount();
    if (n != type2.getFieldCount()) {
      return false;
    }
    for (Pair<RelDataTypeField, RelDataTypeField> pair
        : Pair.zip(type1.getFieldList(), type2.getFieldList())) {
      if (!isSameFamily(pair.left.getType(), pair.right.getType())) {
        return false;
      }
    }
    return true;
  }

  final RelDataTypeFamily family1 = family(type1);
  final RelDataTypeFamily family2 = family(type2);
  return family1 == family2;
}
 
Example #5
Source File: SqlTypeUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Returns whether two types are scalar types of the same family, or struct types whose fields
 * are pairwise of the same family.
 *
 * @param type1 First type
 * @param type2 Second type
 * @return Whether types have the same family
 */
private static boolean isSameFamily(RelDataType type1, RelDataType type2) {
  if (type1.isStruct() != type2.isStruct()) {
    return false;
  }

  if (type1.isStruct()) {
    int n = type1.getFieldCount();
    if (n != type2.getFieldCount()) {
      return false;
    }
    for (Pair<RelDataTypeField, RelDataTypeField> pair
        : Pair.zip(type1.getFieldList(), type2.getFieldList())) {
      if (!isSameFamily(pair.left.getType(), pair.right.getType())) {
        return false;
      }
    }
    return true;
  }

  final RelDataTypeFamily family1 = family(type1);
  final RelDataTypeFamily family2 = family(type2);
  return family1 == family2;
}
 
Example #6
Source File: SqlTypeUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Returns whether a character data type can be implicitly converted to a
 * given family in a compare operation. */
private static boolean canConvertStringInCompare(RelDataTypeFamily family) {
  if (family instanceof SqlTypeFamily) {
    SqlTypeFamily sqlTypeFamily = (SqlTypeFamily) family;
    switch (sqlTypeFamily) {
    case DATE:
    case TIME:
    case TIMESTAMP:
    case INTERVAL_DAY_TIME:
    case INTERVAL_YEAR_MONTH:
    case NUMERIC:
    case APPROXIMATE_NUMERIC:
    case EXACT_NUMERIC:
    case INTEGER:
    case BOOLEAN:
      return true;
    }
  }
  return false;
}
 
Example #7
Source File: SqlTypeFactoryImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
protected RelDataType canonize(RelDataType type) {
  type = super.canonize(type);
  if (!(type instanceof ObjectSqlType)) {
    return type;
  }
  ObjectSqlType objectType = (ObjectSqlType) type;
  if (!objectType.isNullable()) {
    objectType.setFamily(objectType);
  } else {
    objectType.setFamily(
        (RelDataTypeFamily) createTypeWithNullability(
            objectType,
            false));
  }
  return type;
}
 
Example #8
Source File: SqlTypeUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Returns the least restrictive type T, such that a value of type T can be
 * compared with values of type {@code type0} and {@code type1} using
 * {@code =}. */
public static RelDataType leastRestrictiveForComparison(
    RelDataTypeFactory typeFactory, RelDataType type1, RelDataType type2) {
  final RelDataType type =
      typeFactory.leastRestrictive(ImmutableList.of(type1, type2));
  if (type != null) {
    return type;
  }
  final RelDataTypeFamily family1 = family(type1);
  final RelDataTypeFamily family2 = family(type2);

  // If one of the arguments is of type 'ANY', we can compare.
  if (family1 == SqlTypeFamily.ANY) {
    return type2;
  }
  if (family2 == SqlTypeFamily.ANY) {
    return type1;
  }

  // If one of the arguments is of type 'NULL', we can compare.
  if (family1 == SqlTypeFamily.NULL) {
    return type2;
  }
  if (family2 == SqlTypeFamily.NULL) {
    return type1;
  }

  // We can implicitly convert from character to date, numeric, etc.
  if (family1 == SqlTypeFamily.CHARACTER
      && canConvertStringInCompare(family2)) {
    return type2;
  }
  if (family2 == SqlTypeFamily.CHARACTER
      && canConvertStringInCompare(family1)) {
    return type1;
  }

  return null;
}
 
Example #9
Source File: MultisetSqlType.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelDataTypeFamily getFamily() {
  // TODO jvs 2-Dec-2004:  This gives each multiset type its
  // own family.  But that's not quite correct; the family should
  // be based on the element type for proper comparability
  // semantics (per 4.10.4 of SQL/2003).  So either this should
  // make up canonical families dynamically, or the
  // comparison type-checking should not rely on this.  I
  // think the same goes for ROW types.
  return this;
}
 
Example #10
Source File: SqlCastFunction.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
  RelDataTypeFamily castFrom = call.getOperandType(0).getFamily();
  RelDataTypeFamily castTo = call.getOperandType(1).getFamily();
  if (castFrom instanceof SqlTypeFamily
      && castTo instanceof SqlTypeFamily
      && nonMonotonicCasts.containsEntry(castFrom, castTo)) {
    return SqlMonotonicity.NOT_MONOTONIC;
  } else {
    return call.getOperandMonotonicity(0);
  }
}
 
Example #11
Source File: SqlCastFunction.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
  RelDataTypeFamily castFrom = call.getOperandType(0).getFamily();
  RelDataTypeFamily castTo = call.getOperandType(1).getFamily();
  if (castFrom instanceof SqlTypeFamily
      && castTo instanceof SqlTypeFamily
      && nonMonotonicCasts.containsEntry(castFrom, castTo)) {
    return SqlMonotonicity.NOT_MONOTONIC;
  } else {
    return call.getOperandMonotonicity(0);
  }
}
 
Example #12
Source File: StandardConvertletTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static RelDataType consistentType(SqlRexContext cx,
    SqlOperandTypeChecker.Consistency consistency, List<RelDataType> types) {
  switch (consistency) {
  case COMPARE:
    if (SqlTypeUtil.areSameFamily(types)) {
      // All arguments are of same family. No need for explicit casts.
      return null;
    }
    final List<RelDataType> nonCharacterTypes = new ArrayList<>();
    for (RelDataType type : types) {
      if (type.getFamily() != SqlTypeFamily.CHARACTER) {
        nonCharacterTypes.add(type);
      }
    }
    if (!nonCharacterTypes.isEmpty()) {
      final int typeCount = types.size();
      types = nonCharacterTypes;
      if (nonCharacterTypes.size() < typeCount) {
        final RelDataTypeFamily family =
            nonCharacterTypes.get(0).getFamily();
        if (family instanceof SqlTypeFamily) {
          // The character arguments might be larger than the numeric
          // argument. Give ourselves some headroom.
          switch ((SqlTypeFamily) family) {
          case INTEGER:
          case NUMERIC:
            nonCharacterTypes.add(
                cx.getTypeFactory().createSqlType(SqlTypeName.BIGINT));
          }
        }
      }
    }
    // fall through
  case LEAST_RESTRICTIVE:
    return cx.getTypeFactory().leastRestrictive(types);
  default:
    return null;
  }
}
 
Example #13
Source File: ReflectionAnalyzer.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
protected Iterable<StatColumn> getStatColumnsPerField(final RelDataTypeField field) {
  final RelDataTypeFamily family = field.getType().getFamily();
  Collection<StatType> dims = DIMENSIONS.get(family);
  if (dims.isEmpty()) {
    dims = DIMENSIONS.get(SqlTypeFamily.ANY);
  }

  return FluentIterable.from(dims)
    .transform(new Function<StatType, StatColumn>() {
      @Override
      public StatColumn apply(final StatType type) {
        return new StatColumn(type, field);
      }
    });
}
 
Example #14
Source File: ReflectionValidator.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public static final List<com.dremio.service.reflection.proto.MeasureType> getDefaultMeasures(RelDataTypeFamily family){
  List<MeasureType> types = AccelCreateReflectionHandler.getDefaultMeasures(family);
  if(types == null) {
    return ImmutableList.of();
  }

  return types.stream().map(ReflectionValidator::fromSqlMeasureType).collect(Collectors.toList());
}
 
Example #15
Source File: ReflectionValidator.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public static final Set<com.dremio.service.reflection.proto.MeasureType> getValidMeasures(RelDataTypeFamily family){
  Set<MeasureType> types = AccelCreateReflectionHandler.getValidMeasures(family);
  if(types == null) {
    return ImmutableSet.of();
  }

  return types.stream().map(ReflectionValidator::fromSqlMeasureType).collect(Collectors.toSet());
}
 
Example #16
Source File: AccelCreateReflectionHandler.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public static List<MeasureType> getDefaultMeasures(RelDataTypeFamily dataTypeFamily){
  TypeHandler handler = TYPE_ALLOWANCES.get(dataTypeFamily);
  if(handler == null) {
    return ImmutableList.of();
  }

  return handler.defaultMeasures;
}
 
Example #17
Source File: AccelCreateReflectionHandler.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public static Set<MeasureType> getValidMeasures(RelDataTypeFamily dataTypeFamily){
  TypeHandler handler = TYPE_ALLOWANCES.get(dataTypeFamily);
  if(handler == null) {
    return ImmutableSet.of();
  }

  return handler.validMeasures;
}
 
Example #18
Source File: StandardConvertletTable.java    From Bats with Apache License 2.0 5 votes vote down vote up
private static RelDataType consistentType(SqlRexContext cx,
    SqlOperandTypeChecker.Consistency consistency, List<RelDataType> types) {
  switch (consistency) {
  case COMPARE:
    if (SqlTypeUtil.areSameFamily(types)) {
      // All arguments are of same family. No need for explicit casts.
      return null;
    }
    final List<RelDataType> nonCharacterTypes = new ArrayList<>();
    for (RelDataType type : types) {
      if (type.getFamily() != SqlTypeFamily.CHARACTER) {
        nonCharacterTypes.add(type);
      }
    }
    if (!nonCharacterTypes.isEmpty()) {
      final int typeCount = types.size();
      types = nonCharacterTypes;
      if (nonCharacterTypes.size() < typeCount) {
        final RelDataTypeFamily family =
            nonCharacterTypes.get(0).getFamily();
        if (family instanceof SqlTypeFamily) {
          // The character arguments might be larger than the numeric
          // argument. Give ourselves some headroom.
          switch ((SqlTypeFamily) family) {
          case INTEGER:
          case NUMERIC:
            nonCharacterTypes.add(
                cx.getTypeFactory().createSqlType(SqlTypeName.BIGINT));
          }
        }
      }
    }
    // fall through
  case LEAST_RESTRICTIVE:
    return cx.getTypeFactory().leastRestrictive(types);
  default:
    return null;
  }
}
 
Example #19
Source File: MultisetSqlType.java    From Bats with Apache License 2.0 5 votes vote down vote up
public RelDataTypeFamily getFamily() {
  // TODO jvs 2-Dec-2004:  This gives each multiset type its
  // own family.  But that's not quite correct; the family should
  // be based on the element type for proper comparability
  // semantics (per 4.10.4 of SQL/2003).  So either this should
  // make up canonical families dynamically, or the
  // comparison type-checking should not rely on this.  I
  // think the same goes for ROW types.
  return this;
}
 
Example #20
Source File: SqlTypeUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Returns the least restrictive type T, such that a value of type T can be
 * compared with values of type {@code type0} and {@code type1} using
 * {@code =}. */
public static RelDataType leastRestrictiveForComparison(
    RelDataTypeFactory typeFactory, RelDataType type1, RelDataType type2) {
  final RelDataType type =
      typeFactory.leastRestrictive(ImmutableList.of(type1, type2));
  if (type != null) {
    return type;
  }
  final RelDataTypeFamily family1 = family(type1);
  final RelDataTypeFamily family2 = family(type2);

  // If one of the arguments is of type 'ANY', we can compare.
  if (family1 == SqlTypeFamily.ANY) {
    return type2;
  }
  if (family2 == SqlTypeFamily.ANY) {
    return type1;
  }

  // If one of the arguments is of type 'NULL', we can compare.
  if (family1 == SqlTypeFamily.NULL) {
    return type2;
  }
  if (family2 == SqlTypeFamily.NULL) {
    return type1;
  }

  // We can implicitly convert from character to date, numeric, etc.
  if (family1 == SqlTypeFamily.CHARACTER
      && canConvertStringInCompare(family2)) {
    return type2;
  }
  if (family2 == SqlTypeFamily.CHARACTER
      && canConvertStringInCompare(family1)) {
    return type1;
  }

  return null;
}
 
Example #21
Source File: AccelCreateReflectionHandler.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private static List<NameAndMeasures> qualifyColumnsWithMeasures(DremioTable table, List<NameAndMeasures> measures){
  // we are using getSchema() instead of getRowType() as it doesn't always report the correct field types for View tables
  final RelDataType type = CalciteArrowHelper.wrap(table.getSchema()).toCalciteRecordType(JavaTypeFactoryImpl.INSTANCE);
  return measures.stream().map(input -> {
      RelDataTypeField field = type.getField(input.getName(), false, false);
      if(field == null){
        throw UserException.validationError()
          .message("Unable to find field %s in table %s. Available fields were: %s.",
              input.getName(),
              SqlUtils.quotedCompound(table.getPath().getPathComponents()),
              FluentIterable.from(type.getFieldNames()).transform(SqlUtils.QUOTER).join(Joiner.on(", "))
            ).build(logger);
      }

      final RelDataTypeFamily dataTypeFamily = field.getType().getFamily();
      TypeHandler handle = null;
      if(dataTypeFamily instanceof SqlTypeFamily) {
        handle = TYPE_ALLOWANCES.get(dataTypeFamily);
      }

      if(handle == null) {
        throw UserException.validationError()
        .message("Unable to configure reflection on field %s in table %s because it was type %s.",
            input.getName(),
            SqlUtils.quotedCompound(table.getPath().getPathComponents()),
            dataTypeFamily
          ).build(logger);
      }

      return new NameAndMeasures(field.getName(), handle.validate(field.getName(), input.getMeasureTypes()));
    }).collect(Collectors.toList());
}
 
Example #22
Source File: ArraySqlType.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelDataTypeFamily getFamily() {
  return this;
}
 
Example #23
Source File: MockCatalogReader.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelDataTypeFamily getFamily() {
  return delegate.getFamily();
}
 
Example #24
Source File: RexUtil.java    From calcite with Apache License 2.0 4 votes vote down vote up
public static List<RelDataTypeFamily> families(List<RelDataType> types) {
  return Lists.transform(types, RelDataType::getFamily);
}
 
Example #25
Source File: MapSqlType.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelDataTypeFamily getFamily() {
  return this;
}
 
Example #26
Source File: ObjectSqlType.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelDataTypeFamily getFamily() {
  // each UDT is in its own lonely family, until one day when
  // we support inheritance (at which time also need to implement
  // getPrecedenceList).
  return family;
}
 
Example #27
Source File: ObjectSqlType.java    From calcite with Apache License 2.0 4 votes vote down vote up
public void setFamily(RelDataTypeFamily family) {
  this.family = family;
}
 
Example #28
Source File: AbstractSqlType.java    From Bats with Apache License 2.0 4 votes vote down vote up
public RelDataTypeFamily getFamily() {
  return typeName.getFamily();
}
 
Example #29
Source File: ArraySqlType.java    From Bats with Apache License 2.0 4 votes vote down vote up
public RelDataTypeFamily getFamily() {
  return this;
}
 
Example #30
Source File: SqlTypeUtil.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Returns whether two types are comparable. They need to be scalar types of
 * the same family, or struct types whose fields are pairwise comparable.
 *
 * @param type1 First type
 * @param type2 Second type
 * @return Whether types are comparable
 */
public static boolean isComparable(RelDataType type1, RelDataType type2) {
  if (type1.isStruct() != type2.isStruct()) {
    return false;
  }

  if (type1.isStruct()) {
    int n = type1.getFieldCount();
    if (n != type2.getFieldCount()) {
      return false;
    }
    for (Pair<RelDataTypeField, RelDataTypeField> pair
        : Pair.zip(type1.getFieldList(), type2.getFieldList())) {
      if (!isComparable(pair.left.getType(), pair.right.getType())) {
        return false;
      }
    }
    return true;
  }

  final RelDataTypeFamily family1 = family(type1);
  final RelDataTypeFamily family2 = family(type2);
  if (family1 == family2) {
    return true;
  }

  // If one of the arguments is of type 'ANY', return true.
  if (family1 == SqlTypeFamily.ANY
      || family2 == SqlTypeFamily.ANY) {
    return true;
  }

  // If one of the arguments is of type 'NULL', return true.
  if (family1 == SqlTypeFamily.NULL
      || family2 == SqlTypeFamily.NULL) {
    return true;
  }

  // We can implicitly convert from character to date
  if (family1 == SqlTypeFamily.CHARACTER
      && canConvertStringInCompare(family2)
      || family2 == SqlTypeFamily.CHARACTER
      && canConvertStringInCompare(family1)) {
    return true;
  }

  return false;
}