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

The following examples show how to use org.apache.calcite.rel.type.RelDataTypeFactory. 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: KylinRelDataTypeSystem.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
@Override
public RelDataType deriveSumType(RelDataTypeFactory typeFactory, RelDataType argumentType) {
    if (argumentType instanceof BasicSqlType) {
        switch (argumentType.getSqlTypeName()) {
        case INTEGER:
        case SMALLINT:
        case TINYINT:
            return typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.BIGINT),
                    argumentType.isNullable());
        case DECIMAL:
            return typeFactory.createTypeWithNullability(
                    typeFactory.createSqlType(SqlTypeName.DECIMAL, 19, argumentType.getScale()),
                    argumentType.isNullable());
        default:
            break;
        }
    }
    return argumentType;
}
 
Example #2
Source File: RexBuilder.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a RexBuilder.
 *
 * @param typeFactory Type factory
 */
public RexBuilder(RelDataTypeFactory typeFactory) {
  this.typeFactory = typeFactory;
  this.booleanTrue =
      makeLiteral(
          Boolean.TRUE,
          typeFactory.createSqlType(SqlTypeName.BOOLEAN),
          SqlTypeName.BOOLEAN);
  this.booleanFalse =
      makeLiteral(
          Boolean.FALSE,
          typeFactory.createSqlType(SqlTypeName.BOOLEAN),
          SqlTypeName.BOOLEAN);
  this.charEmpty =
      makeLiteral(
          new NlsString("", null, null),
          typeFactory.createSqlType(SqlTypeName.CHAR, 0),
          SqlTypeName.CHAR);
  this.constantNull =
      makeLiteral(
          null,
          typeFactory.createSqlType(SqlTypeName.NULL),
          SqlTypeName.NULL);
}
 
Example #3
Source File: AggregateCall.java    From Bats with Apache License 2.0 6 votes vote down vote up
/** Creates an AggregateCall, inferring its type if {@code type} is null. */
public static AggregateCall create(SqlAggFunction aggFunction,
    boolean distinct, boolean approximate, List<Integer> argList,
    int filterArg, RelCollation collation, int groupCount,
    RelNode input, RelDataType type, String name) {
  if (type == null) {
    final RelDataTypeFactory typeFactory =
        input.getCluster().getTypeFactory();
    final List<RelDataType> types =
        SqlTypeUtil.projectTypes(input.getRowType(), argList);
    final Aggregate.AggCallBinding callBinding =
        new Aggregate.AggCallBinding(typeFactory, aggFunction, types,
            groupCount, filterArg >= 0);
    type = aggFunction.inferReturnType(callBinding);
  }
  return create(aggFunction, distinct, approximate, argList, filterArg,
      collation, type, name);
}
 
Example #4
Source File: View.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * If view fields are present then attempts to gather them
 * into struct type, otherwise returns extension of  {@link DynamicRecordType}.
 *
 * @param factory factory for rel data types creation
 * @return struct type that describes names and types of all
 *         view fields or extension of {@link DynamicRecordType}
 *         when view fields are empty
 */
public RelDataType getRowType(RelDataTypeFactory factory) {

  // if there are no fields defined, this is a dynamic view.
  if (isDynamic()) {
    return new RelDataTypeDrillImpl(new RelDataTypeHolder(), factory);
  }

  List<RelDataType> types = new ArrayList<>(fields.size());
  List<String> names = new ArrayList<>(fields.size());
  for (Field field : fields) {
    names.add(field.getName());
    types.add(getType(field, factory));
  }
  return factory.createStructType(types, names);
}
 
Example #5
Source File: SqlDotOperator.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
  final RelDataType recordType = opBinding.getOperandType(0);
  switch (recordType.getSqlTypeName()) {
  case ROW:
    final String fieldName =
        opBinding.getOperandLiteralValue(1, String.class);
    final RelDataType type = opBinding.getOperandType(0)
        .getField(fieldName, false, false)
        .getType();
    if (recordType.isNullable()) {
      return typeFactory.createTypeWithNullability(type, true);
    } else {
      return type;
    }
  default:
    throw new AssertionError();
  }
}
 
Example #6
Source File: ValuesRel.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Adjust the row type to remove ANY types - derive type from the literals
 *
 * @param typeFactory       RelDataTypeFactory used to create the RelDataType
 * @param rowType           Row type
 * @param tuples            RexLiterals for the Values Rel
 *
 * @return the derived RelDataType from literal.
 */
private static RelDataType adjustRowType(final RelDataTypeFactory typeFactory, final RelDataType rowType,
                                         final ImmutableList<ImmutableList<RexLiteral>> tuples) {
  final int inFieldCount = rowType.getFieldCount();
  List<RelDataType> fieldTypes = Lists.newArrayListWithExpectedSize(inFieldCount);
  List<String> fieldNames = Lists.newArrayListWithExpectedSize(inFieldCount);

  boolean changed = false;
  int i = 0;
  for (final RelDataTypeField field : rowType.getFieldList()) {
    final SqlTypeName sqlTypeName = field.getValue().getSqlTypeName();
    if (sqlTypeName == SqlTypeName.ANY) {
      fieldTypes.add(getFieldTypeFromInput(typeFactory, i, tuples));
      changed = true;
    } else {
      fieldTypes.add(field.getType());
    }
    fieldNames.add(field.getName());
  }

  if (!changed) {
    return rowType;
  }

  return typeFactory.createStructType(fieldTypes, fieldNames);
}
 
Example #7
Source File: SamzaSqlJavaTypeFactoryImpl.java    From samza with Apache License 2.0 6 votes vote down vote up
private static RelDataType convertToSql(final RelDataTypeFactory typeFactory,
    RelDataType type) {
  if (type instanceof RelRecordType) {
    return typeFactory.createStructType(
        Lists.transform(type.getFieldList(), a0 -> convertToSql(typeFactory, a0.getType())),
        type.getFieldNames());
  }
  if (type instanceof JavaType) {
    SqlTypeName typeName = JavaToSqlTypeConversionRules.instance().lookup(((JavaType) type).getJavaClass());
    // For unknown sql type names, return ANY sql type to make Calcite validation not fail.
    if (typeName == null) {
      typeName = SqlTypeName.ANY;
    }
    return typeFactory.createTypeWithNullability(
        typeFactory.createSqlType(typeName),
        type.isNullable());
  } else {
    return JavaTypeFactoryImpl.toSql(typeFactory, type);
  }
}
 
Example #8
Source File: StreamEndpoint.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
@Override
public RelDataType getRowType(RelDataTypeFactory typeFactory)
{
  RelDataTypeFactory.FieldInfoBuilder builder = typeFactory.builder();
  if (fieldMapping != null) {
    for (Map.Entry<String, Class> entry : fieldMapping.entrySet()) {
      builder.add(entry.getKey(), convertField(typeFactory, entry.getValue()));
    }
  } else if (pojoClass != null) {
    for (Field field : pojoClass.getDeclaredFields()) {
      builder.add(field.getName(), convertField(typeFactory, field.getType()));
    }
  } else {
    throw new RuntimeException("Either fieldMapping or pojoClass needs to be set.");
  }

  return builder.build();
}
 
Example #9
Source File: ReflectiveFunctionBase.java    From Bats with Apache License 2.0 6 votes vote down vote up
public ParameterListBuilder add(final Class<?> type, final String name,
    final boolean optional) {
  final int ordinal = builder.size();
  builder.add(
      new FunctionParameter() {
        public int getOrdinal() {
          return ordinal;
        }

        public String getName() {
          return name;
        }

        public RelDataType getType(RelDataTypeFactory typeFactory) {
          return typeFactory.createJavaType(type);
        }

        public boolean isOptional() {
          return optional;
        }
      });
  return this;
}
 
Example #10
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a validator.
 *
 * @param opTab         Operator table
 * @param catalogReader Catalog reader
 * @param typeFactory   Type factory
 * @param conformance   Compatibility mode
 */
protected SqlValidatorImpl(
	SqlOperatorTable opTab,
	SqlValidatorCatalogReader catalogReader,
	RelDataTypeFactory typeFactory,
	SqlConformance conformance) {
	this.opTab = Objects.requireNonNull(opTab);
	this.catalogReader = Objects.requireNonNull(catalogReader);
	this.typeFactory = Objects.requireNonNull(typeFactory);
	this.conformance = Objects.requireNonNull(conformance);

	unknownType = typeFactory.createUnknownType();
	booleanType = typeFactory.createSqlType(SqlTypeName.BOOLEAN);

	rewriteCalls = true;
	expandColumnReferences = true;
	aggFinder = new AggFinder(opTab, false, true, false, null);
	aggOrOverFinder = new AggFinder(opTab, true, true, false, null);
	overFinder = new AggFinder(opTab, true, false, false, aggOrOverFinder);
	groupFinder = new AggFinder(opTab, false, false, true, null);
	aggOrOverOrGroupFinder = new AggFinder(opTab, true, true, true, null);
}
 
Example #11
Source File: KylinRelDataTypeSystemTest.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testLegalDecimalType() {
    RelDataTypeSystem typeSystem = new KylinRelDataTypeSystem();
    RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(typeSystem);
    
    DataType dataType = DataType.getType("decimal(30, 10)");
    RelDataType relDataType = OLAPTable.createSqlType(typeFactory, dataType, true);
    
    Assert.assertTrue(relDataType instanceof BasicSqlType);
    Assert.assertEquals(relDataType.getSqlTypeName(), SqlTypeName.DECIMAL);
    Assert.assertEquals(relDataType.getPrecision(), 30);
    Assert.assertTrue(relDataType.getPrecision() <= typeSystem.getMaxNumericPrecision());
    Assert.assertEquals(relDataType.getScale(), 10);
    Assert.assertTrue(relDataType.getScale() <= typeSystem.getMaxNumericScale());
}
 
Example #12
Source File: CalcitePlanner.java    From herddb with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType getRowType(RelDataTypeFactory typeFactory) {
    RelDataTypeFactory.Builder builder = new RelDataTypeFactory.Builder(typeFactory);
    Table table = tableManager.getTable();
    for (Column c : table.getColumns()) {
        boolean nullable = isColumnNullable(c, table);
        builder.add(c.name, convertType(c.type, typeFactory, nullable));
    }
    return builder.build();
}
 
Example #13
Source File: DremioCatalogReader.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public DremioCatalogReader(
    SimpleCatalog<?> catalog,
    RelDataTypeFactory typeFactory) {
  this.catalog = catalog;
  this.typeFactory = (JavaTypeFactory) typeFactory;

  ImmutableList.Builder<List<String>> schemaPaths = ImmutableList.builder();
  if (catalog.getDefaultSchema() != null) {
    schemaPaths.add(ImmutableList.copyOf(catalog.getDefaultSchema().getPathComponents()));
  }
  schemaPaths.add(ImmutableList.of());
  this.schemaPaths = schemaPaths.build();
}
 
Example #14
Source File: GeodeEnumerator.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a GeodeEnumerator.
 *
 * @param results      Geode result set ({@link SelectResults})
 * @param protoRowType The type of resulting rows
 */
GeodeEnumerator(SelectResults results, RelProtoDataType protoRowType) {
  if (results == null) {
    LOGGER.warn("Null OQL results!");
  }
  this.iterator = (results == null) ? Collections.emptyIterator() : results.iterator();
  this.current = null;

  final RelDataTypeFactory typeFactory =
      new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
  this.fieldTypes = protoRowType.apply(typeFactory).getFieldList();
}
 
Example #15
Source File: ElasticsearchTable.java    From dk-fitting with Apache License 2.0 5 votes vote down vote up
public RelDataType getRowType(RelDataTypeFactory typeFactory) {
    if (rowType == null)
        try {
            rowType = getRowTypeFromEs(typeFactory)[0];
            rowType2 = getRowTypeFromEs(typeFactory)[1];
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    return rowType2;
}
 
Example #16
Source File: CalciteCatalogReader.java    From Bats with Apache License 2.0 5 votes vote down vote up
public CalciteCatalogReader(CalciteSchema rootSchema,
    List<String> defaultSchema, RelDataTypeFactory typeFactory, CalciteConnectionConfig config) {
  this(rootSchema, SqlNameMatchers.withCaseSensitive(config != null && config.caseSensitive()),
      ImmutableList.of(Objects.requireNonNull(defaultSchema),
          ImmutableList.of()),
      typeFactory, config);
}
 
Example #17
Source File: SqlTypeUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static RelDataType createArrayType(
    RelDataTypeFactory typeFactory,
    RelDataType type,
    boolean nullable) {
  RelDataType ret = typeFactory.createArrayType(type, -1);
  return typeFactory.createTypeWithNullability(ret, nullable);
}
 
Example #18
Source File: SqlCaseOperator.java    From Bats with Apache License 2.0 5 votes vote down vote up
private RelDataType inferTypeFromOperands(
    RelDataTypeFactory typeFactory,
    List<RelDataType> argTypes) {
  assert (argTypes.size() % 2) == 1 : "odd number of arguments expected: "
      + argTypes.size();
  assert argTypes.size() > 1 : argTypes.size();
  List<RelDataType> thenTypes = new ArrayList<>();
  for (int j = 1; j < (argTypes.size() - 1); j += 2) {
    thenTypes.add(argTypes.get(j));
  }

  thenTypes.add(Iterables.getLast(argTypes));
  return typeFactory.leastRestrictive(thenTypes);
}
 
Example #19
Source File: HiveSqlUDAFReturnTypeInference.java    From marble with Apache License 2.0 5 votes vote down vote up
@Override public RelDataType inferReturnType(
    final SqlOperatorBinding opBinding) {
  try {
    RelDataTypeFactory factory = opBinding.getTypeFactory();
    SqlOperator sqlOperator = opBinding.getOperator();
    String opName = sqlOperator.getName();
    Class hiveUDAFClass = HiveSqlOperatorTable.instance()
        .getHiveUDAFClass(opName);
    List<RelDataTypeHolder> argsType = new ArrayList<>();

    for (int i = 0; i < opBinding.getOperandCount(); i++) {
      RelDataTypeHolder relDataTypeHolder;
      if (TypeInferenceUtil.isOperandConstantForHiveUDAF(hiveUDAFClass, i)) {
        //we use a pre-defined fake value here to getGenericUDAFReturnType
        Object constantValue = TypeInferenceUtil
            .HIVE_UDAF_CONSTANT_OBJECT_INSPECT_CONTEXT_MAP
            .get(hiveUDAFClass).get(i);
        relDataTypeHolder = new RelDataTypeHolder(opBinding.getOperandType(i),
            true, constantValue);
      } else {
        relDataTypeHolder = new RelDataTypeHolder(
            opBinding.getOperandType(i));
      }
      argsType.add(relDataTypeHolder);
    }
    RelDataType resultType = getGenericUDAFReturnType(
        hiveUDAFClass,
        argsType.toArray(new RelDataTypeHolder[0]), factory);
    return resultType;
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}
 
Example #20
Source File: OLAPAggregateRel.java    From kylin-on-parquet-v2 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 #21
Source File: PigTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public RelDataType getRowType(RelDataTypeFactory typeFactory) {
  final RelDataTypeFactory.Builder builder = typeFactory.builder();
  for (String fieldName : fieldNames) {
    // only supports CHARARRAY types for now
    final RelDataType relDataType = typeFactory
        .createSqlType(PigDataType.valueOf(DataType.CHARARRAY).getSqlType());
    final RelDataType nullableRelDataType = typeFactory
        .createTypeWithNullability(relDataType, true);
    builder.add(fieldName, nullableRelDataType);
  }
  return builder.build();
}
 
Example #22
Source File: InfoSchemaTable.java    From Bats with Apache License 2.0 5 votes vote down vote up
public RelDataType getRowType(RelDataTypeFactory typeFactory) {

    // Convert the array of Drill types to an array of Optiq types
    List<RelDataType> relTypes = Lists.newArrayList();
    List<String> fieldNames = Lists.newArrayList();
    for (Field field : fields) {
      relTypes.add(getRelDataType(typeFactory, field.type));
      fieldNames.add(field.name);
    }

    return typeFactory.createStructType(relTypes, fieldNames);
  }
 
Example #23
Source File: TypeInferenceUtils.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();
  final SqlTypeName sqlTypeName = SqlTypeName.INTEGER;

  // We need to check only the first argument because
  // the second one is used to represent encoding type
  final boolean isNullable = opBinding.getOperandType(0).isNullable();
  return createCalciteTypeWithNullability(factory, sqlTypeName, isNullable, null);
}
 
Example #24
Source File: SqlValidatorUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Factory method for {@link SqlValidator}.
 */
public static SqlValidatorWithHints newValidator(
    SqlOperatorTable opTab,
    SqlValidatorCatalogReader catalogReader,
    RelDataTypeFactory typeFactory,
    SqlConformance conformance) {
  return new SqlValidatorImpl(opTab, catalogReader, typeFactory,
      conformance);
}
 
Example #25
Source File: LealoneTable.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType getRowType(RelDataTypeFactory typeFactory) {
    List<String> names = Lists.newArrayList();
    List<RelDataType> types = Lists.newArrayList();
    for (Column column : table.getColumns()) {
        names.add(column.getName());
        RelDataType type = getSqlTypeFromLealoneType(typeFactory, column.getType());
        type = typeFactory.createTypeWithNullability(type, column.isNullable());
        types.add(type);
    }

    return typeFactory.createStructType(types, names);
}
 
Example #26
Source File: RelOptCluster.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a cluster.
 */
@Deprecated // to be removed before 2.0
RelOptCluster(
		RelOptQuery query,
		RelOptPlanner planner,
		RelDataTypeFactory typeFactory,
		RexBuilder rexBuilder) {
	this(planner, typeFactory, rexBuilder, query.nextCorrel,
			query.mapCorrelToRel);
}
 
Example #27
Source File: TypeInferenceUtils.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();
  final boolean isNullable = opBinding.getOperandType(0).isNullable();
  return createCalciteTypeWithNullability(
      factory,
      SqlTypeName.DOUBLE,
      isNullable);
}
 
Example #28
Source File: Interpreter.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelDataType combinedRowType(List<RelNode> inputs) {
  final RelDataTypeFactory.Builder builder =
      interpreter.dataContext.getTypeFactory().builder();
  for (RelNode input : inputs) {
    builder.addAll(input.getRowType().getFieldList());
  }
  return builder.build();
}
 
Example #29
Source File: Schemas.java    From Bats with Apache License 2.0 5 votes vote down vote up
private static boolean matches(RelDataTypeFactory typeFactory,
    Function member, List<RelDataType> argumentTypes) {
  List<FunctionParameter> parameters = member.getParameters();
  if (parameters.size() != argumentTypes.size()) {
    return false;
  }
  for (int i = 0; i < argumentTypes.size(); i++) {
    RelDataType argumentType = argumentTypes.get(i);
    FunctionParameter parameter = parameters.get(i);
    if (!canConvert(argumentType, parameter.getType(typeFactory))) {
      return false;
    }
  }
  return true;
}
 
Example #30
Source File: TypeInferenceUtils.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();
  final SqlTypeName sqlTypeName = SqlTypeName.VARCHAR;

  for(int i = 0; i < opBinding.getOperandCount(); ++i) {
    if(opBinding.getOperandType(i).isNullable()) {
      return createCalciteTypeWithNullability(factory, sqlTypeName, true, null);
    }
  }

  return createCalciteTypeWithNullability(factory, sqlTypeName, false, null);
}