Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[Cloud Spanner] Fix order of index columns #298

Merged
merged 1 commit into from
Feb 17, 2021
Merged

Conversation

naoina
Copy link
Contributor

@naoina naoina commented Feb 17, 2021

On Cloud Spanner, columns order of an index should be the same as the definition because it is important.

Probably you can reproduce it on Linux as follows using cloud-spanner-emulator and spanner-cli.

docker run --rm --net=host -d gcr.io/cloud-spanner-emulator/emulator:latest
sleep 5
curl -s localhost:9020/v1/projects/dummy/instances --data '{"instanceId":"dummy"}'
cat <<EOF | SPANNER_EMULATOR_HOST=localhost:9010 spanner-cli -p dummy -i dummy -d dummy -t
CREATE DATABASE dummy;
CREATE TABLE test (
    id INT64,
    key_a INT64,
    key_b INT64,
    key_c INT64,
) PRIMARY KEY (id);
CREATE UNIQUE INDEX test_index ON test(id, key_a, key_b, key_c);

SELECT
  c.INDEX_NAME, c.INDEX_TYPE, ARRAY_TO_STRING(ARRAY(
   SELECT COLUMN_NAME
   FROM INFORMATION_SCHEMA.INDEX_COLUMNS
   WHERE TABLE_NAME = c.TABLE_NAME AND INDEX_NAME = c.INDEX_NAME AND INDEX_TYPE = c.INDEX_TYPE AND COLUMN_ORDERING IS NOT NULL
   ORDER BY COLUMN_ORDERING ASC
 ), ", ") AS columns,
 ARRAY_TO_STRING(ARRAY(
   SELECT COLUMN_NAME
   FROM INFORMATION_SCHEMA.INDEX_COLUMNS
   WHERE TABLE_NAME = c.TABLE_NAME AND INDEX_NAME = c.INDEX_NAME AND INDEX_TYPE = c.INDEX_TYPE AND COLUMN_ORDERING IS NULL
   ORDER BY INDEX_NAME ASC
 ), ", ") AS storing_columns,
  i.PARENT_TABLE_NAME, i.IS_UNIQUE, i.IS_NULL_FILTERED, i.INDEX_STATE
FROM
  INFORMATION_SCHEMA.INDEX_COLUMNS AS c
INNER JOIN INFORMATION_SCHEMA.INDEXES AS i ON i.TABLE_NAME = c.TABLE_NAME AND i.INDEX_NAME = c.INDEX_NAME
WHERE
  c.TABLE_CATALOG = '' AND c.TABLE_SCHEMA = '' AND c.TABLE_NAME = "test"
GROUP BY c.TABLE_CATALOG, c.TABLE_SCHEMA, c.TABLE_NAME, c.INDEX_NAME, c.INDEX_TYPE, i.PARENT_TABLE_NAME, i.IS_UNIQUE, i.IS_NULL_FILTERED, i.INDEX_STATE;
EOF
18f0c531b197fca6f7a41e9756a50f25d7dbdcd169d52affdea360a4c4844756
{"name":"projects/dummy/instances/dummy/operations/_auto0","metadata":{"@type":"type.googleapis.com/google.spanner.admin.instance.v1.CreateInstanceMetadata","instance":{"name":"projects/dummy/instances/dummy","state":"READY"},"startTime":"2021-02-17T11:32:47.046560Z","endTime":"2021-02-17T11:32:47.046563Z"},"done":true,"response":{"@type":"type.googleapis.com/google.spanner.admin.instance.v1.Instance","name":"projects/dummy/instances/dummy","state":"READY"}}+-------------+-------------+-------------------------+-----------------+-------------------+-----------+------------------+-------------+
| INDEX_NAME  | INDEX_TYPE  | columns                 | storing_columns | PARENT_TABLE_NAME | IS_UNIQUE | IS_NULL_FILTERED | INDEX_STATE |
+-------------+-------------+-------------------------+-----------------+-------------------+-----------+------------------+-------------+
| PRIMARY_KEY | PRIMARY_KEY | id                      |                 |                   | true      | false            | NULL        |
| test_index  | INDEX       | key_c, key_b, key_a, id |                 |                   | true      | false            | READ_WRITE  |
+-------------+-------------+-------------------------+-----------------+-------------------+-----------+------------------+-------------+

@codecov
Copy link

codecov bot commented Feb 17, 2021

Codecov Report

Merging #298 (910568c) into master (c3cffef) will decrease coverage by 0.07%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #298      +/-   ##
==========================================
- Coverage   66.54%   66.47%   -0.08%     
==========================================
  Files          25       25              
  Lines        4101     4101              
==========================================
- Hits         2729     2726       -3     
- Misses       1089     1091       +2     
- Partials      283      284       +1     
Impacted Files Coverage Δ
datasource/datasource.go 40.14% <0.00%> (-2.12%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c3cffef...910568c. Read the comment docs.

@k1LoW k1LoW self-assigned this Feb 17, 2021
@k1LoW k1LoW added the bug Something isn't working label Feb 17, 2021
@k1LoW k1LoW removed their assignment Feb 17, 2021
@k1LoW
Copy link
Owner

k1LoW commented Feb 17, 2021

Hi @naoina! Thank you for your commit!!

Oops. This is a bug...

https://cloud.google.com/spanner/docs/information-schema#information_schemaindex_columns

Column name Type Description
👍 ORDINAL_POSITION INT64 The ordinal position of the column in the index (or primary key), starting with a value of 1. This value is NULL for non-key columns (for example, columns specified in the STORING clause of an index).
👎 COLUMN_ORDERING STRING The ordering of the column. The value is ASC or DESC for key columns, and NULL for non-key columns (for example, columns specified in the STORING clause of an index).

@k1LoW k1LoW merged commit 839a34c into k1LoW:master Feb 17, 2021
@k1LoW k1LoW changed the title Fix order of index columns [Cloud Spanner] Fix order of index columns Feb 17, 2021
k1LoW added a commit that referenced this pull request Feb 17, 2021
@k1LoW k1LoW self-assigned this Feb 27, 2021
@k1LoW k1LoW self-requested a review February 27, 2021 01:40
@k1LoW k1LoW removed their assignment Feb 27, 2021
@k1LoW k1LoW removed their request for review February 27, 2021 13:31
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants