Пример #1
0
// Initializes the column structures.
func (n *scanNode) initDescDefaults(scanVisibility scanVisibility) {
	n.index = &n.desc.PrimaryIndex
	n.cols = make([]sqlbase.ColumnDescriptor, 0, len(n.desc.Columns)+len(n.desc.Mutations))
	switch scanVisibility {
	case publicColumns:
		n.cols = append(n.cols, n.desc.Columns...)
	case publicAndNonPublicColumns:
		n.cols = append(n.cols, n.desc.Columns...)
		for _, mutation := range n.desc.Mutations {
			if c := mutation.GetColumn(); c != nil {
				n.cols = append(n.cols, *c)
			}
		}
	}
	n.resultColumns = makeResultColumns(n.cols)
	n.colIdxMap = make(map[sqlbase.ColumnID]int, len(n.cols))
	for i, c := range n.cols {
		n.colIdxMap[c.ID] = i
	}
	n.valNeededForCol = make([]bool, len(n.cols))
	for i := range n.cols {
		n.valNeededForCol[i] = true
	}
	n.row = make([]parser.Datum, len(n.cols))
	n.filterVars = parser.MakeIndexedVarHelper(n, len(n.cols))
}
Пример #2
0
func (eh *exprHelper) init(
	expr Expression, types []sqlbase.ColumnType_Kind, evalCtx *parser.EvalContext,
) error {
	if expr.Expr == "" {
		return nil
	}
	eh.types = types
	eh.evalCtx = evalCtx
	eh.vars = parser.MakeIndexedVarHelper(eh, len(types))
	var err error
	eh.expr, err = processExpression(expr, &eh.vars)
	return err
}
Пример #3
0
// Initializes the column structures.
func (n *scanNode) initDescDefaults() {
	n.index = &n.desc.PrimaryIndex
	cols := n.desc.Columns
	n.resultColumns = makeResultColumns(cols)
	n.colIdxMap = make(map[sqlbase.ColumnID]int, len(cols))
	for i, c := range cols {
		n.colIdxMap[c.ID] = i
	}
	n.valNeededForCol = make([]bool, len(cols))
	for i := range cols {
		n.valNeededForCol[i] = true
	}
	n.row = make([]parser.Datum, len(cols))
	n.filterVars = parser.MakeIndexedVarHelper(n, len(cols))
}
Пример #4
0
func TestProcessExpression(t *testing.T) {
	defer leaktest.AfterTest(t)()

	e := Expression{Expr: "$0 * ($1 + $2) + $0"}
	h := parser.MakeIndexedVarHelper(testVarContainer{}, 4)
	expr, err := processExpression(e, &h)
	if err != nil {
		t.Fatal(err)
	}

	if !h.IndexedVarUsed(0) || !h.IndexedVarUsed(1) || !h.IndexedVarUsed(2) || h.IndexedVarUsed(3) {
		t.Errorf("invalid IndexedVarUsed results %t %t %t %t (expected false false false true)",
			h.IndexedVarUsed(0), h.IndexedVarUsed(1), h.IndexedVarUsed(2), h.IndexedVarUsed(3))
	}

	str := expr.String()
	expectedStr := "(var0 * (var1 + var2)) + var0"
	if str != expectedStr {
		t.Errorf("invalid expression string '%s', expected '%s'", str, expectedStr)
	}
}
Пример #5
0
// newTableReader creates a tableReader.
func newTableReader(
	spec *TableReaderSpec, txn *client.Txn, output rowReceiver, evalCtx parser.EvalContext,
) (*tableReader, error) {
	tr := &tableReader{
		desc:    spec.Table,
		txn:     txn,
		output:  output,
		evalCtx: evalCtx,
	}

	numCols := len(tr.desc.Columns)

	tr.outputCols = make([]int, len(spec.OutputColumns))
	for i, v := range spec.OutputColumns {
		tr.outputCols[i] = int(v)
	}

	tr.filterVars = parser.MakeIndexedVarHelper(tr, numCols)
	var err error
	tr.filter, err = processExpression(spec.Filter, &tr.filterVars)
	if err != nil {
		return nil, err
	}

	// Figure out which columns we need: the output columns plus any other
	// columns used by the filter expression.
	valNeededForCol := make([]bool, numCols)
	for _, c := range tr.outputCols {
		if c < 0 || c >= numCols {
			return nil, util.Errorf("invalid column index %d", c)
		}
		valNeededForCol[c] = true
	}
	for c := 0; c < numCols; c++ {
		valNeededForCol[c] = valNeededForCol[c] || tr.filterVars.IndexedVarUsed(c)
	}

	var index *sqlbase.IndexDescriptor
	var isSecondaryIndex bool
	switch {
	case spec.IndexIdx == 0:
		index = &tr.desc.PrimaryIndex
	case spec.IndexIdx <= uint32(len(tr.desc.Indexes)):
		index = &tr.desc.Indexes[spec.IndexIdx-1]
		isSecondaryIndex = true
	default:
		return nil, util.Errorf("Invalid indexIdx %d", spec.IndexIdx)
	}

	colIdxMap := make(map[sqlbase.ColumnID]int, len(tr.desc.Columns))
	for i, c := range tr.desc.Columns {
		colIdxMap[c.ID] = i
	}
	err = tr.fetcher.Init(&tr.desc, colIdxMap, index, spec.Reverse, isSecondaryIndex,
		valNeededForCol)
	if err != nil {
		return nil, err
	}

	tr.spans = make(sqlbase.Spans, len(spec.Spans))
	for i, s := range spec.Spans {
		tr.spans[i] = sqlbase.Span{Start: s.Span.Key, End: s.Span.EndKey}
	}

	return tr, nil
}