I see. I will look into the Babel parser then. Thank you!

Best,
Xiaoying

On Tue, Mar 5, 2024 at 3:48 PM Julian Hyde <[email protected]> wrote:

> The parser does its work without looking at the operator table. (Like any
> sane parser, it deals only with syntax, not semantics.) So, it’s impossible
> for your TRIM function to affect what the parser considers to be valid.
>
> One option is for you to modify the Babel parser, which is a more lenient
> variant of the parser. For example, it allows DATE as a function:
> https://issues.apache.org/jira/browse/CALCITE-3022.
>
> Julian
>
>
> > On Mar 5, 2024, at 2:18 PM, Xiaoying Wang <[email protected]>
> wrote:
> >
> > Hi community,
> >
> > I want to override the `trim` function. Currently the function it cannot
> > support signature like this: `trim(string, characters)`, which is
> supported
> > by database like DuckDB[1].
> >
> > I implemented a function like the following:
> >
> > ```
> > public class SqlTrim2Function extends SqlFunction {
> >    public static final SqlTrim2Function INSTANCE = new
> SqlTrim2Function();
> >    public SqlTrim2Function() {
> >        super(
> >                "TRIM",
> >                SqlKind.OTHER_FUNCTION,
> >                ReturnTypes.VARCHAR,
> >                null,
> >                OperandTypes.STRING
> >                        .or(OperandTypes.sequence("TRIM(<STRING>,
> > <STRING>)",
> >                                OperandTypes.STRING,
> OperandTypes.STRING)),
> >                SqlFunctionCategory.STRING);
> >    }
> > }
> > ```
> >
> > and added to the operator table like this:
> >
> > ```
> >        SqlOperatorTable sqlOperatorTable = SqlOperatorTables.chain(
> >                SqlOperatorTables.of(
> >                        SqlTrim2Function.INSTANCE
> >                )
> >        );
> >
> >        FrameworkConfig frameworkConfig = Frameworks.newConfigBuilder()
> >                ...
> >                .operatorTable(sqlOperatorTable)
> >                ...
> >                .build();
> > ```
> >
> > However, I still got the parsing error (saying there is a ',').
> >
> > The reason is that the `TRIM` expression directly goes to the
> > `BuiltinFunctionCall` since `jj_2_60(2)` is true inside
> > `AtomicRowExpression` of `SqlParserImpl` instead of goes to
> > `NamedFunctionCall·. So it does not goes to the function I defined.
> >
> > Currently I workaround this by changing the function name to `trim2` so
> > that it is not recognized as a built-in function, which works but is not
> > ideal. Is there a easy way to override the `trim` function inside
> > SqlParserImpl (seems like it is generated)?
> >
> > Best,
> > Xiaoying
> >
> >
> > [1] https://duckdb.org/docs/sql/functions/char.html
>
>

Reply via email to