Skip to content

Commit

Permalink
Add version() system information function.
Browse files Browse the repository at this point in the history
This function is used by some PostgreSQL protocol based clients.
  • Loading branch information
seut authored and mergify[bot] committed Jan 21, 2020
1 parent 92856f2 commit bf618fd
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 2 deletions.
4 changes: 2 additions & 2 deletions docs/appendices/release-notes/unreleased.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ None
Changes
=======

- Added the :ref:`version() <version>` system information function.

- Added support for the PostgreSQL notation to refer to array types. For
example, it is now possible to use ``text[]`` instead of ``array(test)``.

None

Fixes
=====

Expand Down
27 changes: 27 additions & 0 deletions docs/general/builtins/scalar.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2402,6 +2402,33 @@ Example:
+--------------+
SELECT 1 row in set (... sec)

.. _version:

``version``
-----------

Returns the CrateDB version information.

Returns: ``text``

Synopsis::

version()

Example:

::

cr> select version();
+---------...-+
| version() |
+---------...-+
| CrateDB ... |
+---------...-+
SELECT 1 row in set (... sec)



Special functions
=================

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import io.crate.expression.scalar.systeminformation.CurrentSchemasFunction;
import io.crate.expression.scalar.systeminformation.PgGetExpr;
import io.crate.expression.scalar.systeminformation.PgTypeofFunction;
import io.crate.expression.scalar.systeminformation.VersionFunction;
import io.crate.expression.scalar.timestamp.CurrentTimestampFunction;
import io.crate.expression.scalar.timestamp.TimezoneFunction;
import io.crate.metadata.FunctionIdent;
Expand Down Expand Up @@ -171,6 +172,7 @@ protected void configure() {
PgGetUserByIdFunction.register(this);
PgTypeofFunction.register(this);
register(new CurrentDatabaseFunction());
VersionFunction.register(this);

// bind all registered functions and resolver
// by doing it here instead of the register functions, plugins can also use the
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Licensed to Crate under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership. Crate licenses this file
* to you under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* However, if you have executed another commercial license agreement
* with Crate these terms will supersede the license and you may use the
* software solely pursuant to the terms of the relevant commercial
* agreement.
*/

package io.crate.expression.scalar.systeminformation;

import io.crate.data.Input;
import io.crate.expression.scalar.ScalarFunctionModule;
import io.crate.metadata.FunctionIdent;
import io.crate.metadata.FunctionInfo;
import io.crate.metadata.FunctionName;
import io.crate.metadata.Scalar;
import io.crate.metadata.TransactionContext;
import io.crate.metadata.pgcatalog.PgCatalogSchemaInfo;
import io.crate.types.DataTypes;
import org.elasticsearch.Build;
import org.elasticsearch.Version;

import java.util.Collections;
import java.util.Locale;

public class VersionFunction extends Scalar<String, Void> {

public static final String NAME = "version";

private static final FunctionName FQN = new FunctionName(PgCatalogSchemaInfo.NAME, NAME);

protected static final FunctionInfo INFO = new FunctionInfo(
new FunctionIdent(FQN, Collections.emptyList()), DataTypes.DOUBLE,
FunctionInfo.Type.SCALAR, FunctionInfo.NO_FEATURES);

public static void register(ScalarFunctionModule scalarFunctionModule) {
scalarFunctionModule.register(new VersionFunction());
}

private static String formatVersion() {
String version = Version.displayVersion(Version.CURRENT, Version.CURRENT.isSnapshot());
String built = String.format(
Locale.ENGLISH,
"built %s/%s",
Build.CURRENT.hashShort(),
Build.CURRENT.timestamp());
String vmVersion = String.format(
Locale.ENGLISH,
"%s %s",
System.getProperty("java.vm.name"),
System.getProperty("java.vm.version"));
String osVersion = String.format(
Locale.ENGLISH,
"%s %s %s",
System.getProperty("os.name"),
System.getProperty("os.version"),
System.getProperty("os.arch"));

return String.format(
Locale.ENGLISH,
"CrateDB %s (%s, %s, %s)",
version,
built,
osVersion,
vmVersion);
}

private static final String VERSION = formatVersion();

@Override
public String evaluate(TransactionContext txnCtx, Input<Void>... args) {
return VERSION;
}

@Override
public FunctionInfo info() {
return INFO;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed to Crate under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership. Crate licenses this file
* to you under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* However, if you have executed another commercial license agreement
* with Crate these terms will supersede the license and you may use the
* software solely pursuant to the terms of the relevant commercial
* agreement.
*/

package io.crate.expression.scalar.postgres;

import io.crate.expression.scalar.AbstractScalarFunctionsTest;
import org.elasticsearch.Version;
import org.junit.Test;

import static org.hamcrest.Matchers.startsWith;

public class VersionFunctionTest extends AbstractScalarFunctionsTest {

@Test
public void testVersion() {
String expected = "CrateDB " + Version.displayVersion(Version.CURRENT, Version.CURRENT.isSnapshot()) + " (";
assertEvaluate("version()", startsWith(expected));
}
}

0 comments on commit bf618fd

Please sign in to comment.