prestodb/presto · error · IllegalArgumentException

pattern may be specified only if location is a directory

Error message

pattern may be specified only if location is a directory

What it means

The DataLocation constructor validates that a 'pattern' (glob for file discovery) may only be supplied when the configured location path is an existing directory. If the location resolves to a regular file (or non-directory) and a pattern was provided, IllegalArgumentException is thrown.

Source

Thrown at presto-local-file/src/main/java/com/facebook/presto/localfile/DataLocation.java:57

    private final Optional<String> pattern;

    @SuppressWarnings("ResultOfMethodCallIgnored")
    @JsonCreator
    public DataLocation(
            @JsonProperty("location") String location,
            @JsonProperty("pattern") Optional<String> pattern)
    {
        requireNonNull(location, "location is null");
        requireNonNull(pattern, "pattern is null");

        File file = new File(location);
        if (!file.exists() && pattern.isPresent()) {
            file.mkdirs();
        }

        checkArgument(file.exists(), "location does not exist");
        if (pattern.isPresent() && !file.isDirectory()) {
            throw new IllegalArgumentException("pattern may be specified only if location is a directory");
        }

        this.location = file;
        this.pattern = (!pattern.isPresent() && file.isDirectory()) ? Optional.of("*") : pattern;
    }

    @JsonProperty
    public File getLocation()
    {
        return location;
    }

    @JsonProperty
    public Optional<String> getPattern()
    {
        return pattern;
    }

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Point localfile.location at a directory, keeping the pattern.
  2. Remove the localfile.pattern property if the location is meant to be a single file.
  3. Verify the location path exists and is a directory before startup (constructor already mkdirs when absent and pattern is present).

Example fix

// before (catalog properties)
localfile.location=/data/events.csv
localfile.pattern=*.csv

// after
localfile.location=/data/events
localfile.pattern=*.csv
Defensive patterns

Strategy: validation

Validate before calling

File f = new File(location);
if (pattern != null && !f.isDirectory()) {
    throw new IllegalArgumentException("pattern requires a directory location");
}

Type guard

boolean patternNeedsDirectory(Optional<String> pattern, File f) { return pattern.isPresent() && !f.isDirectory(); }

Try / catch

try { DataLocation loc = new DataLocation(config.getLocation(), config.getPattern()); } catch (IllegalArgumentException e) { if (e.getMessage().contains("pattern may be specified only if location is a directory")) { fixCatalogProperties(); } else { throw e; } }

Prevention

When it happens

Trigger: Constructing DataLocation with localfile.location pointing at a file while localfile.pattern is set in the connector config, or programmatically passing a pattern for a file-based location.

Common situations: Misconfigured localfile catalog: location set to a single file but a glob pattern left over from a previous directory-based config; copy-pasting properties between catalogs.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04). Data as JSON: /api/errors/354a7e371c22095e. Report an issue: GitHub.