Skip to content

Conversation

maddyblue
Copy link
Contributor

@maddyblue maddyblue commented Apr 24, 2017

Fixes #1327


This change is Reviewable

@maddyblue maddyblue requested a review from jseldess April 24, 2017 20:04
@cockroach-teamcity
Copy link
Member

Copy link
Contributor

@jseldess jseldess left a comment

Choose a reason for hiding this comment

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

LGTM, with some minor nits. Thanks very much, @mjibson!


- title: Information Schema
url: /information-schema.html
thirdlevel:
Copy link
Contributor

Choose a reason for hiding this comment

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

For consistency with the rest of the file, would you mind formatting this as follows?

thirdlevel:
  - title: Porting Applications
    thirdlevelitems:

      - title: From PostgreSQL
        url: /porting-postgres.html

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

- title: Porting Applications
thirdlevelitems:

- title: Postgres
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it'd be clearer to use the title From PostgreSQL here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

toc: false
---

Although CockroachDB supports Postgres syntax and drivers, it does not offer exact compatibility. This page documents the known list of differences between Postgres and CockroachDB for identical input. That is, a SQL statement of the type listed here will behave differently than in Postgres. Porting an existing application to CockroachDB will require changing these expressions.
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's use PostgreSQL throughout instead of Postgres.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

ERROR: zero raised to a negative power is undefined
```

In CockroachDB these expressions will instead return Infinity:
Copy link
Contributor

Choose a reason for hiding this comment

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

Add comma after In CockroachDB

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

In CockroachDB these expressions will instead return Infinity:

```
root@:26257/> select 1e300::float * 1e10::float;
Copy link
Contributor

Choose a reason for hiding this comment

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

For the sql prompt, let's use >. Also, can you capitalize the keywords and break commands and responses into separate code blocks, where the commands have sql syntax highlighting and the responses have node?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


### Integer division

Division of integers in Postgres results in an integer. The query:
Copy link
Contributor

Choose a reason for hiding this comment

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

Again, I'd rewrite this so the code block doesn't interrupt the sentence:

For example, the following query returns 1, since the 1 / 2 is truncated to 0 since it is performing integer division:

SELECT 1 + 1 / 2

CockroachDB instead provides the // operator to perform floor division.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


In CockroachDB, unary `~` has the same (high) precedence as unary `-`, so the above expression will be parsed as `(~1) + 2`.

**Porting instructions:** manually add parentheses around expressions that depend on the Postgres behavior.
Copy link
Contributor

Choose a reason for hiding this comment

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

Capitalize Manually

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


Returns `1`, since the `1 / 2` is truncated to `0` since it is performing integer division. In CockroachDB integer division results in a `decimal`. CockroachDB instead provides the `//` operator to perform floor division.

**Porting instructions:** change `/` to `//` in integer division where the result must be an integer.
Copy link
Contributor

Choose a reason for hiding this comment

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

Capitalize Change

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


### Shift argument modulo

The shift operators in Postgres (`<<`, `>>`) sometimes modulo their second argument to the bit size of the underlying type. For example:
Copy link
Contributor

Choose a reason for hiding this comment

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

Again, revise so the sample doesn't interrupt the sentence:

For example, the following query results in a 1 because the int type is 32 bits, and 32 % 32 is 0, so this is the equivalent of 1 << 0:

SELECT 1::int << 32

In CockroachDB, no such modulo is performed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


Results in a `1` because the `int` type is 32 bits, and `32 % 32` is `0`, so this is the equivalent of `1 << 0`. In CockroachDB, no such modulo is performed.

**Porting instructions:** manually add a modulo to the second argument. Also note that CockroachDB's int type is always 64 bits. For example:
Copy link
Contributor

Choose a reason for hiding this comment

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

Capitalize Manually. Also, let's capitalize INT and link to our docs:

[`INT`](int.html)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@cockroach-teamcity
Copy link
Member

@cockroach-teamcity
Copy link
Member

Copy link
Contributor

@jseldess jseldess left a comment

Choose a reason for hiding this comment

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

LGTM, with a few final comments

SELECT ~1 + 2
```

Is parsed as `~ (1 + 2)` because `~` has a lower precedence than `+`.
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't need this line anymore.


### Integer division

Division of integers in PostgreSQL results in an integer. For example, the following query returns `1`, since the `1 / 2` is truncated to `0` since it is performing integer division:
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd actually remove since it is performing integer division. Feels redundant and makes the sentence a bit convoluted.

@sploiselle
Copy link
Contributor

I'd recommend mentioning that this guide primarily focuses on expressions and does not demonstrate how to port applications that use unsupported SQL features (e.g. arrays, enum).

@jseldess
Copy link
Contributor

Good point, @sploiselle. We may want to expand this page to cover a lot more, but for now, totally agree that it's best to be clear about the focus.

@sploiselle
Copy link
Contributor

Review status: 0 of 2 files reviewed at latest revision, 15 unresolved discussions.


porting-postgres.md, line 9 at r3 (raw file):

Although CockroachDB supports PostgreSQL syntax and drivers, it does not offer exact compatibility. This page documents the known list of differences between PostgreSQL and CockroachDB for identical input. That is, a SQL statement of the type listed here will behave differently than in PostgreSQL. Porting an existing application to CockroachDB will require changing these expressions.

Note that some of these differences only apply to rare inputs, and so no change will be needed, even if the listed feature is being used. In these cases, it is safe to ignore the porting instructions.

After this line, recommend adding something like:

{{site.data.alerts.callout_info}}This document currently only covers how to rewrite SQL expressions. It does not discuss strategies for porting application that use SQL features CockroachDB does not currently support, such as arrays or the ENUM type.{{site.data.alerts.end}}


Comments from Reviewable

@maddyblue
Copy link
Contributor Author

I've made those changes. RFAL.

@cockroach-teamcity
Copy link
Member

Copy link
Contributor

@jseldess jseldess left a comment

Choose a reason for hiding this comment

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

LGTM, with one final nit.


Note that some of these differences only apply to rare inputs, and so no change will be needed, even if the listed feature is being used. In these cases, it is safe to ignore the porting instructions.

{{site.data.alerts.callout_info}}This document currently only covers how to rewrite SQL expressions. It does not discuss strategies for porting application that use [SQL features CockroachDB does not currently support](/sql-feature-support.html), such as arrays or the `ENUM` type.{{site.data.alerts.end}}
Copy link
Contributor

Choose a reason for hiding this comment

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

porting applications - needs to be plural

@cockroach-teamcity
Copy link
Member

@cockroach-teamcity
Copy link
Member

@cockroach-teamcity
Copy link
Member

@maddyblue maddyblue merged commit 249e0fa into gh-pages Apr 26, 2017
@maddyblue maddyblue deleted the porting-postgres branch April 26, 2017 05:33
Simran-B added a commit to Simran-B/docs that referenced this pull request Aug 21, 2025
…ckroachdb#1328)

* Optimize access of last element in traversals

* Remove no longer existing patch-update-statements AQL optimizer rule

* Account for optimizer rules that are applied a 3rd time (-3) suffix

---------

Co-authored-by: Paula Mihu <97217318+nerpaula@users.noreply.github.com>
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.

4 participants