Re: A linq4j question

2019-12-30 Thread Rui Wang
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  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*) 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  于2019年12月31日周二 下午1:42写道:
>
> > Thanks Feng.
> >
> > I want to access elements before evaluation. Use your example:
> >
> > Define:
> >  public static List myFunc() {return Arrays.asList(1L, 2L);  }
> >  public static Long myFunc2(Long i) {return i - 1l }
> >
> > I want build expressions as the following:
> >
> > List 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  wrote:
> >
> > > >
> > > > Basically I know the function return a list so I want to access each
> > > > element of it. Ideally I can get a List ret =
> > > > Expressions.call(FindMethod("myFunc"))
> > > >
> > > Can you elaborate on what do you want? i.e., *List 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 myFunc() {return Arrays.asList(1L, 2L);
> > }*
> > >
> > >
> > > *MethodCallExpression mce = Expressions.call(SqlFunctions.class,
> > > "myFunc");List ret = (List) Expressions.evaluate(mce);*
> > >
> > > Best,
> > >
> > > Rui Wang  于2019年12月31日周二 下午12:14写道:
> > >
> > > > Hi community,
> > > >
> > > > Let's say I define a function like this:
> > > >
> > > > List 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 ret =
> > > > Expressions.call(FindMethod("myFunc"))
> > > >
> > > >
> > > > -Rui
> > > >
> > >
> >
>


Re: A linq4j question

2019-12-30 Thread Feng Zhu
>
>   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*) 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  于2019年12月31日周二 下午1:42写道:

> Thanks Feng.
>
> I want to access elements before evaluation. Use your example:
>
> Define:
>  public static List myFunc() {return Arrays.asList(1L, 2L);  }
>  public static Long myFunc2(Long i) {return i - 1l }
>
> I want build expressions as the following:
>
> List 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  wrote:
>
> > >
> > > Basically I know the function return a list so I want to access each
> > > element of it. Ideally I can get a List ret =
> > > Expressions.call(FindMethod("myFunc"))
> > >
> > Can you elaborate on what do you want? i.e., *List 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 myFunc() {return Arrays.asList(1L, 2L);
> }*
> >
> >
> > *MethodCallExpression mce = Expressions.call(SqlFunctions.class,
> > "myFunc");List ret = (List) Expressions.evaluate(mce);*
> >
> > Best,
> >
> > Rui Wang  于2019年12月31日周二 下午12:14写道:
> >
> > > Hi community,
> > >
> > > Let's say I define a function like this:
> > >
> > > List 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 ret =
> > > Expressions.call(FindMethod("myFunc"))
> > >
> > >
> > > -Rui
> > >
> >
>


Re: A linq4j question

2019-12-30 Thread Rui Wang
Thanks Feng.

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

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

I want build expressions as the following:

List 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  wrote:

> >
> > Basically I know the function return a list so I want to access each
> > element of it. Ideally I can get a List ret =
> > Expressions.call(FindMethod("myFunc"))
> >
> Can you elaborate on what do you want? i.e., *List 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 myFunc() {return Arrays.asList(1L, 2L);  }*
>
>
> *MethodCallExpression mce = Expressions.call(SqlFunctions.class,
> "myFunc");List ret = (List) Expressions.evaluate(mce);*
>
> Best,
>
> Rui Wang  于2019年12月31日周二 下午12:14写道:
>
> > Hi community,
> >
> > Let's say I define a function like this:
> >
> > List 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 ret =
> > Expressions.call(FindMethod("myFunc"))
> >
> >
> > -Rui
> >
>


Re: A linq4j question

2019-12-30 Thread Feng Zhu
>
> Basically I know the function return a list so I want to access each
> element of it. Ideally I can get a List ret =
> Expressions.call(FindMethod("myFunc"))
>
Can you elaborate on what do you want? i.e., *List 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 myFunc() {return Arrays.asList(1L, 2L);  }*


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

Best,

Rui Wang  于2019年12月31日周二 下午12:14写道:

> Hi community,
>
> Let's say I define a function like this:
>
> List 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 ret =
> Expressions.call(FindMethod("myFunc"))
>
>
> -Rui
>