org.apache.calcite.sql.type.SqlTypeFamily Java Examples

The following examples show how to use org.apache.calcite.sql.type.SqlTypeFamily. 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: ProjectRelBase.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public Boolean visitCall(RexCall call) {
  if (call.getOperator() == SqlStdOperatorTable.ITEM) {
    final RexNode op0 = call.getOperands().get(0);
    final RexNode op1 = call.getOperands().get(1);

    if (op0 instanceof RexInputRef &&
        op1 instanceof RexLiteral && ((RexLiteral) op1).getTypeName().getFamily() == SqlTypeFamily.CHARACTER) {
      return true;
    } else if (op0 instanceof RexCall &&
        op1 instanceof RexLiteral && ((RexLiteral) op1).getTypeName().getFamily() == SqlTypeFamily.CHARACTER) {
      return op0.accept(this);
    }
  }

  return false;
}
 
Example #2
Source File: PredicateAnalyzer.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * If one operand in a binary operator is a DateTime type, but the other isn't,
 * we should not push down the predicate
 * @param call current node being evaluated
 */
private static void checkForIncompatibleDateTimeOperands(RexCall call) {
  RelDataType op1 = call.getOperands().get(0).getType();
  RelDataType op2 = call.getOperands().get(1).getType();
  if ((SqlTypeFamily.DATETIME.contains(op1) && !SqlTypeFamily.DATETIME.contains(op2))
         || (SqlTypeFamily.DATETIME.contains(op2) && !SqlTypeFamily.DATETIME.contains(op1))
         || (SqlTypeFamily.DATE.contains(op1) && !SqlTypeFamily.DATE.contains(op2))
         || (SqlTypeFamily.DATE.contains(op2) && !SqlTypeFamily.DATE.contains(op1))
         || (SqlTypeFamily.TIMESTAMP.contains(op1) && !SqlTypeFamily.TIMESTAMP.contains(op2))
         || (SqlTypeFamily.TIMESTAMP.contains(op2) && !SqlTypeFamily.TIMESTAMP.contains(op1))
         || (SqlTypeFamily.TIME.contains(op1) && !SqlTypeFamily.TIME.contains(op2))
         || (SqlTypeFamily.TIME.contains(op2) && !SqlTypeFamily.TIME.contains(op1))) {
    throw new PredicateAnalyzerException("Cannot handle " + call.getKind()
        + " expression for _id field, " + call);
  }
}
 
Example #3
Source File: SqlDatePartOperator.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
public SqlDatePartOperator() {
  super(
      "DATE_PART",
      SqlKind.OTHER_FUNCTION,
      ReturnTypes.BIGINT_NULLABLE,
      null,
      OperandTypes.sequence(
          "<PERIOD LITERAL>, <DATE or TIMESTAMP or INTERVAL>",
          new EnumeratedListChecker(VALID_PERIODS.keySet()),
          OperandTypes.or(
              OperandTypes.family(SqlTypeFamily.DATE),
              OperandTypes.family(SqlTypeFamily.TIMESTAMP),
              OperandTypes.family(SqlTypeFamily.DATETIME),
              OperandTypes.family(SqlTypeFamily.DATETIME_INTERVAL),
              OperandTypes.family(SqlTypeFamily.INTERVAL_DAY_TIME),
              OperandTypes.family(SqlTypeFamily.INTERVAL_YEAR_MONTH))
          ),
          SqlFunctionCategory.SYSTEM);
}
 
Example #4
Source File: TypeCoercionTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Decision method for {@link AbstractTypeCoercion#implicitCast}. */
private void shouldCast(
    RelDataType from,
    SqlTypeFamily family,
    RelDataType expected) {
  if (family == null) {
    // ROW type do not have a family.
    return;
  }
  RelDataType castedType = ((AbstractTypeCoercion) typeCoercion).implicitCast(from, family);
  boolean equals = castedType != null
      && (from.equals(castedType)
      || SqlTypeUtil.equalSansNullability(dataTypeFactory, castedType, expected)
      || expected.getSqlTypeName().getFamily().contains(castedType));
  assert equals
      : "Failed to cast from "
      + from.getSqlTypeName()
      + " to "
      + family;
}
 
Example #5
Source File: TypeInferenceUtils.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Given a {@link SqlTypeName} and nullability, create a RelDataType from the RelDataTypeFactory
 *
 * @param typeFactory RelDataTypeFactory used to create the RelDataType
 * @param sqlTypeName the given SqlTypeName
 * @param isNullable  the nullability of the created RelDataType
 * @return RelDataType Type of call
 */
public static RelDataType createCalciteTypeWithNullability(RelDataTypeFactory typeFactory,
                                                           SqlTypeName sqlTypeName,
                                                           boolean isNullable) {
  RelDataType type;
  if (sqlTypeName.getFamily() == SqlTypeFamily.INTERVAL_DAY_TIME) {
    type = typeFactory.createSqlIntervalType(
        new SqlIntervalQualifier(
            TimeUnit.DAY,
            TimeUnit.MINUTE,
            SqlParserPos.ZERO));
  } else if (sqlTypeName.getFamily() == SqlTypeFamily.INTERVAL_YEAR_MONTH) {
    type = typeFactory.createSqlIntervalType(
        new SqlIntervalQualifier(
            TimeUnit.YEAR,
            TimeUnit.MONTH,
            SqlParserPos.ZERO));
  } else if (sqlTypeName == SqlTypeName.VARCHAR) {
    type = typeFactory.createSqlType(sqlTypeName, Types.MAX_VARCHAR_LENGTH);
  } else {
    type = typeFactory.createSqlType(sqlTypeName);
  }
  return typeFactory.createTypeWithNullability(type, isNullable);
}
 
Example #6
Source File: TypeCoercionImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
public boolean builtinFunctionCoercion(
    SqlCallBinding binding,
    List<RelDataType> operandTypes,
    List<SqlTypeFamily> expectedFamilies) {
  assert binding.getOperandCount() == operandTypes.size();
  if (!canImplicitTypeCast(operandTypes, expectedFamilies)) {
    return false;
  }
  boolean coerced = false;
  for (int i = 0; i < operandTypes.size(); i++) {
    RelDataType implicitType = implicitCast(operandTypes.get(i), expectedFamilies.get(i));
    coerced = null != implicitType
        && operandTypes.get(i) != implicitType
        && coerceOperandType(binding.getScope(), binding.getCall(), i, implicitType)
        || coerced;
  }
  return coerced;
}
 
Example #7
Source File: TypeUtils.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
public static boolean isTemporal(final RField field) {
  final Optional<SqlTypeFamily> familyOpt = getSqlTypeFamily(field);
  if (!familyOpt.isPresent()) {
    return false;
  }

  final SqlTypeFamily family = familyOpt.get();
  switch (family) {
    case DATETIME:
    case TIMESTAMP:
    case DATE:
    case TIME:
      return true;
    default:
      return false;
  }
}
 
Example #8
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 #9
Source File: SqlItemOperator.java    From calcite with Apache License 2.0 6 votes vote down vote up
private SqlSingleOperandTypeChecker getChecker(SqlCallBinding callBinding) {
  final RelDataType operandType = callBinding.getOperandType(0);
  switch (operandType.getSqlTypeName()) {
  case ARRAY:
    return OperandTypes.family(SqlTypeFamily.INTEGER);
  case MAP:
    return OperandTypes.family(
        operandType.getKeyType().getSqlTypeName().getFamily());
  case ROW:
    return OperandTypes.CHARACTER;
  case ANY:
  case DYNAMIC_STAR:
    return OperandTypes.or(
        OperandTypes.family(SqlTypeFamily.INTEGER),
        OperandTypes.family(SqlTypeFamily.CHARACTER));
  default:
    throw callBinding.newValidationSignatureError();
  }
}
 
Example #10
Source File: ReflectionValidator.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
private void validateFields(List<ReflectionField> fieldList, Map<String, ViewFieldType> schemaMap, String fieldName, Boolean mustBePrimitive) {
  if (fieldList == null) {
    return;
  }

  for (ReflectionField reflectionField : fieldList) {
    // A field must exist in the schema
    Preconditions.checkArgument(schemaMap.containsKey(reflectionField.getName()), String.format("%s field contains a field name [%s] that does not exist in the dataset", fieldName, reflectionField.getName()));

    if (mustBePrimitive) {
      ViewFieldType fieldType = schemaMap.get(reflectionField.getName());
      // We let ANY type pass primarily because pre-1.5 datasets may have valid fields marked as ANY.
      Preconditions.checkArgument(!Arrays.asList(SqlTypeFamily.ARRAY.name(), SqlTypeFamily.MAP.name()).contains(fieldType.getTypeFamily()), String.format("%s field cannot have field [%s] of type list, map or union", fieldName, reflectionField.getName()));
    }
  }
}
 
Example #11
Source File: SqlItemOperator.java    From Bats with Apache License 2.0 6 votes vote down vote up
private SqlSingleOperandTypeChecker getChecker(RelDataType operandType) {
  switch (operandType.getSqlTypeName()) {
  case ARRAY:
    return OperandTypes.family(SqlTypeFamily.INTEGER);
  case MAP:
    return OperandTypes.family(
        operandType.getKeyType().getSqlTypeName().getFamily());
  case ANY:
  case DYNAMIC_STAR:
    return OperandTypes.or(
        OperandTypes.family(SqlTypeFamily.INTEGER),
        OperandTypes.family(SqlTypeFamily.CHARACTER));
  default:
    throw new AssertionError(operandType.getSqlTypeName());
  }
}
 
Example #12
Source File: ReflectionExpander.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
private Stream<AggregateCall> toCalls(ReflectionMeasureField field) {
  Optional<SqlTypeFamily> typeFamily = getSqlTypeFamily(field.getName());
  if(!typeFamily.isPresent()) {
    // are we silently not measuring the field ? should we catch this during validation ?
    return Stream.of();
  }

  // for old systems, make sure we have a default measure list if one is not specificed.
  List<MeasureType> measures = field.getMeasureTypeList() == null || field.getMeasureTypeList().isEmpty() ? DEFAULT_MEASURE_LIST : field.getMeasureTypeList();
  List<AggregateCall> calls = new ArrayList<>();
  final int inputRef = getField(field.getName()).getIndex();
  int inFieldIndex = 0;
  for(MeasureType t : measures) {
    AggregateCall c = createMeasureFor(inputRef, inFieldIndex, typeFamily.get(), t);
    if(c == null) {
      continue;
    }
    calls.add(c);
  }
  return calls.stream();
}
 
Example #13
Source File: ReflectionExpander.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
/**
 * For a particular input and type family, create the request type if it is allowed.
 * @param inputRef The input of that this measure will be applied to.
 * @param index The index of this measure when the collection of measure for this input field.
 * @param family The type family of the field.
 * @param type The type of measure to generate.
 * @return An aggregate call or null if we can't create a measure of the requested type.
 */
private AggregateCall createMeasureFor(int inputRef, int index, SqlTypeFamily family, MeasureType type) {

  // skip measure columns for invalid types.
  if(!ReflectionValidator.getValidMeasures(family).contains(type)) {
    return null;
  }

  switch(type) {
  case APPROX_COUNT_DISTINCT:
    return AggregateCall.create(HyperLogLog.HLL, false, ImmutableList.of(inputRef), -1, 1, view, null, String.format("agg-%s-%s", inputRef, index));
  case COUNT:
    return AggregateCall.create(SqlStdOperatorTable.COUNT, false, ImmutableList.of(inputRef), -1, 1, view, null, String.format("agg-%s-%s", inputRef, index));
  case MAX:
    return AggregateCall.create(SqlStdOperatorTable.MAX, false, ImmutableList.of(inputRef), -1, 1, view, null, String.format("agg-%s-%s", inputRef, index));
  case MIN:
    return AggregateCall.create(SqlStdOperatorTable.MIN, false, ImmutableList.of(inputRef), -1, 1, view, null, String.format("agg-%s-%s", inputRef, index));
  case SUM:
    return AggregateCall.create(SqlStdOperatorTable.SUM, false, ImmutableList.of(inputRef), -1, 1, view, null, String.format("agg-%s-%s", inputRef, index));
  case UNKNOWN:
  default:
    throw new UnsupportedOperationException(type.name());
  }
}
 
Example #14
Source File: SqlJsonArrayAggAggFunction.java    From calcite with Apache License 2.0 5 votes vote down vote up
public SqlJsonArrayAggAggFunction(SqlKind kind,
    SqlJsonConstructorNullClause nullClause) {
  super(kind + "_" + nullClause.name(), null, kind, ReturnTypes.VARCHAR_2000,
      InferTypes.ANY_NULLABLE, OperandTypes.family(SqlTypeFamily.ANY),
      SqlFunctionCategory.SYSTEM, false, false, Optionality.OPTIONAL);
  this.nullClause = Objects.requireNonNull(nullClause);
}
 
Example #15
Source File: SqlImplementor.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public SqlNode toSql(RexProgram program, RexNode rex) {
    if (rex.getKind() == SqlKind.LITERAL) {
        final RexLiteral literal = (RexLiteral) rex;
        if (literal.getTypeName().getFamily() == SqlTypeFamily.CHARACTER) {
            return new SqlIdentifier(RexLiteral.stringValue(literal), POS);
        }
    }
    return super.toSql(program, rex);
}
 
Example #16
Source File: SqlJsonObjectAggAggFunction.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a SqlJsonObjectAggAggFunction. */
public SqlJsonObjectAggAggFunction(SqlKind kind,
    SqlJsonConstructorNullClause nullClause) {
  super(kind + "_" + nullClause.name(), null, kind, ReturnTypes.VARCHAR_2000,
      (callBinding, returnType, operandTypes) -> {
        RelDataTypeFactory typeFactory = callBinding.getTypeFactory();
        operandTypes[0] = typeFactory.createSqlType(SqlTypeName.VARCHAR);
        operandTypes[1] = typeFactory.createTypeWithNullability(
            typeFactory.createSqlType(SqlTypeName.ANY), true);
      }, OperandTypes.family(SqlTypeFamily.CHARACTER,
          SqlTypeFamily.ANY),
      SqlFunctionCategory.SYSTEM, false, false, Optionality.FORBIDDEN);
  this.nullClause = Objects.requireNonNull(nullClause);
}
 
Example #17
Source File: SqlTimestampDiffFunction.java    From calcite with Apache License 2.0 5 votes vote down vote up
SqlTimestampDiffFunction() {
  super("TIMESTAMPDIFF", SqlKind.TIMESTAMP_DIFF,
      RETURN_TYPE_INFERENCE, null,
      OperandTypes.family(SqlTypeFamily.ANY, SqlTypeFamily.DATETIME,
          SqlTypeFamily.DATETIME),
      SqlFunctionCategory.TIMEDATE);
}
 
Example #18
Source File: SqlTimestampAddFunction.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a SqlTimestampAddFunction. */
SqlTimestampAddFunction() {
  super("TIMESTAMPADD", SqlKind.TIMESTAMP_ADD, RETURN_TYPE_INFERENCE, null,
      OperandTypes.family(SqlTypeFamily.ANY, SqlTypeFamily.INTEGER,
          SqlTypeFamily.DATETIME),
      SqlFunctionCategory.TIMEDATE);
}
 
Example #19
Source File: SqlDotOperator.java    From calcite with Apache License 2.0 5 votes vote down vote up
private SqlSingleOperandTypeChecker getChecker(RelDataType operandType) {
  switch (operandType.getSqlTypeName()) {
  case ROW:
    return OperandTypes.family(SqlTypeFamily.STRING);
  default:
    throw new AssertionError(operandType.getSqlTypeName());
  }
}
 
Example #20
Source File: SqlImplementor.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public SqlNode toSql(RexProgram program, RexNode rex) {
  if (rex.getKind() == SqlKind.LITERAL) {
    final RexLiteral literal = (RexLiteral) rex;
    if (literal.getTypeName().getFamily() == SqlTypeFamily.CHARACTER) {
      return new SqlIdentifier(RexLiteral.stringValue(literal), POS);
    }
  }
  return super.toSql(program, rex);
}
 
Example #21
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 #22
Source File: StreamRecordTimestampSqlFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
public StreamRecordTimestampSqlFunction() {
	super(
		"STREAMRECORD_TIMESTAMP",
		SqlKind.OTHER_FUNCTION,
		ReturnTypes.explicit(SqlTypeName.BIGINT),
		InferTypes.RETURN_TYPE,
		OperandTypes.family(SqlTypeFamily.NUMERIC),
		SqlFunctionCategory.SYSTEM);
}
 
Example #23
Source File: TypeCoercionTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
private void shouldNotCast(
    RelDataType from,
    SqlTypeFamily family) {
  if (family == null) {
    // ROW type do not have a family.
    return;
  }
  RelDataType castedType = ((AbstractTypeCoercion) typeCoercion).implicitCast(from, family);
  assert castedType == null
      : "Should not be able to cast from "
      + from.getSqlTypeName()
      + " to "
      + family;
}
 
Example #24
Source File: OLAPAggregateRel.java    From kylin with Apache License 2.0 5 votes vote down vote up
SqlAggFunction createCustomAggFunction(String funcName, RelDataType returnType, Class<?> customAggFuncClz) {
    RelDataTypeFactory typeFactory = getCluster().getTypeFactory();
    SqlIdentifier sqlIdentifier = new SqlIdentifier(funcName, new SqlParserPos(1, 1));
    AggregateFunction aggFunction = AggregateFunctionImpl.create(customAggFuncClz);
    List<RelDataType> argTypes = new ArrayList<RelDataType>();
    List<SqlTypeFamily> typeFamilies = new ArrayList<SqlTypeFamily>();
    for (FunctionParameter o : aggFunction.getParameters()) {
        final RelDataType type = o.getType(typeFactory);
        argTypes.add(type);
        typeFamilies.add(Util.first(type.getSqlTypeName().getFamily(), SqlTypeFamily.ANY));
    }
    return new SqlUserDefinedAggFunction(sqlIdentifier, ReturnTypes.explicit(returnType),
            InferTypes.explicit(argTypes), OperandTypes.family(typeFamilies), aggFunction, false, false,
            typeFactory);
}
 
Example #25
Source File: TypeUtils.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public static Optional<SqlTypeFamily> getSqlTypeFamily(final RField field) {
  try {
    return Optional.of(SqlTypeFamily.valueOf(field.getTypeFamily()));
  } catch (final IllegalArgumentException ex) {
    return Optional.absent();
  }
}
 
Example #26
Source File: TypeUtils.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public static boolean isBoolean(final RField field) {
  final Optional<SqlTypeFamily> familyOpt = getSqlTypeFamily(field);
  if (!familyOpt.isPresent()) {
    return false;
  }
  final SqlTypeFamily family = familyOpt.get();
  return family == SqlTypeFamily.BOOLEAN;
}
 
Example #27
Source File: TypeUtils.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public static boolean isNumeric(final RField field) {
  final Optional<SqlTypeFamily> familyOpt = getSqlTypeFamily(field);
  if (!familyOpt.isPresent()) {
    return false;
  }

  final SqlTypeFamily family = familyOpt.get();
  return family == SqlTypeFamily.NUMERIC;
}
 
Example #28
Source File: ReflectionValidator.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private void validateDimensions(List<ReflectionDimensionField> fieldList, Map<String, ViewFieldType> schemaMap) {
  if (fieldList == null) {
    return;
  }

  for (ReflectionDimensionField reflectionField : fieldList) {
    // A field must exist in the schema
    Preconditions.checkArgument(schemaMap.containsKey(reflectionField.getName()), String.format("dimension field contains a field [%s] that does exist in the dataset", reflectionField.getName()));

    ViewFieldType fieldType = schemaMap.get(reflectionField.getName());
    // We let ANY type pass primarily because pre-1.5 datasets may have valid fields marked as ANY.
    Preconditions.checkArgument(!Arrays.asList(SqlTypeFamily.ARRAY.name(), SqlTypeFamily.MAP.name()).contains(fieldType.getTypeFamily()), String.format("dimension field cannot have field [%s] of type list, map or union", reflectionField.getName()));
  }
}
 
Example #29
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(String family){
  try {
    return getDefaultMeasures(SqlTypeFamily.valueOf(family));
  } catch (RuntimeException e) {
    return ImmutableList.of();
  }
}
 
Example #30
Source File: ReflectionValidator.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private void validateMeasures(List<ReflectionMeasureField> fieldList, Map<String, ViewFieldType> schemaMap, String fieldName) {
  if (fieldList == null) {
    return;
  }

  for (ReflectionMeasureField measureField : fieldList) {
    // A field must exist in the schema
    Preconditions.checkArgument(schemaMap.containsKey(measureField.getName()), String.format("%s field contains a field name [%s] that does not exist in the dataset", fieldName, measureField.getName()));

    ViewFieldType fieldType = schemaMap.get(measureField.getName());
    // We let ANY type pass primarily because pre-1.5 datasets may have valid fields marked as ANY.
    Preconditions.checkArgument(!Arrays.asList(SqlTypeFamily.ARRAY.name(), SqlTypeFamily.MAP.name()).contains(fieldType.getTypeFamily()), String.format("%s field cannot have field [%s] of type list, map or union", fieldName, measureField.getName()));


    final SqlTypeFamily family;
    try {
      family = SqlTypeFamily.valueOf(fieldType.getTypeFamily());
    } catch (RuntimeException ex) {
      throw UserException.validationError().message("Field %s cannot be configured as measure due to unexpected type of %s." ).build(logger);
    }
    final List<MeasureType> measures = AccelerationUtils.selfOrEmpty(measureField.getMeasureTypeList())
      .stream()
      .map(ReflectionValidator::toSqlMeasureType)
      .collect(Collectors.toList());

    // validate field types
    AccelCreateReflectionHandler.validate(fieldName, family, measures);
  }
}