Detect reading from non-indexed JSON documents and store it in a LazyJSONDocument instead of an IndexedJSONDocument.

This commit is contained in:
Nick Tobey
2024-07-18 12:54:08 -07:00
parent 71f6737ea7
commit 0888820e91
14 changed files with 49 additions and 0 deletions

View File

@@ -304,6 +304,10 @@ func (b *JSONDoc) ToIndexedJSONDocument(ctx context.Context) (sql.JSONWrapper, e
if err != nil {
return nil, err
}
if root.level > 0 && root.keys.IsEmpty() {
// We're reading a non-indexed multi-chunk document written by an older version of Dolt.
return b.ToLazyJSONDocument(ctx)
}
return NewIndexedJsonDocument(ctx, root, b.ns), nil
}

View File

@@ -0,0 +1 @@
{}

View File

@@ -0,0 +1 @@
5:__DOLT__:9meojkibso7athar08m9btgeohtavtkh:k6bgrd1f6142qmf4dh6gucgmcp1djbm4:00000000000000000000000000000000:vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv:3826

View File

@@ -0,0 +1,6 @@
{
"head": "refs/heads/main",
"remotes": {},
"backups": {},
"branches": {}
}

View File

@@ -0,0 +1 @@
{}

View File

@@ -0,0 +1 @@
5:__DOLT__:0ced3kicdirbi5vrl72i5as7dtis5elq:1dd8gihhhhnmqc4s142f5pshrvgcndji:00000000000000000000000000000000:vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv:17

View File

@@ -0,0 +1,6 @@
{
"head": "refs/heads/main",
"remotes": {},
"backups": {},
"branches": {}
}

View File

@@ -0,0 +1,29 @@
#!/usr/bin/env bats
load $BATS_TEST_DIRNAME/helper/common.bash
# This BATS test attempts to read JSON documents that were written prior to v1.40
# This function was used to create the dolt repo used for this test. It is not run during testing.
create_repo() {
dolt init
dolt sql -q "CREATE TABLE jsonTable(pk int primary key, j json);"
dolt sql -q 'INSERT INTO jsonTable( with recursive cte (pk, j) as ( select 0, JSON_OBJECT("K", "V") union all select pk+1, JSON_INSERT(j, CONCAT("$.k", pk), j) from cte where pk < 20 ) select * from cte );'
dolt add .
dolt commit -m "create json table"
}
setup() {
cp -r $BATS_TEST_DIRNAME/json-oldformat-repo/ $BATS_TMPDIR/dolt-repo-$$
cd $BATS_TMPDIR/dolt-repo-$$
}
@test "json-oldformat: verify queries" {
run dolt sql -q "SELECT pk, JSON_EXTRACT(j, '$.k10.k5.k3.k2') FROM jsonTable WHERE pk = 12;"
[[ "$output" =~ '{"K": "V", "k0": {"K": "V"}, "k1": {"K": "V", "k0": {"K": "V"}}}' ]] || false
run dolt sql -q "SELECT pk, JSON_VALUE(j, '$.k8.k6.k4.k1') FROM jsonTable WHERE pk = 12;"
[[ "$output" =~ '{"K": "V", "k0": {"K": "V"}}' ]] || false
run dolt sql -q "SELECT pk, JSON_EXTRACT(JSON_INSERT(j, '$.k9.k6.k1.TESTKEY', 'TESTVALUE'), '$.k9.k6.k1') FROM jsonTable WHERE pk = 12;"
[[ "$output" =~ '{"K": "V", "k0": {"K": "V"}, "TESTKEY": "TESTVALUE"}' ]] || false
}