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

Add typename in header and add DESCRIBE statement #191

Merged
merged 21 commits into from
Oct 17, 2024

Conversation

apstndb
Copy link
Collaborator

@apstndb apstndb commented Sep 27, 2024

This PR implements both of typename in header and DESCRIBE statement.
Their implementation have some common components..

Now, column names in verbose mode are multi line. It contains column names with column types.
Header will be printed even if there is no row in the result.

$ ./spanner-cli --project ${SPANNER_PROJECT} --instance ${SPANNER_INSTANCE} --database ${SPANNER_DATABASE} -t -v
Connected.
spanner> SELECT
      -> 1 AS I,
      -> new examples.spanner.music.SingerInfo{} AS p,
      -> [new examples.spanner.music.SingerInfo{}] AS arr_p,
      -> CAST("ROCK" AS examples.spanner.music.Genre) AS e,
      -> [CAST("ROCK" AS examples.spanner.music.Genre)] AS arr_e,
      -> ARRAY(SELECT AS STRUCT 1 AS i) AS s
      -> LIMIT 0;
+-------+-------+--------------+------+-------------+---------------+
| I     | p     | arr_p        | e    | arr_e       | s             |
| INT64 | PROTO | ARRAY<PROTO> | ENUM | ARRAY<ENUM> | ARRAY<STRUCT> |
+-------+-------+--------------+------+-------------+---------------+
+-------+-------+--------------+------+-------------+---------------+
Empty set (0.37 msecs)
timestamp:            2024-10-03T00:37:40.476382+09:00
cpu time:             0.32 msecs
rows scanned:         0 rows
deleted rows scanned: 0 rows
optimizer version:    7
optimizer statistics: auto_20241001_01_32_11UTC

Column type in header also supports DMLs with THEN RETURN.

spanner> DELETE DMLTest WHERE TRUE THEN RETURN WITH ACTION *;
+-------+-------+--------+
| PK    | Col   | ACTION |
| INT64 | INT64 | STRING |
+-------+-------+--------+
| 1     | 2     | DELETE |
| 3     | 4     | DELETE |
| 4     | 5     | DELETE |
+-------+-------+--------+
Query OK, 3 rows affected (0.56 sec)
timestamp:      2024-10-03T01:45:19.716519+09:00
mutation_count: 6

There is no change in no --verbose mode.

$ ./spanner-cli --project ${SPANNER_PROJECT} --instance ${SPANNER_INSTANCE} --database ${SPANNER_DATABASE} -t
Connected.
spanner> SELECT 1 AS a LIMIT 0;
Empty set (1.87 msecs)

spanner> SELECT 1 AS a;
+---+
| a |
+---+
| 1 |
+---+
1 rows in set (2.55 msecs)

Now, DESCRIBE statement prints the shape of result set without execution, and DESC is not valid statement.

spanner> DESCRIBE
      -> SELECT
      -> 1 AS I,
      -> new examples.spanner.music.SingerInfo{} AS p,
      -> [new examples.spanner.music.SingerInfo{}] AS arr_p,
      -> CAST("ROCK" AS examples.spanner.music.Genre) AS e,
      -> [CAST("ROCK" AS examples.spanner.music.Genre)] AS arr_e,
      -> ARRAY(SELECT AS STRUCT 1 AS i) AS s;
+-------------+------------------------------------------+
| Column_Name | Column_Type                              |
+-------------+------------------------------------------+
| I           | INT64                                    |
| p           | examples.spanner.music.SingerInfo        |
| arr_p       | ARRAY<examples.spanner.music.SingerInfo> |
| e           | examples.spanner.music.Genre             |
| arr_e       | ARRAY<examples.spanner.music.Genre>      |
| s           | ARRAY<STRUCT<i INT64>>                   |
+-------------+------------------------------------------+
1 rows in set (0.17 sec)

DESCRIBE statement also supports DMLs with THEN RETURN.

spanner> DESCRIBE DELETE Singers WHERE TRUE THEN RETURN WITH ACTION *;
+-------------+-------------+
| Column_Name | Column_Type |
+-------------+-------------+
| SingerId    | INT64       |
| FirstName   | STRING      |
| LastName    | STRING      |
| SingerInfo  | BYTES       |
| BirthDate   | DATE        |
| ACTION      | STRING      |
+-------------+-------------+
Query OK, 0 rows affected (0.62 sec)

DESCRIBE statement is fully compatible with Cloud Spanner Emulator.

Note: This PR unifies existing ExplainDmlStatement into ExplainStatement.

resolve #190

@apstndb apstndb changed the title Add typename into header Add typename into header and DESCRIBE statement Sep 27, 2024
@apstndb apstndb changed the title Add typename into header and DESCRIBE statement Add typename into header and add DESCRIBE statement Sep 28, 2024
@apstndb apstndb changed the title Add typename into header and add DESCRIBE statement Add typename in header and add DESCRIBE statement Sep 28, 2024
@apstndb apstndb marked this pull request as ready for review October 2, 2024 15:51
@apstndb
Copy link
Collaborator Author

apstndb commented Oct 3, 2024

It seems that there is a bug when executing DML without THEN RETURN.

=== RUN   TestDml
    integration_test.go:218: diff:   &main.Result{
          	... // 3 ignored and 7 identical fields
          	RowType: Inverse(protocmp.Transform, protocmp.Message{
        + 		"@invalid": bool(true),
          		"@type":    s"google.spanner.v1.StructType",
          	}),
          }

@apstndb
Copy link
Collaborator Author

apstndb commented Oct 3, 2024

My thought:

  • This header behavior can be generalized in another statements like SHOW TABLES.
  • Column name is always available as metadata.rowType.fields[].name, so we can eliminate the use of (*Row).ColumnNames() in parseQueryResult(). IMO, (*Row).ColumnNames() method is naive because it is not available when the result is empty.

@apstndb
Copy link
Collaborator Author

apstndb commented Oct 3, 2024

Now Result.ColumnNames is populated in many statements even if there is no result, but it won't be printed even if --verbose.
Header behavior has changed only in SelectStatement and DmlStatement.

@apstndb apstndb requested a review from yfuruyama October 4, 2024 11:39
@apstndb
Copy link
Collaborator Author

apstndb commented Oct 6, 2024

FYI: FormatTypeVerbose and FormatTypeSimple can use apstndb/spantype package if needed.

@yfuruyama
Copy link
Collaborator

Thank you for filing a PR. Let me review this when I have chance (ETA: end of this week).

cli.go Show resolved Hide resolved
statement.go Outdated Show resolved Hide resolved
@yfuruyama
Copy link
Collaborator

@apstndb Just to confirm, is it ok to merge this PR?

@apstndb
Copy link
Collaborator Author

apstndb commented Oct 17, 2024

OK!

@yfuruyama
Copy link
Collaborator

Thanks!

@yfuruyama yfuruyama merged commit 73f1bc3 into cloudspannerecosystem:master Oct 17, 2024
2 checks passed
@apstndb apstndb deleted the typename-in-header branch October 17, 2024 14:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Print result type information inspired by DuckDB DESCRIBE
2 participants