partiql / partiql-spec

The language specification of PartiQL.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Clarify `SELECT *` behavior

alancai98 opened this issue · comments

Some prior context covered in this conformance test PR: partiql/partiql-tests#43.

There's some ambiguity regarding how SELECT * should behave in PartiQL. Section 6.3.2 of the PartiQL spec goes over the semantics of select items with * (e.g. SELECT x.*) but not how SELECT * behaves. This came about from running certain queries in partiql-lang-kotlin that I couldn't find an explanation for in the spec.

For example, it's unclear why the following two queries yield different results:

Given an input env:

someOrderedTable: [
    { 'a': 0, 'b': 0 },
    { 'a': 1,  'b': 1 }
]
PartiQL> SELECT * FROM someOrderedTable[0].a AS x;
==='
<<
  {
    '_1': 0
  }
>>
---
OK!
PartiQL> SELECT * FROM <<{'x': 0}>>;
==='
<<
  {
    'x': 0
  }
>>
---
OK!

Each of the above queries should yield the same binding tuples from the FROM source (i.e. << < x : 0 > >>) but the results differ with SELECT *.

Similarly why do the following yield different results:

PartiQL> SELECT * FROM someOrderedTable AS x AT y
   |
==='
<<
  {
    'a': 0,
    'b': 0,
    'y': 0
  },
  {
    'a': 1,
    'b': 1,
    'y': 1
  }
>>
---
OK!
PartiQL> SELECT x.*,y.* FROM someOrderedTable AS x AT y
   |
==='
<<
  {
    'a': 0,
    'b': 0,
    '_3': 0
  },
  {
    'a': 1,
    'b': 1,
    '_3': 1
  }
>>
---
OK!

It's unclear whether this is a bug in partiql-lang-kotlin or the intended behavior. Either way, the query behavior should be better defined in the PartiQL spec.

Following this issue's resolution, may need to cut an issue + bug fix to partiql-lang-kotlin.

After looking more into SELECT * and the spec, there are some corrections to the examples I included above. Though, the issue of SELECT * specification still remains.

In the first statement, SELECT * FROM someOrderedTable[0].a AS x, the FROM clause outputs the binding tuple << < 'x': 0 > >>. If rewriting SELECT * in terms of the FROM source variable (i.e. SELECT x.*), the resulting binding tuple would be << { '_1': 0 } >>.

In the second statement, SELECT * FROM <<{'x': 0}>>, the FROM clause outputs the binding tuple << < _1: {'x': 0} > >> (incorrect in original issue text). The SELECT * equivalence would then be SELECT _1.*, which results in <<{'x': 0}>>

Thus those two statements having different behavior is correct.


The second set of queries in the issue text have different results as expected if following the partiql-lang-kotlin SELECT * rewrite (rewrite code here: SelectStarVisitorTransform). The output FROM source binding tuples would be

<<
    < x: { 'a': 0, 'b': 0 }, y: 0 >,
    < x: { 'a': 1,  'b': 1 }, y: 1 >
>>

The SELECT * rewrite for FROM someOrderedTable AS x AT y would actually be SELECT x.*, y and not SELECT x.*, y.*. Using the correct rewrite for SELECT *, we get the same result as SELECT *.


To summarize, partiql-lang-kotlin's behavior is consistent and consistent with how it performs the SELECT * rewrite. There's still some underspecified parts of SELECT * associated with the ordering of the tuples and whether certain clauses' variables should be included in the SELECT * output, which I'll cut a separate issue to track.

Present spec discussion items for SELECT * moved to partiql/partiql-lang#19