Skip to content

Commit

Permalink
Add Python language functions
Browse files Browse the repository at this point in the history
  • Loading branch information
electrum committed Dec 8, 2024
1 parent 243a82c commit 6b806ae
Show file tree
Hide file tree
Showing 11 changed files with 3,363 additions and 0 deletions.
164 changes: 164 additions & 0 deletions plugin/trino-functions-python/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.trino</groupId>
<artifactId>trino-root</artifactId>
<version>468-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<artifactId>trino-functions-python</artifactId>
<packaging>trino-plugin</packaging>
<description>Trino - Python language functions</description>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>bom</artifactId>
<version>1.0.0-M2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>log</artifactId>
</dependency>

<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>runtime</artifactId>
</dependency>

<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>wasi</artifactId>
</dependency>

<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>wasm</artifactId>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>

<dependency>
<groupId>com.google.jimfs</groupId>
<artifactId>jimfs</artifactId>
<version>1.3.0</version>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>log</artifactId>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>units</artifactId>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-plugin-toolkit</artifactId>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-wasm-python</artifactId>
<version>3.13-1</version>
</dependency>

<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>slice</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-api</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-context</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-spi</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>junit-extensions</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-main</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-main</artifactId>
<type>test-jar</type>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-testing</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-tpch</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Licensed 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.
*/
package io.trino.plugin.functions.python;

import com.dylibso.chicory.log.Logger;

import static java.util.Objects.requireNonNull;

final class JdkLogger
implements Logger
{
private final java.util.logging.Logger logger;

public static Logger get(Class<?> clazz)
{
return new JdkLogger(java.util.logging.Logger.getLogger(clazz.getName()));
}

public JdkLogger(java.util.logging.Logger logger)
{
this.logger = requireNonNull(logger, "logger is null");
}

@Override
public void log(Level level, String msg, Throwable throwable)
{
logger.log(toJdkLevel(level), msg, throwable);
}

@Override
public boolean isLoggable(Level level)
{
return logger.isLoggable(toJdkLevel(level));
}

private static java.util.logging.Level toJdkLevel(Level level)
{
return switch (level) {
case ALL -> java.util.logging.Level.ALL;
case TRACE -> java.util.logging.Level.FINEST;
case DEBUG -> java.util.logging.Level.FINE;
case INFO -> java.util.logging.Level.INFO;
case WARNING -> java.util.logging.Level.WARNING;
case ERROR -> java.util.logging.Level.SEVERE;
case OFF -> java.util.logging.Level.OFF;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Licensed 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.
*/
package io.trino.plugin.functions.python;

import io.airlift.log.Logger;

import java.io.ByteArrayOutputStream;

import static com.google.common.base.CharMatcher.javaIsoControl;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Objects.requireNonNull;

@SuppressWarnings("UnsynchronizedOverridesSynchronized")
final class LoggingOutputStream
extends ByteArrayOutputStream
{
private final Logger logger;

public LoggingOutputStream(Logger logger)
{
this.logger = requireNonNull(logger, "logger is null");
}

@Override
public void write(byte[] b, int off, int len)
{
if (logger.isDebugEnabled()) {
super.write(b, off, len);
flush();
}
}

@Override
public void flush()
{
if (count > 4096) {
log(toString(UTF_8));
reset();
return;
}

int index;
for (index = count - 1; index >= 0; index--) {
if (buf[index] == '\n') {
break;
}
}
if (index == -1) {
return;
}

String data = new String(buf, 0, index, UTF_8);
data.lines().forEach(this::log);

int remaining = count - index - 1;
System.arraycopy(buf, index + 1, buf, 0, remaining);
count = remaining;
}

@Override
public void close()
{
log(toString(UTF_8));
reset();
}

private void log(String message)
{
String value = javaIsoControl().removeFrom(message).strip();
if (!value.isEmpty()) {
logger.debug(value);
}
}
}
Loading

0 comments on commit 6b806ae

Please sign in to comment.