public F getFunction() { if (function != null) { return function; } if (dynamicFunction == null) { final Invokable x = compile(); //noinspection unchecked dynamicFunction = (F) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{Types.toClass(type)}, (proxy, method, args) -> x.dynamicInvoke(args)); } return dynamicFunction; }
private static Object coerce(Object o, RelDataType type) { if (o == null) { return null; } if (!(type instanceof RelDataTypeFactoryImpl.JavaType)) { return null; } final RelDataTypeFactoryImpl.JavaType javaType = (RelDataTypeFactoryImpl.JavaType) type; final Class clazz = javaType.getJavaClass(); //noinspection unchecked if (clazz.isAssignableFrom(o.getClass())) { return o; } if (clazz == String.class && o instanceof NlsString) { return ((NlsString) o).getValue(); } // We need optimization here for constant folding. // Not all the expressions can be interpreted (e.g. ternary), so // we rely on optimization capabilities to fold non-interpretable // expressions. BlockBuilder bb = new BlockBuilder(); final Expression expr = RexToLixTranslator.convert(Expressions.constant(o), clazz); bb.add(Expressions.return_(null, expr)); final FunctionExpression convert = Expressions.lambda(bb.toBlock(), Collections.emptyList()); return convert.compile().dynamicInvoke(); }
private static Object coerce(Object o, RelDataType type) { if (o == null) { return null; } if (!(type instanceof RelDataTypeFactoryImpl.JavaType)) { return null; } final RelDataTypeFactoryImpl.JavaType javaType = (RelDataTypeFactoryImpl.JavaType) type; final Class clazz = javaType.getJavaClass(); //noinspection unchecked if (clazz.isAssignableFrom(o.getClass())) { return o; } if (clazz == String.class && o instanceof NlsString) { return ((NlsString) o).getValue(); } // We need optimization here for constant folding. // Not all the expressions can be interpreted (e.g. ternary), so // we rely on optimization capabilities to fold non-interpretable // expressions. BlockBuilder bb = new BlockBuilder(); final Expression expr = RexToLixTranslator.convert(Expressions.constant(o), clazz); bb.add(Expressions.return_(null, expr)); final FunctionExpression convert = Expressions.lambda(bb.toBlock(), Collections.emptyList()); return convert.compile().dynamicInvoke(); }