org.apache.calcite.schema.ScalarFunction Java Examples

The following examples show how to use org.apache.calcite.schema.ScalarFunction. 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: ScalarFunctionImpl.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Creates {@link org.apache.calcite.schema.ScalarFunction} for each method in
 * a given class.
 */
public static ImmutableMultimap<String, ScalarFunction> createAll(
    Class<?> clazz) {
  final ImmutableMultimap.Builder<String, ScalarFunction> builder =
      ImmutableMultimap.builder();
  for (Method method : clazz.getMethods()) {
    if (method.getDeclaringClass() == Object.class) {
      continue;
    }
    if (!Modifier.isStatic(method.getModifiers())
        && !classHasPublicZeroArgsConstructor(clazz)) {
      continue;
    }
    final ScalarFunction function = create(method);
    builder.put(method.getName(), function);
  }
  return builder.build();
}
 
Example #2
Source File: ScalarFunctionImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Creates {@link org.apache.calcite.schema.ScalarFunction} for each method in
 * a given class.
 */
public static ImmutableMultimap<String, ScalarFunction> createAll(
    Class<?> clazz) {
  final ImmutableMultimap.Builder<String, ScalarFunction> builder =
      ImmutableMultimap.builder();
  for (Method method : clazz.getMethods()) {
    if (method.getDeclaringClass() == Object.class) {
      continue;
    }
    if (!Modifier.isStatic(method.getModifiers())
        && !classHasPublicZeroArgsConstructor(clazz)) {
      continue;
    }
    final ScalarFunction function = create(method);
    builder.put(method.getName(), function);
  }
  return builder.build();
}
 
Example #3
Source File: ScalarFunctionImpl.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Creates {@link org.apache.calcite.schema.ScalarFunction} from given method.
 * When {@code eval} method does not suit, {@code null} is returned.
 *
 * @param method method that is used to implement the function
 * @return created {@link ScalarFunction} or null
 */
public static ScalarFunction create(Method method) {
  if (!Modifier.isStatic(method.getModifiers())) {
    Class<?> clazz = method.getDeclaringClass();
    if (!classHasPublicZeroArgsConstructor(clazz)) {
      throw RESOURCE.requireDefaultConstructor(clazz.getName()).ex();
    }
  }
  return new ScalarFunctionImpl(method);
}
 
Example #4
Source File: JSqlSchema.java    From kafka-eagle with Apache License 2.0 5 votes vote down vote up
@Override
protected Multimap<String, Function> getFunctionMultimap() {
	ImmutableMultimap<String, ScalarFunction> funcs = ScalarFunctionImpl.createAll(JSONFunction.class);
	Multimap<String, Function> functions = HashMultimap.create();
	for (String key : funcs.keySet()) {
		for (ScalarFunction func : funcs.get(key)) {
			functions.put(key, func);
		}
	}
	return functions;
}
 
Example #5
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 #6
Source File: DremioCatalogReader.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private SqlReturnTypeInference infer(final ScalarFunction function) {
  return new SqlReturnTypeInference() {
    @Override
    public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
      final RelDataType type = function.getReturnType(typeFactory);
      return toSql(type);
    }
  };
}
 
Example #7
Source File: PigRelSqlUdfs.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the return data type for a given function.
 *
 * @param function ScalarFunction
 * @return returned data type
 */
private static RelDataType getRelDataType(ScalarFunction function) {
  final JavaTypeFactory typeFactory = TYPE_FACTORY;
  final RelDataType type = function.getReturnType(typeFactory);
  if (type instanceof RelDataTypeFactoryImpl.JavaType
      && ((RelDataTypeFactoryImpl.JavaType) type).getJavaClass()
      == Object.class) {
    return typeFactory.createTypeWithNullability(
        typeFactory.createSqlType(SqlTypeName.ANY), true);
  }
  return typeFactory.toSql(type);
}
 
Example #8
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 #9
Source File: CalciteCatalogReader.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static SqlReturnTypeInference infer(final ScalarFunction function) {
  return opBinding -> {
    final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
    final RelDataType type;
    if (function instanceof ScalarFunctionImpl) {
      type = ((ScalarFunctionImpl) function).getReturnType(typeFactory,
          opBinding);
    } else {
      type = function.getReturnType(typeFactory);
    }
    return toSql(typeFactory, type);
  };
}
 
Example #10
Source File: ScalarFunctionImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates {@link org.apache.calcite.schema.ScalarFunction} from given method.
 * When {@code eval} method does not suit, {@code null} is returned.
 *
 * @param method method that is used to implement the function
 * @return created {@link ScalarFunction} or null
 */
public static ScalarFunction create(Method method) {
  if (!Modifier.isStatic(method.getModifiers())) {
    Class clazz = method.getDeclaringClass();
    if (!classHasPublicZeroArgsConstructor(clazz)) {
      throw RESOURCE.requireDefaultConstructor(clazz.getName()).ex();
    }
  }
  CallImplementor implementor = createImplementor(method);
  return new ScalarFunctionImpl(method, implementor);
}
 
Example #11
Source File: CollectionsFunctions.java    From mat-calcite-plugin with Apache License 2.0 4 votes vote down vote up
public static Multimap<String, ScalarFunction> createAll() {
    ImmutableMultimap.Builder<String, ScalarFunction> builder = ImmutableMultimap.builder();
    builder.put("asMap", new MapFunction(findMethod(CollectionsFunctions.class, "asMap")));
    builder.put("asMultiSet", new MultiSetFunction(findMethod(CollectionsFunctions.class, "asMultiSet")));
    return builder.build();
}
 
Example #12
Source File: ScalarFunctionImpl.java    From Bats with Apache License 2.0 3 votes vote down vote up
/**
 * Creates {@link org.apache.calcite.schema.ScalarFunction} from given class.
 *
 * <p>If a method of the given name is not found or it does not suit,
 * returns {@code null}.
 *
 * @param clazz class that is used to implement the function
 * @param methodName Method name (typically "eval")
 * @return created {@link ScalarFunction} or null
 */
public static ScalarFunction create(Class<?> clazz, String methodName) {
  final Method method = findMethod(clazz, methodName);
  if (method == null) {
    return null;
  }
  return create(method);
}
 
Example #13
Source File: SQLExecEnvironment.java    From attic-apex-malhar with Apache License 2.0 3 votes vote down vote up
/**
 * Register custom function from given static method with this {@link SQLExecEnvironment}
 *
 * @param name Name of the scalar SQL function that needs make available to SQL Statement
 * @param clazz {@link Class} which contains given static method
 * @param methodName Name of the method from given clazz
 *
 * @return Return this {@link SQLExecEnvironment}
 */
public SQLExecEnvironment registerFunction(String name, Class clazz, String methodName)
{
  Preconditions.checkNotNull(name, "Function name cannot be null");
  ScalarFunction scalarFunction = ScalarFunctionImpl.create(clazz, methodName);
  return registerFunction(name, scalarFunction);
}
 
Example #14
Source File: ScalarFunctionImpl.java    From calcite with Apache License 2.0 3 votes vote down vote up
/**
 * Creates {@link org.apache.calcite.schema.ScalarFunction} from given class.
 *
 * <p>If a method of the given name is not found or it does not suit,
 * returns {@code null}.
 *
 * @param clazz class that is used to implement the function
 * @param methodName Method name (typically "eval")
 * @return created {@link ScalarFunction} or null
 */
public static ScalarFunction create(Class<?> clazz, String methodName) {
  final Method method = findMethod(clazz, methodName);
  if (method == null) {
    return null;
  }
  return create(method);
}
 
Example #15
Source File: PigRelSqlUdfs.java    From calcite with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the SqlReturnTypeInference that can infer the return type from a
 * function.
 *
 * @param function ScalarFunction
 * @return SqlReturnTypeInference
 */
private static SqlReturnTypeInference infer(final ScalarFunction function) {
  return opBinding -> getRelDataType(function);
}
 
Example #16
Source File: ScalarFunctionImpl.java    From calcite with Apache License 2.0 2 votes vote down vote up
/**
 * Creates unsafe version of {@link ScalarFunction} from any method. The method
 * does not need to be static or belong to a class with default constructor. It is
 * the responsibility of the underlying engine to initialize the UDF object that
 * contain the method.
 *
 * @param method method that is used to implement the function
 */
public static ScalarFunction createUnsafe(Method method) {
  CallImplementor implementor = createImplementor(method);
  return new ScalarFunctionImpl(method, implementor);
}