org.apache.calcite.schema.FunctionParameter Java Examples

The following examples show how to use org.apache.calcite.schema.FunctionParameter. 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: UdfTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
public List<FunctionParameter> getParameters() {
  final List<FunctionParameter> parameters = new ArrayList<>();
  for (final Ord<RelProtoDataType> type : Ord.zip(getParams())) {
    parameters.add(
        new FunctionParameter() {
          public int getOrdinal() {
            return type.i;
          }

          public String getName() {
            return "arg" + type.i;
          }

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

          public boolean isOptional() {
            return false;
          }
        });
  }
  return parameters;
}
 
Example #2
Source File: ReflectiveFunctionBase.java    From calcite 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 #3
Source File: AggregateFunctionImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Private constructor; use {@link #create}. */
private AggregateFunctionImpl(Class<?> declaringClass,
    List<FunctionParameter> params,
    List<Class<?>> valueTypes,
    Class<?> accumulatorType,
    Class<?> resultType,
    Method initMethod,
    Method addMethod,
    Method mergeMethod,
    Method resultMethod) {
  this.declaringClass = declaringClass;
  this.valueTypes = ImmutableList.copyOf(valueTypes);
  this.parameters = params;
  this.accumulatorType = accumulatorType;
  this.resultType = resultType;
  this.initMethod = Objects.requireNonNull(initMethod);
  this.addMethod = Objects.requireNonNull(addMethod);
  this.mergeMethod = mergeMethod;
  this.resultMethod = resultMethod;
  this.isStatic = Modifier.isStatic(initMethod.getModifiers());

  assert resultMethod != null || accumulatorType == resultType;
}
 
Example #4
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 #5
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 #6
Source File: SqlUserDefinedTableMacro.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Converts arguments from {@link org.apache.calcite.sql.SqlNode} to
 * java object format.
 *
 * @param typeFactory type factory used to convert the arguments
 * @param operandList input arguments
 * @param function target function to get parameter types from
 * @param opName name of the operator to use in error message
 * @param failOnNonLiteral true when conversion should fail on non-literal
 * @return converted list of arguments
 */
public static List<Object> convertArguments(RelDataTypeFactory typeFactory,
    List<SqlNode> operandList, Function function,
    SqlIdentifier opName,
    boolean failOnNonLiteral) {
  List<Object> arguments = new ArrayList<>(operandList.size());
  // Construct a list of arguments, if they are all constants.
  for (Pair<FunctionParameter, SqlNode> pair
      : Pair.zip(function.getParameters(), operandList)) {
    try {
      final Object o = getValue(pair.right);
      final Object o2 = coerce(o, pair.left.getType(typeFactory));
      arguments.add(o2);
    } catch (NonLiteralException e) {
      if (failOnNonLiteral) {
        throw new IllegalArgumentException("All arguments of call to macro "
            + opName + " should be literal. Actual argument #"
            + pair.left.getOrdinal() + " (" + pair.left.getName()
            + ") is not literal: " + pair.right);
      }
      final RelDataType type = pair.left.getType(typeFactory);
      final Object value;
      if (type.isNullable()) {
        value = null;
      } else {
        value = 0L;
      }
      arguments.add(value);
    }
  }
  return arguments;
}
 
Example #7
Source File: SqlUserDefinedAggFunction.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public List<RelDataType> getParamTypes() {
  List<RelDataType> argTypes = new ArrayList<>();
  for (FunctionParameter o : function.getParameters()) {
    final RelDataType type = o.getType(typeFactory);
    argTypes.add(type);
  }
  return toSql(argTypes);
}
 
Example #8
Source File: SqlUserDefinedAggFunction.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public List<RelDataType> getParamTypes() {
  List<RelDataType> argTypes = new ArrayList<>();
  for (FunctionParameter o : function.getParameters()) {
    final RelDataType type = o.getType(typeFactory);
    argTypes.add(type);
  }
  return toSql(argTypes);
}
 
Example #9
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 #10
Source File: WithOptionsTableMacro.java    From dremio-oss 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.getParams().size(); i++) {
    final TableInstance.TableParamDef p = sig.getParams().get(i);
    final int ordinal = i;
    result.add(new FunctionParameter() {
      @Override
      public int getOrdinal() {
        return ordinal;
      }

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

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

      @Override
      public boolean isOptional() {
        return p.isOptional();
      }
    });
  }
  return result;
}
 
Example #11
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 #12
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 #13
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 #14
Source File: SqlUserDefinedTableMacro.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Converts arguments from {@link org.apache.calcite.sql.SqlNode} to
 * java object format.
 *
 * @param typeFactory type factory used to convert the arguments
 * @param operandList input arguments
 * @param function target function to get parameter types from
 * @param opName name of the operator to use in error message
 * @param failOnNonLiteral true when conversion should fail on non-literal
 * @return converted list of arguments
 */
public static List<Object> convertArguments(RelDataTypeFactory typeFactory,
    List<SqlNode> operandList, Function function,
    SqlIdentifier opName,
    boolean failOnNonLiteral) {
  List<Object> arguments = new ArrayList<>(operandList.size());
  // Construct a list of arguments, if they are all constants.
  for (Pair<FunctionParameter, SqlNode> pair
      : Pair.zip(function.getParameters(), operandList)) {
    try {
      final Object o = getValue(pair.right);
      final Object o2 = coerce(o, pair.left.getType(typeFactory));
      arguments.add(o2);
    } catch (NonLiteralException e) {
      if (failOnNonLiteral) {
        throw new IllegalArgumentException("All arguments of call to macro "
            + opName + " should be literal. Actual argument #"
            + pair.left.getOrdinal() + " (" + pair.left.getName()
            + ") is not literal: " + pair.right);
      }
      final RelDataType type = pair.left.getType(typeFactory);
      final Object value;
      if (type.isNullable()) {
        value = null;
      } else {
        value = 0L;
      }
      arguments.add(value);
    }
  }
  return arguments;
}
 
Example #15
Source File: SqlUserDefinedTableMacro.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override public List<String> getParamNames() {
  return Lists.transform(tableMacro.getParameters(),
      FunctionParameter::getName);
}
 
Example #16
Source File: ViewTableMacro.java    From calcite with Apache License 2.0 4 votes vote down vote up
public List<FunctionParameter> getParameters() {
  return Collections.emptyList();
}
 
Example #17
Source File: ReflectiveFunctionBase.java    From calcite with Apache License 2.0 4 votes vote down vote up
public ImmutableList<FunctionParameter> build() {
  return ImmutableList.copyOf(builder);
}
 
Example #18
Source File: AggregateFunctionImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
public List<FunctionParameter> getParameters() {
  return parameters;
}
 
Example #19
Source File: SqlUserDefinedFunction.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override public List<String> getParamNames() {
  return Lists.transform(function.getParameters(),
      FunctionParameter::getName);
}
 
Example #20
Source File: SqlUserDefinedFunction.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public List<String> getParamNames() {
  return Lists.transform(function.getParameters(),
      FunctionParameter::getName);
}
 
Example #21
Source File: SqlUserDefinedTableMacro.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public List<String> getParamNames() {
  return Lists.transform(tableMacro.getParameters(),
      FunctionParameter::getName);
}
 
Example #22
Source File: ReflectiveFunctionBase.java    From Bats with Apache License 2.0 4 votes vote down vote up
public ImmutableList<FunctionParameter> build() {
  return ImmutableList.copyOf(builder);
}
 
Example #23
Source File: SqlAdvisorGetHintsFunction.java    From calcite with Apache License 2.0 4 votes vote down vote up
public List<FunctionParameter> getParameters() {
  return PARAMETERS;
}
 
Example #24
Source File: SqlAdvisorGetHintsFunction2.java    From calcite with Apache License 2.0 4 votes vote down vote up
public List<FunctionParameter> getParameters() {
  return PARAMETERS;
}
 
Example #25
Source File: ViewTableMacro.java    From Bats with Apache License 2.0 4 votes vote down vote up
public List<FunctionParameter> getParameters() {
  return Collections.emptyList();
}
 
Example #26
Source File: SamzaSqlScalarFunctionImpl.java    From samza with Apache License 2.0 4 votes vote down vote up
@Override
public List<FunctionParameter> getParameters() {
  return myIncFunction.getParameters();
}
 
Example #27
Source File: ReflectiveFunctionBase.java    From Bats with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the parameters of this function.
 *
 * @return Parameters; never null
 */
public List<FunctionParameter> getParameters() {
  return parameters;
}
 
Example #28
Source File: ReflectiveFunctionBase.java    From calcite with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the parameters of this function.
 *
 * @return Parameters; never null
 */
public List<FunctionParameter> getParameters() {
  return parameters;
}