Skip to content

Parse conditions that use an operator CCL does not define - #104

Open
jtnelson wants to merge 1 commit into
developfrom
feature/custom-operators
Open

jtnelson wants to merge 1 commit into
developfrom
feature/custom-operators

Conversation

@jtnelson

@jtnelson jtnelson commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Background

Before this change, a CCL condition could not use an operator that a Concourse
plugin provides. CCL read an operator only from the tokens that its grammar
reserves, such as =, bw, like, and contains, so the condition
bio about "red running shoes" was a syntax error. Each operator also took a
fixed number of values (one, or two for bw), whether the condition came from
CCL text or from a list of symbols, such as the one a Concourse Criteria
builds. The Operator interface could state only that fixed number of values,
and it had no way to reject a value of the wrong kind.

An order could apply only a plain function name to a key, so
order by bio | vec.about desc did not parse either.

Change

A condition can use a custom operator, which is an operator that CCL does not
define, such as one that a Concourse plugin provides. A word in the operator
position that is not a built-in operator names a custom operator, and the word
can be plain, as in about, or qualified with periods, as in vec.about. The
operator function that the caller passes to Compiler.create resolves the word
to an Operator, the same way it resolves the symbol of a built-in operator.
One or more values follow the word, and a timestamp, and, or, and
parentheses work with it as they do with a built-in operator.

Operator has three new default methods that describe the values an operator
accepts. minOperands() and maxOperands() bound how many values a condition
can give it, and validate(List<Object>) throws IllegalArgumentException for
values it rejects. The parser applies those rules through
Parsing.checkOperands, a new public method.

In this example, a caller defines an operator that takes a phrase and an
optional limit, and resolves both about and vec.about to it.

public final class AboutOperator implements Operator {

    @Override
    public int operands() {
        return 1;
    }

    @Override
    public int minOperands() {
        return 1;
    }

    @Override
    public int maxOperands() {
        return 2;
    }

    @Override
    public String symbol() {
        return "about";
    }

    @Override
    public void validate(List<Object> operands) {
        if(!(operands.get(0) instanceof String)) {
            throw new IllegalArgumentException("the phrase must be a string");
        }
    }

}

Operator about = new AboutOperator();
Compiler compiler = Compiler.create(Convert::stringToJava, symbol -> {
    if(symbol.equals("about") || symbol.equals("vec.about")) {
        return about;
    }
    else {
        return Convert.stringToOperator(symbol);
    }
});

// about gets the values "red running shoes" and 10
compiler.parse("bio about \"red running shoes\" 10 and status = active");
compiler.parse("bio vec.about \"red running shoes\"");
compiler.parse("find bio about \"shoes\" order by bio | vec.about desc");

// Throws a SyntaxException, because validate rejects 5 as the phrase
compiler.parse("bio about 5");

// Throws a SyntaxException, because about takes at most two values
compiler.parse("bio about \"red running shoes\" 10 20");

Parsing.isOperatorName(String) returns whether a name is one plain word that a
custom operator can use as its symbol.

Parsing.isOperatorName("about");      // true
Parsing.isOperatorName("similar_to"); // true
Parsing.isOperatorName("like");       // false, because like is a built-in operator
Parsing.isOperatorName("limit");      // false, because limit is a reserved word
Parsing.isOperatorName("vec.about");  // false, because it is not one plain word

Most of the diff is mechanical, because Grammar.java is generated from
grammar.jjt, and the within duration constants in Parsing only moved to
the end of the class.

Semantics

Custom operators in CCL

  • Each word or quoted string after a custom operator is its own value, so a
    value that contains spaces needs quotes. For example, loc near x 1 2 gives
    near three values, while a = 1 2 3 still gives = the single value
    1 2 3.
  • In findOrInsert, a quoted string that ends the command is the command's JSON
    and not a value. For example, findOrInsert bio about x 'y' '{"a": 1}' gives
    about the values x and y.
  • When the operator function throws for a word, the parse fails with a
    SyntaxException whose message names the word, its line and column, and the
    reason that the function gave.

Value checks

  • The parser checks the values of every condition against its operator, whether
    the condition comes from CCL text or from a list of symbols. A condition with
    too few values, too many values, or values that validate rejects fails with
    a SyntaxException that names the operator.
  • The parser calls validate only after the number of values is within bounds,
    and it passes the parsed values in order. So validate can read the first
    value whenever minOperands() is at least 1.
  • By default, minOperands() and maxOperands() both return operands(), and
    validate accepts any values. So an existing Operator takes exactly
    operands() values, and every built-in operator parses as it did before.
  • In a list of symbols, an operator takes as many of the values that follow it
    as its maxOperands() allows, so a Criteria can give a custom operator more
    than two values.

@jtnelson

jtnelson commented Nov 3, 2026

Copy link
Copy Markdown
Member Author

This change is the parser support for plugin query operators. The pull requests that build on it:

  • cinchapi/concourse#1555 lets a Concourse plugin provide query operators. Its Java driver depends on the 4.2.0-FEATURE-CUSTOM-OPERATORS snapshot of this change.
  • cinchapi/vector-search#37 provides the about operator for semantic search, built on cinchapi/concourse#1555.

Merge order: this pull request first, then cinchapi/concourse#1555 after its ccl dependency moves to the released version, then cinchapi/vector-search#37 after its Concourse dependency moves to the released version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant