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

Support index acess for Row Type #7640

Closed
wenleix opened this issue Mar 22, 2017 · 10 comments
Closed

Support index acess for Row Type #7640

wenleix opened this issue Mar 22, 2017 · 10 comments
Assignees
Labels

Comments

@wenleix
Copy link
Contributor

wenleix commented Mar 22, 2017

Today we only support access by field for Row Type. For row types without field types, we have to first cast it into a Row with field names:

presto > select CAST(v AS ROW(f1 BIGINT, f2 VARCHAR)).f1 
from (VALUES ROW(ROW(1, 'a')), ROW(ROW(2, 'b'))) AS t(v);

 _col0
-------
     1
     2
(2 rows)

It will be nice to support index access, i.e. v[1] will return the first field of the row.

@wenleix wenleix self-assigned this Mar 22, 2017
@geraint0923
Copy link
Contributor

Good idea! But I think we should only allow constant index, otherwise it will be hard to infer the field type.

@wenleix
Copy link
Contributor Author

wenleix commented Mar 29, 2017

Related PR for . operator:

#3636
#2378

@wenleix
Copy link
Contributor Author

wenleix commented Mar 29, 2017

Looking at how PostgreSQL and Oracle solve the problem. In Postgre SQL, the row constructor will construct an record, while the composite type stored in table has its own type name.

wxie=# select pg_typeof(ROW('x',1));                                                     

pg_typeof 
-----------
 record

If I understand correctly, the only way to extract specific field from an unnamed record is to cast it into a defined composite type , e.g. http://stackoverflow.com/questions/4700661/how-can-i-extract-the-values-from-a-record-as-individual-columns-in-postgresql :

CREATE TYPE my_type AS (
   f1 VARCHAR,
   f2 INTEGER
);

WITH tmp AS
(
	SELECT ROW('x', 1) x
	UNION ALL
	SELECT ROW('y', 2) x
	UNION ALL
	SELECT ROW('z', 3) x
)
SELECT (x::text::my_type).f2 FROM tmp

Would like to hear any comments/suggestions from people who are more familiar with PostgreSQL :).

@wenleix
Copy link
Contributor Author

wenleix commented Apr 8, 2017

In Oracle, the RECORD type always have FieldName: https://docs.oracle.com/cd/B28359_01/appdev.111/b28370/record_definition.htm#LNPLS01338, https://docs.oracle.com/cd/B28359_01/appdev.111/b28370/collections.htm#BABHHJGA.

Previous SQL engine doesn't have the same flexibility as Presto to get unnamed RowType. As such, accessing field in unnamed RowType is not a pain point for them.

We plan to add constant subscript access support for RowType in Presto

@electrum
Copy link
Contributor

electrum commented Apr 8, 2017 via email

@wenleix
Copy link
Contributor Author

wenleix commented Apr 8, 2017

@electrum We probably want to use something that cannot be a legal field name then :). What about ."$field1", ."$field2" then? (Similar to the hidden bucket column name).

@electrum
Copy link
Contributor

electrum commented Apr 8, 2017 via email

@martint
Copy link
Contributor

martint commented Apr 8, 2017

That's not true. They type of a "relation" is multiset(row(...)). If that relation is a subquery, some fields may be unnamed (e.g., if they are the result of some complex expression and don't have an alias)

@martint
Copy link
Contributor

martint commented Apr 8, 2017

I think we should hold off adding this feature for now. I really don't like the idea of adding synthetic fields with some arbitrary name, and the subscript operator version has enough edge cases and potential complications that we need to think about it very carefully.

@stale
Copy link

stale bot commented Apr 9, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants