Skip to content

feat(function): tupleElement with default value - #38989

Merged
CurtizJ merged 11 commits into
ClickHouse:masterfrom
lokax:tuple_elem
Jul 21, 2022
Merged

CurtizJ merged 11 commits into
ClickHouse:masterfrom
lokax:tuple_elem

Conversation

@lokax

@lokax lokax commented Jul 8, 2022 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • New Feature

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):

Add the 3rd parameter to the tupleElement function and return it if tuple doesn't have a member. Only works if the 2nd parameter is of type String.
Closes #38872

Information about CI checks: https://clickhouse.com/docs/en/development/continuous-integration/

@robot-clickhouse robot-clickhouse added the pr-feature Pull request with new product feature label Jul 8, 2022
@CurtizJ CurtizJ self-assigned this Jul 8, 2022
@CurtizJ CurtizJ added the can be tested Allows running workflows for external contributors label Jul 8, 2022
Comment thread src/DataTypes/DataTypeTuple.h Outdated
const Strings & getElementNames() const { return names; }

size_t getPositionByName(const String & name) const;
bool getPositionByName(const String & name, size_t & index) const;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd prefer to name this method tryGetPositionByName and make it return std::optional<size>. It's more a c++ way to do this and it's more consistent with other code in our codebase.

Comment thread src/Functions/tupleElement.cpp Outdated

private:
size_t getElementNum(const ColumnPtr & index_column, const DataTypeTuple & tuple) const
bool getElementNum(const ColumnPtr & index_column, const DataTypeTuple & tuple, size_t & index, const size_t argument_size) const

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The same as with getPositionByName.

Comment thread src/Functions/tupleElement.cpp Outdated
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override
{
return {1};
return {1, 2};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why not allow to use regular columns as default arguments? It may be useful in some cases and doesn't require any changes in implementation.

Comment thread src/Functions/tupleElement.cpp Outdated
Comment on lines +122 to +151
size_t index = 0;
if (!getElementNum(arguments[1].column, *tuple_type_concrete, index, arguments.size()))
{
return arguments[2].column;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In case of Array(Tuple)) it may work incorrectly. In that case we should return array with same sizes as other elements have, but now arbitrary value of any type may be returned.

So, in case of Array(Tuple)) we have to check that default value has Array type with the same number of dimension as the original column and check that it has the same offsets in all rows as the original column.

@lokax
lokax force-pushed the tuple_elem branch 5 times, most recently from 03d791c to 2405525 Compare July 13, 2022 14:23
@lokax

lokax commented Jul 14, 2022

Copy link
Copy Markdown
Contributor Author

@CurtizJ
Should I check the offset of the array for each dimension?
for example:
Type: Array(Array(Array(Tuple(UInt8))))
Data: [[[tuple(1), tuple(2), tuple(3)], [tuple(4)]], [[tuple(5)]]] AS b
tupleElement(b, 1) --> [[[1,2,3],[4]],[[5]]] AS c
Offset: [2] --> [2, 3] --> [3, 4, 5]

The same layout as c can be produced if the array offsets of each dimension of the default value argument match the Offset([2] --> [2, 3] --> [3, 4, 5]).

Dimension         Layout
3:                 [[],[]] (offset: [2])
2:                 [[[],[]],[[]]] (offset: [2, 3])
1:                 [[[x, x, x],[x]],[[x]]]  (offset: [3, 4, 5])

@lokax
lokax force-pushed the tuple_elem branch 2 times, most recently from ba603b2 to b720d77 Compare July 15, 2022 05:42
@CurtizJ

CurtizJ commented Jul 15, 2022

Copy link
Copy Markdown
Member

Yes, you need to check offsets for each dimension.

@CurtizJ CurtizJ left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In general LGTM. Please, fix style issue and I'll merge PR.

Comment thread src/Functions/tupleElement.cpp Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

By our code style else and else if should be placed on the next line.

@lokax
lokax force-pushed the tuple_elem branch 4 times, most recently from ad4dcc5 to 4a276e6 Compare July 20, 2022 04:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-feature Pull request with new product feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tupleElement with default value

3 participants