GithubHelp home page GithubHelp logo

Comments (5)

alexey-milovidov avatar alexey-milovidov commented on May 27, 2024

In your example, the json1 parameter has a String type, which means that it has to be provided as a String, e.g.,

set param_json1='{
    "a":1,
    "b":2
}'

select foo({json1:String});

Surprisingly to me, it produces an error:

Code: 457. DB::Exception: Value {
    "a":1,
    "b":2
} cannot be parsed as String for query parameter 'json1' because it isn't parsed completely: only 1 of 24 bytes was parsed: {. (BAD_QUERY_PARAMETER)

from clickhouse.

evillique avatar evillique commented on May 27, 2024

param_ settings are translated into String type after parsing automatically, so you can use it after as a String in substitution.

However, these parameters must be a literal, Array/Map/Tuple of literals, or a combination of them written in ClickHouse syntax. In ClickHouse double quotes (") are used for identifiers and you can't have those inside a literal.

Setting parameters like this is supported:

SET param_json1={
    'a':1,
    'b':2
};

SELECT {json1:String} AS str, {json1:Map(String, UInt64)} AS map;

┌─str────────────┬─map───────────┐
│ {'a':1, 'b':2} │ {'a':1,'b':2} │
└────────────────┴───────────────┘

If the parameter you are trying to set has some other format then you will have to use String literal. Inside this literal the parameter must be in "escaped" format (https://clickhouse.com/docs/en/interfaces/http#tabs-in-url-parameters).

So in your example it would be:

SET param_json1='{\\n    "a":1,\\n    "b":2,\\n};'

SELECT {json1:String} AS str;

┌─str─────────────────────┐
│ {
    "a":1,
    "b":2,
}; │
└─────────────────────────┘

from clickhouse.

alexey-milovidov avatar alexey-milovidov commented on May 27, 2024

@evillique It is ok to require Escaped format in HTTP interface and in command line arguments, because it is the only way to distinguish between a string and NULL.

But when the parameter is provided as an explicit string literal, it is questionable.

from clickhouse.

Alex-Cheng avatar Alex-Cheng commented on May 27, 2024

@evillique It is ok to require Escaped format in HTTP interface and in command line arguments, because it is the only way to distinguish between a string and NULL.

But when the parameter is provided as an explicit string literal, it is questionable.

Thank you for responding my question. Actually I have a very long JSON where strings are double-quoted(because it is standard JSON format). So if I cannot use multi-line string as parameter nor double-quote in JSON, then it would be inconvenient.

from clickhouse.

Alex-Cheng avatar Alex-Cheng commented on May 27, 2024

@evillique It is ok to require Escaped format in HTTP interface and in command line arguments, because it is the only way to distinguish between a string and NULL.
But when the parameter is provided as an explicit string literal, it is questionable.

Thank you for responding my question. Actually I have a very long JSON where strings are double-quoted(because it is standard JSON format). So if I cannot use multi-line string as parameter nor double-quote in JSON, then it would be inconvenient.

I have tried more. The RapidJSONParser cannot parse a JSON with single-quoted key, such as {'A':1, 'B':2}. If I use standard JSON format, such as {"A":1, "B":2}, parsing succeeds.

from clickhouse.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.