You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@calcite.apache.org by Rui Wang <am...@apache.org> on 2019/12/31 04:13:49 UTC

A linq4j question

Hi community,

Let's say I define a function like this:

List<Long> myFunc() {
  // function body
  // do something
  // return a list of long
}

Then by linq4j library, I can

Expression ret = Expressions.call(
  FindMethod("myFunc")
)


my question is, how to make the returned expression of the call to be a
list, or something equivalent?

Basically I know the function return a list so I want to access each
element of it. Ideally I can get a List<Expression> ret =
Expressions.call(FindMethod("myFunc"))


-Rui

Re: A linq4j question

Posted by Rui Wang <am...@apache.org>.
I agree with you analysis. The primary problem is we cannot know the length
of the list before runtime thus we cannot write a for loop before runtime.

The right way will be what you have suggested, to write a for statement
directly and generate code from there.


Thanks,
Rui

On Mon, Dec 30, 2019 at 10:30 PM Feng Zhu <we...@gmail.com> wrote:

> >
> >   As you can see, I know "myFunc" returns a list, so I want to make each
> > element of that list an expression so I can continue manipulating them.
> > Eventually they will be used to generate code.
> >
>
> AFAIK, it is nearly impossible know the list's length if we do not rely on
> "evaluate", because it is determined in runtime.
> Therefore, the (*List<Expression>*) may be difficult.
> But if you want to manipulate the list element in code, I think we can also
> use the functions provided in Expressions.
>
> *    // org.apache.calcite.runtime.SqlFunctions.myFunc()*
>     MethodCallExpression mce = Expressions.call(SqlFunctions.class,
> "myFunc");
> *    // final java.util.List res*
>     ParameterExpression pe = Expressions.parameter(mce.type, "res");
> *    // final java.util.List res =
> org.apache.calcite.runtime.SqlFunctions.myFunc();*
>     DeclarationStatement statement = Expressions.declare(Modifier.FINAL,
> pe, mce);
>     System.out.println(statement);
>
>     ParameterExpression pe1 = Expressions.parameter(Object.class, "obj");
>     Expression input = Expressions.convert_(pe1, Long.class);
>     MethodCallExpression mce2 = Expressions.call(SqlFunctions.class,
> "myFunc2", input);
>     org.apache.calcite.linq4j.tree.Statement s =
> Expressions.statement(mce2);
>
>
>
> *    //for (Object obj : res) {    //
>  org.apache.calcite.runtime.SqlFunctions.myFunc2((Long) obj);    //}*
>     ForEachStatement forEachStatement = Expressions.forEach(pe1, pe, s);
>
>
> Best,
> Feng
>
> Rui Wang <am...@apache.org> 于2019年12月31日周二 下午1:42写道:
>
> > Thanks Feng.
> >
> > I want to access elements before evaluation. Use your example:
> >
> > Define:
> >  public static List<Long> myFunc() {    return Arrays.asList(1L, 2L);  }
> >  public static Long myFunc2(Long i) {    return i - 1l }
> >
> > I want build expressions as the following:
> >
> > List<Expression> ret = new ArrayList();
> > MethodCallExpression mce = Expressions.call(SqlFunctions.class,"myFunc");
> >
> > // how to make each element an expression?
> > for (Expression expr : mce) {
> >      ret.dd(Expressions.call(SqlFunctions.class, "myFunc2", expr));
> > }
> >
> > return ret;
> >
> >
> >
> > As you can see, I know "myFunc" returns a list, so I want to make each
> > element of that list an expression so I can continue manipulating them.
> > Eventually they will be used to generate code.
> >
> >
> >
> > -Rui
> >
> >
> >
> >
> >
> >
> > On Mon, Dec 30, 2019 at 9:29 PM Feng Zhu <we...@gmail.com> wrote:
> >
> > > >
> > > > Basically I know the function return a list so I want to access each
> > > > element of it. Ideally I can get a List<Expression> ret =
> > > > Expressions.call(FindMethod("myFunc"))
> > > >
> > > Can you elaborate on what do you want? i.e., *List<Expression> ret*
> > > If you want to get the element of a method call result, you can
> evaluate
> > > the expression and cast it to list.
> > >
> > > For example, assume "myFunc" in SqlFunctions.
> > >
> > >
> > >
> > > *  public static List<Long> myFunc() {    return Arrays.asList(1L, 2L);
> > }*
> > >
> > >
> > > *    MethodCallExpression mce = Expressions.call(SqlFunctions.class,
> > > "myFunc");    List<Long> ret = (List<Long>) Expressions.evaluate(mce);*
> > >
> > > Best,
> > >
> > > Rui Wang <am...@apache.org> 于2019年12月31日周二 下午12:14写道:
> > >
> > > > Hi community,
> > > >
> > > > Let's say I define a function like this:
> > > >
> > > > List<Long> myFunc() {
> > > >   // function body
> > > >   // do something
> > > >   // return a list of long
> > > > }
> > > >
> > > > Then by linq4j library, I can
> > > >
> > > > Expression ret = Expressions.call(
> > > >   FindMethod("myFunc")
> > > > )
> > > >
> > > >
> > > > my question is, how to make the returned expression of the call to
> be a
> > > > list, or something equivalent?
> > > >
> > > > Basically I know the function return a list so I want to access each
> > > > element of it. Ideally I can get a List<Expression> ret =
> > > > Expressions.call(FindMethod("myFunc"))
> > > >
> > > >
> > > > -Rui
> > > >
> > >
> >
>

Re: A linq4j question

Posted by Feng Zhu <we...@gmail.com>.
>
>   As you can see, I know "myFunc" returns a list, so I want to make each
> element of that list an expression so I can continue manipulating them.
> Eventually they will be used to generate code.
>

AFAIK, it is nearly impossible know the list's length if we do not rely on
"evaluate", because it is determined in runtime.
Therefore, the (*List<Expression>*) may be difficult.
But if you want to manipulate the list element in code, I think we can also
use the functions provided in Expressions.

*    // org.apache.calcite.runtime.SqlFunctions.myFunc()*
    MethodCallExpression mce = Expressions.call(SqlFunctions.class,
"myFunc");
*    // final java.util.List res*
    ParameterExpression pe = Expressions.parameter(mce.type, "res");
*    // final java.util.List res =
org.apache.calcite.runtime.SqlFunctions.myFunc();*
    DeclarationStatement statement = Expressions.declare(Modifier.FINAL,
pe, mce);
    System.out.println(statement);

    ParameterExpression pe1 = Expressions.parameter(Object.class, "obj");
    Expression input = Expressions.convert_(pe1, Long.class);
    MethodCallExpression mce2 = Expressions.call(SqlFunctions.class,
"myFunc2", input);
    org.apache.calcite.linq4j.tree.Statement s =
Expressions.statement(mce2);



*    //for (Object obj : res) {    //
 org.apache.calcite.runtime.SqlFunctions.myFunc2((Long) obj);    //}*
    ForEachStatement forEachStatement = Expressions.forEach(pe1, pe, s);


Best,
Feng

Rui Wang <am...@apache.org> 于2019年12月31日周二 下午1:42写道:

> Thanks Feng.
>
> I want to access elements before evaluation. Use your example:
>
> Define:
>  public static List<Long> myFunc() {    return Arrays.asList(1L, 2L);  }
>  public static Long myFunc2(Long i) {    return i - 1l }
>
> I want build expressions as the following:
>
> List<Expression> ret = new ArrayList();
> MethodCallExpression mce = Expressions.call(SqlFunctions.class,"myFunc");
>
> // how to make each element an expression?
> for (Expression expr : mce) {
>      ret.dd(Expressions.call(SqlFunctions.class, "myFunc2", expr));
> }
>
> return ret;
>
>
>
> As you can see, I know "myFunc" returns a list, so I want to make each
> element of that list an expression so I can continue manipulating them.
> Eventually they will be used to generate code.
>
>
>
> -Rui
>
>
>
>
>
>
> On Mon, Dec 30, 2019 at 9:29 PM Feng Zhu <we...@gmail.com> wrote:
>
> > >
> > > Basically I know the function return a list so I want to access each
> > > element of it. Ideally I can get a List<Expression> ret =
> > > Expressions.call(FindMethod("myFunc"))
> > >
> > Can you elaborate on what do you want? i.e., *List<Expression> ret*
> > If you want to get the element of a method call result, you can evaluate
> > the expression and cast it to list.
> >
> > For example, assume "myFunc" in SqlFunctions.
> >
> >
> >
> > *  public static List<Long> myFunc() {    return Arrays.asList(1L, 2L);
> }*
> >
> >
> > *    MethodCallExpression mce = Expressions.call(SqlFunctions.class,
> > "myFunc");    List<Long> ret = (List<Long>) Expressions.evaluate(mce);*
> >
> > Best,
> >
> > Rui Wang <am...@apache.org> 于2019年12月31日周二 下午12:14写道:
> >
> > > Hi community,
> > >
> > > Let's say I define a function like this:
> > >
> > > List<Long> myFunc() {
> > >   // function body
> > >   // do something
> > >   // return a list of long
> > > }
> > >
> > > Then by linq4j library, I can
> > >
> > > Expression ret = Expressions.call(
> > >   FindMethod("myFunc")
> > > )
> > >
> > >
> > > my question is, how to make the returned expression of the call to be a
> > > list, or something equivalent?
> > >
> > > Basically I know the function return a list so I want to access each
> > > element of it. Ideally I can get a List<Expression> ret =
> > > Expressions.call(FindMethod("myFunc"))
> > >
> > >
> > > -Rui
> > >
> >
>

Re: A linq4j question

Posted by Rui Wang <am...@apache.org>.
Thanks Feng.

I want to access elements before evaluation. Use your example:

Define:
 public static List<Long> myFunc() {    return Arrays.asList(1L, 2L);  }
 public static Long myFunc2(Long i) {    return i - 1l }

I want build expressions as the following:

List<Expression> ret = new ArrayList();
MethodCallExpression mce = Expressions.call(SqlFunctions.class,"myFunc");

// how to make each element an expression?
for (Expression expr : mce) {
     ret.dd(Expressions.call(SqlFunctions.class, "myFunc2", expr));
}

return ret;



As you can see, I know "myFunc" returns a list, so I want to make each
element of that list an expression so I can continue manipulating them.
Eventually they will be used to generate code.



-Rui






On Mon, Dec 30, 2019 at 9:29 PM Feng Zhu <we...@gmail.com> wrote:

> >
> > Basically I know the function return a list so I want to access each
> > element of it. Ideally I can get a List<Expression> ret =
> > Expressions.call(FindMethod("myFunc"))
> >
> Can you elaborate on what do you want? i.e., *List<Expression> ret*
> If you want to get the element of a method call result, you can evaluate
> the expression and cast it to list.
>
> For example, assume "myFunc" in SqlFunctions.
>
>
>
> *  public static List<Long> myFunc() {    return Arrays.asList(1L, 2L);  }*
>
>
> *    MethodCallExpression mce = Expressions.call(SqlFunctions.class,
> "myFunc");    List<Long> ret = (List<Long>) Expressions.evaluate(mce);*
>
> Best,
>
> Rui Wang <am...@apache.org> 于2019年12月31日周二 下午12:14写道:
>
> > Hi community,
> >
> > Let's say I define a function like this:
> >
> > List<Long> myFunc() {
> >   // function body
> >   // do something
> >   // return a list of long
> > }
> >
> > Then by linq4j library, I can
> >
> > Expression ret = Expressions.call(
> >   FindMethod("myFunc")
> > )
> >
> >
> > my question is, how to make the returned expression of the call to be a
> > list, or something equivalent?
> >
> > Basically I know the function return a list so I want to access each
> > element of it. Ideally I can get a List<Expression> ret =
> > Expressions.call(FindMethod("myFunc"))
> >
> >
> > -Rui
> >
>

Re: A linq4j question

Posted by Feng Zhu <we...@gmail.com>.
>
> Basically I know the function return a list so I want to access each
> element of it. Ideally I can get a List<Expression> ret =
> Expressions.call(FindMethod("myFunc"))
>
Can you elaborate on what do you want? i.e., *List<Expression> ret*
If you want to get the element of a method call result, you can evaluate
the expression and cast it to list.

For example, assume "myFunc" in SqlFunctions.



*  public static List<Long> myFunc() {    return Arrays.asList(1L, 2L);  }*


*    MethodCallExpression mce = Expressions.call(SqlFunctions.class,
"myFunc");    List<Long> ret = (List<Long>) Expressions.evaluate(mce);*

Best,

Rui Wang <am...@apache.org> 于2019年12月31日周二 下午12:14写道:

> Hi community,
>
> Let's say I define a function like this:
>
> List<Long> myFunc() {
>   // function body
>   // do something
>   // return a list of long
> }
>
> Then by linq4j library, I can
>
> Expression ret = Expressions.call(
>   FindMethod("myFunc")
> )
>
>
> my question is, how to make the returned expression of the call to be a
> list, or something equivalent?
>
> Basically I know the function return a list so I want to access each
> element of it. Ideally I can get a List<Expression> ret =
> Expressions.call(FindMethod("myFunc"))
>
>
> -Rui
>