Github user zsxwing commented on the issue:

    https://github.com/apache/spark/pull/17209
  
    Could you add a utility method like this?
    ```
      def getKafkaOffsetRangeLimit(
          params: Map[String, String],
          key: String,
          default: KafkaOffsetRangeLimit) : KafkaOffsetRangeLimit = {
        params.get(key).map(_.trim) match {
          case Some(offset) if offset.toLowerCase == "latest" => 
LatestOffsetRangeLimit
          case Some(offset) if offset.toLowerCase == "earliest" => 
EarliestOffsetRangeLimit
          case Some(json) => 
SpecificOffsetRangeLimit(JsonUtils.partitionOffsets(json))
          case None => default
        }
      }
    ```
    Then you can reuse the method and only test it, e.g.,
    ```
        val startingOffsets = getKafkaOffsetRangeLimit(
          caseInsensitiveParams,
          STARTING_OFFSETS_OPTION_KEY,
          EarliestOffsetRangeLimit
        )
    
        // Batch specific options
        startingOffsets match {
          case EarliestOffsetRangeLimit => // good to go
          case LatestOffsetRangeLimit =>
            throw new IllegalArgumentException("starting offset can't be latest 
" +
              "for batch queries on Kafka")
          case SpecificOffsetRangeLimit(partitionOffsets) =>
            partitionOffsets.foreach {
              case (tp, off) if off == KafkaOffsetRangeLimit.LATEST =>
                throw new IllegalArgumentException(s"startingOffsets for $tp 
can't " +
                  "be latest for batch queries on Kafka")
              case _ => // ignore
            }
        }
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to