Repository: dolthub/dolt
File: go/libraries/doltcore/sqle/kvexec/lookup_join.go (Go)

Code before:
			col, ok := sourceSch.GetAllCols().LowerNameToCol[strings.ToLower(e.Name())]
			if !ok {
				return nil, fmt.Errorf("failed to build lookup mapping, column missing from schema: %s", e.Name())
			}
			if col.IsPartOfPK {
				srcMapping[i] = sourceSch.GetPKCols().TagToIdx[col.Tag]
			} else if keyless {
				// Skip cardinality column
				srcMapping[i] = split + 1 + sourceSch.GetNonPKCols().TagToIdx[col.Tag]
			} else {
				srcMapping[i] = split + sourceSch.GetNonPKCols().TagToIdx[col.Tag]
			}
		case *expression.Literal:
			srcMapping[i] = -1
			litMappings = append(litMappings, i)
			tgtTyp := tgtKeyDesc.Types[i]
			litTypes = append(litTypes, tgtTyp)
			tda.Handlers = append(tda.Handlers, tgtKeyDesc.Handlers[i])
		}
	}

	litDesc := val.NewTupleDescriptorWithArgs(tda, litTypes...)
	litTb := val.NewTupleBuilder(litDesc, ns)
	for i, j := range litMappings {
		colTyp := typs[j]
		value, err := convertLiteralKeyValue(ctx, colTyp, keyExprs[j].(*expression.Literal))
		if err != nil && !sql.ErrTruncatedIncorrect.Is(err) {
			return nil, err
		}

		if err := tree.PutField(ctx, ns, litTb, i, value); err != nil {
			return nil, err
		}
	}

	var litTuple val.Tuple
	var err error
	if litDesc.Count() > 0 {
		litTuple, err = litTb.Build(ns.Pool())
		if err != nil {
			return nil, err
		}
	}

	return &lookupMapping{
		split:      split,
		srcMapping: srcMapping,
		litTuple:   litTuple,
		litKd:      litDesc,
		srcKd:      sourceSch.GetKeyDescriptor(ns),
		srcVd:      sourceSch.GetValueDescriptor(ns),

Review comment (bug):
Strikes me as the wrong fix. If the literal is out of range for the target type, we know the lookup will return no rows. We could communicate this to the call site of this function and let them deal with it appropriately by returning an empty result, which is how this works in GMS's join / index lookup logic. Same thing should happen here.

Code after:
			col, ok := sourceSch.GetAllCols().LowerNameToCol[strings.ToLower(e.Name())]
			if !ok {
				return nil, fmt.Errorf("failed to build lookup mapping, column missing from schema: %s", e.Name())
			}
			if col.IsPartOfPK {
				srcMapping[i] = sourceSch.GetPKCols().TagToIdx[col.Tag]
			} else if keyless {
				// Skip cardinality column
				srcMapping[i] = split + 1 + sourceSch.GetNonPKCols().TagToIdx[col.Tag]
			} else {
				srcMapping[i] = split + sourceSch.GetNonPKCols().TagToIdx[col.Tag]
			}
		case *expression.Literal:
			srcMapping[i] = -1
			litMappings = append(litMappings, i)
			tgtTyp := tgtKeyDesc.Types[i]
			litTypes = append(litTypes, tgtTyp)
			tda.Handlers = append(tda.Handlers, tgtKeyDesc.Handlers[i])
		}
	}

	litDesc := val.NewTupleDescriptorWithArgs(tda, litTypes...)
	litTb := val.NewTupleBuilder(litDesc, ns)
	for i, j := range litMappings {
		colTyp := typs[j]
		value, inRange, err := convertLiteralKeyValue(ctx, colTyp, keyExprs[j].(*expression.Literal))
		if err != nil && !sql.ErrTruncatedIncorrect.Is(err) {
			return nil, err
		}
		if inRange != sql.InRange {
			return nil, nil
		}

		if err := tree.PutField(ctx, ns, litTb, i, value); err != nil {
			return nil, err
		}
	}

	var litTuple val.Tuple
	var err error
	if litDesc.Count() > 0 {
		litTuple, err = litTb.Build(ns.Pool())
		if err != nil {
			return nil, err
		}
	}

	return &lookupMapping{
		split:      split,
		srcMapping: srcMapping,
		litTuple:   litTuple,