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

Code before:
	var fieldsToCreate []*FieldCreate

	// We return fieldsToCreate even on error, because other writes
	// 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) {
			logger.Warn("invalid field name \"time\" for measurement, dropping \"time\" field during write.")
			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,
			}
		} else if created {

Review comment (suggestion):
My difficulty with this is that the writer does not get the error.  I would prefer creating a PartialWriteError with a `Dropped` count of zero, and the same error message we give with single field `time` problems.

In the caller we then, rather than checking for PartialWriteError and continuing if we have one, we check for a PartialWriteError with a `Dropped` count > 0.

Code after:
	var fieldsToCreate []*FieldCreate
	var partialWriteError *PartialWriteError

	// We return fieldsToCreate even on error, because other writes
	// 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,