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

The following examples show how to use org.apache.calcite.rel.type.RelDataType. 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 7 votes vote down vote up
/**
 * Adds collation and charset to a character type, returns other types
 * unchanged.
 *
 * @param type        Type
 * @param typeFactory Type factory
 * @return Type with added charset and collation, or unchanged type if it is
 * not a char type.
 */
public static RelDataType addCharsetAndCollation(
    RelDataType type,
    RelDataTypeFactory typeFactory) {
  if (!inCharFamily(type)) {
    return type;
  }
  Charset charset = type.getCharset();
  if (charset == null) {
    charset = typeFactory.getDefaultCharset();
  }
  SqlCollation collation = type.getCollation();
  if (collation == null) {
    collation = SqlCollation.IMPLICIT;
  }

  // todo: should get the implicit collation from repository
  //   instead of null
  type =
      typeFactory.createTypeWithCharsetAndCollation(
          type,
          charset,
          collation);
  SqlValidatorUtil.checkCharsetAndCollateConsistentIfCharType(type);
  return type;
}
 
Example #2
Source File: SqlMultisetQueryConstructor.java    From Bats with Apache License 2.0 6 votes vote down vote up
public boolean checkOperandTypes(
    SqlCallBinding callBinding,
    boolean throwOnFailure) {
  final List<RelDataType> argTypes =
      SqlTypeUtil.deriveAndCollectTypes(
          callBinding.getValidator(),
          callBinding.getScope(),
          callBinding.operands());
  final RelDataType componentType =
      getComponentType(
          callBinding.getTypeFactory(),
          argTypes);
  if (null == componentType) {
    if (throwOnFailure) {
      throw callBinding.newValidationError(RESOURCE.needSameTypeParameter());
    }
    return false;
  }
  return true;
}
 
Example #3
Source File: RexProgram.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a program which calculates projections and filters rows based
 * upon a condition. Does not attempt to eliminate common sub-expressions.
 *
 * @param projectExprs  Project expressions
 * @param conditionExpr Condition on which to filter rows, or null if rows
 *                      are not to be filtered
 * @param fieldNames    Names of projected fields
 * @param rexBuilder    Builder of rex expressions
 * @return A program
 */
public static RexProgram create(
    RelDataType inputRowType,
    List<? extends RexNode> projectExprs,
    RexNode conditionExpr,
    List<String> fieldNames,
    RexBuilder rexBuilder) {
  if (fieldNames == null) {
    fieldNames = Collections.nCopies(projectExprs.size(), null);
  } else {
    assert fieldNames.size() == projectExprs.size()
        : "fieldNames=" + fieldNames
        + ", exprs=" + projectExprs;
  }
  final RexProgramBuilder programBuilder =
      new RexProgramBuilder(inputRowType, rexBuilder);
  for (int i = 0; i < projectExprs.size(); i++) {
    programBuilder.addProject(projectExprs.get(i), fieldNames.get(i));
  }
  if (conditionExpr != null) {
    programBuilder.addCondition(conditionExpr);
  }
  return programBuilder.getProgram();
}
 
Example #4
Source File: SqlOperator.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Validates the operands of a call, inferring the return type in the
 * process.
 *
 * @param validator active validator
 * @param scope     validation scope
 * @param call      call to be validated
 * @return inferred type
 */
public final RelDataType validateOperands(
    SqlValidator validator,
    SqlValidatorScope scope,
    SqlCall call) {
  // Let subclasses know what's up.
  preValidateCall(validator, scope, call);

  // Check the number of operands
  checkOperandCount(validator, operandTypeChecker, call);

  SqlCallBinding opBinding = new SqlCallBinding(validator, scope, call);

  checkOperandTypes(
      opBinding,
      true);

  // Now infer the result type.
  RelDataType ret = inferReturnType(opBinding);
  ((SqlValidatorImpl) validator).setValidatedNodeType(call, ret);
  return ret;
}
 
Example #5
Source File: SqlCastFunction.java    From Bats with Apache License 2.0 6 votes vote down vote up
public RelDataType inferReturnType(
    SqlOperatorBinding opBinding) {
  assert opBinding.getOperandCount() == 2;
  RelDataType ret = opBinding.getOperandType(1);
  RelDataType firstType = opBinding.getOperandType(0);
  ret =
      opBinding.getTypeFactory().createTypeWithNullability(
          ret,
          firstType.isNullable());
  if (opBinding instanceof SqlCallBinding) {
    SqlCallBinding callBinding = (SqlCallBinding) opBinding;
    SqlNode operand0 = callBinding.operand(0);

    // dynamic parameters and null constants need their types assigned
    // to them using the type they are casted to.
    if (((operand0 instanceof SqlLiteral)
        && (((SqlLiteral) operand0).getValue() == null))
        || (operand0 instanceof SqlDynamicParam)) {
      final SqlValidatorImpl validator =
          (SqlValidatorImpl) callBinding.getValidator();
      validator.setValidatedNodeType(operand0, ret);
    }
  }
  return ret;
}
 
Example #6
Source File: SqlDotOperator.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public boolean checkOperandTypes(SqlCallBinding callBinding,
    boolean throwOnFailure) {
  final SqlNode left = callBinding.operand(0);
  final SqlNode right = callBinding.operand(1);
  final RelDataType type =
      callBinding.getValidator().deriveType(callBinding.getScope(), left);
  if (type.getSqlTypeName() != SqlTypeName.ROW) {
    return false;
  } else if (type.getSqlIdentifier().isStar()) {
    return false;
  }
  final RelDataType operandType = callBinding.getOperandType(0);
  final SqlSingleOperandTypeChecker checker = getChecker(operandType);
  // Actually operand0 always comes from parsing the SqlIdentifier, so there
  // is no need to make implicit type coercion.
  return checker.checkSingleOperandType(callBinding, right, 0,
      throwOnFailure);
}
 
Example #7
Source File: EnumerableFilterToCalcRule.java    From calcite with Apache License 2.0 6 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final EnumerableFilter filter = call.rel(0);
  final RelNode input = filter.getInput();

  // Create a program containing a filter.
  final RexBuilder rexBuilder = filter.getCluster().getRexBuilder();
  final RelDataType inputRowType = input.getRowType();
  final RexProgramBuilder programBuilder =
      new RexProgramBuilder(inputRowType, rexBuilder);
  programBuilder.addIdentity();
  programBuilder.addCondition(filter.getCondition());
  final RexProgram program = programBuilder.getProgram();

  final EnumerableCalc calc = EnumerableCalc.create(input, program);
  call.transformTo(calc);
}
 
Example #8
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 #9
Source File: ViewTable.java    From calcite with Apache License 2.0 6 votes vote down vote up
private RelRoot expandView(RelOptTable.ToRelContext context,
    RelDataType rowType, String queryString) {
  try {
    final RelRoot root =
        context.expandView(rowType, queryString, schemaPath, viewPath);
    final RelNode rel = RelOptUtil.createCastRel(root.rel, rowType, true);
    // Expand any views
    final RelNode rel2 = rel.accept(
        new RelShuttleImpl() {
          @Override public RelNode visit(TableScan scan) {
            final RelOptTable table = scan.getTable();
            final TranslatableTable translatableTable =
                table.unwrap(TranslatableTable.class);
            if (translatableTable != null) {
              return translatableTable.toRel(context, table);
            }
            return super.visit(scan);
          }
        });
    return root.withRel(rel2);
  } catch (Exception e) {
    throw new RuntimeException("Error while parsing view definition: "
        + queryString, e);
  }
}
 
Example #10
Source File: FlinkReturnTypes.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
	final RelDataType numType = opBinding.getOperandType(0);
	if (numType.getSqlTypeName() != SqlTypeName.DECIMAL) {
		return numType;
	}
	final BigDecimal lenVal;
	if (opBinding.getOperandCount() == 1) {
		lenVal = BigDecimal.ZERO;
	} else if (opBinding.getOperandCount() == 2) {
		lenVal = getArg1Literal(opBinding); // may return null
	} else {
		throw new AssertionError();
	}
	if (lenVal == null) {
		return numType; //
	}
	// ROUND( decimal(p,s), r )
	final int p = numType.getPrecision();
	final int s = numType.getScale();
	final int r = lenVal.intValueExact();
	DecimalType dt = FlinkTypeSystem.inferRoundType(p, s, r);
	return opBinding.getTypeFactory().createSqlType(
		SqlTypeName.DECIMAL, dt.getPrecision(), dt.getScale());
}
 
Example #11
Source File: VisitorDataContext.java    From Bats with Apache License 2.0 6 votes vote down vote up
public static DataContext of(RelDataType rowType,
    List<Pair<RexInputRef, RexNode>> usageList) {
  final int size = rowType.getFieldList().size();
  final Object[] values = new Object[size];
  for (Pair<RexInputRef, RexNode> elem : usageList) {
    Pair<Integer, ?> value = getValue(elem.getKey(), elem.getValue());
    if (value == null) {
      LOGGER.warn("{} is not handled for {} for checking implication",
          elem.getKey(), elem.getValue());
      return null;
    }
    int index = value.getKey();
    values[index] = value.getValue();
  }
  return new VisitorDataContext(values);
}
 
Example #12
Source File: PhysTypeTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-3364">[CALCITE-3364]
 * Can't group table function result due to a type cast error if table function
 * returns a row with a single value</a>. */
@Test void testOneColumnJavaRowFormatConversion() {
  RelDataType rowType = TYPE_FACTORY.createStructType(
      ImmutableList.of(TYPE_FACTORY.createSqlType(SqlTypeName.INTEGER)),
      ImmutableList.of("intField"));
  final PhysType rowPhysType = PhysTypeImpl.of(TYPE_FACTORY, rowType,
      JavaRowFormat.ARRAY, false);
  final Expression e = rowPhysType.convertTo(
      Expressions.parameter(Enumerable.class, "input"),
      JavaRowFormat.SCALAR);
  final String expected = "input.select(new org.apache.calcite.linq4j.function.Function1() {\n"
      + "  public int apply(Object[] o) {\n"
      + "    return org.apache.calcite.runtime.SqlFunctions.toInt(o[0]);\n"
      + "  }\n"
      + "  public Object apply(Object o) {\n"
      + "    return apply(\n"
      + "      (Object[]) o);\n"
      + "  }\n"
      + "}\n"
      + ")";
  assertEquals(Expressions.toString(e), expected);
}
 
Example #13
Source File: SqlUserDefinedAggFunction.java    From calcite with Apache License 2.0 5 votes vote down vote up
private RelDataType toSql(RelDataType type) {
  if (type instanceof RelDataTypeFactoryImpl.JavaType
      && ((RelDataTypeFactoryImpl.JavaType) type).getJavaClass()
      == Object.class) {
    return typeFactory.createTypeWithNullability(
        typeFactory.createSqlType(SqlTypeName.ANY), true);
  }
  return JavaTypeFactoryImpl.toSql(typeFactory, type);
}
 
Example #14
Source File: SqlValidatorScope.java    From Bats with Apache License 2.0 5 votes vote down vote up
Step(Path parent, RelDataType rowType, int i, String name,
    StructKind kind) {
  this.parent = Objects.requireNonNull(parent);
  this.rowType = rowType; // may be null
  this.i = i;
  this.name = name;
  this.kind = Objects.requireNonNull(kind);
}
 
Example #15
Source File: LogicalValues.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Creates a LogicalValues that outputs one row and one column. */
public static LogicalValues createOneRow(RelOptCluster cluster) {
  final RelDataType rowType =
      cluster.getTypeFactory().builder()
          .add("ZERO", SqlTypeName.INTEGER).nullable(false)
          .build();
  final ImmutableList<ImmutableList<RexLiteral>> tuples =
      ImmutableList.of(
          ImmutableList.of(
              cluster.getRexBuilder().makeExactLiteral(BigDecimal.ZERO,
                  rowType.getFieldList().get(0).getType())));
  return create(cluster, rowType, tuples);
}
 
Example #16
Source File: CsvSchema.java    From kareldb with Apache License 2.0 5 votes vote down vote up
public static RelDataType getRowType(Source source) {
    try (CSVReader reader = openCsv(source)) {
        String[] strings = reader.readNext(); // get header row
        LinkedHashMap<String, ColumnDef> types = toColumnDefs(strings);
        return Schema.toRowType(types, Collections.emptyList()).getRowType();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #17
Source File: NumericOrDefaultReturnTypeInference.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
	int nOperands = opBinding.getOperandCount();
	List<RelDataType> types = new ArrayList<>();
	for (int i = startTypeIdx; i < nOperands; i++) {
		RelDataType type = opBinding.getOperandType(i);
		if (SqlTypeUtil.isNumeric(type)) {
			types.add(type);
		} else {
			return opBinding.getOperandType(defaultTypeIdx);
		}
	}
	return opBinding.getTypeFactory().leastRestrictive(types);
}
 
Example #18
Source File: TypeInferenceOperandChecker.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Adopted from {@link org.apache.calcite.sql.validate.implicit.AbstractTypeCoercion}.
 */
private void updateInferredType(SqlValidator validator, SqlNode node, RelDataType type) {
	validator.setValidatedNodeType(node, type);
	final SqlValidatorNamespace namespace = validator.getNamespace(node);
	if (namespace != null) {
		namespace.setType(type);
	}
}
 
Example #19
Source File: SqlToRelTestBase.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public RelRoot expandView(RelDataType rowType, String queryString,
    List<String> schemaPath, List<String> viewPath) {
  try {
    SqlNode parsedNode = SqlParser.create(queryString).parseStmt();
    SqlNode validatedNode = validator.validate(parsedNode);
    SqlToRelConverter converter = new SqlToRelConverter(
        this, validator, catalogReader, cluster,
        StandardConvertletTable.INSTANCE, config);
    return converter.convertQuery(validatedNode, false, true);
  } catch (SqlParseException e) {
    throw new RuntimeException("Error happened while expanding view.", e);
  }
}
 
Example #20
Source File: PigTypes.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a Pig tuple schema to a SQL row type.
 *
 * @param pigSchema Pig tuple schema
 * @param nullable true if the type is nullable
 * @return a SQL row type
 */
static RelDataType convertSchema(LogicalSchema pigSchema, boolean nullable) {
  if (pigSchema != null && pigSchema.size() > 0) {
    List<String> fieldNameList = new ArrayList<>();
    List<RelDataType> typeList = new ArrayList<>();
    for (int i = 0; i < pigSchema.size(); i++) {
      final LogicalSchema.LogicalFieldSchema subPigField = pigSchema.getField(i);
      fieldNameList.add(subPigField.alias != null ? subPigField.alias : "$" + i);
      typeList.add(convertSchemaField(subPigField, nullable));
    }
    return TYPE_FACTORY.createStructType(typeList, fieldNameList, nullable);
  }
  return new DynamicTupleRecordType(TYPE_FACTORY);
}
 
Example #21
Source File: WorkspaceSchemaFactory.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public List<FunctionParameter> getParameters() {
  List<FunctionParameter> result = new ArrayList<>();
  for (int i = 0; i < sig.params.size(); i++) {
    final TableParamDef p = sig.params.get(i);
    final int ordinal = i;
    result.add(new FunctionParameter() {
      @Override
      public int getOrdinal() {
        return ordinal;
      }

      @Override
      public String getName() {
        return p.name;
      }

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

      @Override
      public boolean isOptional() {
        return p.optional;
      }
    });
  }
  return result;
}
 
Example #22
Source File: SetOp.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override protected RelDataType deriveRowType() {
  final List<RelDataType> inputRowTypes =
      Lists.transform(inputs, RelNode::getRowType);
  final RelDataType rowType =
      getCluster().getTypeFactory().leastRestrictive(inputRowTypes);
  if (rowType == null) {
    throw new IllegalArgumentException("Cannot compute compatible row type "
        + "for arguments to set op: "
        + Util.sepList(inputRowTypes, ", "));
  }
  return rowType;
}
 
Example #23
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public RelDataType visit(SqlDataTypeSpec dataType) {
	// Q. How can a data type have a type?
	// A. When it appears in an expression. (Say as the 2nd arg to the
	//    CAST operator.)
	validateDataType(dataType);
	return dataType.deriveType(SqlValidatorImpl.this);
}
 
Example #24
Source File: SchemaNamespace.java    From Bats with Apache License 2.0 5 votes vote down vote up
protected RelDataType validateImpl(RelDataType targetRowType) {
  final RelDataTypeFactory.Builder builder =
      validator.getTypeFactory().builder();
  for (SqlMoniker moniker
      : validator.catalogReader.getAllSchemaObjectNames(names)) {
    final List<String> names1 = moniker.getFullyQualifiedNames();
    final SqlValidatorTable table = validator.catalogReader.getTable(names1);
    builder.add(Util.last(names1), table.getRowType());
  }
  return builder.build();
}
 
Example #25
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 #26
Source File: RexBuilder.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Makes a cast of a value to NOT NULL;
 * no-op if the type already has NOT NULL.
 */
public RexNode makeNotNull(RexNode exp) {
  final RelDataType type = exp.getType();
  if (!type.isNullable()) {
    return exp;
  }
  final RelDataType notNullType =
      typeFactory.createTypeWithNullability(type, false);
  return makeAbstractCast(notNullType, exp);
}
 
Example #27
Source File: SqlTypeFactoryTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testLeastRestrictiveWithNull() {
  SqlTypeFixture f = new SqlTypeFixture();
  RelDataType leastRestrictive =
      f.typeFactory.leastRestrictive(Lists.newArrayList(f.sqlNull, f.sqlNull));
  assertThat(leastRestrictive.getSqlTypeName(), is(SqlTypeName.NULL));
  assertThat(leastRestrictive.isNullable(), is(true));
}
 
Example #28
Source File: RelStructuredTypeFlattener.java    From Bats with Apache License 2.0 5 votes vote down vote up
public void rewriteRel(LogicalCalc rel) {
    // Translate the child.
    final RelNode newInput = getNewForOldRel(rel.getInput());

    final RelOptCluster cluster = rel.getCluster();
    RexProgramBuilder programBuilder = new RexProgramBuilder(newInput.getRowType(), cluster.getRexBuilder());

    // Convert the common expressions.
    final RexProgram program = rel.getProgram();
    final RewriteRexShuttle shuttle = new RewriteRexShuttle();
    for (RexNode expr : program.getExprList()) {
        programBuilder.registerInput(expr.accept(shuttle));
    }

    // Convert the projections.
    final List<Pair<RexNode, String>> flattenedExpList = new ArrayList<>();
    List<String> fieldNames = rel.getRowType().getFieldNames();
    flattenProjections(new RewriteRexShuttle(), program.getProjectList(), fieldNames, "", flattenedExpList);

    // Register each of the new projections.
    for (Pair<RexNode, String> flattenedExp : flattenedExpList) {
        programBuilder.addProject(flattenedExp.left, flattenedExp.right);
    }

    // Translate the condition.
    final RexLocalRef conditionRef = program.getCondition();
    if (conditionRef != null) {
        final Ord<RelDataType> newField = getNewFieldForOldInput(conditionRef.getIndex());
        programBuilder.addCondition(RexBuilder.getRexFactory().makeInputRef(newField.i, newField.e));
    }

    RexProgram newProgram = programBuilder.getProgram();

    // Create a new calc relational expression.
    LogicalCalc newRel = LogicalCalc.create(newInput, newProgram);
    setNewForOldRel(rel, newRel);
}
 
Example #29
Source File: RelToSqlConverter.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void addSelect(List<SqlNode> selectList, SqlNode node,
                      RelDataType rowType) {
  String name = rowType.getFieldNames().get(selectList.size());
  String alias = SqlValidatorUtil.getAlias(node, -1);
  final String lowerName = name.toLowerCase(Locale.ROOT);
  if (lowerName.startsWith("expr$")) {
    // Put it in ordinalMap
    ordinalMap.put(lowerName, node);
  } else if (alias == null || !alias.equals(name)) {
    node = as(node, name);
  }
  selectList.add(node);
}
 
Example #30
Source File: SparkExec.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public static Enumerable<Object[]> asyncResult(DataContext dataContext) {
    if (BackdoorToggles.getPrepareOnly()) {
        return Linq4j.emptyEnumerable();
    }
    OLAPRel olapRel = (OLAPRel) QueryContextFacade.current().getOlapRel();
    RelDataType rowType = (RelDataType) QueryContextFacade.current().getResultType();
    return QueryEngineFactory.computeAsync(dataContext, olapRel, rowType);
}