Skip to content

Commit

Permalink
[Legend SQL] Support second format of transaction isolation query (#3274
Browse files Browse the repository at this point in the history
)

* Support second format of transaction isolation query

* Add support for pg_type system schema
  • Loading branch information
gs-manvig authored Nov 26, 2024
1 parent 9cb8bf1 commit c177652
Show file tree
Hide file tree
Showing 7 changed files with 334 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
import org.finos.legend.engine.language.sql.grammar.from.antlr4.SqlBaseParserBaseVisitor;
import org.finos.legend.engine.postgres.handler.SessionHandler;
import org.finos.legend.engine.postgres.handler.empty.EmptySessionHandler;
import org.finos.legend.engine.postgres.handler.txn.TxnIsolationSessionHandler;
import org.finos.legend.engine.protocol.sql.metamodel.QualifiedName;

public class ExecutionDispatcher extends SqlBaseParserBaseVisitor<SessionHandler>
{
private static final TableNameExtractor EXTRACTOR = new TableNameExtractor();
private static final SessionHandler EMPTY_SESSION_HANDLER = new EmptySessionHandler();
private static final SessionHandler TXN_ISOLATION_HANDLER = new TxnIsolationSessionHandler();
private final SessionHandler dataSessionHandler;
private final SessionHandler metaDataSessionHandler;

Expand All @@ -51,8 +53,7 @@ public SessionHandler visitSet(SqlBaseParser.SetContext ctx)
@Override
public SessionHandler visitShowTransaction(SqlBaseParser.ShowTransactionContext ctx)
{
// TODO: Handle show transaction instead of routing to metadata session handler
return metaDataSessionHandler;
return TXN_ISOLATION_HANDLER;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public enum SystemSchemas
{

INFORMATION_SCHEMA("information_schema"),
PG_CATALOG("pg_catalog");
PG_CATALOG("pg_catalog"),
PG_TYPE("pg_type");

private final String schemaName;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// Copyright 2023 Goldman Sachs
//
// 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 org.finos.legend.engine.postgres.handler.txn;

import org.finos.legend.engine.postgres.handler.PostgresPreparedStatement;
import org.finos.legend.engine.postgres.handler.PostgresResultSet;
import org.finos.legend.engine.postgres.handler.PostgresResultSetMetaData;

import java.sql.ParameterMetaData;
import java.sql.Types;

public class TxnIsolationPreparedStatement implements PostgresPreparedStatement
{
private boolean isComplete = false;
static final String READ_COMMITTED = "read committed";
static final PostgresResultSetMetaData TXN_ISOLATION_RS_META_DATA = new PostgresResultSetMetaData()
{
@Override
public int getColumnCount()
{
return 1;
}

@Override
public String getColumnName(int i)
{
return "transaction_isolation";
}

@Override
public int getColumnType(int i)
{
return Types.VARCHAR;
}

@Override
public int getScale(int i)
{
return 0;
}
};

@Override
public void setObject(int i, Object o)
{
// empty statement doesn't require objects to be set
}

@Override
public PostgresResultSetMetaData getMetaData()
{
return TXN_ISOLATION_RS_META_DATA;
}

@Override
public ParameterMetaData getParameterMetaData()
{
return null;
}

@Override
public void close() throws Exception
{
// txn isolation handler doesn't require closure
}

@Override
public void setMaxRows(int maxRows)
{
// txn isolation handler doesn't require max rows
}

@Override
public int getMaxRows()
{
return 0;
}

@Override
public boolean isExecuted()
{
return true;
}

@Override
public boolean execute()
{
return true;
}

@Override
public PostgresResultSet getResultSet()
{
return new PostgresResultSet()
{
@Override
public PostgresResultSetMetaData getMetaData()
{
return TXN_ISOLATION_RS_META_DATA;
}

@Override
public Object getObject(int i)
{
return READ_COMMITTED;
}

@Override
public boolean next()
{
if (!isComplete)
{
isComplete = true;
return true;
}
else
{
return false;
}
}

@Override
public void close()
{
// txn isolation result set doesn't require closure
}
};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2023 Goldman Sachs
//
// 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 org.finos.legend.engine.postgres.handler.txn;

import org.finos.legend.engine.postgres.handler.PostgresPreparedStatement;
import org.finos.legend.engine.postgres.handler.PostgresStatement;
import org.finos.legend.engine.postgres.handler.SessionHandler;

public class TxnIsolationSessionHandler implements SessionHandler
{
@Override
public PostgresPreparedStatement prepareStatement(String query)
{
return new TxnIsolationPreparedStatement();
}

@Override
public PostgresStatement createStatement()
{
return new TxnIsolationStatement();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2023 Goldman Sachs
//
// 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 org.finos.legend.engine.postgres.handler.txn;

import org.finos.legend.engine.postgres.handler.PostgresResultSet;
import org.finos.legend.engine.postgres.handler.PostgresResultSetMetaData;
import org.finos.legend.engine.postgres.handler.PostgresStatement;

import static org.finos.legend.engine.postgres.handler.txn.TxnIsolationPreparedStatement.READ_COMMITTED;
import static org.finos.legend.engine.postgres.handler.txn.TxnIsolationPreparedStatement.TXN_ISOLATION_RS_META_DATA;

public class TxnIsolationStatement implements PostgresStatement
{
private boolean isComplete = false;

@Override
public boolean execute(String query)
{
return true;
}

@Override
public PostgresResultSet getResultSet()
{
return new PostgresResultSet()
{
@Override
public PostgresResultSetMetaData getMetaData()
{
return TXN_ISOLATION_RS_META_DATA;
}

@Override
public Object getObject(int i)
{
return READ_COMMITTED;
}

@Override
public boolean next()
{
if (!isComplete)
{
isComplete = true;
return true;
}
else
{
return false;
}
}

@Override
public void close()
{
// txn isolation result set doesn't require closure
}
};
}

@Override
public void close() throws Exception
{
// txn isolation handler doesn't require closure
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.finos.legend.engine.postgres.handler.PostgresStatement;
import org.finos.legend.engine.postgres.handler.SessionHandler;
import org.finos.legend.engine.postgres.handler.empty.EmptySessionHandler;
import org.finos.legend.engine.postgres.handler.txn.TxnIsolationSessionHandler;
import org.junit.Assert;
import org.junit.Test;

Expand All @@ -42,6 +43,12 @@ public void testSelectInformationSchema()
assertMetadataSessionHandler("SELECT * FROM information_schema.TABLES");
}

@Test
public void testSelectPgType()
{
assertMetadataSessionHandler("SELECT oid, typbasetype FROM pg_type");
}

@Test
public void testSelectPgCatalog()
{
Expand Down Expand Up @@ -79,7 +86,8 @@ public void testSelectTableName()
@Test
public void testShowTxnLevel()
{
assertMetadataSessionHandler("SHOW TRANSACTION ISOLATION LEVEL");
assertTxnIsoSessionHandler("SHOW TRANSACTION ISOLATION LEVEL");
assertTxnIsoSessionHandler("SHOW transaction_isolation");
}

private static void assertEmptySessionHandler(String query)
Expand All @@ -94,6 +102,12 @@ private static void assertMetadataSessionHandler(String query)
Assert.assertSame(metadataSessionHandler, sessionHandler);
}

private static void assertTxnIsoSessionHandler(String query)
{
SessionHandler sessionHandler = getSessionHandler(query);
Assert.assertTrue(sessionHandler instanceof TxnIsolationSessionHandler);
}

private static void assertDataSessionHandler(String query)
{
SessionHandler sessionHandler = getSessionHandler(query);
Expand Down
Loading

0 comments on commit c177652

Please sign in to comment.