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: 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 #3
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 #4
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 #5
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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
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 #13
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 #14
Source File: CalciteCatalogReader.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Converts a function to a {@link org.apache.calcite.sql.SqlOperator}.
 *
 * <p>The {@code typeFactory} argument is technical debt; see [CALCITE-2082]
 * Remove RelDataTypeFactory argument from SqlUserDefinedAggFunction
 * constructor. */
private static SqlOperator toOp(RelDataTypeFactory typeFactory,
    SqlIdentifier name, final Function function) {
  List<RelDataType> argTypes = new ArrayList<>();
  List<SqlTypeFamily> typeFamilies = new ArrayList<>();
  for (FunctionParameter o : function.getParameters()) {
    final RelDataType type = o.getType(typeFactory);
    argTypes.add(type);
    typeFamilies.add(
        Util.first(type.getSqlTypeName().getFamily(), SqlTypeFamily.ANY));
  }
  final FamilyOperandTypeChecker typeChecker =
      OperandTypes.family(typeFamilies, i ->
          function.getParameters().get(i).isOptional());
  final List<RelDataType> paramTypes = toSql(typeFactory, argTypes);
  if (function instanceof ScalarFunction) {
    return new SqlUserDefinedFunction(name, infer((ScalarFunction) function),
        InferTypes.explicit(argTypes), typeChecker, paramTypes, function);
  } else if (function instanceof AggregateFunction) {
    return new SqlUserDefinedAggFunction(name,
        infer((AggregateFunction) function), InferTypes.explicit(argTypes),
        typeChecker, (AggregateFunction) function, false, false,
        Optionality.FORBIDDEN, typeFactory);
  } else if (function instanceof TableMacro) {
    return new SqlUserDefinedTableMacro(name, ReturnTypes.CURSOR,
        InferTypes.explicit(argTypes), typeChecker, paramTypes,
        (TableMacro) function);
  } else if (function instanceof TableFunction) {
    return new SqlUserDefinedTableFunction(name, ReturnTypes.CURSOR,
        InferTypes.explicit(argTypes), typeChecker, paramTypes,
        (TableFunction) function);
  } else {
    throw new AssertionError("unknown function type " + function);
  }
}
 
Example #15
Source File: Checker.java    From samza with Apache License 2.0 5 votes vote down vote up
@Override
public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) {
  if (!udfMetadataOptional.isPresent() || udfMetadataOptional.get().isDisableArgCheck() || !throwOnFailure) {
    return true;
  } else {
    // 1. Generate a mapping from argument index to parsed calcite-type for the sql UDF.
    Map<Integer, RelDataType> argumentIndexToCalciteType = IntStream.range(0, callBinding.getOperandCount())
        .boxed()
        .collect(Collectors.toMap(operandIndex -> operandIndex, callBinding::getOperandType, (a, b) -> b));

    UdfMetadata udfMetadata = udfMetadataOptional.get();
    List<SamzaSqlFieldType> udfArguments = udfMetadata.getArguments();

    // 2. Compare the argument type in samza-sql UDF against the RelType generated by the
    // calcite parser engine.
    for (int udfArgumentIndex = 0; udfArgumentIndex < udfArguments.size(); ++udfArgumentIndex) {
      SamzaSqlFieldType udfArgumentType = udfArguments.get(udfArgumentIndex);
      SqlTypeName udfArgumentAsSqlType = toCalciteSqlType(udfArgumentType);
      RelDataType parsedSqlArgType = argumentIndexToCalciteType.get(udfArgumentIndex);

      // 3(a). Special-case, where static strings used as method-arguments in udf-methods during invocation are parsed as the Char type by calcite.
      if (parsedSqlArgType.getSqlTypeName() == SqlTypeName.CHAR && udfArgumentAsSqlType == SqlTypeName.VARCHAR) {
        return true;
      } else if (!Objects.equals(parsedSqlArgType.getSqlTypeName(), udfArgumentAsSqlType)
              && !ANY_SQL_TYPE_NAMES.contains(parsedSqlArgType.getSqlTypeName()) && hasOneUdfMethod(udfMetadata)) {
        // 3(b). Throw up and fail on mismatch between the SamzaSqlType and CalciteType for any argument.
        String msg = String.format("Type mismatch in udf class: %s at argument index: %d." +
                        "Expected type: %s, actual type: %s.", udfMetadata.getName(),
                udfArgumentIndex, parsedSqlArgType.getSqlTypeName(), udfArgumentAsSqlType);
        LOG.error(msg);
        throw new SamzaSqlValidatorException(msg);
      }
    }
  }
  // 4. The SamzaSqlFieldType and CalciteType has matched for all the arguments in the UDF.
  return true;
}
 
Example #16
Source File: CSVMessageFormat.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
private RelDataType convertField(RelDataTypeFactory typeFactory, DelimitedSchema.FieldType type)
{
  RelDataType relDataType;
  switch (type) {
    case BOOLEAN:
      relDataType = typeFactory.createSqlType(SqlTypeName.BOOLEAN);
      break;
    case DOUBLE:
      relDataType = typeFactory.createSqlType(SqlTypeName.DOUBLE);
      break;
    case INTEGER:
      relDataType = typeFactory.createSqlType(SqlTypeName.INTEGER);
      break;
    case FLOAT:
      relDataType = typeFactory.createSqlType(SqlTypeName.FLOAT);
      break;
    case LONG:
      relDataType = typeFactory.createSqlType(SqlTypeName.BIGINT);
      break;
    case SHORT:
      relDataType = typeFactory.createSqlType(SqlTypeName.SMALLINT);
      break;
    case CHARACTER:
      relDataType = typeFactory.createSqlType(SqlTypeName.CHAR);
      break;
    case STRING:
      relDataType = typeFactory.createSqlType(SqlTypeName.VARCHAR);
      break;
    case DATE:
      relDataType = typeFactory.createSqlType(SqlTypeName.TIMESTAMP);
      break;
    default:
      relDataType = typeFactory.createSqlType(SqlTypeName.ANY);
  }

  return relDataType;
}
 
Example #17
Source File: SqlTypeUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Recreates a given RelDataType with nullability iff any of the param
 * argTypes are nullable.
 */
public static RelDataType makeNullableIfOperandsAre(
    final RelDataTypeFactory typeFactory,
    final List<RelDataType> argTypes,
    RelDataType type) {
  Objects.requireNonNull(type);
  if (containsNullable(argTypes)) {
    type = typeFactory.createTypeWithNullability(type, true);
  }
  return type;
}
 
Example #18
Source File: SqlReturnTypeInferenceChain.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  for (SqlReturnTypeInference rule : rules) {
    RelDataType ret = rule.inferReturnType(opBinding);
    if (ret != null) {
      return ret;
    }
  }
  return null;
}
 
Example #19
Source File: SqlTypeUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Iterates over all operands, derives their types, and collects them into
 * a list.
 */
public static List<RelDataType> deriveAndCollectTypes(
    SqlValidator validator,
    SqlValidatorScope scope,
    List<SqlNode> operands) {
  // NOTE: Do not use an AbstractList. Don't want to be lazy. We want
  // errors.
  List<RelDataType> types = new ArrayList<>();
  for (SqlNode operand : operands) {
    types.add(validator.deriveType(scope, operand));
  }
  return types;
}
 
Example #20
Source File: StandardConvertletTable.java    From Bats with Apache License 2.0 5 votes vote down vote up
private SqlNode getCastedSqlNode(SqlNode argInput, RelDataType varType,
    SqlParserPos pos, RexNode argRex) {
  SqlNode arg;
  if (argRex != null && !argRex.getType().equals(varType)) {
    arg = SqlStdOperatorTable.CAST.createCall(
        pos, argInput, SqlTypeUtil.convertTypeToSpec(varType));
  } else {
    arg = argInput;
  }
  return arg;
}
 
Example #21
Source File: EnumerableTableFunctionScan.java    From calcite with Apache License 2.0 5 votes vote down vote up
public EnumerableTableFunctionScan(RelOptCluster cluster,
    RelTraitSet traits, List<RelNode> inputs, Type elementType,
    RelDataType rowType, RexNode call,
    Set<RelColumnMapping> columnMappings) {
  super(cluster, traits, inputs, call, elementType, rowType,
      columnMappings);
}
 
Example #22
Source File: RelStructuredTypeFlattener.java    From Bats with Apache License 2.0 5 votes vote down vote up
public void rewriteRel(LogicalCorrelate rel) {
    ImmutableBitSet.Builder newPos = ImmutableBitSet.builder();
    for (int pos : rel.getRequiredColumns()) {
        RelDataType corrFieldType = rel.getLeft().getRowType().getFieldList().get(pos).getType();
        if (corrFieldType.isStruct()) {
            throw Util.needToImplement("correlation on structured type");
        }
        newPos.set(getNewForOldInput(pos));
    }
    LogicalCorrelate newRel = LogicalCorrelate.create(getNewForOldRel(rel.getLeft()),
            getNewForOldRel(rel.getRight()), rel.getCorrelationId(), newPos.build(), rel.getJoinType());
    setNewForOldRel(rel, newRel);
}
 
Example #23
Source File: FamilyOperandTypeChecker.java    From calcite with Apache License 2.0 5 votes vote down vote up
public boolean checkSingleOperandType(
    SqlCallBinding callBinding,
    SqlNode node,
    int iFormalOperand,
    boolean throwOnFailure) {
  SqlTypeFamily family = families.get(iFormalOperand);
  if (family == SqlTypeFamily.ANY) {
    // no need to check
    return true;
  }
  if (SqlUtil.isNullLiteral(node, false)) {
    if (callBinding.isTypeCoercionEnabled()) {
      return true;
    } else 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) {
    return true;
  }

  if (!family.getTypeNames().contains(typeName)) {
    if (throwOnFailure) {
      throw callBinding.newValidationSignatureError();
    }
    return false;
  }
  return true;
}
 
Example #24
Source File: RelToSqlConverter.java    From quark with Apache License 2.0 5 votes vote down vote up
private Result(SqlNode node, Collection<Clause> clauses, String neededAlias,
               List<Pair<String, RelDataType>> aliases) {
  this.node = node;
  this.neededAlias = neededAlias;
  this.aliases = aliases;
  this.clauses = Expressions.list(clauses);
}
 
Example #25
Source File: RexProgramBuilderBase.java    From calcite with Apache License 2.0 5 votes vote down vote up
protected RelDataType tBigInt(boolean nullable) {
  RelDataType type = typeFactory.createSqlType(SqlTypeName.BIGINT);
  if (nullable) {
    type = nullable(type);
  }
  return type;
}
 
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: 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 #28
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);
}
 
Example #29
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 #30
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));
}