github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/multi/json_extract_test.go (about)

     1  // Copyright 2022 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package multi
    16  
    17  import (
    18  	"fmt"
    19  	"github.com/matrixorigin/matrixone/pkg/container/types"
    20  	"github.com/matrixorigin/matrixone/pkg/testutil"
    21  	"github.com/stretchr/testify/require"
    22  	"testing"
    23  )
    24  
    25  var (
    26  	kases = []struct {
    27  		index        int
    28  		json         string
    29  		path         string
    30  		want         string
    31  		pathNullList []bool
    32  	}{
    33  		{
    34  			index: 0,
    35  			json:  `{"a":1,"b":2,"c":3}`,
    36  			path:  `$.a`,
    37  			want:  `1`,
    38  		},
    39  		{
    40  			index: 1,
    41  			json:  `{"a":1,"b":2,"c":3}`,
    42  			path:  `$.b`,
    43  			want:  `2`,
    44  		},
    45  		{
    46  			index: 2,
    47  			json:  `{"a":{"q":[1,2,3]}}`,
    48  			path:  `$.a.q[1]`,
    49  			want:  `2`,
    50  		},
    51  		{
    52  			index: 3,
    53  			json:  `[{"a":1,"b":2,"c":3},{"a":4,"b":5,"c":6}]`,
    54  			path:  `$[1].a`,
    55  			want:  `4`,
    56  		},
    57  		{
    58  			index: 4,
    59  			json:  `{"a":{"q":[{"a":1},{"a":2},{"a":3}]}}`,
    60  			path:  `$.a.q[1]`,
    61  			want:  `{"a":2}`,
    62  		},
    63  		{
    64  			index: 5,
    65  			json:  `{"a":{"q":[{"a":1},{"a":2},{"a":3}]}}`,
    66  			path:  `$.a.q`,
    67  			want:  `[{"a":1},{"a":2},{"a":3}]`,
    68  		},
    69  		{
    70  			index: 6,
    71  			json:  `[1,2,3]`,
    72  			path:  "$[*]",
    73  			want:  "[1,2,3]",
    74  		},
    75  		{
    76  			index: 7,
    77  			json:  `{"a":[1,2,3,{"b":4}]}`,
    78  			path:  "$.a[3].b",
    79  			want:  "4",
    80  		},
    81  		{
    82  			index: 8,
    83  			json:  `{"a":[1,2,3,{"b":4}]}`,
    84  			path:  "$.a[3].c",
    85  			want:  "null",
    86  		},
    87  		{
    88  			index: 9,
    89  			json:  `{"a":[1,2,3,{"b":4}],"c":5}`,
    90  			path:  "$.*",
    91  			want:  `[[1,2,3,{"b":4}],5]`,
    92  		},
    93  		{
    94  			index: 10,
    95  			json:  `{"a":[1,2,3,{"a":4}]}`,
    96  			path:  "$**.a",
    97  			want:  `[[1,2,3,{"a":4}],4]`,
    98  		},
    99  		{
   100  			index: 11,
   101  			json:  `{"a":[1,2,3,{"a":4}]}`,
   102  			path:  "$.a[*].a",
   103  			want:  `4`,
   104  		},
   105  		{
   106  			index:        12,
   107  			json:         `{"a":[1,2,3,{"a":4}]}`,
   108  			pathNullList: []bool{true},
   109  			want:         "null",
   110  		},
   111  	}
   112  )
   113  
   114  func TestJsonExtract(t *testing.T) {
   115  	proc := testutil.NewProc()
   116  	for _, kase := range kases {
   117  		inputs := []testutil.FunctionTestInput{
   118  			testutil.NewFunctionTestInput(types.T_varchar.ToType(), []string{kase.json}, nil),
   119  			testutil.NewFunctionTestInput(types.T_varchar.ToType(), []string{kase.path}, kase.pathNullList),
   120  		}
   121  		want := make([]string, 1)
   122  		if kase.want != "null" {
   123  			bj, _ := types.ParseStringToByteJson(kase.want)
   124  			dt, _ := bj.Marshal()
   125  			want[0] = string(dt)
   126  		}
   127  		expect := testutil.NewFunctionTestResult(types.T_varchar.ToType(), false, want, nil)
   128  		kaseNow := testutil.NewFunctionTestCase(proc,
   129  			inputs, expect, JsonExtract)
   130  		s, info := kaseNow.Run()
   131  		require.True(t, s, fmt.Sprintf("case %d, err info is '%s'", kase.index, info))
   132  	}
   133  }