Java Code Examples for org.apache.calcite.rel.type.RelDataType#getSqlTypeName()

The following examples show how to use org.apache.calcite.rel.type.RelDataType#getSqlTypeName() . 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 Project: calcite   File: SqlTypeUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @return true if type is one of the integer types
 */
public static boolean isIntType(RelDataType type) {
  SqlTypeName typeName = type.getSqlTypeName();
  if (typeName == null) {
    return false;
  }
  switch (typeName) {
  case TINYINT:
  case SMALLINT:
  case INTEGER:
  case BIGINT:
    return true;
  default:
    return false;
  }
}
 
Example 2
Source Project: calcite   File: SqlTests.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Helper function to get the string representation of a RelDataType
 * (include precision/scale but no charset or collation)
 *
 * @param sqlType Type
 * @return String representation of type
 */
public static String getTypeString(RelDataType sqlType) {
  switch (sqlType.getSqlTypeName()) {
  case VARCHAR:
  case CHAR:
    String actual = sqlType.getSqlTypeName().name();
    if (sqlType.getPrecision() != RelDataType.PRECISION_NOT_SPECIFIED) {
      actual = actual + "(" + sqlType.getPrecision() + ")";
    }
    if (!sqlType.isNullable()) {
      actual += " NOT NULL";
    }
    return actual;

  default:
    return sqlType.getFullTypeString();
  }
}
 
Example 3
Source Project: calcite   File: RelOptUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns whether two types are equal using 'equals'.
 *
 * @param desc1 Description of first type
 * @param type1 First type
 * @param desc2 Description of second type
 * @param type2 Second type
 * @param litmus What to do if an error is detected (types are not equal)
 * @return Whether the types are equal
 */
public static boolean eq(
    final String desc1,
    RelDataType type1,
    final String desc2,
    RelDataType type2,
    Litmus litmus) {
  // if any one of the types is ANY return true
  if (type1.getSqlTypeName() == SqlTypeName.ANY
      || type2.getSqlTypeName() == SqlTypeName.ANY) {
    return litmus.succeed();
  }

  if (!type1.equals(type2)) {
    return litmus.fail("type mismatch:\n{}:\n{}\n{}:\n{}",
        desc1, type1.getFullTypeString(),
        desc2, type2.getFullTypeString());
  }
  return litmus.succeed();
}
 
Example 4
private static boolean isSumAggOutput(RelDataType type1, RelDataType type2) {
  if (type1.getSqlTypeName() == SqlTypeName
    .DECIMAL && type2.getSqlTypeName() == SqlTypeName.DECIMAL) {
    // output of sum aggregation is always 38,inputScale
    return type1.getPrecision() == 38 && type1.getScale() == type2.getScale();
  }
  return false;
}
 
Example 5
Source Project: Bats   File: SqlTypeUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static boolean needsNullIndicator(RelDataType recordType) {
  // NOTE jvs 9-Mar-2005: It would be more storage-efficient to say that
  // no null indicator is required for structured type columns declared
  // as NOT NULL.  However, the uniformity of always having a null
  // indicator makes things cleaner in many places.
  return recordType.getSqlTypeName() == SqlTypeName.STRUCTURED;
}
 
Example 6
Source Project: dremio-oss   File: DremioArgChecker.java    License: Apache License 2.0 5 votes vote down vote up
public static Checker ofDouble(String argName) {
  return new Checker() {

    @Override
    public boolean check(RelDataType type) {
      return type.getSqlTypeName() == SqlTypeName.DOUBLE;
    }

    @Override
    public String signature() {
      return argName + " <DOUBLE>";
    }

  };
}
 
Example 7
Source Project: calcite   File: ClickHouseSqlDialect.java    License: Apache License 2.0 5 votes vote down vote up
@Override public SqlNode getCastSpec(RelDataType type) {
  if (type instanceof BasicSqlType) {
    SqlTypeName typeName = type.getSqlTypeName();
    switch (typeName) {
    case VARCHAR:
      return createSqlDataTypeSpecByName("String", typeName);
    case TINYINT:
      return createSqlDataTypeSpecByName("Int8", typeName);
    case SMALLINT:
      return createSqlDataTypeSpecByName("Int16", typeName);
    case INTEGER:
      return createSqlDataTypeSpecByName("Int32", typeName);
    case BIGINT:
      return createSqlDataTypeSpecByName("Int64", typeName);
    case FLOAT:
      return createSqlDataTypeSpecByName("Float32", typeName);
    case DOUBLE:
      return createSqlDataTypeSpecByName("Float64", typeName);
    case DATE:
      return createSqlDataTypeSpecByName("Date", typeName);
    case TIMESTAMP:
    case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
      return createSqlDataTypeSpecByName("DateTime", typeName);
    }
  }

  return super.getCastSpec(type);
}
 
Example 8
Source Project: calcite   File: HiveSqlDialect.java    License: Apache License 2.0 5 votes vote down vote up
@Override public SqlNode getCastSpec(final RelDataType type) {
  if (type instanceof BasicSqlType) {
    switch (type.getSqlTypeName()) {
    case INTEGER:
      SqlAlienSystemTypeNameSpec typeNameSpec = new SqlAlienSystemTypeNameSpec(
          "INT", type.getSqlTypeName(), SqlParserPos.ZERO);
      return new SqlDataTypeSpec(typeNameSpec, SqlParserPos.ZERO);
    }
  }
  return super.getCastSpec(type);
}
 
Example 9
Source Project: calcite   File: MysqlSqlDialect.java    License: Apache License 2.0 5 votes vote down vote up
@Override public SqlNode getCastSpec(RelDataType type) {
  switch (type.getSqlTypeName()) {
  case VARCHAR:
    // MySQL doesn't have a VARCHAR type, only CHAR.
    int vcMaxPrecision = this.getTypeSystem().getMaxPrecision(SqlTypeName.CHAR);
    int precision = type.getPrecision();
    if (vcMaxPrecision > 0 && precision > vcMaxPrecision) {
      precision = vcMaxPrecision;
    }
    return new SqlDataTypeSpec(
        new SqlBasicTypeNameSpec(SqlTypeName.CHAR, precision, SqlParserPos.ZERO),
        SqlParserPos.ZERO);
  case INTEGER:
  case BIGINT:
    return new SqlDataTypeSpec(
        new SqlAlienSystemTypeNameSpec(
            "SIGNED",
            type.getSqlTypeName(),
            SqlParserPos.ZERO),
        SqlParserPos.ZERO);
  case TIMESTAMP:
    return new SqlDataTypeSpec(
        new SqlAlienSystemTypeNameSpec(
            "DATETIME",
            type.getSqlTypeName(),
            SqlParserPos.ZERO),
        SqlParserPos.ZERO);
  }
  return super.getCastSpec(type);
}
 
Example 10
Source Project: calcite   File: SqlTypeUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @return class name of the numeric data type.
 */
@Deprecated // to be removed before 2.0
public static String getNumericJavaClassName(RelDataType type) {
  if (type == null) {
    return null;
  }
  SqlTypeName typeName = type.getSqlTypeName();
  if (typeName == null) {
    return null;
  }

  switch (typeName) {
  case TINYINT:
    return "Byte";
  case SMALLINT:
    return "Short";
  case INTEGER:
    return "Integer";
  case BIGINT:
    return "Long";
  case REAL:
    return "Float";
  case DECIMAL:
  case FLOAT:
  case DOUBLE:
    return "Double";
  default:
    return null;
  }
}
 
Example 11
Source Project: calcite   File: EnumerableTableScan.java    License: Apache License 2.0 5 votes vote down vote up
private Expression fieldExpression(ParameterExpression row_, int i,
    PhysType physType, JavaRowFormat format) {
  final Expression e =
      format.field(row_, i, null, physType.getJavaFieldType(i));
  final RelDataType relFieldType =
      physType.getRowType().getFieldList().get(i).getType();
  switch (relFieldType.getSqlTypeName()) {
  case ARRAY:
  case MULTISET:
    final RelDataType fieldType = relFieldType.getComponentType();
    if (fieldType.isStruct()) {
      // We can't represent a multiset or array as a List<Employee>, because
      // the consumer does not know the element type.
      // The standard element type is List.
      // We need to convert to a List<List>.
      final JavaTypeFactory typeFactory =
              (JavaTypeFactory) getCluster().getTypeFactory();
      final PhysType elementPhysType = PhysTypeImpl.of(
              typeFactory, fieldType, JavaRowFormat.CUSTOM);
      final MethodCallExpression e2 =
              Expressions.call(BuiltInMethod.AS_ENUMERABLE2.method, e);
      final Expression e3 = elementPhysType.convertTo(e2, JavaRowFormat.LIST);
      return Expressions.call(e3, BuiltInMethod.ENUMERABLE_TO_LIST.method);
    } else {
      return e;
    }
  default:
    return e;
  }
}
 
Example 12
Source Project: Bats   File: RexBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private static Comparable zeroValue(RelDataType type) {
  switch (type.getSqlTypeName()) {
  case CHAR:
    return new NlsString(Spaces.of(type.getPrecision()), null, null);
  case VARCHAR:
    return new NlsString("", null, null);
  case BINARY:
    return new ByteString(new byte[type.getPrecision()]);
  case VARBINARY:
    return ByteString.EMPTY;
  case TINYINT:
  case SMALLINT:
  case INTEGER:
  case BIGINT:
  case DECIMAL:
  case FLOAT:
  case REAL:
  case DOUBLE:
    return BigDecimal.ZERO;
  case BOOLEAN:
    return false;
  case TIME:
  case DATE:
  case TIMESTAMP:
    return DateTimeUtils.ZERO_CALENDAR;
  case TIME_WITH_LOCAL_TIME_ZONE:
    return new TimeString(0, 0, 0);
  case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
    return new TimestampString(0, 0, 0, 0, 0, 0);
  default:
    throw Util.unexpected(type.getSqlTypeName());
  }
}
 
Example 13
Source Project: dremio-oss   File: CompareFunction.java    License: Apache License 2.0 5 votes vote down vote up
private boolean isTemporal(RelDataType rt){
  switch(rt.getSqlTypeName()){
  case DATE:
  case TIME:
  case TIMESTAMP:
    return true;
  default:
    return false;
  }
}
 
Example 14
Source Project: Bats   File: SqlTypeFactoryImpl.java    License: Apache License 2.0 5 votes vote down vote up
private RelDataType leastRestrictiveByCast(List<RelDataType> types) {
  RelDataType resultType = types.get(0);
  boolean anyNullable = resultType.isNullable();
  for (int i = 1; i < types.size(); i++) {
    RelDataType type = types.get(i);
    if (type.getSqlTypeName() == SqlTypeName.NULL) {
      anyNullable = true;
      continue;
    }

    if (type.isNullable()) {
      anyNullable = true;
    }

    if (SqlTypeUtil.canCastFrom(type, resultType, false)) {
      resultType = type;
    } else {
      if (!SqlTypeUtil.canCastFrom(resultType, type, false)) {
        return null;
      }
    }
  }
  if (anyNullable) {
    return createTypeWithNullability(resultType, true);
  } else {
    return resultType;
  }
}
 
Example 15
Source Project: dremio-oss   File: DremioArgChecker.java    License: Apache License 2.0 5 votes vote down vote up
private boolean checkOp(Checker checker, SqlCallBinding callBinding, SqlNode node, int iFormalOperand,
    boolean throwOnFailure) {

  if (SqlUtil.isNullLiteral(node, false)) {
    if (throwOnFailure) {
      throw callBinding.getValidator().newValidationError(node,
          RESOURCE.nullIllegal());
    } else {
      return false;
    }
  }
  RelDataType type = callBinding.getValidator().deriveType(callBinding.getScope(), node);
  SqlTypeName typeName = type.getSqlTypeName();

  // Pass type checking for operators if it's of type 'ANY'.
  if (typeName.getFamily() == SqlTypeFamily.ANY && allowAny) {
    return true;
  }

  if (!checker.check(type)) {
    if (throwOnFailure) {
      throw callBinding.newValidationSignatureError();
    }
    return false;
  }
  return true;
}
 
Example 16
Source Project: calcite   File: OracleSqlDialect.java    License: Apache License 2.0 5 votes vote down vote up
@Override public boolean supportsDataType(RelDataType type) {
  switch (type.getSqlTypeName()) {
  case BOOLEAN:
    return false;
  default:
    return super.supportsDataType(type);
  }
}
 
Example 17
Source Project: dremio-oss   File: MoreRelOptUtil.java    License: Apache License 2.0 4 votes vote down vote up
private static boolean checkRowTypesCompatiblity(
    RelDataType rowType1,
    RelDataType rowType2,
    boolean compareNames,
    boolean allowSubstring,
    boolean insertOp) {
  if (rowType1 == rowType2) {
    return true;
  }
  if (compareNames) {
    // if types are not identity-equal, then either the names or
    // the types must be different
    return false;
  }
  if (rowType2.getFieldCount() != rowType1.getFieldCount()) {
    return false;
  }
  final List<RelDataTypeField> f1 = rowType1.getFieldList();
  final List<RelDataTypeField> f2 = rowType2.getFieldList();
  for (Pair<RelDataTypeField, RelDataTypeField> pair : Pair.zip(f1, f2)) {
    final RelDataType type1 = pair.left.getType();
    final RelDataType type2 = pair.right.getType();
    // If one of the types is ANY comparison should succeed
    if (type1.getSqlTypeName() == SqlTypeName.ANY
      || type2.getSqlTypeName() == SqlTypeName.ANY) {
      continue;
    }
    if (!(type1.toString().equals(type2.toString()))) {
      if (allowSubstring
          && (type1.getSqlTypeName() == SqlTypeName.CHAR && type2.getSqlTypeName() == SqlTypeName.CHAR)
          && (type1.getPrecision() <= type2.getPrecision())) {
        continue;
      }

      // Check if Dremio implicit casting can resolve the incompatibility
      List<TypeProtos.MinorType> types = Lists.newArrayListWithCapacity(2);
      TypeProtos.MinorType minorType1 = Types.getMinorTypeFromName(type1.getSqlTypeName().getName());
      TypeProtos.MinorType minorType2 = Types.getMinorTypeFromName(type2.getSqlTypeName().getName());
      types.add(minorType1);
      types.add(minorType2);
      if (insertOp) {
        // Insert is more strict than normal select in terms of implicit casts
        // Return false if TypeCastRules do not allow implicit cast
        if (TypeCastRules.isCastable(minorType1, minorType2, true) &&
          TypeCastRules.getLeastRestrictiveTypeForInsert(types) != null) {
          if (TypeCastRules.isCastSafeFromDataTruncation(type1, type2)) {
            continue;
          }
        }
      } else {
        if (TypeCastRules.getLeastRestrictiveType(types) != null) {
          continue;
        }
      }

      return false;
    }
  }
  return true;
}
 
Example 18
Source Project: Bats   File: RelStructuredTypeFlattener.java    License: Apache License 2.0 4 votes vote down vote up
private RelDataType removeDistinct(RelDataType type) {
    if (type.getSqlTypeName() != SqlTypeName.DISTINCT) {
        return type;
    }
    return type.getFieldList().get(0).getType();
}
 
Example 19
Source Project: dremio-oss   File: TypeInferenceUtils.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();
  if (functions.isEmpty()) {
    return factory.createTypeWithNullability(
        factory.createSqlType(SqlTypeName.ANY),
        true);
  }

  // The following logic is just a safe play:
  // Even if any of the input arguments has ANY type,
  // it "might" still be possible to determine the return type based on other non-ANY types
  for (RelDataType type : opBinding.collectOperandTypes()) {
    if (getMinorTypeFromCalciteType(type) == TypeProtos.MinorType.LATE) {
      // This code for boolean output type is added for addressing DRILL-1729
      // In summary, if we have a boolean output function in the WHERE-CLAUSE,
      // this logic can validate and execute user queries seamlessly
      boolean allBooleanOutput = true;
      for (AbstractFunctionHolder function : functions) {
        if (!function.isReturnTypeIndependent() || function.getReturnType(null).toMinorType() != TypeProtos.MinorType.BIT) {
          allBooleanOutput = false;
          break;
        }
      }

      if(allBooleanOutput) {
        return factory.createTypeWithNullability(
            factory.createSqlType(SqlTypeName.BOOLEAN), true);
      } else {
        return factory.createTypeWithNullability(
            factory.createSqlType(SqlTypeName.ANY),
            true);
      }
    }
  }

  final AbstractFunctionHolder func = resolveFunctionHolder(opBinding, functions, isDecimalV2Enabled);
  final RelDataType returnType = getReturnType(opBinding, func);
  return returnType.getSqlTypeName() == SqlTypeName.VARBINARY
      ? createCalciteTypeWithNullability(factory, SqlTypeName.ANY, returnType.isNullable(), null)
          : returnType;
}
 
Example 20
Source Project: flink   File: RelDecorrelator.java    License: Apache License 2.0 2 votes vote down vote up
/** Returns whether one type is just a widening of another.
 *
 * <p>For example:<ul>
 * <li>{@code VARCHAR(10)} is a widening of {@code VARCHAR(5)}.
 * <li>{@code VARCHAR(10)} is a widening of {@code VARCHAR(10) NOT NULL}.
 * </ul>
 */
private boolean isWidening(RelDataType type, RelDataType type1) {
  return type.getSqlTypeName() == type1.getSqlTypeName()
      && type.getPrecision() >= type1.getPrecision();
}