Repository: influxdata/influxdb
File: tsdb/field_validator.go (Go)

Code before:
	// in parallel may depend on these previous fields having been
	// created in memory
	for iter.Next() {
		if !skipSizeValidation {
			// Check for size of field too large. Note it is much cheaper to check the whole point size
			// than checking the StringValue size (StringValue potentially takes an allocation if it must
			// unescape the string, and must at least parse the string)
			if pointSize > MaxFieldValueLength && iter.Type() == models.String {
				if sz := len(iter.StringValue()); sz > MaxFieldValueLength {
					return fieldsToCreate, &PartialWriteError{
						Reason: fmt.Sprintf(
							"input field %q on measurement %q is too long, %d > %d",
							iter.FieldKey(), point.Name(), sz, MaxFieldValueLength),
						Dropped: 1,
					}
				}
			}
		}

		fieldKey := iter.FieldKey()
		// Skip fields name "time", they are illegal.
		if bytes.Equal(fieldKey, TimeBytes) {
			partialWriteError = &PartialWriteError{
				Reason: fmt.Sprintf(
					"invalid field name: input field \"%[1]s\" on measurement \"%s\" is invalid. Field \"%[1]s\" has been stripped from point.",
					string(fieldKey), string(point.Name())),
				Dropped: 0,
			}
			continue
		}

		dataType := dataTypeFromModelsFieldType(iter.Type())
		if dataType == influxql.Unknown {
			continue
		}

		fieldName := string(fieldKey)
		f, created, err := mf.CreateFieldIfNotExists(fieldName, dataType)
		if errors.Is(err, ErrFieldTypeConflict) {
			return fieldsToCreate, &PartialWriteError{
				Reason: fmt.Sprintf(
					"%s: input field %q on measurement %q is type %s, already exists as type %s",
					err, fieldName, point.Name(), dataType, f.Type),
				Dropped: 1,
			}
		} else if err != nil {
			return fieldsToCreate, &PartialWriteError{
				Reason: fmt.Sprintf(
					"error adding field %q to measurement %q: %s",
					fieldName, point.Name(), err),
				Dropped: 1,

Review comment (suggestion):
You probably do not need the cast to `string`; `Sprintf` can treat byte slices correctly with `%s`, I believe

Code after:
	// in parallel may depend on these previous fields having been
	// created in memory
	for iter.Next() {
		if !skipSizeValidation {
			// Check for size of field too large. Note it is much cheaper to check the whole point size
			// than checking the StringValue size (StringValue potentially takes an allocation if it must
			// unescape the string, and must at least parse the string)
			if pointSize > MaxFieldValueLength && iter.Type() == models.String {
				if sz := len(iter.StringValue()); sz > MaxFieldValueLength {
					return fieldsToCreate, &PartialWriteError{
						Reason: fmt.Sprintf(
							"input field %q on measurement %q is too long, %d > %d",
							iter.FieldKey(), point.Name(), sz, MaxFieldValueLength),
						Dropped: 1,
					}
				}
			}
		}

		fieldKey := iter.FieldKey()
		// Skip fields name "time", they are illegal.
		if bytes.Equal(fieldKey, TimeBytes) {
			if partialWriteError == nil {
				partialWriteError = &PartialWriteError{
					Reason: fmt.Sprintf(
						"invalid field name: input field \"%[1]s\" on measurement \"%s\" is invalid. Field \"%[1]s\" has been stripped from point.",
						fieldKey, point.Name()),
					Dropped: 0,
				}
			}

			continue
		}

		dataType := dataTypeFromModelsFieldType(iter.Type())
		if dataType == influxql.Unknown {
			continue
		}

		fieldName := string(fieldKey)
		f, created, err := mf.CreateFieldIfNotExists(fieldName, dataType)
		if errors.Is(err, ErrFieldTypeConflict) {
			return fieldsToCreate, &PartialWriteError{
				Reason: fmt.Sprintf(
					"%s: input field %q on measurement %q is type %s, already exists as type %s",
					err, fieldName, point.Name(), dataType, f.Type),
				Dropped: 1,
			}
		} else if err != nil {
			return fieldsToCreate, &PartialWriteError{
				Reason: fmt.Sprintf(