org.apache.calcite.adapter.enumerable.CallImplementor Java Examples

The following examples show how to use org.apache.calcite.adapter.enumerable.CallImplementor. 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: SamzaSqlScalarFunctionImpl.java    From samza with Apache License 2.0 6 votes vote down vote up
@Override
public CallImplementor getImplementor() {
  return RexImpTable.createImplementor((translator, call, translatedOperands) -> {
    final Expression sqlContext = Expressions.parameter(SamzaSqlExecutionContext.class, "sqlContext");
    final Expression samzaContext = Expressions.parameter(SamzaSqlExecutionContext.class, "context");
    final Expression getUdfInstance = Expressions.call(ScalarUdf.class, sqlContext, getUdfMethod,
        Expressions.constant(udfMethod.getDeclaringClass().getName()), Expressions.constant(udfName), samzaContext);

    List<Expression> convertedOperands = new ArrayList<>();
    // SAMZA: 2230 To allow UDFS to accept Untyped arguments.
    // We explicitly Convert the untyped arguments to type that the UDf expects.
    for (int index = 0; index < translatedOperands.size(); index++) {
      if (!udfMetadata.isDisableArgCheck() && translatedOperands.get(index).type == Object.class
          && udfMethod.getParameters()[index].getType() != Object.class) {
        convertedOperands.add(Expressions.convert_(translatedOperands.get(index), udfMethod.getParameters()[index].getType()));
      } else {
        convertedOperands.add(translatedOperands.get(index));
      }
    }

    final Expression callExpression = Expressions.call(Expressions.convert_(getUdfInstance, udfMethod.getDeclaringClass()), udfMethod,
        convertedOperands);
    return callExpression;
  }, NullPolicy.NONE, false);
}
 
Example #2
Source File: TableFunctionImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Creates a {@link TableFunctionImpl} from a method. */
public static TableFunction create(final Method method) {
  if (!Modifier.isStatic(method.getModifiers())) {
    Class clazz = method.getDeclaringClass();
    if (!classHasPublicZeroArgsConstructor(clazz)) {
      throw RESOURCE.requireDefaultConstructor(clazz.getName()).ex();
    }
  }
  final Class<?> returnType = method.getReturnType();
  if (!QueryableTable.class.isAssignableFrom(returnType)
      && !ScannableTable.class.isAssignableFrom(returnType)) {
    return null;
  }
  CallImplementor implementor = createImplementor(method);
  return new TableFunctionImpl(method, implementor);
}
 
Example #3
Source File: TableFunctionImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
private static CallImplementor createImplementor(final Method method) {
  return RexImpTable.createImplementor(
      new ReflectiveCallNotNullImplementor(method) {
        public Expression implement(RexToLixTranslator translator,
            RexCall call, List<Expression> translatedOperands) {
          Expression expr = super.implement(translator, call,
              translatedOperands);
          final Class<?> returnType = method.getReturnType();
          if (QueryableTable.class.isAssignableFrom(returnType)) {
            Expression queryable = Expressions.call(
                Expressions.convert_(expr, QueryableTable.class),
                BuiltInMethod.QUERYABLE_TABLE_AS_QUERYABLE.method,
                Expressions.call(DataContext.ROOT,
                    BuiltInMethod.DATA_CONTEXT_GET_QUERY_PROVIDER.method),
                Expressions.constant(null, SchemaPlus.class),
                Expressions.constant(call.getOperator().getName(), String.class));
            expr = Expressions.call(queryable,
                BuiltInMethod.QUERYABLE_AS_ENUMERABLE.method);
          } else {
            expr = Expressions.call(expr,
                BuiltInMethod.SCANNABLE_TABLE_SCAN.method, DataContext.ROOT);
          }
          return expr;
        }
      }, NullPolicy.NONE, false);
}
 
Example #4
Source File: HiveSqlOperatorTable.java    From marble with Apache License 2.0 5 votes vote down vote up
private void defineImplementors() {
  //define implementors for hive operators
  final List<SqlOperator> operatorList = getOperatorList();
  RexImpTable.INSTANCE.defineImplementors((map, aggMap, winAggMap) -> {
    for (SqlOperator sqlOperator : operatorList) {
      if (sqlOperator instanceof HiveSqlAggFunction) {
        HiveSqlAggFunction aggFunction = (HiveSqlAggFunction) sqlOperator;
        aggMap.put(aggFunction, () -> new HiveUDAFImplementor(aggFunction));
      } else {
        /**since SqlOperator is identified by name and kind ,see
         *  {@link SqlOperator#equals(Object)} and
         *  {@link SqlOperator#hashCode()},
         *  we can override implementors of operators that declared in
         *  SqlStdOperatorTable
         *  */
        CallImplementor callImplementor;
        if (sqlOperator.getName().equals("NOT RLIKE") || sqlOperator.getName()
            .equals("NOT REGEXP")) {
          callImplementor =
              RexImpTable.createImplementor(
                  RexImpTable.NotImplementor.of(
                      new HiveUDFImplementor()), NullPolicy.STRICT, false);
        } else {
          callImplementor =
              RexImpTable.createImplementor(
                  new HiveUDFImplementor(), NullPolicy.NONE, false);
        }
        map.put(sqlOperator, callImplementor);

      }

    }
    // directly override some implementors of SqlOperator that declared in
    // SqlStdOperatorTable
    map.put(SqlStdOperatorTable.ITEM,
        new RexImpTable.ItemImplementor(true));
  });


}
 
Example #5
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 #6
Source File: UdfTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public CallImplementor getImplementor() {
  return (translator, call, nullAs) -> {
    Method lookupMethod =
        Types.lookupMethod(Smalls.AllTypesFunction.class,
            "arrayAppendFun", List.class, Integer.class);
    return Expressions.call(lookupMethod,
        translator.translateList(call.getOperands(), nullAs));
  };
}
 
Example #7
Source File: CollectionsFunctions.java    From mat-calcite-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public CallImplementor getImplementor() {
    return implementor;
}
 
Example #8
Source File: SqlAdvisorGetHintsFunction2.java    From calcite with Apache License 2.0 4 votes vote down vote up
public CallImplementor getImplementor() {
  return IMPLEMENTOR;
}
 
Example #9
Source File: SqlAdvisorGetHintsFunction.java    From calcite with Apache License 2.0 4 votes vote down vote up
public CallImplementor getImplementor() {
  return IMPLEMENTOR;
}
 
Example #10
Source File: ScalarFunctionImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Private constructor. */
private ScalarFunctionImpl(Method method, CallImplementor implementor) {
  super(method);
  this.implementor = implementor;
}
 
Example #11
Source File: ScalarFunctionImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
public CallImplementor getImplementor() {
  return implementor;
}
 
Example #12
Source File: ScalarFunctionImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
private static CallImplementor createImplementor(final Method method) {
  final NullPolicy nullPolicy = getNullPolicy(method);
  return RexImpTable.createImplementor(
      new ReflectiveCallNotNullImplementor(method), nullPolicy, false);
}
 
Example #13
Source File: TableFunctionImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Private constructor; use {@link #create}. */
private TableFunctionImpl(Method method, CallImplementor implementor) {
  super(method);
  this.implementor = implementor;
}
 
Example #14
Source File: TableFunctionImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
public CallImplementor getImplementor() {
  return implementor;
}
 
Example #15
Source File: ImplementableFunction.java    From calcite with Apache License 2.0 2 votes vote down vote up
/**
 * Returns implementor that translates the function to linq4j expression.
 * @return implementor that translates the function to linq4j expression.
 */
CallImplementor getImplementor();
 
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);
}