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

spannertest: support for aliases in SELECT statements #2463

Closed
jrmeinride opened this issue Jun 16, 2020 · 4 comments · Fixed by #2931
Closed

spannertest: support for aliases in SELECT statements #2463

jrmeinride opened this issue Jun 16, 2020 · 4 comments · Fixed by #2931
Assignees
Labels
api: spanner Issues related to the Spanner API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@jrmeinride
Copy link

Is your feature request related to a problem? Please describe.
To make our queries shorter and easier to read we use aliases in our queries. We want to unit test code that has such queries.

Example:

SELECT t1.* 
FROM table1 AS t1
	LEFT OUTER HASH JOIN table2 AS t2 
		ON t1.id = t2.table1_id

Granted, at this point there is no support for JOIN in SELECT statements, but we are hoping that this will be added, so that we can properly unit test our code.

Describe the solution you'd like
We would like to se aliases in SELECT statements.

Describe alternatives you've considered
The only viable option would be to spin up a new table in one of our projects, but that is out of scope for unit tests.

Additional context
Ẃe have another feature request for JOIN in SELECT statements, that is by far a more important feature. This would be a further quality of life improvement, we can write queries without aliases, though it would be nice to have them.

@jrmeinride jrmeinride added the triage me I really want to be triaged. label Jun 16, 2020
@tbpg tbpg added api: spanner Issues related to the Spanner API. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. and removed triage me I really want to be triaged. labels Jun 16, 2020
@dsymonds
Copy link
Contributor

dsymonds commented Jun 18, 2020

There's already support for aliases in SELECT expression lists and FROM clauses, I believe. Do you have an example query not using JOIN that has an alias that doesn't work?

@skuruppu skuruppu assigned dsymonds and unassigned skuruppu Jun 18, 2020
@jrmeinride
Copy link
Author

There's already support for aliases in SELECT expression lists and FROM clauses, I believe. Do you have an example query not using JOIN that has an alias that doesn't work?

Using the 1.6.0 version of spanner we get this error message when I run a query:

SQL for table:

CREATE TABLE something (
    id STRING(100) NOT NULL,
    created_time TIMESTAMP OPTIONS (allow_commit_timestamp=true)
) PRIMARY KEY(id)

Test code:

func Test_Spanner(t *testing.T) {
	fx := newFixture(t)
	defer fx.Fixture.Teardown()
	stmt := spanner.Statement{
		SQL: `SELECT s.* 
		FROM something as s
		WHERE s.id = @some_id 
		ORDER BY s.created_time ASC
		LIMIT 1`,
		Params: map[string]interface{}{
			"some_id": "1",
		},
	}
	rowIterator := fx.SpannerClient.Single().Query(context.Background(), stmt)
	defer rowIterator.Stop()
	_, err := rowIterator.Next()
	if err != nil {
		if err == iterator.Done {
			t.Log("done")
		} else {
			t.Log(err)
			t.FailNow()
		}
	}
}

service_test.go:nn: spanner: code = "InvalidArgument", desc = "bad query: unexpected trailing query contents "s.* \n\t\tFROM something as s\n\t\tWHERE s.id = @some_id \n\t\tORDER BY s.created_time ASC\n\t\tLIMIT 1""

@dsymonds
Copy link
Contributor

The missing feature there is the s.*, s.id and s.created_time parts of the query rather than the alias itself, but yes, this is a known TODO. (The distinction here is that SELECT * FROM something AS s WHERE id = @some_id ORDER BY created_time ASC LIMIT 1 should work fine, though that s alias is not usable).

@jrmeinride
Copy link
Author

jrmeinride commented Jun 18, 2020

I ran a test for that case, and got an error. I tested with and without the AS keyword, neither worked for me :-(

func Test_Spanner(t *testing.T) {
	fx := newFixture(t)
	defer fx.Fixture.Teardown()
	stmt := spanner.Statement{
		SQL: `SELECT * 
		FROM something AS s
		WHERE s.id = @some_id 
		ORDER BY s.created_time ASC
		LIMIT 1`,
		Params: map[string]interface{}{
			"some_id": "1",
		},
	}
	rowIterator := fx.SpannerClient.Single().Query(context.Background(), stmt)
	defer rowIterator.Stop()
	_, err := rowIterator.Next()
	if err != nil {
		if err == iterator.Done {
			t.Log("done")
		} else {
			t.Log(err)
			t.FailNow()
		}
	}
}

Output:

service_test.go:nn: spanner: code = "InvalidArgument", desc = "bad query: unexpected trailing query contents \"s.id = @some_id \\n\\t\\tORDER BY s.created_time ASC\\n\\t\\tLIMIT 1\""

@dsymonds dsymonds added the priority: p2 Moderately-important priority. Fix may not be included in next release. label Sep 26, 2020
gcf-merge-on-green bot pushed a commit that referenced this issue Sep 28, 2020
These are not properly documented, so this only attempts to support a
limited but widely used subset, which will be sufficient for more fully
supporting aliases and joins.

Updates #2462.
Updates #2463.
Updates #2850.
gcf-merge-on-green bot pushed a commit that referenced this issue Sep 29, 2020
This adds support for aliases in SELECT statements, and in particular
table aliases that can then be referenced from other places in the query
evaluation.

Fixes #2463.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: spanner Issues related to the Spanner API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants