org.apache.calcite.sql.validate.SqlUserDefinedTableFunction Java Examples

The following examples show how to use org.apache.calcite.sql.validate.SqlUserDefinedTableFunction. 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: EnumerableTableFunctionScan.java    From calcite with Apache License 2.0 6 votes vote down vote up
private boolean isQueryable() {
  if (!(getCall() instanceof RexCall)) {
    return false;
  }
  final RexCall call = (RexCall) getCall();
  if (!(call.getOperator() instanceof SqlUserDefinedTableFunction)) {
    return false;
  }
  final SqlUserDefinedTableFunction udtf =
      (SqlUserDefinedTableFunction) call.getOperator();
  if (!(udtf.getFunction() instanceof TableFunctionImpl)) {
    return false;
  }
  final TableFunctionImpl tableFunction =
      (TableFunctionImpl) udtf.getFunction();
  final Method method = tableFunction.method;
  return QueryableTable.class.isAssignableFrom(method.getReturnType());
}
 
Example #2
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 #3
Source File: DremioCatalogReader.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Rest of class is utility functions taken directly from CalciteCatalogReader. This is because that class consider these utilities to be private concerns.
 */
private SqlOperator toOp(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 Predicate<Integer> optional =
      new Predicate<Integer>() {
        @Override
        public boolean apply(Integer input) {
          return function.getParameters().get(input).isOptional();
        }
      };
  final FamilyOperandTypeChecker typeChecker =
      OperandTypes.family(typeFamilies, optional);
  final List<RelDataType> paramTypes = toSql(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, 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 #4
Source File: CalciteCatalogReader.java    From calcite 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 #5
Source File: RexImpTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
private ParameterExpression genValueStatement(
    final RexToLixTranslator translator,
    final RexCall call, final List<Expression> argValueList,
    final Expression condition) {
  List<Expression> optimizedArgValueList = argValueList;
  if (harmonize) {
    optimizedArgValueList =
        harmonize(optimizedArgValueList, translator, call);
  }
  optimizedArgValueList = unboxIfNecessary(optimizedArgValueList);

  final Expression callValue =
      implementSafe(translator, call, optimizedArgValueList);

  // In general, RexCall's type is correct for code generation
  // and thus we should ensure the consistency.
  // However, for some special cases (e.g., TableFunction),
  // the implementation's type is correct, we can't convert it.
  final SqlOperator op = call.getOperator();
  final Type returnType = translator.typeFactory.getJavaClass(call.getType());
  final boolean noConvert = (returnType == null)
          || (returnType == callValue.getType())
          || (op instanceof SqlUserDefinedTableMacro)
          || (op instanceof SqlUserDefinedTableFunction);
  final Expression convertedCallValue =
          noConvert
          ? callValue
          : EnumUtils.convert(callValue, returnType);

  final Expression valueExpression =
      Expressions.condition(condition,
          getIfTrue(convertedCallValue.getType(), argValueList),
          convertedCallValue);
  final ParameterExpression value =
      Expressions.parameter(convertedCallValue.getType(),
          translator.getBlockBuilder().newName(getVariableName() + "_value"));
  translator.getBlockBuilder().add(
      Expressions.declare(Modifier.FINAL, value, valueExpression));
  return value;
}
 
Example #6
Source File: LookupOperatorOverloadsTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
private void checkFunctionType(int size, String name,
    List<SqlOperator> operatorList) {
  assertThat(size, is(operatorList.size()));

  for (SqlOperator op : operatorList) {
    assertThat(op, instanceOf(SqlUserDefinedTableFunction.class));
    assertThat(name, is(op.getName()));
  }
}