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-1581] UDTF like in hive #1138

Closed

Conversation

pengzhiwei2018
Copy link

No description provided.

@@ -1320,6 +1331,175 @@ protected SqlNode performUnconditionalRewrites(
return node;
}

private SqlNode performHiveUdtfRewrite(SqlNode node) {
Map<SqlSelect, UdtfInfo> selectUdtfInfo = new HashMap<>();
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add some comments?

Copy link
Author

Choose a reason for hiding this comment

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

done! Thanks for the review.

Map<SqlSelect, UdtfInfo> selectUdtfInfo = new HashMap<>();
SqlNode rewirte = performHiveUdtfRewriteInternal(node, selectUdtfInfo);
// System.out.println("before:\n" + node.toString() +" \nafter:\n" + rewirte.toString());
return node;
Copy link
Contributor

Choose a reason for hiding this comment

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

Should remove this useless comment.

Copy link
Author

Choose a reason for hiding this comment

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

Sorry to push this test code.remove now!

private SqlNode performHiveUdtfRewriteInternal(SqlNode current,
Map<SqlSelect, UdtfInfo> selectUdtfInfos) {
System.out.println("start rewrite:" + current);

Copy link
Contributor

Choose a reason for hiding this comment

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

We don't print anything in general.

Copy link
Author

Choose a reason for hiding this comment

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

Sorry for push this test code,remove now!

// join the from node with the table function.
if (udtfInfo != null && select.getFrom() != null) {
SqlBasicCall joinRight = createLateralTable(udtfInfo);
SqlNode newFrom = new SqlJoin(
Copy link
Contributor

@chunweilei chunweilei Mar 29, 2019

Choose a reason for hiding this comment

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

Maybe use SqlCall instead of SqlBasicCall is better.

Copy link
Author

Choose a reason for hiding this comment

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

well,the createLateralTable() method return a "as" node, so it must be a SqlBasicCall.

@pengzhiwei2018 pengzhiwei2018 force-pushed the dev_udtf_table_function branch 4 times, most recently from ed821b5 to 635914d Compare March 29, 2019 05:45
@BaseMessage("''{0}'' should be a table function")
ExInst<SqlValidatorException> exceptTableFunction(String name);

@BaseMessage("Only one table function allowed in select list")

Choose a reason for hiding this comment

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

is allowed

for (int i = 0; i < info.fieldNames.size(); i++) {
operands[2 + i] = info.fieldNames.get(i);
}
SqlBasicCall lateralTableAs = new SqlBasicCall(asOp, operands, SqlParserPos.ZERO);

Choose a reason for hiding this comment

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

can directly return new SqlBasicCall(asOp, operands, SqlParserPos.ZERO);

Copy link
Author

Choose a reason for hiding this comment

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

agreed! Thanks for you review!

tableFunctionInfo.node = (SqlBasicCall) udtfNode;
tableFunctionInfo.selectIndex = i;
tableFunctionInfo.fieldNames = (SqlNodeList) aliasNode;
tableFunctionInfo.tableName = "_table_function_" + nextTableFunctionNameId++;

Choose a reason for hiding this comment

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

make "_table_function_" a constant string may be better like
public static final String TABLE_FUNCTION_PREFIX = "_table_function_"
The global variables nextTableFunctionNameId once use once in this function is a little odd

Copy link
Author

Choose a reason for hiding this comment

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

I agree with you than make "table_function" as constant. But nextTableFunctionNameId should be a global variable in SqlValidatorImpl, as we may reuse the SqlValidatorImpl to validator different SqlNode. It is better to differentiate the table function in different SqlNode.

@pengzhiwei2018 pengzhiwei2018 force-pushed the dev_udtf_table_function branch 8 times, most recently from 86684b8 to 48942ba Compare April 3, 2019 09:17
@pengzhiwei2018 pengzhiwei2018 force-pushed the dev_udtf_table_function branch 2 times, most recently from 18fd91c to 486c3c2 Compare April 16, 2019 03:57
@michaelmior
Copy link
Member

@pengzhiwei2018 Could you take a look at the conflicts here?

@pengzhiwei2018 pengzhiwei2018 force-pushed the dev_udtf_table_function branch 2 times, most recently from d4704e9 to d649921 Compare May 28, 2019 02:41
@pengzhiwei2018
Copy link
Author

@pengzhiwei2018 Could you take a look at the conflicts here?

@pengzhiwei2018 Could you take a look at the conflicts here?

Thanks for start this review! Done for it!

@pengzhiwei2018 pengzhiwei2018 force-pushed the dev_udtf_table_function branch 2 times, most recently from b6a2dcc to 41ae5d1 Compare June 7, 2019 06:01
@pengzhiwei2018 pengzhiwei2018 force-pushed the dev_udtf_table_function branch from 41ae5d1 to 893d69d Compare June 17, 2019 02:58
@pengzhiwei2018 pengzhiwei2018 force-pushed the dev_udtf_table_function branch 2 times, most recently from 5a9d3d3 to 892ab01 Compare August 22, 2019 10:27
Copy link
Contributor

@chunweilei chunweilei left a comment

Choose a reason for hiding this comment

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

LGTM

@pengzhiwei2018 pengzhiwei2018 force-pushed the dev_udtf_table_function branch 2 times, most recently from a90cd98 to 2e176f0 Compare August 28, 2019 04:00
@pengzhiwei2018 pengzhiwei2018 force-pushed the dev_udtf_table_function branch 2 times, most recently from 31bba48 to beaf125 Compare October 29, 2019 07:09
final SqlIdentifier id;
SqlIdentifier id;
final List<SqlNode> ids = new ArrayList();
final Span s = span();
Copy link
Contributor

Choose a reason for hiding this comment

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

Rename ids to columnAliases and id to columnAlias ?

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for you suggestions.

aliases.addAll(aliasList);

String tableAlias = TABLE_FUNCTION_PREFIX + nextGeneratedId++;
// Expand the table function alias
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we just use the function name as the alias prefix, the TABLE_FUNCTION_PREFIX seems unnecessary.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for your suggestion. done!

scopes.put(functionCall, tableScope);
// Associate the select with the table function
selectTableFunctions.put(parentSelect, functionCall);

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm skeptical about this member selectTableFunctions, the SqlValidator never has such mapping, it handles all the states to the "Namespace" and "scope", i'm wondering if we can move this mapping into each scope, because each select has a scope. Thus, we can keep the validator code clean.

Copy link
Author

Choose a reason for hiding this comment

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

I think selectTableFunctions is the same to the whereScopes or groupByScopes in the SqlValidatorImpl.

@@ -294,6 +299,9 @@
// Flag saying if we enable the implicit type coercion.
private boolean enableTypeCoercion;

// Maping the table function and the select node.
Copy link
Contributor

Choose a reason for hiding this comment

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

typo -- "Mapping between select node and table function. "

<RPAREN> {
if (!this.conformance.allowSelectTableFunction()) {
throw SqlUtil.newContextException(getPos(),
RESOURCE.notAllowTableFunctionInSelect());
Copy link
Contributor

Choose a reason for hiding this comment

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

indent: 4 spaces

throw SqlUtil.newContextException(getPos(),
RESOURCE.notAllowTableFunctionInSelect());
}
e = SqlStdOperatorTable.AS.createCall(s.end(e), e, new SqlNodeList(ids, s.end(e)));
Copy link
Contributor

Choose a reason for hiding this comment

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

Though less than 100 characters, but shall we break the line ?

assert asIdentifier instanceof SqlIdentifier
|| (asIdentifier instanceof SqlNodeList
&& validator.getConformance().allowSelectTableFunction());

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm confused. In validateSelectList, this change has already rewriten the select items by handleTableFunctionInSelect, how is it possible that SqlAsOperator does validation here ?
Can you explain or give a test that the assertion can be failed ?

SqlIdentifier id = (SqlIdentifier) ids.get(i);
if (!id.isSimple()) {
throw validator.newValidationError(id,
RESOURCE.aliasMustBeSimpleIdentifier());
Copy link
Contributor

Choose a reason for hiding this comment

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

indent -- 4 spaces

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for your suggestion.

@@ -408,6 +409,13 @@
* false otherwise.
*/
boolean allowPluralTimeUnits();

/**
* Whether SELECT can contain a table function.
Copy link
Contributor

Choose a reason for hiding this comment

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

How about SELECT -> select list ?

SQL_SERVER_2008,

/** Conformance value that instructs Calcite to use SQL semantics
* consistent with Hive version. */
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove the word of version or specify the version.

@pengzhiwei2018 pengzhiwei2018 force-pushed the dev_udtf_table_function branch from beaf125 to 7d1a5bc Compare November 29, 2019 07:43
remove unused empty line

remove test code  & add comment

add comment

check for more than one table function in select

fix test sql

make _table_function_ as a constant

support table function without from

add comment

fix conflict

move rewrite to SqlToRelConverter

fix code style

modify comment

fix doc issue

remove used blank

add more test case

fix format

fix some format issue

fix compile failture

fix compile error

use SqlUtil#newContextException

format code
@pengzhiwei2018 pengzhiwei2018 force-pushed the dev_udtf_table_function branch from 27692d3 to ed35496 Compare November 29, 2019 10:28
@danny0405 danny0405 force-pushed the master branch 2 times, most recently from 80f411d to ca27fe9 Compare November 30, 2019 07:52
@danny0405
Copy link
Contributor

Thanks @pengzhiwei2018 for the update, maybe i can find some time to review the latest change in this week ~

@chunweilei
Copy link
Contributor

@pengzhiwei2018 , could you please resolve conflicts? I think it needs a final review and I would like to do that.

@chunweilei chunweilei added the needs-a-final-review This patch looks good from at least one PMC or committer, but still needs a specialist final review label Jul 30, 2020
chunweilei pushed a commit to chunweilei/calcite that referenced this pull request Jul 31, 2020
@danny0405 danny0405 closed this Aug 4, 2020
@danny0405
Copy link
Contributor

Close because there is already another PR.

chunweilei pushed a commit to chunweilei/calcite that referenced this pull request Aug 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-a-final-review This patch looks good from at least one PMC or committer, but still needs a specialist final review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants