-
Notifications
You must be signed in to change notification settings - Fork 6
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
A query in scalar context should use the first column #75
Comments
Could we instead use the label for the lhs of in() to select the column with the same label if the rhs doesn't have a select? e.g.
I'm hesitant to pick the first column since this makes the interpretation of a query depend upon the catalog: tables with the same name and same columns may have different interpretation if order of columns somehow differs. Also, an implicit positional approach may lead to confusion. For example, with Alternatively, perhaps we might use the primary key column, should the PK have exactly one field. Or perhaps we use a tuple in this case? If there is no primary key, it's an error. |
My primary motivating example is concept matching: filter(condition_concept_id == SNOMED("Essential hypertension"))
filter(condition_concept_id in SNOMED("Essential hypertension").with_descendands()) If you interpret The first column = the primary key is a very common conventions, at least for ORM-generated schemas. This interpretation will give exactly what you need in the majority of cases, and you can always use explicit |
Consider a query used in a scalar context, such as
SELECT
clause,EXISTS
expression,IN
expression, or an argument of a scalar function.For convenience, assume a query that returns one row, e.g.,
@funsql from(person).limit(1)
. It does return multiple columns:person_id
,gender_concept_id
, etc. How should this query be interpreted when used in a scalar context, such as:This is a challenge because in a scalar context, a query must return exactly one column. Currently, the returned column is
NULL
unless the column is explicitly specified with aselect()
combinator. Thus,select(from(person).limit(1))
returnsNULL
, butselect(from(person).limit(1).select(person_id))
returns the value ofperson_id
.This interpretation allows us to accept any query in a scalar context, which is particularly useful for
EXISTS
. However, it may cause confusion when the query is used as an argument ofIN
or a scalar function.There is a better interpretation: A query used in a scalar context should return its first column.
This interpretation does not change the semantics of a query with an explicit
select()
. For queries withoutselect()
, it would pick the first column of a table, which is typically its primary key. This allows us to write, for exampleThe text was updated successfully, but these errors were encountered: