All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.openlineage.sql.OpenLineageSql Maven / Gradle / Ivy

There is a newer version: 1.26.0
Show newest version
/*
/* Copyright 2018-2024 contributors to the OpenLineage project
/* SPDX-License-Identifier: Apache-2.0
*/

package io.openlineage.sql;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.util.List;
import java.util.Optional;
import org.apache.commons.lang3.SystemUtils;

public final class OpenLineageSql {

  // TODO: wrap defaultSchema
  private static native SqlMeta parse(List sql, String dialect, String defaultSchema)
      throws RuntimeException;

  public static Optional parse(List sql, String dialect) {
    if (loadError.isPresent()) {
      // TODO: pass error
      return Optional.empty();
    }
    try {
      return Optional.of(parse(sql, dialect, null));
    } catch (RuntimeException e) {
      return Optional.empty();
    }
  }

  public static Optional parse(List sql) {
    if (loadError.isPresent()) {
      // TODO: pass error
      return Optional.empty();
    }
    try {
      SqlMeta x = parse(sql, null, null);
      return Optional.ofNullable(x);
    } catch (RuntimeException e) {
      return Optional.empty();
    }
  }

  public static native String provider();

  public static Optional loadError = Optional.empty();

  private static void loadNativeLibrary(String libName) throws IOException {
    String fullName = "io/openlineage/sql/" + libName;

    URL url = OpenLineageSql.class.getResource("/" + fullName);
    if (url == null) {
      throw new IOException("Library not found in resources.");
    }

    File tmpDir = Files.createTempDirectory("native-lib").toFile();
    tmpDir.deleteOnExit();
    File nativeLib = new File(tmpDir, libName);
    nativeLib.deleteOnExit();

    try (InputStream in = url.openStream()) {
      Files.copy(in, nativeLib.toPath());
    }

    System.load(nativeLib.getAbsolutePath());
  }

  static {
    String libName = "libopenlineage_sql_java";
    if (SystemUtils.IS_OS_MAC_OSX && SystemUtils.OS_ARCH.equals("aarch64")) {
      libName += "_arm64.dylib";
    } else if (SystemUtils.IS_OS_MAC_OSX) {
      libName += ".dylib";
    } else if (SystemUtils.IS_OS_LINUX && SystemUtils.OS_ARCH.equals("aarch64")) {
      libName += "_aarch64.so";
    } else if (SystemUtils.IS_OS_LINUX && SystemUtils.OS_ARCH.equals("amd64")) {
      libName += "_x86_64.so";
    } else {
      loadError = Optional.of("Cannot link native library: unsupported OS");
    }

    try {
      loadNativeLibrary(libName);
    } catch (IOException e) {
      loadError =
          Optional.of(
              String.format("Error extracting native library '%s': %s", libName, e.getMessage()));
    }

    if (loadError.isPresent()) {
      System.err.println(loadError.get());
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy