ClickHouse / ClickHouse/clickhouse-js-parser

format() emits `CREATE OR REPLACE ROW POLICY`, which ClickHouse rejects

Open
#38 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
5
Forks
1
Avg merge
4h 16m
Merged PRs (30d)
2

Description

`format()` re-prints a parsed `CREATE ROW POLICY OR REPLACE policy_name ...` statement as `CREATE OR REPLACE ROW POLICY policy_name ...`, which the ClickHouse server rejects — the documented syntax only allows `OR REPLACE` after `POLICY`, never after `CREATE`.

## Reproduction

```js
const { parse, format } = require('@clickhouse/parser');

const sql = 'CREATE ROW POLICY OR REPLACE p ON db.table USING db.table.tenant_id = 1';

parse(sql)[0].or_replace; // true — parses correctly

format(parse(sql));
// => CREATE OR REPLACE ROW POLICY p ON db.table USING db.table.tenant_id = 1;
// invalid: https://clickhouse.com/docs/reference/statements/create/row-policy
// only accepts CREATE [ROW] POLICY [IF NOT EXISTS | OR REPLACE] policy_name ...
```

The parser also accepts that same invalid form as input, without error:

```js
const bad = 'CREATE OR REPLACE ROW POLICY p ON db.table USING db.table.tenant_id = 1';

parse(bad)[0].or_replace; // true — accepted, though the server would reject this syntax
```

## Suggested fixes

1. Fix parser to accept `OR REPLACE` in correct position.
2. Fix `format()` to emit `OR REPLACE` in the correct position.

## Environment

`@clickhouse/parser` 0.3.0

Contributor guide

Open the contributing guide

Research direction

Start with the parse() and format() entry points in @clickhouse/parser and reproduce the CREATE ROW POLICY OR REPLACE example from the issue. Trace how or_replace is parsed and emitted; done means valid ClickHouse syntax is formatted correctly and the invalid CREATE OR REPLACE form is no longer accepted.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, sql, typescript
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.