github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/unnest.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 plan
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/container/types"
    19  	"github.com/matrixorigin/matrixone/pkg/pb/plan"
    20  	"github.com/matrixorigin/matrixone/pkg/sql/parsers/dialect"
    21  	"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
    22  )
    23  
    24  var (
    25  	defaultColDefs = []*plan.ColDef{
    26  		{
    27  			Name: "col",
    28  			Typ: plan.Type{
    29  				Id:          int32(types.T_varchar),
    30  				NotNullable: false,
    31  				Width:       types.MaxVarcharLen,
    32  			},
    33  		},
    34  		{
    35  			Name: "seq",
    36  			Typ: plan.Type{
    37  				Id:          int32(types.T_int32),
    38  				NotNullable: false,
    39  				Width:       4,
    40  			},
    41  		},
    42  		{
    43  			Name: "key",
    44  			Typ: plan.Type{
    45  				Id:          int32(types.T_varchar),
    46  				NotNullable: false,
    47  				Width:       types.MaxVarcharLen,
    48  			},
    49  		},
    50  		{
    51  			Name: "path",
    52  			Typ: plan.Type{
    53  				Id:          int32(types.T_varchar),
    54  				NotNullable: false,
    55  				Width:       types.MaxVarcharLen,
    56  			},
    57  		},
    58  		{
    59  			Name: "index",
    60  			Typ: plan.Type{
    61  				Id:          int32(types.T_int32),
    62  				NotNullable: false,
    63  				Width:       4,
    64  			},
    65  		},
    66  		{
    67  			Name: "value",
    68  			Typ: plan.Type{
    69  				Id:          int32(types.T_json),
    70  				NotNullable: false,
    71  			},
    72  		},
    73  		{
    74  			Name: "this",
    75  			Typ: plan.Type{
    76  				Id:          int32(types.T_json),
    77  				NotNullable: false,
    78  			},
    79  		},
    80  	}
    81  )
    82  
    83  func _dupType(typ *plan.Type) *plan.Type {
    84  	return &plan.Type{
    85  		Id:          typ.Id,
    86  		NotNullable: typ.NotNullable,
    87  		Width:       typ.Width,
    88  		Scale:       typ.Scale,
    89  	}
    90  }
    91  
    92  func _dupColDef(src *plan.ColDef) *plan.ColDef {
    93  	return &plan.ColDef{
    94  		Name: src.Name,
    95  		Typ:  *_dupType(&src.Typ),
    96  	}
    97  }
    98  
    99  func _getDefaultColDefs() []*plan.ColDef {
   100  	ret := make([]*plan.ColDef, 0, len(defaultColDefs))
   101  	for _, v := range defaultColDefs {
   102  		ret = append(ret, _dupColDef(v))
   103  	}
   104  	return ret
   105  }
   106  
   107  func (builder *QueryBuilder) buildUnnest(tbl *tree.TableFunction, ctx *BindContext, exprs []*plan.Expr, childId int32) (int32, error) {
   108  	colDefs := _getDefaultColDefs()
   109  	colName := findColName(tbl.Func)
   110  	node := &plan.Node{
   111  		NodeType: plan.Node_FUNCTION_SCAN,
   112  		Stats:    &plan.Stats{},
   113  		TableDef: &plan.TableDef{
   114  			TableType: "func_table", //test if ok
   115  			//Name:               tbl.String(),
   116  			TblFunc: &plan.TableFunction{
   117  				Name:  "unnest",
   118  				Param: []byte(colName),
   119  			},
   120  			Cols: colDefs,
   121  		},
   122  		BindingTags:     []int32{builder.genNewTag()},
   123  		TblFuncExprList: exprs,
   124  		Children:        []int32{childId},
   125  	}
   126  	return builder.appendNode(node, ctx), nil
   127  }
   128  
   129  func findColName(fn *tree.FuncExpr) string {
   130  	if _, ok := fn.Exprs[0].(*tree.NumVal); ok {
   131  		return ""
   132  	}
   133  	return tree.String(fn.Exprs[0], dialect.MYSQL)
   134  }