Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type attribute to A_CONST nodes. #5

Merged
merged 2 commits into from
Nov 17, 2015

Conversation

zhm
Copy link
Contributor

@zhm zhm commented Nov 17, 2015

This fixes an issue where BitString's cannot be deparsed properly
because they cannot be distinguished from normal strings.

Example:

SELECT B'0101'

and

SELECT '0101'

This patch adds a type which outputs the integer node tag so that
callers can be certain of the type of the constant given the limitations
of the serialization format (JSON). This also fixes a bug where
BitStrings were being dumped as invalid JSON:

{ "val": b0101 }

This fixes an issue where BitString's cannot be deparsed properly
because they cannot be distinguished from normal strings.

Example:

SELECT B'0101'

and

SELECT '0101'

This patch adds a `type` which outputs the integer node tag so that
callers can be certain of the type of the constant given the limitations
of the serialization format (JSON). This also fixes a bug where
`BitString`s were being dumped as invalid JSON:

```
{ "val": b0101 }
```
@@ -140,7 +140,7 @@ index 0000000..eaded0f
+static void
+_outToken(StringInfo str, const char *s)
+{
+ if (s == NULL || *s == '\0')
+ if (s == NULL)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats the fix for #3 - correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, that's a fix for #3. It was such a trivial change I just included it here ;)

@lfittl
Copy link
Member

lfittl commented Nov 17, 2015

Looks good!

I'll do some quick testing with the Ruby library, to make sure this doesn't confuse any callers - looking to merge this later today.

@zhm
Copy link
Contributor Author

zhm commented Nov 17, 2015

@lfittl one possible thing to consider with the bitstrings is if the leading char should be stripped.

Current PR:

{
    "A_CONST": {
        "location": 7,
        "type": 654,
        "val": "b0001"
    }
}

The lib could either return "val": "b0001" or "val": "0001" with the leader stripped since it's implicit from the type. Not sure which is better, just thought I'd bring it up!

@lfittl
Copy link
Member

lfittl commented Nov 17, 2015

@zhm Good point - lets maybe do the version without b, since you're right, its already in the type.

Also, looking at the output, do you think we can output the type as a readable name instead? (might make parsing more reliable across versions)

@zhm
Copy link
Contributor Author

zhm commented Nov 17, 2015

👍 for readable type names!

@zhm
Copy link
Contributor Author

zhm commented Nov 17, 2015

I updated the PR to use strings instead of enums for the type names.

We now have:

integer
float
string
bitstring
null

Also I think we need to leave the b prefix on bitstrings. It turns out there's yet another syntax for bitstrings. Both of the following are returned as bitstrings:

SELECT B'0101`

> b0101
SELECT X'EFFF`

> xEFFF

@zhm
Copy link
Contributor Author

zhm commented Nov 17, 2015

Also note that I don't think bitstrings are particularly useful to most people, just trying to get some random SQL scripts to parse/deparse ;)

I suppose it's possible that Postgres could introduce new value types, so maybe this all makes sense anyways.

@zhm
Copy link
Contributor Author

zhm commented Nov 17, 2015

@lfittl also, are there any other scenarios where the caller would care about the type of the Value struct? I had originally put the type info in the _outValue function but I noticed the Value node is used for very trivial things like column names and such. Is there anything other than A_CONST where a bitstring/integer/float would come up?

@lfittl
Copy link
Member

lfittl commented Nov 17, 2015

@zhm Patch looks good!

From my quick grep-ing through the node definitions, it looks like A_CONST is the only one that really uses different types - all others seem to be strings.

If we wanted to put it into all value nodes, we could consider only outputting the type if its not string (and maybe also if its not null), to avoid bloating the tree. But I'll merge this as-is, we can revisit later if needed.

lfittl added a commit that referenced this pull request Nov 17, 2015
Add `type` attribute to A_CONST nodes.
@lfittl lfittl merged commit 95e5e72 into pganalyze:master Nov 17, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants