Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CALCITE-3679] Allow lambda expressions in SQL queries #1733

Closed
wants to merge 12 commits into from

Conversation

ritesh-kapoor
Copy link
Contributor

No description provided.

@ritesh-kapoor
Copy link
Contributor Author

This PR is incomplete in terms of functionality and requires alot of refactoring and test cases addition.

@danny0405
Copy link
Contributor

danny0405 commented Jan 9, 2020

An interesting topic, would review if i have time ~

@ritesh-kapoor
Copy link
Contributor Author

Need help in PR review :)

@ritesh-kapoor
Copy link
Contributor Author

@danny0405 Need you favour for PR review, I know its hard for you to review so many PR's. It would be really nice if you could help me with this one. :)

@danny0405
Copy link
Contributor

danny0405 commented Mar 16, 2020

@danny0405 Need you favour for PR review, I know its hard for you to review so many PR's. It would be really nice if you could help me with this one. :)

Would review this weekend ~ There are some conflicts, you can resolve them first.

@ritesh-kapoor
Copy link
Contributor Author

@danny0405 Need you favour for PR review, I know its hard for you to review so many PR's. It would be really nice if you could help me with this one. :)

Would review this weekend ~ There are some conflicts, you can resolve them first.

I have resolved conflicts, Thanks for taking out time. :)

if (argFamilies.get(i) != SqlTypeFamily.ANY) {
type = argFamilies.get(i)
.getDefaultConcreteType(callBinding.getTypeFactory());
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type inference should happen in SqlLambdaOperator, not the type checker(only check validated node types).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type inference is done in SqlLambdaOperator because return and parameter type information has scope in LambdaOperandTypeChecker.

@@ -687,6 +690,10 @@ private Expression translate0(RexNode expr, RexImpTable.NullAs nullAs,
Expression input = list.append("inp" + index + "_", x); // safe to share
return handleNullUnboxingIfNecessary(input, nullAs, storageType);
}
case LAMBDA_REF: {
final String name = ((RexLambdaRef) expr).getName();
return Expressions.parameter(typeFactory.getJavaClass(expr.getType()), name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remove this and the tests still passes, so, it seems not necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is important and there are test failures
FAILURE 49.5sec, 277 completed, 1 failed, 1 skipped, org.apache.calcite.test.CalciteSqlOperatorTest FAILURE 83.9sec, 5925 completed, 3 failed, 104 skipped, Gradle Test Run :core:test

* field a.</p>
*/
public class RexLambdaRef extends RexSlot {
//~ Static fields/initializers ---------------------------------------------
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't is just a RexLocalRef ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used to refer lambda ref, appears to be similar but are different

public SqlNodeList getParameters() {
return parameters;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unparse the SqlLambda correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like Sql Lambda is unparsed correctly, Please refer SqlParserTest.testLambdaExpr test case


@Override public RelDataType deriveType(
SqlValidator validator, SqlValidatorScope scope, SqlCall call) {
return validateOperands(validator, scope, call);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should implement the checkOperandTypes correctly to deduce the type, you can take SqlCaseOperator for a reference.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SqlCaseOperator has different implementation Lambda type inference is derived from LambdaOperandTypeChecker.java

OperandTypes.sequence("MAP_FILTER(<ANY>, <LAMBDA(BOOLEAN, ANY, ANY)>)",
OperandTypes.family(SqlTypeFamily.MAP),
OperandTypes.lambda(SqlTypeFamily.BOOLEAN, SqlTypeFamily.ANY, SqlTypeFamily.ANY)),
SqlFunctionCategory.SYSTEM);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the operand type check be OperandTypes.family(SqlTypeFamily.MAP, SqlTypeFamily.LAMBDA) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Map Function accepts lambda which returns boolean value and takes two arguments of any type. This seems to be correct.

@ritesh-kapoor
Copy link
Contributor Author

ritesh-kapoor commented Apr 28, 2020

I will resolve conflicts after PR tagged LGTM. Let me know if there are concerns :)

@jkeung
Copy link

jkeung commented Apr 21, 2023

@ritesh-kapoor @danny0405 Hello, was this PR ever approved, or what was blocking it? I am looking into lambda functions in SQL queries in Calcite.

@julianhyde julianhyde force-pushed the main branch 2 times, most recently from 8a5cf83 to cf7f71b Compare June 8, 2023 21:21
@gabrywu
Copy link
Member

gabrywu commented Jul 12, 2023

any updates here?

@MasseGuillaume
Copy link
Contributor

@mihaibudiu can you close this PR?

CALCITE-3679 got resolved in #3502

@mihaibudiu
Copy link
Contributor

Closing in favor of #3502.

@mihaibudiu mihaibudiu closed this Jan 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
7 participants